| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | #include "divetooltipitem.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-05 14:53:57 -02:00
										 |  |  | #include "divecartesianaxis.h"
 | 
					
						
							| 
									
										
										
										
											2014-05-23 21:06:30 -03:00
										 |  |  | #include "profilewidget2.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-05 14:53:57 -02:00
										 |  |  | #include "profile.h"
 | 
					
						
							| 
									
										
										
										
											2014-01-18 16:21:13 -08:00
										 |  |  | #include "dive.h"
 | 
					
						
							|  |  |  | #include "membuffer.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | #include <QPropertyAnimation>
 | 
					
						
							|  |  |  | #include <QGraphicsSceneMouseEvent>
 | 
					
						
							|  |  |  | #include <QPen>
 | 
					
						
							|  |  |  | #include <QBrush>
 | 
					
						
							|  |  |  | #include <QGraphicsScene>
 | 
					
						
							|  |  |  | #include <QSettings>
 | 
					
						
							|  |  |  | #include <QGraphicsView>
 | 
					
						
							| 
									
										
										
										
											2014-02-05 15:25:24 -02:00
										 |  |  | #include <QDebug>
 | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define PORT_IN_PROGRESS 1
 | 
					
						
							|  |  |  | #ifdef PORT_IN_PROGRESS
 | 
					
						
							|  |  |  | #include "display.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void ToolTipItem::addToolTip(const QString &toolTip, const QIcon &icon) | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | { | 
					
						
							|  |  |  | 	QGraphicsPixmapItem *iconItem = 0; | 
					
						
							|  |  |  | 	double yValue = title->boundingRect().height() + SPACING; | 
					
						
							| 
									
										
										
										
											2014-05-22 11:40:22 -07:00
										 |  |  | 	Q_FOREACH (ToolTip t, toolTips) { | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | 		yValue += t.second->boundingRect().height(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (!icon.isNull()) { | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 		iconItem = new QGraphicsPixmapItem(icon.pixmap(ICON_SMALL, ICON_SMALL), this); | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | 		iconItem->setPos(SPACING, yValue); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QGraphicsSimpleTextItem *textItem = new QGraphicsSimpleTextItem(toolTip, this); | 
					
						
							|  |  |  | 	textItem->setPos(SPACING + ICON_SMALL + SPACING, yValue); | 
					
						
							|  |  |  | 	textItem->setBrush(QBrush(Qt::white)); | 
					
						
							|  |  |  | 	textItem->setFlag(ItemIgnoresTransformations); | 
					
						
							|  |  |  | 	toolTips.push_back(qMakePair(iconItem, textItem)); | 
					
						
							|  |  |  | 	expand(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ToolTipItem::clear() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-05-22 11:40:22 -07:00
										 |  |  | 	Q_FOREACH (ToolTip t, toolTips) { | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | 		delete t.first; | 
					
						
							|  |  |  | 		delete t.second; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	toolTips.clear(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void ToolTipItem::setRect(const QRectF &r) | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | { | 
					
						
							|  |  |  | 	// qDeleteAll(childItems());
 | 
					
						
							|  |  |  | 	delete background; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rectangle = r; | 
					
						
							|  |  |  | 	setBrush(QBrush(Qt::white)); | 
					
						
							|  |  |  | 	setPen(QPen(Qt::black, 0.5)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Creates a 2pixels border
 | 
					
						
							|  |  |  | 	QPainterPath border; | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 	border.addRoundedRect(-4, -4, rectangle.width() + 8, rectangle.height() + 10, 3, 3); | 
					
						
							|  |  |  | 	border.addRoundedRect(-1, -1, rectangle.width() + 3, rectangle.height() + 4, 3, 3); | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | 	setPath(border); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QPainterPath bg; | 
					
						
							|  |  |  | 	bg.addRoundedRect(-1, -1, rectangle.width() + 3, rectangle.height() + 4, 3, 3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QColor c = QColor(Qt::black); | 
					
						
							|  |  |  | 	c.setAlpha(155); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QGraphicsPathItem *b = new QGraphicsPathItem(bg, this); | 
					
						
							|  |  |  | 	b->setFlag(ItemStacksBehindParent); | 
					
						
							|  |  |  | 	b->setFlag(ItemIgnoresTransformations); | 
					
						
							|  |  |  | 	b->setBrush(c); | 
					
						
							|  |  |  | 	b->setPen(QPen(QBrush(Qt::transparent), 0)); | 
					
						
							|  |  |  | 	b->setZValue(-10); | 
					
						
							|  |  |  | 	background = b; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	updateTitlePosition(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ToolTipItem::collapse() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QPropertyAnimation *animation = new QPropertyAnimation(this, "rect"); | 
					
						
							|  |  |  | 	animation->setDuration(100); | 
					
						
							|  |  |  | 	animation->setStartValue(nextRectangle); | 
					
						
							|  |  |  | 	animation->setEndValue(QRect(0, 0, ICON_SMALL, ICON_SMALL)); | 
					
						
							|  |  |  | 	animation->start(QAbstractAnimation::DeleteWhenStopped); | 
					
						
							|  |  |  | 	clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	status = COLLAPSED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ToolTipItem::expand() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (!title) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	double width = 0, height = title->boundingRect().height() + SPACING; | 
					
						
							| 
									
										
										
										
											2014-05-22 11:40:22 -07:00
										 |  |  | 	Q_FOREACH (ToolTip t, toolTips) { | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | 		if (t.second->boundingRect().width() > width) | 
					
						
							|  |  |  | 			width = t.second->boundingRect().width(); | 
					
						
							|  |  |  | 		height += t.second->boundingRect().height(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	/*       Left padding, Icon Size,   space, right padding */ | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 	width += SPACING + ICON_SMALL + SPACING + SPACING; | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 	if (width < title->boundingRect().width() + SPACING * 2) | 
					
						
							|  |  |  | 		width = title->boundingRect().width() + SPACING * 2; | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (height < ICON_SMALL) | 
					
						
							|  |  |  | 		height = ICON_SMALL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	nextRectangle.setWidth(width); | 
					
						
							|  |  |  | 	nextRectangle.setHeight(height); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QPropertyAnimation *animation = new QPropertyAnimation(this, "rect"); | 
					
						
							|  |  |  | 	animation->setDuration(100); | 
					
						
							|  |  |  | 	animation->setStartValue(rectangle); | 
					
						
							|  |  |  | 	animation->setEndValue(nextRectangle); | 
					
						
							|  |  |  | 	animation->start(QAbstractAnimation::DeleteWhenStopped); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	status = EXPANDED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | ToolTipItem::ToolTipItem(QGraphicsItem *parent) : QGraphicsPathItem(parent), | 
					
						
							| 
									
										
										
										
											2014-02-09 19:04:59 +01:00
										 |  |  | 	background(0), | 
					
						
							|  |  |  | 	separator(new QGraphicsLineItem(this)), | 
					
						
							|  |  |  | 	title(new QGraphicsSimpleTextItem(tr("Information"), this)), | 
					
						
							|  |  |  | 	status(COLLAPSED), | 
					
						
							| 
									
										
										
										
											2014-02-19 16:06:16 -08:00
										 |  |  | 	timeAxis(0), | 
					
						
							|  |  |  | 	lastTime(-1) | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-02-09 19:04:59 +01:00
										 |  |  | 	memset(&pInfo, 0, sizeof(pInfo)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | 	setFlags(ItemIgnoresTransformations | ItemIsMovable | ItemClipsChildrenToShape); | 
					
						
							|  |  |  | 	updateTitlePosition(); | 
					
						
							|  |  |  | 	setZValue(99); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ToolTipItem::~ToolTipItem() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	clear(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ToolTipItem::updateTitlePosition() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 	if (rectangle.width() < title->boundingRect().width() + SPACING * 4) { | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | 		QRectF newRect = rectangle; | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 		newRect.setWidth(title->boundingRect().width() + SPACING * 4); | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | 		newRect.setHeight((newRect.height() && isExpanded()) ? newRect.height() : ICON_SMALL); | 
					
						
							|  |  |  | 		setRect(newRect); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 	title->setPos(boundingRect().width() / 2 - title->boundingRect().width() / 2 - 1, 0); | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | 	title->setFlag(ItemIgnoresTransformations); | 
					
						
							|  |  |  | 	title->setPen(QPen(Qt::white, 1)); | 
					
						
							|  |  |  | 	title->setBrush(Qt::white); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (toolTips.size() > 0) { | 
					
						
							|  |  |  | 		double x1 = 3; | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 		double y1 = title->pos().y() + SPACING / 2 + title->boundingRect().height(); | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | 		double x2 = boundingRect().width() - 10; | 
					
						
							|  |  |  | 		double y2 = y1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		separator->setLine(x1, y1, x2, y2); | 
					
						
							|  |  |  | 		separator->setFlag(ItemIgnoresTransformations); | 
					
						
							|  |  |  | 		separator->setPen(QPen(Qt::white)); | 
					
						
							|  |  |  | 		separator->show(); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		separator->hide(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | bool ToolTipItem::isExpanded() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | 	return status == EXPANDED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void ToolTipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | { | 
					
						
							|  |  |  | 	persistPos(); | 
					
						
							|  |  |  | 	QGraphicsPathItem::mouseReleaseEvent(event); | 
					
						
							| 
									
										
										
										
											2014-05-23 21:06:30 -03:00
										 |  |  | 	Q_FOREACH (QGraphicsItem *item, oldSelection) { | 
					
						
							|  |  |  | 		item->setSelected(true); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ToolTipItem::persistPos() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QSettings s; | 
					
						
							|  |  |  | 	s.beginGroup("ProfileMap"); | 
					
						
							| 
									
										
										
										
											2014-03-13 10:23:20 -07:00
										 |  |  | 	s.setValue("tooltip_position", pos()); | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | 	s.endGroup(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ToolTipItem::readPos() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QSettings s; | 
					
						
							|  |  |  | 	s.beginGroup("ProfileMap"); | 
					
						
							| 
									
										
										
										
											2014-03-13 10:23:20 -07:00
										 |  |  | 	QPointF value = s.value("tooltip_position").toPoint(); | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | 	if (!scene()->sceneRect().contains(value)) { | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 		value = QPointF(0, 0); | 
					
						
							| 
									
										
										
										
											2014-02-05 14:25:28 -02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	setPos(value); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-05 14:53:57 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void ToolTipItem::setPlotInfo(const plot_info &plot) | 
					
						
							| 
									
										
										
										
											2014-02-05 14:53:57 -02:00
										 |  |  | { | 
					
						
							|  |  |  | 	pInfo = plot; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void ToolTipItem::setTimeAxis(DiveCartesianAxis *axis) | 
					
						
							| 
									
										
										
										
											2014-02-05 14:53:57 -02:00
										 |  |  | { | 
					
						
							|  |  |  | 	timeAxis = axis; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void ToolTipItem::refresh(const QPointF &pos) | 
					
						
							| 
									
										
										
										
											2014-02-05 14:53:57 -02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 	int time = timeAxis->valueAt(pos); | 
					
						
							| 
									
										
										
										
											2014-02-19 16:06:16 -08:00
										 |  |  | 	if (time == lastTime) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	lastTime = time; | 
					
						
							|  |  |  | 	clear(); | 
					
						
							| 
									
										
										
										
											2014-01-18 16:21:13 -08:00
										 |  |  | 	struct membuffer mb = { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	get_plot_details_new(&pInfo, time, &mb); | 
					
						
							|  |  |  | 	addToolTip(QString::fromUtf8(mb.buffer, mb.len)); | 
					
						
							|  |  |  | 	free_buffer(&mb); | 
					
						
							| 
									
										
										
										
											2014-02-05 14:53:57 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-15 14:43:20 -03:00
										 |  |  | 	Q_FOREACH (QGraphicsItem *item, scene()->items(pos, Qt::IntersectsItemShape | 
					
						
							|  |  |  | 		,Qt::DescendingOrder, scene()->views().first()->transform())) { | 
					
						
							| 
									
										
										
										
											2014-02-05 14:53:57 -02:00
										 |  |  | 		if (!item->toolTip().isEmpty()) | 
					
						
							|  |  |  | 			addToolTip(item->toolTip()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-05-23 21:06:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | void ToolTipItem::mousePressEvent(QGraphicsSceneMouseEvent *event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	oldSelection = scene()->selectedItems(); | 
					
						
							|  |  |  | 	scene()->clearSelection(); | 
					
						
							|  |  |  | 	QGraphicsItem::mousePressEvent(event); | 
					
						
							|  |  |  | } |