Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checkerboard for transparent background #494

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/qvgraphicspixmapitem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "qvgraphicspixmapitem.h"
#include <QPainter>

void QVGraphicsPixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
if (drawCheckerBoardBackground) {
const auto w = pixmap().width();
const auto h = pixmap().height();
const auto gridSize = qMax(w / 32, 8);
static const QColor checkerBoardColors[] = {QColor(150, 150, 150), QColor(200, 200, 200)};
auto colorIdx = 0;
auto wCursor = 0;
auto hCursor = 0;

for (int i = 0; i < h; i += gridSize, ++hCursor) {
wCursor = 0;
for (int j = 0; j < w; j += gridSize, ++wCursor) {
colorIdx = (hCursor + wCursor) % 2;
painter->fillRect(j, i,
qMin(gridSize, w - j),
qMin(gridSize, h - i),
checkerBoardColors[colorIdx]);
}
}
}

QGraphicsPixmapItem::paint(painter, option, widget);
}
23 changes: 23 additions & 0 deletions src/qvgraphicspixmapitem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef QVGRAPHICSPIXMAPITEM_H
#define QVGRAPHICSPIXMAPITEM_H

#include <QGraphicsPixmapItem>

class QVGraphicsPixmapItem : public QGraphicsPixmapItem
{
public:
explicit QVGraphicsPixmapItem(QGraphicsItem *parent = nullptr)
: QGraphicsPixmapItem(parent) {}

explicit QVGraphicsPixmapItem(const QPixmap &pixmap, QGraphicsItem *parent = nullptr)
: QGraphicsPixmapItem(pixmap, parent) {}

void setDrawCheckerBoardBackground(bool en) {
drawCheckerBoardBackground = en;
}
private:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
bool drawCheckerBoardBackground{true};
};

#endif // QVGRAPHICSPIXMAPITEM_H
6 changes: 4 additions & 2 deletions src/qvgraphicsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <QtMath>
#include <QGestureEvent>
#include <QScrollBar>
#include "qvgraphicspixmapitem.h"

QVGraphicsView::QVGraphicsView(QWidget *parent) : QGraphicsView(parent)
{
Expand Down Expand Up @@ -58,8 +59,7 @@ QVGraphicsView::QVGraphicsView(QWidget *parent) : QGraphicsView(parent)
expensiveScaleTimerNew->setInterval(50);
connect(expensiveScaleTimerNew, &QTimer::timeout, this, [this]{scaleExpensively();});


loadedPixmapItem = new QGraphicsPixmapItem();
loadedPixmapItem = new QVGraphicsPixmapItem();
scene->addItem(loadedPixmapItem);

// Connect to settings signal
Expand Down Expand Up @@ -642,6 +642,8 @@ void QVGraphicsView::settingsUpdated()
}
setBackgroundBrush(newBrush);

loadedPixmapItem->setDrawCheckerBoardBackground(settingsManager.getBoolean("checkerboardBackground"));

//filtering
if (settingsManager.getBoolean("filteringenabled"))
loadedPixmapItem->setTransformationMode(Qt::SmoothTransformation);
Expand Down
4 changes: 3 additions & 1 deletion src/qvgraphicsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <QTimer>
#include <QFileInfo>

class QVGraphicsPixmapItem;

class QVGraphicsView : public QGraphicsView
{
Q_OBJECT
Expand Down Expand Up @@ -116,7 +118,7 @@ private slots:
private:


QGraphicsPixmapItem *loadedPixmapItem;
QVGraphicsPixmapItem *loadedPixmapItem;

bool isFilteringEnabled;
bool isScalingEnabled;
Expand Down
2 changes: 1 addition & 1 deletion src/qvimagecore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void QVImageCore::loadPixmap(const ReadData &readData, bool fromCache)

emit fileChanged();

QtConcurrent::run(&QVImageCore::requestCaching, this);
QtConcurrent::run(std::mem_fn(&QVImageCore::requestCaching), this);
}

void QVImageCore::closeImage()
Expand Down
3 changes: 3 additions & 0 deletions src/qvoptionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ void QVOptionsDialog::syncSettings(bool defaults, bool makeConnections)
transientSettings.insert("bgcolor", ui->bgColorButton->text());
updateBgColorButton();
connect(ui->bgColorButton, &QPushButton::clicked, this, &QVOptionsDialog::bgColorButtonClicked);

syncCheckbox(ui->checkerboardBackgroundCheckbox, "checkerboardBackground", defaults, makeConnections);

// titlebarmode
syncRadioButtons({ui->titlebarRadioButton0, ui->titlebarRadioButton1,
ui->titlebarRadioButton2, ui->titlebarRadioButton3}, "titlebarmode", defaults, makeConnections);
Expand Down
41 changes: 24 additions & 17 deletions src/qvoptionsdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
</property>
</widget>
</item>
<item row="1" column="0">
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="toolTip">
<string>Changes the amount of information displayed in the titlebar</string>
Expand All @@ -60,14 +60,14 @@
</property>
</widget>
</item>
<item row="1" column="1">
<item row="2" column="1">
<widget class="QRadioButton" name="titlebarRadioButton0">
<property name="text">
<string>&amp;Basic</string>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="3" column="1">
<widget class="QRadioButton" name="titlebarRadioButton1">
<property name="text">
<string>&amp;Minimal</string>
Expand All @@ -77,21 +77,21 @@
</property>
</widget>
</item>
<item row="3" column="1">
<item row="4" column="1">
<widget class="QRadioButton" name="titlebarRadioButton2">
<property name="text">
<string>&amp;Practical</string>
</property>
</widget>
</item>
<item row="4" column="1">
<item row="5" column="1">
<widget class="QRadioButton" name="titlebarRadioButton3">
<property name="text">
<string>&amp;Verbose</string>
</property>
</widget>
</item>
<item row="6" column="1">
<item row="7" column="1">
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand All @@ -104,7 +104,7 @@
</property>
</spacer>
</item>
<item row="7" column="0">
<item row="8" column="0">
<widget class="QLabel" name="label_6">
<property name="toolTip">
<string>Control when the window should resize to fit the image's actual size</string>
Expand All @@ -114,7 +114,7 @@
</property>
</widget>
</item>
<item row="7" column="1">
<item row="8" column="1">
<widget class="QComboBox" name="windowResizeComboBox">
<property name="toolTip">
<string>Control when the window should resize to fit the image's actual size</string>
Expand All @@ -139,14 +139,14 @@
</item>
</widget>
</item>
<item row="8" column="0">
<item row="9" column="0">
<widget class="QLabel" name="minWindowResizeLabel">
<property name="text">
<string>Minimum size:</string>
</property>
</widget>
</item>
<item row="8" column="1">
<item row="9" column="1">
<widget class="QSpinBox" name="minWindowResizeSpinBox">
<property name="toolTip">
<string>Control the minimum size that the window should reach when matching the image's actual size</string>
Expand All @@ -168,7 +168,7 @@
</property>
</widget>
</item>
<item row="9" column="0">
<item row="10" column="0">
<widget class="QLabel" name="maxWindowResizeLabel">
<property name="toolTip">
<string>Control the maximum size that the window should reach when matching the image's actual size</string>
Expand All @@ -178,7 +178,7 @@
</property>
</widget>
</item>
<item row="9" column="1">
<item row="10" column="1">
<widget class="QSpinBox" name="maxWindowResizeSpinBox">
<property name="toolTip">
<string>Control the maximum size that the window should reach when matching the image's actual size</string>
Expand All @@ -200,7 +200,7 @@
</property>
</widget>
</item>
<item row="10" column="1">
<item row="11" column="1">
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand All @@ -213,7 +213,7 @@
</property>
</spacer>
</item>
<item row="13" column="1">
<item row="14" column="1">
<widget class="QCheckBox" name="darkTitlebarCheckbox">
<property name="toolTip">
<string>Choose whether or not the titlebar should always be dark regardless of your chosen macOS appearance</string>
Expand All @@ -226,14 +226,14 @@
</property>
</widget>
</item>
<item row="11" column="1">
<item row="12" column="1">
<widget class="QCheckBox" name="menubarCheckbox">
<property name="text">
<string>Show menubar</string>
</property>
</widget>
</item>
<item row="12" column="1">
<item row="13" column="1">
<widget class="QCheckBox" name="detailsInFullscreen">
<property name="toolTip">
<string>Choose whether or not to display the titlebar text while in fullscreen</string>
Expand All @@ -243,13 +243,20 @@
</property>
</widget>
</item>
<item row="14" column="1">
<item row="15" column="1">
<widget class="QCheckBox" name="quitOnLastWindowCheckbox">
<property name="text">
<string>&amp;Quit on last window closed</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QCheckBox" name="checkerboardBackgroundCheckbox">
<property name="text">
<string>Checkerboard for transparent background </string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="image">
Expand Down
1 change: 1 addition & 0 deletions src/settingsmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ void SettingsManager::initializeSettingsLibrary()
// Window
settingsLibrary.insert("bgcolorenabled", {true, {}});
settingsLibrary.insert("bgcolor", {"#212121", {}});
settingsLibrary.insert("checkerboardBackground", {false, {}});
settingsLibrary.insert("titlebarmode", {1, {}});
settingsLibrary.insert("windowresizemode", {1, {}});
settingsLibrary.insert("minwindowresizedpercentage", {20, {}});
Expand Down
2 changes: 2 additions & 0 deletions src/src.pri
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ SOURCES += \
$$PWD/main.cpp \
$$PWD/mainwindow.cpp \
$$PWD/openwith.cpp \
$$PWD/qvgraphicspixmapitem.cpp \
$$PWD/qvgraphicsview.cpp \
$$PWD/qvoptionsdialog.cpp \
$$PWD/qvapplication.cpp \
Expand All @@ -22,6 +23,7 @@ win32:!CONFIG(NO_WIN32):SOURCES += $$PWD/qvwin32functions.cpp
HEADERS += \
$$PWD/mainwindow.h \
$$PWD/openwith.h \
$$PWD/qvgraphicspixmapitem.h \
$$PWD/qvgraphicsview.h \
$$PWD/qvoptionsdialog.h \
$$PWD/qvapplication.h \
Expand Down