| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							|  |  |  | #include "TabDiveEquipment.h"
 | 
					
						
							|  |  |  | #include "maintab.h"
 | 
					
						
							|  |  |  | #include "desktop-widgets/simplewidgets.h" // For isGnome3Session()
 | 
					
						
							|  |  |  | #include "desktop-widgets/modeldelegates.h"
 | 
					
						
							| 
									
										
										
										
											2021-09-02 19:11:35 +02:00
										 |  |  | #include "core/dive.h"
 | 
					
						
							|  |  |  | #include "core/selection.h"
 | 
					
						
							| 
									
										
										
										
											2019-11-13 15:08:40 +01:00
										 |  |  | #include "commands/command.h"
 | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "qt-models/cylindermodel.h"
 | 
					
						
							|  |  |  | #include "qt-models/weightmodel.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-02 19:11:35 +02:00
										 |  |  | #include <QMessageBox>
 | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | #include <QSettings>
 | 
					
						
							| 
									
										
										
										
											2019-07-20 20:53:06 +02:00
										 |  |  | #include <QCompleter>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-20 12:22:40 +01:00
										 |  |  | static bool ignoreHiddenFlag(int i) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return i == CylindersModel::REMOVE || i == CylindersModel::TYPE || | 
					
						
							|  |  |  | 	       i == CylindersModel::WORKINGPRESS_INT || i == CylindersModel::SIZE_INT; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | TabDiveEquipment::TabDiveEquipment(QWidget *parent) : TabBase(parent), | 
					
						
							| 
									
										
										
										
											2021-11-20 09:37:28 +01:00
										 |  |  | 	cylindersModel(new CylindersModel(false, true, this)), | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | 	weightModel(new WeightModel(this)) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-20 20:53:06 +02:00
										 |  |  | 	QCompleter *suitCompleter; | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | 	ui.setupUi(this); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// This makes sure we only delete the models
 | 
					
						
							|  |  |  | 	// after the destructor of the tables,
 | 
					
						
							|  |  |  | 	// this is needed to save the column sizes.
 | 
					
						
							|  |  |  | 	cylindersModel->setParent(ui.cylinders); | 
					
						
							|  |  |  | 	weightModel->setParent(ui.weights); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ui.cylinders->setModel(cylindersModel); | 
					
						
							|  |  |  | 	ui.weights->setModel(weightModel); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-20 20:53:06 +02:00
										 |  |  | 	connect(&diveListNotifier, &DiveListNotifier::divesChanged, this, &TabDiveEquipment::divesChanged); | 
					
						
							| 
									
										
										
										
											2019-11-02 22:52:27 +01:00
										 |  |  | 	connect(ui.cylinders, &TableView::itemClicked, this, &TabDiveEquipment::editCylinderWidget); | 
					
						
							|  |  |  | 	connect(ui.weights, &TableView::itemClicked, this, &TabDiveEquipment::editWeightWidget); | 
					
						
							| 
									
										
										
										
											2021-11-20 09:37:28 +01:00
										 |  |  | 	connect(cylindersModel, &CylindersModel::divesEdited, this, &TabDiveEquipment::divesEdited); | 
					
						
							| 
									
										
										
										
											2020-03-27 21:49:19 +01:00
										 |  |  | 	connect(weightModel, &WeightModel::divesEdited, this, &TabDiveEquipment::divesEdited); | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Current display of things on Gnome3 looks like shit, so
 | 
					
						
							|  |  |  | 	// let's fix that.
 | 
					
						
							|  |  |  | 	if (isGnome3Session()) { | 
					
						
							|  |  |  | 		QPalette p; | 
					
						
							|  |  |  | 		p.setColor(QPalette::Window, QColor(Qt::white)); | 
					
						
							|  |  |  | 		ui.scrollArea->viewport()->setPalette(p); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ui.cylinders->view()->setItemDelegateForColumn(CylindersModel::TYPE, new TankInfoDelegate(this)); | 
					
						
							|  |  |  | 	ui.cylinders->view()->setItemDelegateForColumn(CylindersModel::USE, new TankUseDelegate(this)); | 
					
						
							|  |  |  | 	ui.weights->view()->setItemDelegateForColumn(WeightModel::TYPE, new WSInfoDelegate(this)); | 
					
						
							|  |  |  | 	ui.cylinders->view()->setColumnHidden(CylindersModel::DEPTH, true); | 
					
						
							| 
									
										
										
										
											2021-11-20 12:22:40 +01:00
										 |  |  | 	ui.cylinders->view()->setColumnHidden(CylindersModel::WORKINGPRESS_INT, true); | 
					
						
							|  |  |  | 	ui.cylinders->view()->setColumnHidden(CylindersModel::SIZE_INT, true); | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ui.cylinders->setTitle(tr("Cylinders")); | 
					
						
							|  |  |  | 	ui.cylinders->setBtnToolTip(tr("Add cylinder")); | 
					
						
							|  |  |  | 	connect(ui.cylinders, &TableView::addButtonClicked, this, &TabDiveEquipment::addCylinder_clicked); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ui.weights->setTitle(tr("Weights")); | 
					
						
							|  |  |  | 	ui.weights->setBtnToolTip(tr("Add weight system")); | 
					
						
							|  |  |  | 	connect(ui.weights, &TableView::addButtonClicked, this, &TabDiveEquipment::addWeight_clicked); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-20 20:53:06 +02:00
										 |  |  | 	QAction *action = new QAction(tr("OK"), this); | 
					
						
							|  |  |  | 	connect(action, &QAction::triggered, this, &TabDiveEquipment::closeWarning); | 
					
						
							|  |  |  | 	ui.multiDiveWarningMessage->addAction(action); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	action = new QAction(tr("Undo"), this); | 
					
						
							|  |  |  | 	connect(action, &QAction::triggered, Command::undoAction(this), &QAction::trigger); | 
					
						
							|  |  |  | 	connect(action, &QAction::triggered, this, &TabDiveEquipment::closeWarning); | 
					
						
							|  |  |  | 	ui.multiDiveWarningMessage->addAction(action); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ui.multiDiveWarningMessage->hide(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | 	QSettings s; | 
					
						
							|  |  |  | 	s.beginGroup("cylinders_dialog"); | 
					
						
							|  |  |  | 	for (int i = 0; i < CylindersModel::COLUMNS; i++) { | 
					
						
							| 
									
										
										
										
											2021-11-20 12:22:40 +01:00
										 |  |  | 		if (ignoreHiddenFlag(i)) | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | 			continue; | 
					
						
							|  |  |  | 		bool checked = s.value(QString("column%1_hidden").arg(i)).toBool(); | 
					
						
							|  |  |  | 		QAction *action = new QAction(cylindersModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString(), ui.cylinders->view()); | 
					
						
							|  |  |  | 		action->setCheckable(true); | 
					
						
							|  |  |  | 		action->setData(i); | 
					
						
							|  |  |  | 		action->setChecked(!checked); | 
					
						
							|  |  |  | 		connect(action, &QAction::triggered, this, &TabDiveEquipment::toggleTriggeredColumn); | 
					
						
							|  |  |  | 		ui.cylinders->view()->setColumnHidden(i, checked); | 
					
						
							|  |  |  | 		ui.cylinders->view()->horizontalHeader()->addAction(action); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ui.cylinders->view()->horizontalHeader()->setContextMenuPolicy(Qt::ActionsContextMenu); | 
					
						
							|  |  |  | 	ui.weights->view()->horizontalHeader()->setContextMenuPolicy(Qt::ActionsContextMenu); | 
					
						
							| 
									
										
										
										
											2019-07-20 20:53:06 +02:00
										 |  |  | 	suitCompleter = new QCompleter(&suitModel, ui.suit); | 
					
						
							|  |  |  | 	suitCompleter->setCaseSensitivity(Qt::CaseInsensitive); | 
					
						
							|  |  |  | 	ui.suit->setCompleter(suitCompleter); | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TabDiveEquipment::~TabDiveEquipment() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QSettings s; | 
					
						
							|  |  |  | 	s.beginGroup("cylinders_dialog"); | 
					
						
							|  |  |  | 	for (int i = 0; i < CylindersModel::COLUMNS; i++) { | 
					
						
							| 
									
										
										
										
											2021-11-20 12:22:40 +01:00
										 |  |  | 		if (ignoreHiddenFlag(i)) | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | 			continue; | 
					
						
							|  |  |  | 		s.setValue(QString("column%1_hidden").arg(i), ui.cylinders->view()->isColumnHidden(i)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-20 20:53:06 +02:00
										 |  |  | // This function gets called if a field gets updated by an undo command.
 | 
					
						
							|  |  |  | // Refresh the corresponding UI field.
 | 
					
						
							|  |  |  | void TabDiveEquipment::divesChanged(const QVector<dive *> &dives, DiveField field) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// If the current dive is not in list of changed dives, do nothing
 | 
					
						
							|  |  |  | 	if (!current_dive || !dives.contains(current_dive)) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-13 12:44:39 +02:00
										 |  |  | 	if (field.suit) | 
					
						
							| 
									
										
										
										
											2019-07-20 20:53:06 +02:00
										 |  |  | 		ui.suit->setText(QString(current_dive->suit)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | void TabDiveEquipment::toggleTriggeredColumn() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QAction *action = qobject_cast<QAction *>(sender()); | 
					
						
							|  |  |  | 	int col = action->data().toInt(); | 
					
						
							|  |  |  | 	QTableView *view = ui.cylinders->view(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (action->isChecked()) { | 
					
						
							|  |  |  | 		view->showColumn(col); | 
					
						
							|  |  |  | 		if (view->columnWidth(col) <= 15) | 
					
						
							|  |  |  | 			view->setColumnWidth(col, 80); | 
					
						
							| 
									
										
										
										
											2021-11-20 12:22:40 +01:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | 		view->hideColumn(col); | 
					
						
							| 
									
										
										
										
											2021-11-20 12:22:40 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TabDiveEquipment::updateData() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-23 12:44:09 +01:00
										 |  |  | 	cylindersModel->updateDive(current_dive); | 
					
						
							| 
									
										
										
										
											2019-11-02 18:47:56 +01:00
										 |  |  | 	weightModel->updateDive(current_dive); | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 19:04:29 +02:00
										 |  |  | 	bool is_ccr = current_dive && get_dive_dc(current_dive, dc_number)->divemode == CCR; | 
					
						
							|  |  |  | 	if (is_ccr) | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | 		ui.cylinders->view()->showColumn(CylindersModel::USE); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		ui.cylinders->view()->hideColumn(CylindersModel::USE); | 
					
						
							| 
									
										
										
										
											2019-07-20 20:53:06 +02:00
										 |  |  | 	if (current_dive && current_dive->suit) | 
					
						
							|  |  |  | 		ui.suit->setText(QString(current_dive->suit)); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		ui.suit->clear(); | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TabDiveEquipment::clear() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	cylindersModel->clear(); | 
					
						
							|  |  |  | 	weightModel->clear(); | 
					
						
							| 
									
										
										
										
											2019-07-20 20:53:06 +02:00
										 |  |  | 	ui.suit->clear(); | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TabDiveEquipment::addCylinder_clicked() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-27 20:42:13 +01:00
										 |  |  | 	divesEdited(Command::addCylinder(false)); | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TabDiveEquipment::addWeight_clicked() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-11-02 21:19:29 +01:00
										 |  |  | 	divesEdited(Command::addWeight(false)); | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TabDiveEquipment::editCylinderWidget(const QModelIndex &index) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-24 11:14:55 +01:00
										 |  |  | 	if (!index.isValid()) | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2020-02-24 11:14:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-02 19:11:35 +02:00
										 |  |  | 	if (index.column() == CylindersModel::REMOVE) { | 
					
						
							|  |  |  | 		for (dive *d: getDiveSelection()) { | 
					
						
							| 
									
										
										
										
											2021-11-20 09:37:28 +01:00
										 |  |  | 			if (cylinder_with_sensor_sample(d, index.row())) { | 
					
						
							| 
									
										
										
										
											2021-09-02 19:11:35 +02:00
										 |  |  | 				if (QMessageBox::warning(this, tr("Remove cylinder?"), | 
					
						
							|  |  |  | 							 tr("The deleted cylinder has sensor readings, which will be lost.\n" | 
					
						
							|  |  |  | 							    "Do you want to continue?"), | 
					
						
							|  |  |  | 							 QMessageBox::Yes|QMessageBox::No) != QMessageBox::Yes) | 
					
						
							|  |  |  | 					return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-11-20 09:37:28 +01:00
										 |  |  | 		divesEdited(Command::removeCylinder(index.row(), false)); | 
					
						
							| 
									
										
										
										
											2021-09-02 19:11:35 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | 		ui.cylinders->edit(index); | 
					
						
							| 
									
										
										
										
											2021-09-02 19:11:35 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TabDiveEquipment::editWeightWidget(const QModelIndex &index) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-11-03 15:04:48 +01:00
										 |  |  | 	if (!index.isValid()) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-08 22:47:38 +01:00
										 |  |  | 	if (index.column() == WeightModel::REMOVE) | 
					
						
							| 
									
										
										
										
											2019-11-03 15:04:48 +01:00
										 |  |  | 		divesEdited(Command::removeWeight(index.row(), false)); | 
					
						
							| 
									
										
										
										
											2019-11-08 22:47:38 +01:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2019-04-13 17:43:45 +02:00
										 |  |  | 		ui.weights->edit(index); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-20 20:53:06 +02:00
										 |  |  | void TabDiveEquipment::divesEdited(int i) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// No warning if only one dive was edited
 | 
					
						
							|  |  |  | 	if (i <= 1) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	ui.multiDiveWarningMessage->setCloseButtonVisible(false); | 
					
						
							|  |  |  | 	ui.multiDiveWarningMessage->setText(tr("Warning: edited %1 dives").arg(i)); | 
					
						
							|  |  |  | 	ui.multiDiveWarningMessage->show(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TabDiveEquipment::on_suit_editingFinished() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (!current_dive) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	divesEdited(Command::editSuit(ui.suit->text(), false)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TabDiveEquipment::closeWarning() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ui.multiDiveWarningMessage->hide(); | 
					
						
							|  |  |  | } |