Simplify coloring of common ForkAwesome icons

The normal QPalette can be used to determine the icon color so it is
unnecessary to use the flag for custom bright colors here (which is only
intended for colors which are not on the standard QPalette).
This commit is contained in:
Martchus 2021-10-16 21:20:12 +02:00
parent de3205df00
commit 19bede39a2
11 changed files with 74 additions and 47 deletions

View File

@ -12,7 +12,8 @@ SyncthingDirActions::SyncthingDirActions(const SyncthingDir &dir, const Syncthin
: QObject(parent)
, m_dirId(dir.id)
{
const auto &icons = !data || !data->isUsingBrightCustomColors() ? forkAwesomeIconsForLightTheme() : forkAwesomeIconsForDarkTheme();
Q_UNUSED(data)
const auto &icons = commonForkAwesomeIcons();
m_infoAction.setSeparator(true);
m_infoAction.setIcon(icons.folder);
m_globalStatusAction.setIcon(icons.globe);

View File

@ -169,7 +169,7 @@ QVariant SyncthingDeviceModel::data(const QModelIndex &index, int role) const
case DeviceDetailIcon:
if (index.column() == 0) {
// attribute icons
const auto &icons = m_brightColors ? forkAwesomeIconsForDarkTheme() : forkAwesomeIconsForLightTheme();
const auto &icons = commonForkAwesomeIcons();
switch (index.row()) {
case 0:
return icons.hashtag;
@ -354,7 +354,12 @@ void SyncthingDeviceModel::devStatusChanged(const SyncthingDev &, int index)
void SyncthingDeviceModel::handleStatusIconsChanged()
{
emit dataChanged(index(0, 0), index(static_cast<int>(m_devs.size()) - 1, 0), QVector<int>({ Qt::DecorationRole }));
invalidateTopLevelIndicies(QVector<int>({ Qt::DecorationRole }));
}
void SyncthingDeviceModel::handleForkAwesomeIconsChanged()
{
invalidateNestedIndicies(QVector<int>({ Qt::DecorationRole, DeviceDetailIcon }));
}
QString SyncthingDeviceModel::devStatusString(const SyncthingDev &dev)

View File

@ -43,6 +43,7 @@ public Q_SLOTS:
private Q_SLOTS:
void devStatusChanged(const SyncthingDev &, int index);
void handleStatusIconsChanged() override;
void handleForkAwesomeIconsChanged() override;
private:
static QString devStatusString(const SyncthingDev &dev);

View File

@ -191,7 +191,7 @@ QVariant SyncthingDirectoryModel::data(const QModelIndex &index, int role) const
case DirectoryDetailIcon:
if (index.column() == 0) {
// attribute icons
const auto &icons = m_brightColors ? forkAwesomeIconsForDarkTheme() : forkAwesomeIconsForLightTheme();
const auto &icons = commonForkAwesomeIcons();
switch (row) {
case 0:
return icons.hashtag;
@ -445,7 +445,12 @@ void SyncthingDirectoryModel::handleNewConfigAvailable()
void SyncthingDirectoryModel::handleStatusIconsChanged()
{
emit dataChanged(index(0, 0), index(static_cast<int>(m_dirs.size()) - 1, 0), QVector<int>({ Qt::DecorationRole }));
invalidateTopLevelIndicies(QVector<int>({ Qt::DecorationRole }));
}
void SyncthingDirectoryModel::handleForkAwesomeIconsChanged()
{
invalidateNestedIndicies(QVector<int>({ Qt::DecorationRole, DirectoryDetailIcon }));
}
QString SyncthingDirectoryModel::dirStatusString(const SyncthingDir &dir)

View File

@ -46,6 +46,7 @@ private Q_SLOTS:
void handleConfigInvalidated() override;
void handleNewConfigAvailable() override;
void handleStatusIconsChanged() override;
void handleForkAwesomeIconsChanged() override;
private:
static QString dirStatusString(const SyncthingDir &dir);

View File

@ -9,6 +9,7 @@
#include <QFile>
#include <QGuiApplication>
#include <QPainter>
#include <QPalette>
#include <QStringBuilder>
#include <QSvgRenderer>
@ -301,9 +302,15 @@ ForkAwesomeIcons::ForkAwesomeIcons(QtForkAwesome::Renderer &renderer, const QCol
IconManager::IconManager()
: m_statusIcons()
, m_trayIcons(m_statusIcons)
, m_forkAwesomeIconsForLightTheme(m_forkAwesomeRenderer, QColor(10, 10, 10), QSize(64, 64))
, m_fontAwesomeIconsForDarkTheme(m_forkAwesomeRenderer, Qt::white, QSize(64, 64))
, m_commonForkAwesomeIcons(m_forkAwesomeRenderer, QGuiApplication::palette().color(QPalette::Normal, QPalette::Text), QSize(64, 64))
{
QObject::connect(qGuiApp, &QGuiApplication::paletteChanged, this, &IconManager::handlePaletteChanged);
}
void IconManager::handlePaletteChanged(const QPalette &pal)
{
emit forkAwesomeIconsChanged(
m_commonForkAwesomeIcons = ForkAwesomeIcons(m_forkAwesomeRenderer, pal.color(QPalette::Normal, QPalette::Text), QSize(64, 64)));
}
IconManager &IconManager::instance()

View File

@ -11,6 +11,7 @@
#include <vector>
QT_FORWARD_DECLARE_CLASS(QColor)
QT_FORWARD_DECLARE_CLASS(QPalette)
namespace Data {
@ -148,11 +149,14 @@ public:
const StatusIcons &statusIcons() const;
const StatusIcons &trayIcons() const;
QtForkAwesome::Renderer &forkAwesomeRenderer();
const ForkAwesomeIcons &forkAwesomeIconsForLightTheme() const;
const ForkAwesomeIcons &forkAwesomeIconsForDarkTheme() const;
const ForkAwesomeIcons &commonForkAwesomeIcons() const;
Q_SIGNALS:
void statusIconsChanged(const StatusIcons &newStatusIcons, const StatusIcons &newTrayIcons);
void forkAwesomeIconsChanged(const ForkAwesomeIcons &newForkAwesomeIcons);
private Q_SLOTS:
void handlePaletteChanged(const QPalette &pal);
private:
IconManager();
@ -160,8 +164,7 @@ private:
StatusIcons m_statusIcons;
StatusIcons m_trayIcons;
QtForkAwesome::Renderer m_forkAwesomeRenderer;
ForkAwesomeIcons m_forkAwesomeIconsForLightTheme;
ForkAwesomeIcons m_fontAwesomeIconsForDarkTheme;
ForkAwesomeIcons m_commonForkAwesomeIcons;
};
inline void IconManager::applySettings(const StatusIconSettings *statusIconSettings, const StatusIconSettings *trayIconSettings)
@ -194,14 +197,9 @@ inline QtForkAwesome::Renderer &IconManager::forkAwesomeRenderer()
return m_forkAwesomeRenderer;
}
inline const ForkAwesomeIcons &IconManager::forkAwesomeIconsForLightTheme() const
inline const ForkAwesomeIcons &IconManager::commonForkAwesomeIcons() const
{
return m_forkAwesomeIconsForLightTheme;
}
inline const ForkAwesomeIcons &IconManager::forkAwesomeIconsForDarkTheme() const
{
return m_fontAwesomeIconsForDarkTheme;
return m_commonForkAwesomeIcons;
}
inline const StatusIcons &statusIcons()
@ -214,14 +212,9 @@ inline const StatusIcons &trayIcons()
return IconManager::instance().trayIcons();
}
inline const ForkAwesomeIcons &forkAwesomeIconsForLightTheme()
inline const ForkAwesomeIcons &commonForkAwesomeIcons()
{
return IconManager::instance().forkAwesomeIconsForLightTheme();
}
inline const ForkAwesomeIcons &forkAwesomeIconsForDarkTheme()
{
return IconManager::instance().forkAwesomeIconsForDarkTheme();
return IconManager::instance().commonForkAwesomeIcons();
}
} // namespace Data

View File

@ -12,7 +12,10 @@ SyncthingModel::SyncthingModel(SyncthingConnection &connection, QObject *parent)
{
connect(&m_connection, &SyncthingConnection::newConfig, this, &SyncthingModel::handleConfigInvalidated);
connect(&m_connection, &SyncthingConnection::newConfigApplied, this, &SyncthingModel::handleNewConfigAvailable);
connect(&IconManager::instance(), &IconManager::statusIconsChanged, this, &SyncthingModel::handleStatusIconsChanged);
const auto &iconManager = IconManager::instance();
connect(&iconManager, &IconManager::statusIconsChanged, this, &SyncthingModel::handleStatusIconsChanged);
connect(&iconManager, &IconManager::forkAwesomeIconsChanged, this, &SyncthingModel::handleForkAwesomeIconsChanged);
}
const QVector<int> &SyncthingModel::colorRoles() const
@ -21,6 +24,22 @@ const QVector<int> &SyncthingModel::colorRoles() const
return colorRoles;
}
void SyncthingModel::invalidateTopLevelIndicies(const QVector<int> &affectedRoles)
{
emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1), affectedRoles);
}
void SyncthingModel::invalidateNestedIndicies(const QVector<int> &affectedRoles)
{
for (auto i = 0, rows = rowCount(); i != rows; ++i) {
const auto parentIndex = index(i, 0);
const auto childRows = rowCount(parentIndex);
if (childRows > 0) {
emit dataChanged(index(0, 0, parentIndex), index(childRows - 1, columnCount(parentIndex) - 1), affectedRoles);
}
}
}
void SyncthingModel::setBrightColors(bool brightColors)
{
if (m_brightColors == brightColors) {
@ -28,22 +47,8 @@ void SyncthingModel::setBrightColors(bool brightColors)
}
m_brightColors = brightColors;
const QVector<int> &affectedRoles = colorRoles();
if (affectedRoles.isEmpty()) {
return;
}
// update top-level indices
const auto rows = rowCount();
emit dataChanged(index(0, 0), index(rows - 1, columnCount() - 1), affectedRoles);
// update nested indices
for (auto i = 0; i != rows; ++i) {
const auto parentIndex = index(i, 0);
const auto childRows = rowCount(parentIndex);
if (childRows > 0) {
emit dataChanged(index(0, 0, parentIndex), index(childRows - 1, columnCount(parentIndex) - 1), affectedRoles);
}
if (const QVector<int> &affectedRoles = colorRoles(); !affectedRoles.isEmpty()) {
invalidateTopLevelIndicies(affectedRoles);
}
}
@ -61,4 +66,8 @@ void SyncthingModel::handleStatusIconsChanged()
{
}
void SyncthingModel::handleForkAwesomeIconsChanged()
{
}
} // namespace Data

View File

@ -27,11 +27,14 @@ public:
protected:
virtual const QVector<int> &colorRoles() const;
void invalidateTopLevelIndicies(const QVector<int> &affectedRoles);
void invalidateNestedIndicies(const QVector<int> &affectedRoles);
private Q_SLOTS:
virtual void handleConfigInvalidated();
virtual void handleNewConfigAvailable();
virtual void handleStatusIconsChanged();
virtual void handleForkAwesomeIconsChanged();
protected:
Data::SyncthingConnection &m_connection;

View File

@ -110,11 +110,7 @@ QVariant SyncthingRecentChangesModel::data(const QModelIndex &index, int role) c
case ActionIcon:
switch (index.column()) {
case 0:
if (change.fileChange.local) {
return m_brightColors ? forkAwesomeIconsForDarkTheme().home : forkAwesomeIconsForLightTheme().home;
} else {
return m_brightColors ? forkAwesomeIconsForDarkTheme().globe : forkAwesomeIconsForLightTheme().globe;
}
return change.fileChange.local ? commonForkAwesomeIcons().home : commonForkAwesomeIcons().globe;
}
break;
case Qt::ToolTipRole:
@ -218,6 +214,11 @@ void SyncthingRecentChangesModel::handleStatusChanged(SyncthingStatus status)
endResetModel();
}
void SyncthingRecentChangesModel::handleForkAwesomeIconsChanged()
{
invalidateTopLevelIndicies(QVector<int>({ Qt::DecorationRole, ActionIcon }));
}
void SyncthingRecentChangesModel::setMaxRows(int maxRows)
{
m_maxRows = maxRows < 0 ? std::numeric_limits<int>::max() : maxRows;

View File

@ -51,6 +51,7 @@ private Q_SLOTS:
void handleConfigInvalidated() override;
void handleNewConfigAvailable() override;
void handleStatusChanged(SyncthingStatus status);
void handleForkAwesomeIconsChanged() override;
private:
void ensureWithinLimit();