From caf3b7653c6125f1c99ec01a106f827316f945a0 Mon Sep 17 00:00:00 2001 From: Mike Gelfand Date: Wed, 8 Feb 2017 01:06:28 +0300 Subject: [PATCH] Improve Qt client look on hi-dpi displays --- qt/Application.cc | 3 +++ qt/CMakeLists.txt | 2 ++ qt/FaviconCache.cc | 4 +--- qt/FilterBar.cc | 4 ++-- qt/FilterBarComboBox.cc | 14 ++++---------- qt/FilterBarComboBoxDelegate.cc | 9 +++++---- qt/StyleHelper.cc | 19 +++++++++++++++++++ qt/StyleHelper.h | 18 ++++++++++++++++++ qt/TrackerModel.cc | 2 +- qt/Utils.cc | 17 +++++++++++++++++ qt/Utils.h | 3 +++ qt/qtr.pro | 1 + 12 files changed, 76 insertions(+), 20 deletions(-) create mode 100644 qt/StyleHelper.cc create mode 100644 qt/StyleHelper.h diff --git a/qt/Application.cc b/qt/Application.cc index 77909aca5..4051bb583 100644 --- a/qt/Application.cc +++ b/qt/Application.cc @@ -589,6 +589,9 @@ tr_main (int argc, { InteropHelper::initialize (); + Application::setAttribute (Qt::AA_EnableHighDpiScaling); + Application::setAttribute (Qt::AA_UseHighDpiPixmaps); + Application app (argc, argv); return app.exec (); } diff --git a/qt/CMakeLists.txt b/qt/CMakeLists.txt index 7822f9f02..6c9363c10 100644 --- a/qt/CMakeLists.txt +++ b/qt/CMakeLists.txt @@ -59,6 +59,7 @@ set(${PROJECT_NAME}_SOURCES SessionDialog.cc SqueezeLabel.cc StatsDialog.cc + StyleHelper.cc Torrent.cc TorrentDelegate.cc TorrentDelegateMin.cc @@ -119,6 +120,7 @@ set(${PROJECT_NAME}_HEADERS Speed.h SqueezeLabel.h StatsDialog.h + StyleHelper.h Torrent.h TorrentDelegate.h TorrentDelegateMin.h diff --git a/qt/FaviconCache.cc b/qt/FaviconCache.cc index f6915c073..420d72450 100644 --- a/qt/FaviconCache.cc +++ b/qt/FaviconCache.cc @@ -104,9 +104,7 @@ FaviconCache::findFromHost (const QString& host) { ensureCacheDirHasBeenScanned (); - const QPixmap pixmap = myPixmaps[host]; - const QSize rightSize = getIconSize (); - return pixmap.isNull () || pixmap.size () == rightSize ? pixmap : pixmap.scaled (rightSize); + return myPixmaps[host]; } void diff --git a/qt/FilterBar.cc b/qt/FilterBar.cc index fc4a07e70..d84b3d17a 100644 --- a/qt/FilterBar.cc +++ b/qt/FilterBar.cc @@ -147,7 +147,7 @@ FilterBar::refreshTrackers () const int count = torrentsPerHost[name]; row->setData (count, FilterBarComboBox::CountRole); row->setData (getCountString (count), FilterBarComboBox::CountStringRole); - row->setData (favicons.findFromHost (host), Qt::DecorationRole); + row->setData (QIcon (favicons.findFromHost (host)), Qt::DecorationRole); } // rows to remove @@ -182,7 +182,7 @@ FilterBar::refreshTrackers () const int count = torrentsPerHost[host]; row->setData (count, FilterBarComboBox::CountRole); row->setData (getCountString (count), FilterBarComboBox::CountStringRole); - row->setData (favicons.findFromHost (host), Qt::DecorationRole); + row->setData (QIcon (favicons.findFromHost (host)), Qt::DecorationRole); row->setData (host, TrackerRole); myTrackerModel->insertRow (i, row); anyAdded = true; diff --git a/qt/FilterBarComboBox.cc b/qt/FilterBarComboBox.cc index 2e2270557..91fb5d26e 100644 --- a/qt/FilterBarComboBox.cc +++ b/qt/FilterBarComboBox.cc @@ -11,6 +11,7 @@ #include #include "FilterBarComboBox.h" +#include "StyleHelper.h" #include "Utils.h" namespace @@ -109,19 +110,12 @@ FilterBarComboBox::paintEvent (QPaintEvent * e) rect.adjust (2, 1, -2, -1); // draw the icon - QPixmap pixmap; - QVariant variant = modelIndex.data (Qt::DecorationRole); - switch (variant.type ()) - { - case QVariant::Pixmap: pixmap = qvariant_cast (variant); break; - case QVariant::Icon: pixmap = qvariant_cast (variant).pixmap (iconSize ()); break; - default: break; - } - if (!pixmap.isNull ()) + const QIcon icon = Utils::getIconFromIndex (modelIndex); + if (!icon.isNull ()) { const QRect iconRect = QStyle::alignedRect(opt.direction, Qt::AlignLeft | Qt::AlignVCenter, opt.iconSize, rect); - painter.drawPixmap (iconRect.topLeft (), pixmap); + icon.paint (&painter, iconRect, Qt::AlignCenter, StyleHelper::getIconMode (opt.state), QIcon::Off); Utils::narrowRect (rect, iconRect.width () + hmargin, 0, opt.direction); } diff --git a/qt/FilterBarComboBoxDelegate.cc b/qt/FilterBarComboBoxDelegate.cc index 29c889627..ff67c27b9 100644 --- a/qt/FilterBarComboBoxDelegate.cc +++ b/qt/FilterBarComboBoxDelegate.cc @@ -13,6 +13,7 @@ #include "FilterBarComboBox.h" #include "FilterBarComboBoxDelegate.h" +#include "StyleHelper.h" #include "Utils.h" namespace @@ -86,10 +87,10 @@ FilterBarComboBoxDelegate::paint (QPainter * painter, Utils::narrowRect (boundingBox, 0, countRect.width () + hmargin, option.direction); const QRect displayRect = boundingBox; + const QIcon icon = Utils::getIconFromIndex (index); + drawBackground (painter, option, index); - QStyleOptionViewItem option2 = option; - option2.decorationSize = myCombo->iconSize (); - drawDecoration (painter, option, decorationRect, decoration (option2,index.data (Qt::DecorationRole))); + icon.paint (painter, decorationRect, Qt::AlignCenter, StyleHelper::getIconMode (option.state), QIcon::Off); drawDisplay (painter, option, displayRect, index.data (Qt::DisplayRole).toString ()); drawDisplay (painter, disabledOption, countRect, index.data (FilterBarComboBox::CountStringRole).toString ()); drawFocus (painter, option, displayRect|countRect); @@ -113,7 +114,7 @@ FilterBarComboBoxDelegate::sizeHint (const QStyleOptionViewItem & option, QSize size = QItemDelegate::sizeHint (option, index); size.setHeight (qMax (size.height (), myCombo->iconSize ().height () + 6)); size.rwidth () += s->pixelMetric (QStyle::PM_FocusFrameHMargin, 0, myCombo); - size.rwidth () += rect (option,index,FilterBarComboBox::CountStringRole).width (); + size.rwidth () += rect (option, index, FilterBarComboBox::CountStringRole).width (); size.rwidth () += hmargin * 4; return size; } diff --git a/qt/StyleHelper.cc b/qt/StyleHelper.cc new file mode 100644 index 000000000..7818266b6 --- /dev/null +++ b/qt/StyleHelper.cc @@ -0,0 +1,19 @@ +/* + * This file Copyright (C) 2017 Mnemosyne LLC + * + * It may be used under the GNU GPL versions 2 or 3 + * or any future license endorsed by Mnemosyne LLC. + * + */ + +#include "StyleHelper.h" + +QIcon::Mode +StyleHelper::getIconMode (QStyle::State state) +{ + if ((state & QStyle::State_Enabled) == 0) + return QIcon::Disabled; + if ((state & QStyle::State_Selected) != 0) + return QIcon::Selected; + return QIcon::Normal; +} diff --git a/qt/StyleHelper.h b/qt/StyleHelper.h new file mode 100644 index 000000000..821d0506a --- /dev/null +++ b/qt/StyleHelper.h @@ -0,0 +1,18 @@ +/* + * This file Copyright (C) 2017 Mnemosyne LLC + * + * It may be used under the GNU GPL versions 2 or 3 + * or any future license endorsed by Mnemosyne LLC. + * + */ + +#pragma once + +#include +#include + +class StyleHelper +{ + public: + static QIcon::Mode getIconMode (QStyle::State state); +}; diff --git a/qt/TrackerModel.cc b/qt/TrackerModel.cc index d72e3deca..d27c835b3 100644 --- a/qt/TrackerModel.cc +++ b/qt/TrackerModel.cc @@ -40,7 +40,7 @@ TrackerModel::data (const QModelIndex& index, int role) const break; case Qt::DecorationRole: - var = trackerInfo.st.getFavicon (); + var = QIcon (trackerInfo.st.getFavicon ()); break; case TrackerRole: diff --git a/qt/Utils.cc b/qt/Utils.cc index 6f1cd437f..ac67bbcc6 100644 --- a/qt/Utils.cc +++ b/qt/Utils.cc @@ -181,6 +181,23 @@ Utils::guessMimeIcon (const QString& filename) return fallback; } +QIcon +Utils::getIconFromIndex (const QModelIndex& index) +{ + const QVariant variant = index.data (Qt::DecorationRole); + switch (variant.type ()) + { + case QVariant::Icon: + return qvariant_cast (variant); + + case QVariant::Pixmap: + return QIcon (qvariant_cast (variant)); + + default: + return QIcon (); + } +} + bool Utils::isValidUtf8 (const char * s) { diff --git a/qt/Utils.h b/qt/Utils.h index ce73d97a6..60e9f19a7 100644 --- a/qt/Utils.h +++ b/qt/Utils.h @@ -18,11 +18,14 @@ class QAbstractItemView; class QColor; class QHeaderView; class QIcon; +class QModelIndex; class Utils { public: static QIcon guessMimeIcon (const QString& filename); + static QIcon getIconFromIndex (const QModelIndex& index); + // Test if string is UTF-8 or not static bool isValidUtf8 (const char * s); diff --git a/qt/qtr.pro b/qt/qtr.pro index 4598997f0..3d35805ca 100644 --- a/qt/qtr.pro +++ b/qt/qtr.pro @@ -105,6 +105,7 @@ SOURCES += AboutDialog.cc \ SessionDialog.cc \ SqueezeLabel.cc \ StatsDialog.cc \ + StyleHelper.cc \ Torrent.cc \ TorrentDelegate.cc \ TorrentDelegateMin.cc \ -- 2.40.0