Skip to content

Commit fb24e54

Browse files
committed
feat: add prev and next button
1 parent 3b1af64 commit fb24e54

File tree

9 files changed

+221
-13
lines changed

9 files changed

+221
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Feel free to open up an issue to request an new language to translate.
2323

2424
- Mixed `CR LF` and `LF`.
2525
- Ugly action icons.
26+
- Ugly implementations.
2627
- For windows build, win32 APIs are used.
2728
- No drag-window-border-to-resize support under non-windows platforms (I use <kbd>Meta+Drag</kbd> to resize window under my x11 desktop).
2829

graphicsview.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ void GraphicsView::dropEvent(QDropEvent *event)
242242
if (urls.isEmpty()) {
243243
showText(tr("File url list is empty"));
244244
} else {
245-
showFileFromUrl(urls.first());
245+
showFileFromUrl(urls.first(), true);
246246
}
247247
} else if (mimeData->hasImage()) {
248248
QImage img = qvariant_cast<QImage>(mimeData->imageData());

icons/go-next.svg

Lines changed: 79 additions & 0 deletions
Loading

icons/go-previous.svg

Lines changed: 79 additions & 0 deletions
Loading

mainwindow.cpp

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ MainWindow::MainWindow(QWidget *parent) :
3131
this->setAttribute(Qt::WA_TranslucentBackground, true);
3232
this->setMinimumSize(710, 530);
3333
this->setWindowIcon(QIcon(":/icons/app-icon.svg"));
34+
this->setMouseTracking(true);
3435

3536
m_fadeOutAnimation = new QPropertyAnimation(this, "windowOpacity");
3637
m_fadeOutAnimation->setDuration(300);
@@ -72,13 +73,29 @@ MainWindow::MainWindow(QWidget *parent) :
7273
connect(m_graphicsView, &GraphicsView::requestGallery,
7374
this, &MainWindow::loadGalleryBySingleLocalFile);
7475

75-
m_closeButton = new ToolButton(m_graphicsView);
76+
m_closeButton = new ToolButton(true, m_graphicsView);
7677
m_closeButton->setIcon(QIcon(":/icons/window-close"));
7778
m_closeButton->setIconSize(QSize(50, 50));
7879

7980
connect(m_closeButton, &QAbstractButton::clicked,
8081
this, &MainWindow::closeWindow);
8182

83+
m_prevButton = new ToolButton(false, m_graphicsView);
84+
m_prevButton->setIcon(QIcon(":/icons/go-previous"));
85+
m_prevButton->setIconSize(QSize(75, 75));
86+
m_prevButton->setVisible(false);
87+
m_prevButton->setOpacity(0, false);
88+
m_nextButton = new ToolButton(false, m_graphicsView);
89+
m_nextButton->setIcon(QIcon(":/icons/go-next"));
90+
m_nextButton->setIconSize(QSize(75, 75));
91+
m_nextButton->setVisible(false);
92+
m_nextButton->setOpacity(0, false);
93+
94+
connect(m_prevButton, &QAbstractButton::clicked,
95+
this, &MainWindow::galleryPrev);
96+
connect(m_nextButton, &QAbstractButton::clicked,
97+
this, &MainWindow::galleryNext);
98+
8299
m_bottomButtonGroup = new BottomButtonGroup(this);
83100

84101
connect(m_bottomButtonGroup, &BottomButtonGroup::resetToOriginalBtnClicked,
@@ -109,6 +126,11 @@ MainWindow::MainWindow(QWidget *parent) :
109126
m_gv->setOpacity(0, false);
110127
m_closeButton->setOpacity(0, false);
111128

129+
connect(this, &MainWindow::galleryLoaded, this, [this]() {
130+
m_prevButton->setVisible(isGalleryAvailable());
131+
m_nextButton->setVisible(isGalleryAvailable());
132+
});
133+
112134
QShortcut * quitAppShorucut = new QShortcut(QKeySequence(Qt::Key_Space), this);
113135
connect(quitAppShorucut, &QShortcut::activated,
114136
std::bind(&MainWindow::quitAppAction, this, false));
@@ -222,13 +244,13 @@ void MainWindow::loadGalleryBySingleLocalFile(const QString &path)
222244
}
223245
}
224246

225-
// qDebug() << m_files << m_currentFileIndex;
247+
emit galleryLoaded();
226248
}
227249

228250
void MainWindow::galleryPrev()
229251
{
230252
int count = m_files.count();
231-
if (m_currentFileIndex < 0 || m_files.isEmpty() || m_currentFileIndex >= m_files.count()) {
253+
if (!isGalleryAvailable()) {
232254
return;
233255
}
234256

@@ -240,7 +262,7 @@ void MainWindow::galleryPrev()
240262
void MainWindow::galleryNext()
241263
{
242264
int count = m_files.count();
243-
if (m_currentFileIndex < 0 || m_files.isEmpty() || m_currentFileIndex >= m_files.count()) {
265+
if (!isGalleryAvailable()) {
244266
return;
245267
}
246268

@@ -249,6 +271,14 @@ void MainWindow::galleryNext()
249271
m_graphicsView->showFileFromUrl(m_files.at(m_currentFileIndex), false);
250272
}
251273

274+
bool MainWindow::isGalleryAvailable()
275+
{
276+
if (m_currentFileIndex < 0 || m_files.isEmpty() || m_currentFileIndex >= m_files.count()) {
277+
return false;
278+
}
279+
return true;
280+
}
281+
252282
void MainWindow::showEvent(QShowEvent *event)
253283
{
254284
updateWidgetsPosition();
@@ -262,6 +292,8 @@ void MainWindow::enterEvent(QEvent *event)
262292
m_gv->setOpacity(1);
263293

264294
m_closeButton->setOpacity(1);
295+
m_prevButton->setOpacity(1);
296+
m_nextButton->setOpacity(1);
265297

266298
return QMainWindow::enterEvent(event);
267299
}
@@ -272,6 +304,8 @@ void MainWindow::leaveEvent(QEvent *event)
272304
m_gv->setOpacity(0);
273305

274306
m_closeButton->setOpacity(0);
307+
m_prevButton->setOpacity(0);
308+
m_nextButton->setOpacity(0);
275309

276310
return QMainWindow::leaveEvent(event);
277311
}
@@ -537,6 +571,9 @@ void MainWindow::closeWindow()
537571
void MainWindow::updateWidgetsPosition()
538572
{
539573
m_closeButton->move(width() - m_closeButton->width(), 0);
574+
m_prevButton->move(25, (height() - m_prevButton->height()) / 2);
575+
m_nextButton->move(width() - m_nextButton->width() - 25,
576+
(height() - m_prevButton->height()) / 2);
540577
m_bottomButtonGroup->move((width() - m_bottomButtonGroup->width()) / 2,
541578
height() - m_bottomButtonGroup->height());
542579
m_gv->move(width() - m_gv->width(), height() - m_gv->height());
@@ -546,6 +583,8 @@ void MainWindow::toggleProtectedMode()
546583
{
547584
m_protectedMode = !m_protectedMode;
548585
m_closeButton->setVisible(!m_protectedMode);
586+
m_prevButton->setVisible(!m_protectedMode);
587+
m_nextButton->setVisible(!m_protectedMode);
549588
}
550589

551590
void MainWindow::toggleStayOnTop()

mainwindow.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class MainWindow : public QMainWindow
3131
void loadGalleryBySingleLocalFile(const QString &path);
3232
void galleryPrev();
3333
void galleryNext();
34+
bool isGalleryAvailable();
35+
36+
signals:
37+
void galleryLoaded();
3438

3539
protected slots:
3640
void showEvent(QShowEvent *event) override;
@@ -61,6 +65,8 @@ protected slots:
6165
QPropertyAnimation *m_floatUpAnimation;
6266
QParallelAnimationGroup *m_exitAnimationGroup;
6367
ToolButton *m_closeButton;
68+
ToolButton *m_prevButton;
69+
ToolButton *m_nextButton;
6470
GraphicsView *m_graphicsView;
6571
NavigatorView *m_gv;
6672
BottomButtonGroup *m_bottomButtonGroup;

resources.qrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88
<file>icons/view-background-checkerboard.svg</file>
99
<file>icons/app-icon.svg</file>
1010
<file>icons/window-close.svg</file>
11+
<file>icons/go-next.svg</file>
12+
<file>icons/go-previous.svg</file>
1113
</qresource>
1214
</RCC>

toolbutton.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66
#include <QGraphicsOpacityEffect>
77
#include <QPropertyAnimation>
88

9-
ToolButton::ToolButton(QWidget *parent)
9+
ToolButton::ToolButton(bool hoverColor, QWidget *parent)
1010
: QPushButton(parent)
1111
, m_opacityHelper(new OpacityHelper(this))
1212
{
1313
setFlat(true);
14-
setFixedSize(50, 50);
15-
setStyleSheet("QPushButton {"
14+
QString qss = "QPushButton {"
1615
"background: transparent;"
17-
"}"
18-
"QPushButton:hover {"
19-
"background: red;"
20-
"}");
16+
"}";
17+
if (hoverColor) {
18+
qss += "QPushButton:hover {"
19+
"background: red;"
20+
"}";
21+
}
22+
setStyleSheet(qss);
2123
}
2224

2325
void ToolButton::setOpacity(qreal opacity, bool animated)

toolbutton.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ToolButton : public QPushButton
88
{
99
Q_OBJECT
1010
public:
11-
ToolButton(QWidget * parent = nullptr);
11+
ToolButton(bool hoverColor = false, QWidget * parent = nullptr);
1212

1313
public slots:
1414
void setOpacity(qreal opacity, bool animated = true);

0 commit comments

Comments
 (0)