| 
									
										
										
										
											2014-01-14 15:23:57 -02:00
										 |  |  | #include "divetextitem.h"
 | 
					
						
							| 
									
										
										
										
											2014-01-14 16:01:17 -02:00
										 |  |  | #include "animationfunctions.h"
 | 
					
						
							| 
									
										
										
										
											2014-07-09 13:09:52 -07:00
										 |  |  | #include "mainwindow.h"
 | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-14 15:23:57 -02:00
										 |  |  | #include <QPropertyAnimation>
 | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | #include <QApplication>
 | 
					
						
							|  |  |  | #include <QFont>
 | 
					
						
							|  |  |  | #include <QFontMetrics>
 | 
					
						
							|  |  |  | #include <QBrush>
 | 
					
						
							|  |  |  | #include <QPen>
 | 
					
						
							| 
									
										
										
										
											2014-01-19 17:19:00 -02:00
										 |  |  | #include <QDebug>
 | 
					
						
							| 
									
										
										
										
											2014-01-14 15:23:57 -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), | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | 	textBackgroundItem(NULL), | 
					
						
							|  |  |  | 	textItem(NULL), | 
					
						
							| 
									
										
										
										
											2014-02-15 07:03:41 -08:00
										 |  |  | 	colorIndex(SAC_DEFAULT), | 
					
						
							|  |  |  | 	scale(1.0) | 
					
						
							| 
									
										
										
										
											2014-01-14 15:23:57 -02:00
										 |  |  | { | 
					
						
							|  |  |  | 	setFlag(ItemIgnoresTransformations); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DiveTextItem::setAlignment(int alignFlags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	internalAlignFlags = alignFlags; | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | 	updateText(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void DiveTextItem::setBrush(const QBrush &b) | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | { | 
					
						
							|  |  |  | 	brush = b; | 
					
						
							|  |  |  | 	updateText(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-15 07:03:41 -08:00
										 |  |  | void DiveTextItem::setScale(double newscale) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	scale = newscale; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void DiveTextItem::setText(const QString &t) | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-01-21 10:48:26 -02:00
										 |  |  | 	internalText = t; | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | 	updateText(); | 
					
						
							| 
									
										
										
										
											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-01-19 16:16:04 -02:00
										 |  |  | 	delete textItem; | 
					
						
							| 
									
										
										
										
											2014-01-27 17:09:08 -02:00
										 |  |  | 	textItem = NULL; | 
					
						
							| 
									
										
										
										
											2014-01-19 16:16:04 -02:00
										 |  |  | 	delete textBackgroundItem; | 
					
						
							| 
									
										
										
										
											2014-01-27 17:09:08 -02:00
										 |  |  | 	textBackgroundItem = NULL; | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2014-07-09 13:09:52 -07:00
										 |  |  | 		size *= scale * MainWindow::instance()->graphics()->getFontPrintScale(); | 
					
						
							| 
									
										
										
										
											2014-02-15 07:03:41 -08:00
										 |  |  | 		fnt.setPixelSize(size); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		size = fnt.pointSizeF(); | 
					
						
							| 
									
										
										
										
											2014-07-11 18:55:33 -03:00
										 |  |  | 		size *= scale * MainWindow::instance()->graphics()->getFontPrintScale(); | 
					
						
							| 
									
										
										
										
											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 : | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08: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() : | 
					
						
							|  |  |  | 						      (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); | 
					
						
							|  |  |  | 	textBackgroundItem = new QGraphicsPathItem(stroker.createStroke(textPath), this); | 
					
						
							|  |  |  | 	textBackgroundItem->setBrush(QBrush(getColor(TEXT_BACKGROUND))); | 
					
						
							|  |  |  | 	textBackgroundItem->setPen(Qt::NoPen); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	textItem = new QGraphicsPathItem(textPath, this); | 
					
						
							|  |  |  | 	textItem->setBrush(brush); | 
					
						
							|  |  |  | 	textItem->setPen(Qt::NoPen); | 
					
						
							| 
									
										
										
										
											2014-01-14 15:23:57 -02:00
										 |  |  | } |