From fb24e54579bea2843e07ac29bc1a2c436015b0c3 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sat, 4 Jul 2020 13:49:12 +0800 Subject: [PATCH] feat: add prev and next button --- README.md | 1 + graphicsview.cpp | 2 +- icons/go-next.svg | 79 +++++++++++++++++++++++++++++++++++++++++++ icons/go-previous.svg | 79 +++++++++++++++++++++++++++++++++++++++++++ mainwindow.cpp | 47 ++++++++++++++++++++++--- mainwindow.h | 6 ++++ resources.qrc | 2 ++ toolbutton.cpp | 16 +++++---- toolbutton.h | 2 +- 9 files changed, 221 insertions(+), 13 deletions(-) create mode 100644 icons/go-next.svg create mode 100644 icons/go-previous.svg diff --git a/README.md b/README.md index 2ed6611..f331110 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Feel free to open up an issue to request an new language to translate. - Mixed `CR LF` and `LF`. - Ugly action icons. + - Ugly implementations. - For windows build, win32 APIs are used. - No drag-window-border-to-resize support under non-windows platforms (I use Meta+Drag to resize window under my x11 desktop). diff --git a/graphicsview.cpp b/graphicsview.cpp index adbe5d7..08dfc96 100644 --- a/graphicsview.cpp +++ b/graphicsview.cpp @@ -242,7 +242,7 @@ void GraphicsView::dropEvent(QDropEvent *event) if (urls.isEmpty()) { showText(tr("File url list is empty")); } else { - showFileFromUrl(urls.first()); + showFileFromUrl(urls.first(), true); } } else if (mimeData->hasImage()) { QImage img = qvariant_cast(mimeData->imageData()); diff --git a/icons/go-next.svg b/icons/go-next.svg new file mode 100644 index 0000000..4083e47 --- /dev/null +++ b/icons/go-next.svg @@ -0,0 +1,79 @@ + + + + + + + + + + + image/svg+xml + + + + + + + + + + diff --git a/icons/go-previous.svg b/icons/go-previous.svg new file mode 100644 index 0000000..640caea --- /dev/null +++ b/icons/go-previous.svg @@ -0,0 +1,79 @@ + + + + + + + + + + + image/svg+xml + + + + + + + + + + diff --git a/mainwindow.cpp b/mainwindow.cpp index 3f0f25b..5805b7d 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -31,6 +31,7 @@ MainWindow::MainWindow(QWidget *parent) : this->setAttribute(Qt::WA_TranslucentBackground, true); this->setMinimumSize(710, 530); this->setWindowIcon(QIcon(":/icons/app-icon.svg")); + this->setMouseTracking(true); m_fadeOutAnimation = new QPropertyAnimation(this, "windowOpacity"); m_fadeOutAnimation->setDuration(300); @@ -72,13 +73,29 @@ MainWindow::MainWindow(QWidget *parent) : connect(m_graphicsView, &GraphicsView::requestGallery, this, &MainWindow::loadGalleryBySingleLocalFile); - m_closeButton = new ToolButton(m_graphicsView); + m_closeButton = new ToolButton(true, m_graphicsView); m_closeButton->setIcon(QIcon(":/icons/window-close")); m_closeButton->setIconSize(QSize(50, 50)); connect(m_closeButton, &QAbstractButton::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, @@ -109,6 +126,11 @@ MainWindow::MainWindow(QWidget *parent) : 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 * quitAppShorucut = new QShortcut(QKeySequence(Qt::Key_Space), this); connect(quitAppShorucut, &QShortcut::activated, std::bind(&MainWindow::quitAppAction, this, false)); @@ -222,13 +244,13 @@ void MainWindow::loadGalleryBySingleLocalFile(const QString &path) } } -// qDebug() << m_files << m_currentFileIndex; + emit galleryLoaded(); } void MainWindow::galleryPrev() { int count = m_files.count(); - if (m_currentFileIndex < 0 || m_files.isEmpty() || m_currentFileIndex >= m_files.count()) { + if (!isGalleryAvailable()) { return; } @@ -240,7 +262,7 @@ void MainWindow::galleryPrev() void MainWindow::galleryNext() { int count = m_files.count(); - if (m_currentFileIndex < 0 || m_files.isEmpty() || m_currentFileIndex >= m_files.count()) { + if (!isGalleryAvailable()) { return; } @@ -249,6 +271,14 @@ void MainWindow::galleryNext() 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(); @@ -262,6 +292,8 @@ void MainWindow::enterEvent(QEvent *event) m_gv->setOpacity(1); m_closeButton->setOpacity(1); + m_prevButton->setOpacity(1); + m_nextButton->setOpacity(1); return QMainWindow::enterEvent(event); } @@ -272,6 +304,8 @@ void MainWindow::leaveEvent(QEvent *event) m_gv->setOpacity(0); m_closeButton->setOpacity(0); + m_prevButton->setOpacity(0); + m_nextButton->setOpacity(0); return QMainWindow::leaveEvent(event); } @@ -537,6 +571,9 @@ void MainWindow::closeWindow() 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()); @@ -546,6 +583,8 @@ void MainWindow::toggleProtectedMode() { m_protectedMode = !m_protectedMode; m_closeButton->setVisible(!m_protectedMode); + m_prevButton->setVisible(!m_protectedMode); + m_nextButton->setVisible(!m_protectedMode); } void MainWindow::toggleStayOnTop() diff --git a/mainwindow.h b/mainwindow.h index ef3c65c..6d9d798 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -31,6 +31,10 @@ public: void loadGalleryBySingleLocalFile(const QString &path); void galleryPrev(); void galleryNext(); + bool isGalleryAvailable(); + +signals: + void galleryLoaded(); protected slots: void showEvent(QShowEvent *event) override; @@ -61,6 +65,8 @@ private: QPropertyAnimation *m_floatUpAnimation; QParallelAnimationGroup *m_exitAnimationGroup; ToolButton *m_closeButton; + ToolButton *m_prevButton; + ToolButton *m_nextButton; GraphicsView *m_graphicsView; NavigatorView *m_gv; BottomButtonGroup *m_bottomButtonGroup; diff --git a/resources.qrc b/resources.qrc index 71ea699..89fa435 100644 --- a/resources.qrc +++ b/resources.qrc @@ -8,5 +8,7 @@ icons/view-background-checkerboard.svg icons/app-icon.svg icons/window-close.svg + icons/go-next.svg + icons/go-previous.svg diff --git a/toolbutton.cpp b/toolbutton.cpp index 689b4cb..3057ba5 100644 --- a/toolbutton.cpp +++ b/toolbutton.cpp @@ -6,18 +6,20 @@ #include #include -ToolButton::ToolButton(QWidget *parent) +ToolButton::ToolButton(bool hoverColor, QWidget *parent) : QPushButton(parent) , m_opacityHelper(new OpacityHelper(this)) { setFlat(true); - setFixedSize(50, 50); - setStyleSheet("QPushButton {" + QString qss = "QPushButton {" "background: transparent;" - "}" - "QPushButton:hover {" - "background: red;" - "}"); + "}"; + if (hoverColor) { + qss += "QPushButton:hover {" + "background: red;" + "}"; + } + setStyleSheet(qss); } void ToolButton::setOpacity(qreal opacity, bool animated) diff --git a/toolbutton.h b/toolbutton.h index f6db8f5..d4e902f 100644 --- a/toolbutton.h +++ b/toolbutton.h @@ -8,7 +8,7 @@ class ToolButton : public QPushButton { Q_OBJECT public: - ToolButton(QWidget * parent = nullptr); + ToolButton(bool hoverColor = false, QWidget * parent = nullptr); public slots: void setOpacity(qreal opacity, bool animated = true);