My patches yesterday were a bit messy, this are the correct ones.
From d24e62bedada7c73468a99c661e89056dfd742f2 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava <[email protected]> Date: Fri, 16 Jan 2015 16:01:54 -0200 Subject: [PATCH 1/7] [SPEED ToolTip] Inherit from QGraphicsRectItem instead of QGraphicsShapeItem
a rectangle is *much* faster to paint than a simple ShapeItem, so this is a safer choice. We still need to create the paint method so we can use the correct roundness for the rectangle. Currently it's white with a 1px solid line - terrible. :) Signed-off-by: Tomaz Canabrava <[email protected]> --- qt-ui/profile/divetooltipitem.cpp | 25 +++++++++---------------- qt-ui/profile/divetooltipitem.h | 6 +++--- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/qt-ui/profile/divetooltipitem.cpp b/qt-ui/profile/divetooltipitem.cpp index 790c222..c351a30 100644 --- a/qt-ui/profile/divetooltipitem.cpp +++ b/qt-ui/profile/divetooltipitem.cpp @@ -57,20 +57,13 @@ void ToolTipItem::clear() void ToolTipItem::setRect(const QRectF &r) { - if( r == rectangle ) { + if( r == rect() ) { return; } - rectangle = r; - - // Creates a 2pixels border - QPainterPath border; - border.addRoundedRect(-4, -4, rectangle.width() + 8, rectangle.height() + 10, 3, 3); - border.addRoundedRect(-1, -1, rectangle.width() + 3, rectangle.height() + 4, 3, 3); - setPath(border); - + QGraphicsRectItem::setRect(r); QPainterPath bg; - bg.addRoundedRect(-1, -1, rectangle.width() + 3, rectangle.height() + 4, 3, 3); + bg.addRoundedRect(-1, -1, rect().width() + 3, rect().height() + 4, 3, 3); background->setPath(bg); updateTitlePosition(); @@ -122,10 +115,10 @@ void ToolTipItem::expand() nextRectangle.setWidth(width); nextRectangle.setHeight(height); - if (nextRectangle != rectangle) { + if (nextRectangle != rect()) { QPropertyAnimation *animation = new QPropertyAnimation(this, "rect", this); animation->setDuration(100); - animation->setStartValue(rectangle); + animation->setStartValue(rect()); animation->setEndValue(nextRectangle); animation->start(QAbstractAnimation::DeleteWhenStopped); } @@ -133,7 +126,7 @@ void ToolTipItem::expand() status = EXPANDED; } -ToolTipItem::ToolTipItem(QGraphicsItem *parent) : QGraphicsPathItem(parent), +ToolTipItem::ToolTipItem(QGraphicsItem *parent) : QGraphicsRectItem(parent), background(0), separator(new QGraphicsLineItem(this)), title(new QGraphicsSimpleTextItem(tr("Information"), this)), @@ -181,8 +174,8 @@ ToolTipItem::~ToolTipItem() void ToolTipItem::updateTitlePosition() { const IconMetrics& iconMetrics = defaultIconMetrics(); - if (rectangle.width() < title->boundingRect().width() + iconMetrics.spacing * 4) { - QRectF newRect = rectangle; + if (rect().width() < title->boundingRect().width() + iconMetrics.spacing * 4) { + QRectF newRect = rect(); newRect.setWidth(title->boundingRect().width() + iconMetrics.spacing * 4); newRect.setHeight((newRect.height() && isExpanded()) ? newRect.height() : iconMetrics.sz_small); setRect(newRect); @@ -206,7 +199,7 @@ bool ToolTipItem::isExpanded() const void ToolTipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { persistPos(); - QGraphicsPathItem::mouseReleaseEvent(event); + QGraphicsRectItem::mouseReleaseEvent(event); Q_FOREACH (QGraphicsItem *item, oldSelection) { item->setSelected(true); } diff --git a/qt-ui/profile/divetooltipitem.h b/qt-ui/profile/divetooltipitem.h index ca5bc89..7ac9d86 100644 --- a/qt-ui/profile/divetooltipitem.h +++ b/qt-ui/profile/divetooltipitem.h @@ -1,7 +1,7 @@ #ifndef DIVETOOLTIPITEM_H #define DIVETOOLTIPITEM_H -#include <QGraphicsPathItem> +#include <QGraphicsRectItem> #include <QVector> #include <QPair> #include <QRectF> @@ -17,10 +17,10 @@ struct graphics_context; /* To use a tooltip, simply ->setToolTip on the QGraphicsItem that you want * or, if it's a "global" tooltip, set it on the mouseMoveEvent of the ProfileGraphicsView. */ -class ToolTipItem : public QObject, public QGraphicsPathItem { +class ToolTipItem : public QObject, public QGraphicsRectItem { Q_OBJECT void updateTitlePosition(); - Q_PROPERTY(QRectF rect READ boundingRect WRITE setRect) + Q_PROPERTY(QRectF rect READ rect WRITE setRect) public: enum Status { -- 2.2.2
From 0f4afd1f5b88b650408336927d4fe528ab52bd51 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava <[email protected]> Date: Fri, 16 Jan 2015 16:06:14 -0200 Subject: [PATCH 2/7] Removed the Background and the Separator Those Items were used to fake the background of the Path Item but since the rectangle can be painted with a border and a fill, this is uneeded. The rect is still ugly. Signed-off-by: Tomaz Canabrava <[email protected]> --- qt-ui/profile/divetooltipitem.cpp | 25 ++----------------------- qt-ui/profile/divetooltipitem.h | 2 -- 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/qt-ui/profile/divetooltipitem.cpp b/qt-ui/profile/divetooltipitem.cpp index c351a30..a868567 100644 --- a/qt-ui/profile/divetooltipitem.cpp +++ b/qt-ui/profile/divetooltipitem.cpp @@ -62,10 +62,6 @@ void ToolTipItem::setRect(const QRectF &r) } QGraphicsRectItem::setRect(r); - QPainterPath bg; - bg.addRoundedRect(-1, -1, rect().width() + 3, rect().height() + 4, 3, 3); - - background->setPath(bg); updateTitlePosition(); } @@ -127,8 +123,6 @@ void ToolTipItem::expand() } ToolTipItem::ToolTipItem(QGraphicsItem *parent) : QGraphicsRectItem(parent), - background(0), - separator(new QGraphicsLineItem(this)), title(new QGraphicsSimpleTextItem(tr("Information"), this)), status(COLLAPSED), timeAxis(0), @@ -141,23 +135,15 @@ ToolTipItem::ToolTipItem(QGraphicsItem *parent) : QGraphicsRectItem(parent), QColor c = QColor(Qt::black); c.setAlpha(155); - background = new QGraphicsPathItem(this); - background->setFlag(ItemStacksBehindParent); - background->setFlag(ItemIgnoresTransformations); - background->setBrush(c); - background->setPen(QPen(QBrush(Qt::transparent), 0)); - background->setZValue(-10); + setBrush(c); + setPen(QPen(QBrush(Qt::transparent), 0)); - updateTitlePosition(); 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); @@ -182,13 +168,6 @@ void ToolTipItem::updateTitlePosition() } title->setPos(boundingRect().width() / 2 - title->boundingRect().width() / 2 - 1, 0); - - double x1 = 3; - double y1 = title->pos().y() + iconMetrics.spacing / 2 + title->boundingRect().height(); - double x2 = boundingRect().width() - 10; - double y2 = y1; - - separator->setLine(x1, y1, x2, y2); } bool ToolTipItem::isExpanded() const diff --git a/qt-ui/profile/divetooltipitem.h b/qt-ui/profile/divetooltipitem.h index 7ac9d86..a383fec 100644 --- a/qt-ui/profile/divetooltipitem.h +++ b/qt-ui/profile/divetooltipitem.h @@ -51,8 +51,6 @@ private: typedef QPair<QGraphicsPixmapItem *, QGraphicsSimpleTextItem *> ToolTip; QVector<ToolTip> toolTips; ToolTip entryToolTip; - QGraphicsPathItem *background; - QGraphicsLineItem *separator; QGraphicsSimpleTextItem *title; Status status; QRectF rectangle; -- 2.2.2
From 2e7105fff967923568295ed80997fbefe35d9478 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava <[email protected]> Date: Fri, 16 Jan 2015 16:17:26 -0200 Subject: [PATCH 3/7] Implement the Paint method to draw the rounded rectangle. The rectangle is now correct, but the collors are still wrong. I'm tracking that down - most probably I'v set the wrong pen or brush ( or both ) somewhere. Signed-off-by: Tomaz Canabrava <[email protected]> --- qt-ui/profile/divetooltipitem.cpp | 12 ++++++++++++ qt-ui/profile/divetooltipitem.h | 1 + 2 files changed, 13 insertions(+) diff --git a/qt-ui/profile/divetooltipitem.cpp b/qt-ui/profile/divetooltipitem.cpp index a868567..42aaf1d 100644 --- a/qt-ui/profile/divetooltipitem.cpp +++ b/qt-ui/profile/divetooltipitem.cpp @@ -12,6 +12,7 @@ #include <QGraphicsScene> #include <QSettings> #include <QGraphicsView> +#include <QStyleOptionGraphicsItem> #include <QDebug> #define PORT_IN_PROGRESS 1 @@ -184,6 +185,17 @@ void ToolTipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) } } +void ToolTipItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(widget); + painter->save(); + painter->setClipRect(option->rect); + painter->setPen(pen()); + painter->setBrush(brush()); + painter->drawRoundedRect(rect(), 10, 10, Qt::AbsoluteSize); + painter->restore(); +} + void ToolTipItem::persistPos() { QSettings s; diff --git a/qt-ui/profile/divetooltipitem.h b/qt-ui/profile/divetooltipitem.h index a383fec..6235ec8 100644 --- a/qt-ui/profile/divetooltipitem.h +++ b/qt-ui/profile/divetooltipitem.h @@ -43,6 +43,7 @@ public: void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void setTimeAxis(DiveCartesianAxis *axis); void setPlotInfo(const plot_info &plot); + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); public slots: void setRect(const QRectF &rect); -- 2.2.2
From b0f0432692e94c77c2c4453590301abb129d88d7 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava <[email protected]> Date: Fri, 16 Jan 2015 16:19:28 -0200 Subject: [PATCH 4/7] Fix the colors of the Rectangle. Correct Pen and Brush setted. the ToolTip now is correctly rounded, translucent and happy. Signed-off-by: Tomaz Canabrava <[email protected]> --- qt-ui/profile/divetooltipitem.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/qt-ui/profile/divetooltipitem.cpp b/qt-ui/profile/divetooltipitem.cpp index 42aaf1d..db2bd1f 100644 --- a/qt-ui/profile/divetooltipitem.cpp +++ b/qt-ui/profile/divetooltipitem.cpp @@ -137,7 +137,6 @@ ToolTipItem::ToolTipItem(QGraphicsItem *parent) : QGraphicsRectItem(parent), QColor c = QColor(Qt::black); c.setAlpha(155); setBrush(c); - setPen(QPen(QBrush(Qt::transparent), 0)); setZValue(99); @@ -149,8 +148,7 @@ ToolTipItem::ToolTipItem(QGraphicsItem *parent) : QGraphicsRectItem(parent), title->setPen(QPen(Qt::white, 1)); title->setBrush(Qt::white); - setBrush(QBrush(Qt::white)); - setPen(QPen(Qt::black, 0.5)); + setPen(QPen(Qt::white, 2)); } ToolTipItem::~ToolTipItem() -- 2.2.2
From 15f9808de0f50fc0a7044c8c1dc8db101d260e8e Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava <[email protected]> Date: Fri, 16 Jan 2015 16:29:07 -0200 Subject: [PATCH 5/7] Reduce the number of calls to boundingRect There's a bit of calculations that goes on boundingRect that can be avoided if we simply store the result. Signed-off-by: Tomaz Canabrava <[email protected]> --- qt-ui/profile/divetooltipitem.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/qt-ui/profile/divetooltipitem.cpp b/qt-ui/profile/divetooltipitem.cpp index db2bd1f..b07c729 100644 --- a/qt-ui/profile/divetooltipitem.cpp +++ b/qt-ui/profile/divetooltipitem.cpp @@ -89,15 +89,17 @@ void ToolTipItem::expand() double width = 0, height = title->boundingRect().height() + iconMetrics.spacing; Q_FOREACH (const ToolTip& t, toolTips) { - if (t.second->boundingRect().width() > width) - width = t.second->boundingRect().width(); - height += t.second->boundingRect().height(); + QRectF sRect = t.second->boundingRect(); + if (sRect.width() > width) + width = sRect.width(); + height += sRect.height(); } if (entryToolTip.first) { - if (entryToolTip.second->boundingRect().width() > width) - width = entryToolTip.second->boundingRect().width(); - height += entryToolTip.second->boundingRect().height(); + QRectF sRect = entryToolTip.second->boundingRect(); + if (sRect.width() > width) + width = sRect.width(); + height += sRect.height(); } /* Left padding, Icon Size, space, right padding */ @@ -166,7 +168,7 @@ void ToolTipItem::updateTitlePosition() setRect(newRect); } - title->setPos(boundingRect().width() / 2 - title->boundingRect().width() / 2 - 1, 0); + title->setPos(rect().width() / 2 - title->boundingRect().width() / 2 - 1, 0); } bool ToolTipItem::isExpanded() const -- 2.2.2
From 1ad020f8d701b3cbf58390f08f817e6423840d62 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava <[email protected]> Date: Fri, 16 Jan 2015 16:39:14 -0200 Subject: [PATCH 6/7] Honor prefs.anim_speed on the ToolTip animations The tooltip animation had a fixed animation speed, this patch nohors the anim_speed on the preferences, and also disable the animationcompletely if the speed == 0. Signed-off-by: Tomaz Canabrava <[email protected]> --- qt-ui/profile/divetooltipitem.cpp | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/qt-ui/profile/divetooltipitem.cpp b/qt-ui/profile/divetooltipitem.cpp index b07c729..821c4a6 100644 --- a/qt-ui/profile/divetooltipitem.cpp +++ b/qt-ui/profile/divetooltipitem.cpp @@ -70,11 +70,15 @@ 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, dim, dim)); - animation->start(QAbstractAnimation::DeleteWhenStopped); + if (prefs.animation_speed) { + QPropertyAnimation *animation = new QPropertyAnimation(this, "rect"); + animation->setDuration(100); + animation->setStartValue(nextRectangle); + animation->setEndValue(QRect(0, 0, dim, dim)); + animation->start(QAbstractAnimation::DeleteWhenStopped); + } else { + setRect(nextRectangle); + } clear(); status = COLLAPSED; @@ -115,11 +119,15 @@ void ToolTipItem::expand() nextRectangle.setHeight(height); if (nextRectangle != rect()) { - QPropertyAnimation *animation = new QPropertyAnimation(this, "rect", this); - animation->setDuration(100); - animation->setStartValue(rect()); - animation->setEndValue(nextRectangle); - animation->start(QAbstractAnimation::DeleteWhenStopped); + if (prefs.animation_speed) { + QPropertyAnimation *animation = new QPropertyAnimation(this, "rect", this); + animation->setDuration(prefs.animation_speed); + animation->setStartValue(rect()); + animation->setEndValue(nextRectangle); + animation->start(QAbstractAnimation::DeleteWhenStopped); + } else { + setRect(nextRectangle); + } } status = EXPANDED; -- 2.2.2
From 1b69c01e5fb86fa83ca96fa7f8c272e168a1605f Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava <[email protected]> Date: Fri, 16 Jan 2015 16:45:13 -0200 Subject: [PATCH 7/7] Reduce the refresh rate of the toolTipItem to 25fps. This reduces a lot of CPU time and makes the overall use of the tooltip a breeze. Signed-off-by: Tomaz Canabrava <[email protected]> --- qt-ui/profile/divetooltipitem.cpp | 5 +++++ qt-ui/profile/divetooltipitem.h | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/qt-ui/profile/divetooltipitem.cpp b/qt-ui/profile/divetooltipitem.cpp index 821c4a6..e89feff 100644 --- a/qt-ui/profile/divetooltipitem.cpp +++ b/qt-ui/profile/divetooltipitem.cpp @@ -159,6 +159,7 @@ ToolTipItem::ToolTipItem(QGraphicsItem *parent) : QGraphicsRectItem(parent), title->setBrush(Qt::white); setPen(QPen(Qt::white, 2)); + refreshTime.start(); } ToolTipItem::~ToolTipItem() @@ -240,6 +241,10 @@ void ToolTipItem::refresh(const QPointF &pos) static QPainter painter(&tissues); static struct membuffer mb = { 0 }; + if(refreshTime.elapsed() < 40) + return; + refreshTime.start(); + int time = timeAxis->valueAt(pos); if (time == lastTime) return; diff --git a/qt-ui/profile/divetooltipitem.h b/qt-ui/profile/divetooltipitem.h index 6235ec8..4fa7ec2 100644 --- a/qt-ui/profile/divetooltipitem.h +++ b/qt-ui/profile/divetooltipitem.h @@ -6,6 +6,7 @@ #include <QPair> #include <QRectF> #include <QIcon> +#include <QTime> #include "display.h" class DiveCartesianAxis; @@ -59,7 +60,7 @@ private: DiveCartesianAxis *timeAxis; plot_info pInfo; int lastTime; - + QTime refreshTime; QList<QGraphicsItem*> oldSelection; }; -- 2.2.2
_______________________________________________ subsurface mailing list [email protected] http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface
