21#include <QCryptographicHash>
24#include "moc_qgsstoredquerymanager.cpp"
26using namespace Qt::StringLiterals;
39 if (
query.isEmpty() || name.isEmpty() )
42 bool wasAdded =
false;
43 bool wasUpdated =
false;
46 const QString hash = getQueryHash( name );
52 const bool isExisting = sTreeStoredQueries->items().contains( hash );
53 wasAdded = !isExisting;
54 wasUpdated = isExisting;
55 settingQueryName->setValue( name, hash );
56 settingQueryDefinition->setValue(
query, hash );
63 wasAdded = !isExisting;
64 wasUpdated = isExisting;
73 else if ( wasUpdated )
82 bool wasDeleted =
false;
84 const QString hash = getQueryHash( name );
90 wasDeleted = sTreeStoredQueries->items().contains( hash );
91 sTreeStoredQueries->deleteItem( hash );
114 const QStringList hashes = sTreeStoredQueries->items();
115 names.reserve( hashes.size() );
116 for (
const QString &hash : hashes )
118 names.append( settingQueryName->value( hash ) );
126 names.reserve( hashes.size() );
127 for (
const QString &hash : hashes )
129 names.append(
QgsProject::instance()->readEntry( u
"DBManager"_s, u
"savedQueries/%1/name"_s.arg( hash ) ) );
140 const QString hash = getQueryHash( name );
146 return settingQueryDefinition->value( hash );
159 QList<QgsStoredQueryManager::QueryDetails> res;
161 const QStringList localProfileHashes = sTreeStoredQueries->items();
163 res.reserve( localProfileHashes.size() + projectHashes.size() );
165 for (
const QString &hash : localProfileHashes )
168 details.
name = settingQueryName->value( hash );
169 details.
definition = settingQueryDefinition->value( hash );
171 res.append( details );
174 for (
const QString &hash : projectHashes )
180 res.append( details );
184 if ( a.name == b.name )
186 if ( a.backend == b.backend )
189 return a.backend == Qgis::QueryStorageBackend::CurrentProject;
192 return QString::localeAwareCompare( a.
name, b.
name ) < 0;
198QString QgsStoredQueryManager::getQueryHash(
const QString &name )
201 QByteArray nameUtf8 = name.toUtf8();
202 QByteArray hash = QCryptographicHash::hash( nameUtf8, QCryptographicHash::Md5 ).toHex();
203 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