diff --git a/bottombuttongroup.cpp b/bottombuttongroup.cpp index f2c3f98..573b5d1 100644 --- a/bottombuttongroup.cpp +++ b/bottombuttongroup.cpp @@ -1,5 +1,7 @@ #include "bottombuttongroup.h" +#include + #include #include #include @@ -24,17 +26,27 @@ BottomButtonGroup::BottomButtonGroup(QWidget *parent) "border-style: none;" "}"); - auto newBtn = [](QString text) -> QPushButton * { + auto newBtn = [](QString text, std::function func) -> QPushButton * { QPushButton * btn = new QPushButton(QIcon(QStringLiteral(":/icons/") + text), ""); btn->setIconSize(QSize(40, 40)); btn->setFixedSize(40, 40); + connect(btn, &QAbstractButton::clicked, btn, func); return btn; }; - addButton(newBtn("zoom-original")); - addButton(newBtn("view-fullscreen")); - addButton(newBtn("zoom-in")); - addButton(newBtn("zoom-out")); - addButton(newBtn("object-rorate-right")); + addButton(newBtn("zoom-original", [this]() { + emit resetToOriginalBtnClicked(); + })); + addButton(newBtn("view-fullscreen", [](){qDebug()<< "TODO: view-fullscreen";})); + addButton(newBtn("zoom-in", [this]() { + emit zoomInBtnClicked(); + })); + addButton(newBtn("zoom-out", [this]() { + emit zoomOutBtnClicked(); + })); + addButton(newBtn("view-background-checkerboard", [this]() { + emit toggleCheckerboardBtnClicked(); + })); + addButton(newBtn("object-rorate-right", [](){qDebug()<< "TODO: object-rorate-right";})); } void BottomButtonGroup::addButton(QAbstractButton *button) diff --git a/bottombuttongroup.h b/bottombuttongroup.h index 8e8e032..85f60ba 100644 --- a/bottombuttongroup.h +++ b/bottombuttongroup.h @@ -11,6 +11,12 @@ public: explicit BottomButtonGroup(QWidget *parent = nullptr); void addButton(QAbstractButton *button); + +signals: + void resetToOriginalBtnClicked(); + void zoomInBtnClicked(); + void zoomOutBtnClicked(); + void toggleCheckerboardBtnClicked(); }; #endif // BOTTOMBUTTONGROUP_H diff --git a/graphicsview.cpp b/graphicsview.cpp index 55bfb02..93c38a2 100644 --- a/graphicsview.cpp +++ b/graphicsview.cpp @@ -18,31 +18,35 @@ GraphicsView::GraphicsView(QWidget *parent) setStyleSheet("background-color: rgba(0, 0, 0, 220);" "border-radius: 3px;"); setAcceptDrops(true); + setCheckerboardEnabled(false); } void GraphicsView::showImage(const QPixmap &pixmap) { resetTransform(); scene()->showImage(pixmap); - if (!isThingSmallerThanWindowWith(transform())) { - m_enableFitInView = true; - fitInView(sceneRect(), Qt::KeepAspectRatio); - } + checkAndDoFitInView(); } void GraphicsView::showText(const QString &text) { + resetTransform(); scene()->showText(text); + checkAndDoFitInView(); } void GraphicsView::showSvg(const QString &filepath) { + resetTransform(); scene()->showSvg(filepath); + checkAndDoFitInView(); } void GraphicsView::showGif(const QString &filepath) { + resetTransform(); scene()->showGif(filepath); + checkAndDoFitInView(); } GraphicsScene *GraphicsView::scene() const @@ -55,6 +59,11 @@ void GraphicsView::setScene(GraphicsScene *scene) return QGraphicsView::setScene(scene); } +void GraphicsView::toggleCheckerboard() +{ + setCheckerboardEnabled(!m_checkerboardEnabled); +} + void GraphicsView::mousePressEvent(QMouseEvent *event) { if (shouldIgnoreMousePressMoveEvent(event)) { @@ -138,6 +147,11 @@ void GraphicsView::dropEvent(QDropEvent *event) const QMimeData * mimeData = event->mimeData(); if (mimeData->hasUrls()) { + if (mimeData->urls().isEmpty()) { + // yeah, it's possible. dragging QQ's original sticker will trigger this, for example. + showText("File url list is empty"); + return; + } QUrl url(mimeData->urls().first()); QString filePath(url.toLocalFile()); @@ -188,3 +202,30 @@ bool GraphicsView::shouldIgnoreMousePressMoveEvent(const QMouseEvent *event) con return false; } + +void GraphicsView::checkAndDoFitInView() +{ + if (!isThingSmallerThanWindowWith(transform())) { + m_enableFitInView = true; + fitInView(sceneRect(), Qt::KeepAspectRatio); + } +} + +void GraphicsView::setCheckerboardEnabled(bool enabled) +{ + m_checkerboardEnabled = enabled; + if (m_checkerboardEnabled) { + // Prepare background check-board pattern + QPixmap tilePixmap(0x20, 0x20); + tilePixmap.fill(QColor(30, 30, 30, 100)); + QPainter tilePainter(&tilePixmap); + QColor color(40, 40, 40, 100); + tilePainter.fillRect(0, 0, 0x10, 0x10, color); + tilePainter.fillRect(0x10, 0x10, 0x10, 0x10, color); + tilePainter.end(); + + setBackgroundBrush(tilePixmap); + } else { + setBackgroundBrush(Qt::transparent); + } +} diff --git a/graphicsview.h b/graphicsview.h index e6d42f8..cf75612 100644 --- a/graphicsview.h +++ b/graphicsview.h @@ -17,6 +17,9 @@ public: GraphicsScene * scene() const; void setScene(GraphicsScene *scene); +public slots: + void toggleCheckerboard(); + private: void mousePressEvent(QMouseEvent * event) override; void mouseMoveEvent(QMouseEvent * event) override; @@ -30,8 +33,11 @@ private: bool isThingSmallerThanWindowWith(const QTransform &transform) const; bool shouldIgnoreMousePressMoveEvent(const QMouseEvent *event) const; + void checkAndDoFitInView(); + void setCheckerboardEnabled(bool enabled); bool m_enableFitInView = false; + bool m_checkerboardEnabled = false; }; #endif // GRAPHICSVIEW_H diff --git a/icons/view-background-checkerboard.svg b/icons/view-background-checkerboard.svg new file mode 100644 index 0000000..20d2481 --- /dev/null +++ b/icons/view-background-checkerboard.svg @@ -0,0 +1,87 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + + + + diff --git a/mainwindow.cpp b/mainwindow.cpp index 88c00d4..0cd9590 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -57,6 +57,15 @@ MainWindow::MainWindow(QWidget *parent) : m_bottomButtonGroup = new BottomButtonGroup(this); + connect(m_bottomButtonGroup, &BottomButtonGroup::resetToOriginalBtnClicked, + this, [ = ](){ pictureView->setTransform(QTransform()); }); + connect(m_bottomButtonGroup, &BottomButtonGroup::zoomInBtnClicked, + this, [ = ](){ pictureView->scale(1.25, 1.25); }); + connect(m_bottomButtonGroup, &BottomButtonGroup::zoomOutBtnClicked, + this, [ = ](){ pictureView->scale(0.75, 0.75); }); + connect(m_bottomButtonGroup, &BottomButtonGroup::toggleCheckerboardBtnClicked, + this, [ = ](){ pictureView->toggleCheckerboard(); }); + this->setGeometry( QStyle::alignedRect( Qt::LeftToRight, diff --git a/resources.qrc b/resources.qrc index fb9d616..4c2ddc8 100644 --- a/resources.qrc +++ b/resources.qrc @@ -5,5 +5,6 @@ icons/view-fullscreen.svg icons/zoom-original.svg icons/object-rorate-right.svg + icons/view-background-checkerboard.svg