21#include <QCryptographicHash>
24#include "moc_qgsstoredquerymanager.cpp"
26using namespace Qt::StringLiterals;
40 if (
query.isEmpty() || name.isEmpty() )
43 bool wasAdded =
false;
44 bool wasUpdated =
false;
47 const QString hash = getQueryHash( name );
53 const bool isExisting = sTreeStoredQueries->items().contains( hash );
54 wasAdded = !isExisting;
55 wasUpdated = isExisting;
56 settingQueryName->setValue( name, hash );
57 settingQueryDefinition->setValue(
query, hash );
64 wasAdded = !isExisting;
65 wasUpdated = isExisting;
74 else if ( wasUpdated )
83 bool wasDeleted =
false;
85 const QString hash = getQueryHash( name );
91 wasDeleted = sTreeStoredQueries->items().contains( hash );
92 sTreeStoredQueries->deleteItem( hash );
100 "DBManager", u
"savedQueries/%1"_s.arg( hash )
117 const QStringList hashes = sTreeStoredQueries->items();
118 names.reserve( hashes.size() );
119 for (
const QString &hash : hashes )
121 names.append( settingQueryName->value( hash ) );
129 names.reserve( hashes.size() );
130 for (
const QString &hash : hashes )
133 u
"DBManager"_s, u
"savedQueries/%1/name"_s.arg( hash )
145 const QString hash = getQueryHash( name );
151 return settingQueryDefinition->value( hash );
164 QList<QgsStoredQueryManager::QueryDetails> res;
166 const QStringList localProfileHashes = sTreeStoredQueries->items();
168 res.reserve( localProfileHashes.size() + projectHashes.size() );
170 for (
const QString &hash : localProfileHashes )
173 details.
name = settingQueryName->value( hash );
174 details.
definition = settingQueryDefinition->value( hash );
176 res.append( details );
179 for (
const QString &hash : projectHashes )
183 u
"DBManager"_s, u
"savedQueries/%1/name"_s.arg( hash )
186 u
"DBManager"_s, u
"savedQueries/%1/query"_s.arg( hash )
189 res.append( details );
193 if ( a.name == b.name )
195 if ( a.backend == b.backend )
198 return a.backend == Qgis::QueryStorageBackend::CurrentProject;
201 return QString::localeAwareCompare( a.
name, b.
name ) < 0;
207QString QgsStoredQueryManager::getQueryHash(
const QString &name )
210 QByteArray nameUtf8 = name.toUtf8();
211 QByteArray hash = QCryptographicHash::hash( nameUtf8, QCryptographicHash::Md5 ).toHex();
212 return u
"q%1"_s.arg( QString::fromUtf8( hash ) );
QueryStorageBackend
Stored query storage backends.
@ CurrentProject
Current QGIS project.
@ LocalProfile
Local user profile.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QString readEntry(const QString &scope, const QString &key, const QString &def=QString(), bool *ok=nullptr) const
Reads a string from the specified scope and key.
bool writeEntry(const QString &scope, const QString &key, bool value)
Write a boolean value to the project file.
QStringList subkeyList(const QString &scope, const QString &key) const
Returns a list of child keys which contain other keys that exist within the specified scope and key.
bool removeEntry(const QString &scope, const QString &key)
Remove the given key from the specified scope.
Contains details about a stored query.
QString name
Name of the query.
QString definition
Query definition.
Qgis::QueryStorageBackend backend
Storage backend.
QList< QgsStoredQueryManager::QueryDetails > allQueries() const
Returns details of all queries stored in the manager.
QStringList allQueryNames(Qgis::QueryStorageBackend backend=Qgis::QueryStorageBackend::LocalProfile) const
Returns a list of the names of all stored queries for the specified backend.
void queryRemoved(const QString &name, Qgis::QueryStorageBackend backend)
Emitted when a query is removed from the manager.
void queryChanged(const QString &name, Qgis::QueryStorageBackend backend)
Emitted when an existing query is changed in the manager.
void queryAdded(const QString &name, Qgis::QueryStorageBackend backend)
Emitted when a query is added to the manager.
QgsStoredQueryManager(QObject *parent=nullptr)
Constructor for QgsStoredQueryManager, with the specified parent object.
QString query(const QString &name, Qgis::QueryStorageBackend backend=Qgis::QueryStorageBackend::LocalProfile) const
Returns the query definition with matching name, from the specified backend.
void removeQuery(const QString &name, Qgis::QueryStorageBackend backend=Qgis::QueryStorageBackend::LocalProfile)
Removes the stored query with matching name.
void storeQuery(const QString &name, const QString &query, Qgis::QueryStorageBackend backend=Qgis::QueryStorageBackend::LocalProfile)
Saves a query to the manager.
#define BUILTIN_UNREACHABLE