subsurface/qt-ui/profile/divetextitem.cpp
Dirk Hohndel 02d8dd5d13 Clean up the header files
Lots and lots and lots of header files were being included without being
needed. This attempts to clean some of that crud up.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2015-01-17 23:00:43 +13:00

86 lines
2.1 KiB
C++

#include "divetextitem.h"
#include "mainwindow.h"
DiveTextItem::DiveTextItem(QGraphicsItem *parent) : QGraphicsItemGroup(parent),
internalAlignFlags(Qt::AlignHCenter | Qt::AlignVCenter),
textBackgroundItem(new QGraphicsPathItem(this)),
textItem(new QGraphicsPathItem(this)),
scale(1.0)
{
setFlag(ItemIgnoresTransformations);
textBackgroundItem->setBrush(QBrush(getColor(TEXT_BACKGROUND)));
textBackgroundItem->setPen(Qt::NoPen);
textItem->setPen(Qt::NoPen);
}
void DiveTextItem::setAlignment(int alignFlags)
{
if (alignFlags != internalAlignFlags) {
internalAlignFlags = alignFlags;
updateText();
}
}
void DiveTextItem::setBrush(const QBrush &b)
{
textItem->setBrush(b);
}
void DiveTextItem::setScale(double newscale)
{
if (scale != newscale) {
scale = newscale;
updateText();
}
}
void DiveTextItem::setText(const QString &t)
{
if (internalText != 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 * MainWindow::instance()->graphics()->getFontPrintScale();
fnt.setPixelSize(size);
} else {
size = fnt.pointSizeF();
size *= scale * MainWindow::instance()->graphics()->getFontPrintScale();
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);
}