From a5aa4f957579636caf6e0e78e5bbc54848a1e13f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomi=20Korpip=C3=A4=C3=A4?= Date: Tue, 9 Apr 2013 08:25:17 +0300 Subject: Moved text printing to Utils -class Fixed examples Change-Id: Idfb0eb511cb03ac4f038c957f0b93bb5736e6b05 Change-Id: Idfb0eb511cb03ac4f038c957f0b93bb5736e6b05 Reviewed-by: Mika Salmela --- src/datavis3d/utils/utils.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++ src/datavis3d/utils/utils_p.h | 4 ++++ 2 files changed, 52 insertions(+) (limited to 'src/datavis3d/utils') diff --git a/src/datavis3d/utils/utils.cpp b/src/datavis3d/utils/utils.cpp index fe0b9c5a..6a45b1f4 100644 --- a/src/datavis3d/utils/utils.cpp +++ b/src/datavis3d/utils/utils.cpp @@ -43,6 +43,8 @@ #include #include +#include +#include QTCOMMERCIALDATAVIS3D_BEGIN_NAMESPACE @@ -51,4 +53,50 @@ QVector3D Utils::vectorFromColor(const QColor &color) return QVector3D(color.redF(), color.greenF(), color.blueF()); } +void Utils::printText(QPainter *painter, const QString &text, const QPoint &position) +{ + painter->save(); + painter->setCompositionMode(QPainter::CompositionMode_Source); + // TODO: None of the commented-out stuff works.. + //painter->setBackgroundMode(Qt::OpaqueMode); + //painter->setBackground(QBrush(d_ptr->m_textBackgroundColor)); + //painter->setBrush(QBrush(d_ptr->m_textBackgroundColor)); + //painter->setPen(d_ptr->m_textBackgroundColor); + painter->setPen(Qt::black); // TODO: Use black, as nothing works + QFont bgrFont = QFont(QStringLiteral("Arial"), 17); + QFont valueFont = QFont(QStringLiteral("Arial"), 11); + valueFont.setBold(true); + painter->setFont(bgrFont); + QFontMetrics valueFM(valueFont); + QFontMetrics bgrFM(bgrFont); + int valueStrLen = valueFM.width(text); + int bgrStrLen = 0; + int bgrHeight = valueFM.height() + 8; + QString bgrStr = QString(); + do { + bgrStr.append(QStringLiteral("I")); + bgrStrLen = bgrFM.width(bgrStr); + } while (bgrStrLen <= (valueStrLen + 8)); + //int bgrLen = valueStrLen + 10; + //painter->drawRoundedRect(data->d_ptr->position().x() - (bgrLen / 2) + // , data->d_ptr->position().y() - 30 + // , bgrLen, 30, 10.0, 10.0); + // Hack solution, as drawRect doesn't work + painter->drawText(position.x() - (bgrStrLen / 2) + , position.y() - bgrHeight + , bgrStrLen, bgrHeight + , Qt::AlignCenter | Qt::AlignVCenter + , bgrStr); + //painter->setPen(d_ptr->m_textColor); + painter->setPen(Qt::lightGray); // TODO: Use lightGray, as nothing works + painter->setFont(valueFont); + painter->drawText(position.x() - (valueStrLen / 2) + , position.y() - bgrHeight + , valueStrLen, bgrHeight + , Qt::AlignCenter | Qt::AlignVCenter + , text); + painter->restore(); + +} + QTCOMMERCIALDATAVIS3D_END_NAMESPACE diff --git a/src/datavis3d/utils/utils_p.h b/src/datavis3d/utils/utils_p.h index 11ddf61f..4e210fde 100644 --- a/src/datavis3d/utils/utils_p.h +++ b/src/datavis3d/utils/utils_p.h @@ -46,6 +46,9 @@ class QVector3D; class QColor; +class QPainter; +class QString; +class QPoint; QTCOMMERCIALDATAVIS3D_BEGIN_NAMESPACE @@ -53,6 +56,7 @@ class Utils { public: static QVector3D vectorFromColor(const QColor &color); + static void printText(QPainter *painter, const QString &text, const QPoint &position); }; QTCOMMERCIALDATAVIS3D_END_NAMESPACE -- cgit v1.2.3