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 <tomaz.canabrava@intel.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Tomaz Canabrava 2015-01-16 17:13:58 -02:00 committed by Dirk Hohndel
parent abddb3ecb9
commit a6e95511a6

View file

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