Skip to content

Commit

Permalink
feat: add prev and next button
Browse files Browse the repository at this point in the history
  • Loading branch information
BLumia committed Jul 4, 2020
1 parent 3b1af64 commit fb24e54
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <kbd>Meta+Drag</kbd> to resize window under my x11 desktop).

Expand Down
2 changes: 1 addition & 1 deletion graphicsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QImage>(mimeData->imageData());
Expand Down
79 changes: 79 additions & 0 deletions icons/go-next.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 79 additions & 0 deletions icons/go-previous.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 43 additions & 4 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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();
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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());
Expand All @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class MainWindow : public QMainWindow
void loadGalleryBySingleLocalFile(const QString &path);
void galleryPrev();
void galleryNext();
bool isGalleryAvailable();

signals:
void galleryLoaded();

protected slots:
void showEvent(QShowEvent *event) override;
Expand Down Expand Up @@ -61,6 +65,8 @@ protected slots:
QPropertyAnimation *m_floatUpAnimation;
QParallelAnimationGroup *m_exitAnimationGroup;
ToolButton *m_closeButton;
ToolButton *m_prevButton;
ToolButton *m_nextButton;
GraphicsView *m_graphicsView;
NavigatorView *m_gv;
BottomButtonGroup *m_bottomButtonGroup;
Expand Down
2 changes: 2 additions & 0 deletions resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
<file>icons/view-background-checkerboard.svg</file>
<file>icons/app-icon.svg</file>
<file>icons/window-close.svg</file>
<file>icons/go-next.svg</file>
<file>icons/go-previous.svg</file>
</qresource>
</RCC>
16 changes: 9 additions & 7 deletions toolbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>

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)
Expand Down
2 changes: 1 addition & 1 deletion toolbutton.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit fb24e54

Please sign in to comment.