| 
									
										
										
										
											2017-04-27 20:26:36 +02:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							| 
									
										
										
										
											2014-01-14 15:23:57 -02:00
										 |  |  | #include "divetextitem.h"
 | 
					
						
							| 
									
										
										
										
											2015-02-09 19:51:31 -02:00
										 |  |  | #include "profilewidget2.h"
 | 
					
						
							| 
									
										
										
										
											2016-04-04 22:02:03 -07:00
										 |  |  | #include "core/color.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-05 19:41:15 +02:00
										 |  |  | #include "core/errorhelper.h"
 | 
					
						
							| 
									
										
										
										
											2015-09-03 14:20:19 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <QBrush>
 | 
					
						
							| 
									
										
										
										
											2015-11-05 17:05:30 -08:00
										 |  |  | #include <QDebug>
 | 
					
						
							|  |  |  | #include <QApplication>
 | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | DiveTextItem::DiveTextItem(QGraphicsItem *parent) : QGraphicsItemGroup(parent), | 
					
						
							| 
									
										
										
										
											2014-02-09 19:00:03 +01:00
										 |  |  | 	internalAlignFlags(Qt::AlignHCenter | Qt::AlignVCenter), | 
					
						
							| 
									
										
										
										
											2015-01-16 17:13:58 -02:00
										 |  |  | 	textBackgroundItem(new QGraphicsPathItem(this)), | 
					
						
							|  |  |  | 	textItem(new QGraphicsPathItem(this)), | 
					
						
							| 
									
										
										
										
											2015-07-29 12:21:27 -07:00
										 |  |  | 	printScale(1.0), | 
					
						
							|  |  |  | 	scale(1.0), | 
					
						
							|  |  |  | 	connected(false) | 
					
						
							| 
									
										
										
										
											2014-01-14 15:23:57 -02:00
										 |  |  | { | 
					
						
							|  |  |  | 	setFlag(ItemIgnoresTransformations); | 
					
						
							| 
									
										
										
										
											2015-01-16 17:13:58 -02:00
										 |  |  | 	textBackgroundItem->setBrush(QBrush(getColor(TEXT_BACKGROUND))); | 
					
						
							|  |  |  | 	textBackgroundItem->setPen(Qt::NoPen); | 
					
						
							|  |  |  | 	textItem->setPen(Qt::NoPen); | 
					
						
							| 
									
										
										
										
											2014-01-14 15:23:57 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-29 10:57:05 -07:00
										 |  |  | void DiveTextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	updateText(); | 
					
						
							|  |  |  | 	QGraphicsItemGroup::paint(painter, option, widget); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-29 12:21:27 -07:00
										 |  |  | void DiveTextItem::fontPrintScaleUpdate(double scale) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	printScale = scale; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-14 15:23:57 -02:00
										 |  |  | void DiveTextItem::setAlignment(int alignFlags) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-01-16 17:22:30 -02:00
										 |  |  | 	if (alignFlags != internalAlignFlags) { | 
					
						
							|  |  |  | 		internalAlignFlags = alignFlags; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void DiveTextItem::setBrush(const QBrush &b) | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-01-16 17:17:55 -02:00
										 |  |  | 	textItem->setBrush(b); | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-15 07:03:41 -08:00
										 |  |  | void DiveTextItem::setScale(double newscale) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-01-16 17:22:30 -02:00
										 |  |  | 	if (scale != newscale) { | 
					
						
							|  |  |  | 		scale = newscale; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-15 07:03:41 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void DiveTextItem::setText(const QString &t) | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-01-16 17:22:30 -02:00
										 |  |  | 	if (internalText != t) { | 
					
						
							| 
									
										
										
										
											2015-07-29 12:21:27 -07:00
										 |  |  | 		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 { | 
					
						
							| 
									
										
										
										
											2016-04-13 13:09:22 -07:00
										 |  |  | 				if (verbose) | 
					
						
							|  |  |  | 					qDebug() << "called before scene was set up" << t; | 
					
						
							| 
									
										
										
										
											2015-07-29 12:21:27 -07:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-01-16 17:22:30 -02:00
										 |  |  | 		internalText = t; | 
					
						
							| 
									
										
										
										
											2015-08-05 19:01:50 +03:00
										 |  |  | 		updateText(); | 
					
						
							| 
									
										
										
										
											2015-01-16 17:22:30 -02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-14 15:23:57 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | const QString &DiveTextItem::text() | 
					
						
							| 
									
										
										
										
											2014-01-21 10:48:26 -02:00
										 |  |  | { | 
					
						
							|  |  |  | 	return internalText; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | void DiveTextItem::updateText() | 
					
						
							| 
									
										
										
										
											2014-01-14 15:23:57 -02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-02-15 07:03:41 -08:00
										 |  |  | 	double size; | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 	if (internalText.isEmpty()) { | 
					
						
							| 
									
										
										
										
											2014-01-27 17:09:08 -02:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	QFont fnt(qApp->font()); | 
					
						
							| 
									
										
										
										
											2014-02-15 07:03:41 -08:00
										 |  |  | 	if ((size = fnt.pixelSize()) > 0) { | 
					
						
							|  |  |  | 		// set in pixels - so the scale factor may not make a difference if it's too close to 1
 | 
					
						
							| 
									
										
										
										
											2015-07-29 12:21:27 -07:00
										 |  |  | 		size *= scale * printScale; | 
					
						
							| 
									
										
										
										
											2017-03-23 08:13:49 +07:00
										 |  |  | 		fnt.setPixelSize(lrint(size)); | 
					
						
							| 
									
										
										
										
											2014-02-15 07:03:41 -08:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		size = fnt.pointSizeF(); | 
					
						
							| 
									
										
										
										
											2015-07-29 12:21:27 -07:00
										 |  |  | 		size *= scale * printScale; | 
					
						
							| 
									
										
										
										
											2014-02-15 07:03:41 -08:00
										 |  |  | 		fnt.setPointSizeF(size); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | 	QFontMetrics fm(fnt); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QPainterPath textPath; | 
					
						
							|  |  |  | 	qreal xPos = 0, yPos = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-21 10:48:26 -02:00
										 |  |  | 	QRectF rect = fm.boundingRect(internalText); | 
					
						
							| 
									
										
										
										
											2014-01-29 13:48:06 -02:00
										 |  |  | 	yPos = (internalAlignFlags & Qt::AlignTop) ? 0 : | 
					
						
							| 
									
										
										
										
											2015-01-16 17:13:58 -02:00
										 |  |  | 		(internalAlignFlags & Qt::AlignBottom) ? +rect.height() : | 
					
						
							|  |  |  | 		/*(internalAlignFlags & Qt::AlignVCenter  ? */ +rect.height() / 4; | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 	xPos = (internalAlignFlags & Qt::AlignLeft) ? -rect.width() : | 
					
						
							| 
									
										
										
										
											2015-01-16 17:13:58 -02:00
										 |  |  | 		(internalAlignFlags & Qt::AlignHCenter) ? -rect.width() / 2 : | 
					
						
							|  |  |  | 		/* (internalAlignFlags & Qt::AlignRight) */ 0; | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 	textPath.addText(xPos, yPos, fnt, internalText); | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | 	QPainterPathStroker stroker; | 
					
						
							|  |  |  | 	stroker.setWidth(3); | 
					
						
							| 
									
										
										
										
											2015-01-16 17:13:58 -02:00
										 |  |  | 	textBackgroundItem->setPath(stroker.createStroke(textPath)); | 
					
						
							|  |  |  | 	textItem->setPath(textPath); | 
					
						
							| 
									
										
										
										
											2014-01-14 15:23:57 -02:00
										 |  |  | } |