SIP4 python bindings for TQt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qmag.py 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #!/usr/bin/env python
  2. #****************************************************************************
  3. #** $Id: qmag.py,v 1.1 2002/06/20 18:52:31 phil Exp $
  4. #**
  5. #** Copyright (C) 1992-1998 Troll Tech AS. All rights reserved.
  6. #**
  7. #** This file is part of an example program for PyTQt. This example
  8. #** program may be used, distributed and modified without limitation.
  9. #**
  10. #*****************************************************************************/
  11. import sys
  12. from python_tqt.qt import *
  13. TRUE = 1
  14. FALSE = 0
  15. zoomfactors = ["100%","200%","300%","400%","500%","600%","700%","800%","1600%"]
  16. refreshrates = [
  17. "No autorefresh", "50 per second", "4 per second", "3 per second",
  18. "2 per second", "Every second", "Every two seconds", "Every three seconds",
  19. "Every five seconds", "Every ten seconds"]
  20. timer = (0, 20, 250, 333, 500, 1000, 2000, 3000, 5000, 10000)
  21. class MagWidget(TQWidget):
  22. def __init__(self, tqApp, parent = None, name = None):
  23. self.qApp = tqApp
  24. TQWidget.__init__(self, parent, name)
  25. self.pm = TQPixmap() # pixmap magnified
  26. self.p = TQPixmap() # pixmap
  27. self.image = TQImage() # image of pixmap (for RGB)
  28. self.z = 0 # zoom factor
  29. self.r = 0 # autorefresh rate (index into refreshrates)
  30. self.grabbing = FALSE # TRUE if currently grabbing
  31. self.grabx = -1
  32. self.graby = -1
  33. self.zoom = TQComboBox(FALSE, self)
  34. self.zoom.insertStrList(zoomfactors, len(zoomfactors))
  35. self.connect(self.zoom, SIGNAL("activated(int)"),
  36. self.setZoom)
  37. self.refresh = TQComboBox(FALSE, self)
  38. self.refresh.insertStrList(refreshrates, len(refreshrates))
  39. self.connect(self.refresh, SIGNAL("activated(int)"),
  40. self.setRefresh)
  41. x = 0
  42. w = 0
  43. h = 20
  44. for s in zoomfactors:
  45. cw = self.zoom.fontMetrics().width(s)
  46. w = max(cw, w)
  47. self.zoom.setGeometry(x, 2, w + 30, h)
  48. x = w + 34
  49. w = 0
  50. for s in refreshrates:
  51. cw = self.refresh.fontMetrics().width(s)
  52. w = max(cw, w)
  53. self.refresh.setGeometry(x, 2, w + 30, h)
  54. self.saveButton = TQPushButton(self)
  55. self.connect(self.saveButton, SIGNAL("clicked()"), self.save)
  56. self.saveButton.setText("Save")
  57. self.saveButton.setGeometry(x + w + 30 + 2, 2,
  58. 10 + self.saveButton.fontMetrics().width("Save"), h)
  59. self.quitButton = TQPushButton(self)
  60. self.connect(self.quitButton, SIGNAL("clicked()"), self.qApp, SLOT("quit()"))
  61. self.quitButton.setText("Quit")
  62. self.quitButton.setGeometry(self.saveButton.geometry().right() + 2, 2,
  63. 10 + self.quitButton.fontMetrics().width("Quit"), h)
  64. self.rgb = TQLabel(self)
  65. self.rgb.setText("")
  66. self.rgb.setAlignment(TQt.AlignVCenter)
  67. self.rgb.resize(self.width(), self.rgb.fontMetrics().height() + 4)
  68. self.yoffset = self.zoom.height() + 4 + self.rgb.height()
  69. self.setMinimumSize(self.quitButton.geometry().topRight().x() + 2,
  70. self.yoffset + 20)
  71. w = self.quitButton.geometry().topRight().x() + 2
  72. self.resize(w, w)
  73. self.setMouseTracking(TRUE)
  74. self.grabx = self.qApp.desktop().width() / 2
  75. self.graby = self.qApp.desktop().height() / 2
  76. self.grabAround(TQPoint(self.grabx, self.graby))
  77. #self.zoom.setCurrentItem(1) # grabAround sets zoom factor
  78. #self.setZoom(1)
  79. self.refresh.setCurrentItem(5)
  80. self.setRefresh(5)
  81. def setZoom(self, index):
  82. if index == 8:
  83. self.z = 16
  84. else:
  85. self.z = index + 1
  86. self.grab()
  87. def setRefresh(self, index):
  88. self.r = index
  89. self.killTimers()
  90. if index and not self.grabbing:
  91. #print "st:", timer[index]
  92. self.startTimer(timer[index])
  93. def save(self):
  94. if not self.p.isNull():
  95. self.killTimers()
  96. fn = TQFileDialog.getSaveFileName()
  97. if not fn.isEmpty():
  98. self.p.save(fn, "BMP")
  99. if self.r:
  100. self.startTimer(timer[self.r])
  101. def grab(self):
  102. if not self.isVisible() or self.grabx < 0 or self.graby < 0:
  103. return
  104. w = (self.width() + self.z - 1) / self.z
  105. h = (self.height() + self.z - 1 - self.yoffset) / self.z
  106. if w < 1 or h < 1:
  107. return
  108. x = self.grabx - w / 2
  109. y = self.graby - h / 2
  110. if x + w > TQApplication.desktop().width():
  111. x = TQApplication.desktop().width() - w
  112. elif x < 0:
  113. x = 0
  114. if y + h > TQApplication.desktop().height():
  115. y = TQApplication.desktop().height() - h
  116. elif y < 0:
  117. y = 0
  118. self.p = TQPixmap.grabWindow(TQApplication.desktop().winId(), x, y, w, h)
  119. self.image = self.p.convertToImage()
  120. m = TQWMatrix()
  121. #print "z:", self.z
  122. m.scale(float(self.z), float(self.z))
  123. self.pm = self.p.xForm(m)
  124. self.repaint(FALSE)
  125. def paintEvent(self, e):
  126. if not self.pm.isNull():
  127. paint = TQPainter(self)
  128. paint.drawPixmap(0, self.zoom.height() + 4, self.pm,
  129. 0, 0, self.width(), self.height() - self.yoffset)
  130. def mousePressEvent(self, e):
  131. if not self.grabbing:
  132. self.grabbing = TRUE
  133. self.killTimers()
  134. self.grabMouse(TQt.crossCursor)
  135. self.grabx = -1
  136. self.graby = -1
  137. else:
  138. self.grabx = self.mapToGlobal(e.pos()).x()
  139. self.graby = self.mapToGlobal(e.pos()).y()
  140. def mouseReleaseEvent(self, e):
  141. if self.grabbing and self.grabx >= 0 and self.graby >= 0:
  142. self.grabbing = FALSE
  143. self.grabAround(e.pos())
  144. self.releaseMouse()
  145. def grabAround(self, pos):
  146. rx = self.mapToGlobal(pos).x()
  147. ry = self.mapToGlobal(pos).y()
  148. w = abs(rx - self.grabx)
  149. h = abs(ry - self.graby)
  150. if w > 10 and h > 10:
  151. pz = 1
  152. while w*pz*h*pz < self.width()*(self.height()-self.yoffset) and \
  153. w*pz < TQApplication.desktop().width() and \
  154. h*pz < TQApplication.desktop().height():
  155. pz += 1
  156. if (w*pz*h*pz - self.width()*(self.height()-self.yoffset)) > \
  157. (self.width()*(self.height()-self.yoffset) - w*(pz-1)*h*(pz-1)):
  158. pz -= 1
  159. if pz < 1:
  160. pz = 1
  161. elif pz > 8:
  162. pz = 8
  163. self.zoom.setCurrentItem(pz-1)
  164. self.z = pz
  165. self.grabx = min(rx, self.grabx) + w / 2
  166. self.graby = min(ry, self.graby) + h / 2
  167. #self.resize(w*self.z, h*self.z*self.yoffset)
  168. self.grab()
  169. def mouseMoveEvent(self, e):
  170. if self.grabbing or self.pm.isNull() or \
  171. e.pos().y() > self.height() - self.zoom.fontMetrics().height() - 4 or \
  172. e.pos().y() < self.zoom.height() + 4:
  173. self.rgb.setText("")
  174. else:
  175. x = e.pos().x() / self.z
  176. y = (e.pos().y() - self.zoom.height() - 4) / self.z
  177. pixelinfo = ""
  178. if self.image.valid(x, y):
  179. px = self.image.pixel(x, y)
  180. pixelinfo = "%3d,%3d,%3d #%02x%02x%02x" % (
  181. tqRed(px), tqGreen(px), tqBlue(px),
  182. tqRed(px), tqGreen(px), tqBlue(px))
  183. self.rgb.setText("x=%d, y=%d %s" % \
  184. (x + self.grabx, y + self.graby, pixelinfo))
  185. def focusOutEvent(self, e):
  186. self.rgb.setText("")
  187. def timerEvent(self, e):
  188. self.grab()
  189. def resizeEvent(self, e):
  190. self.rgb.setGeometry(0, self.height() - self.rgb.height(),
  191. self.width(), self.rgb.height())
  192. if __name__=='__main__':
  193. app = TQApplication( sys.argv )
  194. m = MagWidget(app)
  195. app.setMainWidget(m)
  196. m.show()
  197. app.exec_loop()