mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +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);
|
painter->setRenderHint(QPainter::Antialiasing, true);
|
||||||
const QPixmap active = QPixmap::fromImage(StarWidget::starActive());
|
const QPixmap active = QPixmap::fromImage(StarWidget::starActive());
|
||||||
const QPixmap inactive = QPixmap::fromImage(StarWidget::starInactive());
|
const QPixmap inactive = QPixmap::fromImage(StarWidget::starInactive());
|
||||||
|
const StarMetrics& metrics = StarWidget::metrics();
|
||||||
|
|
||||||
for (int i = 0; i < rating; i++)
|
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++)
|
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();
|
painter->restore();
|
||||||
}
|
}
|
||||||
|
|
||||||
QSize StarWidgetsDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
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)
|
ComboBoxDelegate::ComboBoxDelegate(QAbstractItemModel *model, QObject *parent) : QStyledItemDelegate(parent), model(model)
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
|
|
||||||
QImage StarWidget::activeStar;
|
QImage StarWidget::activeStar;
|
||||||
QImage StarWidget::inactiveStar;
|
QImage StarWidget::inactiveStar;
|
||||||
|
StarMetrics StarWidget::imgMetrics = { -1, -1 };
|
||||||
|
|
||||||
const QImage& StarWidget::starActive()
|
const QImage& StarWidget::starActive()
|
||||||
{
|
{
|
||||||
|
@ -22,6 +23,11 @@ const QImage& StarWidget::starInactive()
|
||||||
return inactiveStar;
|
return inactiveStar;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const StarMetrics& StarWidget::metrics()
|
||||||
|
{
|
||||||
|
return imgMetrics;
|
||||||
|
}
|
||||||
|
|
||||||
int StarWidget::currentStars() const
|
int StarWidget::currentStars() const
|
||||||
{
|
{
|
||||||
return current;
|
return current;
|
||||||
|
@ -33,7 +39,7 @@ void StarWidget::mouseReleaseEvent(QMouseEvent *event)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int starClicked = event->pos().x() / IMG_SIZE + 1;
|
int starClicked = event->pos().x() / imgMetrics.size + 1;
|
||||||
if (starClicked > TOTALSTARS)
|
if (starClicked > TOTALSTARS)
|
||||||
starClicked = TOTALSTARS;
|
starClicked = TOTALSTARS;
|
||||||
|
|
||||||
|
@ -53,10 +59,10 @@ void StarWidget::paintEvent(QPaintEvent *event)
|
||||||
QPixmap inactive = QPixmap::fromImage(starInactive());
|
QPixmap inactive = QPixmap::fromImage(starInactive());
|
||||||
|
|
||||||
for (int i = 0; i < current; i++)
|
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++)
|
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()) {
|
if (hasFocus()) {
|
||||||
QStyleOptionFocusRect option;
|
QStyleOptionFocusRect option;
|
||||||
|
@ -77,14 +83,25 @@ StarWidget::StarWidget(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f),
|
||||||
current(0),
|
current(0),
|
||||||
readOnly(false)
|
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()) {
|
if (activeStar.isNull()) {
|
||||||
QSvgRenderer render(QString(":star"));
|
QSvgRenderer render(QString(":star"));
|
||||||
QPixmap renderedStar(IMG_SIZE, IMG_SIZE);
|
QPixmap renderedStar(imgMetrics.size, imgMetrics.size);
|
||||||
|
|
||||||
renderedStar.fill(Qt::transparent);
|
renderedStar.fill(Qt::transparent);
|
||||||
QPainter painter(&renderedStar);
|
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();
|
activeStar = renderedStar.toImage();
|
||||||
}
|
}
|
||||||
if (inactiveStar.isNull()) {
|
if (inactiveStar.isNull()) {
|
||||||
|
@ -113,7 +130,7 @@ QImage grayImage(const QImage& coloredImg)
|
||||||
|
|
||||||
QSize StarWidget::sizeHint() const
|
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)
|
void StarWidget::setReadOnly(bool r)
|
||||||
|
|
|
@ -4,11 +4,14 @@
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
enum StarConfig {
|
enum StarConfig {
|
||||||
SPACING = 2,
|
|
||||||
IMG_SIZE = 16,
|
|
||||||
TOTALSTARS = 5
|
TOTALSTARS = 5
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct StarMetrics {
|
||||||
|
int size;
|
||||||
|
int spacing;
|
||||||
|
};
|
||||||
|
|
||||||
class StarWidget : public QWidget {
|
class StarWidget : public QWidget {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
|
@ -19,6 +22,7 @@ public:
|
||||||
|
|
||||||
static const QImage& starActive();
|
static const QImage& starActive();
|
||||||
static const QImage& starInactive();
|
static const QImage& starInactive();
|
||||||
|
static const StarMetrics& metrics();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void valueChanged(int stars);
|
void valueChanged(int stars);
|
||||||
|
@ -41,6 +45,7 @@ private:
|
||||||
|
|
||||||
static QImage activeStar;
|
static QImage activeStar;
|
||||||
static QImage inactiveStar;
|
static QImage inactiveStar;
|
||||||
|
static StarMetrics imgMetrics;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // STARWIDGET_H
|
#endif // STARWIDGET_H
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue