summaryrefslogtreecommitdiffstats
path: root/desktop-widgets/profile/divetextitem.cpp
diff options
context:
space:
mode:
authorGravatar Tomaz Canabrava <tomaz.canabrava@intel.com>2015-09-03 14:20:19 -0300
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-10-30 10:36:49 -0700
commite49d6213ad129284a45d53c3fcdc03249e84efe2 (patch)
tree2946a666ab38af3375e7bb2b8c5dd887d4a7f9a1 /desktop-widgets/profile/divetextitem.cpp
parent588abd019fb2ed3f607682f2b6c7fe86a7a5bb90 (diff)
downloadsubsurface-e49d6213ad129284a45d53c3fcdc03249e84efe2.tar.gz
Move qt-ui to desktop-widgets
Since we have now destkop and mobile versions, 'qt-ui' was a very poor name choice for a folder that contains only destkop-enabled widgets. Also, move the graphicsview-common.h/cpp to subsurface-core because it doesn't depend on qgraphicsview, it merely implements all the colors that we use throughout Subsurface, and we will use colors on both desktop and mobile versions Same thing applies for metrics.h/cpp Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'desktop-widgets/profile/divetextitem.cpp')
-rw-r--r--desktop-widgets/profile/divetextitem.cpp113
1 files changed, 113 insertions, 0 deletions
diff --git a/desktop-widgets/profile/divetextitem.cpp b/desktop-widgets/profile/divetextitem.cpp
new file mode 100644
index 000000000..3bf00d68f
--- /dev/null
+++ b/desktop-widgets/profile/divetextitem.cpp
@@ -0,0 +1,113 @@
+#include "divetextitem.h"
+#include "mainwindow.h"
+#include "profilewidget2.h"
+#include "subsurface-core/color.h"
+
+#include <QBrush>
+
+DiveTextItem::DiveTextItem(QGraphicsItem *parent) : QGraphicsItemGroup(parent),
+ internalAlignFlags(Qt::AlignHCenter | Qt::AlignVCenter),
+ textBackgroundItem(new QGraphicsPathItem(this)),
+ textItem(new QGraphicsPathItem(this)),
+ printScale(1.0),
+ scale(1.0),
+ connected(false)
+{
+ setFlag(ItemIgnoresTransformations);
+ textBackgroundItem->setBrush(QBrush(getColor(TEXT_BACKGROUND)));
+ textBackgroundItem->setPen(Qt::NoPen);
+ textItem->setPen(Qt::NoPen);
+}
+
+void DiveTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
+{
+ updateText();
+ QGraphicsItemGroup::paint(painter, option, widget);
+}
+
+void DiveTextItem::fontPrintScaleUpdate(double scale)
+{
+ printScale = scale;
+}
+
+void DiveTextItem::setAlignment(int alignFlags)
+{
+ if (alignFlags != internalAlignFlags) {
+ internalAlignFlags = alignFlags;
+ }
+}
+
+void DiveTextItem::setBrush(const QBrush &b)
+{
+ textItem->setBrush(b);
+}
+
+void DiveTextItem::setScale(double newscale)
+{
+ if (scale != newscale) {
+ scale = newscale;
+ }
+}
+
+void DiveTextItem::setText(const QString &t)
+{
+ if (internalText != t) {
+ if (!connected) {
+ if (scene()) {
+ // by now we should be on a scene. grab the profile widget from it and setup our printScale
+ // and connect to the signal that makes sure we keep track if that changes
+ ProfileWidget2 *profile = qobject_cast<ProfileWidget2 *>(scene()->views().first());
+ connect(profile, SIGNAL(fontPrintScaleChanged(double)), this, SLOT(fontPrintScaleUpdate(double)), Qt::UniqueConnection);
+ fontPrintScaleUpdate(profile->getFontPrintScale());
+ connected = true;
+ } else {
+ qDebug() << "called before scene was set up" << t;
+ }
+ }
+ internalText = t;
+ updateText();
+ }
+}
+
+const QString &DiveTextItem::text()
+{
+ return internalText;
+}
+
+void DiveTextItem::updateText()
+{
+ double size;
+ if (internalText.isEmpty()) {
+ return;
+ }
+
+ QFont fnt(qApp->font());
+ if ((size = fnt.pixelSize()) > 0) {
+ // set in pixels - so the scale factor may not make a difference if it's too close to 1
+ size *= scale * printScale;
+ fnt.setPixelSize(size);
+ } else {
+ size = fnt.pointSizeF();
+ size *= scale * printScale;
+ fnt.setPointSizeF(size);
+ }
+ QFontMetrics fm(fnt);
+
+ QPainterPath textPath;
+ qreal xPos = 0, yPos = 0;
+
+ QRectF rect = fm.boundingRect(internalText);
+ yPos = (internalAlignFlags & Qt::AlignTop) ? 0 :
+ (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;
+
+ textPath.addText(xPos, yPos, fnt, internalText);
+ QPainterPathStroker stroker;
+ stroker.setWidth(3);
+ textBackgroundItem->setPath(stroker.createStroke(textPath));
+ textItem->setPath(textPath);
+}