| 
									
										
										
										
											2017-04-27 20:26:05 +02:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							| 
									
										
										
										
											2016-04-04 22:02:03 -07:00
										 |  |  | #include "desktop-widgets/locationinformation.h"
 | 
					
						
							|  |  |  | #include "core/dive.h"
 | 
					
						
							|  |  |  | #include "desktop-widgets/mainwindow.h"
 | 
					
						
							|  |  |  | #include "desktop-widgets/divelistview.h"
 | 
					
						
							|  |  |  | #include "core/qthelper.h"
 | 
					
						
							|  |  |  | #include "desktop-widgets/globe.h"
 | 
					
						
							|  |  |  | #include "qt-models/filtermodels.h"
 | 
					
						
							|  |  |  | #include "qt-models/divelocationmodel.h"
 | 
					
						
							|  |  |  | #include "core/divesitehelpers.h"
 | 
					
						
							|  |  |  | #include "desktop-widgets/modeldelegates.h"
 | 
					
						
							| 
									
										
										
										
											2015-08-25 18:45:29 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | #include <QDebug>
 | 
					
						
							|  |  |  | #include <QShowEvent>
 | 
					
						
							| 
									
										
										
										
											2015-08-31 21:11:28 -03:00
										 |  |  | #include <QItemSelectionModel>
 | 
					
						
							| 
									
										
										
										
											2015-08-31 22:01:25 -03:00
										 |  |  | #include <qmessagebox.h>
 | 
					
						
							| 
									
										
										
										
											2015-08-31 21:35:17 -03:00
										 |  |  | #include <cstdlib>
 | 
					
						
							| 
									
										
										
										
											2015-09-21 16:08:58 -03:00
										 |  |  | #include <QDesktopWidget>
 | 
					
						
							|  |  |  | #include <QScrollBar>
 | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | LocationInformationWidget::LocationInformationWidget(QWidget *parent) : QGroupBox(parent), modified(false) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ui.setupUi(this); | 
					
						
							|  |  |  | 	ui.diveSiteMessage->setCloseButtonVisible(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	acceptAction = new QAction(tr("Apply changes"), this); | 
					
						
							|  |  |  | 	connect(acceptAction, SIGNAL(triggered(bool)), this, SLOT(acceptChanges())); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rejectAction = new QAction(tr("Discard changes"), this); | 
					
						
							|  |  |  | 	connect(rejectAction, SIGNAL(triggered(bool)), this, SLOT(rejectChanges())); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ui.diveSiteMessage->setText(tr("Dive site management")); | 
					
						
							| 
									
										
										
										
											2015-06-04 00:18:25 -03:00
										 |  |  | 	ui.diveSiteMessage->addAction(acceptAction); | 
					
						
							|  |  |  | 	ui.diveSiteMessage->addAction(rejectAction); | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:42:45 -03:00
										 |  |  | 	connect(this, SIGNAL(startFilterDiveSite(uint32_t)), MultiFilterSortModel::instance(), SLOT(startFilterDiveSite(uint32_t))); | 
					
						
							|  |  |  | 	connect(this, SIGNAL(stopFilterDiveSite()), MultiFilterSortModel::instance(), SLOT(stopFilterDiveSite())); | 
					
						
							| 
									
										
										
										
											2015-08-25 18:45:29 -03:00
										 |  |  | 	connect(ui.geoCodeButton, SIGNAL(clicked()), this, SLOT(reverseGeocode())); | 
					
						
							| 
									
										
										
										
											2015-07-30 22:10:01 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 20:59:13 -03:00
										 |  |  | 	SsrfSortFilterProxyModel *filter_model = new SsrfSortFilterProxyModel(this); | 
					
						
							|  |  |  | 	filter_model->setSourceModel(LocationInformationModel::instance()); | 
					
						
							|  |  |  | 	filter_model->setFilterRow(filter_same_gps_cb); | 
					
						
							|  |  |  | 	ui.diveSiteListView->setModel(filter_model); | 
					
						
							| 
									
										
										
										
											2015-08-31 21:03:31 -03:00
										 |  |  | 	ui.diveSiteListView->setModelColumn(LocationInformationModel::NAME); | 
					
						
							| 
									
										
										
										
											2015-08-31 21:11:28 -03:00
										 |  |  | 	ui.diveSiteListView->installEventFilter(this); | 
					
						
							| 
									
										
										
										
											2015-07-31 11:15:35 -07:00
										 |  |  | #ifndef NO_MARBLE
 | 
					
						
							| 
									
										
										
										
											2015-07-30 22:10:01 -03:00
										 |  |  | 	// Globe Management Code.
 | 
					
						
							|  |  |  | 	connect(this, &LocationInformationWidget::requestCoordinates, | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 		GlobeGPS::instance(), &GlobeGPS::prepareForGetDiveCoordinates); | 
					
						
							| 
									
										
										
										
											2015-07-30 22:10:01 -03:00
										 |  |  | 	connect(this, &LocationInformationWidget::endRequestCoordinates, | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 		GlobeGPS::instance(), &GlobeGPS::endGetDiveCoordinates); | 
					
						
							| 
									
										
										
										
											2015-07-30 22:10:01 -03:00
										 |  |  | 	connect(GlobeGPS::instance(), &GlobeGPS::coordinatesChanged, | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 		this, &LocationInformationWidget::updateGpsCoordinates); | 
					
						
							| 
									
										
										
										
											2015-07-30 22:47:17 -03:00
										 |  |  | 	connect(this, &LocationInformationWidget::endEditDiveSite, | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 		GlobeGPS::instance(), &GlobeGPS::repopulateLabels); | 
					
						
							| 
									
										
										
										
											2015-07-31 11:15:35 -07:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-05-17 17:14:23 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | bool LocationInformationWidget::eventFilter(QObject *, QEvent *ev) | 
					
						
							| 
									
										
										
										
											2015-08-31 21:11:28 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	if (ev->type() == QEvent::ContextMenu) { | 
					
						
							|  |  |  | 		QContextMenuEvent *ctx = (QContextMenuEvent *)ev; | 
					
						
							| 
									
										
										
										
											2015-09-04 12:23:19 +02:00
										 |  |  | 		QMenu contextMenu; | 
					
						
							|  |  |  | 		contextMenu.addAction(tr("Merge into current site"), this, SLOT(mergeSelectedDiveSites())); | 
					
						
							|  |  |  | 		contextMenu.exec(ctx->globalPos()); | 
					
						
							|  |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2015-08-31 21:11:28 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-31 19:24:36 -07:00
										 |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2015-08-31 21:11:28 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | void LocationInformationWidget::mergeSelectedDiveSites() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-08-31 22:01:25 -03:00
										 |  |  | 	if (QMessageBox::warning(MainWindow::instance(), tr("Merging dive sites"), | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 				 tr("You are about to merge dive sites, you can't undo that action \n Are you sure you want to continue?"), | 
					
						
							|  |  |  | 				 QMessageBox::Ok, QMessageBox::Cancel) != QMessageBox::Ok) | 
					
						
							| 
									
										
										
										
											2015-08-31 22:01:25 -03:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-31 21:35:17 -03:00
										 |  |  | 	QModelIndexList selection = ui.diveSiteListView->selectionModel()->selectedIndexes(); | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	uint32_t *selected_dive_sites = (uint32_t *)malloc(sizeof(uint32_t) * selection.count()); | 
					
						
							| 
									
										
										
										
											2015-08-31 21:35:17 -03:00
										 |  |  | 	int i = 0; | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	Q_FOREACH (const QModelIndex &idx, selection) { | 
					
						
							|  |  |  | 		selected_dive_sites[i] = (uint32_t)idx.data(LocationInformationModel::UUID_ROLE).toInt(); | 
					
						
							| 
									
										
										
										
											2015-08-31 22:01:25 -03:00
										 |  |  | 		i++; | 
					
						
							| 
									
										
										
										
											2015-08-31 21:35:17 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-31 21:45:31 -03:00
										 |  |  | 	merge_dive_sites(displayed_dive_site.uuid, selected_dive_sites, i); | 
					
						
							| 
									
										
										
										
											2015-08-31 22:01:25 -03:00
										 |  |  | 	LocationInformationModel::instance()->update(); | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	QSortFilterProxyModel *m = (QSortFilterProxyModel *)ui.diveSiteListView->model(); | 
					
						
							| 
									
										
										
										
											2015-08-31 22:01:25 -03:00
										 |  |  | 	m->invalidate(); | 
					
						
							| 
									
										
										
										
											2015-08-31 21:35:17 -03:00
										 |  |  | 	free(selected_dive_sites); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-25 13:03:14 -03:00
										 |  |  | void LocationInformationWidget::updateLabels() | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (displayed_dive_site.name) | 
					
						
							|  |  |  | 		ui.diveSiteName->setText(displayed_dive_site.name); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		ui.diveSiteName->clear(); | 
					
						
							|  |  |  | 	if (displayed_dive_site.description) | 
					
						
							|  |  |  | 		ui.diveSiteDescription->setText(displayed_dive_site.description); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		ui.diveSiteDescription->clear(); | 
					
						
							|  |  |  | 	if (displayed_dive_site.notes) | 
					
						
							|  |  |  | 		ui.diveSiteNotes->setPlainText(displayed_dive_site.notes); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		ui.diveSiteNotes->clear(); | 
					
						
							| 
									
										
										
										
											2015-10-01 21:00:38 -04:00
										 |  |  | 	if (displayed_dive_site.latitude.udeg || displayed_dive_site.longitude.udeg) { | 
					
						
							|  |  |  | 		const char *coords = printGPSCoords(displayed_dive_site.latitude.udeg, displayed_dive_site.longitude.udeg); | 
					
						
							|  |  |  | 		ui.diveSiteCoordinates->setText(coords); | 
					
						
							|  |  |  | 		free((void *)coords); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | 		ui.diveSiteCoordinates->clear(); | 
					
						
							| 
									
										
										
										
											2015-10-01 21:00:38 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-10-07 20:00:22 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ui.locationTags->setText(constructLocationTags(displayed_dive_site.uuid)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-25 12:56:48 -03:00
										 |  |  | 	emit startFilterDiveSite(displayed_dive_site.uuid); | 
					
						
							| 
									
										
										
										
											2015-07-25 13:03:14 -03:00
										 |  |  | 	emit startEditDiveSite(displayed_dive_site.uuid); | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LocationInformationWidget::updateGpsCoordinates() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-07-30 22:41:02 -03:00
										 |  |  | 	QString oldText = ui.diveSiteCoordinates->text(); | 
					
						
							| 
									
										
										
										
											2015-10-01 21:00:38 -04:00
										 |  |  | 	const char *coords = printGPSCoords(displayed_dive_site.latitude.udeg, displayed_dive_site.longitude.udeg); | 
					
						
							|  |  |  | 	ui.diveSiteCoordinates->setText(coords); | 
					
						
							|  |  |  | 	free((void *)coords); | 
					
						
							| 
									
										
										
										
											2015-07-30 22:41:02 -03:00
										 |  |  | 	if (oldText != ui.diveSiteCoordinates->text()) | 
					
						
							|  |  |  | 		markChangedWidget(ui.diveSiteCoordinates); | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LocationInformationWidget::acceptChanges() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char *uiString; | 
					
						
							| 
									
										
										
										
											2015-08-30 01:00:22 +02:00
										 |  |  | 	struct dive_site *currentDs; | 
					
						
							|  |  |  | 	uiString = ui.diveSiteName->text().toUtf8().data(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (get_dive_site_by_uuid(displayed_dive_site.uuid) != NULL) | 
					
						
							|  |  |  | 		currentDs = get_dive_site_by_uuid(displayed_dive_site.uuid); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		currentDs = get_dive_site_by_uuid(create_dive_site_from_current_dive(uiString)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | 	currentDs->latitude = displayed_dive_site.latitude; | 
					
						
							|  |  |  | 	currentDs->longitude = displayed_dive_site.longitude; | 
					
						
							|  |  |  | 	if (!same_string(uiString, currentDs->name)) { | 
					
						
							|  |  |  | 		free(currentDs->name); | 
					
						
							|  |  |  | 		currentDs->name = copy_string(uiString); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	uiString = ui.diveSiteDescription->text().toUtf8().data(); | 
					
						
							|  |  |  | 	if (!same_string(uiString, currentDs->description)) { | 
					
						
							|  |  |  | 		free(currentDs->description); | 
					
						
							|  |  |  | 		currentDs->description = copy_string(uiString); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	uiString = ui.diveSiteNotes->document()->toPlainText().toUtf8().data(); | 
					
						
							|  |  |  | 	if (!same_string(uiString, currentDs->notes)) { | 
					
						
							|  |  |  | 		free(currentDs->notes); | 
					
						
							|  |  |  | 		currentDs->notes = copy_string(uiString); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-07-30 22:38:28 -03:00
										 |  |  | 	if (!ui.diveSiteCoordinates->text().isEmpty()) { | 
					
						
							|  |  |  | 		double lat, lon; | 
					
						
							|  |  |  | 		parseGpsText(ui.diveSiteCoordinates->text(), &lat, &lon); | 
					
						
							| 
									
										
										
										
											2017-03-23 08:13:49 +07:00
										 |  |  | 		currentDs->latitude.udeg = (int)(lat * 1000000.0); | 
					
						
							|  |  |  | 		currentDs->longitude.udeg = (int)(lon * 1000000.0); | 
					
						
							| 
									
										
										
										
											2015-07-30 22:38:28 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | 	if (dive_site_is_empty(currentDs)) { | 
					
						
							| 
									
										
										
										
											2015-06-01 23:13:51 -03:00
										 |  |  | 		LocationInformationModel::instance()->removeRow(get_divesite_idx(currentDs)); | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | 		displayed_dive.dive_site_uuid = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-08-30 01:00:22 +02:00
										 |  |  | 	copy_dive_site(currentDs, &displayed_dive_site); | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | 	mark_divelist_changed(true); | 
					
						
							|  |  |  | 	resetState(); | 
					
						
							| 
									
										
										
										
											2015-07-30 22:10:01 -03:00
										 |  |  | 	emit endRequestCoordinates(); | 
					
						
							| 
									
										
										
										
											2015-07-30 21:18:57 -03:00
										 |  |  | 	emit endEditDiveSite(); | 
					
						
							| 
									
										
										
										
											2015-09-30 19:11:16 -04:00
										 |  |  | 	emit stopFilterDiveSite(); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:15:53 -07:00
										 |  |  | 	emit coordinatesChanged(); | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LocationInformationWidget::rejectChanges() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	resetState(); | 
					
						
							| 
									
										
										
										
											2015-07-30 22:10:01 -03:00
										 |  |  | 	emit endRequestCoordinates(); | 
					
						
							| 
									
										
										
										
											2015-05-27 06:54:00 -07:00
										 |  |  | 	emit stopFilterDiveSite(); | 
					
						
							| 
									
										
										
										
											2015-07-30 21:18:57 -03:00
										 |  |  | 	emit endEditDiveSite(); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:15:53 -07:00
										 |  |  | 	emit coordinatesChanged(); | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-17 17:14:23 -03:00
										 |  |  | void LocationInformationWidget::showEvent(QShowEvent *ev) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-07-30 22:10:01 -03:00
										 |  |  | 	if (displayed_dive_site.uuid) { | 
					
						
							| 
									
										
										
										
											2015-07-25 13:03:14 -03:00
										 |  |  | 		updateLabels(); | 
					
						
							| 
									
										
										
										
											2015-10-13 13:29:44 -07:00
										 |  |  | 		ui.geoCodeButton->setEnabled(dive_site_has_gps_location(&displayed_dive_site)); | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 		QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel *>(ui.diveSiteListView->model()); | 
					
						
							| 
									
										
										
										
											2015-05-31 17:56:53 -03:00
										 |  |  | 		emit startFilterDiveSite(displayed_dive_site.uuid); | 
					
						
							| 
									
										
										
										
											2015-08-31 20:59:13 -03:00
										 |  |  | 		if (m) | 
					
						
							|  |  |  | 			m->invalidate(); | 
					
						
							| 
									
										
										
										
											2015-07-30 22:10:01 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	emit requestCoordinates(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-17 17:14:23 -03:00
										 |  |  | 	QGroupBox::showEvent(ev); | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LocationInformationWidget::markChangedWidget(QWidget *w) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QPalette p; | 
					
						
							|  |  |  | 	qreal h, s, l, a; | 
					
						
							|  |  |  | 	if (!modified) | 
					
						
							|  |  |  | 		enableEdition(); | 
					
						
							|  |  |  | 	qApp->palette().color(QPalette::Text).getHslF(&h, &s, &l, &a); | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	p.setBrush(QPalette::Base, (l <= 0.3) ? QColor(Qt::yellow).lighter() : (l <= 0.6) ? QColor(Qt::yellow).light() : /* else */ QColor(Qt::yellow).darker(300)); | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | 	w->setPalette(p); | 
					
						
							|  |  |  | 	modified = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LocationInformationWidget::resetState() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	modified = false; | 
					
						
							|  |  |  | 	resetPallete(); | 
					
						
							|  |  |  | 	MainWindow::instance()->dive_list()->setEnabled(true); | 
					
						
							|  |  |  | 	MainWindow::instance()->setEnabledToolbar(true); | 
					
						
							|  |  |  | 	ui.diveSiteMessage->setText(tr("Dive site management")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LocationInformationWidget::enableEdition() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	MainWindow::instance()->dive_list()->setEnabled(false); | 
					
						
							|  |  |  | 	MainWindow::instance()->setEnabledToolbar(false); | 
					
						
							|  |  |  | 	ui.diveSiteMessage->setText(tr("You are editing a dive site")); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | void LocationInformationWidget::on_diveSiteCoordinates_textChanged(const QString &text) | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-07-25 12:56:48 -03:00
										 |  |  | 	uint lat = displayed_dive_site.latitude.udeg; | 
					
						
							|  |  |  | 	uint lon = displayed_dive_site.longitude.udeg; | 
					
						
							| 
									
										
										
										
											2015-10-01 21:00:38 -04:00
										 |  |  | 	const char *coords = printGPSCoords(lat, lon); | 
					
						
							|  |  |  | 	if (!same_string(qPrintable(text), coords)) { | 
					
						
							| 
									
										
										
										
											2015-05-22 11:56:03 -07:00
										 |  |  | 		double latitude, longitude; | 
					
						
							|  |  |  | 		if (parseGpsText(text, &latitude, &longitude)) { | 
					
						
							| 
									
										
										
										
											2017-03-23 08:13:49 +07:00
										 |  |  | 			displayed_dive_site.latitude.udeg = (int)(latitude * 1000000); | 
					
						
							|  |  |  | 			displayed_dive_site.longitude.udeg = (int)(longitude * 1000000); | 
					
						
							| 
									
										
										
										
											2015-05-22 11:56:03 -07:00
										 |  |  | 			markChangedWidget(ui.diveSiteCoordinates); | 
					
						
							| 
									
										
										
										
											2015-05-22 12:15:53 -07:00
										 |  |  | 			emit coordinatesChanged(); | 
					
						
							| 
									
										
										
										
											2015-10-13 13:34:16 -07:00
										 |  |  | 			ui.geoCodeButton->setEnabled(latitude != 0 && longitude != 0); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			ui.geoCodeButton->setEnabled(false); | 
					
						
							| 
									
										
										
										
											2015-05-22 11:56:03 -07:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-10-01 21:00:38 -04:00
										 |  |  | 	free((void *)coords); | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | void LocationInformationWidget::on_diveSiteDescription_textChanged(const QString &text) | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-07-25 12:56:48 -03:00
										 |  |  | 	if (!same_string(qPrintable(text), displayed_dive_site.description)) | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | 		markChangedWidget(ui.diveSiteDescription); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | void LocationInformationWidget::on_diveSiteName_textChanged(const QString &text) | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-07-25 12:56:48 -03:00
										 |  |  | 	if (!same_string(qPrintable(text), displayed_dive_site.name)) | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | 		markChangedWidget(ui.diveSiteName); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LocationInformationWidget::on_diveSiteNotes_textChanged() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	if (!same_string(qPrintable(ui.diveSiteNotes->toPlainText()), displayed_dive_site.notes)) | 
					
						
							| 
									
										
										
										
											2015-05-17 16:13:41 -03:00
										 |  |  | 		markChangedWidget(ui.diveSiteNotes); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void LocationInformationWidget::resetPallete() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QPalette p; | 
					
						
							|  |  |  | 	ui.diveSiteCoordinates->setPalette(p); | 
					
						
							|  |  |  | 	ui.diveSiteDescription->setPalette(p); | 
					
						
							|  |  |  | 	ui.diveSiteName->setPalette(p); | 
					
						
							|  |  |  | 	ui.diveSiteNotes->setPalette(p); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-06-26 13:23:53 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-25 18:45:29 -03:00
										 |  |  | void LocationInformationWidget::reverseGeocode() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ReverseGeoLookupThread *geoLookup = ReverseGeoLookupThread::instance(); | 
					
						
							|  |  |  | 	geoLookup->lookup(&displayed_dive_site); | 
					
						
							|  |  |  | 	updateLabels(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | DiveLocationFilterProxyModel::DiveLocationFilterProxyModel(QObject *parent) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-09 20:31:52 -08:00
										 |  |  | 	Q_UNUSED(parent) | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DiveLocationLineEdit *location_line_edit = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | bool DiveLocationFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-09 20:31:52 -08:00
										 |  |  | 	Q_UNUSED(source_parent) | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	if (source_row == 0) | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QString sourceString = sourceModel()->index(source_row, DiveLocationModel::NAME).data(Qt::DisplayRole).toString(); | 
					
						
							| 
									
										
										
										
											2015-11-24 11:11:39 -08:00
										 |  |  | 	return sourceString.toLower().contains(location_line_edit->text().toLower()); | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | bool DiveLocationFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const | 
					
						
							| 
									
										
										
										
											2015-09-21 15:04:52 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	return source_left.data().toString() <= source_right.data().toString(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | DiveLocationModel::DiveLocationModel(QObject *o) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-09 20:31:52 -08:00
										 |  |  | 	Q_UNUSED(o) | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | 	resetModel(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DiveLocationModel::resetModel() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	beginResetModel(); | 
					
						
							|  |  |  | 	endResetModel(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | QVariant DiveLocationModel::data(const QModelIndex &index, int role) const | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-25 12:19:41 -03:00
										 |  |  | 	static const QIcon plusIcon(":plus"); | 
					
						
							|  |  |  | 	static const QIcon geoCode(":geocode"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	if (index.row() <= 1) { // two special cases.
 | 
					
						
							|  |  |  | 		if (index.column() == UUID) { | 
					
						
							| 
									
										
										
										
											2015-09-22 16:36:17 -03:00
										 |  |  | 			return RECENTLY_ADDED_DIVESITE; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 		switch (role) { | 
					
						
							|  |  |  | 		case Qt::DisplayRole: | 
					
						
							|  |  |  | 			return new_ds_value[index.row()]; | 
					
						
							|  |  |  | 		case Qt::ToolTipRole: | 
					
						
							| 
									
										
										
										
											2015-09-30 19:33:33 -03:00
										 |  |  | 			return displayed_dive_site.uuid ? | 
					
						
							|  |  |  | 				tr("Create a new dive site, copying relevant information from the current dive.") : | 
					
						
							|  |  |  | 				tr("Create a new dive site with this name"); | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 		case Qt::DecorationRole: | 
					
						
							|  |  |  | 			return plusIcon; | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// The dive sites are -2 because of the first two items.
 | 
					
						
							|  |  |  | 	struct dive_site *ds = get_dive_site(index.row() - 2); | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	switch (role) { | 
					
						
							|  |  |  | 	case Qt::EditRole: | 
					
						
							|  |  |  | 	case Qt::DisplayRole: | 
					
						
							|  |  |  | 		switch (index.column()) { | 
					
						
							|  |  |  | 		case UUID: | 
					
						
							|  |  |  | 			return ds->uuid; | 
					
						
							|  |  |  | 		case NAME: | 
					
						
							|  |  |  | 			return ds->name; | 
					
						
							|  |  |  | 		case LATITUDE: | 
					
						
							|  |  |  | 			return ds->latitude.udeg; | 
					
						
							|  |  |  | 		case LONGITUDE: | 
					
						
							|  |  |  | 			return ds->longitude.udeg; | 
					
						
							|  |  |  | 		case DESCRIPTION: | 
					
						
							|  |  |  | 			return ds->description; | 
					
						
							|  |  |  | 		case NOTES: | 
					
						
							|  |  |  | 			return ds->name; | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	case Qt::DecorationRole: { | 
					
						
							|  |  |  | 		if (dive_site_has_gps_location(ds)) | 
					
						
							|  |  |  | 			return geoCode; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return QVariant(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | int DiveLocationModel::columnCount(const QModelIndex &parent) const | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-09 20:31:52 -08:00
										 |  |  | 	Q_UNUSED(parent) | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | 	return COLUMNS; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | int DiveLocationModel::rowCount(const QModelIndex &parent) const | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-09 20:31:52 -08:00
										 |  |  | 	Q_UNUSED(parent) | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | 	return dive_site_table.nr + 2; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | bool DiveLocationModel::setData(const QModelIndex &index, const QVariant &value, int role) | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-09 20:31:52 -08:00
										 |  |  | 	Q_UNUSED(role) | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	if (!index.isValid()) | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	if (index.row() > 1) | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	new_ds_value[index.row()] = value.toString(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dataChanged(index, index); | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-22 14:31:56 -03:00
										 |  |  | DiveLocationLineEdit::DiveLocationLineEdit(QWidget *parent) : QLineEdit(parent), | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 							      proxy(new DiveLocationFilterProxyModel()), | 
					
						
							|  |  |  | 							      model(new DiveLocationModel()), | 
					
						
							| 
									
										
										
										
											2015-10-02 15:22:55 -04:00
										 |  |  | 							      view(new DiveLocationListView()), | 
					
						
							|  |  |  | 							      currType(NO_DIVE_SITE) | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-23 21:12:18 -10:00
										 |  |  | 	currUuid = 0; | 
					
						
							| 
									
										
										
										
											2015-09-21 15:04:52 -03:00
										 |  |  | 	location_line_edit = this; | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	proxy->setSourceModel(model); | 
					
						
							| 
									
										
										
										
											2015-09-21 15:04:52 -03:00
										 |  |  | 	proxy->setFilterKeyColumn(DiveLocationModel::NAME); | 
					
						
							| 
									
										
										
										
											2015-09-21 16:51:39 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 15:04:52 -03:00
										 |  |  | 	view->setModel(proxy); | 
					
						
							|  |  |  | 	view->setModelColumn(DiveLocationModel::NAME); | 
					
						
							|  |  |  | 	view->setItemDelegate(new LocationFilterDelegate()); | 
					
						
							| 
									
										
										
										
											2015-09-21 16:51:39 -03:00
										 |  |  | 	view->setEditTriggers(QAbstractItemView::NoEditTriggers); | 
					
						
							|  |  |  | 	view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | 
					
						
							|  |  |  | 	view->setSelectionBehavior(QAbstractItemView::SelectRows); | 
					
						
							|  |  |  | 	view->setSelectionMode(QAbstractItemView::SingleSelection); | 
					
						
							|  |  |  | 	view->setParent(0, Qt::Popup); | 
					
						
							|  |  |  | 	view->installEventFilter(this); | 
					
						
							| 
									
										
										
										
											2015-09-22 14:31:56 -03:00
										 |  |  | 	view->setFocusPolicy(Qt::NoFocus); | 
					
						
							|  |  |  | 	view->setFocusProxy(this); | 
					
						
							| 
									
										
										
										
											2015-09-25 13:11:44 -03:00
										 |  |  | 	view->setMouseTracking(true); | 
					
						
							| 
									
										
										
										
											2015-09-21 16:51:39 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | 	connect(this, &QLineEdit::textEdited, this, &DiveLocationLineEdit::setTemporaryDiveSiteName); | 
					
						
							| 
									
										
										
										
											2015-09-21 17:18:52 -03:00
										 |  |  | 	connect(view, &QAbstractItemView::activated, this, &DiveLocationLineEdit::itemActivated); | 
					
						
							| 
									
										
										
										
											2015-09-25 13:11:44 -03:00
										 |  |  | 	connect(view, &QAbstractItemView::entered, this, &DiveLocationLineEdit::entered); | 
					
						
							| 
									
										
										
										
											2015-09-25 13:21:23 -03:00
										 |  |  | 	connect(view, &DiveLocationListView::currentIndexChanged, this, &DiveLocationLineEdit::currentChanged); | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 16:51:39 -03:00
										 |  |  | bool DiveLocationLineEdit::eventFilter(QObject *o, QEvent *e) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-09 20:31:52 -08:00
										 |  |  | 	Q_UNUSED(o) | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	if (e->type() == QEvent::KeyPress) { | 
					
						
							|  |  |  | 		QKeyEvent *keyEv = (QKeyEvent *)e; | 
					
						
							| 
									
										
										
										
											2015-09-21 16:51:39 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (keyEv->key() == Qt::Key_Escape) { | 
					
						
							|  |  |  | 			view->hide(); | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 		if (keyEv->key() == Qt::Key_Return || | 
					
						
							|  |  |  | 		    keyEv->key() == Qt::Key_Enter) { | 
					
						
							| 
									
										
										
										
											2015-10-25 10:38:16 +09:00
										 |  |  | #if __APPLE__
 | 
					
						
							|  |  |  | 			// for some reason it seems like on a Mac hitting return/enter
 | 
					
						
							|  |  |  | 			// doesn't call 'activated' for that index. so let's do it manually
 | 
					
						
							|  |  |  | 			if (view->currentIndex().isValid()) | 
					
						
							|  |  |  | 				itemActivated(view->currentIndex()); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-09-21 16:51:39 -03:00
										 |  |  | 			view->hide(); | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-09-22 16:27:07 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 		if (keyEv->key() == Qt::Key_Tab) { | 
					
						
							|  |  |  | 			itemActivated(view->currentIndex()); | 
					
						
							|  |  |  | 			view->hide(); | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-09-21 17:11:59 -03:00
										 |  |  | 		event(e); | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	} else if (e->type() == QEvent::MouseButtonPress) { | 
					
						
							| 
									
										
										
										
											2015-09-22 14:31:56 -03:00
										 |  |  | 		if (!view->underMouse()) { | 
					
						
							|  |  |  | 			view->hide(); | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-09-21 16:51:39 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-21 17:11:59 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 16:51:39 -03:00
										 |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | void DiveLocationLineEdit::focusOutEvent(QFocusEvent *ev) | 
					
						
							| 
									
										
										
										
											2015-09-22 15:23:38 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (!view->isVisible()) { | 
					
						
							|  |  |  | 		QLineEdit::focusOutEvent(ev); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | void DiveLocationLineEdit::itemActivated(const QModelIndex &index) | 
					
						
							| 
									
										
										
										
											2015-09-21 17:18:52 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-10-05 18:01:28 -03:00
										 |  |  | 	QModelIndex idx = index; | 
					
						
							|  |  |  | 	if (index.column() == DiveLocationModel::UUID) | 
					
						
							|  |  |  | 		idx = index.model()->index(index.row(), DiveLocationModel::NAME); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-22 16:36:17 -03:00
										 |  |  | 	QModelIndex uuidIndex = index.model()->index(index.row(), DiveLocationModel::UUID); | 
					
						
							|  |  |  | 	uint32_t uuid = uuidIndex.data().toInt(); | 
					
						
							|  |  |  | 	currType = uuid == 1 ? NEW_DIVE_SITE : EXISTING_DIVE_SITE; | 
					
						
							|  |  |  | 	currUuid = uuid; | 
					
						
							| 
									
										
										
										
											2015-10-05 18:01:28 -03:00
										 |  |  | 	setText(idx.data().toString()); | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	if (currUuid == NEW_DIVE_SITE) | 
					
						
							| 
									
										
										
										
											2015-09-22 17:20:55 -03:00
										 |  |  | 		qDebug() << "Setting a New dive site"; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		qDebug() << "Setting a Existing dive site"; | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	if (view->isVisible()) | 
					
						
							| 
									
										
										
										
											2015-09-23 13:56:49 -03:00
										 |  |  | 		view->hide(); | 
					
						
							| 
									
										
										
										
											2015-09-23 14:46:29 -03:00
										 |  |  | 	emit diveSiteSelected(currUuid); | 
					
						
							| 
									
										
										
										
											2015-09-21 17:18:52 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | void DiveLocationLineEdit::refreshDiveSiteCache() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	model->resetModel(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | static struct dive_site *get_dive_site_name_start_which_str(const QString &str) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | 	struct dive_site *ds; | 
					
						
							|  |  |  | 	int i; | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	for_each_dive_site (i, ds) { | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | 		QString dsName(ds->name); | 
					
						
							| 
									
										
										
										
											2015-09-21 15:04:52 -03:00
										 |  |  | 		if (dsName.toLower().startsWith(str.toLower())) { | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | 			return ds; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | void DiveLocationLineEdit::setTemporaryDiveSiteName(const QString &s) | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-09 20:31:52 -08:00
										 |  |  | 	Q_UNUSED(s) | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | 	QModelIndex i0 = model->index(0, DiveLocationModel::NAME); | 
					
						
							| 
									
										
										
										
											2015-09-21 15:04:52 -03:00
										 |  |  | 	QModelIndex i1 = model->index(1, DiveLocationModel::NAME); | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | 	model->setData(i0, text()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 15:04:52 -03:00
										 |  |  | 	QString i1_name = INVALID_DIVE_SITE_NAME; | 
					
						
							|  |  |  | 	if (struct dive_site *ds = get_dive_site_name_start_which_str(text())) { | 
					
						
							|  |  |  | 		const QString orig_name = QString(ds->name).toLower(); | 
					
						
							|  |  |  | 		const QString new_name = text().toLower(); | 
					
						
							|  |  |  | 		if (new_name != orig_name) | 
					
						
							|  |  |  | 			i1_name = QString(ds->name); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	model->setData(i1, i1_name); | 
					
						
							| 
									
										
										
										
											2015-09-21 15:04:52 -03:00
										 |  |  | 	proxy->invalidate(); | 
					
						
							| 
									
										
										
										
											2015-09-23 16:27:44 -03:00
										 |  |  | 	fixPopupPosition(); | 
					
						
							|  |  |  | 	if (!view->isVisible()) | 
					
						
							|  |  |  | 		view->show(); | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 16:08:58 -03:00
										 |  |  | void DiveLocationLineEdit::keyPressEvent(QKeyEvent *ev) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-21 17:22:31 -03:00
										 |  |  | 	QLineEdit::keyPressEvent(ev); | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	if (ev->key() != Qt::Key_Left && | 
					
						
							|  |  |  | 	    ev->key() != Qt::Key_Right && | 
					
						
							|  |  |  | 	    ev->key() != Qt::Key_Escape && | 
					
						
							|  |  |  | 	    ev->key() != Qt::Key_Return) { | 
					
						
							| 
									
										
										
										
											2015-09-22 17:20:55 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 		if (ev->key() != Qt::Key_Up && ev->key() != Qt::Key_Down) { | 
					
						
							| 
									
										
										
										
											2015-09-22 17:20:55 -03:00
										 |  |  | 			currType = NEW_DIVE_SITE; | 
					
						
							|  |  |  | 			currUuid = RECENTLY_ADDED_DIVESITE; | 
					
						
							| 
									
										
										
										
											2015-09-23 16:27:44 -03:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			showPopup(); | 
					
						
							| 
									
										
										
										
											2015-09-22 17:20:55 -03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-09-21 16:51:39 -03:00
										 |  |  | 	} else if (ev->key() == Qt::Key_Escape) { | 
					
						
							|  |  |  | 		view->hide(); | 
					
						
							| 
									
										
										
										
											2015-09-21 16:08:58 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-23 16:03:28 -03:00
										 |  |  | void DiveLocationLineEdit::fixPopupPosition() | 
					
						
							| 
									
										
										
										
											2015-09-21 16:08:58 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-22 14:13:51 -03:00
										 |  |  | 	const QRect screen = QApplication::desktop()->availableGeometry(this); | 
					
						
							|  |  |  | 	const int maxVisibleItems = 5; | 
					
						
							|  |  |  | 	QPoint pos; | 
					
						
							|  |  |  | 	int rh, w; | 
					
						
							|  |  |  | 	int h = (view->sizeHintForRow(0) * qMin(maxVisibleItems, view->model()->rowCount()) + 3) + 3; | 
					
						
							|  |  |  | 	QScrollBar *hsb = view->horizontalScrollBar(); | 
					
						
							|  |  |  | 	if (hsb && hsb->isVisible()) | 
					
						
							|  |  |  | 		h += view->horizontalScrollBar()->sizeHint().height(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rh = height(); | 
					
						
							|  |  |  | 	pos = mapToGlobal(QPoint(0, height() - 2)); | 
					
						
							|  |  |  | 	w = width(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (w > screen.width()) | 
					
						
							|  |  |  | 		w = screen.width(); | 
					
						
							|  |  |  | 	if ((pos.x() + w) > (screen.x() + screen.width())) | 
					
						
							|  |  |  | 		pos.setX(screen.x() + screen.width() - w); | 
					
						
							|  |  |  | 	if (pos.x() < screen.x()) | 
					
						
							|  |  |  | 		pos.setX(screen.x()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int top = pos.y() - rh - screen.top() + 2; | 
					
						
							|  |  |  | 	int bottom = screen.bottom() - pos.y(); | 
					
						
							|  |  |  | 	h = qMax(h, view->minimumHeight()); | 
					
						
							|  |  |  | 	if (h > bottom) { | 
					
						
							|  |  |  | 		h = qMin(qMax(top, bottom), h); | 
					
						
							|  |  |  | 		if (top > bottom) | 
					
						
							|  |  |  | 			pos.setY(pos.y() - h - rh + 2); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-21 16:08:58 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-22 14:13:51 -03:00
										 |  |  | 	view->setGeometry(pos.x(), pos.y(), w, h); | 
					
						
							| 
									
										
										
										
											2015-10-01 16:27:22 -03:00
										 |  |  | 	if (!view->currentIndex().isValid() && view->model()->rowCount()) { | 
					
						
							| 
									
										
										
										
											2016-01-20 01:05:52 +05:30
										 |  |  | 		view->setCurrentIndex(view->model()->index(0, 1)); | 
					
						
							| 
									
										
										
										
											2015-09-23 16:19:58 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-23 16:03:28 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-09-21 16:08:58 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-25 14:51:10 -03:00
										 |  |  | void DiveLocationLineEdit::setCurrentDiveSiteUuid(uint32_t uuid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	currUuid = uuid; | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	if (uuid == 0) { | 
					
						
							| 
									
										
										
										
											2015-09-25 14:51:10 -03:00
										 |  |  | 		currType = NO_DIVE_SITE; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	struct dive_site *ds = get_dive_site_by_uuid(uuid); | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | 	if (!ds) | 
					
						
							| 
									
										
										
										
											2015-09-25 14:51:10 -03:00
										 |  |  | 		clear(); | 
					
						
							| 
									
										
										
										
											2015-10-01 21:24:04 -04:00
										 |  |  | 	else | 
					
						
							|  |  |  | 		setText(ds->name); | 
					
						
							| 
									
										
										
										
											2015-09-25 14:51:10 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-23 16:03:28 -03:00
										 |  |  | void DiveLocationLineEdit::showPopup() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	fixPopupPosition(); | 
					
						
							| 
									
										
										
										
											2015-09-22 14:13:51 -03:00
										 |  |  | 	if (!view->isVisible()) { | 
					
						
							| 
									
										
										
										
											2015-09-22 16:27:07 -03:00
										 |  |  | 		setTemporaryDiveSiteName(text()); | 
					
						
							| 
									
										
										
										
											2015-09-22 14:31:56 -03:00
										 |  |  | 		proxy->invalidate(); | 
					
						
							|  |  |  | 		view->show(); | 
					
						
							| 
									
										
										
										
											2015-09-22 14:13:51 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-21 16:08:58 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-23 14:46:29 -03:00
										 |  |  | DiveLocationLineEdit::DiveSiteType DiveLocationLineEdit::currDiveSiteType() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return currType; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | uint32_t DiveLocationLineEdit::currDiveSiteUuid() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return currUuid; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | DiveLocationListView::DiveLocationListView(QWidget *parent) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-03-09 20:31:52 -08:00
										 |  |  | 	Q_UNUSED(parent) | 
					
						
							| 
									
										
										
										
											2015-09-21 14:01:58 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-09-25 13:21:23 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-29 19:57:53 -04:00
										 |  |  | void DiveLocationListView::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) | 
					
						
							| 
									
										
										
										
											2015-09-25 13:21:23 -03:00
										 |  |  | { | 
					
						
							|  |  |  | 	QListView::currentChanged(current, previous); | 
					
						
							|  |  |  | 	emit currentIndexChanged(current); | 
					
						
							|  |  |  | } |