1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-03-03 18:25:35 +00:00

Improve Qt client look on hi-dpi displays

This commit is contained in:
Mike Gelfand 2017-02-08 01:06:28 +03:00
parent 13e111a89a
commit caf3b7653c
12 changed files with 76 additions and 20 deletions

View file

@ -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 ();
}

View file

@ -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

View file

@ -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

View file

@ -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;

View file

@ -11,6 +11,7 @@
#include <QStylePainter>
#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<QPixmap> (variant); break;
case QVariant::Icon: pixmap = qvariant_cast<QIcon> (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);
}

View file

@ -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;
}

19
qt/StyleHelper.cc Normal file
View file

@ -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;
}

18
qt/StyleHelper.h Normal file
View file

@ -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 <QIcon>
#include <QStyle>
class StyleHelper
{
public:
static QIcon::Mode getIconMode (QStyle::State state);
};

View file

@ -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:

View file

@ -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<QIcon> (variant);
case QVariant::Pixmap:
return QIcon (qvariant_cast<QPixmap> (variant));
default:
return QIcon ();
}
}
bool
Utils::isValidUtf8 (const char * s)
{

View file

@ -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);

View file

@ -105,6 +105,7 @@ SOURCES += AboutDialog.cc \
SessionDialog.cc \
SqueezeLabel.cc \
StatsDialog.cc \
StyleHelper.cc \
Torrent.cc \
TorrentDelegate.cc \
TorrentDelegateMin.cc \