aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tomaz Canabrava <tcanabrava@kde.org>2014-01-19 16:16:04 -0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2014-01-19 21:36:08 -0800
commit72b5bbce6e2f177af0b769dbafd0db17dd6a1899 (patch)
treedb2e6a233668fa27b0259218ac0c1ecd8a20b27d
parent6a58712270c15488ddb799fbee8bc9eef85054be (diff)
downloadsubsurface-72b5bbce6e2f177af0b769dbafd0db17dd6a1899.tar.gz
Remove the plotText function, standardize with the TextItem.
Removed the plotText function and replaced it with the DiveTextItem class - this way there's just one way to add text on screen, and it also makes it easy to fix positioning of stuff there. Which is what I'll try to fix on the next commit. Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r--qt-ui/profile/diveprofileitem.cpp29
-rw-r--r--qt-ui/profile/divetextitem.cpp78
-rw-r--r--qt-ui/profile/divetextitem.h15
3 files changed, 68 insertions, 54 deletions
diff --git a/qt-ui/profile/diveprofileitem.cpp b/qt-ui/profile/diveprofileitem.cpp
index 32679b565..b88ff6abb 100644
--- a/qt-ui/profile/diveprofileitem.cpp
+++ b/qt-ui/profile/diveprofileitem.cpp
@@ -220,32 +220,5 @@ void DiveGasPressureItem::paint(QPainter* painter, const QStyleOptionGraphicsIte
QGraphicsItemGroup *plotText(text_render_options_t* tro, const QPointF& pos, const QString& text, QGraphicsItem *parent)
{
- QFont fnt(qApp->font());
- QFontMetrics fm(fnt);
-
- /*
- if (printMode)
- fnt.setPixelSize(tro->size);
- */
-
- QGraphicsItemGroup *group = new QGraphicsItemGroup(parent);
- QPainterPath textPath;
- /* addText() uses bottom-left text baseline and the -3 offset is probably slightly off
- * for different font sizes. */
- textPath.addText(0, fm.height() - 3, fnt, text);
- QPainterPathStroker stroker;
- stroker.setWidth(3);
- QGraphicsPathItem *strokedItem = new QGraphicsPathItem(stroker.createStroke(textPath), group);
- strokedItem->setBrush(QBrush(getColor(TEXT_BACKGROUND)));
- strokedItem->setPen(Qt::NoPen);
-
- QGraphicsPathItem *textItem = new QGraphicsPathItem(textPath, group);
- textItem->setBrush(QBrush(getColor(tro->color)));
- textItem->setPen(Qt::NoPen);
-
- group->setPos(pos);
- //group->setPos(pos.x() + dx, pos.y() + dy);
-// if (!printMode)
- group->setFlag(QGraphicsItem::ItemIgnoresTransformations);
- return group;
+
}
diff --git a/qt-ui/profile/divetextitem.cpp b/qt-ui/profile/divetextitem.cpp
index 37f4bfc6c..fcc0517f1 100644
--- a/qt-ui/profile/divetextitem.cpp
+++ b/qt-ui/profile/divetextitem.cpp
@@ -1,8 +1,17 @@
#include "divetextitem.h"
#include "animationfunctions.h"
+
#include <QPropertyAnimation>
+#include <QApplication>
+#include <QFont>
+#include <QFontMetrics>
+#include <QBrush>
+#include <QPen>
-DiveTextItem::DiveTextItem(QGraphicsItem* parent): QGraphicsSimpleTextItem(parent)
+DiveTextItem::DiveTextItem(QGraphicsItem* parent): QGraphicsItemGroup(parent),
+ textBackgroundItem(NULL),
+ textItem(NULL),
+ internalAlignFlags(0)
{
setFlag(ItemIgnoresTransformations);
}
@@ -10,31 +19,54 @@ DiveTextItem::DiveTextItem(QGraphicsItem* parent): QGraphicsSimpleTextItem(paren
void DiveTextItem::setAlignment(int alignFlags)
{
internalAlignFlags = alignFlags;
- update();
+ updateText();
+}
+
+void DiveTextItem::setBrush(const QBrush& b)
+{
+ brush = b;
+ updateText();
+}
+
+void DiveTextItem::setText(const QString& t)
+{
+ text = t;
+ updateText();
}
-void DiveTextItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
+void DiveTextItem::updateText()
{
- /* This block of code corrects paints the Text on the
- * alignment choosed, but it created artifacts on the screen,
- * so I'm leaving this disabled for the time being.
- */
-// QRectF rect = boundingRect();
-// if (internalAlignFlags & Qt::AlignTop)
-// painter->translate(0, -rect.height());
-// // else if (internalAlignFlags & Qt::AlignBottom)
-// // painter->translate(0, rect.height()); this is the default, uneeded.
-// else if (internalAlignFlags & Qt::AlignVCenter)
-// painter->translate(0, -rect.height() / 2);
-//
-// // if (internalAlignFlags & Qt::AlignLeft )
-// // painter->translate(); // This is the default, uneeded.
-// if (internalAlignFlags & Qt::AlignHCenter)
-// painter->translate(-rect.width()/2, 0);
-// else if (internalAlignFlags & Qt::AlignRight)
-// painter->translate(-rect.width(), 0);
-
- QGraphicsSimpleTextItem::paint(painter, option, widget);
+ if(!internalAlignFlags || text.isEmpty())
+ return;
+
+ delete textItem;
+ delete textBackgroundItem;
+
+ QFont fnt(qApp->font());
+ QFontMetrics fm(fnt);
+
+ QPainterPath textPath;
+ qreal xPos = 0, yPos = 0;
+
+ QRectF rect = fm.boundingRect(text);
+ yPos = (internalAlignFlags & Qt::AlignTop) ? -rect.height() :
+ (internalAlignFlags & Qt::AlignBottom) ? 0 :
+ /*(internalAlignFlags & Qt::AlignVCenter ? */ -rect.height() / 2;
+
+ yPos = (internalAlignFlags & Qt::AlignLeft ) ? 0 :
+ (internalAlignFlags & Qt::AlignHCenter) ? -rect.width()/2 :
+ /* (internalAlignFlags & Qt::AlignRight) */ -rect.width();
+
+ textPath.addText( xPos, yPos, fnt, text);
+ 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);
}
void DiveTextItem::animatedHide()
diff --git a/qt-ui/profile/divetextitem.h b/qt-ui/profile/divetextitem.h
index b2e3fb61e..6771d91d3 100644
--- a/qt-ui/profile/divetextitem.h
+++ b/qt-ui/profile/divetextitem.h
@@ -2,21 +2,30 @@
#define DIVETEXTITEM_H
#include <QObject>
-#include <QGraphicsSimpleTextItem>
+#include <QGraphicsItemGroup>
+#include "graphicsview-common.h"
+#include <QBrush>
/* A Line Item that has animated-properties. */
-class DiveTextItem :public QObject, public QGraphicsSimpleTextItem{
+class DiveTextItem :public QObject, public QGraphicsItemGroup{
Q_OBJECT
Q_PROPERTY(QPointF pos READ pos WRITE setPos)
Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity)
public:
DiveTextItem(QGraphicsItem* parent = 0);
- virtual void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget);
+ void setText(const QString& text);
void setAlignment(int alignFlags);
+ void setBrush(const QBrush& brush);
void animatedHide();
void animateMoveTo(qreal x, qreal y);
private:
+ void updateText();
int internalAlignFlags;
+ QGraphicsPathItem *textBackgroundItem;
+ QGraphicsPathItem *textItem;
+ QString text;
+ color_indice_t colorIndex;
+ QBrush brush;
};
#endif