I'm retarded. I'v just send this to the wrong list.

On Fri, Jan 16, 2015 at 5:27 PM, Tomaz Canabrava <[email protected]> wrote:

> following up from the ToolTipItems, now speed improvs on DiveTextItems.
>
>
>
This should bring a huge amount of speed for mouse movements.
here it goes from 17% CPU to 9% CPU moving the mouse frenetically.
From d0c1941f576193c7157379d520034e1b128ee9d1 Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Fri, 16 Jan 2015 17:13:58 -0200
Subject: [PATCH 08/12] Reduce the amount of new/delete when setting a new text
 on DiveTextItem

We were recreating the PathItems ( one for the outline, other for
the real text ) for every call to setText. this was a very un-smart
move.

Signed-off-by: Tomaz Canabrava <[email protected]>
---
 qt-ui/profile/divetextitem.cpp | 29 ++++++++++++-----------------
 1 file changed, 12 insertions(+), 17 deletions(-)

diff --git a/qt-ui/profile/divetextitem.cpp b/qt-ui/profile/divetextitem.cpp
index fad35eb..ddaf6ef 100644
--- a/qt-ui/profile/divetextitem.cpp
+++ b/qt-ui/profile/divetextitem.cpp
@@ -12,12 +12,16 @@
 
 DiveTextItem::DiveTextItem(QGraphicsItem *parent) : QGraphicsItemGroup(parent),
 	internalAlignFlags(Qt::AlignHCenter | Qt::AlignVCenter),
-	textBackgroundItem(NULL),
-	textItem(NULL),
+	textBackgroundItem(new QGraphicsPathItem(this)),
+	textItem(new QGraphicsPathItem(this)),
 	colorIndex(SAC_DEFAULT),
 	scale(1.0)
 {
 	setFlag(ItemIgnoresTransformations);
+	textBackgroundItem->setBrush(QBrush(getColor(TEXT_BACKGROUND)));
+	textBackgroundItem->setPen(Qt::NoPen);
+	textItem->setBrush(brush);
+	textItem->setPen(Qt::NoPen);
 }
 
 void DiveTextItem::setAlignment(int alignFlags)
@@ -51,10 +55,6 @@ const QString &DiveTextItem::text()
 void DiveTextItem::updateText()
 {
 	double size;
-	delete textItem;
-	textItem = NULL;
-	delete textBackgroundItem;
-	textBackgroundItem = NULL;
 	if (internalText.isEmpty()) {
 		return;
 	}
@@ -76,21 +76,16 @@ void DiveTextItem::updateText()
 
 	QRectF rect = fm.boundingRect(internalText);
 	yPos = (internalAlignFlags & Qt::AlignTop) ? 0 :
-						     (internalAlignFlags & Qt::AlignBottom) ? +rect.height() :
-											      /*(internalAlignFlags & Qt::AlignVCenter  ? */ +rect.height() / 4;
+		(internalAlignFlags & Qt::AlignBottom) ? +rect.height() :
+		/*(internalAlignFlags & Qt::AlignVCenter  ? */ +rect.height() / 4;
 
 	xPos = (internalAlignFlags & Qt::AlignLeft) ? -rect.width() :
-						      (internalAlignFlags & Qt::AlignHCenter) ? -rect.width() / 2 :
-												/* (internalAlignFlags & Qt::AlignRight) */ 0;
+		(internalAlignFlags & Qt::AlignHCenter) ? -rect.width() / 2 :
+		/* (internalAlignFlags & Qt::AlignRight) */ 0;
 
 	textPath.addText(xPos, yPos, fnt, internalText);
 	QPainterPathStroker stroker;
 	stroker.setWidth(3);
-	textBackgroundItem = new QGraphicsPathItem(stroker.createStroke(textPath), this);
-	textBackgroundItem->setBrush(QBrush(getColor(TEXT_BACKGROUND)));
-	textBackgroundItem->setPen(Qt::NoPen);
-
-	textItem = new QGraphicsPathItem(textPath, this);
-	textItem->setBrush(brush);
-	textItem->setPen(Qt::NoPen);
+	textBackgroundItem->setPath(stroker.createStroke(textPath));
+	textItem->setPath(textPath);
 }
-- 
2.2.2

From efdb0341e5cb8382ddeb30a068f41d4991dcca0b Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Fri, 16 Jan 2015 17:17:55 -0200
Subject: [PATCH 09/12] Fix the colors - brush wasn't being set anymore.

Also a bit of code cleanup.

Signed-off-by: Tomaz Canabrava <[email protected]>
---
 qt-ui/profile/divetextitem.cpp | 5 +----
 qt-ui/profile/divetextitem.h   | 2 --
 2 files changed, 1 insertion(+), 6 deletions(-)

diff --git a/qt-ui/profile/divetextitem.cpp b/qt-ui/profile/divetextitem.cpp
index ddaf6ef..24f9d6b 100644
--- a/qt-ui/profile/divetextitem.cpp
+++ b/qt-ui/profile/divetextitem.cpp
@@ -14,13 +14,11 @@ DiveTextItem::DiveTextItem(QGraphicsItem *parent) : QGraphicsItemGroup(parent),
 	internalAlignFlags(Qt::AlignHCenter | Qt::AlignVCenter),
 	textBackgroundItem(new QGraphicsPathItem(this)),
 	textItem(new QGraphicsPathItem(this)),
-	colorIndex(SAC_DEFAULT),
 	scale(1.0)
 {
 	setFlag(ItemIgnoresTransformations);
 	textBackgroundItem->setBrush(QBrush(getColor(TEXT_BACKGROUND)));
 	textBackgroundItem->setPen(Qt::NoPen);
-	textItem->setBrush(brush);
 	textItem->setPen(Qt::NoPen);
 }
 
@@ -32,8 +30,7 @@ void DiveTextItem::setAlignment(int alignFlags)
 
 void DiveTextItem::setBrush(const QBrush &b)
 {
-	brush = b;
-	updateText();
+	textItem->setBrush(b);
 }
 
 void DiveTextItem::setScale(double newscale)
diff --git a/qt-ui/profile/divetextitem.h b/qt-ui/profile/divetextitem.h
index a6c0622..0c0ec4b 100644
--- a/qt-ui/profile/divetextitem.h
+++ b/qt-ui/profile/divetextitem.h
@@ -25,8 +25,6 @@ private:
 	QGraphicsPathItem *textBackgroundItem;
 	QGraphicsPathItem *textItem;
 	QString internalText;
-	color_indice_t colorIndex;
-	QBrush brush;
 	double scale;
 };
 
-- 
2.2.2

From cf286879e4dc2f5413d1e42c6a5b35223330d1ce Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Fri, 16 Jan 2015 17:22:30 -0200
Subject: [PATCH 10/12] Only update text if something changed.

We were callign this even if we didn't really changed anything
and Paths are expensive to paint.

Signed-off-by: Tomaz Canabrava <[email protected]>
---
 qt-ui/profile/divetextitem.cpp | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/qt-ui/profile/divetextitem.cpp b/qt-ui/profile/divetextitem.cpp
index 24f9d6b..761f0e6 100644
--- a/qt-ui/profile/divetextitem.cpp
+++ b/qt-ui/profile/divetextitem.cpp
@@ -24,8 +24,10 @@ DiveTextItem::DiveTextItem(QGraphicsItem *parent) : QGraphicsItemGroup(parent),
 
 void DiveTextItem::setAlignment(int alignFlags)
 {
-	internalAlignFlags = alignFlags;
-	updateText();
+	if (alignFlags != internalAlignFlags) {
+		internalAlignFlags = alignFlags;
+		updateText();
+	}
 }
 
 void DiveTextItem::setBrush(const QBrush &b)
@@ -35,13 +37,18 @@ void DiveTextItem::setBrush(const QBrush &b)
 
 void DiveTextItem::setScale(double newscale)
 {
-	scale = newscale;
+	if (scale != newscale) {
+		scale = newscale;
+		updateText();
+	}
 }
 
 void DiveTextItem::setText(const QString &t)
 {
-	internalText = t;
-	updateText();
+	if (internalText != t) {
+		internalText = t;
+		updateText();
+	}
 }
 
 const QString &DiveTextItem::text()
-- 
2.2.2

From 67e0a37bbb532b66ed5a26827bf23180e23d4b6b Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Fri, 16 Jan 2015 18:12:02 -0200
Subject: [PATCH 11/12] Fix memleak of QGraphicsRectItem

We used to create a new QGraphicsRectItem everytime a Pixmap
changed. Since I'm pretty sure I deleted every bit of the
PictureItem before setting a new one, no leak was due, but this
version is safer.

Signed-off-by: Tomaz Canabrava <[email protected]>
---
 qt-ui/profile/divepixmapitem.cpp | 33 ++++++++++++++++-----------------
 qt-ui/profile/divepixmapitem.h   |  2 ++
 2 files changed, 18 insertions(+), 17 deletions(-)

diff --git a/qt-ui/profile/divepixmapitem.cpp b/qt-ui/profile/divepixmapitem.cpp
index ce5d3c4..8aff531 100644
--- a/qt-ui/profile/divepixmapitem.cpp
+++ b/qt-ui/profile/divepixmapitem.cpp
@@ -45,17 +45,26 @@ void CloseButtonItem::show()
 	DiveButtonItem::show();
 }
 
-DivePictureItem::DivePictureItem(QObject *parent): DivePixmapItem(parent)
+DivePictureItem::DivePictureItem(QObject *parent): DivePixmapItem(parent),
+	canvas(new QGraphicsRectItem(this)),
+	shadow(new QGraphicsRectItem(this))
 {
 	setFlag(ItemIgnoresTransformations);
-#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
-	setAcceptsHoverEvents(true);
-#else
 	setAcceptHoverEvents(true);
-#endif
 	setScale(0.2);
 	connect(PreferencesDialog::instance(), SIGNAL(settingsChanged()), this, SLOT(settingsChanged()));
 	setVisible(prefs.show_pictures_in_profile);
+
+	canvas->setPen(Qt::NoPen);
+	canvas->setBrush(QColor(Qt::white));
+	canvas->setFlag(ItemStacksBehindParent);
+	canvas->setZValue(-1);
+
+	shadow->setPos(5,5);
+	shadow->setPen(Qt::NoPen);
+	shadow->setBrush(QColor(Qt::lightGray));
+	shadow->setFlag(ItemStacksBehindParent);
+	shadow->setZValue(-2);
 }
 
 void DivePictureItem::settingsChanged()
@@ -67,18 +76,8 @@ void DivePictureItem::setPixmap(const QPixmap &pix)
 {
 	DivePixmapItem::setPixmap(pix);
 	QRectF r = boundingRect();
-	QGraphicsRectItem *rect = new QGraphicsRectItem(0 - 10, 0 -10, r.width() + 20, r.height() + 20, this);
-	rect->setPen(Qt::NoPen);
-	rect->setBrush(QColor(Qt::white));
-	rect->setFlag(ItemStacksBehindParent);
-	rect->setZValue(-1);
-
-	QGraphicsRectItem *shadow = new QGraphicsRectItem(rect->boundingRect(), this);
-	shadow->setPos(5,5);
-	shadow->setPen(Qt::NoPen);
-	shadow->setBrush(QColor(Qt::lightGray));
-	shadow->setFlag(ItemStacksBehindParent);
-	shadow->setZValue(-2);
+	canvas->setRect(0 - 10, 0 -10, r.width() + 20, r.height() + 20);
+	shadow->setRect(canvas->rect());
 }
 
 CloseButtonItem *button = NULL;
diff --git a/qt-ui/profile/divepixmapitem.h b/qt-ui/profile/divepixmapitem.h
index 963e641..02c1523 100644
--- a/qt-ui/profile/divepixmapitem.h
+++ b/qt-ui/profile/divepixmapitem.h
@@ -31,6 +31,8 @@ protected:
 	void mousePressEvent(QGraphicsSceneMouseEvent *event);
 private:
 	QString fileUrl;
+	QGraphicsRectItem *canvas;
+	QGraphicsRectItem *shadow;
 };
 
 class DiveButtonItem : public DivePixmapItem {
-- 
2.2.2

From 4619a53c8c5682394d3f773423bfa2573e9fc020 Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Fri, 16 Jan 2015 18:50:28 -0200
Subject: [PATCH 12/12] Cache the complex items to give us a boost of speed

This cache give us a huge gain in performance, going from
17% moving the mouse frenetically to 9%, wich is quite acceptable.

Signed-off-by: Tomaz Canabrava <[email protected]>
---
 qt-ui/profile/diveprofileitem.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/qt-ui/profile/diveprofileitem.cpp b/qt-ui/profile/diveprofileitem.cpp
index 7531b2a..490f045 100644
--- a/qt-ui/profile/diveprofileitem.cpp
+++ b/qt-ui/profile/diveprofileitem.cpp
@@ -23,6 +23,7 @@
 
 AbstractProfilePolygonItem::AbstractProfilePolygonItem() : QObject(), QGraphicsPolygonItem(), hAxis(NULL), vAxis(NULL), dataModel(NULL), hDataColumn(-1), vDataColumn(-1)
 {
+	setCacheMode(DeviceCoordinateCache);
 	connect(PreferencesDialog::instance(), SIGNAL(settingsChanged()), this, SLOT(settingsChanged()));
 }
 
-- 
2.2.2

_______________________________________________
subsurface mailing list
[email protected]
http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to