summaryrefslogtreecommitdiffstats
path: root/qt-ui
diff options
context:
space:
mode:
authorGravatar Giuseppe Bilotta <giuseppe.bilotta@gmail.com>2014-10-15 15:30:46 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2014-10-18 15:15:44 -0700
commit0171368b6dd32d02441096b358893f1e72d6848f (patch)
tree47687acb4ccf3812913da75127b780fea5a027a7 /qt-ui
parent29851d956f166f8f57a8878047df94b0b058c7eb (diff)
downloadsubsurface-0171368b6dd32d02441096b358893f1e72d6848f.tar.gz
Dynamic StarWidget metrics
The default IMG_SIZE and SPACING in the StarWidget are not appropriate for HiDPI displays. Replace them with StarMetrics which are auto-computed from the (default) font size (which Qt determines from the display DPI settings). Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-ui')
-rw-r--r--qt-ui/modeldelegates.cpp8
-rw-r--r--qt-ui/starwidget.cpp29
-rw-r--r--qt-ui/starwidget.h9
3 files changed, 35 insertions, 11 deletions
diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp
index c0b6522cf..70cce7d68 100644
--- a/qt-ui/modeldelegates.cpp
+++ b/qt-ui/modeldelegates.cpp
@@ -50,17 +50,19 @@ void StarWidgetsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &o
painter->setRenderHint(QPainter::Antialiasing, true);
const QPixmap active = QPixmap::fromImage(StarWidget::starActive());
const QPixmap inactive = QPixmap::fromImage(StarWidget::starInactive());
+ const StarMetrics& metrics = StarWidget::metrics();
for (int i = 0; i < rating; i++)
- painter->drawPixmap(option.rect.x() + i * IMG_SIZE + SPACING, option.rect.y() + deltaY, active);
+ painter->drawPixmap(option.rect.x() + i * metrics.size + metrics.spacing, option.rect.y() + deltaY, active);
for (int i = rating; i < TOTALSTARS; i++)
- painter->drawPixmap(option.rect.x() + i * IMG_SIZE + SPACING, option.rect.y() + deltaY, inactive);
+ painter->drawPixmap(option.rect.x() + i * metrics.size + metrics.spacing, option.rect.y() + deltaY, inactive);
painter->restore();
}
QSize StarWidgetsDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
- return QSize(IMG_SIZE * TOTALSTARS + SPACING * (TOTALSTARS - 1), IMG_SIZE);
+ const StarMetrics& metrics = StarWidget::metrics();
+ return QSize(metrics.size * TOTALSTARS + metrics.spacing * (TOTALSTARS - 1), metrics.size);
}
ComboBoxDelegate::ComboBoxDelegate(QAbstractItemModel *model, QObject *parent) : QStyledItemDelegate(parent), model(model)
diff --git a/qt-ui/starwidget.cpp b/qt-ui/starwidget.cpp
index c682d95e3..e12692c52 100644
--- a/qt-ui/starwidget.cpp
+++ b/qt-ui/starwidget.cpp
@@ -11,6 +11,7 @@
QImage StarWidget::activeStar;
QImage StarWidget::inactiveStar;
+StarMetrics StarWidget::imgMetrics = { -1, -1 };
const QImage& StarWidget::starActive()
{
@@ -22,6 +23,11 @@ const QImage& StarWidget::starInactive()
return inactiveStar;
}
+const StarMetrics& StarWidget::metrics()
+{
+ return imgMetrics;
+}
+
int StarWidget::currentStars() const
{
return current;
@@ -33,7 +39,7 @@ void StarWidget::mouseReleaseEvent(QMouseEvent *event)
return;
}
- int starClicked = event->pos().x() / IMG_SIZE + 1;
+ int starClicked = event->pos().x() / imgMetrics.size + 1;
if (starClicked > TOTALSTARS)
starClicked = TOTALSTARS;
@@ -53,10 +59,10 @@ void StarWidget::paintEvent(QPaintEvent *event)
QPixmap inactive = QPixmap::fromImage(starInactive());
for (int i = 0; i < current; i++)
- p.drawPixmap(i * IMG_SIZE + SPACING, 0, active);
+ p.drawPixmap(i * imgMetrics.size + imgMetrics.spacing, 0, active);
for (int i = current; i < TOTALSTARS; i++)
- p.drawPixmap(i * IMG_SIZE + SPACING, 0, inactive);
+ p.drawPixmap(i * imgMetrics.size + imgMetrics.spacing, 0, inactive);
if (hasFocus()) {
QStyleOptionFocusRect option;
@@ -77,14 +83,25 @@ StarWidget::StarWidget(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f),
current(0),
readOnly(false)
{
+ // compute image size, by rounding the font height to the nearest multiple of 16
+ if (imgMetrics.size == -1) {
+ int height = QFontMetrics(parent->font()).height();
+ imgMetrics.size = (height + 8)/16;
+ imgMetrics.size *= 16;
+ // enforce a minimum size
+ if (imgMetrics.size < 16)
+ imgMetrics.size = 16;
+ imgMetrics.spacing = imgMetrics.size/8;
+ }
+
if (activeStar.isNull()) {
QSvgRenderer render(QString(":star"));
- QPixmap renderedStar(IMG_SIZE, IMG_SIZE);
+ QPixmap renderedStar(imgMetrics.size, imgMetrics.size);
renderedStar.fill(Qt::transparent);
QPainter painter(&renderedStar);
- render.render(&painter, QRectF(0, 0, IMG_SIZE, IMG_SIZE));
+ render.render(&painter, QRectF(0, 0, imgMetrics.size, imgMetrics.size));
activeStar = renderedStar.toImage();
}
if (inactiveStar.isNull()) {
@@ -113,7 +130,7 @@ QImage grayImage(const QImage& coloredImg)
QSize StarWidget::sizeHint() const
{
- return QSize(IMG_SIZE * TOTALSTARS + SPACING * (TOTALSTARS - 1), IMG_SIZE);
+ return QSize(imgMetrics.size * TOTALSTARS + imgMetrics.spacing * (TOTALSTARS - 1), imgMetrics.size);
}
void StarWidget::setReadOnly(bool r)
diff --git a/qt-ui/starwidget.h b/qt-ui/starwidget.h
index 3103a43eb..90310f041 100644
--- a/qt-ui/starwidget.h
+++ b/qt-ui/starwidget.h
@@ -4,11 +4,14 @@
#include <QWidget>
enum StarConfig {
- SPACING = 2,
- IMG_SIZE = 16,
TOTALSTARS = 5
};
+struct StarMetrics {
+ int size;
+ int spacing;
+};
+
class StarWidget : public QWidget {
Q_OBJECT
public:
@@ -19,6 +22,7 @@ public:
static const QImage& starActive();
static const QImage& starInactive();
+ static const StarMetrics& metrics();
signals:
void valueChanged(int stars);
@@ -41,6 +45,7 @@ private:
static QImage activeStar;
static QImage inactiveStar;
+ static StarMetrics imgMetrics;
};
#endif // STARWIDGET_H