| 
									
										
										
										
											2013-06-20 12:33:26 -03:00
										 |  |  | #include "diveplanner.h"
 | 
					
						
							| 
									
										
										
										
											2013-07-02 10:13:06 -03:00
										 |  |  | #include "graphicsview-common.h"
 | 
					
						
							| 
									
										
										
										
											2013-08-28 07:48:46 -03:00
										 |  |  | #include "models.h"
 | 
					
						
							| 
									
										
										
										
											2013-08-30 07:14:30 -03:00
										 |  |  | #include "modeldelegates.h"
 | 
					
						
							| 
									
										
										
										
											2013-08-30 16:43:10 -03:00
										 |  |  | #include "mainwindow.h"
 | 
					
						
							| 
									
										
										
										
											2013-09-18 23:02:53 -05:00
										 |  |  | #include "maintab.h"
 | 
					
						
							| 
									
										
										
										
											2013-09-02 16:21:08 -03:00
										 |  |  | #include "tableview.h"
 | 
					
						
							| 
									
										
										
										
											2013-09-16 19:21:13 -03:00
										 |  |  | #include "graphicsview-common.h"
 | 
					
						
							| 
									
										
										
										
											2013-07-02 10:13:06 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-23 18:04:35 -07:00
										 |  |  | #include "../dive.h"
 | 
					
						
							| 
									
										
										
										
											2013-07-05 23:42:35 +02:00
										 |  |  | #include "../divelist.h"
 | 
					
						
							| 
									
										
										
										
											2013-08-30 15:06:26 -03:00
										 |  |  | #include "../planner.h"
 | 
					
						
							| 
									
										
										
										
											2013-09-20 16:41:42 -07:00
										 |  |  | #include "helpers.h"
 | 
					
						
							| 
									
										
										
										
											2013-08-30 07:14:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-20 12:37:41 -03:00
										 |  |  | #include <QMouseEvent>
 | 
					
						
							| 
									
										
										
										
											2013-06-20 13:39:41 -03:00
										 |  |  | #include <QDebug>
 | 
					
						
							| 
									
										
										
										
											2013-07-02 14:36:52 -03:00
										 |  |  | #include <QGraphicsSceneMouseEvent>
 | 
					
						
							| 
									
										
										
										
											2013-07-04 12:58:11 -03:00
										 |  |  | #include <QMessageBox>
 | 
					
						
							| 
									
										
										
										
											2013-07-21 12:56:21 -03:00
										 |  |  | #include <QStringListModel>
 | 
					
						
							| 
									
										
										
										
											2013-07-21 13:54:21 -03:00
										 |  |  | #include <QListView>
 | 
					
						
							|  |  |  | #include <QModelIndex>
 | 
					
						
							| 
									
										
										
										
											2013-08-28 07:48:46 -03:00
										 |  |  | #include <QSettings>
 | 
					
						
							| 
									
										
										
										
											2013-09-02 16:21:08 -03:00
										 |  |  | #include <QTableView>
 | 
					
						
							| 
									
										
										
										
											2013-09-16 19:21:13 -03:00
										 |  |  | #include <QColor>
 | 
					
						
							| 
									
										
										
										
											2013-06-20 12:33:26 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-03 13:34:50 -07:00
										 |  |  | #include <algorithm>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-27 22:10:03 +08:00
										 |  |  | #define TIME_INITIAL_MAX 30
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | #define MAX_DEPTH M_OR_FT(150, 450)
 | 
					
						
							|  |  |  | #define MIN_DEPTH M_OR_FT(20, 60)
 | 
					
						
							| 
									
										
										
										
											2013-07-02 14:14:09 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-26 23:11:30 +01:00
										 |  |  | #define M_OR_FT(_m,_f) ((prefs.units.length == units::METERS) ? ((_m) * 1000) : (feet_to_mm(_f)))
 | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-11 17:09:19 +09:00
										 |  |  | QString gasToStr(const int o2Permille, const int hePermille) { | 
					
						
							|  |  |  | 	uint o2 = (o2Permille + 5) / 10, he = (hePermille + 5) / 10; | 
					
						
							|  |  |  | 	QString result = is_air(o2Permille, hePermille) ? QObject::tr("AIR") | 
					
						
							|  |  |  | 							: he == 0 ? QString("EAN%1").arg(o2, 2, 10, QChar('0')) | 
					
						
							|  |  |  | 								  : QString("%1/%2").arg(o2).arg(he); | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QString dpGasToStr(const divedatapoint& p) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return gasToStr(p.o2, p.he); | 
					
						
							| 
									
										
										
										
											2013-08-30 15:06:26 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-16 19:21:13 -03:00
										 |  |  | QColor getColor(const color_indice_t i) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return profile_color[i].at(0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-26 16:34:06 -03:00
										 |  |  | static DivePlannerPointsModel *plannerModel = DivePlannerPointsModel::instance(); | 
					
						
							| 
									
										
										
										
											2013-07-04 10:06:52 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-04 10:29:28 -03:00
										 |  |  | DivePlannerGraphics::DivePlannerGraphics(QWidget* parent): QGraphicsView(parent), activeDraggedHandler(0) | 
					
						
							| 
									
										
										
										
											2013-06-20 12:33:26 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-07-02 10:13:06 -03:00
										 |  |  | 	fill_profile_color(); | 
					
						
							|  |  |  | 	setBackgroundBrush(profile_color[BACKGROUND].at(0)); | 
					
						
							| 
									
										
										
										
											2013-06-20 13:53:12 -03:00
										 |  |  | 	setMouseTracking(true); | 
					
						
							| 
									
										
										
										
											2013-06-23 13:09:29 -07:00
										 |  |  | 	setScene(new QGraphicsScene()); | 
					
						
							| 
									
										
										
										
											2013-07-02 10:53:08 -03:00
										 |  |  | 	scene()->setSceneRect(0,0,1920,1080); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	verticalLine = new QGraphicsLineItem( | 
					
						
							|  |  |  | 		fromPercent(0, Qt::Horizontal), | 
					
						
							|  |  |  | 		fromPercent(0, Qt::Vertical), | 
					
						
							|  |  |  | 		fromPercent(0, Qt::Horizontal), | 
					
						
							| 
									
										
										
										
											2013-07-02 11:12:15 -03:00
										 |  |  | 		fromPercent(100, Qt::Vertical) | 
					
						
							|  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2013-06-20 13:53:12 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	verticalLine->setPen(QPen(Qt::DotLine)); | 
					
						
							|  |  |  | 	scene()->addItem(verticalLine); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 11:12:15 -03:00
										 |  |  | 	horizontalLine = new QGraphicsLineItem( | 
					
						
							|  |  |  | 		fromPercent(0, Qt::Horizontal), | 
					
						
							|  |  |  | 		fromPercent(0, Qt::Vertical), | 
					
						
							|  |  |  | 		fromPercent(100, Qt::Horizontal), | 
					
						
							|  |  |  | 		fromPercent(0, Qt::Vertical) | 
					
						
							|  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2013-09-16 19:21:13 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-20 13:53:12 -03:00
										 |  |  | 	horizontalLine->setPen(QPen(Qt::DotLine)); | 
					
						
							|  |  |  | 	scene()->addItem(horizontalLine); | 
					
						
							| 
									
										
										
										
											2013-06-20 16:48:24 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	timeLine = new Ruler(); | 
					
						
							|  |  |  | 	timeLine->setMinimum(0); | 
					
						
							| 
									
										
										
										
											2013-06-27 22:10:03 +08:00
										 |  |  | 	timeLine->setMaximum(TIME_INITIAL_MAX); | 
					
						
							| 
									
										
										
										
											2013-06-20 16:48:24 -03:00
										 |  |  | 	timeLine->setTickInterval(10); | 
					
						
							| 
									
										
										
										
											2013-09-16 19:21:13 -03:00
										 |  |  | 	timeLine->setColor(getColor(TIME_GRID)); | 
					
						
							| 
									
										
										
										
											2013-07-02 11:12:15 -03:00
										 |  |  | 	timeLine->setLine( | 
					
						
							|  |  |  | 		fromPercent(10, Qt::Horizontal), | 
					
						
							| 
									
										
										
										
											2013-11-17 21:20:07 -02:00
										 |  |  | 		fromPercent(85, Qt::Vertical), | 
					
						
							| 
									
										
										
										
											2013-07-02 11:12:15 -03:00
										 |  |  | 		fromPercent(90, Qt::Horizontal), | 
					
						
							| 
									
										
										
										
											2013-11-17 21:20:07 -02:00
										 |  |  | 		fromPercent(85, Qt::Vertical) | 
					
						
							| 
									
										
										
										
											2013-07-02 11:12:15 -03:00
										 |  |  | 	); | 
					
						
							| 
									
										
										
										
											2013-06-20 16:48:24 -03:00
										 |  |  | 	timeLine->setOrientation(Qt::Horizontal); | 
					
						
							| 
									
										
										
										
											2013-07-02 11:12:15 -03:00
										 |  |  | 	timeLine->setTickSize(fromPercent(1, Qt::Vertical)); | 
					
						
							| 
									
										
										
										
											2013-09-16 20:10:12 -03:00
										 |  |  | 	timeLine->setTextColor(getColor(TIME_TEXT)); | 
					
						
							| 
									
										
										
										
											2013-06-20 16:48:24 -03:00
										 |  |  | 	timeLine->updateTicks(); | 
					
						
							|  |  |  | 	scene()->addItem(timeLine); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	depthLine = new Ruler(); | 
					
						
							|  |  |  | 	depthLine->setMinimum(0); | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | 	depthLine->setMaximum(M_OR_FT(40,120)); | 
					
						
							|  |  |  | 	depthLine->setTickInterval(M_OR_FT(10,30)); | 
					
						
							| 
									
										
										
										
											2013-07-02 11:12:15 -03:00
										 |  |  | 	depthLine->setLine( | 
					
						
							|  |  |  | 		fromPercent(10, Qt::Horizontal), | 
					
						
							|  |  |  | 		fromPercent(10, Qt::Vertical), | 
					
						
							|  |  |  | 		fromPercent(10, Qt::Horizontal), | 
					
						
							| 
									
										
										
										
											2013-11-17 21:20:07 -02:00
										 |  |  | 		fromPercent(85, Qt::Vertical) | 
					
						
							| 
									
										
										
										
											2013-07-02 11:12:15 -03:00
										 |  |  | 	); | 
					
						
							| 
									
										
										
										
											2013-06-20 16:48:24 -03:00
										 |  |  | 	depthLine->setOrientation(Qt::Vertical); | 
					
						
							| 
									
										
										
										
											2013-07-02 11:12:15 -03:00
										 |  |  | 	depthLine->setTickSize(fromPercent(1, Qt::Horizontal)); | 
					
						
							| 
									
										
										
										
											2013-09-16 19:21:13 -03:00
										 |  |  | 	depthLine->setColor(getColor(DEPTH_GRID)); | 
					
						
							| 
									
										
										
										
											2013-09-16 20:10:12 -03:00
										 |  |  | 	depthLine->setTextColor(getColor(SAMPLE_DEEP)); | 
					
						
							| 
									
										
										
										
											2013-06-20 16:48:24 -03:00
										 |  |  | 	depthLine->updateTicks(); | 
					
						
							| 
									
										
										
										
											2013-11-26 18:07:08 -02:00
										 |  |  | 	depthLine->unitSystem = prefs.units.length; | 
					
						
							| 
									
										
										
										
											2013-06-20 16:48:24 -03:00
										 |  |  | 	scene()->addItem(depthLine); | 
					
						
							| 
									
										
										
										
											2013-06-20 17:54:36 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	timeString = new QGraphicsSimpleTextItem(); | 
					
						
							|  |  |  | 	timeString->setFlag(QGraphicsItem::ItemIgnoresTransformations); | 
					
						
							| 
									
										
										
										
											2013-07-02 13:31:25 -03:00
										 |  |  | 	timeString->setBrush(profile_color[TIME_TEXT].at(0)); | 
					
						
							| 
									
										
										
										
											2013-06-20 17:54:36 -03:00
										 |  |  | 	scene()->addItem(timeString); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	depthString = new QGraphicsSimpleTextItem(); | 
					
						
							|  |  |  | 	depthString->setFlag(QGraphicsItem::ItemIgnoresTransformations); | 
					
						
							| 
									
										
										
										
											2013-07-02 13:31:25 -03:00
										 |  |  | 	depthString->setBrush(profile_color[SAMPLE_DEEP].at(0)); | 
					
						
							| 
									
										
										
										
											2013-06-20 17:54:36 -03:00
										 |  |  | 	scene()->addItem(depthString); | 
					
						
							| 
									
										
										
										
											2013-06-21 16:44:38 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-27 19:52:58 -03:00
										 |  |  | 	diveBg = new QGraphicsPolygonItem(); | 
					
						
							| 
									
										
										
										
											2013-07-02 13:39:54 -03:00
										 |  |  | 	diveBg->setPen(QPen(QBrush(),0)); | 
					
						
							| 
									
										
										
										
											2013-06-27 19:52:58 -03:00
										 |  |  | 	scene()->addItem(diveBg); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | 	QString incrText; | 
					
						
							|  |  |  | 	if (prefs.units.length == units::METERS) | 
					
						
							|  |  |  | 		incrText = tr("10m"); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		incrText = tr("30ft"); | 
					
						
							| 
									
										
										
										
											2013-07-04 11:06:28 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-17 21:20:07 -02:00
										 |  |  | 	timeHandler = new ExpanderGraphics(); | 
					
						
							|  |  |  | 	timeHandler->increaseBtn->setPixmap(QString(":plan_plus")); | 
					
						
							|  |  |  | 	timeHandler->decreaseBtn->setPixmap(QString(":plan_minus")); | 
					
						
							|  |  |  | 	timeHandler->icon->setPixmap(QString(":icon_time")); | 
					
						
							|  |  |  | 	connect(timeHandler->increaseBtn, SIGNAL(clicked()), this, SLOT(increaseTime())); | 
					
						
							|  |  |  | 	connect(timeHandler->decreaseBtn, SIGNAL(clicked()), this, SLOT(decreaseTime())); | 
					
						
							| 
									
										
										
										
											2013-11-18 21:13:05 -02:00
										 |  |  | 	timeHandler->setPos(fromPercent(83, Qt::Horizontal), fromPercent(100, Qt::Vertical)); | 
					
						
							| 
									
										
										
										
											2013-11-17 21:20:07 -02:00
										 |  |  | 	timeHandler->setZValue(-2); | 
					
						
							|  |  |  | 	scene()->addItem(timeHandler); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	depthHandler = new ExpanderGraphics(); | 
					
						
							|  |  |  | 	depthHandler->increaseBtn->setPixmap(QString(":arrow_up")); | 
					
						
							|  |  |  | 	depthHandler->decreaseBtn->setPixmap(QString(":arrow_down")); | 
					
						
							|  |  |  | 	depthHandler->icon->setPixmap(QString(":icon_depth")); | 
					
						
							| 
									
										
										
										
											2013-11-18 22:34:59 -02:00
										 |  |  | 	// Inverted here in the slots because the 'up' graphi should increase the depness,
 | 
					
						
							|  |  |  | 	// and the down should decrease.
 | 
					
						
							|  |  |  | 	connect(depthHandler->increaseBtn, SIGNAL(clicked()), this, SLOT(decreaseDepth())); | 
					
						
							|  |  |  | 	connect(depthHandler->decreaseBtn, SIGNAL(clicked()), this, SLOT(increaseDepth())); | 
					
						
							| 
									
										
										
										
											2013-11-18 21:13:05 -02:00
										 |  |  | 	depthHandler->setPos(fromPercent(0, Qt::Horizontal), fromPercent(100, Qt::Vertical)); | 
					
						
							| 
									
										
										
										
											2013-11-17 21:20:07 -02:00
										 |  |  | 	depthHandler->setZValue(-2); | 
					
						
							|  |  |  | 	scene()->addItem(depthHandler); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	minMinutes = TIME_INITIAL_MAX; | 
					
						
							| 
									
										
										
										
											2013-12-08 14:52:34 +01:00
										 |  |  | 	minDepth = M_OR_FT(40,120); | 
					
						
							| 
									
										
										
										
											2013-07-04 11:16:42 -03:00
										 |  |  | 	QAction *action = NULL; | 
					
						
							| 
									
										
										
										
											2013-07-04 11:06:28 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-04 11:16:42 -03:00
										 |  |  | #define ADD_ACTION( SHORTCUT, Slot ) \
 | 
					
						
							|  |  |  | 	action = new QAction(this); \ | 
					
						
							|  |  |  | 	action->setShortcut( SHORTCUT ); \ | 
					
						
							| 
									
										
										
										
											2013-07-04 12:30:05 -03:00
										 |  |  | 	action->setShortcutContext(Qt::WindowShortcut); \ | 
					
						
							| 
									
										
										
										
											2013-07-04 11:16:42 -03:00
										 |  |  | 	addAction(action); \ | 
					
						
							|  |  |  | 	connect(action, SIGNAL(triggered(bool)), this, SLOT( Slot )) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ADD_ACTION(Qt::Key_Escape, keyEscAction()); | 
					
						
							|  |  |  | 	ADD_ACTION(Qt::Key_Delete, keyDeleteAction()); | 
					
						
							| 
									
										
										
										
											2013-07-04 11:28:39 -03:00
										 |  |  | 	ADD_ACTION(Qt::Key_Up, keyUpAction()); | 
					
						
							|  |  |  | 	ADD_ACTION(Qt::Key_Down, keyDownAction()); | 
					
						
							| 
									
										
										
										
											2013-07-04 12:30:05 -03:00
										 |  |  | 	ADD_ACTION(Qt::Key_Left, keyLeftAction()); | 
					
						
							|  |  |  | 	ADD_ACTION(Qt::Key_Right, keyRightAction()); | 
					
						
							| 
									
										
										
										
											2013-07-04 11:16:42 -03:00
										 |  |  | #undef ADD_ACTION
 | 
					
						
							| 
									
										
										
										
											2013-07-04 11:06:28 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-18 22:19:49 -05:00
										 |  |  | 	connect(plannerModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(drawProfile())); | 
					
						
							| 
									
										
										
										
											2013-08-30 15:53:10 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	connect(plannerModel, SIGNAL(rowsInserted(const QModelIndex&,int,int)), | 
					
						
							| 
									
										
										
										
											2013-08-26 14:17:39 -03:00
										 |  |  | 			this, SLOT(pointInserted(const QModelIndex&, int, int))); | 
					
						
							| 
									
										
										
										
											2013-08-30 15:53:10 -03:00
										 |  |  | 	connect(plannerModel, SIGNAL(rowsRemoved(const QModelIndex&, int, int)), | 
					
						
							|  |  |  | 			this, SLOT(pointsRemoved(const QModelIndex&, int, int))); | 
					
						
							| 
									
										
										
										
											2013-06-27 19:52:58 -03:00
										 |  |  | 	setRenderHint(QPainter::Antialiasing); | 
					
						
							| 
									
										
										
										
											2013-06-27 16:45:58 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-26 18:07:08 -02:00
										 |  |  | void DivePlannerGraphics::settingsChanged() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (depthLine->unitSystem == prefs.units.length) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	depthLine->setTickInterval(M_OR_FT(10,30)); | 
					
						
							|  |  |  | 	depthLine->updateTicks(); | 
					
						
							|  |  |  | 	depthLine->unitSystem = prefs.units.length; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-26 14:17:39 -03:00
										 |  |  | void DivePlannerGraphics::pointInserted(const QModelIndex& parent, int start , int end) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	DiveHandler *item = new DiveHandler (); | 
					
						
							|  |  |  | 	scene()->addItem(item); | 
					
						
							|  |  |  | 	handles << item; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 20:06:07 -02:00
										 |  |  | 	QGraphicsSimpleTextItem *gasChooseBtn = new QGraphicsSimpleTextItem(); | 
					
						
							| 
									
										
										
										
											2013-08-26 14:17:39 -03:00
										 |  |  | 	scene()->addItem(gasChooseBtn); | 
					
						
							|  |  |  | 	gasChooseBtn->setZValue(10); | 
					
						
							| 
									
										
										
										
											2013-11-19 20:06:07 -02:00
										 |  |  | 	gasChooseBtn->setFlag(QGraphicsItem::ItemIgnoresTransformations); | 
					
						
							| 
									
										
										
										
											2013-08-26 14:17:39 -03:00
										 |  |  | 	gases << gasChooseBtn; | 
					
						
							| 
									
										
										
										
											2013-09-18 22:19:49 -05:00
										 |  |  | 	drawProfile(); | 
					
						
							| 
									
										
										
										
											2013-08-26 14:17:39 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-04 11:28:39 -03:00
										 |  |  | void DivePlannerGraphics::keyDownAction() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-11 10:21:04 -03:00
										 |  |  | 	Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()) { | 
					
						
							|  |  |  | 		if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)) { | 
					
						
							|  |  |  | 			int row = handles.indexOf(handler); | 
					
						
							|  |  |  | 			divedatapoint dp = plannerModel->at(row); | 
					
						
							|  |  |  | 			if (dp.depth >= depthLine->maximum()) | 
					
						
							|  |  |  | 				continue; | 
					
						
							| 
									
										
										
										
											2013-07-04 11:28:39 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-11 10:21:04 -03:00
										 |  |  | 			dp.depth += M_OR_FT(1,5); | 
					
						
							|  |  |  | 			plannerModel->editStop(row, dp); | 
					
						
							| 
									
										
										
										
											2013-07-04 11:28:39 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DivePlannerGraphics::keyUpAction() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()) { | 
					
						
							|  |  |  | 		if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)) { | 
					
						
							| 
									
										
										
										
											2013-08-26 16:34:06 -03:00
										 |  |  | 			int row = handles.indexOf(handler); | 
					
						
							|  |  |  | 			divedatapoint dp = plannerModel->at(row); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | 			if (dp.depth <= 0) | 
					
						
							| 
									
										
										
										
											2013-07-04 11:28:39 -03:00
										 |  |  | 				continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | 			dp.depth -= M_OR_FT(1,5); | 
					
						
							| 
									
										
										
										
											2013-08-26 16:34:06 -03:00
										 |  |  | 			plannerModel->editStop(row, dp); | 
					
						
							| 
									
										
										
										
											2013-07-04 11:28:39 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-09-18 22:19:49 -05:00
										 |  |  | 	drawProfile(); | 
					
						
							| 
									
										
										
										
											2013-07-04 11:28:39 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-04 12:30:05 -03:00
										 |  |  | void DivePlannerGraphics::keyLeftAction() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()) { | 
					
						
							|  |  |  | 		if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)) { | 
					
						
							| 
									
										
										
										
											2013-08-26 16:34:06 -03:00
										 |  |  | 			int row = handles.indexOf(handler); | 
					
						
							|  |  |  | 			divedatapoint dp = plannerModel->at(row); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (dp.time / 60 <= 0) | 
					
						
							| 
									
										
										
										
											2013-07-04 12:30:05 -03:00
										 |  |  | 				continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// don't overlap positions.
 | 
					
						
							|  |  |  | 			// maybe this is a good place for a 'goto'?
 | 
					
						
							| 
									
										
										
										
											2013-08-26 16:34:06 -03:00
										 |  |  | 			double xpos = timeLine->posAtValue((dp.time - 60) / 60); | 
					
						
							| 
									
										
										
										
											2013-07-04 12:30:05 -03:00
										 |  |  | 			bool nextStep = false; | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 			Q_FOREACH(DiveHandler *h, handles) { | 
					
						
							|  |  |  | 				if (h->pos().x() == xpos) { | 
					
						
							| 
									
										
										
										
											2013-07-04 12:30:05 -03:00
										 |  |  | 					nextStep = true; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if(nextStep) | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-26 16:34:06 -03:00
										 |  |  | 			dp.time -= 60; | 
					
						
							|  |  |  | 			plannerModel->editStop(row, dp); | 
					
						
							| 
									
										
										
										
											2013-07-04 12:30:05 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DivePlannerGraphics::keyRightAction() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()) { | 
					
						
							|  |  |  | 		if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)) { | 
					
						
							| 
									
										
										
										
											2013-08-26 16:34:06 -03:00
										 |  |  | 			int row = handles.indexOf(handler); | 
					
						
							|  |  |  | 			divedatapoint dp = plannerModel->at(row); | 
					
						
							|  |  |  | 			if (dp.time / 60 >= timeLine->maximum()) | 
					
						
							| 
									
										
										
										
											2013-07-04 12:30:05 -03:00
										 |  |  | 				continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// don't overlap positions.
 | 
					
						
							|  |  |  | 			// maybe this is a good place for a 'goto'?
 | 
					
						
							| 
									
										
										
										
											2013-08-26 16:34:06 -03:00
										 |  |  | 			double xpos = timeLine->posAtValue((dp.time + 60) / 60); | 
					
						
							| 
									
										
										
										
											2013-07-04 12:30:05 -03:00
										 |  |  | 			bool nextStep = false; | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 			Q_FOREACH(DiveHandler *h, handles) { | 
					
						
							|  |  |  | 				if (h->pos().x() == xpos) { | 
					
						
							| 
									
										
										
										
											2013-07-04 12:30:05 -03:00
										 |  |  | 					nextStep = true; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if(nextStep) | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-26 16:34:06 -03:00
										 |  |  | 			dp.time += 60; | 
					
						
							|  |  |  | 			plannerModel->editStop(row, dp); | 
					
						
							| 
									
										
										
										
											2013-07-04 12:30:05 -03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-08-30 16:43:10 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-04 12:30:05 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-07-04 11:28:39 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-04 11:16:42 -03:00
										 |  |  | void DivePlannerGraphics::keyDeleteAction() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-07-21 12:12:31 -03:00
										 |  |  | 	int selCount = scene()->selectedItems().count(); | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	if(selCount) { | 
					
						
							| 
									
										
										
										
											2013-08-30 15:53:10 -03:00
										 |  |  | 		QVector<int> selectedIndexes; | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 		Q_FOREACH(QGraphicsItem *i, scene()->selectedItems()) { | 
					
						
							|  |  |  | 			if (DiveHandler *handler = qgraphicsitem_cast<DiveHandler*>(i)) { | 
					
						
							| 
									
										
										
										
											2013-08-30 15:53:10 -03:00
										 |  |  | 				selectedIndexes.push_back(handles.indexOf(handler)); | 
					
						
							| 
									
										
										
										
											2013-07-04 11:16:42 -03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-08-30 15:53:10 -03:00
										 |  |  | 		plannerModel->removeSelectedPoints(selectedIndexes); | 
					
						
							| 
									
										
										
										
											2013-07-04 11:16:42 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-30 15:53:10 -03:00
										 |  |  | void DivePlannerGraphics::pointsRemoved(const QModelIndex& , int start, int end) | 
					
						
							|  |  |  | {	// start and end are inclusive.
 | 
					
						
							|  |  |  | 	int num = (end - start) + 1; | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	for(int i = num; i != 0; i--) { | 
					
						
							| 
									
										
										
										
											2013-08-30 15:53:10 -03:00
										 |  |  | 		delete handles.back(); | 
					
						
							|  |  |  | 		handles.pop_back(); | 
					
						
							| 
									
										
										
										
											2013-08-30 16:08:55 -03:00
										 |  |  | 		delete gases.back(); | 
					
						
							|  |  |  | 		gases.pop_back(); | 
					
						
							| 
									
										
										
										
											2013-08-30 15:53:10 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	scene()->clearSelection(); | 
					
						
							| 
									
										
										
										
											2013-09-18 22:19:49 -05:00
										 |  |  | 	drawProfile(); | 
					
						
							| 
									
										
										
										
											2013-08-30 15:53:10 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | bool intLessThan(int a, int b) { | 
					
						
							| 
									
										
										
										
											2013-08-30 15:53:10 -03:00
										 |  |  | 	return a <= b; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | void DivePlannerPointsModel::removeSelectedPoints(const QVector< int >& rows) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int firstRow = rowCount() - rows.count(); | 
					
						
							|  |  |  | 	QVector<int> v2 = rows; | 
					
						
							|  |  |  | 	std::sort(v2.begin(), v2.end(), intLessThan); | 
					
						
							|  |  |  | 	beginRemoveRows(QModelIndex(), firstRow, rowCount()-1); | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	for(int i = v2.count()-1; i >= 0; i--) { | 
					
						
							| 
									
										
										
										
											2013-08-30 15:53:10 -03:00
										 |  |  | 		divepoints.remove(v2[i]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	endRemoveRows(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-04 11:06:28 -03:00
										 |  |  | void DivePlannerGraphics::keyEscAction() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	if (scene()->selectedItems().count()) { | 
					
						
							| 
									
										
										
										
											2013-07-04 11:06:28 -03:00
										 |  |  | 		scene()->clearSelection(); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-09-22 08:45:14 -07:00
										 |  |  | 	if (DivePlannerPointsModel::instance()->isPlanner()) | 
					
						
							|  |  |  | 		plannerModel->cancelPlan(); | 
					
						
							| 
									
										
										
										
											2013-07-04 11:06:28 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 10:53:08 -03:00
										 |  |  | qreal DivePlannerGraphics::fromPercent(qreal percent, Qt::Orientation orientation) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	qreal total = orientation == Qt::Horizontal ? sceneRect().width() : sceneRect().height(); | 
					
						
							|  |  |  | 	qreal result = (total * percent) / 100; | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-21 16:51:13 -03:00
										 |  |  | void DivePlannerGraphics::increaseDepth() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | 	if (depthLine->maximum() + M_OR_FT(10,30) > MAX_DEPTH) | 
					
						
							| 
									
										
										
										
											2013-07-02 14:14:09 -03:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2013-12-08 14:52:34 +01:00
										 |  |  | 	minDepth += M_OR_FT(10,30); | 
					
						
							|  |  |  | 	depthLine->setMaximum( minDepth ); | 
					
						
							| 
									
										
										
										
											2013-07-02 14:14:09 -03:00
										 |  |  | 	depthLine->updateTicks(); | 
					
						
							| 
									
										
										
										
											2013-09-18 22:19:49 -05:00
										 |  |  | 	drawProfile(); | 
					
						
							| 
									
										
										
										
											2013-06-21 16:51:13 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DivePlannerGraphics::increaseTime() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-07-02 14:36:52 -03:00
										 |  |  | 	minMinutes += 10; | 
					
						
							|  |  |  | 	timeLine->setMaximum( minMinutes ); | 
					
						
							|  |  |  | 	timeLine->updateTicks(); | 
					
						
							| 
									
										
										
										
											2013-09-18 22:19:49 -05:00
										 |  |  | 	drawProfile(); | 
					
						
							| 
									
										
										
										
											2013-06-20 12:33:26 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-06-20 12:37:41 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-21 09:27:04 -03:00
										 |  |  | void DivePlannerGraphics::decreaseDepth() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | 	if (depthLine->maximum() - M_OR_FT(10,30) < MIN_DEPTH) | 
					
						
							| 
									
										
										
										
											2013-07-21 09:27:04 -03:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | 	Q_FOREACH(DiveHandler *d, handles) { | 
					
						
							|  |  |  | 		if (depthLine->valueAt(d->pos()) > depthLine->maximum() - M_OR_FT(10,30)) { | 
					
						
							| 
									
										
										
										
											2013-07-21 09:27:04 -03:00
										 |  |  | 			QMessageBox::warning(mainWindow(), | 
					
						
							|  |  |  | 				tr("Handler Position Error"), | 
					
						
							|  |  |  | 				tr("One or more of your stops will be lost with this operations, \n" | 
					
						
							|  |  |  | 					"Please, remove them first.")); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-12-08 14:52:34 +01:00
										 |  |  | 	minDepth -= M_OR_FT(10,30); | 
					
						
							|  |  |  | 	depthLine->setMaximum( minDepth ); | 
					
						
							| 
									
										
										
										
											2013-07-21 09:27:04 -03:00
										 |  |  | 	depthLine->updateTicks(); | 
					
						
							| 
									
										
										
										
											2013-09-18 22:19:49 -05:00
										 |  |  | 	drawProfile(); | 
					
						
							| 
									
										
										
										
											2013-07-21 09:27:04 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DivePlannerGraphics::decreaseTime() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-10-11 10:21:04 -03:00
										 |  |  | 	if (timeLine->maximum() - 10 < TIME_INITIAL_MAX || timeLine->maximum() - 10 < dpMaxTime) | 
					
						
							| 
									
										
										
										
											2013-07-21 09:44:52 -03:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2013-10-11 10:21:04 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-21 09:44:52 -03:00
										 |  |  | 	minMinutes -= 10; | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	timeLine->setMaximum(timeLine->maximum() - 10); | 
					
						
							| 
									
										
										
										
											2013-07-21 09:44:52 -03:00
										 |  |  | 	timeLine->updateTicks(); | 
					
						
							| 
									
										
										
										
											2013-09-18 22:19:49 -05:00
										 |  |  | 	drawProfile(); | 
					
						
							| 
									
										
										
										
											2013-07-21 09:27:04 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-20 18:40:59 -03:00
										 |  |  | void DivePlannerGraphics::mouseDoubleClickEvent(QMouseEvent* event) | 
					
						
							| 
									
										
										
										
											2013-06-20 12:37:41 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-06-20 13:56:28 -03:00
										 |  |  | 	QPointF mappedPos = mapToScene(event->pos()); | 
					
						
							| 
									
										
										
										
											2013-06-23 13:09:29 -07:00
										 |  |  | 	if (isPointOutOfBoundaries(mappedPos)) | 
					
						
							| 
									
										
										
										
											2013-06-20 13:56:28 -03:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-23 18:04:35 -07:00
										 |  |  | 	int minutes = rint(timeLine->valueAt(mappedPos)); | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | 	int milimeters = rint(depthLine->valueAt(mappedPos) / M_OR_FT(1,1)) * M_OR_FT(1,1); | 
					
						
							| 
									
										
										
										
											2013-11-12 11:19:04 +09:00
										 |  |  | 	plannerModel->addStop(milimeters, minutes * 60, -1, 0, 0); | 
					
						
							| 
									
										
										
										
											2013-06-20 13:20:41 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-20 07:36:14 -05:00
										 |  |  | void DivePlannerPointsModel::createSimpleDive() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-14 18:45:32 -02:00
										 |  |  | //	plannerModel->addStop(0, 0, O2_IN_AIR, 0, 0);
 | 
					
						
							| 
									
										
										
										
											2013-11-08 18:15:04 +09:00
										 |  |  | 	plannerModel->addStop(M_OR_FT(15,45), 1 * 60, O2_IN_AIR, 0, 0); | 
					
						
							|  |  |  | 	plannerModel->addStop(M_OR_FT(15,45), 40 * 60, O2_IN_AIR, 0, 0); | 
					
						
							|  |  |  | 	plannerModel->addStop(M_OR_FT(5,15), 42 * 60, O2_IN_AIR, 0, 0); | 
					
						
							|  |  |  | 	plannerModel->addStop(M_OR_FT(5,15), 45 * 60, O2_IN_AIR, 0, 0); | 
					
						
							| 
									
										
										
										
											2013-09-20 07:36:14 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-01 11:48:34 -04:00
										 |  |  | void DivePlannerPointsModel::loadFromDive(dive* d) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	/* We need to make a copy, because
 | 
					
						
							|  |  |  | 	 * as soon as the model is modified, it will | 
					
						
							|  |  |  | 	 * remove all samples from the current dive. | 
					
						
							|  |  |  | 	 * */ | 
					
						
							| 
									
										
										
										
											2013-11-18 11:55:56 -08:00
										 |  |  | 	memcpy(&backupDive, current_dive, sizeof(struct dive)); | 
					
						
							|  |  |  | 	copy_samples(current_dive, &backupDive); | 
					
						
							|  |  |  | 	copy_events(current_dive, &backupDive); | 
					
						
							| 
									
										
										
										
											2013-11-14 18:36:21 +09:00
										 |  |  | 	copy_cylinders(current_dive, stagingDive); // this way the correct cylinder data is shown
 | 
					
						
							|  |  |  | 	CylindersModel::instance()->setDive(stagingDive); | 
					
						
							| 
									
										
										
										
											2013-11-13 22:39:05 +09:00
										 |  |  | 	int lasttime = 0; | 
					
						
							| 
									
										
										
										
											2013-11-18 11:55:56 -08:00
										 |  |  | 	// we start with the first gas and see if it was changed
 | 
					
						
							|  |  |  | 	int o2 = backupDive.cylinder[0].gasmix.o2.permille; | 
					
						
							|  |  |  | 	int he = backupDive.cylinder[0].gasmix.he.permille; | 
					
						
							| 
									
										
										
										
											2013-12-03 20:52:28 -08:00
										 |  |  | 	for (int i = 0; i < backupDive.dc.samples - 1; i++) { | 
					
						
							| 
									
										
										
										
											2013-11-18 11:55:56 -08:00
										 |  |  | 		const sample &s = backupDive.dc.sample[i]; | 
					
						
							| 
									
										
										
										
											2013-11-15 12:41:00 -08:00
										 |  |  | 		if (s.time.seconds == 0) | 
					
						
							|  |  |  | 			continue; | 
					
						
							| 
									
										
										
										
											2013-11-18 11:55:56 -08:00
										 |  |  | 		get_gas_from_events(&backupDive.dc, lasttime, &o2, &he); | 
					
						
							| 
									
										
										
										
											2013-11-08 18:15:04 +09:00
										 |  |  | 		plannerModel->addStop(s.depth.mm, s.time.seconds, o2, he, 0); | 
					
						
							| 
									
										
										
										
											2013-11-13 22:39:05 +09:00
										 |  |  | 		lasttime = s.time.seconds; | 
					
						
							| 
									
										
										
										
											2013-11-01 11:48:34 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-18 11:55:56 -08:00
										 |  |  | void DivePlannerPointsModel::restoreBackupDive() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	memcpy(current_dive, &backupDive, sizeof(struct dive)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-14 18:36:21 +09:00
										 |  |  | void DivePlannerPointsModel::copyCylinders(dive *d) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	copy_cylinders(stagingDive, d); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-11 17:09:19 +09:00
										 |  |  | QStringList& DivePlannerPointsModel::getGasList() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-19 18:23:14 -08:00
										 |  |  | 	struct dive *activeDive = isPlanner() ? stagingDive : current_dive; | 
					
						
							| 
									
										
										
										
											2013-11-11 17:09:19 +09:00
										 |  |  | 	static QStringList list; | 
					
						
							|  |  |  | 	list.clear(); | 
					
						
							| 
									
										
										
										
											2013-11-19 18:23:14 -08:00
										 |  |  | 	if (!activeDive) { | 
					
						
							| 
									
										
										
										
											2013-11-11 17:09:19 +09:00
										 |  |  | 		list.push_back(tr("AIR")); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		for (int i = 0; i < MAX_CYLINDERS; i++) { | 
					
						
							| 
									
										
										
										
											2013-11-19 18:23:14 -08:00
										 |  |  | 			cylinder_t *cyl = &activeDive->cylinder[i]; | 
					
						
							| 
									
										
										
										
											2013-11-11 17:09:19 +09:00
										 |  |  | 			if (cylinder_nodata(cyl)) | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			list.push_back(gasToStr(cyl->gasmix.o2.permille, cyl->gasmix.he.permille)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return list; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-18 22:19:49 -05:00
										 |  |  | void DivePlannerGraphics::drawProfile() | 
					
						
							| 
									
										
										
										
											2013-06-20 13:20:41 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-06-26 20:41:39 -03:00
										 |  |  | 	qDeleteAll(lines); | 
					
						
							|  |  |  | 	lines.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-16 12:11:06 -03:00
										 |  |  | 	plannerModel->createTemporaryPlan(); | 
					
						
							| 
									
										
										
										
											2013-09-09 07:18:22 -03:00
										 |  |  | 	struct diveplan diveplan = plannerModel->getDiveplan(); | 
					
						
							| 
									
										
										
										
											2013-09-16 12:11:06 -03:00
										 |  |  | 	struct divedatapoint *dp = diveplan.dp; | 
					
						
							| 
									
										
										
										
											2013-12-07 16:25:38 +01:00
										 |  |  | 	unsigned int max_depth = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-08 18:15:04 +09:00
										 |  |  | 	if (!dp) { | 
					
						
							|  |  |  | 		plannerModel->deleteTemporaryPlan(); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-12-07 16:25:38 +01:00
										 |  |  | 	while(dp->next) { | 
					
						
							|  |  |  | 		if (dp->depth > max_depth) | 
					
						
							|  |  |  | 			max_depth = dp->depth; | 
					
						
							| 
									
										
										
										
											2013-06-23 21:27:52 -07:00
										 |  |  | 		dp = dp->next; | 
					
						
							| 
									
										
										
										
											2013-12-07 16:25:38 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-06-26 20:41:39 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-19 09:13:53 -05:00
										 |  |  | 	if (!activeDraggedHandler && (timeLine->maximum() < dp->time / 60.0 + 5 || dp->time / 60.0 + 15 < timeLine->maximum())) { | 
					
						
							| 
									
										
										
										
											2013-12-11 22:29:28 -02:00
										 |  |  | 		minMinutes = fmax(dp->time / 60.0 + 5, minMinutes); | 
					
						
							|  |  |  | 		timeLine->setMaximum(minMinutes); | 
					
						
							| 
									
										
										
										
											2013-06-26 19:14:55 -03:00
										 |  |  | 		timeLine->updateTicks(); | 
					
						
							| 
									
										
										
										
											2013-06-27 22:10:03 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-12-07 16:25:38 +01:00
										 |  |  | 	if (!activeDraggedHandler && (depthLine->maximum() < max_depth + M_OR_FT(10,30) || max_depth + M_OR_FT(10,30) < depthLine->maximum())) { | 
					
						
							| 
									
										
										
										
											2013-12-11 22:29:28 -02:00
										 |  |  | 		minDepth  = fmax(max_depth + M_OR_FT(10,30), minDepth); | 
					
						
							|  |  |  | 		depthLine->setMaximum(minDepth); | 
					
						
							| 
									
										
										
										
											2013-12-07 16:25:38 +01:00
										 |  |  | 		depthLine->updateTicks(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-06-26 19:14:55 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-20 18:25:03 -03:00
										 |  |  | 	// Re-position the user generated dive handlers
 | 
					
						
							| 
									
										
										
										
											2013-11-12 11:19:04 +09:00
										 |  |  | 	int last = 0; | 
					
						
							| 
									
										
										
										
											2013-11-14 18:45:32 -02:00
										 |  |  | 	for (int i = 0; i < plannerModel->rowCount(); i++) { | 
					
						
							| 
									
										
										
										
											2013-08-26 16:34:06 -03:00
										 |  |  | 		divedatapoint dp = plannerModel->at(i); | 
					
						
							| 
									
										
										
										
											2013-11-12 11:19:04 +09:00
										 |  |  | 		if (dp.time == 0) // those are the magic entries for tanks
 | 
					
						
							|  |  |  | 			continue; | 
					
						
							| 
									
										
										
										
											2013-08-26 16:34:06 -03:00
										 |  |  | 		DiveHandler *h = handles.at(i); | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | 		h->setPos(timeLine->posAtValue(dp.time / 60), depthLine->posAtValue(dp.depth)); | 
					
						
							| 
									
										
										
										
											2013-11-19 20:08:46 -02:00
										 |  |  | 		QPointF p1 = (last == i) ?  QPointF(timeLine->posAtValue(0), depthLine->posAtValue(0)) : handles[last]->pos(); | 
					
						
							| 
									
										
										
										
											2013-07-21 12:12:31 -03:00
										 |  |  | 		QPointF p2 = handles[i]->pos(); | 
					
						
							|  |  |  | 		QLineF line(p1, p2); | 
					
						
							|  |  |  | 		QPointF pos = line.pointAt(0.5); | 
					
						
							|  |  |  | 		gases[i]->setPos(pos); | 
					
						
							| 
									
										
										
										
											2013-11-12 11:19:04 +09:00
										 |  |  | 		gases[i]->setText(dpGasToStr(plannerModel->at(i))); | 
					
						
							|  |  |  | 		last = i; | 
					
						
							| 
									
										
										
										
											2013-07-21 12:12:31 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-27 20:54:17 +08:00
										 |  |  | 	// (re-) create the profile with different colors for segments that were
 | 
					
						
							|  |  |  | 	// entered vs. segments that were calculated
 | 
					
						
							|  |  |  | 	double lastx = timeLine->posAtValue(0); | 
					
						
							|  |  |  | 	double lasty = depthLine->posAtValue(0); | 
					
						
							| 
									
										
										
										
											2013-06-27 19:52:58 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	QPolygonF poly; | 
					
						
							|  |  |  | 	poly.append(QPointF(lastx, lasty)); | 
					
						
							| 
									
										
										
										
											2013-09-16 12:11:06 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-23 21:27:52 -07:00
										 |  |  | 	for (dp = diveplan.dp; dp != NULL; dp = dp->next) { | 
					
						
							| 
									
										
										
										
											2013-11-12 11:19:04 +09:00
										 |  |  | 		if (dp->time == 0) // magic entry for available tank
 | 
					
						
							|  |  |  | 			continue; | 
					
						
							| 
									
										
										
										
											2013-06-27 10:56:46 +02:00
										 |  |  | 		double xpos = timeLine->posAtValue(dp->time / 60.0); | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | 		double ypos = depthLine->posAtValue(dp->depth); | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 		if (!dp->entered) { | 
					
						
							| 
									
										
										
										
											2013-07-02 13:39:54 -03:00
										 |  |  | 			QGraphicsLineItem *item = new QGraphicsLineItem(lastx, lasty, xpos, ypos); | 
					
						
							|  |  |  | 			item->setPen(QPen(QBrush(Qt::red),0)); | 
					
						
							|  |  |  | 			scene()->addItem(item); | 
					
						
							|  |  |  | 			lines << item; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-06-27 10:56:46 +02:00
										 |  |  | 		lastx = xpos; | 
					
						
							|  |  |  | 		lasty = ypos; | 
					
						
							| 
									
										
										
										
											2013-06-27 19:52:58 -03:00
										 |  |  | 		poly.append(QPointF(lastx, lasty)); | 
					
						
							| 
									
										
										
										
											2013-06-23 21:27:52 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-06-27 17:31:10 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-27 19:52:58 -03:00
										 |  |  | 	diveBg->setPolygon(poly); | 
					
						
							|  |  |  | 	QRectF b = poly.boundingRect(); | 
					
						
							| 
									
										
										
										
											2013-07-02 11:21:28 -03:00
										 |  |  | 	QLinearGradient pat( | 
					
						
							| 
									
										
										
										
											2013-06-27 19:52:58 -03:00
										 |  |  | 		b.x(), | 
					
						
							|  |  |  | 		b.y(), | 
					
						
							|  |  |  | 		b.x(), | 
					
						
							|  |  |  | 		b.height() + b.y() | 
					
						
							|  |  |  | 	); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 11:21:28 -03:00
										 |  |  | 	pat.setColorAt(1, profile_color[DEPTH_BOTTOM].first()); | 
					
						
							|  |  |  | 	pat.setColorAt(0, profile_color[DEPTH_TOP].first()); | 
					
						
							|  |  |  | 	diveBg->setBrush(pat); | 
					
						
							| 
									
										
										
										
											2013-06-27 19:52:58 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-16 12:11:06 -03:00
										 |  |  | 	plannerModel->deleteTemporaryPlan(); | 
					
						
							| 
									
										
										
										
											2013-06-20 12:37:41 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-20 18:40:59 -03:00
										 |  |  | void DivePlannerGraphics::resizeEvent(QResizeEvent* event) | 
					
						
							| 
									
										
										
										
											2013-06-20 13:28:04 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-06-23 13:09:29 -07:00
										 |  |  | 	QGraphicsView::resizeEvent(event); | 
					
						
							| 
									
										
										
										
											2013-06-27 14:48:03 -03:00
										 |  |  | 	fitInView(sceneRect(), Qt::IgnoreAspectRatio); | 
					
						
							| 
									
										
										
										
											2013-06-20 13:28:04 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-20 18:40:59 -03:00
										 |  |  | void DivePlannerGraphics::showEvent(QShowEvent* event) | 
					
						
							| 
									
										
										
										
											2013-06-20 13:28:04 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-06-23 13:09:29 -07:00
										 |  |  | 	QGraphicsView::showEvent(event); | 
					
						
							| 
									
										
										
										
											2013-06-27 14:48:03 -03:00
										 |  |  | 	fitInView(sceneRect(), Qt::IgnoreAspectRatio); | 
					
						
							| 
									
										
										
										
											2013-06-20 13:28:04 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-20 18:40:59 -03:00
										 |  |  | void DivePlannerGraphics::mouseMoveEvent(QMouseEvent* event) | 
					
						
							| 
									
										
										
										
											2013-06-20 13:53:12 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	QPointF mappedPos = mapToScene(event->pos()); | 
					
						
							| 
									
										
										
										
											2013-06-20 14:20:45 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 21:07:06 -02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	double xpos = timeLine->valueAt(mappedPos); | 
					
						
							|  |  |  | 	double ypos = depthLine->valueAt(mappedPos); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	xpos =  (xpos > timeLine->maximum()) ? timeLine->posAtValue(timeLine->maximum()) | 
					
						
							|  |  |  | 		: (xpos < timeLine->minimum()) ? timeLine->posAtValue(timeLine->minimum()) | 
					
						
							|  |  |  | 		: timeLine->posAtValue(xpos); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ypos =  (ypos > depthLine->maximum()) ? depthLine->posAtValue(depthLine->maximum()) | 
					
						
							|  |  |  | 		: ( ypos < depthLine->minimum()) ? depthLine->posAtValue(depthLine->minimum()) | 
					
						
							|  |  |  | 		: depthLine->posAtValue(ypos); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	verticalLine->setPos(xpos, fromPercent(0, Qt::Vertical)); | 
					
						
							|  |  |  | 	horizontalLine->setPos(fromPercent(0, Qt::Horizontal), ypos); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	depthString->setPos(fromPercent(1, Qt::Horizontal), ypos); | 
					
						
							|  |  |  | 	timeString->setPos(xpos+1, fromPercent(95, Qt::Vertical)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if(isPointOutOfBoundaries(mappedPos)) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2013-07-02 11:12:15 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | 	depthString->setText(get_depth_string(depthLine->valueAt(mappedPos), true, false)); | 
					
						
							| 
									
										
										
										
											2013-06-23 17:32:23 -07:00
										 |  |  | 	timeString->setText(QString::number(rint(timeLine->valueAt(mappedPos))) + "min"); | 
					
						
							| 
									
										
										
										
											2013-06-20 14:20:45 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 13:31:25 -03:00
										 |  |  | 	// calculate the correct color for the depthString.
 | 
					
						
							|  |  |  | 	// QGradient doesn't returns it's interpolation, meh.
 | 
					
						
							|  |  |  | 	double percent = depthLine->percentAt(mappedPos); | 
					
						
							|  |  |  | 	QColor& startColor = profile_color[SAMPLE_SHALLOW].first(); | 
					
						
							|  |  |  | 	QColor& endColor = profile_color[SAMPLE_DEEP].first(); | 
					
						
							|  |  |  | 	short redDelta = (endColor.red() - startColor.red()) * percent + startColor.red(); | 
					
						
							|  |  |  | 	short greenDelta = (endColor.green() - startColor.green()) * percent + startColor.green(); | 
					
						
							|  |  |  | 	short blueDelta = (endColor.blue() - startColor.blue()) * percent + startColor.blue(); | 
					
						
							|  |  |  | 	depthString->setBrush( QColor(redDelta, greenDelta, blueDelta)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-23 13:09:29 -07:00
										 |  |  | 	if (activeDraggedHandler) | 
					
						
							| 
									
										
										
										
											2013-09-19 09:13:53 -05:00
										 |  |  | 		moveActiveHandler(mappedPos, handles.indexOf(activeDraggedHandler)); | 
					
						
							| 
									
										
										
										
											2013-06-20 14:20:45 -03:00
										 |  |  | 	if (!handles.count()) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-23 13:09:29 -07:00
										 |  |  | 	if (handles.last()->x() > mappedPos.x()) { | 
					
						
							|  |  |  | 		verticalLine->setPen(QPen(QBrush(Qt::red), 0, Qt::SolidLine)); | 
					
						
							|  |  |  | 		horizontalLine->setPen(QPen(QBrush(Qt::red), 0, Qt::SolidLine)); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2013-06-20 14:20:45 -03:00
										 |  |  | 		verticalLine->setPen(QPen(Qt::DotLine)); | 
					
						
							|  |  |  | 		horizontalLine->setPen(QPen(Qt::DotLine)); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-06-20 13:53:12 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-06-20 13:56:28 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-19 09:13:53 -05:00
										 |  |  | void DivePlannerGraphics::moveActiveHandler(const QPointF& mappedPos, const int pos) | 
					
						
							| 
									
										
										
										
											2013-06-20 15:15:10 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-19 09:13:53 -05:00
										 |  |  | 	divedatapoint data = plannerModel->at(pos); | 
					
						
							| 
									
										
										
										
											2013-09-22 11:01:18 -07:00
										 |  |  | 	int mintime = 0, maxtime = (timeLine->maximum() + 10) * 60; | 
					
						
							|  |  |  | 	if (pos > 0) | 
					
						
							|  |  |  | 		mintime = plannerModel->at(pos - 1).time; | 
					
						
							|  |  |  | 	if (pos < plannerModel->size() - 1) | 
					
						
							|  |  |  | 		maxtime = plannerModel->at(pos + 1).time; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-19 09:13:53 -05:00
										 |  |  | 	int minutes = rint(timeLine->valueAt(mappedPos)); | 
					
						
							| 
									
										
										
										
											2013-09-22 11:01:18 -07:00
										 |  |  | 	if (minutes * 60 <= mintime || minutes * 60 >= maxtime) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | 	int milimeters = rint(depthLine->valueAt(mappedPos) / M_OR_FT(1,1)) * M_OR_FT(1,1); | 
					
						
							| 
									
										
										
										
											2013-09-19 09:13:53 -05:00
										 |  |  | 	double xpos = timeLine->posAtValue(minutes); | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | 	double ypos = depthLine->posAtValue(milimeters); | 
					
						
							| 
									
										
										
										
											2013-09-19 09:13:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | 	data.depth = milimeters; | 
					
						
							| 
									
										
										
										
											2013-09-19 09:13:53 -05:00
										 |  |  | 	data.time = rint(timeLine->valueAt(mappedPos)) * 60; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	plannerModel->editStop(pos, data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-04 10:29:28 -03:00
										 |  |  | 	activeDraggedHandler->setPos(QPointF(xpos, ypos)); | 
					
						
							| 
									
										
										
										
											2013-06-27 10:56:46 +02:00
										 |  |  | 	qDeleteAll(lines); | 
					
						
							| 
									
										
										
										
											2013-06-26 20:41:39 -03:00
										 |  |  | 	lines.clear(); | 
					
						
							| 
									
										
										
										
											2013-09-19 23:02:50 -05:00
										 |  |  | 	drawProfile(); | 
					
						
							| 
									
										
										
										
											2013-06-20 15:15:10 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-21 16:28:17 -03:00
										 |  |  | bool DivePlannerGraphics::isPointOutOfBoundaries(const QPointF& point) | 
					
						
							| 
									
										
										
										
											2013-06-20 13:56:28 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-06-21 16:07:44 -03:00
										 |  |  | 	double xpos = timeLine->valueAt(point); | 
					
						
							|  |  |  | 	double ypos = depthLine->valueAt(point); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-23 13:09:29 -07:00
										 |  |  | 	if (xpos > timeLine->maximum() || | 
					
						
							|  |  |  | 	    xpos < timeLine->minimum() || | 
					
						
							|  |  |  | 	    ypos > depthLine->maximum() || | 
					
						
							|  |  |  | 	    ypos < depthLine->minimum()) | 
					
						
							| 
									
										
										
										
											2013-06-20 13:56:28 -03:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-06-20 14:29:32 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-20 18:40:59 -03:00
										 |  |  | void DivePlannerGraphics::mousePressEvent(QMouseEvent* event) | 
					
						
							| 
									
										
										
										
											2013-06-20 14:46:40 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	if (event->modifiers()) { | 
					
						
							| 
									
										
										
										
											2013-07-04 11:01:59 -03:00
										 |  |  | 		QGraphicsView::mousePressEvent(event); | 
					
						
							| 
									
										
										
										
											2013-07-04 12:30:05 -03:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2013-07-04 11:01:59 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 08:45:14 -07:00
										 |  |  | 	QPointF mappedPos = mapToScene(event->pos()); | 
					
						
							| 
									
										
										
										
											2013-11-14 23:29:36 -02:00
										 |  |  | 	if (event->button() == Qt::LeftButton){ | 
					
						
							|  |  |  | 		Q_FOREACH(QGraphicsItem *item, scene()->items(mappedPos, Qt::IntersectsItemBoundingRect, Qt::AscendingOrder, transform())) { | 
					
						
							|  |  |  | 			if (DiveHandler *h = qgraphicsitem_cast<DiveHandler*>(item)) { | 
					
						
							|  |  |  | 				activeDraggedHandler = h; | 
					
						
							|  |  |  | 				activeDraggedHandler->setBrush(Qt::red); | 
					
						
							|  |  |  | 				originalHandlerPos = activeDraggedHandler->pos(); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-20 14:46:40 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-04 12:30:05 -03:00
										 |  |  | 	QGraphicsView::mousePressEvent(event); | 
					
						
							| 
									
										
										
										
											2013-06-20 14:46:40 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-06-20 14:29:32 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-20 18:40:59 -03:00
										 |  |  | void DivePlannerGraphics::mouseReleaseEvent(QMouseEvent* event) | 
					
						
							| 
									
										
										
										
											2013-06-20 14:46:40 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-06-23 13:09:29 -07:00
										 |  |  | 	if (activeDraggedHandler) { | 
					
						
							| 
									
										
										
										
											2013-09-22 11:01:18 -07:00
										 |  |  | 		/* we already deal with all the positioning in the life update,
 | 
					
						
							|  |  |  | 		 * so all we need to do here is change the color of the handler */ | 
					
						
							| 
									
										
										
										
											2013-06-27 19:52:58 -03:00
										 |  |  | 		activeDraggedHandler->setBrush(QBrush(Qt::white)); | 
					
						
							| 
									
										
										
										
											2013-06-20 14:46:40 -03:00
										 |  |  | 		activeDraggedHandler = 0; | 
					
						
							| 
									
										
										
										
											2013-09-19 23:02:50 -05:00
										 |  |  | 		drawProfile(); | 
					
						
							| 
									
										
										
										
											2013-06-20 17:34:42 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-06-20 14:46:40 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-06-20 14:29:32 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-21 10:03:04 -03:00
										 |  |  | DiveHandler::DiveHandler(): QGraphicsEllipseItem() | 
					
						
							| 
									
										
										
										
											2013-06-20 14:29:32 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-07-04 10:06:52 -03:00
										 |  |  | 	setRect(-5,-5,10,10); | 
					
						
							|  |  |  | 	setFlag(QGraphicsItem::ItemIgnoresTransformations); | 
					
						
							| 
									
										
										
										
											2013-07-04 11:01:59 -03:00
										 |  |  | 	setFlag(QGraphicsItem::ItemIsSelectable); | 
					
						
							| 
									
										
										
										
											2013-06-27 19:52:58 -03:00
										 |  |  | 	setBrush(Qt::white); | 
					
						
							|  |  |  | 	setZValue(2); | 
					
						
							| 
									
										
										
										
											2013-06-20 14:29:32 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-07-04 11:01:59 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 20:42:05 -02:00
										 |  |  | int DiveHandler::parentIndex() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	DivePlannerGraphics *view = qobject_cast<DivePlannerGraphics*>(scene()->views().first()); | 
					
						
							|  |  |  | 	return view->handles.indexOf(this); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-14 23:29:36 -02:00
										 |  |  | void DiveHandler::contextMenuEvent(QGraphicsSceneContextMenuEvent* event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QMenu m; | 
					
						
							| 
									
										
										
										
											2013-11-19 20:42:05 -02:00
										 |  |  | 	GasSelectionModel *model = GasSelectionModel::instance(); | 
					
						
							|  |  |  | 	model->repopulate(); | 
					
						
							|  |  |  | 	int rowCount = model->rowCount(); | 
					
						
							|  |  |  | 	for(int i = 0; i < rowCount; i++){ | 
					
						
							|  |  |  | 		QAction *action = new QAction(&m); | 
					
						
							|  |  |  | 		action->setText( model->data(model->index(i, 0),Qt::DisplayRole).toString()); | 
					
						
							|  |  |  | 		connect(action, SIGNAL(triggered(bool)), this, SLOT(changeGas())); | 
					
						
							|  |  |  | 		m.addAction(action); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	m.addSeparator(); | 
					
						
							| 
									
										
										
										
											2013-11-14 23:29:36 -02:00
										 |  |  | 	m.addAction(QObject::tr("Remove this Point"), this, SLOT(selfRemove())); | 
					
						
							|  |  |  | 	m.exec(event->screenPos()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DiveHandler::selfRemove() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	setSelected(true); | 
					
						
							|  |  |  | 	DivePlannerGraphics *view = qobject_cast<DivePlannerGraphics*>(scene()->views().first()); | 
					
						
							|  |  |  | 	view->keyDeleteAction(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 20:42:05 -02:00
										 |  |  | void DiveHandler::changeGas() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QAction *action = qobject_cast<QAction*>(sender()); | 
					
						
							|  |  |  | 	QModelIndex index = plannerModel->index(parentIndex(), DivePlannerPointsModel::GAS); | 
					
						
							|  |  |  | 	plannerModel->setData(index, action->text()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-04 11:01:59 -03:00
										 |  |  | void DiveHandler::mousePressEvent(QGraphicsSceneMouseEvent* event) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-14 23:29:36 -02:00
										 |  |  | 	if (event->button() != Qt::LeftButton) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	if (event->modifiers().testFlag(Qt::ControlModifier)) { | 
					
						
							| 
									
										
										
										
											2013-07-04 11:01:59 -03:00
										 |  |  | 		setSelected(true); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-04 11:06:28 -03:00
										 |  |  | 	// mousePressEvent 'grabs' the mouse and keyboard, annoying.
 | 
					
						
							| 
									
										
										
										
											2013-07-04 11:01:59 -03:00
										 |  |  | 	ungrabMouse(); | 
					
						
							| 
									
										
										
										
											2013-08-30 15:53:10 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* hack. Sometimes the keyboard is grabbed, sometime it's not,
 | 
					
						
							|  |  |  | 	so, let's force a grab and release, to get rid of a warning. */ | 
					
						
							|  |  |  | 	grabKeyboard(); | 
					
						
							| 
									
										
										
										
											2013-07-04 11:01:59 -03:00
										 |  |  | 	ungrabKeyboard(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-06-20 15:52:27 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | void Ruler::setMaximum(double maximum) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	max = maximum; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Ruler::setMinimum(double minimum) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	min = minimum; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-16 20:10:12 -03:00
										 |  |  | void Ruler::setTextColor(const QColor& color) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	textColor = color; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-30 09:18:03 -08:00
										 |  |  | void Ruler::eraseAll() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	qDeleteAll(ticks); | 
					
						
							|  |  |  | 	ticks.clear(); | 
					
						
							|  |  |  | 	qDeleteAll(labels); | 
					
						
							|  |  |  | 	labels.clear(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-16 20:10:12 -03:00
										 |  |  | Ruler::Ruler() : orientation(Qt::Horizontal) | 
					
						
							| 
									
										
										
										
											2013-06-20 15:52:27 -03:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-30 09:18:03 -08:00
										 |  |  | Ruler::~Ruler() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	eraseAll(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-20 15:52:27 -03:00
										 |  |  | void Ruler::setOrientation(Qt::Orientation o) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	orientation = o; | 
					
						
							| 
									
										
										
										
											2013-09-16 11:09:54 -03:00
										 |  |  | 	// position the elements on the screen.
 | 
					
						
							|  |  |  | 	setMinimum(minimum()); | 
					
						
							|  |  |  | 	setMaximum(maximum()); | 
					
						
							| 
									
										
										
										
											2013-06-20 15:52:27 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Ruler::updateTicks() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-30 09:18:03 -08:00
										 |  |  | 	eraseAll(); | 
					
						
							| 
									
										
										
										
											2013-09-16 20:10:12 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-20 16:48:24 -03:00
										 |  |  | 	QLineF m = line(); | 
					
						
							| 
									
										
										
										
											2013-07-02 12:01:47 -03:00
										 |  |  | 	QGraphicsLineItem *item = NULL; | 
					
						
							| 
									
										
										
										
											2013-09-16 20:10:12 -03:00
										 |  |  | 	QGraphicsSimpleTextItem *label = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	double steps = (max - min) / interval; | 
					
						
							|  |  |  | 	qreal pos; | 
					
						
							|  |  |  | 	double currValue = min; | 
					
						
							| 
									
										
										
										
											2013-07-02 12:01:47 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-23 13:09:29 -07:00
										 |  |  | 	if (orientation == Qt::Horizontal) { | 
					
						
							| 
									
										
										
										
											2013-06-20 16:48:24 -03:00
										 |  |  | 		double stepSize = (m.x2() - m.x1()) / steps; | 
					
						
							| 
									
										
										
										
											2013-11-26 18:07:08 -02:00
										 |  |  | 		for (pos = m.x1(); pos <= m.x2(); pos += stepSize, currValue += interval) { | 
					
						
							| 
									
										
										
										
											2013-07-02 12:01:47 -03:00
										 |  |  | 			item = new QGraphicsLineItem(pos, m.y1(), pos, m.y1() + tickSize, this); | 
					
						
							|  |  |  | 			item->setPen(pen()); | 
					
						
							|  |  |  | 			ticks.push_back(item); | 
					
						
							| 
									
										
										
										
											2013-09-16 20:10:12 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			label = new QGraphicsSimpleTextItem(QString::number(currValue), this); | 
					
						
							|  |  |  | 			label->setBrush(QBrush(textColor)); | 
					
						
							|  |  |  | 			label->setFlag(ItemIgnoresTransformations); | 
					
						
							|  |  |  | 			label->setPos(pos - label->boundingRect().width()/2, m.y1() + tickSize + 5); | 
					
						
							|  |  |  | 			labels.push_back(label); | 
					
						
							| 
									
										
										
										
											2013-06-20 16:48:24 -03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-06-23 13:09:29 -07:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2013-06-20 16:48:24 -03:00
										 |  |  | 		double stepSize = (m.y2() - m.y1()) / steps; | 
					
						
							| 
									
										
										
										
											2013-11-26 18:07:08 -02:00
										 |  |  | 		for (pos = m.y1(); pos <= m.y2(); pos += stepSize, currValue += interval) { | 
					
						
							| 
									
										
										
										
											2013-07-02 12:01:47 -03:00
										 |  |  | 			item = new QGraphicsLineItem(m.x1(), pos, m.x1() - tickSize, pos, this); | 
					
						
							|  |  |  | 			item->setPen(pen()); | 
					
						
							|  |  |  | 			ticks.push_back(item); | 
					
						
							| 
									
										
										
										
											2013-09-16 20:10:12 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | 			label = new QGraphicsSimpleTextItem(get_depth_string(currValue, false, false), this); | 
					
						
							| 
									
										
										
										
											2013-09-16 20:10:12 -03:00
										 |  |  | 			label->setBrush(QBrush(textColor)); | 
					
						
							|  |  |  | 			label->setFlag(ItemIgnoresTransformations); | 
					
						
							|  |  |  | 			label->setPos(m.x2() - 80, pos); | 
					
						
							|  |  |  | 			labels.push_back(label); | 
					
						
							| 
									
										
										
										
											2013-06-20 16:48:24 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-06-20 15:52:27 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 11:12:15 -03:00
										 |  |  | void Ruler::setTickSize(qreal size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	tickSize = size; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-20 16:48:24 -03:00
										 |  |  | void Ruler::setTickInterval(double i) | 
					
						
							| 
									
										
										
										
											2013-06-20 15:52:27 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-06-20 16:48:24 -03:00
										 |  |  | 	interval = i; | 
					
						
							| 
									
										
										
										
											2013-06-20 15:52:27 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-06-20 17:34:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | qreal Ruler::valueAt(const QPointF& p) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QLineF m = line(); | 
					
						
							| 
									
										
										
										
											2013-06-23 13:09:29 -07:00
										 |  |  | 	double retValue =  orientation == Qt::Horizontal ? | 
					
						
							|  |  |  | 				max * (p.x() - m.x1()) / (m.x2() - m.x1()) : | 
					
						
							|  |  |  | 				max * (p.y() - m.y1()) / (m.y2() - m.y1()); | 
					
						
							| 
									
										
										
										
											2013-06-21 15:53:20 -03:00
										 |  |  | 	return retValue; | 
					
						
							| 
									
										
										
										
											2013-06-20 17:34:42 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-06-20 18:25:03 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | qreal Ruler::posAtValue(qreal value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QLineF m = line(); | 
					
						
							| 
									
										
										
										
											2013-06-21 15:53:20 -03:00
										 |  |  | 	double size = max - min; | 
					
						
							|  |  |  | 	double percent = value / size; | 
					
						
							| 
									
										
										
										
											2013-06-23 13:09:29 -07:00
										 |  |  | 	double realSize = orientation == Qt::Horizontal ? | 
					
						
							|  |  |  | 				m.x2() - m.x1() : | 
					
						
							|  |  |  | 				m.y2() - m.y1(); | 
					
						
							| 
									
										
										
										
											2013-06-21 15:53:20 -03:00
										 |  |  | 	double retValue = realSize * percent; | 
					
						
							| 
									
										
										
										
											2013-06-23 13:09:29 -07:00
										 |  |  | 	retValue =  (orientation == Qt::Horizontal) ? | 
					
						
							|  |  |  | 				retValue + m.x1() : | 
					
						
							|  |  |  | 				retValue + m.y1(); | 
					
						
							| 
									
										
										
										
											2013-06-21 15:53:20 -03:00
										 |  |  | 	return retValue; | 
					
						
							| 
									
										
										
										
											2013-06-20 18:40:59 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 13:31:25 -03:00
										 |  |  | qreal Ruler::percentAt(const QPointF& p) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	qreal value = valueAt(p); | 
					
						
							|  |  |  | 	double size = max - min; | 
					
						
							|  |  |  | 	double percent = value / size; | 
					
						
							|  |  |  | 	return percent; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-21 16:07:44 -03:00
										 |  |  | double Ruler::maximum() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return max; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | double Ruler::minimum() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return min; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 12:01:47 -03:00
										 |  |  | void Ruler::setColor(const QColor& color) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-16 19:21:13 -03:00
										 |  |  | 	QPen defaultPen(color); | 
					
						
							|  |  |  | 	defaultPen.setJoinStyle(Qt::RoundJoin); | 
					
						
							|  |  |  | 	defaultPen.setCapStyle(Qt::RoundCap); | 
					
						
							|  |  |  | 	defaultPen.setWidth(2); | 
					
						
							|  |  |  | 	defaultPen.setCosmetic(true); | 
					
						
							|  |  |  | 	setPen(defaultPen); | 
					
						
							| 
									
										
										
										
											2013-07-02 12:01:47 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-17 21:20:07 -02:00
										 |  |  | Button::Button(QObject* parent, QGraphicsItem *itemParent): QObject(parent), QGraphicsRectItem(itemParent) | 
					
						
							| 
									
										
										
										
											2013-06-21 16:44:38 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-08-26 14:17:39 -03:00
										 |  |  | 	icon = new QGraphicsPixmapItem(this); | 
					
						
							| 
									
										
										
										
											2013-06-27 16:45:58 -03:00
										 |  |  | 	text = new QGraphicsSimpleTextItem(this); | 
					
						
							|  |  |  | 	icon->setPos(0,0); | 
					
						
							|  |  |  | 	text->setPos(0,0); | 
					
						
							| 
									
										
										
										
											2013-06-21 16:44:38 -03:00
										 |  |  | 	setFlag(ItemIgnoresTransformations); | 
					
						
							| 
									
										
										
										
											2013-07-02 14:14:09 -03:00
										 |  |  | 	setPen(QPen(QBrush(), 0)); | 
					
						
							| 
									
										
										
										
											2013-06-27 16:45:58 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Button::setPixmap(const QPixmap& pixmap) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-17 21:20:07 -02:00
										 |  |  | 	icon->setPixmap(pixmap); | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	if(pixmap.isNull()) | 
					
						
							| 
									
										
										
										
											2013-06-27 16:45:58 -03:00
										 |  |  | 		icon->hide(); | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2013-06-27 16:45:58 -03:00
										 |  |  | 		icon->show(); | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-27 16:45:58 -03:00
										 |  |  | 	setRect(childrenBoundingRect()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Button::setText(const QString& t) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	text->setText(t); | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	if(icon->pixmap().isNull()) { | 
					
						
							| 
									
										
										
										
											2013-06-27 16:45:58 -03:00
										 |  |  | 		icon->hide(); | 
					
						
							|  |  |  | 		text->setPos(0,0); | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2013-06-27 16:45:58 -03:00
										 |  |  | 		icon->show(); | 
					
						
							|  |  |  | 		text->setPos(22,0); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	setRect(childrenBoundingRect()); | 
					
						
							| 
									
										
										
										
											2013-06-21 16:44:38 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Button::mousePressEvent(QGraphicsSceneMouseEvent* event) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-07-02 14:36:52 -03:00
										 |  |  | 	event->ignore(); | 
					
						
							| 
									
										
										
										
											2013-06-21 16:44:38 -03:00
										 |  |  | 	emit clicked(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-08-26 08:43:37 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | DivePlannerWidget::DivePlannerWidget(QWidget* parent, Qt::WindowFlags f): QWidget(parent, f) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ui.setupUi(this); | 
					
						
							|  |  |  | 	ui.tableWidget->setTitle(tr("Dive Planner Points")); | 
					
						
							|  |  |  | 	ui.tableWidget->setModel(DivePlannerPointsModel::instance()); | 
					
						
							|  |  |  | 	ui.tableWidget->view()->setItemDelegateForColumn(DivePlannerPointsModel::GAS, new AirTypesDelegate(this)); | 
					
						
							| 
									
										
										
										
											2013-11-10 07:06:26 +09:00
										 |  |  | 	ui.cylinderTableWidget->setTitle(tr("Available Gases")); | 
					
						
							|  |  |  | 	ui.cylinderTableWidget->setModel(CylindersModel::instance()); | 
					
						
							| 
									
										
										
										
											2013-11-12 17:57:33 -02:00
										 |  |  | 	QTableView *view = ui.cylinderTableWidget->view(); | 
					
						
							|  |  |  | 	view->setColumnHidden(CylindersModel::START, true); | 
					
						
							|  |  |  | 	view->setColumnHidden(CylindersModel::END, true); | 
					
						
							|  |  |  | 	view->setColumnHidden(CylindersModel::DEPTH, false); | 
					
						
							|  |  |  | 	view->setItemDelegateForColumn(CylindersModel::TYPE, new TankInfoDelegate()); | 
					
						
							| 
									
										
										
										
											2013-11-10 21:02:53 +09:00
										 |  |  | 	connect(ui.cylinderTableWidget, SIGNAL(addButtonClicked()), DivePlannerPointsModel::instance(), SLOT(addCylinder_clicked())); | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	connect(ui.tableWidget, SIGNAL(addButtonClicked()), DivePlannerPointsModel::instance(), SLOT(addStop())); | 
					
						
							| 
									
										
										
										
											2013-11-14 17:39:35 -02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	connect(CylindersModel::instance(), SIGNAL(dataChanged(QModelIndex,QModelIndex)), | 
					
						
							|  |  |  | 		GasSelectionModel::instance(), SLOT(repopulate())); | 
					
						
							|  |  |  | 	connect(CylindersModel::instance(), SIGNAL(rowsInserted(QModelIndex,int,int)), | 
					
						
							|  |  |  | 		GasSelectionModel::instance(), SLOT(repopulate())); | 
					
						
							|  |  |  | 	connect(CylindersModel::instance(), SIGNAL(rowsRemoved(QModelIndex,int,int)), | 
					
						
							|  |  |  | 		GasSelectionModel::instance(), SLOT(repopulate())); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-10 21:02:53 +09:00
										 |  |  | 	ui.tableWidget->setBtnToolTip(tr("add dive data point")); | 
					
						
							| 
									
										
										
										
											2013-11-14 19:55:35 +01:00
										 |  |  | 	connect(ui.startTime, SIGNAL(timeChanged(QTime)), plannerModel, SLOT(setStartTime(QTime))); | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	connect(ui.ATMPressure, SIGNAL(textChanged(QString)), this, SLOT(atmPressureChanged(QString))); | 
					
						
							|  |  |  | 	connect(ui.bottomSAC, SIGNAL(textChanged(QString)), this, SLOT(bottomSacChanged(QString))); | 
					
						
							|  |  |  | 	connect(ui.decoStopSAC, SIGNAL(textChanged(QString)), this, SLOT(decoSacChanged(QString))); | 
					
						
							| 
									
										
										
										
											2013-11-14 19:55:33 +01:00
										 |  |  | 	connect(ui.gfhigh, SIGNAL(valueChanged(int)), plannerModel, SLOT(setGFHigh(int))); | 
					
						
							|  |  |  | 	connect(ui.gflow, SIGNAL(valueChanged(int)), plannerModel, SLOT(setGFLow(int))); | 
					
						
							| 
									
										
										
										
											2013-11-14 19:55:35 +01:00
										 |  |  | 	connect(ui.lastStop, SIGNAL(toggled(bool)), plannerModel, SLOT(setLastStop6m(bool))); | 
					
						
							| 
									
										
										
										
											2013-09-09 07:18:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-16 11:38:41 -03:00
										 |  |  | 	// Creating the plan
 | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	connect(ui.buttonBox, SIGNAL(accepted()), plannerModel, SLOT(createPlan())); | 
					
						
							|  |  |  | 	connect(ui.buttonBox, SIGNAL(rejected()), plannerModel, SLOT(cancelPlan())); | 
					
						
							| 
									
										
										
										
											2013-09-16 11:38:41 -03:00
										 |  |  | 	connect(plannerModel, SIGNAL(planCreated()), mainWindow(), SLOT(showProfile())); | 
					
						
							| 
									
										
										
										
											2013-09-16 12:30:58 -03:00
										 |  |  | 	connect(plannerModel, SIGNAL(planCreated()), mainWindow(), SLOT(refreshDisplay())); | 
					
						
							| 
									
										
										
										
											2013-09-16 11:38:41 -03:00
										 |  |  | 	connect(plannerModel, SIGNAL(planCanceled()), mainWindow(), SLOT(showProfile())); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-09 07:18:22 -03:00
										 |  |  | 	/* set defaults. */ | 
					
						
							| 
									
										
										
										
											2013-10-03 11:54:25 -07:00
										 |  |  | 	ui.startTime->setTime( QTime(1, 0) ); | 
					
						
							|  |  |  | 	ui.ATMPressure->setText( "1013" ); | 
					
						
							|  |  |  | 	ui.bottomSAC->setText("20"); | 
					
						
							|  |  |  | 	ui.decoStopSAC->setText("17"); | 
					
						
							| 
									
										
										
										
											2013-11-14 19:55:34 +01:00
										 |  |  | 	ui.gflow->setValue(prefs.gflow); | 
					
						
							|  |  |  | 	ui.gfhigh->setValue(prefs.gfhigh); | 
					
						
							| 
									
										
										
										
											2013-09-26 18:14:09 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	setMinimumWidth(0); | 
					
						
							|  |  |  | 	setMinimumHeight(0); | 
					
						
							| 
									
										
										
										
											2013-08-28 07:48:46 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 07:43:00 +01:00
										 |  |  | void DivePlannerWidget::settingsChanged() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ui.gflow->setValue(prefs.gflow); | 
					
						
							|  |  |  | 	ui.gfhigh->setValue(prefs.gfhigh); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-10 21:02:53 +09:00
										 |  |  | void DivePlannerPointsModel::addCylinder_clicked() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	CylindersModel::instance()->add(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | void DivePlannerWidget::atmPressureChanged(const QString& pressure) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-08-26 16:34:06 -03:00
										 |  |  | 	plannerModel->setSurfacePressure(pressure.toInt()); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DivePlannerWidget::bottomSacChanged(const QString& bottomSac) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-08-26 16:34:06 -03:00
										 |  |  | 	plannerModel->setBottomSac(bottomSac.toInt()); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DivePlannerWidget::decoSacChanged(const QString& decosac) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-08-26 16:34:06 -03:00
										 |  |  | 	plannerModel->setDecoSac(decosac.toInt()); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-08 22:09:46 -02:00
										 |  |  | void DivePlannerPointsModel::setPlanMode(Mode m) | 
					
						
							| 
									
										
										
										
											2013-09-18 21:33:53 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-08 22:09:46 -02:00
										 |  |  | 	mode = m; | 
					
						
							| 
									
										
										
										
											2013-09-18 21:33:53 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool DivePlannerPointsModel::isPlanner() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return mode == PLAN; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-26 09:14:19 -03:00
										 |  |  | int DivePlannerPointsModel::columnCount(const QModelIndex& parent) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return COLUMNS; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QVariant DivePlannerPointsModel::data(const QModelIndex& index, int role) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	if(role == Qt::DisplayRole) { | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | 		divedatapoint p = divepoints.at(index.row()); | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 		switch(index.column()) { | 
					
						
							| 
									
										
										
										
											2013-12-07 16:25:44 +01:00
										 |  |  | 		case CCSETPOINT: return (double) p.po2 / 1000; | 
					
						
							| 
									
										
										
										
											2013-11-08 21:22:59 +09:00
										 |  |  | 		case DEPTH: return rint(get_depth_units(p.depth, NULL, NULL)); | 
					
						
							|  |  |  | 		case DURATION: return p.time / 60; | 
					
						
							| 
									
										
										
										
											2013-11-12 11:19:04 +09:00
										 |  |  | 		case GAS: return dpGasToStr(p); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	} else if (role == Qt::DecorationRole) { | 
					
						
							|  |  |  | 		switch(index.column()) { | 
					
						
							| 
									
										
										
										
											2013-08-28 07:27:59 -03:00
										 |  |  | 			case REMOVE : return QIcon(":trash"); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	} else if (role == Qt::FontRole) { | 
					
						
							| 
									
										
										
										
											2013-09-02 16:21:08 -03:00
										 |  |  | 		return  defaultModelFont(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-08-26 09:14:19 -03:00
										 |  |  | 	return QVariant(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-30 07:14:30 -03:00
										 |  |  | bool DivePlannerPointsModel::setData(const QModelIndex& index, const QVariant& value, int role) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-12 11:19:04 +09:00
										 |  |  | 	int o2 = 0; | 
					
						
							|  |  |  | 	int he = 0; | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	if(role == Qt::EditRole) { | 
					
						
							| 
									
										
										
										
											2013-08-30 13:27:15 -03:00
										 |  |  | 		divedatapoint& p = divepoints[index.row()]; | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 		switch(index.column()) { | 
					
						
							| 
									
										
										
										
											2013-11-08 21:22:59 +09:00
										 |  |  | 		case DEPTH: p.depth = units_to_depth(value.toInt()); break; | 
					
						
							|  |  |  | 		case DURATION: p.time = value.toInt() * 60; break; | 
					
						
							|  |  |  | 		case CCSETPOINT: { | 
					
						
							|  |  |  | 			int po2 = 0; | 
					
						
							|  |  |  | 			QByteArray gasv = value.toByteArray(); | 
					
						
							|  |  |  | 			if (validate_po2(gasv.data(), &po2)) | 
					
						
							|  |  |  | 				p.po2 = po2; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2013-11-12 11:19:04 +09:00
										 |  |  | 		case GAS: | 
					
						
							| 
									
										
										
										
											2013-11-08 21:22:59 +09:00
										 |  |  | 			QByteArray gasv = value.toByteArray(); | 
					
						
							|  |  |  | 			if (validate_gas(gasv.data(), &o2, &he)) { | 
					
						
							| 
									
										
										
										
											2013-11-12 11:19:04 +09:00
										 |  |  | 				p.o2 = o2; | 
					
						
							|  |  |  | 				p.he = he; | 
					
						
							| 
									
										
										
										
											2013-08-30 15:06:26 -03:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-11-08 21:22:59 +09:00
										 |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2013-08-30 13:27:15 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		editStop(index.row(), p); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-08-30 15:06:26 -03:00
										 |  |  | 	return QAbstractItemModel::setData(index, value, role); | 
					
						
							| 
									
										
										
										
											2013-08-30 07:14:30 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-26 09:14:19 -03:00
										 |  |  | QVariant DivePlannerPointsModel::headerData(int section, Qt::Orientation orientation, int role) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { | 
					
						
							|  |  |  | 		switch(section) { | 
					
						
							| 
									
										
										
										
											2013-08-26 09:14:19 -03:00
										 |  |  | 			case DEPTH: return tr("Final Depth"); | 
					
						
							|  |  |  | 			case DURATION: return tr("Duration"); | 
					
						
							|  |  |  | 			case GAS: return tr("Used Gas"); | 
					
						
							|  |  |  | 			case CCSETPOINT: return tr("CC Set Point"); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	} else if (role == Qt::FontRole) { | 
					
						
							| 
									
										
										
										
											2013-09-02 16:21:08 -03:00
										 |  |  | 		return  defaultModelFont(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-08-26 09:14:19 -03:00
										 |  |  | 	return QVariant(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-30 07:14:30 -03:00
										 |  |  | Qt::ItemFlags DivePlannerPointsModel::flags(const QModelIndex& index) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-26 09:14:19 -03:00
										 |  |  | int DivePlannerPointsModel::rowCount(const QModelIndex& parent) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | 	return divepoints.count(); | 
					
						
							| 
									
										
										
										
											2013-08-26 09:14:19 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 20:02:28 +01:00
										 |  |  | DivePlannerPointsModel::DivePlannerPointsModel(QObject* parent): QAbstractTableModel(parent), mode(NOTHING), stagingDive(NULL) | 
					
						
							| 
									
										
										
										
											2013-08-26 09:14:19 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-12-09 21:37:37 +01:00
										 |  |  | 	diveplan.dp = NULL; | 
					
						
							| 
									
										
										
										
											2013-08-26 09:14:19 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DivePlannerPointsModel* DivePlannerPointsModel::instance() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-30 09:18:04 -08:00
										 |  |  | 	static QScopedPointer<DivePlannerPointsModel> self(new DivePlannerPointsModel()); | 
					
						
							|  |  |  | 	return self.data(); | 
					
						
							| 
									
										
										
										
											2013-08-26 09:14:19 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | void DivePlannerPointsModel::setBottomSac(int sac) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	diveplan.bottomsac = sac; | 
					
						
							| 
									
										
										
										
											2013-09-09 07:27:46 -03:00
										 |  |  | 	emit dataChanged(createIndex(0, 0), createIndex(rowCount()-1, COLUMNS-1)); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DivePlannerPointsModel::setDecoSac(int sac) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	diveplan.decosac = sac; | 
					
						
							| 
									
										
										
										
											2013-09-09 07:27:46 -03:00
										 |  |  | 	emit dataChanged(createIndex(0, 0), createIndex(rowCount()-1, COLUMNS-1)); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-14 19:55:33 +01:00
										 |  |  | void DivePlannerPointsModel::setGFHigh(const int gfhigh) | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	diveplan.gfhigh = gfhigh; | 
					
						
							| 
									
										
										
										
											2013-09-09 07:27:46 -03:00
										 |  |  | 	emit dataChanged(createIndex(0, 0), createIndex(rowCount()-1, COLUMNS-1)); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-14 19:55:33 +01:00
										 |  |  | void DivePlannerPointsModel::setGFLow(const int ghflow) | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	diveplan.gflow = ghflow; | 
					
						
							| 
									
										
										
										
											2013-09-09 07:27:46 -03:00
										 |  |  | 	emit dataChanged(createIndex(0, 0), createIndex(rowCount()-1, COLUMNS-1)); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DivePlannerPointsModel::setSurfacePressure(int pressure) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	diveplan.surface_pressure = pressure; | 
					
						
							| 
									
										
										
										
											2013-09-09 07:27:46 -03:00
										 |  |  | 	emit dataChanged(createIndex(0, 0), createIndex(rowCount()-1, COLUMNS-1)); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DivePlannerPointsModel::setLastStop6m(bool value) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-10 06:08:42 +09:00
										 |  |  | 	set_last_stop(value); | 
					
						
							|  |  |  | 	emit dataChanged(createIndex(0, 0), createIndex(rowCount()-1, COLUMNS-1)); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DivePlannerPointsModel::setStartTime(const QTime& t) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-20 16:41:42 -07:00
										 |  |  | 	diveplan.when = (t.msec() + QDateTime::currentMSecsSinceEpoch()) / 1000 - gettimezoneoffset(); | 
					
						
							| 
									
										
										
										
											2013-09-09 07:27:46 -03:00
										 |  |  | 	emit dataChanged(createIndex(0, 0), createIndex(rowCount()-1, COLUMNS-1)); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | bool divePointsLessThan(const divedatapoint& p1, const divedatapoint& p2) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-08-26 17:02:34 -03:00
										 |  |  | 	return p1.time <= p2.time; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-12 11:19:04 +09:00
										 |  |  | bool DivePlannerPointsModel::addGas(int o2, int he) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (is_air(o2, he)) | 
					
						
							|  |  |  | 		o2 = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (int i = 0; i < MAX_CYLINDERS; i++) { | 
					
						
							| 
									
										
										
										
											2013-11-13 22:38:14 +09:00
										 |  |  | 		cylinder_t *cyl = &stagingDive->cylinder[i]; | 
					
						
							| 
									
										
										
										
											2013-11-12 11:19:04 +09:00
										 |  |  | 		if (cylinder_nodata(cyl)) { | 
					
						
							| 
									
										
										
										
											2013-11-13 21:44:18 +09:00
										 |  |  | 			fill_default_cylinder(cyl); | 
					
						
							| 
									
										
										
										
											2013-11-12 11:19:04 +09:00
										 |  |  | 			cyl->gasmix.o2.permille = o2; | 
					
						
							|  |  |  | 			cyl->gasmix.he.permille = he; | 
					
						
							|  |  |  | 			CylindersModel::instance()->setDive(stagingDive); | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (cyl->gasmix.o2.permille == o2 && cyl->gasmix.he.permille == he) | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	qDebug("too many gases"); | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-03 20:55:00 -08:00
										 |  |  | int DivePlannerPointsModel::addStop(int milimeters, int seconds, int o2, int he, int ccpoint) | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	int row = divepoints.count(); | 
					
						
							| 
									
										
										
										
											2013-12-03 20:55:00 -08:00
										 |  |  | 	if (seconds == 0 && milimeters == 0 && row != 0){ | 
					
						
							| 
									
										
										
										
											2013-11-15 11:54:13 +09:00
										 |  |  | 		/* this is only possible if the user clicked on the 'plus' sign on the DivePoints Table */ | 
					
						
							|  |  |  | 		struct divedatapoint& t = divepoints.last(); | 
					
						
							|  |  |  | 		milimeters = t.depth; | 
					
						
							| 
									
										
										
										
											2013-12-03 20:55:00 -08:00
										 |  |  | 		seconds = t.time + 600; // 10 minutes.
 | 
					
						
							| 
									
										
										
										
											2013-12-07 16:25:41 +01:00
										 |  |  | 		o2 = t.o2; | 
					
						
							|  |  |  | 		he = t.he; | 
					
						
							|  |  |  | 		ccpoint = t.po2; | 
					
						
							| 
									
										
										
										
											2013-12-03 20:55:00 -08:00
										 |  |  | 	} else if (seconds == 0 && milimeters == 0 && row == 0) { | 
					
						
							| 
									
										
										
										
											2013-11-15 11:54:13 +09:00
										 |  |  | 		milimeters = M_OR_FT(5, 15); // 5m / 15ft
 | 
					
						
							| 
									
										
										
										
											2013-12-03 20:55:00 -08:00
										 |  |  | 		seconds = 600; // 10 min
 | 
					
						
							| 
									
										
										
										
											2013-12-07 23:54:19 +01:00
										 |  |  | 		//Default to the first defined gas, if we got one.
 | 
					
						
							|  |  |  | 		cylinder_t *cyl = &stagingDive->cylinder[0]; | 
					
						
							|  |  |  | 		if (cyl) { | 
					
						
							|  |  |  | 			o2 = cyl->gasmix.o2.permille; | 
					
						
							|  |  |  | 			he = cyl->gasmix.he.permille; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-11-14 18:06:08 -02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-11-12 11:19:04 +09:00
										 |  |  | 	if (o2 != -1) | 
					
						
							|  |  |  | 		if (!addGas(o2, he)) | 
					
						
							|  |  |  | 			qDebug("addGas failed"); // FIXME add error propagation
 | 
					
						
							| 
									
										
										
										
											2013-09-09 22:57:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-26 16:55:30 -03:00
										 |  |  | 	// check if there's already a new stop before this one:
 | 
					
						
							| 
									
										
										
										
											2013-11-09 20:33:33 +09:00
										 |  |  | 	for (int i = 0; i < row; i++) { | 
					
						
							| 
									
										
										
										
											2013-08-26 17:02:34 -03:00
										 |  |  | 		const divedatapoint& dp = divepoints.at(i); | 
					
						
							| 
									
										
										
										
											2013-12-03 20:55:00 -08:00
										 |  |  | 		if (dp.time == seconds) { | 
					
						
							| 
									
										
										
										
											2013-11-08 21:22:59 +09:00
										 |  |  | 			row = i; | 
					
						
							|  |  |  | 			beginRemoveRows(QModelIndex(), row, row); | 
					
						
							|  |  |  | 			divepoints.remove(row); | 
					
						
							|  |  |  | 			endRemoveRows(); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-12-03 20:55:00 -08:00
										 |  |  | 		if (dp.time > seconds ) { | 
					
						
							| 
									
										
										
										
											2013-08-26 17:02:34 -03:00
										 |  |  | 			row = i; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2013-08-26 16:55:30 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-11-15 11:54:13 +09:00
										 |  |  | 	if (o2 == -1) { | 
					
						
							|  |  |  | 		if (row > 0) { | 
					
						
							|  |  |  | 			o2 = divepoints.at(row - 1).o2; | 
					
						
							|  |  |  | 			he = divepoints.at(row - 1).he; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			// when we add a first data point we need to make sure that there is a
 | 
					
						
							|  |  |  | 			// tank for it to use;
 | 
					
						
							|  |  |  | 			// first check to the right, then to the left, but if there's nothing,
 | 
					
						
							|  |  |  | 			// we simply default to AIR
 | 
					
						
							|  |  |  | 			if (row < divepoints.count()) { | 
					
						
							|  |  |  | 				o2 = divepoints.at(row).o2; | 
					
						
							|  |  |  | 				he = divepoints.at(row).he; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				o2 = O2_IN_AIR; | 
					
						
							|  |  |  | 				if (!addGas(o2, 0)) | 
					
						
							|  |  |  | 					qDebug("addGas failed"); // FIXME add error propagation
 | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-11-11 18:13:55 +09:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-08-26 16:55:30 -03:00
										 |  |  | 	// add the new stop
 | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | 	beginInsertRows(QModelIndex(), row, row); | 
					
						
							|  |  |  | 	divedatapoint point; | 
					
						
							| 
									
										
										
										
											2013-09-22 12:37:49 -07:00
										 |  |  | 	point.depth = milimeters; | 
					
						
							| 
									
										
										
										
											2013-12-03 20:55:00 -08:00
										 |  |  | 	point.time = seconds; | 
					
						
							| 
									
										
										
										
											2013-11-08 18:15:04 +09:00
										 |  |  | 	point.o2 = o2; | 
					
						
							|  |  |  | 	point.he = he; | 
					
						
							|  |  |  | 	point.po2 = ccpoint; | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | 	divepoints.append( point ); | 
					
						
							| 
									
										
										
										
											2013-08-26 17:02:34 -03:00
										 |  |  | 	std::sort(divepoints.begin(), divepoints.end(), divePointsLessThan); | 
					
						
							| 
									
										
										
										
											2013-08-26 13:18:21 -03:00
										 |  |  | 	endInsertRows(); | 
					
						
							|  |  |  | 	return row; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-26 14:54:07 -03:00
										 |  |  | void DivePlannerPointsModel::editStop(int row, divedatapoint newData) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	divepoints[row] = newData; | 
					
						
							| 
									
										
										
										
											2013-08-26 17:15:48 -03:00
										 |  |  | 	std::sort(divepoints.begin(), divepoints.end(), divePointsLessThan); | 
					
						
							|  |  |  | 	emit dataChanged(createIndex(0, 0), createIndex(rowCount()-1, COLUMNS-1)); | 
					
						
							| 
									
										
										
										
											2013-08-26 14:54:07 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 11:01:18 -07:00
										 |  |  | int DivePlannerPointsModel::size() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return divepoints.size(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-26 14:17:39 -03:00
										 |  |  | divedatapoint DivePlannerPointsModel::at(int row) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return divepoints.at(row); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-08-30 16:08:55 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-02 16:21:08 -03:00
										 |  |  | void DivePlannerPointsModel::remove(const QModelIndex& index) | 
					
						
							| 
									
										
										
										
											2013-08-30 16:08:55 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (index.column() != REMOVE) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	beginRemoveRows(QModelIndex(), index.row(), index.row()); | 
					
						
							|  |  |  | 	divepoints.remove(index.row()); | 
					
						
							|  |  |  | 	endRemoveRows(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-09-09 07:18:22 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct diveplan DivePlannerPointsModel::getDiveplan() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return diveplan; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-09-16 11:38:41 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | void DivePlannerPointsModel::cancelPlan() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	if (mode == PLAN && rowCount()) { | 
					
						
							| 
									
										
										
										
											2013-11-21 04:23:10 -08:00
										 |  |  | 		if (QMessageBox::warning(mainWindow(), TITLE_OR_TEXT(tr("Discard the Plan?"), | 
					
						
							|  |  |  | 			tr("You are about to discard your plan.")), | 
					
						
							| 
									
										
										
										
											2013-10-05 22:55:01 +02:00
										 |  |  | 			QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Discard) != QMessageBox::Discard) { | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-09-16 11:38:41 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-11-08 22:09:46 -02:00
										 |  |  | 	clear(); | 
					
						
							|  |  |  | 	emit planCanceled(); | 
					
						
							| 
									
										
										
										
											2013-11-12 14:21:50 +09:00
										 |  |  | 	if (mode != ADD) | 
					
						
							|  |  |  | 		free(stagingDive); | 
					
						
							| 
									
										
										
										
											2013-11-08 22:09:46 -02:00
										 |  |  | 	setPlanMode(NOTHING); | 
					
						
							| 
									
										
										
										
											2013-11-10 21:02:53 +09:00
										 |  |  | 	stagingDive = NULL; | 
					
						
							|  |  |  | 	CylindersModel::instance()->setDive(current_dive); | 
					
						
							| 
									
										
										
										
											2013-11-10 07:06:26 +09:00
										 |  |  | 	CylindersModel::instance()->update(); | 
					
						
							| 
									
										
										
										
											2013-11-08 22:09:46 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DivePlannerPointsModel::Mode DivePlannerPointsModel::currentMode() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return mode; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-09-16 11:38:41 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-13 21:45:54 +09:00
										 |  |  | QList<QPair<int, int> > DivePlannerPointsModel::collectGases(struct dive *d) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QList<QPair<int, int> > l; | 
					
						
							|  |  |  | 	for (int i = 0; i < MAX_CYLINDERS; i++) { | 
					
						
							|  |  |  | 		cylinder_t *cyl = &d->cylinder[i]; | 
					
						
							|  |  |  | 		if (!cylinder_nodata(cyl)) | 
					
						
							|  |  |  | 			l.push_back(QPair<int, int>(cyl->gasmix.o2.permille, cyl->gasmix.he.permille)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return l; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | void DivePlannerPointsModel::rememberTanks() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	oldGases = collectGases(stagingDive); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool DivePlannerPointsModel::tankInUse(int o2, int he) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for (int j = 0; j < rowCount(); j++) { | 
					
						
							|  |  |  | 		divedatapoint& p = divepoints[j]; | 
					
						
							|  |  |  | 		if (p.time == 0) // special entries that hold the available gases
 | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		if ((p.o2 == o2 && p.he == he) || | 
					
						
							|  |  |  | 		    (is_air(p.o2, p.he) && is_air(o2, he))) | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DivePlannerPointsModel::tanksUpdated() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-14 21:58:04 +09:00
										 |  |  | 	// we don't know exactly what changed - what we care about is
 | 
					
						
							|  |  |  | 	// "did a gas change on us". So we look through the diveplan to
 | 
					
						
							|  |  |  | 	// see if there is a gas that is now missing and if there is, we
 | 
					
						
							|  |  |  | 	// replace it with the matching new gas.
 | 
					
						
							|  |  |  | 	QList<QPair<int,int> > gases = collectGases(stagingDive); | 
					
						
							|  |  |  | 	if (gases.length() == oldGases.length()) { | 
					
						
							|  |  |  | 		// either nothing relevant changed, or exactly ONE gasmix changed
 | 
					
						
							|  |  |  | 		for (int i = 0; i < gases.length(); i++) { | 
					
						
							|  |  |  | 			if (gases.at(i) != oldGases.at(i)) { | 
					
						
							|  |  |  | 				if (oldGases.count(oldGases.at(i)) > 1) { | 
					
						
							|  |  |  | 					// we had this gas more than once, so don't
 | 
					
						
							|  |  |  | 					// change segments that used this gas as it still exists
 | 
					
						
							| 
									
										
										
										
											2013-11-13 21:45:54 +09:00
										 |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-11-14 21:58:04 +09:00
										 |  |  | 				for (int j = 0; j < rowCount(); j++) { | 
					
						
							|  |  |  | 					divedatapoint& p = divepoints[j]; | 
					
						
							|  |  |  | 					int o2 = oldGases.at(i).first; | 
					
						
							|  |  |  | 					int he = oldGases.at(i).second; | 
					
						
							|  |  |  | 					if ((p.o2 == o2 && p.he == he) || | 
					
						
							|  |  |  | 					    (is_air(p.o2, p.he) && (is_air(o2, he) || (o2 == 0 && he == 0)))) { | 
					
						
							|  |  |  | 						p.o2 = gases.at(i).first; | 
					
						
							|  |  |  | 						p.he = gases.at(i).second; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2013-11-13 21:45:54 +09:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-11-14 21:58:04 +09:00
										 |  |  | 	emit dataChanged(createIndex(0, 0), createIndex(rowCount()-1, COLUMNS-1)); | 
					
						
							| 
									
										
										
										
											2013-11-13 21:45:54 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-08 22:09:46 -02:00
										 |  |  | void DivePlannerPointsModel::clear() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-11-12 14:21:50 +09:00
										 |  |  | 	if (mode == ADD) { | 
					
						
							|  |  |  | 		stagingDive = current_dive; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		if (!stagingDive) | 
					
						
							|  |  |  | 			stagingDive = alloc_dive(); | 
					
						
							|  |  |  | 		memset(stagingDive->cylinder, 0, MAX_CYLINDERS * sizeof(cylinder_t)); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-11-12 11:19:04 +09:00
										 |  |  | 	CylindersModel::instance()->setDive(stagingDive); | 
					
						
							| 
									
										
										
										
											2013-12-10 01:02:06 +02:00
										 |  |  | 	if (rowCount() > 0) { | 
					
						
							|  |  |  | 		beginRemoveRows(QModelIndex(), 0, rowCount() - 1); | 
					
						
							|  |  |  | 		divepoints.clear(); | 
					
						
							|  |  |  | 		endRemoveRows(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-11-10 21:02:53 +09:00
										 |  |  | 	CylindersModel::instance()->clear(); | 
					
						
							| 
									
										
										
										
											2013-09-16 11:38:41 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-09-16 12:11:06 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-08 22:09:46 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-16 12:11:06 -03:00
										 |  |  | void DivePlannerPointsModel::createTemporaryPlan() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// This needs to be done in the following steps:
 | 
					
						
							|  |  |  | 	// Get the user-input and calculate the dive info
 | 
					
						
							|  |  |  | 	// Not sure if this is the place to create the diveplan...
 | 
					
						
							|  |  |  | 	// We just start with a surface node at time = 0
 | 
					
						
							| 
									
										
										
										
											2013-12-09 07:43:00 +01:00
										 |  |  | 	if (!stagingDive) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-07 23:54:15 +01:00
										 |  |  | 	diveplan.dp = NULL; | 
					
						
							| 
									
										
										
										
											2013-09-16 12:11:06 -03:00
										 |  |  | 	int lastIndex = -1; | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	for (int i = 0; i < rowCount(); i++) { | 
					
						
							| 
									
										
										
										
											2013-09-16 12:11:06 -03:00
										 |  |  | 		divedatapoint p = at(i); | 
					
						
							|  |  |  | 		int deltaT = lastIndex != -1 ? p.time - at(lastIndex).time : p.time; | 
					
						
							|  |  |  | 		lastIndex = i; | 
					
						
							| 
									
										
										
										
											2013-12-07 23:54:15 +01:00
										 |  |  | 		plan_add_segment(&diveplan, deltaT, p.depth, p.o2, p.he, p.po2); | 
					
						
							| 
									
										
										
										
											2013-09-16 12:11:06 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	char *cache = NULL; | 
					
						
							|  |  |  | 	tempDive = NULL; | 
					
						
							| 
									
										
										
										
											2013-10-06 08:55:58 -07:00
										 |  |  | 	const char *errorString = NULL; | 
					
						
							| 
									
										
										
										
											2013-12-07 23:54:15 +01:00
										 |  |  | 	struct divedatapoint *dp = NULL; | 
					
						
							| 
									
										
										
										
											2013-11-12 11:19:04 +09:00
										 |  |  | 	for (int i = 0; i < MAX_CYLINDERS; i++) { | 
					
						
							|  |  |  | 		cylinder_t *cyl = &stagingDive->cylinder[i]; | 
					
						
							|  |  |  | 		if (cyl->depth.mm) { | 
					
						
							|  |  |  | 			dp = create_dp(0, cyl->depth.mm, cyl->gasmix.o2.permille, cyl->gasmix.he.permille, 0); | 
					
						
							| 
									
										
										
										
											2013-12-19 22:11:31 +01:00
										 |  |  | 			if (diveplan.dp) | 
					
						
							|  |  |  | 				dp->next = diveplan.dp->next; | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				dp->next = NULL; | 
					
						
							| 
									
										
										
										
											2013-11-12 11:19:04 +09:00
										 |  |  | 			diveplan.dp->next = dp; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #if DEBUG_PLAN
 | 
					
						
							|  |  |  | 	dump_plan(&diveplan); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2013-09-18 22:40:34 -05:00
										 |  |  | 	plan(&diveplan, &cache, &tempDive, isPlanner(), &errorString); | 
					
						
							| 
									
										
										
										
											2013-11-07 17:25:42 +09:00
										 |  |  | 	if (mode == ADD) { | 
					
						
							| 
									
										
										
										
											2013-11-13 21:45:54 +09:00
										 |  |  | 		// copy the samples and events, but don't overwrite the cylinders
 | 
					
						
							| 
									
										
										
										
											2013-09-18 23:33:39 -05:00
										 |  |  | 		copy_samples(tempDive, current_dive); | 
					
						
							| 
									
										
										
										
											2013-11-08 18:15:04 +09:00
										 |  |  | 		copy_events(tempDive, current_dive); | 
					
						
							| 
									
										
										
										
											2013-11-07 17:25:42 +09:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-09-16 12:11:06 -03:00
										 |  |  | #if DEBUG_PLAN
 | 
					
						
							|  |  |  | 	dump_plan(&diveplan); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DivePlannerPointsModel::deleteTemporaryPlan() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	deleteTemporaryPlan(diveplan.dp); | 
					
						
							|  |  |  | 	delete_single_dive(get_divenr(tempDive)); | 
					
						
							|  |  |  | 	tempDive = NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DivePlannerPointsModel::deleteTemporaryPlan(struct divedatapoint *dp) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	if (!dp) | 
					
						
							| 
									
										
										
										
											2013-09-16 12:11:06 -03:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	deleteTemporaryPlan(dp->next); | 
					
						
							|  |  |  | 	free(dp); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-09-16 12:30:58 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | void DivePlannerPointsModel::createPlan() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// Ok, so, here the diveplan creates a dive,
 | 
					
						
							|  |  |  | 	// puts it on the dive list, and we need to remember
 | 
					
						
							|  |  |  | 	// to not delete it later. mumble. ;p
 | 
					
						
							| 
									
										
										
										
											2013-09-22 12:34:50 -07:00
										 |  |  | 	char *cache = NULL; | 
					
						
							| 
									
										
										
										
											2013-09-16 12:30:58 -03:00
										 |  |  | 	tempDive = NULL; | 
					
						
							| 
									
										
										
										
											2013-10-06 08:55:58 -07:00
										 |  |  | 	const char *errorString = NULL; | 
					
						
							| 
									
										
										
										
											2013-09-16 12:30:58 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	createTemporaryPlan(); | 
					
						
							| 
									
										
										
										
											2013-09-18 22:40:34 -05:00
										 |  |  | 	plan(&diveplan, &cache, &tempDive, isPlanner(), &errorString); | 
					
						
							| 
									
										
										
										
											2013-12-07 23:54:18 +01:00
										 |  |  | 	copy_cylinders(stagingDive, tempDive); | 
					
						
							|  |  |  | 	int mean[MAX_CYLINDERS], duration[MAX_CYLINDERS]; | 
					
						
							|  |  |  | 	per_cylinder_mean_depth(tempDive, select_dc(&tempDive->dc), mean, duration); | 
					
						
							|  |  |  | 	for (int i = 0; i < MAX_CYLINDERS; i++) { | 
					
						
							|  |  |  | 		cylinder_t *cyl = tempDive->cylinder+i; | 
					
						
							|  |  |  | 		if (cylinder_none(cyl)) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		// FIXME: The epic assumption that all the cylinders after the first is deco
 | 
					
						
							|  |  |  | 		int sac = i ? diveplan.decosac : diveplan.bottomsac; | 
					
						
							|  |  |  | 		cyl->start.mbar = cyl->type.workingpressure.mbar; | 
					
						
							|  |  |  | 		int consumption = ((depth_to_mbar(mean[i], tempDive) * duration[i] / 60) * sac) / ( cyl->type.size.mliter / 1000); | 
					
						
							|  |  |  | 		cyl->end.mbar = cyl->start.mbar - consumption; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-16 12:30:58 -03:00
										 |  |  | 	mark_divelist_changed(TRUE); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-16 19:21:13 -03:00
										 |  |  | 	// Remove and clean the diveplan, so we don't delete
 | 
					
						
							|  |  |  | 	// the dive by mistake.
 | 
					
						
							|  |  |  | 	diveplan.dp = NULL; | 
					
						
							| 
									
										
										
										
											2013-11-08 22:09:46 -02:00
										 |  |  | 	clear(); | 
					
						
							| 
									
										
										
										
											2013-09-16 12:30:58 -03:00
										 |  |  | 	planCreated(); | 
					
						
							| 
									
										
										
										
											2013-11-08 22:09:46 -02:00
										 |  |  | 	setPlanMode(NOTHING); | 
					
						
							| 
									
										
										
										
											2013-11-10 21:02:53 +09:00
										 |  |  | 	free(stagingDive); | 
					
						
							|  |  |  | 	stagingDive = NULL; | 
					
						
							|  |  |  | 	CylindersModel::instance()->setDive(current_dive); | 
					
						
							| 
									
										
										
										
											2013-11-10 07:06:26 +09:00
										 |  |  | 	CylindersModel::instance()->update(); | 
					
						
							| 
									
										
										
										
											2013-09-16 12:30:58 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-11-17 21:20:07 -02:00
										 |  |  | 
 | 
					
						
							|  |  |  | ExpanderGraphics::ExpanderGraphics(QGraphicsItem* parent): QGraphicsRectItem(parent) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	icon = new QGraphicsPixmapItem(this); | 
					
						
							|  |  |  | 	bg = new QGraphicsPixmapItem(this); | 
					
						
							|  |  |  | 	leftWing = new QGraphicsPixmapItem(this); | 
					
						
							|  |  |  | 	rightWing = new QGraphicsPixmapItem(this); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QPixmap p; | 
					
						
							|  |  |  | 	#define CREATE(item, pixmap) \
 | 
					
						
							|  |  |  | 	p = QPixmap(QString( pixmap ));\ | 
					
						
							|  |  |  | 	item->setPixmap(p); \ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CREATE(icon, ":icon_time"); | 
					
						
							|  |  |  | 	CREATE(bg, ":round_base"); | 
					
						
							|  |  |  | 	CREATE(leftWing, ":left_wing"); | 
					
						
							|  |  |  | 	CREATE(rightWing, ":right_wing"); | 
					
						
							|  |  |  | 	#undef CREATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	decreaseBtn = new Button(0, this); | 
					
						
							|  |  |  | 	increaseBtn = new Button(0, this); | 
					
						
							|  |  |  | 	decreaseBtn->setPixmap(QPixmap(":arrow_down")); | 
					
						
							|  |  |  | 	increaseBtn->setPixmap(QPixmap(":arrow_up")); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setFlag(ItemIgnoresTransformations); | 
					
						
							|  |  |  | 	leftWing->setZValue(-2); | 
					
						
							|  |  |  | 	rightWing->setZValue(-2); | 
					
						
							|  |  |  | 	bg->setZValue(-1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	leftWing->setPos(0,0); | 
					
						
							|  |  |  | 	bg->setPos(leftWing->pos().x() + leftWing->boundingRect().width() -60, 5); | 
					
						
							|  |  |  | 	rightWing->setPos(leftWing->pos().x() + leftWing->boundingRect().width() - 20, 0); | 
					
						
							|  |  |  | 	decreaseBtn->setPos(leftWing->pos().x(), leftWing->pos().y() ); | 
					
						
							|  |  |  | 	increaseBtn->setPos(rightWing->pos().x(), rightWing->pos().y() ); | 
					
						
							|  |  |  | 	icon->setPos(bg->pos().x(), bg->pos().y() - 5); | 
					
						
							| 
									
										
										
										
											2013-11-18 21:13:05 -02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//I need to bottom align the items, I need to make the 0,0 ( orgin ) to be
 | 
					
						
							|  |  |  | 	// the bottom of this item, so shift everything up.
 | 
					
						
							|  |  |  | 	QRectF r = childrenBoundingRect(); | 
					
						
							|  |  |  | 	Q_FOREACH(QGraphicsItem *i, childItems()){ | 
					
						
							|  |  |  | 		i->setPos(i->pos().x(), i->pos().y() - r.height()); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-11-18 17:01:58 -08:00
										 |  |  | 	setScale(0.7); | 
					
						
							| 
									
										
										
										
											2013-11-17 21:20:07 -02:00
										 |  |  | } |