diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2015-07-29 12:21:27 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2015-07-29 12:21:27 -0700 |
commit | 64aace63f0a8478be6538e87fecd574f8cc29e86 (patch) | |
tree | e66b6555cc49d92cac83888145ce7b45e9f709b0 | |
parent | 487ddce3531f6b738affc84edf066bd0ea4a16bb (diff) | |
download | subsurface-64aace63f0a8478be6538e87fecd574f8cc29e86.tar.gz |
Don't connect to the MainWindow
We really shouldn't need to connect to the MainWindow in order to get the
right scale for the fonts when printing. While printing likely will remain
a desktop only function, this is just bad design. And making calls like
this from the paint() function is a bad plan, anyway.
So instead we make sure that every DiveTextItem knows what the printScale
was when it was created (or actually, when the text was first set as they
frequently get created before we have a scene which we use to get to the
profile), and gets updated whenever that scale changes.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r-- | qt-ui/profile/divetextitem.cpp | 25 | ||||
-rw-r--r-- | qt-ui/profile/divetextitem.h | 6 | ||||
-rw-r--r-- | qt-ui/profile/profilewidget2.cpp | 1 | ||||
-rw-r--r-- | qt-ui/profile/profilewidget2.h | 3 |
4 files changed, 32 insertions, 3 deletions
diff --git a/qt-ui/profile/divetextitem.cpp b/qt-ui/profile/divetextitem.cpp index b3d5c39aa..0831a024e 100644 --- a/qt-ui/profile/divetextitem.cpp +++ b/qt-ui/profile/divetextitem.cpp @@ -6,7 +6,9 @@ DiveTextItem::DiveTextItem(QGraphicsItem *parent) : QGraphicsItemGroup(parent), internalAlignFlags(Qt::AlignHCenter | Qt::AlignVCenter), textBackgroundItem(new QGraphicsPathItem(this)), textItem(new QGraphicsPathItem(this)), - scale(1.0) + printScale(1.0), + scale(1.0), + connected(false) { setFlag(ItemIgnoresTransformations); textBackgroundItem->setBrush(QBrush(getColor(TEXT_BACKGROUND))); @@ -20,6 +22,11 @@ void DiveTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti QGraphicsItemGroup::paint(painter, option, widget); } +void DiveTextItem::fontPrintScaleUpdate(double scale) +{ + printScale = scale; +} + void DiveTextItem::setAlignment(int alignFlags) { if (alignFlags != internalAlignFlags) { @@ -42,6 +49,18 @@ void DiveTextItem::setScale(double 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; } } @@ -61,11 +80,11 @@ void DiveTextItem::updateText() 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(); + size *= scale * printScale; fnt.setPixelSize(size); } else { size = fnt.pointSizeF(); - size *= scale * MainWindow::instance()->graphics()->getFontPrintScale(); + size *= scale * printScale; fnt.setPointSizeF(size); } QFontMetrics fm(fnt); diff --git a/qt-ui/profile/divetextitem.h b/qt-ui/profile/divetextitem.h index 26e45d746..3991fe7f3 100644 --- a/qt-ui/profile/divetextitem.h +++ b/qt-ui/profile/divetextitem.h @@ -20,13 +20,19 @@ public: const QString &text(); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); +private +slots: + void fontPrintScaleUpdate(double scale); + private: void updateText(); int internalAlignFlags; QGraphicsPathItem *textBackgroundItem; QGraphicsPathItem *textItem; QString internalText; + double printScale; double scale; + bool connected; }; #endif // DIVETEXTITEM_H diff --git a/qt-ui/profile/profilewidget2.cpp b/qt-ui/profile/profilewidget2.cpp index 76fcd48e0..400401655 100644 --- a/qt-ui/profile/profilewidget2.cpp +++ b/qt-ui/profile/profilewidget2.cpp @@ -1465,6 +1465,7 @@ void ProfileWidget2::setPrintMode(bool mode, bool grayscale) void ProfileWidget2::setFontPrintScale(double scale) { fontPrintScale = scale; + emit fontPrintScaleChanged(scale); } double ProfileWidget2::getFontPrintScale() diff --git a/qt-ui/profile/profilewidget2.h b/qt-ui/profile/profilewidget2.h index 75d072dac..1127b8498 100644 --- a/qt-ui/profile/profilewidget2.h +++ b/qt-ui/profile/profilewidget2.h @@ -86,6 +86,9 @@ public: void setToolTipVisibile(bool visible); State currentState; +signals: + void fontPrintScaleChanged(double scale); + public slots: // Necessary to call from QAction's signals. void settingsChanged(); |