Debian Codegraph @

MainWindow.cpp

raw symbols SCIP
1 /***************************************************************************
2 * This file is part of Robocut. *
3 * Copyright (C) 2010 Tim Hutt <[email protected]> *
4 * Copyright (C) 2010 Markus Schulz <[email protected]> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
20 ***************************************************************************/
21
22 #include "MainWindow.h"
23 #include "ui_MainWindow.h"
24
25 #include "PathPaintPage.h"
26 #include "Plotter.h"
27
28 #include "ProgramOptions.h"
29
30 #include "CuttingDialog.h"
31 #include "PathSorter.h"
32
33 #include <QPainter>
34 #include <QDebug>
35
36 #include <QSvgRenderer>
37
38 #include <QMessageBox>
39 #include <QFileDialog>
40 #include <QDir>
41
42 #include <QShortcut>
43 #include <cmath>
44 #include <QFile>
45
46 #include <QEvent>
47 #include <iostream>
48 #include <QWheelEvent>
49
50 using namespace std;
51
52 const double InitialZoom = 2.0;
53
54 MainWindow::MainWindow(QWidget *parent)
55 : QMainWindow(parent), ui(new Ui::MainWindow)
56 {
57 ui->setupUi(this);
58
59 scene = new QGraphicsScene(this);
60 ui->graphicsView->setScene(scene);
61
62 ui->graphicsView->scale(InitialZoom, InitialZoom);
63
64 ui->graphicsView->viewport()->installEventFilter(this);
65
66 cutDialog = NULL;
67
68 animationTimer = new QTimer(this);
69 connect(animationTimer, SIGNAL(timeout()), SLOT(animate()));
70 cutMarker = NULL;
71
72 // Alternative zoom shortcuts
73 QShortcut* zoom_in = new QShortcut(QKeySequence("X"), this);
74 connect(zoom_in, SIGNAL(activated()), SLOT(on_actionZoom_In_triggered()));
75 QShortcut* zoom_out = new QShortcut(QKeySequence("Z"), this);
76 connect(zoom_out, SIGNAL(activated()), SLOT(on_actionZoom_Out_triggered()));
77
78 //default options if not specified on command line
79 this->show();
80 filename = ProgramOptions::Instance().getFileName();
81 if(QFile::exists(filename)) loadFile();
82 if(ProgramOptions::Instance().getStartCut() == true) on_actionCut_triggered();
83 sortFlag = ProgramOptions::Instance().getSortPath();
84 cout << ProgramOptions::Instance().getVersion().toStdString() << endl;
85 }
86
87 MainWindow::~MainWindow()
88 {
89 delete ui;
90 }
91
92 void MainWindow::on_actionOpen_triggered()
93 {
94 if (lastOpenDir.isEmpty())
95 lastOpenDir = QDir::homePath();
96
97 filename = QFileDialog::getOpenFileName(this,
98 tr("Open File"), lastOpenDir, tr("SVG Files (*.svg)"));
99 if (filename.isEmpty())
100 return;
101
102 lastOpenDir = QFileInfo(filename).absoluteDir().path();
103 loadFile();
104 }
105
106 void MainWindow::on_actionReload_triggered()
107 {
108 if(QFile::exists(filename))
109 loadFile();
110 else
111 qDebug() << "Reload failed. File missing: " << filename;
112 }
113
114 void MainWindow::on_actionIdentify_triggered()
115 {
116 // CAUTION: only call this when not cutting!
117 struct cutter_id *id = Identify();
118 cout << "Identify:";
119 if (id->usb_product_id)
120 {
121 cout << " usb=" << id->usb_vendor_id << "/" << id->usb_product_id;
122 }
123 cout << " " << id->msg << endl;
124 }
125
126
127 void MainWindow::loadFile()
128 {
129 if (filename.isEmpty())
130 return;
131
132 qDebug() << "Reading file: " << filename;
133
134 QSvgRenderer rend;
135 if (!rend.load(filename))
136 {
137 QMessageBox::critical(this, "Error loading file.", "Couldn't open the file for reading.");
138 qDebug() << "Error loading svg.";
139 return;
140 }
141
142 qDebug() << "SVG default size: " << rend.defaultSize() << endl;
143 qDebug() << "SVG view box: " << rend.viewBoxF() << endl;
144
145 // Geqt size from SVG. TODO: Sanity check.
146 // Also TODO: This won't handle offset viewboxes... need to get the offset and subtract it from
147 // all the objects.
148 mediaSize = rend.viewBoxF().size() * 25.4 / 90.0;
149
150 double ppm = 90.0/25.4; // Pixels per mm.
151
152 qDebug() << "Page size (mm): " << mediaSize << endl;
153
154 PathPaintDevice pg(mediaSize.width(), mediaSize.height(), ppm);
155 QPainter p(&pg);
156
157 rend.render(&p);
158
159 PathSorter pathsort(pg.paths(), mediaSize.height());
160 paths = pathsort.UnSort();
161 //paths = pathsort.Sort();
162 //paths = pathsort.TspSort();
163 //paths = pathsort.GroupTSP();
164 if(!ProgramOptions::Instance().getSortPath() == true)paths = pathsort.BestSort();
165 if(ProgramOptions::Instance().getTspSortPath() == true)paths = pathsort.BbSort(paths);
166 scene->clear();
167 scene->setBackgroundBrush(QBrush(Qt::lightGray));
168
169 // The page.
170 scene->addRect(0.0, 0.0, mediaSize.width(), mediaSize.height(), QPen(), QBrush(Qt::white));
171
172 QPen pen;
173 pen.setWidthF(0.0);
174 for (int i = 0, ii = 50, iii=0; i < paths.size(); ++i,ii+=10)
175 {
176 switch (iii)
177 {
178 case 0:
179 pen.setColor(QColor(ii, ii, ii));
180 break;
181 case 1:
182 pen.setColor(QColor(ii, 0, 0));
183 break;
184 case 2:
185 pen.setColor(QColor(0, ii, 0));
186 break;
187 case 3:
188 pen.setColor(QColor(0, 0, ii));
189 break;
190 case 4:
191 pen.setColor(QColor(ii, ii, 0));
192 break;
193 case 5:
194 pen.setColor(QColor(ii, 0, ii));
195 break;
196 case 6:
197 pen.setColor(QColor(0, ii, ii));
198 break;
199 }
200 QPainterPath path;
201 path.addPolygon(paths[i]);
202 scene->addPath(path, pen);
203 if(ii >= 200)
204 {
205 ii = 50;
206 iii++;
207 if(iii >=7)iii=0;
208 }
209 }
210
211 // Handle the animation. I.e. stop it.
212 // The old one was deleted when we cleared the scene.
213 cutMarker = scene->addEllipse(-1.0, -1.0, 2.0, 2.0, QPen(Qt::black), QBrush(Qt::red));
214 cutMarker->hide();
215
216 ui->actionAnimate->setChecked(false);
217
218 // Reset the viewport.
219 on_actionReset_triggered();
220 // Redraw. Probably not necessary.
221 update();
222
223 if (pg.clipped())
224 QMessageBox::warning(this, "Paths clipped", "<b>BIG FAT WARNING!</b><br><br>Some paths lay outside the 210&times;297&thinsp;mm A4 area. These have been squeezed back onto the page in a most ugly fashion, so cutting will almost certainly not do what you want.");
225
226 // Change window title and enable menu items.
227 setFileLoaded(filename);
228 }
229
230 void MainWindow::on_actionAbout_triggered()
231 {
232 QString message = "<b>" + ProgramOptions::Instance().getVersion() +
233 "</b><br><br>By Tim Hutt, &copy; 2010<br/>" +
234 "<br>Parts of the source by Markus Schulz, &copy; 2010<br/>" +
235 "<br/>This software allows you to read a vector image in <a href=\"http://en.wikipedia.org/wiki/Scalable_Vector_Graphics\">SVG format</a>, " +
236 "and send it to a <a href=\"http://www.graphteccorp.com/craftrobo/\">Graphtec Craft Robo 2</a>/3 " +
237 " or <a href=\"http://www.silhouette.com/cameo\">Silhouette Cameo</a> " +
238 " (or similar device) for cutting. It is designed to work with SVGs produced " +
239 "by the excellent free vector graphics editor <a href=\"http://www.inkscape.org/\">Inkscape</a>. " +
240 " It may work with other software but this has not been tested.<br/>" +
241 "<br/>See <a href=\"http://robocut.org\">the website for more information</a>.";
242 QMessageBox::information(this, "About", message);
243 }
244
245 void MainWindow::on_actionExit_triggered()
246 {
247 close();
248 }
249
250 void MainWindow::on_actionCut_triggered()
251 {
252 if (!cutDialog)
253 cutDialog = new CutDialog(this);
254
255 if (cutDialog->exec() != QDialog::Accepted)
256 return;
257
258 // Create a new dialog and run the actual cutting in a different thread.
259
260 CuttingDialog* cuttingDlg = new CuttingDialog(this);
261
262 CutParams params;
263 params.cuts = paths;
264 params.mediawidth = mediaSize.width();
265 params.mediaheight = mediaSize.height();
266 params.media = cutDialog->media();
267 params.pressure = cutDialog->pressure();
268 params.regwidth = cutDialog->regWidth();
269 params.regheight = cutDialog->regHeight();
270 params.regmark = cutDialog->regMark();
271 params.regsearch = cutDialog->regSearch();
272 params.speed = cutDialog->speed();
273 params.trackenhancing = cutDialog->trackEnhancing();
274
275 cuttingDlg->startCut(params);
276 cuttingDlg->show();
277 }
278
279 void MainWindow::on_actionManual_triggered()
280 {
281 QMessageBox::information(this, "Manual", "An online manual is available at <br><br><a href=\"http://robocut.org/\">http://robocut.org/</a>");
282 }
283
284 void MainWindow::on_actionAnimate_toggled(bool animate)
285 {
286 if (animate)
287 {
288 animationTimer->start(30);
289 if (cutMarker)
290 {
291 cutMarker->setPos(0, 0);
292 cutMarker->show();
293 }
294
295 cutMarkerPath = 0;
296 cutMarkerLine = 0;
297 cutMarkerDistance = 0.0;
298 }
299 else
300 {
301 animationTimer->stop();
302 if (cutMarker)
303 cutMarker->hide();
304 }
305 }
306
307 void MainWindow::on_actionReset_triggered()
308 {
309 ui->graphicsView->resetTransform();
310 ui->graphicsView->scale(InitialZoom, InitialZoom);
311 }
312
313 void MainWindow::on_actionZoom_In_triggered()
314 {
315 ui->graphicsView->scale(1.2, 1.2);
316 }
317
318 void MainWindow::on_actionZoom_Out_triggered()
319 {
320 ui->graphicsView->scale(1.0/1.2, 1.0/1.2);
321 }
322
323 void MainWindow::animate()
324 {
325 if (!cutMarker)
326 return;
327
328 // Make sure the current position is sane.
329 if (cutMarkerPath >= paths.size() || cutMarkerLine >= paths[cutMarkerPath].size())
330 {
331 // If not, reset it.
332 cutMarkerPath = 0;
333 cutMarkerLine = 0;
334 cutMarkerDistance = 0.0;
335 return;
336 }
337 // Get the ends of the segment/edge we are currently on.
338
339 QPointF a = paths[cutMarkerPath][cutMarkerLine];
340 QPointF b;
341 if (cutMarkerLine == paths[cutMarkerPath].size()-1)
342 {
343 // We are moving between paths, and not cutting.
344 b = paths[(cutMarkerPath+1) % paths.size()][0];
345 cutMarker->setOpacity(0.2);
346 }
347 else
348 {
349 // We are cutting on a path.
350 b = paths[cutMarkerPath][cutMarkerLine+1];
351 cutMarker->setOpacity(1.0);
352 }
353
354 QPointF r = b-a;
355 double h = hypot(r.x(), r.y());
356 if (h > 0.0)
357 r /= h;
358
359 // The current position of the marker.
360 QPointF p = a + r * cutMarkerDistance;
361
362 cutMarker->setPos(p);
363
364 // Advance the position of the marker for the next time this function is called.
365 cutMarkerDistance += 2.0;
366 if (cutMarkerDistance > h)
367 {
368 cutMarkerDistance = 0.0;
369 if (++cutMarkerLine >= paths[cutMarkerPath].size())
370 {
371 cutMarkerLine = 0;
372 if (++cutMarkerPath >= paths.size())
373 {
374 cutMarkerPath = 0;
375
376 // Also stop the animation...
377 ui->actionAnimate->setChecked(false);
378
379 }
380 }
381 }
382 }
383 void MainWindow::setFileLoaded(QString filename)
384 {
385 bool e = !filename.isEmpty();
386 if (e)
387 setWindowTitle("Robocut - " + filename);
388 else
389 setWindowTitle("Robocut");
390
391 ui->actionAnimate->setEnabled(e);
392 ui->actionReset->setEnabled(e);
393 ui->actionZoom_In->setEnabled(e);
394 ui->actionZoom_Out->setEnabled(e);
395 ui->actionCut->setEnabled(e);
396 ui->actionReload->setEnabled(e);
397
398 }
399
400 bool MainWindow::eventFilter(QObject *o, QEvent *e)
401 {
402 if (o == ui->graphicsView->viewport())
403 {
404 if (e->type() == QEvent::Wheel)
405 {
406 // Anchor under the mouse pointer when using the mouse wheel.
407 // This doesn't quite work as nicely as I'd like because it clamps the scrolling
408 // precisely to the scene boundary. It's a bit hard to zoom to corners. Oh well.
409 ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
410 QWheelEvent *w = dynamic_cast<QWheelEvent*>(e);
411 if (w->delta() <= 0)
412 on_actionZoom_Out_triggered();
413 else
414 on_actionZoom_In_triggered();
415
416 // Anchor in view centre for keyboard shortcuts.
417 ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorViewCenter);
418 return true;
419 }
420 }
421 return false;
422 }
423