| 
									
										
										
										
											2017-04-27 20:25:32 +02:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							| 
									
										
										
										
											2016-04-04 22:02:03 -07:00
										 |  |  | #include "qt-models/completionmodels.h"
 | 
					
						
							|  |  |  | #include "core/dive.h"
 | 
					
						
							| 
									
										
										
										
											2019-05-30 18:29:36 +02:00
										 |  |  | #include "core/tag.h"
 | 
					
						
							| 
									
										
										
										
											2015-09-02 21:22:29 -03:00
										 |  |  | #include <QSet>
 | 
					
						
							| 
									
										
										
										
											2013-08-13 10:30:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-26 07:27:47 -07:00
										 |  |  | #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
 | 
					
						
							|  |  |  | #define SKIP_EMPTY Qt::SkipEmptyParts
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #define SKIP_EMPTY QString::SkipEmptyParts
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-14 17:42:59 +01:00
										 |  |  | CompletionModelBase::CompletionModelBase() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	connect(&diveListNotifier, &DiveListNotifier::dataReset, this, &CompletionModelBase::updateModel); | 
					
						
							|  |  |  | 	connect(&diveListNotifier, &DiveListNotifier::divesImported, this, &CompletionModelBase::updateModel); | 
					
						
							|  |  |  | 	connect(&diveListNotifier, &DiveListNotifier::divesChanged, this, &CompletionModelBase::divesChanged); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CompletionModelBase::updateModel() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	setStringList(getStrings()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CompletionModelBase::divesChanged(const QVector<dive *> &, DiveField field) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (relevantDiveField(field)) | 
					
						
							|  |  |  | 		updateModel(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-12 17:48:54 +01:00
										 |  |  | static QStringList getCSVList(char *dive::*item) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QSet<QString> set; | 
					
						
							|  |  |  | 	struct dive *dive; | 
					
						
							|  |  |  | 	int i = 0; | 
					
						
							|  |  |  | 	for_each_dive (i, dive) { | 
					
						
							|  |  |  | 		QString str(dive->*item); | 
					
						
							|  |  |  | 		for (const QString &value: str.split(",", SKIP_EMPTY)) | 
					
						
							|  |  |  | 			set.insert(value.trimmed()); | 
					
						
							| 
									
										
										
										
											2014-02-27 20:09:57 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-11-12 17:48:54 +01:00
										 |  |  | 	QStringList setList = set.values(); | 
					
						
							|  |  |  | 	std::sort(setList.begin(), setList.end()); | 
					
						
							|  |  |  | 	return setList; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-01-15 22:35:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-14 17:42:59 +01:00
										 |  |  | QStringList BuddyCompletionModel::getStrings() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return getCSVList(&dive::buddy); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool BuddyCompletionModel::relevantDiveField(const DiveField &f) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return f.buddy; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QStringList DiveMasterCompletionModel::getStrings() | 
					
						
							| 
									
										
										
										
											2020-11-12 17:48:54 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-14 17:42:59 +01:00
										 |  |  | 	return getCSVList(&dive::divemaster); | 
					
						
							| 
									
										
										
										
											2020-11-12 17:48:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-14 17:42:59 +01:00
										 |  |  | bool DiveMasterCompletionModel::relevantDiveField(const DiveField &f) | 
					
						
							| 
									
										
										
										
											2020-11-12 17:48:54 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-14 17:42:59 +01:00
										 |  |  | 	return f.divemaster; | 
					
						
							| 
									
										
										
										
											2020-11-12 17:48:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-14 17:42:59 +01:00
										 |  |  | QStringList SuitCompletionModel::getStrings() | 
					
						
							| 
									
										
										
										
											2020-11-12 17:48:54 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	QStringList list; | 
					
						
							|  |  |  | 	struct dive *dive; | 
					
						
							|  |  |  | 	int i = 0; | 
					
						
							|  |  |  | 	for_each_dive (i, dive) { | 
					
						
							|  |  |  | 		QString suit(dive->suit); | 
					
						
							|  |  |  | 		if (!list.contains(suit)) | 
					
						
							|  |  |  | 			list.append(suit); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	std::sort(list.begin(), list.end()); | 
					
						
							| 
									
										
										
										
											2020-11-14 17:42:59 +01:00
										 |  |  | 	return list; | 
					
						
							| 
									
										
										
										
											2020-11-12 17:48:54 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-08-13 10:30:22 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-14 17:42:59 +01:00
										 |  |  | bool SuitCompletionModel::relevantDiveField(const DiveField &f) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return f.suit; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QStringList TagCompletionModel::getStrings() | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-01-16 11:50:56 +07:00
										 |  |  | 	if (g_tag_list == NULL) | 
					
						
							| 
									
										
										
										
											2020-11-14 17:42:59 +01:00
										 |  |  | 		return {}; | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | 	QStringList list; | 
					
						
							| 
									
										
										
										
											2015-09-09 20:52:43 +02:00
										 |  |  | 	struct tag_entry *current_tag_entry = g_tag_list; | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | 	while (current_tag_entry != NULL) { | 
					
						
							|  |  |  | 		list.append(QString(current_tag_entry->tag->name)); | 
					
						
							|  |  |  | 		current_tag_entry = current_tag_entry->next; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-11-14 17:42:59 +01:00
										 |  |  | 	std::sort(list.begin(), list.end()); | 
					
						
							|  |  |  | 	return list; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool TagCompletionModel::relevantDiveField(const DiveField &f) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return f.tags; | 
					
						
							| 
									
										
										
										
											2013-11-02 02:20:02 +01:00
										 |  |  | } |