|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2008 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog showing a SVG graphic. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import * |
|
11 from PyQt4.QtGui import * |
|
12 from PyQt4.QtSvg import QSvgWidget |
|
13 |
|
14 from ZoomDialog import ZoomDialog |
|
15 |
|
16 import UI.Config |
|
17 |
|
18 import Preferences |
|
19 |
|
20 class SvgDiagram(QMainWindow): |
|
21 """ |
|
22 Class implementing a dialog showing a SVG graphic. |
|
23 """ |
|
24 def __init__(self, svgFile, parent = None, name = None): |
|
25 """ |
|
26 Constructor |
|
27 |
|
28 @param svgFile filename of a SVG graphics file to show (string) |
|
29 @param parent parent widget of the view (QWidget) |
|
30 @param name name of the view widget (string) |
|
31 """ |
|
32 QMainWindow.__init__(self, parent) |
|
33 if name: |
|
34 self.setObjectName(name) |
|
35 else: |
|
36 self.setObjectName("SvgDiagram") |
|
37 self.setWindowTitle(self.trUtf8("SVG-Viewer")) |
|
38 |
|
39 self.svgWidget = QSvgWidget() |
|
40 self.svgWidget.setObjectName("svgWidget") |
|
41 self.svgWidget.setBackgroundRole(QPalette.Base) |
|
42 self.svgWidget.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored) |
|
43 |
|
44 self.svgView = QScrollArea() |
|
45 self.svgView.setObjectName("svgView") |
|
46 self.svgView.setBackgroundRole(QPalette.Dark) |
|
47 self.svgView.setWidget(self.svgWidget) |
|
48 |
|
49 self.setCentralWidget(self.svgView) |
|
50 |
|
51 # polish up the dialog |
|
52 self.resize(QSize(800, 600).expandedTo(self.minimumSizeHint())) |
|
53 |
|
54 self.zoom = 1.0 |
|
55 self.svgFile = svgFile |
|
56 self.svgWidget.load(self.svgFile) |
|
57 self.svgWidget.resize(self.svgWidget.renderer().defaultSize()) |
|
58 |
|
59 self.__initActions() |
|
60 self.__initContextMenu() |
|
61 self.__initToolBars() |
|
62 |
|
63 def __initActions(self): |
|
64 """ |
|
65 Private method to initialize the view actions. |
|
66 """ |
|
67 self.closeAct = \ |
|
68 QAction(UI.PixmapCache.getIcon("close.png"), |
|
69 self.trUtf8("Close"), self) |
|
70 self.connect(self.closeAct, SIGNAL("triggered()"), self.close) |
|
71 |
|
72 self.printAct = \ |
|
73 QAction(UI.PixmapCache.getIcon("print.png"), |
|
74 self.trUtf8("Print"), self) |
|
75 self.connect(self.printAct, SIGNAL("triggered()"), self.__printDiagram) |
|
76 |
|
77 self.printPreviewAct = \ |
|
78 QAction(UI.PixmapCache.getIcon("printPreview.png"), |
|
79 self.trUtf8("Print Preview"), self) |
|
80 self.connect(self.printPreviewAct, SIGNAL("triggered()"), |
|
81 self.__printPreviewDiagram) |
|
82 |
|
83 self.zoomInAct = \ |
|
84 QAction(UI.PixmapCache.getIcon("zoomIn.png"), |
|
85 self.trUtf8("Zoom in"), self) |
|
86 self.connect(self.zoomInAct, SIGNAL("triggered()"), self.__zoomIn) |
|
87 |
|
88 self.zoomOutAct = \ |
|
89 QAction(UI.PixmapCache.getIcon("zoomOut.png"), |
|
90 self.trUtf8("Zoom out"), self) |
|
91 self.connect(self.zoomOutAct, SIGNAL("triggered()"), self.__zoomOut) |
|
92 |
|
93 self.zoomAct = \ |
|
94 QAction(UI.PixmapCache.getIcon("zoomTo.png"), |
|
95 self.trUtf8("Zoom..."), self) |
|
96 self.connect(self.zoomAct, SIGNAL("triggered()"), self.__zoom) |
|
97 |
|
98 self.zoomResetAct = \ |
|
99 QAction(UI.PixmapCache.getIcon("zoomReset.png"), |
|
100 self.trUtf8("Zoom reset"), self) |
|
101 self.connect(self.zoomResetAct, SIGNAL("triggered()"), self.__zoomReset) |
|
102 |
|
103 def __initContextMenu(self): |
|
104 """ |
|
105 Private method to initialize the context menu. |
|
106 """ |
|
107 self.__menu = QMenu(self) |
|
108 self.__menu.addAction(self.closeAct) |
|
109 self.__menu.addSeparator() |
|
110 self.__menu.addAction(self.printPreviewAct) |
|
111 self.__menu.addAction(self.printAct) |
|
112 self.__menu.addSeparator() |
|
113 self.__menu.addAction(self.zoomInAct) |
|
114 self.__menu.addAction(self.zoomOutAct) |
|
115 self.__menu.addAction(self.zoomAct) |
|
116 self.__menu.addAction(self.zoomResetAct) |
|
117 |
|
118 self.setContextMenuPolicy(Qt.CustomContextMenu) |
|
119 self.connect(self, SIGNAL('customContextMenuRequested(const QPoint &)'), |
|
120 self.__showContextMenu) |
|
121 |
|
122 def __showContextMenu(self, coord): |
|
123 """ |
|
124 Private slot to show the context menu of the listview. |
|
125 |
|
126 @param coord the position of the mouse pointer (QPoint) |
|
127 """ |
|
128 self.__menu.popup(self.mapToGlobal(coord)) |
|
129 |
|
130 def __initToolBars(self): |
|
131 """ |
|
132 Private method to populate the toolbars with our actions. |
|
133 """ |
|
134 self.windowToolBar = QToolBar(self.trUtf8("Window"), self) |
|
135 self.windowToolBar.setIconSize(UI.Config.ToolBarIconSize) |
|
136 self.windowToolBar.addAction(self.closeAct) |
|
137 |
|
138 self.graphicsToolBar = QToolBar(self.trUtf8("Graphics"), self) |
|
139 self.graphicsToolBar.setIconSize(UI.Config.ToolBarIconSize) |
|
140 self.graphicsToolBar.addAction(self.printPreviewAct) |
|
141 self.graphicsToolBar.addAction(self.printAct) |
|
142 self.graphicsToolBar.addSeparator() |
|
143 self.graphicsToolBar.addAction(self.zoomInAct) |
|
144 self.graphicsToolBar.addAction(self.zoomOutAct) |
|
145 self.graphicsToolBar.addAction(self.zoomAct) |
|
146 self.graphicsToolBar.addAction(self.zoomResetAct) |
|
147 |
|
148 self.addToolBar(Qt.TopToolBarArea, self.windowToolBar) |
|
149 self.addToolBar(Qt.TopToolBarArea, self.graphicsToolBar) |
|
150 |
|
151 def getDiagramName(self): |
|
152 """ |
|
153 Method to retrieve a name for the diagram. |
|
154 |
|
155 @return name for the diagram |
|
156 """ |
|
157 return self.svgFile |
|
158 |
|
159 ############################################################################ |
|
160 ## Private menu handling methods below. |
|
161 ############################################################################ |
|
162 |
|
163 def __adjustScrollBar(self, scrollBar, factor): |
|
164 """ |
|
165 Private method to adjust a scrollbar by a certain factor. |
|
166 |
|
167 @param scrollBar reference to the scrollbar object (QScrollBar) |
|
168 @param factor factor to adjust by (float) |
|
169 """ |
|
170 scrollBar.setValue(int(factor * scrollBar.value() |
|
171 + ((factor - 1) * scrollBar.pageStep()/2))) |
|
172 |
|
173 def __doZoom(self, factor): |
|
174 """ |
|
175 Private method to perform the zooming. |
|
176 |
|
177 @param factor zoom factor (float) |
|
178 """ |
|
179 self.zoom *= factor |
|
180 self.svgWidget.resize(self.zoom * self.svgWidget.sizeHint()) |
|
181 |
|
182 self.__adjustScrollBar(self.svgView.horizontalScrollBar(), factor) |
|
183 self.__adjustScrollBar(self.svgView.verticalScrollBar(), factor) |
|
184 |
|
185 def __zoomIn(self): |
|
186 """ |
|
187 Private method to handle the zoom in context menu entry. |
|
188 """ |
|
189 self.__doZoom(1.25) |
|
190 |
|
191 def __zoomOut(self): |
|
192 """ |
|
193 Private method to handle the zoom out context menu entry. |
|
194 """ |
|
195 self.__doZoom(0.8) |
|
196 |
|
197 def __zoomReset(self): |
|
198 """ |
|
199 Private method to handle the reset zoom context menu entry. |
|
200 """ |
|
201 self.zoom = 1.0 |
|
202 self.svgWidget.adjustSize() |
|
203 |
|
204 def __zoom(self): |
|
205 """ |
|
206 Private method to handle the zoom context menu action. |
|
207 """ |
|
208 dlg = ZoomDialog(self.zoom, self) |
|
209 if dlg.exec_() == QDialog.Accepted: |
|
210 zoom = dlg.getZoomSize() |
|
211 factor = zoom / self.zoom |
|
212 self.__doZoom(factor) |
|
213 |
|
214 def __printDiagram(self): |
|
215 """ |
|
216 Private slot called to print the diagram. |
|
217 """ |
|
218 printer = QPrinter(mode = QPrinter.ScreenResolution) |
|
219 printer.setFullPage(True) |
|
220 if Preferences.getPrinter("ColorMode"): |
|
221 printer.setColorMode(QPrinter.Color) |
|
222 else: |
|
223 printer.setColorMode(QPrinter.GrayScale) |
|
224 if Preferences.getPrinter("FirstPageFirst"): |
|
225 printer.setPageOrder(QPrinter.FirstPageFirst) |
|
226 else: |
|
227 printer.setPageOrder(QPrinter.LastPageFirst) |
|
228 printer.setPrinterName(Preferences.getPrinter("PrinterName")) |
|
229 |
|
230 printDialog = QPrintDialog(printer, self) |
|
231 if printDialog.exec_(): |
|
232 self.__print(printer) |
|
233 |
|
234 def __printPreviewDiagram(self): |
|
235 """ |
|
236 Private slot called to show a print preview of the diagram. |
|
237 """ |
|
238 from PyQt4.QtGui import QPrintPreviewDialog |
|
239 |
|
240 printer = QPrinter(mode = QPrinter.ScreenResolution) |
|
241 printer.setFullPage(True) |
|
242 if Preferences.getPrinter("ColorMode"): |
|
243 printer.setColorMode(QPrinter.Color) |
|
244 else: |
|
245 printer.setColorMode(QPrinter.GrayScale) |
|
246 if Preferences.getPrinter("FirstPageFirst"): |
|
247 printer.setPageOrder(QPrinter.FirstPageFirst) |
|
248 else: |
|
249 printer.setPageOrder(QPrinter.LastPageFirst) |
|
250 printer.setPrinterName(Preferences.getPrinter("PrinterName")) |
|
251 |
|
252 preview = QPrintPreviewDialog(printer, self) |
|
253 self.connect(preview, SIGNAL("paintRequested(QPrinter*)"), self.__print) |
|
254 preview.exec_() |
|
255 |
|
256 def __print(self, printer): |
|
257 """ |
|
258 Private slot to the actual printing. |
|
259 |
|
260 @param printer reference to the printer object (QPrinter) |
|
261 """ |
|
262 painter = QPainter() |
|
263 painter.begin(printer) |
|
264 |
|
265 # calculate margin and width of printout |
|
266 font = QFont("times", 10) |
|
267 painter.setFont(font) |
|
268 fm = painter.fontMetrics() |
|
269 fontHeight = fm.lineSpacing() |
|
270 marginX = printer.pageRect().x() - printer.paperRect().x() |
|
271 marginX = \ |
|
272 Preferences.getPrinter("LeftMargin") * int(printer.resolution() / 2.54) \ |
|
273 - marginX |
|
274 marginY = printer.pageRect().y() - printer.paperRect().y() |
|
275 marginY = \ |
|
276 Preferences.getPrinter("TopMargin") * int(printer.resolution() / 2.54) \ |
|
277 - marginY |
|
278 |
|
279 width = printer.width() - marginX \ |
|
280 - Preferences.getPrinter("RightMargin") * int(printer.resolution() / 2.54) |
|
281 height = printer.height() - fontHeight - 4 - marginY \ |
|
282 - Preferences.getPrinter("BottomMargin") * int(printer.resolution() / 2.54) |
|
283 |
|
284 # write a foot note |
|
285 s = self.trUtf8("Diagram: {0}").format(self.getDiagramName()) |
|
286 tc = QColor(50, 50, 50) |
|
287 painter.setPen(tc) |
|
288 painter.drawRect(marginX, marginY, width, height) |
|
289 painter.drawLine(marginX, marginY + height + 2, |
|
290 marginX + width, marginY + height + 2) |
|
291 painter.setFont(font) |
|
292 painter.drawText(marginX, marginY + height + 4, width, |
|
293 fontHeight, Qt.AlignRight, s) |
|
294 |
|
295 # render the diagram |
|
296 painter.setViewport(marginX, marginY, width, height) |
|
297 self.svgWidget.renderer().render(painter) |
|
298 painter.end() |