| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | #include "diveeventitem.h"
 | 
					
						
							|  |  |  | #include "diveplotdatamodel.h"
 | 
					
						
							|  |  |  | #include "divecartesianaxis.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-15 21:05:47 -02:00
										 |  |  | #include "animationfunctions.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-25 12:27:12 -08:00
										 |  |  | #include "libdivecomputer.h"
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | #include "dive.h"
 | 
					
						
							| 
									
										
										
										
											2014-07-01 12:07:38 -07:00
										 |  |  | #include "planner.h"
 | 
					
						
							| 
									
										
										
										
											2014-03-15 21:12:34 -07:00
										 |  |  | #include "profile.h"
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | #include <QDebug>
 | 
					
						
							| 
									
										
										
										
											2014-04-29 22:37:19 -07:00
										 |  |  | #include "gettextfromc.h"
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-15 21:12:34 -07:00
										 |  |  | extern struct ev_select *ev_namelist; | 
					
						
							|  |  |  | extern int evn_used; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | DiveEventItem::DiveEventItem(QObject *parent) : DivePixmapItem(parent), | 
					
						
							|  |  |  | 	vAxis(NULL), | 
					
						
							|  |  |  | 	hAxis(NULL), | 
					
						
							|  |  |  | 	dataModel(NULL), | 
					
						
							|  |  |  | 	internalEvent(NULL) | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | { | 
					
						
							|  |  |  | 	setFlag(ItemIgnoresTransformations); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void DiveEventItem::setHorizontalAxis(DiveCartesianAxis *axis) | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | { | 
					
						
							|  |  |  | 	hAxis = axis; | 
					
						
							| 
									
										
										
										
											2014-02-15 21:05:47 -02:00
										 |  |  | 	recalculatePos(true); | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void DiveEventItem::setModel(DivePlotDataModel *model) | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | { | 
					
						
							|  |  |  | 	dataModel = model; | 
					
						
							| 
									
										
										
										
											2014-02-15 21:05:47 -02:00
										 |  |  | 	recalculatePos(true); | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void DiveEventItem::setVerticalAxis(DiveCartesianAxis *axis) | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | { | 
					
						
							|  |  |  | 	vAxis = axis; | 
					
						
							| 
									
										
										
										
											2014-02-15 21:05:47 -02:00
										 |  |  | 	recalculatePos(true); | 
					
						
							|  |  |  | 	connect(vAxis, SIGNAL(sizeChanged()), this, SLOT(recalculatePos())); | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-25 12:27:12 -08:00
										 |  |  | struct event *DiveEventItem::getEvent() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return internalEvent; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void DiveEventItem::setEvent(struct event *ev) | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-02-23 08:35:06 -08:00
										 |  |  | 	if (!ev) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | 	internalEvent = ev; | 
					
						
							|  |  |  | 	setupPixmap(); | 
					
						
							|  |  |  | 	setupToolTipString(); | 
					
						
							| 
									
										
										
										
											2014-02-15 21:05:47 -02:00
										 |  |  | 	recalculatePos(true); | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DiveEventItem::setupPixmap() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | #define EVENT_PIXMAP(PIX) QPixmap(QString(PIX)).scaled(20, 20, Qt::KeepAspectRatio, Qt::SmoothTransformation)
 | 
					
						
							| 
									
										
										
										
											2014-07-10 10:54:05 -07:00
										 |  |  | #define EVENT_PIXMAP_BIGGER(PIX) QPixmap(QString(PIX)).scaled(40, 38, Qt::KeepAspectRatio, Qt::SmoothTransformation)
 | 
					
						
							| 
									
										
										
										
											2014-01-17 06:30:47 +07:00
										 |  |  | 	if (!internalEvent->name) { | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | 		setPixmap(EVENT_PIXMAP(":warning")); | 
					
						
							| 
									
										
										
										
											2014-04-03 21:16:15 +02:00
										 |  |  | 	} else if (internalEvent->type == SAMPLE_EVENT_BOOKMARK) { | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | 		setPixmap(EVENT_PIXMAP(":flag")); | 
					
						
							| 
									
										
										
										
											2014-02-21 22:29:35 +01:00
										 |  |  | 	} else if (strcmp(internalEvent->name, "heading") == 0) { | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | 		setPixmap(EVENT_PIXMAP(":flag")); | 
					
						
							| 
									
										
										
										
											2014-06-28 21:07:39 -07:00
										 |  |  | 	} else if (internalEvent->type == SAMPLE_EVENT_GASCHANGE || internalEvent->type == SAMPLE_EVENT_GASCHANGE2) { | 
					
						
							| 
									
										
										
										
											2014-07-10 10:54:05 -07:00
										 |  |  | 		if (internalEvent->value >> 16) | 
					
						
							|  |  |  | 			setPixmap(EVENT_PIXMAP_BIGGER(":gaschangeTrimix")); | 
					
						
							|  |  |  | 		else if (internalEvent->value == 0) | 
					
						
							|  |  |  | 			setPixmap(EVENT_PIXMAP_BIGGER(":gaschangeAir")); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			setPixmap(EVENT_PIXMAP_BIGGER(":gaschangeNitrox")); | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		setPixmap(EVENT_PIXMAP(":warning")); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #undef EVENT_PIXMAP
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DiveEventItem::setupToolTipString() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-02-25 12:27:12 -08:00
										 |  |  | 	// we display the event on screen - so translate
 | 
					
						
							| 
									
										
										
										
											2014-04-29 22:37:19 -07:00
										 |  |  | 	QString name = gettextFromC::instance()->tr(internalEvent->name); | 
					
						
							| 
									
										
										
										
											2014-02-25 12:27:12 -08:00
										 |  |  | 	int value = internalEvent->value; | 
					
						
							| 
									
										
										
										
											2014-04-29 22:37:19 -07:00
										 |  |  | 	int type = internalEvent->type; | 
					
						
							| 
									
										
										
										
											2014-02-25 12:27:12 -08:00
										 |  |  | 	if (value) { | 
					
						
							| 
									
										
										
										
											2014-04-29 22:37:19 -07:00
										 |  |  | 		if (type == SAMPLE_EVENT_GASCHANGE || type == SAMPLE_EVENT_GASCHANGE2) { | 
					
						
							| 
									
										
										
										
											2014-07-17 00:26:46 +02:00
										 |  |  | 			QModelIndexList result = dataModel->match(dataModel->index(0, DivePlotDataModel::TIME), Qt::DisplayRole, internalEvent->time.seconds); | 
					
						
							|  |  |  | 			if (result.isEmpty()) { | 
					
						
							|  |  |  | 				Q_ASSERT("can't find a spot in the dataModel"); | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			// We need to look at row + 1, where the new gas is active to find what we are switching to.
 | 
					
						
							|  |  |  | 			int cylinder_idx = dataModel->data(dataModel->index(result.first().row() + 1, DivePlotDataModel::CYLINDERINDEX)).toInt(); | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | 			name += ": "; | 
					
						
							| 
									
										
										
										
											2014-07-17 19:58:55 +02:00
										 |  |  | 			name += gasname(&displayed_dive.cylinder[cylinder_idx].gasmix); | 
					
						
							| 
									
										
										
										
											2014-05-05 09:28:58 -07:00
										 |  |  | 		} else if (type == SAMPLE_EVENT_PO2 && name == "SP change") { | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 			name += QString(":%1").arg((double)value / 1000); | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2014-02-25 12:27:12 -08:00
										 |  |  | 			name += QString(":%1").arg(value); | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-05-05 09:28:58 -07:00
										 |  |  | 	} else if (type == SAMPLE_EVENT_PO2 && name == "SP change") { | 
					
						
							|  |  |  | 		// this is a bad idea - we are abusing an existing event type that is supposed to
 | 
					
						
							| 
									
										
										
										
											2014-06-22 16:41:44 +02:00
										 |  |  | 		// warn of high or low pO₂ and are turning it into a set point change event
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | 		name += "\n" + tr("Bailing out to OC"); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2014-02-25 12:27:12 -08:00
										 |  |  | 		name += internalEvent->flags == SAMPLE_FLAGS_BEGIN ? tr(" begin", "Starts with space!") : | 
					
						
							|  |  |  | 								     internalEvent->flags == SAMPLE_FLAGS_END ? tr(" end", "Starts with space!") : ""; | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-25 12:27:12 -08:00
										 |  |  | 	// qDebug() << name;
 | 
					
						
							|  |  |  | 	setToolTip(name); | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | void DiveEventItem::eventVisibilityChanged(const QString &eventName, bool visible) | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-16 15:10:03 -07:00
										 |  |  | bool DiveEventItem::shouldBeHidden() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-07-01 12:07:38 -07:00
										 |  |  | 	struct event *event = internalEvent; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Gas change events - particularly at the beginning of a dive - are | 
					
						
							|  |  |  | 	 * special. It's just the dive computer specifying the initial gas. | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * Don't bother showing them if they match the first gas already | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2014-07-17 08:55:03 +02:00
										 |  |  | 	if (!strcmp(event->name, "gaschange") && event->time.seconds <= 30) { | 
					
						
							| 
									
										
										
										
											2014-07-17 19:58:55 +02:00
										 |  |  | 		struct dive *dive = &displayed_dive; | 
					
						
							| 
									
										
										
										
											2014-07-17 08:55:02 +02:00
										 |  |  | 		if (dive && get_cylinder_index(dive, event) == 0) | 
					
						
							| 
									
										
										
										
											2014-07-01 12:07:38 -07:00
										 |  |  | 			return true; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-16 15:10:03 -07:00
										 |  |  | 	for (int i = 0; i < evn_used; i++) { | 
					
						
							| 
									
										
										
										
											2014-07-01 12:07:38 -07:00
										 |  |  | 		if (!strcmp(event->name, ev_namelist[i].ev_name) && ev_namelist[i].plot_ev == false) | 
					
						
							| 
									
										
										
										
											2014-03-16 15:10:03 -07:00
										 |  |  | 			return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-15 21:05:47 -02:00
										 |  |  | void DiveEventItem::recalculatePos(bool instant) | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-02-23 08:35:06 -08:00
										 |  |  | 	if (!vAxis || !hAxis || !internalEvent || !dataModel) | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2014-02-23 08:35:06 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 	QModelIndexList result = dataModel->match(dataModel->index(0, DivePlotDataModel::TIME), Qt::DisplayRole, internalEvent->time.seconds); | 
					
						
							| 
									
										
										
										
											2014-01-17 06:30:47 +07:00
										 |  |  | 	if (result.isEmpty()) { | 
					
						
							| 
									
										
										
										
											2014-02-23 08:35:06 -08:00
										 |  |  | 		Q_ASSERT("can't find a spot in the dataModel"); | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | 		hide(); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-16 15:10:03 -07:00
										 |  |  | 	if (!isVisible() && !shouldBeHidden()) | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | 		show(); | 
					
						
							|  |  |  | 	int depth = dataModel->data(dataModel->index(result.first().row(), DivePlotDataModel::DEPTH)).toInt(); | 
					
						
							|  |  |  | 	qreal x = hAxis->posAtValue(internalEvent->time.seconds); | 
					
						
							|  |  |  | 	qreal y = vAxis->posAtValue(depth); | 
					
						
							| 
									
										
										
										
											2014-02-23 08:35:06 -08:00
										 |  |  | 	if (!instant) | 
					
						
							| 
									
										
										
										
											2014-03-11 17:27:05 -03:00
										 |  |  | 		Animations::moveTo(this, x, y); | 
					
						
							| 
									
										
										
										
											2014-02-23 08:35:06 -08:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 		setPos(x, y); | 
					
						
							| 
									
										
										
										
											2014-03-16 15:10:03 -07:00
										 |  |  | 	if (isVisible() && shouldBeHidden()) | 
					
						
							| 
									
										
										
										
											2014-03-15 21:12:34 -07:00
										 |  |  | 		hide(); | 
					
						
							| 
									
										
										
										
											2014-01-16 15:02:32 -02:00
										 |  |  | } |