Unify icon metrics

Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Giuseppe Bilotta 2014-10-15 15:30:52 +02:00 committed by Dirk Hohndel
parent 72b256de62
commit 32ab2b34d3
9 changed files with 72 additions and 70 deletions

View file

@ -8,6 +8,8 @@
#include "metrics.h"
static IconMetrics dfltIconMetrics = { -1 };
QFont defaultModelFont()
{
QFont font;
@ -22,7 +24,7 @@ QFontMetrics defaultModelFontMetrics()
// return the default icon size, computed as the multiple of 16 closest to
// the given height
int defaultIconSize(int height)
static int defaultIconSize(int height)
{
int ret = (height + 8)/16;
ret *= 16;
@ -30,3 +32,19 @@ int defaultIconSize(int height)
ret = 16;
return ret;
}
const IconMetrics & defaultIconMetrics()
{
if (dfltIconMetrics.sz_small == -1) {
int small = defaultIconSize(defaultModelFontMetrics().height());
dfltIconMetrics.sz_small = small;
dfltIconMetrics.sz_med = small + small/2;
dfltIconMetrics.sz_big = 2*small;
dfltIconMetrics.sz_pic = 8*small;
dfltIconMetrics.spacing = small/8;
}
return dfltIconMetrics;
}

View file

@ -10,12 +10,23 @@
#include <QFont>
#include <QFontMetrics>
#include <QSize>
QFont defaultModelFont();
QFontMetrics defaultModelFontMetrics();
// return the default icon size, computed as the multiple of 16 closest to
// the given height (that defaults to the default font height)
int defaultIconSize(int height = defaultModelFontMetrics().height());
// Collection of icon/picture sizes and other metrics, resolution independent
struct IconMetrics {
// icon sizes
int sz_small; // ex 16px
int sz_med; // ex 24px
int sz_big; // ex 32px
// picture size
int sz_pic; // ex 128px
// icon spacing
int spacing; // ex 2px
};
const IconMetrics & defaultIconMetrics();
#endif // METRICS_H

View file

@ -32,8 +32,8 @@ static bool keyboardFinished = false;
StarWidgetsDelegate::StarWidgetsDelegate(QWidget *parent) : QStyledItemDelegate(parent),
parentWidget(parent)
{
const StarMetrics& metrics = StarWidget::metrics();
minStarSize = QSize(metrics.size * TOTALSTARS + metrics.spacing * (TOTALSTARS - 1), metrics.size);
const IconMetrics& metrics = defaultIconMetrics();
minStarSize = QSize(metrics.sz_small * TOTALSTARS + metrics.spacing * (TOTALSTARS - 1), metrics.sz_small);
}
void StarWidgetsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
@ -52,12 +52,12 @@ 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();
const IconMetrics& metrics = defaultIconMetrics();
for (int i = 0; i < rating; i++)
painter->drawPixmap(option.rect.x() + i * metrics.size + metrics.spacing, option.rect.y() + deltaY, active);
painter->drawPixmap(option.rect.x() + i * metrics.sz_small + metrics.spacing, option.rect.y() + deltaY, active);
for (int i = rating; i < TOTALSTARS; i++)
painter->drawPixmap(option.rect.x() + i * metrics.size + metrics.spacing, option.rect.y() + deltaY, inactive);
painter->drawPixmap(option.rect.x() + i * metrics.sz_small + metrics.spacing, option.rect.y() + deltaY, inactive);
painter->restore();
}

View file

@ -19,17 +19,17 @@
#include "display.h"
#endif
ToolTipItem::IconMetrics ToolTipItem::iconMetrics = { -1, -1, -1, -1 };
void ToolTipItem::addToolTip(const QString &toolTip, const QIcon &icon, const QPixmap *pixmap)
{
const IconMetrics& iconMetrics = defaultIconMetrics();
QGraphicsPixmapItem *iconItem = 0, *pixmapItem = 0;
double yValue = title->boundingRect().height() + iconMetrics.spacing;
Q_FOREACH (ToolTip t, toolTips) {
yValue += t.second->boundingRect().height();
}
if (!icon.isNull()) {
iconItem = new QGraphicsPixmapItem(icon.pixmap(iconMetrics.small, iconMetrics.small), this);
iconItem = new QGraphicsPixmapItem(icon.pixmap(iconMetrics.sz_small, iconMetrics.sz_small), this);
iconItem->setPos(iconMetrics.spacing, yValue);
} else {
if (pixmap && !pixmap->isNull()) {
@ -39,7 +39,7 @@ void ToolTipItem::addToolTip(const QString &toolTip, const QIcon &icon, const QP
}
QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(toolTip, this);
textItem->setPos(iconMetrics.spacing + iconMetrics.small + iconMetrics.spacing, yValue);
textItem->setPos(iconMetrics.spacing + iconMetrics.sz_small + iconMetrics.spacing, yValue);
textItem->setBrush(QBrush(Qt::white));
textItem->setFlag(ItemIgnoresTransformations);
toolTips.push_back(qMakePair(iconItem, textItem));
@ -89,10 +89,12 @@ void ToolTipItem::setRect(const QRectF &r)
void ToolTipItem::collapse()
{
int dim = defaultIconMetrics().sz_small;
QPropertyAnimation *animation = new QPropertyAnimation(this, "rect");
animation->setDuration(100);
animation->setStartValue(nextRectangle);
animation->setEndValue(QRect(0, 0, iconMetrics.small, iconMetrics.small));
animation->setEndValue(QRect(0, 0, dim, dim));
animation->start(QAbstractAnimation::DeleteWhenStopped);
clear();
@ -104,6 +106,8 @@ void ToolTipItem::expand()
if (!title)
return;
const IconMetrics& iconMetrics = defaultIconMetrics();
double width = 0, height = title->boundingRect().height() + iconMetrics.spacing;
Q_FOREACH (ToolTip t, toolTips) {
if (t.second->boundingRect().width() > width)
@ -111,13 +115,13 @@ void ToolTipItem::expand()
height += t.second->boundingRect().height();
}
/* Left padding, Icon Size, space, right padding */
width += iconMetrics.spacing + iconMetrics.small + iconMetrics.spacing + iconMetrics.spacing;
width += iconMetrics.spacing + iconMetrics.sz_small + iconMetrics.spacing + iconMetrics.spacing;
if (width < title->boundingRect().width() + iconMetrics.spacing * 2)
width = title->boundingRect().width() + iconMetrics.spacing * 2;
if (height < iconMetrics.small)
height = iconMetrics.small;
if (height < iconMetrics.sz_small)
height = iconMetrics.sz_small;
nextRectangle.setWidth(width);
nextRectangle.setHeight(height);
@ -139,14 +143,6 @@ ToolTipItem::ToolTipItem(QGraphicsItem *parent) : QGraphicsPathItem(parent),
timeAxis(0),
lastTime(-1)
{
// set icon sizes and spacing from the default icon size
if (iconMetrics.small == -1) {
iconMetrics.small = defaultIconSize();
iconMetrics.medium = iconMetrics.small + iconMetrics.small/2;
iconMetrics.big = iconMetrics.small*2;
iconMetrics.spacing = iconMetrics.small/4;
}
memset(&pInfo, 0, sizeof(pInfo));
setFlags(ItemIgnoresTransformations | ItemIsMovable | ItemClipsChildrenToShape);
@ -161,10 +157,11 @@ ToolTipItem::~ToolTipItem()
void ToolTipItem::updateTitlePosition()
{
const IconMetrics& iconMetrics = defaultIconMetrics();
if (rectangle.width() < title->boundingRect().width() + iconMetrics.spacing * 4) {
QRectF newRect = rectangle;
newRect.setWidth(title->boundingRect().width() + iconMetrics.spacing * 4);
newRect.setHeight((newRect.height() && isExpanded()) ? newRect.height() : iconMetrics.small);
newRect.setHeight((newRect.height() && isExpanded()) ? newRect.height() : iconMetrics.sz_small);
setRect(newRect);
}

View file

@ -28,13 +28,6 @@ public:
EXPANDED
};
struct IconMetrics {
int small;
int medium;
int big;
int spacing;
};
explicit ToolTipItem(QGraphicsItem *parent = 0);
virtual ~ToolTipItem();
@ -68,8 +61,6 @@ private:
int lastTime;
QList<QGraphicsItem*> oldSelection;
static IconMetrics iconMetrics;
};
#endif // DIVETOOLTIPITEM_H

View file

@ -12,7 +12,6 @@
QImage StarWidget::activeStar;
QImage StarWidget::inactiveStar;
StarMetrics StarWidget::imgMetrics = { -1, -1 };
const QImage& StarWidget::starActive()
{
@ -24,11 +23,6 @@ const QImage& StarWidget::starInactive()
return inactiveStar;
}
const StarMetrics& StarWidget::metrics()
{
return imgMetrics;
}
int StarWidget::currentStars() const
{
return current;
@ -40,7 +34,7 @@ void StarWidget::mouseReleaseEvent(QMouseEvent *event)
return;
}
int starClicked = event->pos().x() / imgMetrics.size + 1;
int starClicked = event->pos().x() / defaultIconMetrics().sz_small + 1;
if (starClicked > TOTALSTARS)
starClicked = TOTALSTARS;
@ -59,11 +53,13 @@ void StarWidget::paintEvent(QPaintEvent *event)
QPixmap active = QPixmap::fromImage(starActive());
QPixmap inactive = QPixmap::fromImage(starInactive());
const IconMetrics& metrics = defaultIconMetrics();
for (int i = 0; i < current; i++)
p.drawPixmap(i * imgMetrics.size + imgMetrics.spacing, 0, active);
p.drawPixmap(i * metrics.sz_small + metrics.spacing, 0, active);
for (int i = current; i < TOTALSTARS; i++)
p.drawPixmap(i * imgMetrics.size + imgMetrics.spacing, 0, inactive);
p.drawPixmap(i * metrics.sz_small + metrics.spacing, 0, inactive);
if (hasFocus()) {
QStyleOptionFocusRect option;
@ -84,20 +80,16 @@ StarWidget::StarWidget(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f),
current(0),
readOnly(false)
{
// set image size and spacing from the default icon size
if (imgMetrics.size == -1) {
imgMetrics.size = defaultIconSize();
imgMetrics.spacing = imgMetrics.size/8;
}
int dim = defaultIconMetrics().sz_small;
if (activeStar.isNull()) {
QSvgRenderer render(QString(":star"));
QPixmap renderedStar(imgMetrics.size, imgMetrics.size);
QPixmap renderedStar(dim, dim);
renderedStar.fill(Qt::transparent);
QPainter painter(&renderedStar);
render.render(&painter, QRectF(0, 0, imgMetrics.size, imgMetrics.size));
render.render(&painter, QRectF(0, 0, dim, dim));
activeStar = renderedStar.toImage();
}
if (inactiveStar.isNull()) {
@ -126,7 +118,8 @@ QImage grayImage(const QImage& coloredImg)
QSize StarWidget::sizeHint() const
{
return QSize(imgMetrics.size * TOTALSTARS + imgMetrics.spacing * (TOTALSTARS - 1), imgMetrics.size);
const IconMetrics& metrics = defaultIconMetrics();
return QSize(metrics.sz_small * TOTALSTARS + metrics.spacing * (TOTALSTARS - 1), metrics.sz_small);
}
void StarWidget::setReadOnly(bool r)

View file

@ -7,11 +7,6 @@ enum StarConfig {
TOTALSTARS = 5
};
struct StarMetrics {
int size;
int spacing;
};
class StarWidget : public QWidget {
Q_OBJECT
public:
@ -22,7 +17,6 @@ public:
static const QImage& starActive();
static const QImage& starInactive();
static const StarMetrics& metrics();
signals:
void valueChanged(int stars);
@ -45,7 +39,6 @@ private:
static QImage activeStar;
static QImage inactiveStar;
static StarMetrics imgMetrics;
};
#endif // STARWIDGET_H

View file

@ -16,10 +16,8 @@ TableView::TableView(QWidget *parent) : QWidget(parent)
QFontMetrics fm(defaultModelFont());
int text_ht = fm.height();
int text_em = fm.width('m');
// set icon and button size from the default icon size
metrics.icon_size = defaultIconSize(text_ht);
metrics.btn_size = metrics.icon_size + metrics.icon_size/2;
metrics.btn_gap = metrics.icon_size/8;
metrics.icon = &defaultIconMetrics();
metrics.col_width = 7*text_em;
metrics.rm_col_width = 3*text_em;
@ -35,7 +33,7 @@ TableView::TableView(QWidget *parent) : QWidget(parent)
plusBtn = new QPushButton(plusIcon, QString(), ui.groupBox);
plusBtn->setFlat(true);
plusBtn->setToolTip(tr("Add cylinder"));
plusBtn->setIconSize(QSize(metrics.icon_size, metrics.icon_size));
plusBtn->setIconSize(QSize(metrics.icon->sz_small, metrics.icon->sz_small));
connect(plusBtn, SIGNAL(clicked(bool)), this, SIGNAL(addButtonClicked()));
}
@ -91,9 +89,10 @@ void TableView::setModel(QAbstractItemModel *model)
void TableView::fixPlusPosition()
{
int x = ui.groupBox->contentsRect().width() - 2*metrics.icon_size + metrics.btn_gap;
int y = metrics.btn_gap;
plusBtn->setGeometry(x, y, metrics.btn_size, metrics.btn_size);
int x = ui.groupBox->contentsRect().width() - 2*metrics.icon->sz_small + metrics.icon->spacing;
int y = metrics.icon->spacing;
int sz = metrics.icon->sz_med;
plusBtn->setGeometry(x, y, sz, sz);
}
// We need to manually position the 'plus' on cylinder and weight.

View file

@ -9,6 +9,8 @@
#include "ui_tableview.h"
#include "metrics.h"
class QPushButton;
class QAbstractItemModel;
class QModelIndex;
@ -18,9 +20,7 @@ class TableView : public QWidget {
Q_OBJECT
struct TableMetrics {
int icon_size; // icon size
int btn_size; // button size
int btn_gap; // button gap
const IconMetrics* icon; // icon metrics
int col_width; // generic column width
int rm_col_width; // column width of REMOVE column
int header_ht; // height of the header