mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-06-05 21:59:17 +02:00
Issue #99: Icons for dark mode
This commit is contained in:
@ -28,6 +28,7 @@
|
|||||||
#include "pdfdocumentmanipulator.h"
|
#include "pdfdocumentmanipulator.h"
|
||||||
#include "pdfdocumentbuilder.h"
|
#include "pdfdocumentbuilder.h"
|
||||||
#include "pdfdocumentwriter.h"
|
#include "pdfdocumentwriter.h"
|
||||||
|
#include "pdfwidgetutils.h"
|
||||||
|
|
||||||
#include <QToolBar>
|
#include <QToolBar>
|
||||||
#include <QDesktopServices>
|
#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::progressStarted, this, &MainWindow::onProgressStarted);
|
||||||
connect(m_progress, &pdf::PDFProgress::progressStep, this, &MainWindow::onProgressStep);
|
connect(m_progress, &pdf::PDFProgress::progressStep, this, &MainWindow::onProgressStep);
|
||||||
connect(m_progress, &pdf::PDFProgress::progressFinished, this, &MainWindow::onProgressFinished);
|
connect(m_progress, &pdf::PDFProgress::progressFinished, this, &MainWindow::onProgressFinished);
|
||||||
|
@ -192,6 +192,11 @@ MainWindow::MainWindow(QWidget* parent) :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (pdf::PDFWidgetUtils::isDarkTheme())
|
||||||
|
{
|
||||||
|
pdf::PDFWidgetUtils::convertActionsForDarkTheme(actions, iconSize, qGuiApp->devicePixelRatio());
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize pixmap cache size
|
// Initialize pixmap cache size
|
||||||
const int depth = 4; // 4 bytes (ARGB)
|
const int depth = 4; // 4 bytes (ARGB)
|
||||||
const int reserveSize = 2; // Caching of two screens
|
const int reserveSize = 2; // Caching of two screens
|
||||||
|
@ -16,11 +16,15 @@
|
|||||||
// along with PDF4QT. If not, see <https://www.gnu.org/licenses/>.
|
// along with PDF4QT. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
#include "pdfwidgetutils.h"
|
#include "pdfwidgetutils.h"
|
||||||
|
#include "pdfcolorconvertor.h"
|
||||||
#include "pdfdbgheap.h"
|
#include "pdfdbgheap.h"
|
||||||
|
|
||||||
#include <QDialog>
|
#include <QDialog>
|
||||||
#include <QLayout>
|
#include <QLayout>
|
||||||
|
#include <QPainter>
|
||||||
|
#include <QPixmap>
|
||||||
#include <QGroupBox>
|
#include <QGroupBox>
|
||||||
|
#include <QApplication>
|
||||||
|
|
||||||
#ifdef Q_OS_MAC
|
#ifdef Q_OS_MAC
|
||||||
int qt_default_dpi_x() { return 72; }
|
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
|
} // namespace pdf
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
|
|
||||||
#include "pdfglobal.h"
|
#include "pdfglobal.h"
|
||||||
|
|
||||||
|
#include <QIcon>
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
namespace pdf
|
namespace pdf
|
||||||
@ -57,6 +58,35 @@ public:
|
|||||||
|
|
||||||
/// Apply style to the widget
|
/// Apply style to the widget
|
||||||
static void style(QWidget* 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
|
} // namespace pdf
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
#include "pdfdocumentbuilder.h"
|
#include "pdfdocumentbuilder.h"
|
||||||
#include "pdfdbgheap.h"
|
#include "pdfdbgheap.h"
|
||||||
#include "pdfcertificatemanagerdialog.h"
|
#include "pdfcertificatemanagerdialog.h"
|
||||||
|
#include "pdfwidgetutils.h"
|
||||||
|
|
||||||
#include "pdfviewersettings.h"
|
#include "pdfviewersettings.h"
|
||||||
#include "pdfundoredomanager.h"
|
#include "pdfundoredomanager.h"
|
||||||
@ -292,6 +293,20 @@ void PDFActionManager::initActions(QSize iconSize, bool initializeStampActions)
|
|||||||
createCreateStampAction(CreateStampSold, pdf::Stamp::Sold);
|
createCreateStampAction(CreateStampSold, pdf::Stamp::Sold);
|
||||||
createCreateStampAction(CreateStampTopSecret, pdf::Stamp::TopSecret);
|
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
|
bool PDFActionManager::hasActions(const std::initializer_list<Action>& actionTypes) const
|
||||||
|
@ -230,6 +230,7 @@ public:
|
|||||||
void addAdditionalAction(QAction* action);
|
void addAdditionalAction(QAction* action);
|
||||||
|
|
||||||
void initActions(QSize iconSize, bool initializeStampActions);
|
void initActions(QSize iconSize, bool initializeStampActions);
|
||||||
|
void styleActions();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool hasActions(const std::initializer_list<Action>& actionTypes) const;
|
bool hasActions(const std::initializer_list<Action>& actionTypes) const;
|
||||||
@ -238,6 +239,7 @@ private:
|
|||||||
std::array<QAction*, LastAction> m_actions;
|
std::array<QAction*, LastAction> m_actions;
|
||||||
std::array<QActionGroup*, LastActionGroup> m_actionGroups;
|
std::array<QActionGroup*, LastActionGroup> m_actionGroups;
|
||||||
std::vector<QAction*> m_additionalActions;
|
std::vector<QAction*> m_additionalActions;
|
||||||
|
QSize m_iconSize;
|
||||||
};
|
};
|
||||||
|
|
||||||
class PDF4QTVIEWERLIBSHARED_EXPORT PDFProgramController : public QObject, public pdf::IPluginDataExchange
|
class PDF4QTVIEWERLIBSHARED_EXPORT PDFProgramController : public QObject, public pdf::IPluginDataExchange
|
||||||
|
@ -301,6 +301,8 @@ PDFViewerMainWindow::PDFViewerMainWindow(QWidget* parent) :
|
|||||||
{
|
{
|
||||||
connect(toolManager, &pdf::PDFToolManager::messageDisplayRequest, statusBar(), &QStatusBar::showMessage);
|
connect(toolManager, &pdf::PDFToolManager::messageDisplayRequest, statusBar(), &QStatusBar::showMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_actionManager->styleActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
PDFViewerMainWindow::~PDFViewerMainWindow()
|
PDFViewerMainWindow::~PDFViewerMainWindow()
|
||||||
|
@ -218,6 +218,8 @@ PDFViewerMainWindowLite::PDFViewerMainWindowLite(QWidget* parent) :
|
|||||||
{
|
{
|
||||||
connect(toolManager, &pdf::PDFToolManager::messageDisplayRequest, statusBar(), &QStatusBar::showMessage);
|
connect(toolManager, &pdf::PDFToolManager::messageDisplayRequest, statusBar(), &QStatusBar::showMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_actionManager->styleActions();
|
||||||
}
|
}
|
||||||
|
|
||||||
PDFViewerMainWindowLite::~PDFViewerMainWindowLite()
|
PDFViewerMainWindowLite::~PDFViewerMainWindowLite()
|
||||||
|
Reference in New Issue
Block a user