Dirk found a bug in my perfect code.

On Thu, Jul 16, 2015 at 6:18 PM, Tomaz Canabrava <[email protected]> wrote:

> there, this clears the case of two identical strings would appear as
> 'create dive site'
>
From c8bd843ec7555265ca2fd0763c6f93b819d5db0d Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Thu, 16 Jul 2015 18:47:57 -0300
Subject: [PATCH 2/2] Fix mouse interaction with the Location List

paint methods should be used only to paint, not to trigger other
widget behaviours ( we could got ourselves into a bad recursion
bug from that ). Also, enabled mouse tracking to correctly track
the mouse movement inside the widget.

Signed-off-by: Tomaz Canabrava <[email protected]>
---
 qt-ui/globe.cpp          | 12 ++++++++++++
 qt-ui/globe.h            |  2 ++
 qt-ui/maintab.cpp        |  8 +++++++-
 qt-ui/modeldelegates.cpp | 11 -----------
 4 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/qt-ui/globe.cpp b/qt-ui/globe.cpp
index e74e4d1..066fdc2 100644
--- a/qt-ui/globe.cpp
+++ b/qt-ui/globe.cpp
@@ -373,6 +373,15 @@ void GlobeGPS::resizeEvent(QResizeEvent *event)
 		messageWidget->setGeometry(5, 5, size - 10, 0);
 	messageWidget->setMaximumHeight(500);
 }
+
+void GlobeGPS::centerOnIndex(const QModelIndex& idx)
+{
+	struct dive_site *ds = get_dive_site_by_uuid(idx.model()->index(idx.row(), 0).data().toInt());
+	if (!ds || !dive_site_has_gps_location(ds))
+		MainWindow::instance()->globe()->centerOnDiveSite(&displayed_dive_site);
+	else
+		MainWindow::instance()->globe()->centerOnDiveSite(ds);
+}
 #else
 
 GlobeGPS::GlobeGPS(QWidget *parent)
@@ -398,4 +407,7 @@ void GlobeGPS::endGetDiveCoordinates()
 void GlobeGPS::reload()
 {
 }
+void GlobeGPS::centerOnIndex(const QModelIndex& idx)
+{
+}
 #endif
diff --git a/qt-ui/globe.h b/qt-ui/globe.h
index 5b34b05..e8be53b 100644
--- a/qt-ui/globe.h
+++ b/qt-ui/globe.h
@@ -52,6 +52,7 @@ slots:
 	void prepareForGetDiveCoordinates();
 	void endGetDiveCoordinates();
 	void centerOnDiveSite(struct dive_site *ds);
+	void centerOnIndex(const QModelIndex& idx);
 };
 
 #else // NO_MARBLE
@@ -65,6 +66,7 @@ public:
 	void reload();
 	void repopulateLabels();
 	void centerOnDiveSite(uint32_t uuid);
+	void centerOnIndex(const QModelIndex& idx);
 	void centerOnCurrentDive();
 	bool eventFilter(QObject *, QEvent *);
 public
diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp
index 4920fab..fd78a10 100644
--- a/qt-ui/maintab.cpp
+++ b/qt-ui/maintab.cpp
@@ -67,7 +67,7 @@ MainTab::MainTab(QWidget *parent) : QTabWidget(parent),
 	completer->setCompletionColumn(LocationInformationModel::NAME);
 	completer->setCaseSensitivity(Qt::CaseInsensitive);
 	completerListview->setItemDelegate(new LocationFilterDelegate());
-
+	completerListview->setMouseTracking(true);
 	locationManagementEditHelper = new LocationManagementEditHelper();
 	connect(locationManagementEditHelper, &LocationManagementEditHelper::setLineEditText,
 		ui.location, &QLineEdit::setText);
@@ -453,6 +453,12 @@ void MainTab::showLocation()
 
 void MainTab::updateDiveInfo(bool clear)
 {
+	// I don't like this code here - but globe() wasn't initialized on the constructor.
+	{
+		QListView *completerListview = qobject_cast<QListView*>(ui.location->completer()->popup());
+		connect(completerListview, SIGNAL(entered(QModelIndex)), MainWindow::instance()->globe(), SLOT(centerOnIndex(QModelIndex)), Qt::UniqueConnection);
+	}
+
 	EditMode rememberEM = editMode;
 	// don't execute this while adding / planning a dive
 	if (editMode == ADD || editMode == MANUALLY_ADDED_DIVE || MainWindow::instance()->graphics()->isPlanner())
diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp
index 404d329..848c66f 100644
--- a/qt-ui/modeldelegates.cpp
+++ b/qt-ui/modeldelegates.cpp
@@ -540,17 +540,6 @@ void LocationFilterDelegate::paint(QPainter *painter, const QStyleOptionViewItem
 		free( (void*) gpsCoords);
 	}
 
-#ifndef NO_MARBLE
-	if ((option.state & QStyle::State_HasFocus)) {
-		// show either the GPS location of the currently focused dive site or
-		// the gps data for the displayed dive site (even if that has no GPS -> zoom out)
-		if (dive_site_has_gps_location(ds))
-			MainWindow::instance()->globe()->centerOnDiveSite(ds);
-		else
-			MainWindow::instance()->globe()->centerOnDiveSite(&displayed_dive_site);
-	}
-#endif
-
 	if (dive_site_has_gps_location(ds) && dive_site_has_gps_location(&displayed_dive_site)) {
 		// so we are showing a completion and both the current dive site and the completion
 		// have a GPS fix... so let's show the distance
-- 
2.4.6

From 61d3d59123027aa51b5b4c1587c4cc3bdbeed848 Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Thu, 16 Jul 2015 18:08:08 -0300
Subject: [PATCH 1/2] Show only 1 possibility if string is unique

We should only show one possibility if the dive_site name
string is unique - we don't have that dive_site yet - so
we pass to the Completer filter a Dummy string that will
surelly not be a dive site - konami code.

Signed-off-by: Tomaz Canabrava <[email protected]>
---
 qt-models/divelocationmodel.cpp | 33 +++++++++++++++++++++++++--------
 qt-ui/modeldelegates.cpp        |  5 ++++-
 2 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/qt-models/divelocationmodel.cpp b/qt-models/divelocationmodel.cpp
index 778496a..05d7df7 100644
--- a/qt-models/divelocationmodel.cpp
+++ b/qt-models/divelocationmodel.cpp
@@ -32,6 +32,18 @@ int LocationInformationModel::rowCount(const QModelIndex &parent) const
 	return internalRowCount + 2;
 }
 
+static struct dive_site *get_dive_site_name_start_which_str(const QString& str) {
+	struct dive_site *ds;
+	int i;
+	for_each_dive_site(i,ds) {
+		QString dsName(ds->name);
+		if (dsName.startsWith(str)) {
+			return ds;
+		}
+	}
+	return NULL;
+}
+
 QVariant LocationInformationModel::data(const QModelIndex &index, int role) const
 {
 	if (!index.isValid())
@@ -44,20 +56,25 @@ QVariant LocationInformationModel::data(const QModelIndex &index, int role) cons
 		switch(role) {
 			case Qt::DisplayRole : {
 				if (index.row() == 1) {
-					struct dive_site *ds;
-					int i;
-					for_each_dive_site(i, ds) {
-						QString dsName(ds->name);
-						if (dsName.startsWith(textField->text()))
-							return dsName;
-					}
+					struct dive_site *ds = get_dive_site_name_start_which_str(textField->text());
+					if(ds)
+						return ds->name;
 				}
 				return textField->text();
 			}
 			case Qt::ToolTipRole : {
 				return QString(tr("Create dive site with this name"));
 			}
-			case Qt::EditRole : return textField->text();
+			case Qt::EditRole : {
+				if (index.row() == 1) {
+					struct dive_site *ds = get_dive_site_name_start_which_str(textField->text());
+					if (!ds)
+						return "NOT HERE";
+					if (QString(ds->name) == textField->text())
+						return "NOT HERE";
+				}
+				return textField->text();
+			}
 			case Qt::DecorationRole : return QIcon(":plus");
 		}
 	}
diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp
index f0f77ab..404d329 100644
--- a/qt-ui/modeldelegates.cpp
+++ b/qt-ui/modeldelegates.cpp
@@ -22,6 +22,7 @@
 #include <QFont>
 #include <QBrush>
 #include <QColor>
+#include <QAbstractProxyModel>
 
 QSize DiveListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
 {
@@ -489,12 +490,14 @@ LocationFilterDelegate::LocationFilterDelegate(QObject *parent)
 {
 }
 
-void LocationFilterDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
+void LocationFilterDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &origIdx) const
 {
 	QFont fontBigger = qApp->font();
 	QFont fontSmaller = qApp->font();
 	QFontMetrics fmBigger(fontBigger);
 	QStyleOptionViewItemV4 opt = option;
+	const QAbstractProxyModel *proxyModel = dynamic_cast<const QAbstractProxyModel*>(origIdx.model());
+	QModelIndex index = proxyModel->mapToSource(origIdx);
 	QStyledItemDelegate::initStyleOption(&opt, index);
 	QBrush bg;
 	QString diveSiteName = index.data().toString();
-- 
2.4.6

_______________________________________________
subsurface mailing list
[email protected]
http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to