This is an attempt to resolve some of the problems Linus outlined on his
last batch of emails,
bottom point: this won't solve a thing ( it seems that I don't know how to
use Qt, or that Qt is broken when you try to use a Delegate on a QCompleter
)

But you can already see what will happen when I manage to make the
QCompleter work, there's a new widget ( that will be removed when things
are okay ) showing what the completer should show.

-.-

So, in the perfect world this would do the following:
- do not rely on the name of the dive site, but on the uuid
- treat different dive sites with same names in a sane way
- display coords of the dive sites so you can diferentiate the ones with
same names
- works with different styles

it's not a huge patch but it was a pain to write due to lot of
experimentation so please be nice. :)
From ee759b35438f4f82ac9c0d4587092951e094162d Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Wed, 1 Jul 2015 21:04:03 -0300
Subject: [PATCH 6/6] Correctly display the data on the delegate.

A bit more complex than I tougth it would be ( and a ton of trial
and error to find the right spot on the delegate to draw stuff )
this delegate follows the current style ( so it should be okaish
on a dark and on a light theme )

This is supposed to work on a QCompleter, but it doesn't ( I
really don't know why, so maybe I'll remove that completer. sigh. )

Signed-off-by: Tomaz Canabrava <[email protected]>
---
 qt-ui/maintab.cpp        | 14 +++++++++---
 qt-ui/modeldelegates.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 66 insertions(+), 7 deletions(-)

diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp
index fe1143f..144a430 100644
--- a/qt-ui/maintab.cpp
+++ b/qt-ui/maintab.cpp
@@ -63,6 +63,17 @@ MainTab::MainTab(QWidget *parent) : QTabWidget(parent),
 	completer->setCompletionColumn(LocationInformationModel::NAME);
 	completer->setCompletionRole(Qt::DisplayRole);
 	completer->setCompletionMode(QCompleter::PopupCompletion);
+	completer->setCaseSensitivity(Qt::CaseInsensitive);
+
+	QListView *completerListview = new QListView();
+	completerListview->setItemDelegate(new LocationFilterDelegate());
+	completer->setPopup(completerListview);
+
+	QListView *completerListView2 = new QListView();
+	completerListView2->setItemDelegate(new LocationFilterDelegate());
+	completerListView2->setModel(LocationInformationModel::instance());
+	completerListView2->setModelColumn(1);
+	completerListView2->show();
 
 	ui.location->setCompleter(completer);
 	connect(ui.addDiveSite, SIGNAL(clicked()), this, SLOT(showDiveSiteSimpleEdit()));
@@ -107,9 +118,6 @@ MainTab::MainTab(QWidget *parent) : QTabWidget(parent),
 	connect(ui.cylinders->view(), SIGNAL(clicked(QModelIndex)), this, SLOT(editCylinderWidget(QModelIndex)));
 	connect(ui.weights->view(), SIGNAL(clicked(QModelIndex)), this, SLOT(editWeightWidget(QModelIndex)));
 
-	ui.location->completer()->setCaseSensitivity(Qt::CaseInsensitive);
-	ui.location->completer()->setCompletionMode(QCompleter::PopupCompletion);
-
 	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));
diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp
index 8425d03..68e62a5 100644
--- a/qt-ui/modeldelegates.cpp
+++ b/qt-ui/modeldelegates.cpp
@@ -10,10 +10,15 @@
 #include "weigthsysteminfomodel.h"
 #include "weightmodel.h"
 #include "divetripmodel.h"
+#include "qthelper.h"
 
 #include <QCompleter>
 #include <QKeyEvent>
 #include <QTextDocument>
+#include <QApplication>
+#include <QFont>
+#include <QBrush>
+#include <QColor>
 
 QSize DiveListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
 {
@@ -483,14 +488,60 @@ LocationFilterDelegate::LocationFilterDelegate(QObject *parent)
 
 void LocationFilterDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
 {
+	QFont fontBigger = qApp->font();
+	QFont fontSmaller = qApp->font();
+	QFontMetrics fmBigger(fontBigger);
+	QStyleOptionViewItemV4 opt = option;
+	QStyledItemDelegate::initStyleOption(&opt, index);
+	QBrush bg;
+	QString diveSiteName = index.data().toString();
+
+	struct dive_site *ds = get_dive_site_by_uuid(
+		index.model()->data(index.model()->index(index.row(),0)).toInt()
+	);
+	if (!ds)
+		return;
+
+	const char *gpsCoords = printGPSCoords(displayed_dive_site.latitude.udeg, displayed_dive_site.longitude.udeg);
+	QString diveSiteCoords(gpsCoords);
+	free( (void*) gpsCoords);
+
+	fontBigger.setPointSize(fontBigger.pointSize() + 2);
+	fontBigger.setBold(true);
+
 	painter->save();
-	painter->drawText(QPoint(0, 0), index.data().toString());
+	painter->setRenderHint(QPainter::Antialiasing);
+	if( ( option.state & QStyle::State_Selected ) || ( option.state & QStyle::State_MouseOver ) )
+	    bg = option.palette.highlight();
+	else
+	    bg = option.palette.window();
+	painter->setPen(QPen(Qt::NoPen));
+	painter->setBrush(bg);
+	painter->drawRect(option.rect);
+	painter->restore();
+
+	painter->save();
+	painter->setBrush(option.palette.text());
+	painter->setFont(fontBigger);
+	painter->drawText(option.rect.x(),option.rect.y() + fmBigger.boundingRect("YH").height(), diveSiteName);
+	painter->setFont(fontSmaller);
+	painter->setBrush(option.palette.brightText());
+	painter->drawText(option.rect.x(),option.rect.y() + fmBigger.boundingRect("YH").height() * 2, diveSiteCoords);
 	painter->restore();
 }
 
 QSize LocationFilterDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
 {
-	QSize size;
-	size.setWidth(option.rect.width());
-	size.setHeight(20);
+	QFont fontBigger = qApp->font();
+	fontBigger.setPointSize(fontBigger.pointSize() + 2);
+	fontBigger.setBold(true);
+
+	QFontMetrics fmBigger(fontBigger);
+
+	QFont fontSmaller = qApp->font();
+	QFontMetrics fmSmaller(fontSmaller);
+
+	QSize retSize = QStyledItemDelegate::sizeHint(option, index);
+	retSize.setHeight(fmBigger.boundingRect("Yellow House").height() + 5 /*spacing*/ + fmSmaller.boundingRect("Yellow House").height());
+	return retSize;
 }
-- 
2.4.5

From 0481c478b6f4a3909e97a12029510b6678b4079d Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Wed, 1 Jul 2015 19:31:56 -0300
Subject: [PATCH 5/6] Create a Delegate to display custom data.

Signed-off-by: Tomaz Canabrava <[email protected]>
---
 qt-ui/modeldelegates.cpp | 18 ++++++++++++++++++
 qt-ui/modeldelegates.h   |  9 +++++++++
 2 files changed, 27 insertions(+)

diff --git a/qt-ui/modeldelegates.cpp b/qt-ui/modeldelegates.cpp
index 827e6ab..8425d03 100644
--- a/qt-ui/modeldelegates.cpp
+++ b/qt-ui/modeldelegates.cpp
@@ -476,3 +476,21 @@ QSize HTMLDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModel
 	doc.setTextWidth(options.rect.width());
 	return QSize(doc.idealWidth(), doc.size().height());
 }
+
+LocationFilterDelegate::LocationFilterDelegate(QObject *parent)
+{
+}
+
+void LocationFilterDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
+{
+	painter->save();
+	painter->drawText(QPoint(0, 0), index.data().toString());
+	painter->restore();
+}
+
+QSize LocationFilterDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
+{
+	QSize size;
+	size.setWidth(option.rect.width());
+	size.setHeight(20);
+}
diff --git a/qt-ui/modeldelegates.h b/qt-ui/modeldelegates.h
index f501c52..9570177 100644
--- a/qt-ui/modeldelegates.h
+++ b/qt-ui/modeldelegates.h
@@ -129,4 +129,13 @@ public:
 	virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
 	virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
 };
+
+class LocationFilterDelegate : public QStyledItemDelegate {
+	Q_OBJECT
+public:
+	LocationFilterDelegate(QObject *parent = 0);
+	void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
+	QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
+};
+
 #endif // MODELDELEGATES_H
-- 
2.4.5

From e99c75bea434e9ab969b10a3d6d8dbc502affb41 Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Wed, 1 Jul 2015 19:31:35 -0300
Subject: [PATCH 4/6] Added again a QCompleter to show the possibilities

---
 qt-ui/maintab.cpp | 8 ++++++++
 qt-ui/maintab.h   | 1 +
 2 files changed, 9 insertions(+)

diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp
index 1236c94..fe1143f 100644
--- a/qt-ui/maintab.cpp
+++ b/qt-ui/maintab.cpp
@@ -58,6 +58,13 @@ MainTab::MainTab(QWidget *parent) : QTabWidget(parent),
 	ui.extraData->setModel(extraDataModel);
 	closeMessage();
 
+	QCompleter *completer = new QCompleter();
+	completer->setModel(LocationInformationModel::instance());
+	completer->setCompletionColumn(LocationInformationModel::NAME);
+	completer->setCompletionRole(Qt::DisplayRole);
+	completer->setCompletionMode(QCompleter::PopupCompletion);
+
+	ui.location->setCompleter(completer);
 	connect(ui.addDiveSite, SIGNAL(clicked()), this, SLOT(showDiveSiteSimpleEdit()));
 
 	QAction *action = new QAction(tr("Apply changes"), this);
@@ -746,6 +753,7 @@ void MainTab::reload()
 	buddyModel.updateModel();
 	diveMasterModel.updateModel();
 	tagModel.updateModel();
+	LocationInformationModel::instance()->update();
 }
 
 // tricky little macro to edit all the selected dives
diff --git a/qt-ui/maintab.h b/qt-ui/maintab.h
index a8f2dfa..c96acb2 100644
--- a/qt-ui/maintab.h
+++ b/qt-ui/maintab.h
@@ -14,6 +14,7 @@
 
 #include "ui_maintab.h"
 #include "completionmodels.h"
+#include "divelocationmodel.h"
 #include "dive.h"
 
 class WeightModel;
-- 
2.4.5

From 10f3f0318a6ff4ba3351136b9e36f1289502a770 Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Wed, 1 Jul 2015 18:56:00 -0300
Subject: [PATCH 3/6] Correctly removes the Location Completion Model

Signed-off-by: Tomaz Canabrava <[email protected]>
---
 qt-models/completionmodels.h | 6 ------
 qt-ui/maintab.h              | 1 -
 2 files changed, 7 deletions(-)

diff --git a/qt-models/completionmodels.h b/qt-models/completionmodels.h
index 859b8c0..c4dfd2a 100644
--- a/qt-models/completionmodels.h
+++ b/qt-models/completionmodels.h
@@ -15,12 +15,6 @@ public:
 	void updateModel();
 };
 
-class LocationCompletionModel : public QStringListModel {
-	Q_OBJECT
-public:
-	void updateModel();
-};
-
 class SuitCompletionModel : public QStringListModel {
 	Q_OBJECT
 public:
diff --git a/qt-ui/maintab.h b/qt-ui/maintab.h
index 8447109..a8f2dfa 100644
--- a/qt-ui/maintab.h
+++ b/qt-ui/maintab.h
@@ -105,7 +105,6 @@ private:
 	EditMode editMode;
 	BuddyCompletionModel buddyModel;
 	DiveMasterCompletionModel diveMasterModel;
-	LocationCompletionModel locationModel;
 	SuitCompletionModel suitModel;
 	TagCompletionModel tagModel;
 	DivePictureModel *divePictureModel;
-- 
2.4.5

From ab7092f50e85d8aa0fbbd037e7aa404b4adfaa99 Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Wed, 1 Jul 2015 18:46:27 -0300
Subject: [PATCH 2/6] More information on Dive Site Model

All of dive site information is now exposed to the model

Signed-off-by: Tomaz Canabrava <[email protected]>
---
 qt-models/divelocationmodel.cpp | 23 ++++++++++++++++++++---
 qt-models/divelocationmodel.h   |  7 ++++---
 2 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/qt-models/divelocationmodel.cpp b/qt-models/divelocationmodel.cpp
index e34e20a..53f5189 100644
--- a/qt-models/divelocationmodel.cpp
+++ b/qt-models/divelocationmodel.cpp
@@ -13,10 +13,15 @@ LocationInformationModel *LocationInformationModel::instance()
 	return self;
 }
 
-LocationInformationModel::LocationInformationModel(QObject *obj) : QAbstractListModel(obj), internalRowCount(0)
+LocationInformationModel::LocationInformationModel(QObject *obj) : QAbstractTableModel(obj), internalRowCount(0)
 {
 }
 
+int LocationInformationModel::columnCount(const QModelIndex &parent) const
+{
+	return COLUMNS;
+}
+
 int LocationInformationModel::rowCount(const QModelIndex &parent) const
 {
 	Q_UNUSED(parent);
@@ -33,8 +38,20 @@ QVariant LocationInformationModel::data(const QModelIndex &index, int role) cons
 		return QVariant();
 
 	switch(role) {
-		case Qt::DisplayRole : return qPrintable(ds->name);
-		case DIVE_SITE_UUID  : return ds->uuid;
+	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 COORDS: return "TODO";
+		case DESCRIPTION: return ds->description;
+		case NOTES: return ds->name;
+		case TAXONOMY_1: return "TODO";
+		case TAXONOMY_2: return "TODO";
+		case TAXONOMY_3: return "TODO";
+		}
+	break;
 	}
 
 	return QVariant();
diff --git a/qt-models/divelocationmodel.h b/qt-models/divelocationmodel.h
index c7d8c2e..8cae3a0 100644
--- a/qt-models/divelocationmodel.h
+++ b/qt-models/divelocationmodel.h
@@ -1,15 +1,16 @@
 #ifndef DIVELOCATIONMODEL_H
 #define DIVELOCATIONMODEL_H
 
-#include <QAbstractListModel>
+#include <QAbstractTableModel>
 #include <QStringListModel>
 #include <stdint.h>
 
-class LocationInformationModel : public QAbstractListModel {
+class LocationInformationModel : public QAbstractTableModel {
 Q_OBJECT
 public:
-	enum { DIVE_SITE_UUID = Qt::UserRole+1};
+	enum Columns { UUID, NAME, LATITUDE, LONGITUDE, COORDS, DESCRIPTION, NOTES, TAXONOMY_1, TAXONOMY_2, TAXONOMY_3, COLUMNS};
 	static LocationInformationModel *instance();
+	int columnCount(const QModelIndex &parent) const;
 	int rowCount(const QModelIndex &parent = QModelIndex()) const;
 	QVariant data(const QModelIndex &index = QModelIndex(), int role = Qt::DisplayRole) const;
 	int32_t addDiveSite(const QString& name, int lat = 0, int lon = 0);
-- 
2.4.5

From e68fc515a7bee857824e41857d1e211f7fb60617 Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Wed, 1 Jul 2015 18:35:55 -0300
Subject: [PATCH 1/6] Remove location completion model.

This is a functional but hard to expand model for the dive sites.

Signed-off-by: Tomaz Canabrava <[email protected]>
---
 qt-models/completionmodels.cpp | 13 -------------
 qt-ui/maintab.cpp              |  8 --------
 2 files changed, 21 deletions(-)

diff --git a/qt-models/completionmodels.cpp b/qt-models/completionmodels.cpp
index f2e70af..ff2afd9 100644
--- a/qt-models/completionmodels.cpp
+++ b/qt-models/completionmodels.cpp
@@ -42,19 +42,6 @@ CREATE_CSV_UPDATE_METHOD(BuddyCompletionModel, buddy);
 CREATE_CSV_UPDATE_METHOD(DiveMasterCompletionModel, divemaster);
 CREATE_UPDATE_METHOD(SuitCompletionModel, suit);
 
-void LocationCompletionModel::updateModel()
-{
-	QStringList list;
-	struct dive_site *ds;
-	int i = 0;
-	for_each_dive_site(i, ds) {
-		if (!list.contains(ds->name))
-			list.append(ds->name);
-	}
-	std::sort(list.begin(), list.end());
-	setStringList(list);
-}
-
 void TagCompletionModel::updateModel()
 {
 	if (g_tag_list == NULL)
diff --git a/qt-ui/maintab.cpp b/qt-ui/maintab.cpp
index d60e22a..1236c94 100644
--- a/qt-ui/maintab.cpp
+++ b/qt-ui/maintab.cpp
@@ -100,8 +100,6 @@ MainTab::MainTab(QWidget *parent) : QTabWidget(parent),
 	connect(ui.cylinders->view(), SIGNAL(clicked(QModelIndex)), this, SLOT(editCylinderWidget(QModelIndex)));
 	connect(ui.weights->view(), SIGNAL(clicked(QModelIndex)), this, SLOT(editWeightWidget(QModelIndex)));
 
-	LocationCompletionModel *locationCompletion = new LocationCompletionModel();
-	ui.location->setCompleter(new QCompleter(locationCompletion));
 	ui.location->completer()->setCaseSensitivity(Qt::CaseInsensitive);
 	ui.location->completer()->setCompletionMode(QCompleter::PopupCompletion);
 
@@ -470,9 +468,6 @@ void MainTab::updateDiveInfo(bool clear)
 	struct dive *prevd;
 	char buf[1024];
 
-	LocationCompletionModel *m = qobject_cast<LocationCompletionModel*>(ui.location->completer()->model());
-	m->updateModel();
-
 	process_selected_dives();
 	process_all_dives(&displayed_dive, &prevd);
 
@@ -749,7 +744,6 @@ void MainTab::reload()
 {
 	suitModel.updateModel();
 	buddyModel.updateModel();
-	locationModel.updateModel();
 	diveMasterModel.updateModel();
 	tagModel.updateModel();
 }
@@ -1363,8 +1357,6 @@ void MainTab::on_location_editingFinished()
 		markChangedWidget(ui.location);
 
 		LocationInformationModel::instance()->update();
-		LocationCompletionModel *m = qobject_cast<LocationCompletionModel*>(ui.location->completer()->model());
-		m->updateModel();
 		emit diveSiteChanged(uuid);
 		return;
 	}
-- 
2.4.5

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

Reply via email to