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/tankitem.h"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "qt-models/diveplotdatamodel.h"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "profile-widget/divetextitem.h"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#include "core/profile.h"
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-18 11:47:18 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#include <QPen>
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 18:22:27 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-27 18:32:04 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								static const qreal height = 3.0;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 18:22:27 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								TankItem::TankItem(QObject *parent) :
							 | 
						
					
						
							
								
									
										
										
										
											2016-03-08 02:23:54 -03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									QObject(parent),
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-27 18:08:07 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									plotEndTime(-1)
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 18:22:27 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 21:40:47 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									QColor red(PERSIANRED1);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									QColor blue(AIR_BLUE);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									QColor yellow(NITROX_YELLOW);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									QColor green(NITROX_GREEN);
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-15 07:30:31 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									QLinearGradient nitroxGradient(QPointF(0, 0), QPointF(0, height));
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 21:40:47 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									nitroxGradient.setColorAt(0.0, green);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									nitroxGradient.setColorAt(0.49, green);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									nitroxGradient.setColorAt(0.5, yellow);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									nitroxGradient.setColorAt(1.0, yellow);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									nitrox = nitroxGradient;
							 | 
						
					
						
							
								
									
										
										
										
											2015-01-05 20:24:34 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									oxygen = green;
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-15 07:30:31 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									QLinearGradient trimixGradient(QPointF(0, 0), QPointF(0, height));
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 21:40:47 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									trimixGradient.setColorAt(0.0, green);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									trimixGradient.setColorAt(0.49, green);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									trimixGradient.setColorAt(0.5, red);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									trimixGradient.setColorAt(1.0, red);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									trimix = trimixGradient;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									air = blue;
							 | 
						
					
						
							
								
									
										
										
										
											2019-04-01 23:11:19 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									hAxis = nullptr;
							 | 
						
					
						
							
								
									
										
										
										
											2014-12-18 08:47:53 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 18:22:27 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								void TankItem::setData(DivePlotDataModel *model, struct plot_info *plotInfo, struct dive *d)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-27 18:08:07 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									// If there is nothing to plot, quit early.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if (plotInfo->nr <= 0) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										plotEndTime = -1;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										return;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									// Find correct end of the dive plot for correct end of the tankbar.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									struct plot_data *last_entry = &plotInfo->entry[plotInfo->nr - 1];
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									plotEndTime = last_entry->sec;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									// Stay informed of changes to the tanks.
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-27 15:22:34 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									connect(model, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(modelDataChanged(QModelIndex, QModelIndex)), Qt::UniqueConnection);
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-27 18:08:07 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 18:22:27 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									modelDataChanged();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-27 17:48:04 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								void TankItem::createBar(int startTime, int stopTime, struct gasmix gas)
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 21:40:47 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-27 17:48:04 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									qreal x = hAxis->posAtValue(startTime);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									qreal w = hAxis->posAtValue(stopTime) - hAxis->posAtValue(startTime);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 21:40:47 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									// pick the right gradient, size, position and text
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-15 07:30:31 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									QGraphicsRectItem *rect = new QGraphicsRectItem(x, 0, w, height, this);
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 21:40:47 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									if (gasmix_is_air(gas))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										rect->setBrush(air);
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-16 19:10:10 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									else if (gas.he.permille)
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 21:40:47 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										rect->setBrush(trimix);
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-16 19:10:10 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									else if (gas.o2.permille == 1000)
							 | 
						
					
						
							
								
									
										
										
										
											2015-01-05 20:24:34 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										rect->setBrush(oxygen);
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 21:40:47 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									else
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										rect->setBrush(nitrox);
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-18 11:47:18 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									rect->setPen(QPen(QBrush(), 0.0)); // get rid of the thick line around the rectangle
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 21:40:47 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									rects.push_back(rect);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									DiveTextItem *label = new DiveTextItem(rect);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									label->setText(gasname(gas));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									label->setBrush(Qt::black);
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-15 07:30:31 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									label->setPos(x + 1, 0);
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 21:40:47 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									label->setAlignment(Qt::AlignBottom | Qt::AlignRight);
							 | 
						
					
						
							
								
									
										
										
										
											2017-02-04 10:46:02 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								#ifdef SUBSURFACE_MOBILE
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									label->setPos(x + 1, -2.5);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								#endif
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 21:40:47 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									label->setZValue(101);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-05-21 18:20:29 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								void TankItem::modelDataChanged(const QModelIndex&, const QModelIndex&)
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 18:22:27 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									// We don't have enougth data to calculate things, quit.
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-27 18:08:07 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									if (plotEndTime < 0)
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 18:22:27 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
										return;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									// remove the old rectangles
							 | 
						
					
						
							
								
									
										
										
										
											2019-04-03 20:25:35 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									qDeleteAll(rects);
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 18:22:27 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									rects.clear();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-08-04 19:39:47 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									// Bail if there are no cylinders
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if (displayed_dive.cylinders.nr <= 0)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										return;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-14 05:29:20 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									// get the information directly from the displayed_dive (the dc always exists)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									struct divecomputer *dc = get_dive_dc(&displayed_dive, dc_number);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									// start with the first gasmix and at the start of the dive
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									int cyl = explicit_first_cylinder(&displayed_dive, dc);
							 | 
						
					
						
							
								
									
										
										
										
											2019-08-04 22:13:49 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									struct gasmix gasmix = get_cylinder(&displayed_dive, cyl)->gasmix;
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-14 05:29:20 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									int startTime = 0;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									// work through all the gas changes and add the rectangle for each gas while it was used
							 | 
						
					
						
							
								
									
										
										
										
											2018-08-17 00:58:30 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									const struct event *ev = get_next_event(dc->events, "gaschange");
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-27 18:08:07 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									while (ev && (int)ev->time.seconds < plotEndTime) {
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-27 17:48:04 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										createBar(startTime, ev->time.seconds, gasmix);
							 | 
						
					
						
							
								
									
										
										
										
											2017-09-14 05:29:20 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										startTime = ev->time.seconds;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										gasmix = get_gasmix_from_event(&displayed_dive, ev);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
										ev = get_next_event(ev->next, "gaschange");
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 18:22:27 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-27 18:08:07 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									createBar(startTime, plotEndTime, gasmix);
							 | 
						
					
						
							
								
									
										
										
										
											2014-08-14 18:22:27 -06:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								void TankItem::setHorizontalAxis(DiveCartesianAxis *horizontal)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								{
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									hAxis = horizontal;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									connect(hAxis, SIGNAL(sizeChanged()), this, SLOT(modelDataChanged()));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									modelDataChanged();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 |