-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
558 lines (466 loc) · 17.1 KB
/
mainwindow.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
#include "mainwindow.h"
#include "settings.h"
#include "toolbutton.h"
#include "bottombuttongroup.h"
#include "graphicsview.h"
#include "navigatorview.h"
#include "graphicsscene.h"
#include "settingsdialog.h"
#include <QScreen>
#include <QDebug>
#include <QStyle>
#include <QMouseEvent>
#include <QGraphicsScene>
#include <QApplication>
#include <QGraphicsTextItem>
#include <QMenu>
#include <QShortcut>
#include <QDir>
#include <QCollator>
#include <QClipboard>
#include <QMimeData>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
if (Settings::instance()->stayOnTop()) {
this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint | Qt::WindowStaysOnTopHint);
} else {
this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);
}
this->setAttribute(Qt::WA_TranslucentBackground, true);
this->setMinimumSize(350, 350);
this->setWindowIcon(QIcon(":/icons/app-icon.svg"));
this->setMouseTracking(true);
// 与窗口透明度属性绑定
m_fadeOutAnimation = new QPropertyAnimation(this, "windowOpacity");
m_fadeOutAnimation->setDuration(300);
m_fadeOutAnimation->setStartValue(1);
m_fadeOutAnimation->setEndValue(0);
// 与窗口geometry属性绑定
m_floatUpAnimation = new QPropertyAnimation(this, "geometry");
m_floatUpAnimation->setDuration(300);
m_floatUpAnimation->setEasingCurve(QEasingCurve::OutCirc);
m_exitAnimationGroup = new QParallelAnimationGroup(this);
m_exitAnimationGroup->addAnimation(m_fadeOutAnimation);
m_exitAnimationGroup->addAnimation(m_floatUpAnimation);
connect(m_exitAnimationGroup, &QParallelAnimationGroup::finished,
this, &MainWindow::close);
GraphicsScene *scene = new GraphicsScene(this);
m_graphicsView = new GraphicsView(this);
m_graphicsView->setScene(scene);
this->setCentralWidget(m_graphicsView);
m_gv = new NavigatorView(this);
m_gv->setFixedSize(220, 160);
m_gv->setScene(scene);
m_gv->setMainView(m_graphicsView);
m_gv->fitInView(m_gv->sceneRect(), Qt::KeepAspectRatio);
connect(m_graphicsView, &GraphicsView::navigatorViewRequired,
this, [ = ](bool required, qreal angle) {
m_gv->resetTransform();
m_gv->rotate(angle);
m_gv->fitInView(m_gv->sceneRect(), Qt::KeepAspectRatio);
m_gv->setVisible(required);
m_gv->updateMainViewportRegion();
});
connect(m_graphicsView, &GraphicsView::viewportRectChanged,
m_gv, &NavigatorView::updateMainViewportRegion);
connect(m_graphicsView, &GraphicsView::requestGallery,
this, &MainWindow::loadGalleryBySingleLocalFile);
m_closeButton = new ToolButton(true, m_graphicsView);
m_closeButton->setIcon(QIcon(":/icons/window-close"));
m_closeButton->setIconSize(QSize(50, 50));
connect(m_closeButton, &QPushButton::clicked,
this, &MainWindow::closeWindow);
m_prevButton = new ToolButton(false, m_graphicsView);
m_prevButton->setIcon(QIcon(":/icons/go-previous"));
m_prevButton->setIconSize(QSize(75, 75));
m_prevButton->setVisible(false);
m_prevButton->setOpacity(0, false);
m_nextButton = new ToolButton(false, m_graphicsView);
m_nextButton->setIcon(QIcon(":/icons/go-next"));
m_nextButton->setIconSize(QSize(75, 75));
m_nextButton->setVisible(false);
m_nextButton->setOpacity(0, false);
connect(m_prevButton, &QAbstractButton::clicked,
this, &MainWindow::galleryPrev);
connect(m_nextButton, &QAbstractButton::clicked,
this, &MainWindow::galleryNext);
m_bottomButtonGroup = new BottomButtonGroup(this);
connect(m_bottomButtonGroup, &BottomButtonGroup::resetToOriginalBtnClicked,
this, [=](){ m_graphicsView->resetScale();});
connect(m_bottomButtonGroup, &BottomButtonGroup::zoomInBtnClicked,
this, [=](){ m_graphicsView->zoomView(1.25);});
connect(m_bottomButtonGroup, &BottomButtonGroup::zoomOutBtnClicked,
this, [=](){ m_graphicsView->zoomView(0.75);});
connect(m_bottomButtonGroup, &BottomButtonGroup::toggleCheckerboardBtnClicked,
this, [=](){ m_graphicsView->toggleCheckerboard();});
connect(m_bottomButtonGroup, &BottomButtonGroup::rotateRightBtnClicked,
this, [=](){
m_graphicsView->resetScale();
m_graphicsView->rotateView(90);
m_graphicsView->checkAndDoFitInView();
m_gv->setVisible(false);
});
connect(m_bottomButtonGroup, &BottomButtonGroup::toggleWindowMaximum,
this, &MainWindow::toggleMaximize);
m_bottomButtonGroup->setOpacity(0, false);
m_gv->setOpacity(0, false);
m_closeButton->setOpacity(0, false);
connect(this, &MainWindow::galleryLoaded, this, [this]() {
m_prevButton->setVisible(isGalleryAvailable());
m_nextButton->setVisible(isGalleryAvailable());
});
QShortcut *quitAppShortCut = new QShortcut(QKeySequence(Qt::Key_Space), this);
connect(quitAppShortCut, &QShortcut::activated, this, std::bind(&MainWindow::quitAppAction, this, false));
QShortcut *quitAppShortCut2 = new QShortcut(QKeySequence(Qt::Key_Escape), this);
connect(quitAppShortCut2, &QShortcut::activated, this, std::bind(&MainWindow::quitAppAction, this, false));
QShortcut * prevPictureShorucut = new QShortcut(QKeySequence(Qt::Key_PageUp), this);
connect(prevPictureShorucut, &QShortcut::activated,
this, &MainWindow::galleryPrev);
QShortcut * nextPictureShorucut = new QShortcut(QKeySequence(Qt::Key_PageDown), this);
connect(nextPictureShorucut, &QShortcut::activated,
this, &MainWindow::galleryNext);
QShortcut * fullscreenShorucut = new QShortcut(QKeySequence(QKeySequence::FullScreen), this);
connect(fullscreenShorucut, &QShortcut::activated,
this, &MainWindow::toggleFullscreen);
centerWindow();
}
MainWindow::~MainWindow()
{
}
void MainWindow::showUrls(const QList<QUrl> &urls)
{
if (!urls.isEmpty()) {
if (urls.count() == 1) {
m_graphicsView->showFileFromUrl(urls.first(), true);
} else {
m_graphicsView->showFileFromUrl(urls.first(), false);
m_files = urls;
m_currentFileIndex = 0;
}
} else {
m_graphicsView->showText(tr("File url list is empty"));
return;
}
m_gv->fitInView(m_gv->sceneRect(), Qt::KeepAspectRatio);
}
void MainWindow::adjustWindowSizeBySceneRect()
{
QSize sceneSize = m_graphicsView->sceneRect().toRect().size();
QSize sceneSizeWithMarigins = sceneSize + QSize(130, 125);
// 如果通过调整resize来调整缩放
if (m_graphicsView->scaleFactor() < 1 || size().expandedTo(sceneSizeWithMarigins) != size()) {
QSize screenSize = qApp->screenAt(QCursor::pos())->availableSize();
if (screenSize.expandedTo(sceneSize) == screenSize) {
// 通过增加窗口大小来显示图片(当图片大小小于窗口大小但是大于默认窗口大小时候)
QSize finalSize = (screenSize.expandedTo(sceneSizeWithMarigins) == screenSize) ?
sceneSizeWithMarigins : screenSize;
this->resize(finalSize.expandedTo(this->sizeHint()));
m_graphicsView->resetScale();
centerWindow();
} else {
showMaximized();
}
}
}
QUrl MainWindow::currentImageFileUrl()
{
if (m_currentFileIndex != -1) {
return m_files.value(m_currentFileIndex);
}
return QUrl();
}
void MainWindow::clearGallery()
{
m_currentFileIndex = -1;
m_files.clear();
}
void MainWindow::loadGalleryBySingleLocalFile(const QString &path)
{
QFileInfo info(path);
QDir dir(info.path());
QString currentFileName = info.fileName();
QStringList entryList = dir.entryList({"*.jpg", "*.jpeg", ".jfif", "*.png", "*.gif", "*.svg", "*.bmp"},
QDir::Files | QDir::NoSymLinks, QDir::NoSort);
QCollator collator;
collator.setNumericMode(true);
std::sort(entryList.begin(), entryList.end(), collator);
clearGallery();
for (int i = 0; i < entryList.count(); i++) {
const QString &oneEntry = entryList.at(i);
m_files.append(QUrl::fromLocalFile(dir.absoluteFilePath(oneEntry)));
if (oneEntry == currentFileName) {
m_currentFileIndex = i;
}
}
emit galleryLoaded();
}
void MainWindow::galleryPrev()
{
int count = m_files.count();
if (!isGalleryAvailable()) {
return;
}
m_currentFileIndex = m_currentFileIndex - 1 < 0 ? count - 1 : m_currentFileIndex - 1;
m_graphicsView->showFileFromUrl(m_files.at(m_currentFileIndex), false);
}
void MainWindow::galleryNext()
{
int count = m_files.count();
if (!isGalleryAvailable()) {
return;
}
m_currentFileIndex = m_currentFileIndex + 1 == count ? 0 : m_currentFileIndex + 1;
m_graphicsView->showFileFromUrl(m_files.at(m_currentFileIndex), false);
}
bool MainWindow::isGalleryAvailable()
{
if (m_currentFileIndex < 0 || m_files.isEmpty() || m_currentFileIndex >= m_files.count()) {
return false;
}
return true;
}
void MainWindow::showEvent(QShowEvent *event)
{
updateWidgetsPosition();
return QMainWindow::showEvent(event);
}
void MainWindow::enterEvent(QEvent *event)
{
m_bottomButtonGroup->setOpacity(1);
m_gv->setOpacity(1);
m_closeButton->setOpacity(1);
m_prevButton->setOpacity(1);
m_nextButton->setOpacity(1);
return QMainWindow::enterEvent(event);
}
void MainWindow::leaveEvent(QEvent *event)
{
m_bottomButtonGroup->setOpacity(0);
m_gv->setOpacity(0);
m_closeButton->setOpacity(0);
m_prevButton->setOpacity(0);
m_nextButton->setOpacity(0);
return QMainWindow::leaveEvent(event);
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton && !isMaximized()) {
m_clickedOnWindow = true;
m_oldMousePos = event->pos();
event->accept();
}
return QMainWindow::mouseMoveEvent(event);
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton && m_clickedOnWindow && !isMaximized()) {
move(event->globalPos() - m_oldMousePos);
event->accept();
}
return QMainWindow::mouseMoveEvent(event);
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
m_clickedOnWindow = false;
return QMainWindow::mouseReleaseEvent(event);
}
void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
switch (Settings::instance()->doubleClickBehavior()) {
case ActionCloseWindow:
quitAppAction();
event->accept();
break;
case ActionMaximizeWindow:
toggleMaximize();
event->accept();
break;
case ActionDoNothing:
break;
}
}
void MainWindow::wheelEvent(QWheelEvent *event)
{
QPoint numDegress = event->angleDelta() / 8;
bool needZoom = false, zoomIn = false;
// 在X11上QWheelEvent::pixelDelta()可能会失效
if (!numDegress.isNull() && numDegress.y() != 0) {
needZoom = true;
zoomIn = numDegress.y() > 0;
}
if (needZoom) {
m_graphicsView->zoomView(zoomIn ? 1.25 : 0.8);
event->accept();
} else {
QMainWindow::wheelEvent(event);
}
}
void MainWindow::resizeEvent(QResizeEvent *event)
{
updateWidgetsPosition();
return QMainWindow::resizeEvent(event);
}
void MainWindow::contextMenuEvent(QContextMenuEvent *event)
{
QMenu *menu = new QMenu;
QMenu *copyMenu = new QMenu(tr("&Copy"));
QUrl currentFileUrl = currentImageFileUrl();
QImage clipboardImage;
QUrl clipboardFileUrl;
const QMimeData *clipboardData = QApplication::clipboard()->mimeData();
if (clipboardData->hasImage()) {
QVariant imageVariant(clipboardData->imageData());
if (imageVariant.isValid()) {
clipboardImage = qvariant_cast<QImage>(imageVariant);
}
} else if (clipboardData->hasText()) {
QString clipboardText(clipboardData->text());
if (clipboardText.startsWith("PICTURE:")) {
QString maybeFileName(clipboardText.mid(9));
if (QFile::exists(maybeFileName)) {
clipboardFileUrl = QUrl::fromLocalFile(maybeFileName);
}
}
}
QAction *copyPixmap = new QAction(tr("Copy &Pixmap"));
connect(copyPixmap, &QAction::triggered, this, [=]() {
QClipboard *cb = QApplication::clipboard();
cb->setPixmap(m_graphicsView->scene()->renderToPixmap());
});
QAction *copyFilePath = new QAction(tr("Copy &File Path"));
connect(copyFilePath, &QAction::triggered, this, [=]() {
QClipboard *cb = QApplication::clipboard();
cb->setText(currentFileUrl.toLocalFile());
});
copyMenu->addAction(copyPixmap);
if (currentFileUrl.isValid()) {
copyMenu->addAction(copyFilePath);
}
QAction *pasteImage = new QAction(tr("&Paste Image"));
connect(pasteImage, &QAction::triggered, this, [=](){
clearGallery();
m_graphicsView->showImage(clipboardImage);
});
QAction *pasteImageFile = new QAction(tr("&Paste Image File"));
connect(pasteImageFile, &QAction::triggered, this, [=](){
m_graphicsView->showFileFromUrl(clipboardFileUrl, true);
});
QAction *protectMode = new QAction(tr("Protected mode"));
connect(protectMode, &QAction::triggered, this, [=](){
toggleProtectMode();
});
QAction *stayOnTopMode = new QAction(tr("Stay on top"));
connect(stayOnTopMode, &QAction::triggered, this, [=](){
toggleStayOnTop();
});
stayOnTopMode->setCheckable(true);
stayOnTopMode->setCheckable(stayOnTop());
protectMode->setCheckable(true);
protectMode->setChecked(m_protectMode);
QAction *toggleSettings = new QAction(tr("Configure..."));
connect(toggleSettings, &QAction::triggered, this, [=]() {
SettingsDialog *sd = new SettingsDialog(this);
sd->exec();
sd->deleteLater();
});
QAction * helpAction = new QAction(tr("Help"));
connect(helpAction, &QAction::triggered, this, [ = ](){
QStringList sl {
tr("Launch application with image file path as argument to load the file."),
tr("Drag and drop image file onto the window is also supported."),
"",
tr("Context menu option explanation:"),
(tr("Stay on top") + " : " + tr("Make window stay on top of all other windows.")),
(tr("Protected mode") + " : " + tr("Avoid close window accidentally. (eg. by double clicking the window)"))
};
m_graphicsView->showText(sl.join('\n'));
});
if (copyMenu->actions().count() == 1) {
menu->addActions(copyMenu->actions());
} else {
menu->addMenu(copyMenu);
}
if (!clipboardImage.isNull()) {
menu->addAction(pasteImage);
} else if (clipboardFileUrl.isValid()) {
menu->addAction(pasteImageFile);
}
menu->addAction(stayOnTopMode);
menu->addAction(protectMode);
menu->addSeparator();
menu->addAction(toggleSettings);
menu->addAction(helpAction);
menu->exec(mapToGlobal(event->pos()));
menu->deleteLater();
return QMainWindow::contextMenuEvent(event);
}
QSize MainWindow::sizeHint() const
{
return QSize(350, 350);
}
void MainWindow::centerWindow()
{
this->setGeometry(
QStyle::alignedRect(
Qt::LeftToRight,
Qt::AlignCenter,
this->size(),
qApp->screenAt(QCursor::pos())->geometry()
)
);
}
void MainWindow::closeWindow()
{
m_floatUpAnimation->setStartValue(QRect(this->geometry().x(), this->geometry().y(), this->geometry().width(), this->geometry().height()));
m_floatUpAnimation->setEndValue(QRect(this->geometry().x(), this->geometry().y()-80, this->geometry().width(), this->geometry().height()));
m_exitAnimationGroup->start();
}
void MainWindow::updateWidgetsPosition()
{
m_closeButton->move(width() - m_closeButton->width(), 0);
m_prevButton->move(25, (height() - m_prevButton->height()) / 2);
m_nextButton->move(width() - m_nextButton->width() - 25,
(height() - m_prevButton->height()) / 2);
m_bottomButtonGroup->move((width() - m_bottomButtonGroup->width()) / 2,
height() - m_bottomButtonGroup->height());
m_gv->move(width() - m_gv->width(), height() - m_gv->height());
}
void MainWindow::toggleProtectMode()
{
m_protectMode = !m_protectMode;
m_closeButton->setVisible(!m_protectMode);
m_prevButton->setVisible(!m_protectMode);
m_nextButton->setVisible(!m_protectMode);
}
void MainWindow::toggleStayOnTop()
{
setWindowFlag(Qt::WindowStaysOnTopHint, !stayOnTop());
show();
}
bool MainWindow::stayOnTop()
{
return windowFlags().testFlag(Qt::WindowStaysOnTopHint);
}
void MainWindow::quitAppAction(bool force)
{
if (!m_protectMode || force) {
closeWindow();
}
}
void MainWindow::toggleFullscreen()
{
if (isFullScreen()) {
showNormal();
} else {
showFullScreen();
}
}
void MainWindow::toggleMaximize()
{
if (isMaximized()) {
showNormal();
} else {
showMaximized();
}
}