forked from kleintom/Cstitch
/
patternWindow.cpp
750 lines (636 loc) · 24.6 KB
/
patternWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
//
// Copyright 2010, 2011 Tom Klein.
//
// This file is part of cstitch.
//
// cstitch is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include "patternWindow.h"
#include <QtCore/QTimer>
#include <QtCore/QSettings>
#include <QtWidgets/QScrollArea>
#include <QtWidgets/QDockWidget>
#include <QtWidgets/QMenu>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QScrollBar>
#include <QtWidgets/QColorDialog>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QFileDialog>
#include "patternPrinter.h"
#include "patternDockWidget.h"
#include "windowManager.h"
#include "utility.h"
#include "comboBox.h"
#include "patternImageContainer.h"
#include "helpBrowser.h"
#include "patternImageLabel.h"
#include "dockImage.h"
#include "xmlUtility.h"
#include "floss.h"
// bounds for allowed symbol sizes (too large and file sizes are ridiculous,
// too small and symbols can't be distinguished)
extern const int MAX_SYMBOL_SIZE = 60;
extern const int MIN_SYMBOL_SIZE = 15;
patternWindow::patternWindow(windowManager* winMgr)
: imageSaverWindow(tr("Symbols"), winMgr), pdfSymbolDim_(0),
curImage_(patternImagePtr(NULL)),
fontMetrics_(QFontMetrics(font())) {
installEventFilter(this);
imageLabel_ = new patternImageLabel(this);
imageLabel_->setGridColor(Qt::black);
connect(imageLabel_, SIGNAL(announceImageClick(QMouseEvent* )),
this, SLOT(imageClickSlot(QMouseEvent* )));
scroll_ = new QScrollArea(this);
scroll_->installEventFilter(this);
scroll_->viewport()->installEventFilter(this);
scroll_->setWidget(imageLabel_);
setCentralWidget(scroll_);
dockImageHolder_ = new QDockWidget(this);
dockImageHolder_->setFeatures(QDockWidget::NoDockWidgetFeatures);
dockImage_ = new dockImage(winManager()->originalImage(), this);
dockImageHolder_->setWidget(dockImage_);
addDockWidget(Qt::RightDockWidgetArea, dockImageHolder_);
connect(scroll_->horizontalScrollBar(), SIGNAL(valueChanged(int )),
this, SLOT(labelScrollChange( )));
connect(scroll_->verticalScrollBar(), SIGNAL(valueChanged(int )),
this, SLOT(labelScrollChange( )));
connect(dockImage_, SIGNAL(viewportUpdated(qreal , qreal, bool , bool )),
this, SLOT(processDockImageUpdate(qreal , qreal, bool , bool )));
const QFontMetrics metrics(font());
basePatternDim_ = (metrics.width("@") > metrics.height()) ?
metrics.width("@") : metrics.height();
basePatternDim_ = qMax(basePatternDim_, 30);
listDock_ = new patternDockWidget(basePatternDim_, this);
setListDockWidget(listDock_);
connect(listDock_, SIGNAL(changeSymbol(const triC& )),
this, SLOT(changeSymbolSlot(const triC& )));
constructActions();
constructMenus();
constructToolbar();
constructPdfViewerDialog();
setPermanentStatus(tr("Click the 'To pdf' button to save the pattern as a pdf."));
setStatus(tr("left click: change symbol; right click: switch between square and symbol images"));
}
void patternWindow::constructActions() {
backHistoryAction_ =
new QAction(QIcon(":leftArrow.png"),
tr("Undo"), this);
backHistoryAction_->setShortcut(QKeySequence("Ctrl+z"));
backHistoryAction_->setEnabled(false);
connect(backHistoryAction_, SIGNAL(triggered()),
this, SLOT(backHistoryActionSlot()));
forwardHistoryAction_ =
new QAction(QIcon(":rightArrow.png"),
tr("Redo"), this);
forwardHistoryAction_->setShortcut(QKeySequence("Ctrl+y"));
forwardHistoryAction_->setEnabled(false);
connect(forwardHistoryAction_, SIGNAL(triggered()),
this, SLOT(forwardHistoryActionSlot()));
switchAction_ =
new QAction(QIcon(":switchImages.png"),
tr("Switch between pattern and square images"), this);
connect(switchAction_, SIGNAL(triggered()),
this, SLOT(switchActionSlot()));
gridAction_ =
new QAction(QIcon(":grid.png"), tr("Turn grid on/off"), this);
gridAction_->setCheckable(true);
gridAction_->setChecked(true);
imageLabel_->setGridOn(true);
connect(gridAction_, SIGNAL(triggered(bool )),
this, SLOT(processGridChange(bool )));
deleteImageAction_ = new QAction(tr("Delete image"), this);
connect(deleteImageAction_, SIGNAL(triggered()),
this, SLOT(processDelete()));
gridColorAction_ = new QAction(tr("Change grid color"), this);
connect(gridColorAction_, SIGNAL(triggered()),
this, SLOT(processGridColorChange()));
connect(originalSizeAction(), SIGNAL(triggered()),
this, SLOT(originalSize()));
setPdfViewerAction_ = new QAction(tr("Set pdf viewer"), this);
connect(setPdfViewerAction_, SIGNAL(triggered()),
this, SLOT(updatePdfViewerOptions()));
}
void patternWindow::constructMenus() {
imageMenu()->addAction(switchAction_);
imageMenu()->addAction(zoomInAction());
imageMenu()->addAction(zoomOutAction());
imageMenu()->addAction(originalSizeAction());
imageMenu()->addAction(gridAction_);
imageMenu()->addAction(gridColorAction_);
imageMenu()->addAction(imageInfoAction());
helpMenu()->addAction(setPdfViewerAction_);
imageListMenu_ = new QMenu(tr("I&mages"), this);
menuBar()->insertMenu(helpMenu()->menuAction(), imageListMenu_);
connect(imageListMenu_, SIGNAL(triggered(QAction* )),
this, SLOT(processImageListMenu(QAction* )));
imageListMenu_->addAction(deleteImageAction_);
imageListMenu_->addSeparator();
}
void patternWindow::constructToolbar() {
saveToPdfButton_ = new QPushButton(tr("To pdf (4/4)"), this);
saveToPdfButton_->setShortcut(QKeySequence("Ctrl+return"));
saveToPdfButton_->setToolTip(tr("Save the pattern as a pdf file"));
connect(saveToPdfButton_, SIGNAL(clicked()),
this, SLOT(saveSlot()));
imageListBox_ = new comboBox(this);
connect(imageListBox_, SIGNAL(activated(const QString& )),
this, SLOT(processImageBox(const QString& )));
addToolbarSeparator();
addToolbarSeparator();
addToolbarWidget(saveToPdfButton_);
addToolbarSeparator();
addToolbarSeparator();
addToolbarAction(switchAction_);
addToolbarAction(gridAction_);
addToolbarSeparator();
addToolbarSeparator();
addToolbarAction(backHistoryAction_);
addToolbarAction(forwardHistoryAction_);
addToolbarSeparator();
addToolbarSeparator();
addToolbarAction(zoomInAction());
addToolbarAction(zoomOutAction());
addToolbarAction(originalSizeAction());
addToolbarSeparator();
addToolbarSeparator();
addToolbarWidget(imageListBox_);
addToolbarSeparator();
addToolbarSeparator();
}
void patternWindow::addImage(const QImage& squareImage, int squareDimension,
const QVector<flossColor>& colors, QRgb gridColor,
int imageIndex) {
patternImageContainer* container =
new patternImageContainer(squareImage, imageNameFromIndex(imageIndex),
squareDimension, basePatternDim_, colors);
const patternImagePtr imagePtr(container);
connect(container, SIGNAL(symbolChanged(QRgb , const QPixmap& )),
listDock_, SLOT(changeSymbol(QRgb , const QPixmap& )));
QAction* menuAction = new QAction(container->name(), this);
menuAction->setData(QVariant::fromValue(imagePtr));
imageListMenu_->addAction(menuAction);
imageListBox_->addItem(container->name());
imageLabel_->setGridColor(gridColor);
setCur(imagePtr);
}
void patternWindow::setCur(patternImagePtr container) {
if (curImage_ != container) {
curImage_ = container;
imageLabel_->setSymbols(curImage_->symbols());
imageLabel_->setSquares(curImage_->colorSquares());
imageLabel_->setImage(curImage_->squareImage(),
curImage_->squareDimension(),
curImage_->symbolDimension());
dockImage_->setImage(curImage_->squareImage());
// wait for the geometry to settle itself before
// processing the scroll change
QTimer::singleShot(50, this, SLOT(labelScrollChange()));
listDock_->setSymbolList(curImage_->symbols());
imageListBox_->setCurrentIndex(imageListBox_->findText(curImage_->name()));
updateHistoryButtonStates();
}
}
void patternWindow::processImageListMenu(QAction* action) {
if (! action->data().isNull() &&
action->data().canConvert<patternImagePtr>()) {
patternImagePtr container =
action->data().value<patternImagePtr>();
setCur(container);
}
}
void patternWindow::processImageBox(const QString& imageName) {
QAction* action =
::actionFromImageName(imageListMenu_->actions(),
findPatternActionName(imageName));
if (action) {
processImageListMenu(action);
}
}
void patternWindow::processDelete() {
QAction* action =
::actionFromImageName(imageListMenu_->actions(),
findPatternActionName(curImage_->name()));
delete action;
imageListBox_->removeItem(imageListBox_->findText(curImage_->name()));
int removedImageIndex = imageNameToIndex(curImage_->name());
curImage_ = patternImagePtr(NULL);
winManager()->patternWindowImageDeleted(removedImageIndex);
// find a new image - just show the last image since we have no
// reason to favor any particular image
const QAction* lastAction = imageListMenu_->actions().last();
if (! lastAction->data().isNull() &&
lastAction->data().canConvert<patternImagePtr>()) {
setCur(lastAction->data().value<patternImagePtr>());
}
else {
// close up shop if there are no images left
hide();
winManager()->patternWindowEmpty();
}
}
void patternWindow::processGridChange(bool checked) {
gridAction_->setChecked(checked);
imageLabel_->setGridOn(checked);
}
void patternWindow::processGridColorChange() {
const QColor chosenColor =
QColorDialog::getColor(QColor(imageLabel_->gridColor()), this);
if (chosenColor.isValid()) {
imageLabel_->setGridColor(chosenColor.rgb());
}
}
void patternWindow::changeSymbolSlot(const triC& color) {
const bool updateImage = curImage_->changeSymbol(color);
if (updateImage) {
updateImageLabelSymbols();
}
updateHistoryButtonStates();
}
void patternWindow::updateHistoryButtonStates() {
backHistoryAction_->setEnabled(curImage_->backHistory());
forwardHistoryAction_->setEnabled(curImage_->forwardHistory());
}
void patternWindow::imageClickSlot(QMouseEvent* event) {
if (event->button() == Qt::RightButton) {
switchActionSlot();
}
else if (event->button() == Qt::LeftButton) {
const bool updateImage =
curImage_->mouseActivatedChangeSymbol(imageLabel_->width(),
imageLabel_->height(), event);
if (updateImage) {
updateImageLabelSymbols();
}
updateHistoryButtonStates();
}
}
void patternWindow::updateImageLabelSymbols() {
imageLabel_->setSymbols(curImage_->symbols());
imageLabel_->update();
}
void patternWindow::updateImageLabelSize() {
imageLabel_->setSymbols(curImage_->symbols());
imageLabel_->setSquares(curImage_->colorSquares());
const int newDimension = curImage_->symbolDimension();
imageLabel_->setPatternDim(newDimension);
labelScrollChange();
zoomInAction()->setEnabled(newDimension < MAX_SYMBOL_SIZE);
zoomOutAction()->setEnabled(newDimension > MIN_SYMBOL_SIZE);
}
void patternWindow::originalSize() {
curImage_->setSymbolDimension(basePatternDim_);
zoomInAction()->setEnabled(true);
zoomOutAction()->setEnabled(true);
updateImageLabelSize();
}
void patternWindow::zoom(int zoomIncrement) {
const int zoomDelta = (zoomIncrement > 0) ? 2 : -2;
curImage_->setSymbolDimension(curImage_->symbolDimension() + zoomDelta);
updateImageLabelSize();
}
void patternWindow::switchActionSlot() {
if (curImage_->viewingSquareImage()) {
curImage_->setViewingSquareImage(false);
imageLabel_->viewSquareImage(false);
}
else {
curImage_->setViewingSquareImage(true);
imageLabel_->viewSquareImage(true);
}
}
void patternWindow::forwardHistoryActionSlot() {
curImage_->moveHistoryForward();
updateImageLabelSymbols();
updateHistoryButtonStates();
}
void patternWindow::backHistoryActionSlot() {
curImage_->moveHistoryBack();
updateImageLabelSymbols();
updateHistoryButtonStates();
}
QImage patternWindow::curImageForSaving() const {
QImage returnImage;
if (curImage_->viewingSquareImage()) {
returnImage = curImage_->squareImage();
returnImage = returnImage.scaledToWidth(imageLabel_->width());
}
else {
returnImage = curImage_->patternImageCurSymbolSize();
}
if (gridAction_->isChecked()) {
if (curImage_->viewingSquareImage()) {
::gridImage(&returnImage, curImage_->squareDimension(),
curImage_->squareImage().width(),
curImage_->squareImage().height(),
imageLabel_->gridColor());
}
else {
::gridImage(&returnImage, curImage_->symbolDimension(),
returnImage.width(), returnImage.height(),
imageLabel_->gridColor());
}
}
return returnImage;
}
QSize patternWindow::curImageViewSize() const {
return QSize(imageLabel_->width(), imageLabel_->height());
}
void patternWindow::saveSlot() {
patternPrinter printer(curImage_, originalImage());
printer.save(usePdfViewer_, pdfViewerPath_);
}
bool patternWindow::eventFilter(QObject* watched, QEvent* event) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
if ((keyEvent->key() == Qt::Key_Up || keyEvent->key() == Qt::Key_Down)
&& (keyEvent->modifiers() & Qt::ShiftModifier)) {
if (keyEvent->key() == Qt::Key_Up) {
imageListBox_->moveToPreviousItem();
}
else {
imageListBox_->moveToNextItem();
}
return true;
}
}
return imageSaverWindow::eventFilter(watched, event);
}
// this probably isn't what you want - some active subwidget will probably
// capture key strokes before they ever get here. Use eventFilters instead
void patternWindow::keyPressEvent(QKeyEvent* ) {
}
void patternWindow::labelScrollChange() {
const int labelImageWidth = imageLabel_->width() - 1;
const int labelImageHeight = imageLabel_->height() - 1;
const qreal x1 =
scroll_->horizontalScrollBar()->value()/static_cast<qreal>(labelImageWidth);
const qreal x2 =
(scroll_->horizontalScrollBar()->value() +
scroll_->viewport()->width() - 1)/static_cast<qreal>(labelImageWidth);
const qreal y1 =
scroll_->verticalScrollBar()->value()/static_cast<qreal>(labelImageHeight);
const qreal y2 =
(scroll_->verticalScrollBar()->value() +
scroll_->viewport()->height() - 1)/static_cast<qreal>(labelImageHeight);
dockImage_->updateViewport(x1, x2, y1, y2);
}
void patternWindow::processDockImageUpdate(qreal xPercentage,
qreal yPercentage,
bool rightEdge,
bool bottomEdge) {
scroll_->horizontalScrollBar()->setValue(qRound(xPercentage*imageLabel_->width()));
scroll_->verticalScrollBar()->setValue(qRound(yPercentage*imageLabel_->height()));
if (rightEdge) {
scroll_->horizontalScrollBar()->setValue(scroll_->horizontalScrollBar()->maximum());
}
if (bottomEdge) {
scroll_->verticalScrollBar()->setValue(scroll_->verticalScrollBar()->maximum());
}
}
void patternWindow::resizeEvent(QResizeEvent* event) {
labelScrollChange();
imageSaverWindow::resizeEvent(event);
}
void patternWindow::displayImageInfo() {
if (curImage_) {
const int width = imageLabel_->width();
const int height = imageLabel_->height();
const int symbolDim = curImage_->symbolDimension();
const int xBoxes = width/symbolDim;
const int yBoxes = height/symbolDim;
QMessageBox::information(this, curImage_->name(),
tr("%1 currently has dimensions %2x%3, box "
"dimension %4, and is %5 by %6 boxes.")
.arg(curImage_->name())
.arg(::itoqs(width))
.arg(::itoqs(height))
.arg(::itoqs(symbolDim))
.arg(::itoqs(xBoxes))
.arg(::itoqs(yBoxes)));
}
}
void patternWindow::writeCurrentHistory(QDomDocument* doc,
QDomElement* appendee,
int imageIndex) {
patternImagePtr container = getImageFromIndex(imageIndex);
if (container) {
container->writeSymbolHistory(doc, appendee);
}
else {
qWarning() << "Misplaced container in patternWriteHistory" <<
imageIndex;
}
}
patternImagePtr patternWindow::getImageFromIndex(int index) const {
const QAction* actionMatch =
::actionFromImageName(imageListMenu_->actions(),
findPatternActionName(imageNameFromIndex(index)));
if (actionMatch) {
return actionMatch->data().value<patternImagePtr>();
}
else {
return patternImagePtr(NULL);
}
}
void patternWindow::updateHistory(const QDomElement& xml) {
const int imageIndex = ::getElementText(xml, "index").toInt();
const QDomElement& xmlHistory = xml.firstChildElement("symbol_history");
patternImagePtr container = getImageFromIndex(imageIndex);
if (container) {
container->updateHistory(xmlHistory);
updateImageLabelSymbols();
updateHistoryButtonStates();
}
else {
qWarning() << "Misplaced container on patternHistoryRestore:" <<
imageIndex;
}
}
QString patternWindow::updateCurrentSettings(const QDomElement& xml) {
QDomElement settings(xml.firstChildElement("pattern_window_settings"));
if (settings.isNull()) {
return QString();
}
imageLabel_->setGridColor(::xmlStringToRgb(::getElementText(settings,
"grid_color")));
processGridChange(::stringToBool(::getElementText(settings, "grid_on")));
// TODO: De-duplicate this from the imageCompareBase version. :-(
// Seems like imagePtr and patternImagePtr should be related, but they're not
// (because imageContainer and patternImageContainer aren't either, and really
// don't share a common interface, which is what makes this a mess), and so
// functions here "common" to imageCompareBase and patternWindow like
// getImageFromIndex and setCur can't be virtualized into a common base.
// getImageFromIndex could/maybe should be templated and factored down, but
// setCur can't be - we could template this code as well and pass a
// doubly-templated functor to call the right setCur on the right object, but
// yuk. Live with the duplication for now.
QString errorMessage;
if (!settings.firstChildElement("current_index").isNull()) {
// KEEP THIS IN SYNC WITH imageCompareBase
bool parseOk = false;
const int curIndex =
::getElementText(settings, "current_index").toInt(&parseOk);
if (parseOk) {
patternImagePtr curImage = getImageFromIndex(curIndex);
if (curImage) {
setCur(curImage);
}
else {
errorMessage +=
tr("Pattern Window setting error(s):<br />"
"Image Missing error: image %1").arg(curIndex);
}
}
else {
errorMessage += tr("Pattern Window setting error(s):<br />"
"Parse error parsing \"%1\" for index")
.arg(::getElementText(settings, "current_index"));
}
}
return errorMessage;
}
void patternWindow::appendCurrentSettings(QDomDocument* doc,
QDomElement* appendee) const {
if (curImage_) {
QDomElement settings(doc->createElement("pattern_window_settings"));
appendee->appendChild(settings);
::appendTextElement(doc, "current_index",
QString::number(imageNameToIndex(curImage_->name())),
&settings);
::appendTextElement(doc, "grid_on",
::boolToString(imageLabel_->gridOn()), &settings);
::appendTextElement(doc, "grid_color",
::rgbToString(imageLabel_->gridColor()), &settings);
}
}
void patternWindow::processFirstShow() {
imageSaverWindow::processFirstShow();
// update the dock image now that we have correct geometry measurements
labelScrollChange();
}
helpMode patternWindow::getHelpMode() const {
return helpMode::H_PATTERN;
}
void patternWindow::constructPdfViewerDialog() {
usePdfViewer_ = false;
pdfViewerPath_ = "";
const QSettings settings("cstitch", "cstitch");
if (settings.contains("use_pdf_viewer") &&
settings.contains("pdf_viewer_path")) {
usePdfViewer_ = settings.value("use_pdf_viewer").toBool();
pdfViewerPath_ = settings.value("pdf_viewer_path").toString();
}
else { // very first run on this machine
// find a default viewer if we can
#ifdef Q_OS_LINUX
QStringList viewers;
viewers << "/usr/bin/evince" << "/usr/bin/kpdf" << "/usr/bin/okular" <<
"/usr/bin/xpdf" << "/usr/bin/acroread";
for (int i = 0, size = viewers.size(); i < size; ++i) {
const QString thisViewer = viewers[i];
if (QFile::exists(thisViewer)) {
pdfViewerPath_ = thisViewer;
break;
}
}
#endif
#ifdef Q_OS_WIN
// try to find acroread
QStringList adobeDirectories;
adobeDirectories << "C:/Program Files/Adobe/" <<
"C:/Program Files (x86)/Adobe/";
for (int i = 0, size = adobeDirectories.size(); i < size; ++i) {
QDir thisDir = adobeDirectories[i];
if (thisDir.exists()) {
// sort by time - if somebody has multiple versions of Reader
// installed (not officially supported by Adobe) and they
// install an older version after a newer version then this will
// choose the wrong version
const QStringList readers =
thisDir.entryList(QStringList("Reader *"), QDir::Dirs,
QDir::Time|QDir::Reversed);
if (!readers.empty() && thisDir.exists(readers[0] + "/Reader")) {
thisDir.cd(readers[0] + "/Reader");
if (thisDir.exists("AcroRd32.exe")) {
pdfViewerPath_ = thisDir.absoluteFilePath("AcroRd32.exe");
break;
}
else if (thisDir.exists("AcroRd64.exe")) {
pdfViewerPath_ = thisDir.absoluteFilePath("AcroRd64.exe");
break;
}
}
}
}
#endif // Q_OS_WIN
if (!pdfViewerPath_.isEmpty()) {
usePdfViewer_ = true;
}
}
}
void patternWindow::updatePdfViewerOptions() {
pdfViewerDialog viewerDialog(usePdfViewer_, pdfViewerPath_, this);
const int rc = viewerDialog.exec();
if (rc == QDialog::Accepted) {
QSettings settings("cstitch", "cstitch");
usePdfViewer_ = viewerDialog.useViewer();
settings.setValue("use_pdf_viewer", usePdfViewer_);
pdfViewerPath_ = viewerDialog.viewerPath();
settings.setValue("pdf_viewer_path", pdfViewerPath_);
}
}
bool patternWindow::horizontalWheelScrollEvent(QObject* ,
QWheelEvent* event) const {
scroll_->horizontalScrollBar()->event(event);
return true;
}
pdfViewerDialog::pdfViewerDialog(bool useViewer, const QString& curViewerPath,
QWidget* parent)
: cancelAcceptDialogBase(parent),
useViewer_(new QGroupBox(tr("Use viewer to display saved pdf patterns"))),
dialogLayout_(new QVBoxLayout),
groupLayout_(new QVBoxLayout),
currentPath_(new QLineEdit),
choosePath_(new QPushButton(tr("Choose new pdf viewer"))) {
useViewer_->setCheckable(true);
useViewer_->setChecked(useViewer);
currentPath_->setText(curViewerPath);
connect(choosePath_, SIGNAL(clicked()),
this, SLOT(updateViewerPath()));
groupLayout_->addWidget(currentPath_);
groupLayout_->addWidget(choosePath_);
useViewer_->setLayout(groupLayout_);
dialogLayout_->addWidget(useViewer_);
dialogLayout_->addWidget(cancelAcceptWidget());
setLayout(dialogLayout_);
setWindowTitle(tr("Set pdf viewer"));
}
void pdfViewerDialog::updateViewerPath() {
const QString newPath =
QFileDialog::getOpenFileName(this, tr("Choose new pdf viewer"),
currentPath_->text());
if (!newPath.isEmpty()) {
currentPath_->setText(newPath);
}
}
QString pdfViewerDialog::viewerPath() const {
return currentPath_->text();
}
bool pdfViewerDialog::useViewer() const {
return useViewer_->isChecked();
}