From 32ab2b34d39752693697f0f2a33032ceb3f5f3b4 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Wed, 15 Oct 2014 15:30:52 +0200 Subject: [PATCH] Unify icon metrics Signed-off-by: Giuseppe Bilotta Signed-off-by: Dirk Hohndel --- qt-ui/metrics.cpp | 20 +++++++++++++++++++- qt-ui/metrics.h | 17 ++++++++++++++--- qt-ui/modeldelegates.cpp | 10 +++++----- qt-ui/profile/divetooltipitem.cpp | 31 ++++++++++++++----------------- qt-ui/profile/divetooltipitem.h | 9 --------- qt-ui/starwidget.cpp | 27 ++++++++++----------------- qt-ui/starwidget.h | 7 ------- qt-ui/tableview.cpp | 15 +++++++-------- qt-ui/tableview.h | 6 +++--- 9 files changed, 72 insertions(+), 70 deletions(-) diff --git a/qt-ui/metrics.cpp b/qt-ui/metrics.cpp index 49e27849f..203c2e5e2 100644 --- a/qt-ui/metrics.cpp +++ b/qt-ui/metrics.cpp @@ -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; +} diff --git a/qt-ui/metrics.h b/qt-ui/metrics.h index ba45ab916..30295a3d8 100644 --- a/qt-ui/metrics.h +++ b/qt-ui/metrics.h @@ -10,12 +10,23 @@ #include #include +#include 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 diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp index 507a5f197..8b70b48b3 100644 --- a/qt-ui/modeldelegates.cpp +++ b/qt-ui/modeldelegates.cpp @@ -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(); } diff --git a/qt-ui/profile/divetooltipitem.cpp b/qt-ui/profile/divetooltipitem.cpp index fe69b56dd..ad69560a6 100644 --- a/qt-ui/profile/divetooltipitem.cpp +++ b/qt-ui/profile/divetooltipitem.cpp @@ -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); } diff --git a/qt-ui/profile/divetooltipitem.h b/qt-ui/profile/divetooltipitem.h index bdb7627e7..51e8eccd1 100644 --- a/qt-ui/profile/divetooltipitem.h +++ b/qt-ui/profile/divetooltipitem.h @@ -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 oldSelection; - - static IconMetrics iconMetrics; }; #endif // DIVETOOLTIPITEM_H diff --git a/qt-ui/starwidget.cpp b/qt-ui/starwidget.cpp index fe4342b7b..a56ba0b28 100644 --- a/qt-ui/starwidget.cpp +++ b/qt-ui/starwidget.cpp @@ -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) diff --git a/qt-ui/starwidget.h b/qt-ui/starwidget.h index 90310f041..989aa527d 100644 --- a/qt-ui/starwidget.h +++ b/qt-ui/starwidget.h @@ -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 diff --git a/qt-ui/tableview.cpp b/qt-ui/tableview.cpp index 689a2c1cf..a05b1744c 100644 --- a/qt-ui/tableview.cpp +++ b/qt-ui/tableview.cpp @@ -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. diff --git a/qt-ui/tableview.h b/qt-ui/tableview.h index 00d770842..93134b206 100644 --- a/qt-ui/tableview.h +++ b/qt-ui/tableview.h @@ -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