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

Optimise status colours and icons. Closes #6174. #6186

Closed
wants to merge 8 commits into from
Closed
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ config.status
src/icons/qbt-theme/build-icons/node_modules/
src/icons/skin/build-icons/node_modules/
src/icons/skin/build-icons/icons/*.png
src/icons/status/build-icons/node_modules/

35 changes: 26 additions & 9 deletions src/base/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@
#include "logger.h"
#include "preferences.h"

Preferences* Preferences::m_instance = 0;
Preferences *Preferences::m_instance = 0;

Preferences::Preferences() {}
Preferences::Preferences()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

= default too?

{
}

Preferences *Preferences::instance()
{
Expand Down Expand Up @@ -141,6 +143,16 @@ void Preferences::setAlternatingRowColors(bool b)
setValue("Preferences/General/AlternatingRowColors", b);
}

bool Preferences::useLargeStatusIcons() const
{
return value("Preferences/General/LargeStatusIcons", false).toBool();
}

void Preferences::setLargeStatusIcons(bool b)
{
setValue("Preferences/General/LargeStatusIcons", b);
}

bool Preferences::getHideZeroValues() const
{
return value("Preferences/General/HideZeroValues", false).toBool();
Expand Down Expand Up @@ -250,6 +262,7 @@ void Preferences::setWinStartup(bool b)
settings.remove("qBittorrent");
}
}

#endif

// Downloads
Expand Down Expand Up @@ -712,6 +725,7 @@ void Preferences::useSystemIconTheme(bool enabled)
{
setValue("Preferences/Advanced/useSystemIconTheme", enabled);
}

#endif

bool Preferences::recursiveDownloadDisabled() const
Expand All @@ -725,7 +739,8 @@ void Preferences::disableRecursiveDownload(bool disable)
}

#ifdef Q_OS_WIN
namespace {
namespace
{
enum REG_SEARCH_TYPE
{
USER,
Expand Down Expand Up @@ -814,7 +829,7 @@ namespace {
versions.sort();

bool found = false;
while(!found && !versions.empty()) {
while (!found && !versions.empty()) {
const QString version = versions.takeLast() + "\\InstallPath";
LPWSTR lpSubkey = new WCHAR[version.size() + 1];
version.toWCharArray(lpSubkey);
Expand Down Expand Up @@ -844,7 +859,6 @@ namespace {

return path;
}

}

QString Preferences::getPythonPath()
Expand Down Expand Up @@ -950,6 +964,7 @@ void Preferences::setMagnetLinkAssoc(bool set)

SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);
}

#endif

#ifdef Q_OS_MAC
Expand Down Expand Up @@ -1006,6 +1021,7 @@ void Preferences::setMagnetLinkAssoc()
CFStringRef myBundleId = CFBundleGetIdentifier(CFBundleGetMainBundle());
LSSetDefaultHandlerForURLScheme(magnetUrlScheme, myBundleId);
}

#endif

int Preferences::getTrackerPort() const
Expand All @@ -1028,6 +1044,7 @@ void Preferences::setUpdateCheckEnabled(bool enabled)
{
setValue("Preferences/Advanced/updateCheck", enabled);
}

#endif

bool Preferences::confirmTorrentDeletion() const
Expand Down Expand Up @@ -1132,7 +1149,7 @@ void Preferences::setMainLastDir(const QString &path)
}

#ifndef DISABLE_GUI
QSize Preferences::getPrefSize(const QSize& defaultSize) const
QSize Preferences::getPrefSize(const QSize &defaultSize) const
{
return value("Preferences/State/size", defaultSize).toSize();
}
Expand All @@ -1141,6 +1158,7 @@ void Preferences::setPrefSize(const QSize &size)
{
setValue("Preferences/State/size", size);
}

#endif

QPoint Preferences::getPrefPos() const
Expand Down Expand Up @@ -1477,7 +1495,7 @@ void Preferences::setTransHeaderState(const QByteArray &state)
#endif
}

//From old RssSettings class
// From old RssSettings class
bool Preferences::isRSSEnabled() const
{
return value("Preferences/RSS/RSSEnabled", false).toBool();
Expand Down Expand Up @@ -1611,10 +1629,9 @@ void Preferences::upgrade()
QStringList labels = value("TransferListFilters/customLabels").toStringList();
if (!labels.isEmpty()) {
QVariantMap categories = value("BitTorrent/Session/Categories").toMap();
foreach (const QString &label, labels) {
foreach (const QString &label, labels)
if (!categories.contains(label))
categories[label] = "";
}
setValue("BitTorrent/Session/Categories", categories);
SettingsStorage::instance()->removeValue("TransferListFilters/customLabels");
}
Expand Down
8 changes: 5 additions & 3 deletions src/base/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ class Preferences: public QObject
const QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const;
void setValue(const QString &key, const QVariant &value);

static Preferences* m_instance;
static Preferences *m_instance;

signals:
void changed();

public:
static void initInstance();
static void freeInstance();
static Preferences* instance();
static Preferences *instance();

// General options
QString getLocale() const;
Expand All @@ -110,6 +110,8 @@ class Preferences: public QObject
void showSpeedInTitleBar(bool show);
bool useAlternatingRowColors() const;
void setAlternatingRowColors(bool b);
bool useLargeStatusIcons() const;
void setLargeStatusIcons(bool b);
bool getHideZeroValues() const;
void setHideZeroValues(bool b);
int getHideZeroComboValues() const;
Expand Down Expand Up @@ -332,7 +334,7 @@ class Preferences: public QObject
int getToolbarTextPosition() const;
void setToolbarTextPosition(const int position);

//From old RssSettings class
// From old RssSettings class
bool isRSSEnabled() const;
void setRSSEnabled(const bool enabled);
uint getRSSRefreshInterval() const;
Expand Down
110 changes: 103 additions & 7 deletions src/gui/guiiconprovider.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2017 Tim Delaney <[email protected]>
* Copyright (C) 2015 Vladimir Golovnev <[email protected]>
* Copyright (C) 2011 Christophe Dumez <[email protected]>
*
Expand Down Expand Up @@ -27,23 +28,37 @@
* exception statement from your version.
*/

#include "guiiconprovider.h"
#include "base/bittorrent/torrenthandle.h"
#include "base/preferences.h"
#include "guiiconprovider.h"
#include "torrentmodel.h"

#include <QApplication>
#include <QBitmap>
#include <QDebug>
#include <QHash>
#include <QIcon>
#include <QPair>
#include <QPalette>
#include <QPixmap>

#if (defined(Q_OS_UNIX) && !defined(Q_OS_MAC))
#include <QDir>
#include <QFile>
#endif

GuiIconProvider::GuiIconProvider(QObject *parent)
: IconProvider(parent)
, m_iconCache(new QHash<QPair<QString, BitTorrent::TorrentState>, QIcon>)
{
configure();
configure(true);
connect(Preferences::instance(), SIGNAL(changed()), SLOT(configure()));
}

GuiIconProvider::~GuiIconProvider() {}
GuiIconProvider::~GuiIconProvider()
{
delete m_iconCache;
}

void GuiIconProvider::initInstance()
{
Expand All @@ -64,7 +79,7 @@ QIcon GuiIconProvider::getIcon(const QString &iconId)
QIcon GuiIconProvider::getIcon(const QString &iconId, const QString &fallback)
{
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MAC))
if (m_useSystemTheme) {
if (m_themeFlags & SystemTheme) {
QIcon icon = QIcon::fromTheme(iconId);
if (icon.name() != iconId)
icon = QIcon::fromTheme(fallback, QIcon(IconProvider::getIconPath(iconId)));
Expand Down Expand Up @@ -117,12 +132,13 @@ QIcon GuiIconProvider::generateDifferentSizes(const QIcon &icon)

return newIcon;
}

#endif

QString GuiIconProvider::getIconPath(const QString &iconId)
{
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MAC))
if (m_useSystemTheme) {
if (m_themeFlags & SystemTheme) {
QString path = QDir::temp().absoluteFilePath(iconId + ".png");
if (!QFile::exists(path)) {
const QIcon icon = QIcon::fromTheme(iconId);
Expand All @@ -138,10 +154,90 @@ QString GuiIconProvider::getIconPath(const QString &iconId)
return IconProvider::getIconPath(iconId);
}

void GuiIconProvider::updateTheme()
{
configure();
}

void GuiIconProvider::configure()
void GuiIconProvider::configure(bool firstRun)
{
ThemeFlags themeFlags = 0;

#if (defined(Q_OS_UNIX) && !defined(Q_OS_MAC))
m_useSystemTheme = Preferences::instance()->useSystemIconTheme();
if (Preferences::instance()->useSystemIconTheme())
themeFlags |= SystemTheme;
#endif

// QPalette::Base is used for the background of widgets like list and tree views
QPalette pal = QApplication::palette();
QColor color = pal.color(QPalette::Active, QPalette::Base);
bool darkTheme = (color.lightness() < 127);

if (darkTheme)
themeFlags |= DarkTheme;

const Preferences *const pref = Preferences::instance();

if (pref->useLargeStatusIcons())
themeFlags |= LargeStatusIcons;

if (firstRun || (themeFlags != m_themeFlags)) {
qDebug().nospace() << "Changing status icon theme to " << ((themeFlags & DarkTheme) ? "dark" : "light")
<< ", " << ((themeFlags & LargeStatusIcons) ? "large" : "small") << " icons";
m_iconCache->clear();
m_themeFlags = themeFlags;
emit themeChanged();
}
}

GuiIconProvider::ThemeFlags GuiIconProvider::getThemeFlags()
{
return m_themeFlags;
}

QIcon GuiIconProvider::getStatusIcon(const QString &iconId)
{
return getStatusIcon(iconId, BitTorrent::TorrentState::Unknown, true);
}

QIcon GuiIconProvider::getStatusIcon(const QString &iconId, const BitTorrent::TorrentState &state)
{
return getStatusIcon(iconId, state, false);
}

QIcon GuiIconProvider::getStatusIcon(const QString &iconId, const BitTorrent::TorrentState &state, bool ignoreState)
{
QString path(":/icons/status/" + iconId + ".png");
QPair<QString, BitTorrent::TorrentState> key(path, state);
QIcon icon((*m_iconCache)[key]);

if (!icon.isNull())
return icon;

QPixmap pixmap(path);

if (!ignoreState) {
QPixmap colored(pixmap.size());
colored.fill(TorrentModel::getIconColorByState(state));
colored.setMask(pixmap.createMaskFromColor(Qt::transparent));
pixmap = colored;
}

if (m_themeFlags & LargeStatusIcons) {
// The icon files have a fair amount of whitespace that we can crop off
int wInset = pixmap.width() / 8;
int hInset = pixmap.height() / 8;
QRect rect(wInset, hInset, pixmap.width() - (wInset * 2), pixmap.height() - (hInset * 2));
qDebug() << "Resizing icon" << pixmap.rect() << "->" << rect;
pixmap = pixmap.copy(rect);
}

#if (defined(Q_OS_UNIX) && !defined(Q_OS_MAC))
icon = generateDifferentSizes(QIcon(pixmap));
#else
icon = QIcon(pixmap);
#endif

(*m_iconCache)[key] = icon;
return icon;
}
Loading