1 | /* |
---|
2 | Copyright (C) 2012 |
---|
3 | Alejandro Mujica (amujica@cenditel.gob.ve) |
---|
4 | Erwin Paredes (eparedes@cenditel.gob.ve) |
---|
5 | José Ruiz (jruiz@cenditel.gob.ve) |
---|
6 | Rodolfo Rangel (rrangel@cenditel.gob.ve) |
---|
7 | Julie Vera (jvera@cenditel.gob.ve) |
---|
8 | |
---|
9 | CENDITEL Fundación Centro Nacional de Desarrollo e Investigación en |
---|
10 | Tecnologías Libres |
---|
11 | |
---|
12 | Este programa es software libre; Usted puede usarlo bajo los términos de la |
---|
13 | licencia de software GPL versión 2.0 de la Free Software Foundation. |
---|
14 | |
---|
15 | Este programa se distribuye con la esperanza de que sea útil, pero SIN |
---|
16 | NINGUNA GARANTÍA; tampoco las implícitas garantías de MERCANTILIDAD o |
---|
17 | ADECUACIÓN A UN PROPÓSITO PARTICULAR. |
---|
18 | Consulte la licencia GPL para más detalles. Usted debe recibir una copia |
---|
19 | de la GPL junto con este programa; si no, escriba a la Free Software |
---|
20 | Foundation Inc. 51 Franklin Street,5 Piso, Boston, MA 02110-1301, USA. |
---|
21 | */ |
---|
22 | |
---|
23 | /* |
---|
24 | Este archivo contiene la implementación de la clase SvgViewer. |
---|
25 | |
---|
26 | Autor: Alejandro J. Mujica |
---|
27 | Fecha: |
---|
28 | */ |
---|
29 | |
---|
30 | # include <QSvgRenderer> |
---|
31 | # include <QProcess> |
---|
32 | |
---|
33 | # include <svgViewer.H> |
---|
34 | # include <indicatorGenerator.H> |
---|
35 | # include <simType.H> |
---|
36 | # include <configuration.H> |
---|
37 | |
---|
38 | const double SvgViewer::FACTOR_FOR_ZOOM = 0.1; |
---|
39 | |
---|
40 | SvgViewer::SvgViewer(const QString & xmlFileName, const QString & dotFileName, |
---|
41 | const QString & svgFileName, const QString & graphName, |
---|
42 | QWidget * _parent) |
---|
43 | : QWidget(_parent), layout(this), scrollArea(this), svgWidget(this), |
---|
44 | scaleFactor(1), toolBar("View", this), actPrint(this), |
---|
45 | actZoomIn(this), actZoomOut(this), actFitToWindow(this), |
---|
46 | actNormalSize(this), actSaveImage(this), actGenerateIndicators(NULL), |
---|
47 | vpfilter(scrollArea.viewport(), this), |
---|
48 | vbfilter(scrollArea.verticalScrollBar(), this), |
---|
49 | hbfilter(scrollArea.horizontalScrollBar(), this) { |
---|
50 | |
---|
51 | this->xmlFileName = xmlFileName; |
---|
52 | this->dotFileName = dotFileName; |
---|
53 | this->svgFileName = svgFileName; |
---|
54 | this->graphName = graphName; |
---|
55 | |
---|
56 | createActions(); |
---|
57 | |
---|
58 | createToolBar(); |
---|
59 | |
---|
60 | buildGraph(); |
---|
61 | |
---|
62 | initGui(); |
---|
63 | |
---|
64 | show(); |
---|
65 | } |
---|
66 | |
---|
67 | void SvgViewer::initGui() { |
---|
68 | |
---|
69 | scrollArea.setBackgroundRole(QPalette::Dark); |
---|
70 | scrollArea.setWidget(&svgWidget); |
---|
71 | scrollArea.setAlignment(Qt::AlignCenter); |
---|
72 | |
---|
73 | layout.addWidget(&toolBar); |
---|
74 | layout.addWidget(&scrollArea); |
---|
75 | setLayout(&layout); |
---|
76 | |
---|
77 | scrollArea.viewport()->installEventFilter(&vpfilter); |
---|
78 | |
---|
79 | scrollArea.horizontalScrollBar()->installEventFilter(&hbfilter); |
---|
80 | |
---|
81 | scrollArea.verticalScrollBar()->installEventFilter(&vbfilter); |
---|
82 | |
---|
83 | installEventFilter(this); |
---|
84 | |
---|
85 | connect(&svgWidget, SIGNAL(signalMouseClicked(QPoint)), |
---|
86 | this, SLOT(slotPanelMouseClicked(QPoint))); |
---|
87 | connect(&svgWidget, SIGNAL(signalMouseMoved(QPoint)), |
---|
88 | this, SLOT(slotPanelMouseMoved(QPoint))); |
---|
89 | connect(&svgWidget, SIGNAL(signalMousePressed()), |
---|
90 | this, SLOT(slotPanelMousePressed())); |
---|
91 | } |
---|
92 | |
---|
93 | void SvgViewer::createActions() { |
---|
94 | |
---|
95 | actPrint.setIcon(QIcon(":/icons/print-icon")); |
---|
96 | actPrint.setText("&Imprimir"); |
---|
97 | actPrint.setShortcut(tr("Ctrl+p")); |
---|
98 | connect(&actPrint, SIGNAL(triggered()), this, SLOT(slotPrint())); |
---|
99 | |
---|
100 | actZoomIn.setIcon(QIcon(":/icons/zoom-in-icon")); |
---|
101 | actZoomIn.setText("Acercar"); |
---|
102 | actZoomIn.setShortcut(tr("Ctrl++")); |
---|
103 | connect(&actZoomIn, SIGNAL(triggered()), this, SLOT(slotZoomIn())); |
---|
104 | |
---|
105 | actZoomOut.setIcon(QIcon(":/icons/zoom-out-icon")); |
---|
106 | actZoomOut.setText("Alejar"); |
---|
107 | actZoomOut.setShortcut(tr("Ctrl+-")); |
---|
108 | connect(&actZoomOut, SIGNAL(triggered()), this, SLOT(slotZoomOut())); |
---|
109 | |
---|
110 | actNormalSize.setIcon(QIcon(":/icons/zoom-normal-icon")); |
---|
111 | actNormalSize.setText("Tamano normal"); |
---|
112 | connect(&actNormalSize, SIGNAL(triggered()), this, SLOT(slotNormalSize())); |
---|
113 | |
---|
114 | actFitToWindow.setIcon(QIcon(":/icons/zoom-adjust-icon")); |
---|
115 | actFitToWindow.setText("Ajustar a la pantalla"); |
---|
116 | connect(&actFitToWindow, SIGNAL(triggered()), |
---|
117 | this, SLOT(slotFitToWindow())); |
---|
118 | |
---|
119 | actSaveImage.setText("Guardar imagen"); |
---|
120 | connect(&actSaveImage, SIGNAL(triggered()), this, SLOT(slotSaveImage())); |
---|
121 | } |
---|
122 | |
---|
123 | void SvgViewer::createToolBar() { |
---|
124 | |
---|
125 | toolBar.addAction(&actPrint); |
---|
126 | toolBar.addSeparator(); |
---|
127 | toolBar.addAction(&actZoomIn); |
---|
128 | toolBar.addAction(&actZoomOut); |
---|
129 | toolBar.addAction(&actNormalSize); |
---|
130 | toolBar.addAction(&actFitToWindow); |
---|
131 | toolBar.addSeparator(); |
---|
132 | toolBar.addAction(&actSaveImage); |
---|
133 | } |
---|
134 | |
---|
135 | void SvgViewer::closeEvent(QCloseEvent *) { |
---|
136 | |
---|
137 | // Empty |
---|
138 | } |
---|
139 | |
---|
140 | void SvgViewer::wheelEvent(QWheelEvent * evt) { |
---|
141 | |
---|
142 | if (not evt->modifiers().testFlag(Qt::ControlModifier)) |
---|
143 | return; |
---|
144 | |
---|
145 | if (evt->delta() > 0) |
---|
146 | slotZoomIn(); |
---|
147 | else |
---|
148 | slotZoomOut(); |
---|
149 | } |
---|
150 | |
---|
151 | bool SvgViewer::eventFilter(QObject * obj, QEvent * evt) { |
---|
152 | |
---|
153 | if (obj == this and evt->type() == QEvent::Wheel) { |
---|
154 | |
---|
155 | QWheelEvent * wevt = static_cast <QWheelEvent *>(evt); |
---|
156 | |
---|
157 | if (wevt->modifiers().testFlag(Qt::ControlModifier)) |
---|
158 | return true; |
---|
159 | } |
---|
160 | |
---|
161 | return false; |
---|
162 | } |
---|
163 | |
---|
164 | void SvgViewer::slotPanelMouseMoved(QPoint diff) { |
---|
165 | |
---|
166 | scrollArea.horizontalScrollBar()-> |
---|
167 | setValue(originalScrollPosition.x() - diff.x()); |
---|
168 | scrollArea.verticalScrollBar()-> |
---|
169 | setValue(originalScrollPosition.y() - diff.y()); |
---|
170 | } |
---|
171 | |
---|
172 | void SvgViewer::slotSaveImage() { |
---|
173 | |
---|
174 | QString fileName = QFileDialog::getSaveFileName(this, "Guardar imagen", |
---|
175 | QDir::homePath(), "*.svg"); |
---|
176 | |
---|
177 | if (fileName.isEmpty()) |
---|
178 | return; |
---|
179 | |
---|
180 | |
---|
181 | QString dot; |
---|
182 | |
---|
183 | dot.append(fileName); |
---|
184 | dot.append(".dot"); |
---|
185 | |
---|
186 | QString svg; |
---|
187 | |
---|
188 | svg.append(fileName); |
---|
189 | svg.append(".svg"); |
---|
190 | |
---|
191 | IOManager ioManager; |
---|
192 | |
---|
193 | if (graphName == IOManager::ALTER_GRAPH.c_str()) { |
---|
194 | |
---|
195 | if (SimType::getInstance().getType() == PRODUCTION_CHANGE) |
---|
196 | ioManager.generateDot<IOManager::AlterProductionNodeLabel, |
---|
197 | IOManager::AlterProductionArcLabel> |
---|
198 | (graph, dot.toStdString()); |
---|
199 | else |
---|
200 | ioManager.generateDot<IOManager::AlterPriceNodeLabel, |
---|
201 | IOManager::AlterPriceArcLabel> |
---|
202 | (graph, dot.toStdString()); |
---|
203 | } |
---|
204 | else { |
---|
205 | ioManager.generateDot<IOManager::NodeLabel, IOManager::ArcLabel> |
---|
206 | (graph, dot.toStdString()); |
---|
207 | } |
---|
208 | |
---|
209 | QStringList list; |
---|
210 | |
---|
211 | list.append(QString("-Tsvg")); |
---|
212 | list.append(dot); |
---|
213 | list.append(QString("-o")); |
---|
214 | list.append(svg); |
---|
215 | |
---|
216 | QProcess::execute(QString("dot"), list); |
---|
217 | |
---|
218 | QFile::remove(dot); |
---|
219 | } |
---|
220 | |
---|
221 | void SvgViewer::slotGenenerateIndicators() { |
---|
222 | |
---|
223 | QString path = QFileDialog::getExistingDirectory(this, |
---|
224 | "Seleccione un directorio", |
---|
225 | QDir::homePath()); |
---|
226 | |
---|
227 | if (path.isEmpty()) |
---|
228 | QMessageBox::information(this, "Información", |
---|
229 | "No se generaron indicadores"); |
---|
230 | |
---|
231 | std::unique_ptr <IndicatorGenerator> ptrIndicatorGenerator = |
---|
232 | IndicatorGeneratorFactory::getInstance().getIndicatorGenerator( |
---|
233 | SimType::getInstance().getType()); |
---|
234 | |
---|
235 | std::string msg = |
---|
236 | ptrIndicatorGenerator->generate(graph, path.toStdString(), |
---|
237 | SimType::getInstance().getYear()); |
---|
238 | |
---|
239 | QMessageBox::information(this, "Información", msg.c_str()); |
---|
240 | } |
---|
241 | |
---|
242 | void SvgViewer::resizeSvg() { |
---|
243 | |
---|
244 | svgWidget.resize(scaleFactor * originalSize); |
---|
245 | |
---|
246 | actZoomIn.setEnabled(scaleFactor < 5.0); |
---|
247 | actZoomOut.setEnabled(scaleFactor > 0.05); |
---|
248 | } |
---|
249 | |
---|
250 | void SvgViewer::init() { |
---|
251 | slotNormalSize(); |
---|
252 | originalSize = svgWidget.size(); |
---|
253 | } |
---|
254 | |
---|
255 | Graph & SvgViewer::getGraph() { |
---|
256 | return graph; |
---|
257 | } |
---|
258 | |
---|
259 | void SvgViewer::scaleImage(const double & factor) { |
---|
260 | |
---|
261 | scaleFactor *= factor; |
---|
262 | resizeSvg(); |
---|
263 | adjustSscrollBar(scrollArea.horizontalScrollBar(), factor); |
---|
264 | adjustSscrollBar(scrollArea.verticalScrollBar(), factor); |
---|
265 | } |
---|
266 | |
---|
267 | void SvgViewer::adjustSscrollBar(QScrollBar * scrollBar, |
---|
268 | const double & factor) { |
---|
269 | scrollBar->setValue(int(factor * |
---|
270 | scrollBar->value() + ((factor - 1) * |
---|
271 | scrollBar->pageStep()/2))); |
---|
272 | } |
---|
273 | |
---|
274 | void SvgViewer::load(const QString & fileName) { |
---|
275 | svgWidget.load(fileName); |
---|
276 | init(); |
---|
277 | } |
---|
278 | |
---|
279 | void SvgViewer::slotFitToWindow() { |
---|
280 | |
---|
281 | qreal sx = qreal(scrollArea.viewport()->width()) / |
---|
282 | qreal(originalSize.width()); |
---|
283 | qreal sy = qreal(scrollArea.viewport()->height()) / |
---|
284 | qreal(originalSize.height()); |
---|
285 | |
---|
286 | scaleFactor = qMin(sx, sy); |
---|
287 | |
---|
288 | resizeSvg(); |
---|
289 | } |
---|
290 | |
---|
291 | void SvgViewer::slotNormalSize() { |
---|
292 | svgWidget.adjustSize(); |
---|
293 | setScaleFactor(1.0); |
---|
294 | } |
---|
295 | |
---|
296 | void SvgViewer::slotPanelMousePressed() { |
---|
297 | originalScrollPosition.setX(scrollArea.horizontalScrollBar()->value()); |
---|
298 | originalScrollPosition.setY(scrollArea.verticalScrollBar()->value()); |
---|
299 | } |
---|
300 | |
---|
301 | void SvgViewer::slotPanelMouseClicked(QPoint pos) { |
---|
302 | |
---|
303 | for (Graph::Node_Iterator it(graph); it.has_current(); it.next()) { |
---|
304 | |
---|
305 | Graph::Node * p = it.get_current(); |
---|
306 | |
---|
307 | if (p->id == -1) |
---|
308 | continue; |
---|
309 | |
---|
310 | if (p->type == IMPORT) |
---|
311 | continue; |
---|
312 | |
---|
313 | if (SimType::getInstance().getType() == PRODUCTION_CHANGE and |
---|
314 | p->get_info()->getType() == INPUT_GOOD) |
---|
315 | continue; |
---|
316 | |
---|
317 | QString id; |
---|
318 | |
---|
319 | id.setNum(p->id); |
---|
320 | |
---|
321 | I(svgWidget.renderer()->elementExists(id)); |
---|
322 | |
---|
323 | QRectF rect = svgWidget.renderer()->boundsOnElement(id); |
---|
324 | |
---|
325 | QMatrix matrix = svgWidget.renderer()->matrixForElement(id); |
---|
326 | |
---|
327 | qreal rectX, rectY, w, h; |
---|
328 | |
---|
329 | rect.getRect(&rectX, &rectY, &w, &h); |
---|
330 | |
---|
331 | qreal x = matrix.m11() * rectX + matrix.m21() * rectY + matrix.dx(); |
---|
332 | qreal y = matrix.m22() * rectY + matrix.m12() * rectX + matrix.dy(); |
---|
333 | |
---|
334 | x *= scaleFactor; |
---|
335 | y *= scaleFactor; |
---|
336 | w *= scaleFactor; |
---|
337 | h *= scaleFactor; |
---|
338 | |
---|
339 | if (QRect(x, y, w, h).contains(pos)) { |
---|
340 | emit signalNodeClicked(p); |
---|
341 | return; |
---|
342 | } |
---|
343 | |
---|
344 | } |
---|
345 | |
---|
346 | emit signalNodeClicked(NULL); |
---|
347 | } |
---|
348 | |
---|
349 | void SvgViewer::slotZoomIn() { |
---|
350 | scaleImage(1 + FACTOR_FOR_ZOOM); |
---|
351 | } |
---|
352 | |
---|
353 | void SvgViewer::slotZoomOut() { |
---|
354 | scaleImage(1 - FACTOR_FOR_ZOOM); |
---|
355 | } |
---|
356 | |
---|
357 | void SvgViewer::slotPrint() { |
---|
358 | |
---|
359 | #ifndef QT_NO_PRINTER |
---|
360 | QPrinter printer; |
---|
361 | |
---|
362 | QPrintDialog dialog(&printer, this); |
---|
363 | |
---|
364 | if (dialog.exec()) { |
---|
365 | |
---|
366 | QPainter painter(&printer); |
---|
367 | QRect rect = painter.viewport(); |
---|
368 | QSize size = svgWidget.size(); |
---|
369 | size.scale(rect.size(), Qt::KeepAspectRatio); |
---|
370 | painter.setViewport(rect.x(), rect.y(), size.width(), size.height()); |
---|
371 | painter.setWindow(svgWidget.rect()); |
---|
372 | svgWidget.render(&painter); |
---|
373 | } |
---|
374 | #endif |
---|
375 | } |
---|
376 | |
---|
377 | void SvgViewer::buildGraph() { |
---|
378 | |
---|
379 | IOManager ioManager; |
---|
380 | |
---|
381 | QLocale currLocale(locale()); |
---|
382 | |
---|
383 | setlocale(LC_NUMERIC, "C"); |
---|
384 | |
---|
385 | ioManager.readXmlGraph(xmlFileName.toStdString(), graphName.toStdString(), |
---|
386 | graph); |
---|
387 | |
---|
388 | setLocale(currLocale); |
---|
389 | |
---|
390 | if (graphName == IOManager::ALTER_GRAPH.c_str()) { |
---|
391 | |
---|
392 | if (SimType::getInstance().getType() == PRODUCTION_CHANGE) |
---|
393 | ioManager.generateDot<IOManager::AlterProductionNodeLabel, |
---|
394 | IOManager::AlterProductionArcLabel> |
---|
395 | (graph, dotFileName.toStdString()); |
---|
396 | else |
---|
397 | ioManager.generateDot<IOManager::AlterPriceNodeLabel, |
---|
398 | IOManager::AlterPriceArcLabel> |
---|
399 | (graph, dotFileName.toStdString()); |
---|
400 | |
---|
401 | |
---|
402 | actGenerateIndicators = new QAction(this); |
---|
403 | |
---|
404 | actGenerateIndicators->setText("Generar indicadores"); |
---|
405 | |
---|
406 | toolBar.addAction(actGenerateIndicators); |
---|
407 | |
---|
408 | connect(actGenerateIndicators, SIGNAL(triggered()), |
---|
409 | this, SLOT(slotGenenerateIndicators())); |
---|
410 | |
---|
411 | } |
---|
412 | else { |
---|
413 | ioManager.generateDot<IOManager::NodeLabel, IOManager::ArcLabel> |
---|
414 | (graph, dotFileName.toStdString()); |
---|
415 | } |
---|
416 | |
---|
417 | |
---|
418 | QStringList list; |
---|
419 | |
---|
420 | list.append(QString("-Tsvg")); |
---|
421 | list.append(dotFileName); |
---|
422 | list.append(QString("-o")); |
---|
423 | list.append(svgFileName); |
---|
424 | |
---|
425 | QProcess::execute(QString("dot"), list); |
---|
426 | |
---|
427 | load(svgFileName); |
---|
428 | } |
---|