mirror of
https://github.com/subsurface/subsurface.git
synced 2025-01-31 21:33:24 +00:00
Reuse the entry tooltip item and do fewer calls for each mouse move
While analizing the code for the mouse movement I've discovered that we did a lot of uneeded things: Set the color, the pen, the size of a fixed-colored line, twice. We also deleted-newed the same Pixmap / Text for every mouse movement so now we reuse the 'entryToolTip' that consists of a huge line and a pixmap, and after that we add the other tooltips that are not static Also, reduced a lot the number of calls to expand() (that did a lot of math). Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
3169ec8dc9
commit
6a1a6c82bf
2 changed files with 41 additions and 30 deletions
|
@ -23,27 +23,27 @@ void ToolTipItem::addToolTip(const QString &toolTip, const QIcon &icon, const QP
|
||||||
{
|
{
|
||||||
const IconMetrics& iconMetrics = defaultIconMetrics();
|
const IconMetrics& iconMetrics = defaultIconMetrics();
|
||||||
|
|
||||||
QGraphicsPixmapItem *iconItem = 0, *pixmapItem = 0;
|
QGraphicsPixmapItem *iconItem = 0;
|
||||||
double yValue = title->boundingRect().height() + iconMetrics.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 (entryToolTip.second) {
|
||||||
iconItem = new QGraphicsPixmapItem(icon.pixmap(iconMetrics.sz_small, iconMetrics.sz_small), this);
|
yValue += entryToolTip.second->boundingRect().height();
|
||||||
iconItem->setPos(iconMetrics.spacing, yValue);
|
|
||||||
} else {
|
|
||||||
if (!pixmap.isNull()) {
|
|
||||||
pixmapItem = new QGraphicsPixmapItem(pixmap, this);
|
|
||||||
pixmapItem->setPos(iconMetrics.spacing, yValue);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
iconItem = new QGraphicsPixmapItem(this);
|
||||||
|
if (!icon.isNull()) {
|
||||||
|
iconItem->setPixmap(icon.pixmap(iconMetrics.sz_small, iconMetrics.sz_small));
|
||||||
|
} else if (!pixmap.isNull()) {
|
||||||
|
iconItem->setPixmap(pixmap);
|
||||||
|
}
|
||||||
|
iconItem->setPos(iconMetrics.spacing, yValue);
|
||||||
|
|
||||||
QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(toolTip, this);
|
QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(toolTip, this);
|
||||||
textItem->setPos(iconMetrics.spacing + iconMetrics.sz_small + iconMetrics.spacing, yValue);
|
textItem->setPos(iconMetrics.spacing + iconMetrics.sz_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));
|
||||||
expand();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ToolTipItem::clear()
|
void ToolTipItem::clear()
|
||||||
|
@ -111,11 +111,18 @@ void ToolTipItem::expand()
|
||||||
const IconMetrics& iconMetrics = defaultIconMetrics();
|
const IconMetrics& iconMetrics = defaultIconMetrics();
|
||||||
|
|
||||||
double width = 0, height = title->boundingRect().height() + iconMetrics.spacing;
|
double width = 0, height = title->boundingRect().height() + iconMetrics.spacing;
|
||||||
Q_FOREACH (ToolTip t, toolTips) {
|
Q_FOREACH (const 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (entryToolTip.first) {
|
||||||
|
if (entryToolTip.second->boundingRect().width() > width)
|
||||||
|
width = entryToolTip.second->boundingRect().width();
|
||||||
|
height += entryToolTip.second->boundingRect().height();
|
||||||
|
}
|
||||||
|
|
||||||
/* Left padding, Icon Size, space, right padding */
|
/* Left padding, Icon Size, space, right padding */
|
||||||
width += iconMetrics.spacing + iconMetrics.sz_small + iconMetrics.spacing + iconMetrics.spacing;
|
width += iconMetrics.spacing + iconMetrics.sz_small + iconMetrics.spacing + iconMetrics.spacing;
|
||||||
|
|
||||||
|
@ -148,10 +155,22 @@ ToolTipItem::ToolTipItem(QGraphicsItem *parent) : QGraphicsPathItem(parent),
|
||||||
lastTime(-1)
|
lastTime(-1)
|
||||||
{
|
{
|
||||||
memset(&pInfo, 0, sizeof(pInfo));
|
memset(&pInfo, 0, sizeof(pInfo));
|
||||||
|
entryToolTip.first = NULL;
|
||||||
|
entryToolTip.second = NULL;
|
||||||
setFlags(ItemIgnoresTransformations | ItemIsMovable | ItemClipsChildrenToShape);
|
setFlags(ItemIgnoresTransformations | ItemIsMovable | ItemClipsChildrenToShape);
|
||||||
updateTitlePosition();
|
updateTitlePosition();
|
||||||
setZValue(99);
|
setZValue(99);
|
||||||
|
|
||||||
|
addToolTip(QString(), QIcon(), QPixmap(16,60));
|
||||||
|
entryToolTip = toolTips.first();
|
||||||
|
toolTips.clear();
|
||||||
|
|
||||||
|
separator->setFlag(ItemIgnoresTransformations);
|
||||||
|
separator->setPen(QPen(Qt::white));
|
||||||
|
|
||||||
|
title->setFlag(ItemIgnoresTransformations);
|
||||||
|
title->setPen(QPen(Qt::white, 1));
|
||||||
|
title->setBrush(Qt::white);
|
||||||
}
|
}
|
||||||
|
|
||||||
ToolTipItem::~ToolTipItem()
|
ToolTipItem::~ToolTipItem()
|
||||||
|
@ -170,23 +189,13 @@ void ToolTipItem::updateTitlePosition()
|
||||||
}
|
}
|
||||||
|
|
||||||
title->setPos(boundingRect().width() / 2 - title->boundingRect().width() / 2 - 1, 0);
|
title->setPos(boundingRect().width() / 2 - title->boundingRect().width() / 2 - 1, 0);
|
||||||
title->setFlag(ItemIgnoresTransformations);
|
|
||||||
title->setPen(QPen(Qt::white, 1));
|
|
||||||
title->setBrush(Qt::white);
|
|
||||||
|
|
||||||
if (toolTips.size() > 0) {
|
double x1 = 3;
|
||||||
double x1 = 3;
|
double y1 = title->pos().y() + iconMetrics.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;
|
|
||||||
|
|
||||||
separator->setLine(x1, y1, x2, y2);
|
separator->setLine(x1, y1, x2, y2);
|
||||||
separator->setFlag(ItemIgnoresTransformations);
|
|
||||||
separator->setPen(QPen(Qt::white));
|
|
||||||
separator->show();
|
|
||||||
} else {
|
|
||||||
separator->hide();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ToolTipItem::isExpanded() const
|
bool ToolTipItem::isExpanded() const
|
||||||
|
@ -234,7 +243,6 @@ void ToolTipItem::setTimeAxis(DiveCartesianAxis *axis)
|
||||||
|
|
||||||
void ToolTipItem::refresh(const QPointF &pos)
|
void ToolTipItem::refresh(const QPointF &pos)
|
||||||
{
|
{
|
||||||
int i;
|
|
||||||
struct plot_data *entry;
|
struct plot_data *entry;
|
||||||
static QPixmap tissues(16,60);
|
static QPixmap tissues(16,60);
|
||||||
static QPainter painter(&tissues);
|
static QPainter painter(&tissues);
|
||||||
|
@ -263,10 +271,11 @@ void ToolTipItem::refresh(const QPointF &pos)
|
||||||
painter.drawLine(0, 60 - AMB_PERCENTAGE * (entry->pressures.n2 + entry->pressures.he) / entry->ambpressure / 2,
|
painter.drawLine(0, 60 - AMB_PERCENTAGE * (entry->pressures.n2 + entry->pressures.he) / entry->ambpressure / 2,
|
||||||
16, 60 - AMB_PERCENTAGE * (entry->pressures.n2 + entry->pressures.he) / entry->ambpressure /2);
|
16, 60 - AMB_PERCENTAGE * (entry->pressures.n2 + entry->pressures.he) / entry->ambpressure /2);
|
||||||
painter.setPen(QColor(0, 0, 0, 127));
|
painter.setPen(QColor(0, 0, 0, 127));
|
||||||
for (i=0; i<16; i++) {
|
for (int i=0; i<16; i++) {
|
||||||
painter.drawLine(i, 60, i, 60 - entry->percentages[i] / 2);
|
painter.drawLine(i, 60, i, 60 - entry->percentages[i] / 2);
|
||||||
}
|
}
|
||||||
addToolTip(QString::fromUtf8(mb.buffer, mb.len),QIcon(), tissues);
|
entryToolTip.first->setPixmap(tissues);
|
||||||
|
entryToolTip.second->setText(QString::fromUtf8(mb.buffer, mb.len));
|
||||||
}
|
}
|
||||||
|
|
||||||
Q_FOREACH (QGraphicsItem *item, scene()->items(pos, Qt::IntersectsItemBoundingRect
|
Q_FOREACH (QGraphicsItem *item, scene()->items(pos, Qt::IntersectsItemBoundingRect
|
||||||
|
@ -274,6 +283,7 @@ void ToolTipItem::refresh(const QPointF &pos)
|
||||||
if (!item->toolTip().isEmpty())
|
if (!item->toolTip().isEmpty())
|
||||||
addToolTip(item->toolTip());
|
addToolTip(item->toolTip());
|
||||||
}
|
}
|
||||||
|
expand();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ToolTipItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
void ToolTipItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
||||||
|
|
|
@ -50,6 +50,7 @@ slots:
|
||||||
private:
|
private:
|
||||||
typedef QPair<QGraphicsPixmapItem *, QGraphicsSimpleTextItem *> ToolTip;
|
typedef QPair<QGraphicsPixmapItem *, QGraphicsSimpleTextItem *> ToolTip;
|
||||||
QVector<ToolTip> toolTips;
|
QVector<ToolTip> toolTips;
|
||||||
|
ToolTip entryToolTip;
|
||||||
QGraphicsPathItem *background;
|
QGraphicsPathItem *background;
|
||||||
QGraphicsLineItem *separator;
|
QGraphicsLineItem *separator;
|
||||||
QGraphicsSimpleTextItem *title;
|
QGraphicsSimpleTextItem *title;
|
||||||
|
|
Loading…
Add table
Reference in a new issue