mirror of
https://github.com/subsurface/subsurface.git
synced 2025-01-19 14:25:27 +00:00
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>
This commit is contained in:
parent
29851d956f
commit
0171368b6d
3 changed files with 35 additions and 11 deletions
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue