add checkerboard support

This commit is contained in:
Gary Wang 2019-09-30 23:02:44 +08:00
parent 89c21bc90b
commit 5067c04b91
7 changed files with 172 additions and 10 deletions

View File

@ -1,5 +1,7 @@
#include "bottombuttongroup.h"
#include <functional>
#include <QPushButton>
#include <QVBoxLayout>
#include <QDebug>
@ -24,17 +26,27 @@ BottomButtonGroup::BottomButtonGroup(QWidget *parent)
"border-style: none;"
"}");
auto newBtn = [](QString text) -> QPushButton * {
auto newBtn = [](QString text, std::function<void()> 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)

View File

@ -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

View File

@ -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);
}
}

View File

@ -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

View File

@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="32"
height="32"
viewBox="0 0 8.4666665 8.4666669"
version="1.1"
id="svg8"
inkscape:version="0.92.4 (5da689c313, 2019-01-14)"
sodipodi:docname="view-background-checkerboard.svg">
<defs
id="defs2" />
<sodipodi:namedview
id="base"
pagecolor="#000000"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.69803922"
inkscape:pageshadow="2"
inkscape:zoom="44.8"
inkscape:cx="14.891202"
inkscape:cy="11.596232"
inkscape:document-units="px"
inkscape:current-layer="layer1"
showgrid="false"
units="px"
inkscape:pagecheckerboard="true"
inkscape:window-width="1920"
inkscape:window-height="1001"
inkscape:window-x="-9"
inkscape:window-y="-9"
inkscape:window-maximized="1" />
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title />
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(0,-288.5333)">
<rect
style="fill:#ffffff;fill-opacity:0.87058824;stroke:none;stroke-width:0.37041664;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect814"
width="2.7434142"
height="2.9153748"
x="1.5945871"
y="289.86566" />
<rect
style="fill:#ffffff;fill-opacity:0.87058824;stroke:none;stroke-width:0.37041664;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect814-4"
width="2.6871748"
height="2.9440556"
x="4.3380013"
y="292.78696" />
<rect
style="fill:#ffffff;fill-opacity:0.07843137;stroke:#ffffff;stroke-width:0;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect814-2"
width="2.7455406"
height="2.9440758"
x="1.5924606"
y="292.78696" />
<rect
style="fill:#ffffff;fill-opacity:0.07843137;stroke:#ffffff;stroke-width:0;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect814-2-6"
width="2.6982937"
height="2.9440918"
x="4.3380013"
y="289.83694" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

@ -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,

View File

@ -5,5 +5,6 @@
<file>icons/view-fullscreen.svg</file>
<file>icons/zoom-original.svg</file>
<file>icons/object-rorate-right.svg</file>
<file>icons/view-background-checkerboard.svg</file>
</qresource>
</RCC>