| 
									
										
										
										
											2017-04-27 20:26:36 +02:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							| 
									
										
										
										
											2016-04-04 22:02:03 -07:00
										 |  |  | #include "profile-widget/ruleritem.h"
 | 
					
						
							|  |  |  | #include "profile-widget/profilewidget2.h"
 | 
					
						
							|  |  |  | #include "core/display.h"
 | 
					
						
							| 
									
										
										
										
											2018-08-15 11:52:50 +02:00
										 |  |  | #include "core/settings/qPrefTechnicalDetails.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-27 22:52:03 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-01 19:27:44 -03:00
										 |  |  | #include <qgraphicssceneevent.h>
 | 
					
						
							| 
									
										
										
										
											2014-02-27 14:59:41 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-04 22:02:03 -07:00
										 |  |  | #include "core/profile.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-27 13:28:58 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-22 06:42:02 -07:00
										 |  |  | RulerNodeItem2::RulerNodeItem2() : | 
					
						
							| 
									
										
										
										
											2019-07-06 11:49:29 +02:00
										 |  |  | 	idx(-1), | 
					
						
							| 
									
										
										
										
											2015-06-22 06:42:02 -07:00
										 |  |  | 	ruler(NULL), | 
					
						
							|  |  |  | 	timeAxis(NULL), | 
					
						
							|  |  |  | 	depthAxis(NULL) | 
					
						
							| 
									
										
										
										
											2014-02-27 13:28:58 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-08-25 12:56:41 +02:00
										 |  |  | 	init_plot_info(&pInfo); | 
					
						
							| 
									
										
										
										
											2014-03-09 10:59:31 -03:00
										 |  |  | 	setRect(-8, -8, 16, 16); | 
					
						
							| 
									
										
										
										
											2014-02-27 13:28:58 -03:00
										 |  |  | 	setBrush(QColor(0xff, 0, 0, 127)); | 
					
						
							| 
									
										
										
										
											2014-03-09 10:59:31 -03:00
										 |  |  | 	setPen(QColor(Qt::red)); | 
					
						
							|  |  |  | 	setFlag(ItemIsMovable); | 
					
						
							| 
									
										
										
										
											2014-02-27 13:28:58 -03:00
										 |  |  | 	setFlag(ItemSendsGeometryChanges); | 
					
						
							|  |  |  | 	setFlag(ItemIgnoresTransformations); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-19 20:05:39 +01:00
										 |  |  | void RulerNodeItem2::setPlotInfo(const plot_info &info) | 
					
						
							| 
									
										
										
										
											2014-03-07 12:08:31 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	pInfo = info; | 
					
						
							| 
									
										
										
										
											2019-07-06 11:49:29 +02:00
										 |  |  | 	idx = 0; | 
					
						
							| 
									
										
										
										
											2014-03-07 12:08:31 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 13:28:58 -03:00
										 |  |  | void RulerNodeItem2::setRuler(RulerItem2 *r) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ruler = r; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RulerNodeItem2::recalculate() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-06 11:49:29 +02:00
										 |  |  | 	if (pInfo.nr <= 0) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	const struct plot_data &last = pInfo.entry[pInfo.nr - 1]; | 
					
						
							| 
									
										
										
										
											2014-02-27 14:59:41 -03:00
										 |  |  | 	if (x() < 0) { | 
					
						
							|  |  |  | 		setPos(0, y()); | 
					
						
							| 
									
										
										
										
											2019-07-06 11:49:29 +02:00
										 |  |  | 	} else if (x() > timeAxis->posAtValue(last.sec)) { | 
					
						
							|  |  |  | 		setPos(timeAxis->posAtValue(last.sec), depthAxis->posAtValue(last.depth)); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:59:41 -03:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2019-07-06 11:49:29 +02:00
										 |  |  | 		idx = 0; | 
					
						
							|  |  |  | 		while (idx < pInfo.nr && timeAxis->posAtValue(pInfo.entry[idx].sec) < x()) | 
					
						
							|  |  |  | 			++idx; | 
					
						
							|  |  |  | 		const struct plot_data &data = pInfo.entry[idx]; | 
					
						
							|  |  |  | 		setPos(timeAxis->posAtValue(data.sec), depthAxis->posAtValue(data.depth)); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:59:41 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-27 13:28:58 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-01 19:27:44 -03:00
										 |  |  | void RulerNodeItem2::mouseMoveEvent(QGraphicsSceneMouseEvent *event) | 
					
						
							| 
									
										
										
										
											2014-02-27 13:28:58 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-11-17 22:59:19 +02:00
										 |  |  | 	qreal x = event->scenePos().x(); | 
					
						
							|  |  |  | 	if (x < 0.0) | 
					
						
							|  |  |  | 		x = 0.0; | 
					
						
							|  |  |  | 	setPos(x, event->scenePos().y()); | 
					
						
							| 
									
										
										
										
											2014-06-01 19:27:44 -03:00
										 |  |  | 	recalculate(); | 
					
						
							|  |  |  | 	ruler->recalculate(); | 
					
						
							| 
									
										
										
										
											2014-02-27 13:28:58 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-10 13:43:43 +02:00
										 |  |  | RulerItem2::RulerItem2() : source(new RulerNodeItem2()), | 
					
						
							| 
									
										
										
										
											2014-03-07 12:08:31 -03:00
										 |  |  | 	dest(new RulerNodeItem2()), | 
					
						
							| 
									
										
										
										
											2014-03-10 13:43:43 +02:00
										 |  |  | 	timeAxis(NULL), | 
					
						
							|  |  |  | 	depthAxis(NULL), | 
					
						
							| 
									
										
										
										
											2014-03-12 00:09:53 +02:00
										 |  |  | 	textItemBack(new QGraphicsRectItem(this)), | 
					
						
							| 
									
										
										
										
											2014-02-27 22:52:03 -03:00
										 |  |  | 	textItem(new QGraphicsSimpleTextItem(this)) | 
					
						
							| 
									
										
										
										
											2014-02-27 13:28:58 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-02-27 16:28:19 -03:00
										 |  |  | 	memset(&pInfo, 0, sizeof(pInfo)); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:59:41 -03:00
										 |  |  | 	source->setRuler(this); | 
					
						
							|  |  |  | 	dest->setRuler(this); | 
					
						
							| 
									
										
										
										
											2014-02-27 22:52:03 -03:00
										 |  |  | 	textItem->setFlag(QGraphicsItem::ItemIgnoresTransformations); | 
					
						
							| 
									
										
										
										
											2014-03-12 00:09:53 +02:00
										 |  |  | 	textItemBack->setBrush(QColor(0xff, 0xff, 0xff, 190)); | 
					
						
							|  |  |  | 	textItemBack->setPen(QColor(Qt::white)); | 
					
						
							| 
									
										
										
										
											2014-03-25 23:34:08 +02:00
										 |  |  | 	textItemBack->setFlag(QGraphicsItem::ItemIgnoresTransformations); | 
					
						
							| 
									
										
										
										
											2014-03-19 18:24:42 +02:00
										 |  |  | 	setPen(QPen(QColor(Qt::black), 0.0)); | 
					
						
							| 
									
										
										
										
											2015-11-06 10:20:18 -08:00
										 |  |  | #ifndef SUBSURFACE_MOBILE
 | 
					
						
							| 
									
										
										
										
											2018-09-02 14:20:15 +02:00
										 |  |  | 	connect(qPrefTechnicalDetails::instance(), &qPrefTechnicalDetails::rulergraphChanged, this, &RulerItem2::settingsChanged); | 
					
						
							| 
									
										
										
										
											2015-11-06 10:20:18 -08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2014-05-21 14:24:19 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-25 18:15:06 -02:00
										 |  |  | void RulerItem2::settingsChanged(bool value) | 
					
						
							| 
									
										
										
										
											2014-05-21 14:24:19 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	ProfileWidget2 *profWidget = NULL; | 
					
						
							| 
									
										
										
										
											2014-05-22 11:40:22 -07:00
										 |  |  | 	if (scene() && scene()->views().count()) | 
					
						
							|  |  |  | 		profWidget = qobject_cast<ProfileWidget2 *>(scene()->views().first()); | 
					
						
							| 
									
										
										
										
											2015-03-17 20:35:11 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-25 18:15:06 -02:00
										 |  |  | 	setVisible( (profWidget && profWidget->currentState == ProfileWidget2::PROFILE) ? value : false); | 
					
						
							| 
									
										
										
										
											2014-02-27 13:28:58 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void RulerItem2::recalculate() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char buffer[500]; | 
					
						
							|  |  |  | 	QPointF tmp; | 
					
						
							|  |  |  | 	QFont font; | 
					
						
							|  |  |  | 	QFontMetrics fm(font); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 16:28:19 -03:00
										 |  |  | 	if (timeAxis == NULL || depthAxis == NULL || pInfo.nr == 0) | 
					
						
							| 
									
										
										
										
											2014-02-27 13:28:58 -03:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	prepareGeometryChange(); | 
					
						
							|  |  |  | 	startPoint = mapFromItem(source, 0, 0); | 
					
						
							|  |  |  | 	endPoint = mapFromItem(dest, 0, 0); | 
					
						
							| 
									
										
										
										
											2014-02-27 22:52:03 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 13:28:58 -03:00
										 |  |  | 	if (startPoint.x() > endPoint.x()) { | 
					
						
							|  |  |  | 		tmp = endPoint; | 
					
						
							|  |  |  | 		endPoint = startPoint; | 
					
						
							|  |  |  | 		startPoint = tmp; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	QLineF line(startPoint, endPoint); | 
					
						
							| 
									
										
										
										
											2014-03-09 10:59:31 -03:00
										 |  |  | 	setLine(line); | 
					
						
							| 
									
										
										
										
											2019-07-06 11:58:27 +02:00
										 |  |  | 	compare_samples(&pInfo, source->idx, dest->idx, buffer, 500, 1); | 
					
						
							| 
									
										
										
										
											2014-02-27 13:28:58 -03:00
										 |  |  | 	text = QString(buffer); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 00:09:53 +02:00
										 |  |  | 	// draw text
 | 
					
						
							| 
									
										
										
										
											2014-02-27 22:52:03 -03:00
										 |  |  | 	QGraphicsView *view = scene()->views().first(); | 
					
						
							|  |  |  | 	QPoint begin = view->mapFromScene(mapToScene(startPoint)); | 
					
						
							|  |  |  | 	textItem->setText(text); | 
					
						
							| 
									
										
										
										
											2014-03-12 00:09:53 +02:00
										 |  |  | 	qreal tgtX = startPoint.x(); | 
					
						
							|  |  |  | 	const qreal diff = begin.x() + textItem->boundingRect().width(); | 
					
						
							|  |  |  | 	// clamp so that the text doesn't go out of the screen to the right
 | 
					
						
							|  |  |  | 	if (diff > view->width()) { | 
					
						
							| 
									
										
										
										
											2017-03-23 08:13:49 +07:00
										 |  |  | 		begin.setX(lrint(begin.x() - (diff - view->width()))); | 
					
						
							| 
									
										
										
										
											2014-03-12 00:09:53 +02:00
										 |  |  | 		tgtX = mapFromScene(view->mapToScene(begin)).x(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// always show the text bellow the lowest of the start and end points
 | 
					
						
							|  |  |  | 	qreal tgtY = (startPoint.y() >= endPoint.y()) ? startPoint.y() : endPoint.y(); | 
					
						
							| 
									
										
										
										
											2014-03-25 23:34:08 +02:00
										 |  |  | 	// this isn't exactly optimal, since we want to scale the 1.0, 4.0 distances as well
 | 
					
						
							|  |  |  | 	textItem->setPos(tgtX - 1.0, tgtY + 4.0); | 
					
						
							| 
									
										
										
										
											2014-03-12 00:09:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// setup the text background
 | 
					
						
							|  |  |  | 	textItemBack->setVisible(startPoint.x() != endPoint.x()); | 
					
						
							| 
									
										
										
										
											2014-03-25 23:34:08 +02:00
										 |  |  | 	textItemBack->setPos(textItem->x(), textItem->y()); | 
					
						
							|  |  |  | 	textItemBack->setRect(0, 0, textItem->boundingRect().width(), textItem->boundingRect().height()); | 
					
						
							| 
									
										
										
										
											2014-02-27 13:28:58 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RulerNodeItem2 *RulerItem2::sourceNode() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return source; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | RulerNodeItem2 *RulerItem2::destNode() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return dest; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-19 20:05:39 +01:00
										 |  |  | void RulerItem2::setPlotInfo(const plot_info &info) | 
					
						
							| 
									
										
										
										
											2014-02-27 14:59:41 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-19 20:05:39 +01:00
										 |  |  | 	pInfo = info; | 
					
						
							|  |  |  | 	dest->setPlotInfo(info); | 
					
						
							|  |  |  | 	source->setPlotInfo(info); | 
					
						
							| 
									
										
										
										
											2014-02-27 16:42:00 -03:00
										 |  |  | 	dest->recalculate(); | 
					
						
							|  |  |  | 	source->recalculate(); | 
					
						
							| 
									
										
										
										
											2014-03-07 12:08:31 -03:00
										 |  |  | 	recalculate(); | 
					
						
							| 
									
										
										
										
											2014-02-27 16:28:19 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void RulerItem2::setAxis(DiveCartesianAxis *time, DiveCartesianAxis *depth) | 
					
						
							| 
									
										
										
										
											2014-02-27 16:28:19 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	timeAxis = time; | 
					
						
							|  |  |  | 	depthAxis = depth; | 
					
						
							|  |  |  | 	dest->depthAxis = depth; | 
					
						
							|  |  |  | 	dest->timeAxis = time; | 
					
						
							|  |  |  | 	source->depthAxis = depth; | 
					
						
							|  |  |  | 	source->timeAxis = time; | 
					
						
							| 
									
										
										
										
											2014-02-27 14:59:41 -03:00
										 |  |  | 	recalculate(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-05-21 12:18:05 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | void RulerItem2::setVisible(bool visible) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QGraphicsLineItem::setVisible(visible); | 
					
						
							| 
									
										
										
										
											2014-05-21 14:26:00 -03:00
										 |  |  | 	source->setVisible(visible); | 
					
						
							|  |  |  | 	dest->setVisible(visible); | 
					
						
							| 
									
										
										
										
											2014-05-21 12:18:05 -03:00
										 |  |  | } |