-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathimageZoomWindow.cpp
More file actions
462 lines (374 loc) · 14.2 KB
/
Copy pathimageZoomWindow.cpp
File metadata and controls
462 lines (374 loc) · 14.2 KB
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
//
// 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 "imageZoomWindow.h"
#include <QtCore/QString>
#include <QtXml/QDomDocument>
#include <QtXml/QDomElement>
#include <QtWidgets/QLabel>
#include <QtWidgets/QMenu>
#include <QtWidgets/QToolBar>
#include <QtWidgets/QDockWidget>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QMessageBox>
#include <QHBoxLayout>
#include "windowManager.h"
#include "utility.h"
#include "imageUtility.h"
#include "helpBrowser.h"
#include "quickHelp.h"
#include "floss.h"
extern const int ZOOM_INCREMENT = 100;
imageZoomWindow::imageZoomWindow(const QString& dockName,
windowManager* winMgr)
: Original_(tr("Original")), constructedAndShown_(false),
windowManager_(winMgr) {
createDock(dockName);
createMenus();
createToolbarAndMenuActions();
createStatusBar();
}
void imageZoomWindow::createDock(const QString& dockName) {
// the color/symbol list dock widget
dockHolder_ = new QDockWidget(dockName, this);
dockHolder_->setFeatures(QDockWidget::NoDockWidgetFeatures);
}
void imageZoomWindow::createMenus() {
fileMenu_ = menuBar()->addMenu(tr("&File"));
imageMenu_ = menuBar()->addMenu(tr("&Image"));
helpMenu_ = menuBar()->addMenu(tr("&Help"));
}
void imageZoomWindow::createToolbarAndMenuActions() {
toolBar_ = addToolBar(tr("Toolbar"));
toolBar_->setMovable(false);
// users add these zoom tools using addToolbarZoomIcons()
zoomInAction_ = new QAction(QIcon(":zoomIn.png"), tr("Zoom in"), this);
//zoomInAction_->setShortcut(QKeySequence(QKeySequence::ZoomIn));
zoomInAction_->setShortcut(QKeySequence("Ctrl+="));
zoomOutAction_ = new QAction(QIcon(":zoomOut.png"), tr("Zoom out"), this);
zoomOutAction_->setShortcut(QKeySequence("Ctrl+-"));
originalSizeAction_ = new QAction(QIcon(":zoomToOriginal.png"),
tr("Original size"), this);
zoomToWidthAction_ = new QAction(QIcon(":zoomToWidth.png"),
tr("Zoom to width"), this);
zoomToHeightAction_ = new QAction(QIcon(":zoomToHeight.png"),
tr("Zoom to height"), this);
zoomToImageAction_ = new QAction(QIcon(":zoomToImage.png"),
tr("Zoom to fit"), this);
zoomToWidthAction_->setShortcut(QKeySequence("Ctrl+8"));
zoomToHeightAction_->setShortcut(QKeySequence("Ctrl+9"));
zoomToImageAction_->setShortcut(QKeySequence("Ctrl+0"));
openImageAction_ = new QAction(QIcon(":openImage.png"),
tr("Open a new image"), this);
openImageAction_->setShortcut(QKeySequence("Ctrl+o"));
// openImageAction_->setShortcut(QKeySequence(QKeySequence::Open));
fileMenu_->addAction(openImageAction_);
toolBar_->addAction(openImageAction_);
openProjectAction_ = new QAction(QIcon(":openProject.png"),
tr("Open a saved project"), this);
openProjectAction_->setShortcut(QKeySequence("Ctrl+Shift+o"));
fileMenu_->addAction(openProjectAction_);
toolBar_->addAction(openProjectAction_);
saveAsAction_ = new QAction(QIcon(":saveProjectAs.png"),
tr("Save project as"), this);
saveAsAction_->setShortcut(QKeySequence("Ctrl+Shift+s"));
fileMenu_->addAction(saveAsAction_);
saveAction_ = new QAction(QIcon(":saveProject.png"),
tr("Save project"), this);
saveAction_->setShortcut(QKeySequence("Ctrl+s"));
fileMenu_->addAction(saveAction_);
toolBar_->addAction(saveAction_);
quitAction_ = fileMenu_->addAction(tr("Quit"));
quitAction_->setShortcut(QKeySequence("Ctrl+q"));
imageInfoAction_ = new QAction(tr("Image information"), this);
imageInfoAction_->setShortcut(QKeySequence("Ctrl+i"));
quickHelpAction_ = new QAction(QIcon(":quickHelp.png"), tr("Quick Help"),
this);
quickHelpAction_->setShortcut(Qt::Key_F1);
helpAction_ = new QAction(QIcon(":help.png"), tr("Help"), this);
helpAboutAction_ = new QAction(QIcon(":about.png"), tr("About"), this);
helpMenu_->addAction(quickHelpAction_);
helpMenu_->addAction(helpAction_);
helpMenu_->addAction(helpAboutAction_);
connect(openImageAction_, SIGNAL(triggered()),
this, SLOT(open()));
connect(saveAsAction_, SIGNAL(triggered()),
windowManager_, SLOT(saveAs()));
connect(saveAction_, SIGNAL(triggered()),
windowManager_, SLOT(save()));
connect(openProjectAction_, SIGNAL(triggered()),
windowManager_, SLOT(openProject()));
connect(originalSizeAction_, SIGNAL(triggered()),
this, SLOT(originalSize()));
connect(zoomInAction_, SIGNAL(triggered()),
this, SLOT(zoomIn()));
connect(zoomOutAction_, SIGNAL(triggered()),
this, SLOT(zoomOut()));
connect(zoomToWidthAction_, SIGNAL(triggered()),
this, SLOT(zoomToWidth()));
connect(zoomToHeightAction_, SIGNAL(triggered()),
this, SLOT(zoomToHeight()));
connect(zoomToImageAction_, SIGNAL(triggered()),
this, SLOT(zoomToImage()));
connect(quitAction_, SIGNAL(triggered()),
this, SLOT(quit()));
connect(imageInfoAction_, SIGNAL(triggered()),
this, SLOT(displayImageInfo()));
connect(quickHelpAction_, SIGNAL(triggered()),
this, SLOT(showQuickHelp()));
connect(helpAction_, SIGNAL(triggered()),
this, SLOT(helpRequested()));
connect(helpAboutAction_, SIGNAL(triggered()),
this, SLOT(helpAbout()));
}
void imageZoomWindow::createStatusBar() {
status_ = new QLabel(this);
// 1 means normal status gets as much width as possible
statusBar()->addWidget(status_, 1);
permanentStatus_ = new QLabel(this);
// 0 means permanent status gets no more width than needed
statusBar()->addPermanentWidget(permanentStatus_, 0);
}
void imageZoomWindow::addWindowActions(const QList<QAction*>& windowActions,
QMenu* windowMenu) {
for (int i = 0, size = windowActions.size(); i < size; ++i) {
toolBar_->addAction(windowActions[i]);
}
menuBar()->insertMenu(helpMenu_->menuAction(), windowMenu);
}
void imageZoomWindow::addRecentlyOpenedMenus(QMenu* imagesMenu,
QMenu* projectsMenu) {
fileMenu_->insertMenu(saveAsAction_, imagesMenu);
fileMenu_->insertMenu(saveAsAction_, projectsMenu);
}
void imageZoomWindow::addZoomActionsToImageMenu() {
imageMenu_->addAction(zoomToImageAction_);
imageMenu_->addAction(zoomToWidthAction_);
imageMenu_->addAction(zoomToHeightAction_);
imageMenu_->addAction(zoomInAction_);
imageMenu_->addAction(zoomOutAction_);
imageMenu_->addAction(originalSizeAction_);
}
void imageZoomWindow::addToolbarZoomIcons() {
toolBar_->addAction(zoomInAction_);
toolBar_->addAction(zoomOutAction_);
toolBar_->addAction(zoomToWidthAction_);
toolBar_->addAction(zoomToHeightAction_);
}
void imageZoomWindow::setZoomActionsEnabled(bool b) {
zoomToImageAction_->setEnabled(b);
zoomToWidthAction_->setEnabled(b);
zoomToHeightAction_->setEnabled(b);
zoomInAction_->setEnabled(b);
zoomOutAction_->setEnabled(b);
originalSizeAction_->setEnabled(b);
}
bool imageZoomWindow::quit() {
const int returnCode = QMessageBox::warning(this, "Cstitch",
tr("All unsaved work will be lost if you continue; are you sure you want to quit?"),
QMessageBox::Cancel | QMessageBox::Ok,
QMessageBox::Ok);
if (returnCode == QMessageBox::Ok) {
windowManager_->quit();
exit(0);
}
else {
return false;
}
}
void imageZoomWindow::open() {
windowManager_->openNewImage();
}
const QImage& imageZoomWindow::originalImage() const {
return winManager()->originalImage();
}
void imageZoomWindow::displayOriginalImageInfo(int width, int height) {
if (!imageMenu_->isEnabled()) {
return;
}
const int colorCount = windowManager_->getOriginalImageColorCount();
QMessageBox::information(this,
tr("Original Image"),
//: In English the singular version would be "color"
//: and the plural would be "unique colors" - add
//: "unique" in the plural case when possible
tr("The original image currently has dimensions "
"%1x%2 and contains %n color(s).", "", colorCount)
.arg(::itoqs(width))
.arg(::itoqs(height)));
}
void imageZoomWindow::setStatus(const QString& status) {
status_->setText(status);
status_->setToolTip(status);
}
void imageZoomWindow::setPermanentStatus(const QString& status) {
permanentStatus_->setText("<b>" + status + "</b>");
permanentStatus_->setToolTip(status);
}
void imageZoomWindow::showTemporaryStatusMessage(const QString& status,
int duration) {
statusBar()->showMessage(status, duration);
}
int imageZoomWindow::imageNameToIndex(const QString& imageName) const {
if (imageName == Original_) {
return 0;
}
else {
QRegExp regularExpression("Image (\\d+)");
regularExpression.indexIn(imageName);
const QString indexString = regularExpression.cap(1);
return indexString.isNull() ? -1 : indexString.toInt();
}
}
QString imageZoomWindow::imageNameFromIndex(int index) const {
if (index == 0) {
return Original_;
}
else {
return QString("Image ") + ::itoqs(index);
}
}
void imageZoomWindow::closeEvent(QCloseEvent* event) {
const bool reallyQuit = quit();
if (!reallyQuit) {
event->ignore();
}
}
void imageZoomWindow::showEvent(QShowEvent* ) {
if (constructedAndShown_) {
return;
}
else {
constructedAndShown_ = true;
processFirstShow();
}
}
bool imageZoomWindow::eventFilter(QObject* watched, QEvent* event) {
if (event->type() == QEvent::Wheel) {
QWheelEvent* wheelEvent = static_cast<QWheelEvent*>(event);
if (wheelEvent->modifiers() == Qt::ControlModifier) {
const int yDelta = wheelEvent->angleDelta().y();
bool zoomed = false;
if (yDelta > 0) {
zoomIn();
zoomed = true;
}
else if (yDelta < 0) {
zoomOut();
zoomed = true;
}
if (zoomed) {
wheelEvent->accept();
return true;
}
}
else if (wheelEvent->modifiers() == Qt::ShiftModifier) {
return horizontalWheelScrollEvent(watched, wheelEvent);
}
}
return QMainWindow::eventFilter(watched, event);
}
void imageZoomWindow::helpAbout() {
QMessageBox aboutBox(this);
aboutBox.setWindowTitle(tr("About"));
const QString version = winManager()->getProgramVersion();
aboutBox.setText("<b>Cstitch</b><br />" +
tr("Version: %1<br />"
"Tom Klein<br />email: "
"tomklein@users.sourceforge.net<br />"
"http://cstitch.sourceforge.net/").arg(version));
aboutBox.setIconPixmap(QPixmap(":aboutIcon.png"));
aboutBox.exec();
}
void imageZoomWindow::helpRequested() const {
helpBrowser::loadHelp(getHelpMode());
}
void imageZoomWindow::showQuickHelp(bool show) {
if (show) {
quickHelp::loadQuickHelp(getHelpMode(), this);
}
else {
quickHelp::closeCurrentWindow();
}
}
void imageZoomWindow::processFirstShow() {
// this may be occuring before the actual show, so clear the event
// queue to make sure that show has taken place
clearEventQueue();
}
//// otherwise inline functions defined here to avoid includes - none of
//// these are performance sensitive
void imageZoomWindow::addQuickHelp(QAction* autoShowQuickHelp) {
helpMenu_->insertAction(quickHelpAction_, autoShowQuickHelp);
}
void imageZoomWindow::showListDock() { dockHolder_->show(); }
void imageZoomWindow::hideListDock() { dockHolder_->hide(); }
bool imageZoomWindow::listDockIsVisible() const {
return dockHolder_->isVisible();
}
void imageZoomWindow::setListDockEnabled(bool b) {
dockHolder_->setEnabled(b);
}
void imageZoomWindow::setListDockWidget(QWidget* widget) {
dockHolder_->setWidget(widget);
addDockWidget(Qt::RightDockWidgetArea, dockHolder_);
}
void imageZoomWindow::setListDockTitle(const QString& title) {
dockHolder_->setWindowTitle(title);
}
void imageZoomWindow::addToolbarAction(QAction* action) {
toolBar_->addAction(action);
}
void imageZoomWindow::addToolbarWidget(QWidget* widget) {
toolBar_->addWidget(widget);
}
void imageZoomWindow::addToolbarSeparator() {toolBar_->addSeparator(); }
void imageZoomWindow::addToolbarWidgetPair(QWidget* widget1, QWidget* widget2) {
QWidget* holderWidget = new QWidget;
QHBoxLayout* holderLayout = new QHBoxLayout;
int top, bottom;
holderLayout->getContentsMargins(NULL, &top, NULL, &bottom);
const int spacing = 2;
holderLayout->setContentsMargins(spacing, top, spacing, bottom);
holderLayout->addWidget(widget1);
holderLayout->addSpacing(spacing);
holderLayout->addWidget(widget2);
holderWidget->setLayout(holderLayout);
toolBar_->addWidget(holderWidget);
}
void imageZoomWindow::setToolbarEnabled(bool b) { toolBar_->setEnabled(b); }
void imageZoomWindow::setSaveActionsEnabled(bool b) {
saveAction_->setEnabled(b);
saveAsAction_->setEnabled(b);
}
void imageZoomWindow::setPermanentStatusEnabled(bool b) {
permanentStatus_->setEnabled(b);
}
QString imageZoomWindow::imageInfoFlossString(flossType type) const {
const QString flossTypeText = type.shortText();
if (flossTypeText != "") {
//: %1 is "DMC" or "Anchor" or ...
//: this string will get added to the end of a sentence, as in:
//: "This image contains only DMC colors"
return tr("contains only %1 colors").arg(flossTypeText);
}
else {
return "";
}
}