Issue #99: Icons for dark mode

This commit is contained in:
Jakub Melka 2023-11-26 19:15:20 +01:00
parent f5cb2a17af
commit 1c5c7da5ba
8 changed files with 110 additions and 0 deletions

View File

@ -28,6 +28,7 @@
#include "pdfdocumentmanipulator.h"
#include "pdfdocumentbuilder.h"
#include "pdfdocumentwriter.h"
#include "pdfwidgetutils.h"
#include <QToolBar>
#include <QDesktopServices>
@ -164,6 +165,11 @@ MainWindow::MainWindow(QWidget* parent) :
}
}
if (pdf::PDFWidgetUtils::isDarkTheme())
{
pdf::PDFWidgetUtils::convertActionsForDarkTheme(actions, iconSize, qGuiApp->devicePixelRatio());
}
connect(m_progress, &pdf::PDFProgress::progressStarted, this, &MainWindow::onProgressStarted);
connect(m_progress, &pdf::PDFProgress::progressStep, this, &MainWindow::onProgressStep);
connect(m_progress, &pdf::PDFProgress::progressFinished, this, &MainWindow::onProgressFinished);

View File

@ -192,6 +192,11 @@ MainWindow::MainWindow(QWidget* parent) :
}
}
if (pdf::PDFWidgetUtils::isDarkTheme())
{
pdf::PDFWidgetUtils::convertActionsForDarkTheme(actions, iconSize, qGuiApp->devicePixelRatio());
}
// Initialize pixmap cache size
const int depth = 4; // 4 bytes (ARGB)
const int reserveSize = 2; // Caching of two screens

View File

@ -16,11 +16,15 @@
// along with PDF4QT. If not, see <https://www.gnu.org/licenses/>.
#include "pdfwidgetutils.h"
#include "pdfcolorconvertor.h"
#include "pdfdbgheap.h"
#include <QDialog>
#include <QLayout>
#include <QPainter>
#include <QPixmap>
#include <QGroupBox>
#include <QApplication>
#ifdef Q_OS_MAC
int qt_default_dpi_x() { return 72; }
@ -161,4 +165,48 @@ void PDFWidgetUtils::style(QWidget* widget)
}
}
bool PDFWidgetUtils::isDarkTheme()
{
QPalette palette = QApplication::palette();
QColor backgroundColor = palette.color(QPalette::Window);
QColor textColor = palette.color(QPalette::WindowText);
return backgroundColor.lightness() < textColor.lightness();
}
void PDFWidgetUtils::convertActionForDarkTheme(QAction* action, QSize iconSize, qreal devicePixelRatioF)
{
if (!action)
{
return;
}
QIcon icon = action->icon();
if (!icon.isNull())
{
icon = pdf::PDFWidgetUtils::convertIconForDarkTheme(icon, iconSize, devicePixelRatioF);
action->setIcon(icon);
}
}
QIcon PDFWidgetUtils::convertIconForDarkTheme(QIcon icon, QSize iconSize, qreal devicePixelRatioF)
{
QPixmap pixmap(iconSize * devicePixelRatioF);
pixmap.setDevicePixelRatio(devicePixelRatioF);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
icon.paint(&painter, QRect(0, 0, iconSize.width(), iconSize.height()));
painter.end();
PDFColorConvertor convertor;
convertor.setMode(PDFColorConvertor::Mode::InvertedColors);
QImage image = pixmap.toImage();
image = convertor.convert(image);
pixmap = QPixmap::fromImage(image);
return QIcon(pixmap);
}
} // namespace pdf

View File

@ -20,6 +20,7 @@
#include "pdfglobal.h"
#include <QIcon>
#include <QWidget>
namespace pdf
@ -57,6 +58,35 @@ public:
/// Apply style to the widget
static void style(QWidget* widget);
/// Vrátí true, pokud je aktuálně nastavená dark theme
/// pro aplikaci.
static bool isDarkTheme();
/// Converts an action's icon for use in a dark theme.
/// \param action Pointer to the QAction to be converted.
/// \param iconSize The size of the action's icon.
/// \param devicePixelRatioF The device pixel ratio factor.
static void convertActionForDarkTheme(QAction* action, QSize iconSize, qreal devicePixelRatioF);
/// Converts an icon for use in a dark theme.
/// \param icon The icon to be converted.
/// \param iconSize The size of the icon.
/// \param devicePixelRatioF The device pixel ratio factor.
static QIcon convertIconForDarkTheme(QIcon icon, QSize iconSize, qreal devicePixelRatioF);
/// Converts an action's icon for use in a dark theme.
/// \param action Pointer to the QAction to be converted.
/// \param iconSize The size of the action's icon.
/// \param devicePixelRatioF The device pixel ratio factor.
template<typename Container>
static void convertActionsForDarkTheme(const Container& actions, QSize iconSize, qreal devicePixelRatioF)
{
for (QAction* action : actions)
{
convertActionForDarkTheme(action, iconSize, devicePixelRatioF);
}
}
};
} // namespace pdf

View File

@ -27,6 +27,7 @@
#include "pdfdocumentbuilder.h"
#include "pdfdbgheap.h"
#include "pdfcertificatemanagerdialog.h"
#include "pdfwidgetutils.h"
#include "pdfviewersettings.h"
#include "pdfundoredomanager.h"
@ -292,6 +293,20 @@ void PDFActionManager::initActions(QSize iconSize, bool initializeStampActions)
createCreateStampAction(CreateStampSold, pdf::Stamp::Sold);
createCreateStampAction(CreateStampTopSecret, pdf::Stamp::TopSecret);
}
m_iconSize = iconSize;
}
void PDFActionManager::styleActions()
{
if (pdf::PDFWidgetUtils::isDarkTheme())
{
qreal devicePixelRatio = qGuiApp->devicePixelRatio();
// Convert icons to dark theme icons
pdf::PDFWidgetUtils::convertActionsForDarkTheme(m_actions, m_iconSize, devicePixelRatio);
pdf::PDFWidgetUtils::convertActionsForDarkTheme(m_additionalActions, m_iconSize, devicePixelRatio);
}
}
bool PDFActionManager::hasActions(const std::initializer_list<Action>& actionTypes) const

View File

@ -230,6 +230,7 @@ public:
void addAdditionalAction(QAction* action);
void initActions(QSize iconSize, bool initializeStampActions);
void styleActions();
private:
bool hasActions(const std::initializer_list<Action>& actionTypes) const;
@ -238,6 +239,7 @@ private:
std::array<QAction*, LastAction> m_actions;
std::array<QActionGroup*, LastActionGroup> m_actionGroups;
std::vector<QAction*> m_additionalActions;
QSize m_iconSize;
};
class PDF4QTVIEWERLIBSHARED_EXPORT PDFProgramController : public QObject, public pdf::IPluginDataExchange

View File

@ -301,6 +301,8 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget* parent) :
{
connect(toolManager, &pdf::PDFToolManager::messageDisplayRequest, statusBar(), &QStatusBar::showMessage);
}
m_actionManager->styleActions();
}
PDFViewerMainWindow::~PDFViewerMainWindow()

View File

@ -218,6 +218,8 @@ PDFViewerMainWindowLite::PDFViewerMainWindowLite(QWidget* parent) :
{
connect(toolManager, &pdf::PDFToolManager::messageDisplayRequest, statusBar(), &QStatusBar::showMessage);
}
m_actionManager->styleActions();
}
PDFViewerMainWindowLite::~PDFViewerMainWindowLite()