E5Graphics/E5GraphicsView.py

changeset 945
8cd4d08fa9f6
parent 791
9ec2ac20e54e
child 1112
8a7d1b9d18db
equal deleted inserted replaced
944:1b59c4ba121e 945:8cd4d08fa9f6
11 11
12 from PyQt4.QtCore import * 12 from PyQt4.QtCore import *
13 from PyQt4.QtGui import * 13 from PyQt4.QtGui import *
14 14
15 import Preferences 15 import Preferences
16
16 17
17 class E5GraphicsView(QGraphicsView): 18 class E5GraphicsView(QGraphicsView):
18 """ 19 """
19 Class implementing a graphics view. 20 Class implementing a graphics view.
20 """ 21 """
21 def __init__(self, scene, parent = None): 22 def __init__(self, scene, parent=None):
22 """ 23 """
23 Constructor 24 Constructor
24 25
25 @param scene reference to the scene object (QGraphicsScene) 26 @param scene reference to the scene object (QGraphicsScene)
26 @param parent parent widget (QWidget) 27 @param parent parent widget (QWidget)
90 91
91 @return current zoom factor (float) 92 @return current zoom factor (float)
92 """ 93 """
93 return self.matrix().m11() 94 return self.matrix().m11()
94 95
95 def resizeScene(self, amount, isWidth = True): 96 def resizeScene(self, amount, isWidth=True):
96 """ 97 """
97 Public method to resize the scene. 98 Public method to resize the scene.
98 99
99 @param isWidth flag indicating width is to be resized (boolean) 100 @param isWidth flag indicating width is to be resized (boolean)
100 @param amount size increment (integer) 101 @param amount size increment (integer)
124 rect = self.scene().sceneRect() 125 rect = self.scene().sceneRect()
125 rect.setHeight(height) 126 rect.setHeight(height)
126 rect.setWidth(width) 127 rect.setWidth(width)
127 self.setSceneRect(rect) 128 self.setSceneRect(rect)
128 129
129 def _getDiagramRect(self, border = 0): 130 def _getDiagramRect(self, border=0):
130 """ 131 """
131 Protected method to calculate the minimum rectangle fitting the diagram. 132 Protected method to calculate the minimum rectangle fitting the diagram.
132 133
133 @param border border width to include in the calculation (integer) 134 @param border border width to include in the calculation (integer)
134 @return the minimum rectangle (QRectF) 135 @return the minimum rectangle (QRectF)
139 endy = 0 140 endy = 0
140 items = self.filteredItems(list(self.scene().items())) 141 items = self.filteredItems(list(self.scene().items()))
141 for itm in items: 142 for itm in items:
142 rect = itm.sceneBoundingRect() 143 rect = itm.sceneBoundingRect()
143 itmEndX = rect.x() + rect.width() 144 itmEndX = rect.x() + rect.width()
144 itmEndY = rect.y()+ rect.height() 145 itmEndY = rect.y() + rect.height()
145 itmStartX = rect.x() 146 itmStartX = rect.x()
146 itmStartY = rect.y() 147 itmStartY = rect.y()
147 if startx >= itmStartX: 148 if startx >= itmStartX:
148 startx = itmStartX 149 startx = itmStartX
149 if starty >= itmStartY: 150 if starty >= itmStartY:
158 endx += border 159 endx += border
159 endy += border 160 endy += border
160 161
161 return QRectF(startx, starty, endx - startx + 1, endy - starty + 1) 162 return QRectF(startx, starty, endx - startx + 1, endy - starty + 1)
162 163
163 def _getDiagramSize(self, border = 0): 164 def _getDiagramSize(self, border=0):
164 """ 165 """
165 Protected method to calculate the minimum size fitting the diagram. 166 Protected method to calculate the minimum size fitting the diagram.
166 167
167 @param border border width to include in the calculation (integer) 168 @param border border width to include in the calculation (integer)
168 @return the minimum size (QSizeF) 169 @return the minimum size (QSizeF)
171 endy = 0 172 endy = 0
172 items = self.filteredItems(list(self.scene().items())) 173 items = self.filteredItems(list(self.scene().items()))
173 for itm in items: 174 for itm in items:
174 rect = itm.sceneBoundingRect() 175 rect = itm.sceneBoundingRect()
175 itmEndX = rect.x() + rect.width() 176 itmEndX = rect.x() + rect.width()
176 itmEndY = rect.y()+ rect.height() 177 itmEndY = rect.y() + rect.height()
177 if endx <= itmEndX: 178 if endx <= itmEndX:
178 endx = itmEndX 179 endx = itmEndX
179 if endy <= itmEndY: 180 if endy <= itmEndY:
180 endy = itmEndY 181 endy = itmEndY
181 if border: 182 if border:
182 endx += border 183 endx += border
183 endy += border 184 endy += border
184 185
185 return QSizeF(endx + 1, endy + 1) 186 return QSizeF(endx + 1, endy + 1)
186 187
187 def __getDiagram(self, rect, format = "PNG", filename = None): 188 def __getDiagram(self, rect, format="PNG", filename=None):
188 """ 189 """
189 Private method to retrieve the diagram from the scene fitting it 190 Private method to retrieve the diagram from the scene fitting it
190 in the minimum rectangle. 191 in the minimum rectangle.
191 192
192 @param rect minimum rectangle fitting the diagram (QRectF) 193 @param rect minimum rectangle fitting the diagram (QRectF)
193 @param format format for the image file (string) 194 @param format format for the image file (string)
194 @param filename name of the file for non pixmaps (string) 195 @param filename name of the file for non pixmaps (string)
220 for item in selectedItems: 221 for item in selectedItems:
221 item.setSelected(True) 222 item.setSelected(True)
222 223
223 return paintDevice 224 return paintDevice
224 225
225 def saveImage(self, filename, format = "PNG"): 226 def saveImage(self, filename, format="PNG"):
226 """ 227 """
227 Public method to save the scene to a file. 228 Public method to save the scene to a file.
228 229
229 @param filename name of the file to write the image to (string) 230 @param filename name of the file to write the image to (string)
230 @param format format for the image file (string) 231 @param format format for the image file (string)
231 @return flag indicating success (boolean) 232 @return flag indicating success (boolean)
232 """ 233 """
233 rect = self._getDiagramRect(self.border) 234 rect = self._getDiagramRect(self.border)
234 if format == "SVG": 235 if format == "SVG":
235 self.__getDiagram(rect, format = format, filename = filename) 236 self.__getDiagram(rect, format=format, filename=filename)
236 return True 237 return True
237 else: 238 else:
238 pixmap = self.__getDiagram(rect) 239 pixmap = self.__getDiagram(rect)
239 return pixmap.save(filename, format) 240 return pixmap.save(filename, format)
240 241
241 def printDiagram(self, printer, diagramName = ""): 242 def printDiagram(self, printer, diagramName=""):
242 """ 243 """
243 Public method to print the diagram. 244 Public method to print the diagram.
244 245
245 @param printer reference to a ready configured printer object (QPrinter) 246 @param printer reference to a ready configured printer object (QPrinter)
246 @param diagramName name of the diagram (string) 247 @param diagramName name of the diagram (string)
312 # write a foot note 313 # write a foot note
313 s = self.trUtf8("Diagram: {0}, Page {1}").format(diagramName, page + 1) 314 s = self.trUtf8("Diagram: {0}, Page {1}").format(diagramName, page + 1)
314 tc = QColor(50, 50, 50) 315 tc = QColor(50, 50, 50)
315 painter.setPen(tc) 316 painter.setPen(tc)
316 painter.drawRect(marginX, marginY, width, height) 317 painter.drawRect(marginX, marginY, width, height)
317 painter.drawLine(marginX, marginY + height + 2, 318 painter.drawLine(marginX, marginY + height + 2,
318 marginX + width, marginY + height + 2) 319 marginX + width, marginY + height + 2)
319 painter.setFont(font) 320 painter.setFont(font)
320 painter.drawText(marginX, marginY + height + 4, width, 321 painter.drawText(marginX, marginY + height + 4, width,
321 fontHeight, Qt.AlignRight, s) 322 fontHeight, Qt.AlignRight, s)
322 if not finishX or not finishY: 323 if not finishX or not finishY:

eric ide

mercurial