mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Dynamic ToolTipItem metrics
Instead of hard-coding the icon sizes and spacing, compute them from the font sizes, that Qt auto-computes from the displya DPI. Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
0171368b6d
commit
d8f756fe14
2 changed files with 40 additions and 21 deletions
|
@ -18,25 +18,27 @@
|
||||||
#include "display.h"
|
#include "display.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
ToolTipItem::IconMetrics ToolTipItem::iconMetrics = { -1, -1, -1, -1 };
|
||||||
|
|
||||||
void ToolTipItem::addToolTip(const QString &toolTip, const QIcon &icon, const QPixmap *pixmap)
|
void ToolTipItem::addToolTip(const QString &toolTip, const QIcon &icon, const QPixmap *pixmap)
|
||||||
{
|
{
|
||||||
QGraphicsPixmapItem *iconItem = 0, *pixmapItem = 0;
|
QGraphicsPixmapItem *iconItem = 0, *pixmapItem = 0;
|
||||||
double yValue = title->boundingRect().height() + SPACING;
|
double yValue = title->boundingRect().height() + iconMetrics.spacing;
|
||||||
Q_FOREACH (ToolTip t, toolTips) {
|
Q_FOREACH (ToolTip t, toolTips) {
|
||||||
yValue += t.second->boundingRect().height();
|
yValue += t.second->boundingRect().height();
|
||||||
}
|
}
|
||||||
if (!icon.isNull()) {
|
if (!icon.isNull()) {
|
||||||
iconItem = new QGraphicsPixmapItem(icon.pixmap(ICON_SMALL, ICON_SMALL), this);
|
iconItem = new QGraphicsPixmapItem(icon.pixmap(iconMetrics.small, iconMetrics.small), this);
|
||||||
iconItem->setPos(SPACING, yValue);
|
iconItem->setPos(iconMetrics.spacing, yValue);
|
||||||
} else {
|
} else {
|
||||||
if (pixmap && !pixmap->isNull()) {
|
if (pixmap && !pixmap->isNull()) {
|
||||||
pixmapItem = new QGraphicsPixmapItem(*pixmap, this);
|
pixmapItem = new QGraphicsPixmapItem(*pixmap, this);
|
||||||
pixmapItem->setPos(SPACING, yValue);
|
pixmapItem->setPos(iconMetrics.spacing, yValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(toolTip, this);
|
QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(toolTip, this);
|
||||||
textItem->setPos(SPACING + ICON_SMALL + SPACING, yValue);
|
textItem->setPos(iconMetrics.spacing + iconMetrics.small + iconMetrics.spacing, yValue);
|
||||||
textItem->setBrush(QBrush(Qt::white));
|
textItem->setBrush(QBrush(Qt::white));
|
||||||
textItem->setFlag(ItemIgnoresTransformations);
|
textItem->setFlag(ItemIgnoresTransformations);
|
||||||
toolTips.push_back(qMakePair(iconItem, textItem));
|
toolTips.push_back(qMakePair(iconItem, textItem));
|
||||||
|
@ -89,7 +91,7 @@ void ToolTipItem::collapse()
|
||||||
QPropertyAnimation *animation = new QPropertyAnimation(this, "rect");
|
QPropertyAnimation *animation = new QPropertyAnimation(this, "rect");
|
||||||
animation->setDuration(100);
|
animation->setDuration(100);
|
||||||
animation->setStartValue(nextRectangle);
|
animation->setStartValue(nextRectangle);
|
||||||
animation->setEndValue(QRect(0, 0, ICON_SMALL, ICON_SMALL));
|
animation->setEndValue(QRect(0, 0, iconMetrics.small, iconMetrics.small));
|
||||||
animation->start(QAbstractAnimation::DeleteWhenStopped);
|
animation->start(QAbstractAnimation::DeleteWhenStopped);
|
||||||
clear();
|
clear();
|
||||||
|
|
||||||
|
@ -101,20 +103,20 @@ void ToolTipItem::expand()
|
||||||
if (!title)
|
if (!title)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
double width = 0, height = title->boundingRect().height() + SPACING;
|
double width = 0, height = title->boundingRect().height() + iconMetrics.spacing;
|
||||||
Q_FOREACH (ToolTip t, toolTips) {
|
Q_FOREACH (ToolTip t, toolTips) {
|
||||||
if (t.second->boundingRect().width() > width)
|
if (t.second->boundingRect().width() > width)
|
||||||
width = t.second->boundingRect().width();
|
width = t.second->boundingRect().width();
|
||||||
height += t.second->boundingRect().height();
|
height += t.second->boundingRect().height();
|
||||||
}
|
}
|
||||||
/* Left padding, Icon Size, space, right padding */
|
/* Left padding, Icon Size, space, right padding */
|
||||||
width += SPACING + ICON_SMALL + SPACING + SPACING;
|
width += iconMetrics.spacing + iconMetrics.small + iconMetrics.spacing + iconMetrics.spacing;
|
||||||
|
|
||||||
if (width < title->boundingRect().width() + SPACING * 2)
|
if (width < title->boundingRect().width() + iconMetrics.spacing * 2)
|
||||||
width = title->boundingRect().width() + SPACING * 2;
|
width = title->boundingRect().width() + iconMetrics.spacing * 2;
|
||||||
|
|
||||||
if (height < ICON_SMALL)
|
if (height < iconMetrics.small)
|
||||||
height = ICON_SMALL;
|
height = iconMetrics.small;
|
||||||
|
|
||||||
nextRectangle.setWidth(width);
|
nextRectangle.setWidth(width);
|
||||||
nextRectangle.setHeight(height);
|
nextRectangle.setHeight(height);
|
||||||
|
@ -136,6 +138,20 @@ ToolTipItem::ToolTipItem(QGraphicsItem *parent) : QGraphicsPathItem(parent),
|
||||||
timeAxis(0),
|
timeAxis(0),
|
||||||
lastTime(-1)
|
lastTime(-1)
|
||||||
{
|
{
|
||||||
|
// compute icon size, by rounding the font height to the nearest multiple of
|
||||||
|
// 16 (small), and setting medium, big and spacing proportionally
|
||||||
|
if (iconMetrics.small == -1) {
|
||||||
|
int height = QFontMetrics(title->font()).height();
|
||||||
|
iconMetrics.small = (height + 8)/16;
|
||||||
|
iconMetrics.small *= 16;
|
||||||
|
// enforce a minimum small
|
||||||
|
if (iconMetrics.small < 16)
|
||||||
|
iconMetrics.small = 16;
|
||||||
|
iconMetrics.medium = iconMetrics.small + iconMetrics.small/2;
|
||||||
|
iconMetrics.big = iconMetrics.small*2;
|
||||||
|
iconMetrics.spacing = iconMetrics.small/4;
|
||||||
|
}
|
||||||
|
|
||||||
memset(&pInfo, 0, sizeof(pInfo));
|
memset(&pInfo, 0, sizeof(pInfo));
|
||||||
|
|
||||||
setFlags(ItemIgnoresTransformations | ItemIsMovable | ItemClipsChildrenToShape);
|
setFlags(ItemIgnoresTransformations | ItemIsMovable | ItemClipsChildrenToShape);
|
||||||
|
@ -150,10 +166,10 @@ ToolTipItem::~ToolTipItem()
|
||||||
|
|
||||||
void ToolTipItem::updateTitlePosition()
|
void ToolTipItem::updateTitlePosition()
|
||||||
{
|
{
|
||||||
if (rectangle.width() < title->boundingRect().width() + SPACING * 4) {
|
if (rectangle.width() < title->boundingRect().width() + iconMetrics.spacing * 4) {
|
||||||
QRectF newRect = rectangle;
|
QRectF newRect = rectangle;
|
||||||
newRect.setWidth(title->boundingRect().width() + SPACING * 4);
|
newRect.setWidth(title->boundingRect().width() + iconMetrics.spacing * 4);
|
||||||
newRect.setHeight((newRect.height() && isExpanded()) ? newRect.height() : ICON_SMALL);
|
newRect.setHeight((newRect.height() && isExpanded()) ? newRect.height() : iconMetrics.small);
|
||||||
setRect(newRect);
|
setRect(newRect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,7 +180,7 @@ void ToolTipItem::updateTitlePosition()
|
||||||
|
|
||||||
if (toolTips.size() > 0) {
|
if (toolTips.size() > 0) {
|
||||||
double x1 = 3;
|
double x1 = 3;
|
||||||
double y1 = title->pos().y() + SPACING / 2 + title->boundingRect().height();
|
double y1 = title->pos().y() + iconMetrics.spacing / 2 + title->boundingRect().height();
|
||||||
double x2 = boundingRect().width() - 10;
|
double x2 = boundingRect().width() - 10;
|
||||||
double y2 = y1;
|
double y2 = y1;
|
||||||
|
|
||||||
|
|
|
@ -27,11 +27,12 @@ public:
|
||||||
COLLAPSED,
|
COLLAPSED,
|
||||||
EXPANDED
|
EXPANDED
|
||||||
};
|
};
|
||||||
enum {
|
|
||||||
ICON_SMALL = 16,
|
struct IconMetrics {
|
||||||
ICON_MEDIUM = 24,
|
int small;
|
||||||
ICON_BIG = 32,
|
int medium;
|
||||||
SPACING = 4
|
int big;
|
||||||
|
int spacing;
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit ToolTipItem(QGraphicsItem *parent = 0);
|
explicit ToolTipItem(QGraphicsItem *parent = 0);
|
||||||
|
@ -67,6 +68,8 @@ private:
|
||||||
int lastTime;
|
int lastTime;
|
||||||
|
|
||||||
QList<QGraphicsItem*> oldSelection;
|
QList<QGraphicsItem*> oldSelection;
|
||||||
|
|
||||||
|
static IconMetrics iconMetrics;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DIVETOOLTIPITEM_H
|
#endif // DIVETOOLTIPITEM_H
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue