Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Fix initialization of the global instance of KisStorageModel

Dmitry Kazakov760fbe0dc11c
diff --git a/libs/resources/KisResourceLocator.cpp b/libs/resources/KisResourceLocator.cpp
index 8f0fe46b6d..e085013a43 100644
--- a/libs/resources/KisResourceLocator.cpp
+++ b/libs/resources/KisResourceLocator.cpp
@@ -972,7 +972,7 @@ void KisResourceLocator::updateFontStorage()
if (!KisResourceCacheDb::synchronizeStorage(fontStorage())) {
qWarning() << i18n("Could not synchronize updated font registery with the database");
} else {
- emit storageUpdated(fontStorage()->location());
+ Q_EMIT storageResynchronized(fontStorage()->location(), false);
}
}
@@ -1051,6 +1051,9 @@ bool KisResourceLocator::initializeDb()
d->errorMessages.append(QString("Could not add tags for storage %1 to the cache database").arg(storage->location()));
}
}
+
+ Q_EMIT storagesBulkSynchronizationFinished();
+
return (d->errorMessages.isEmpty());
}
@@ -1207,6 +1210,8 @@ bool KisResourceLocator::synchronizeDb()
Q_FOREACH(const KisResourceStorageSP storage, d->storages) {
if (!KisResourceCacheDb::synchronizeStorage(storage)) {
d->errorMessages.append(i18n("Could not synchronize %1 with the database", storage->location()));
+ } else {
+ Q_EMIT storageResynchronized(storage->location(), true);
}
}
@@ -1216,6 +1221,8 @@ bool KisResourceLocator::synchronizeDb()
}
}
+ Q_EMIT storagesBulkSynchronizationFinished();
+
/**
* In the current layout of the database we cannot set FOREIGN KEY
* for the metadata table (since it links to both, resources and storages),
diff --git a/libs/resources/KisResourceLocator.h b/libs/resources/KisResourceLocator.h
index 7ddd0e59f8..10270cd04f 100644
--- a/libs/resources/KisResourceLocator.h
+++ b/libs/resources/KisResourceLocator.h
@@ -147,8 +147,17 @@ Q_SIGNALS:
/// Emitted when a resource changes its active state
void resourceActiveStateChanged(const QString &resourceType, int resourceId);
- /// Emitted when a storage is updated.
- void storageUpdated(const QString &location);
+ /// Emitted when a storage is resynchronized using KisresourceCacheDb::synchronizeStorage()
+ ///
+ /// if \p isBulkResynchronization then this resynchronization happened as a part
+ /// of bulk resynchronization at the start of Krita. At the end of this bulk
+ /// action storagesBulkSynchronizationFinished() will be emitted as well.
+ void storageResynchronized(const QString &storage, bool isBulkResynchronization);
+
+ /// Emitted when bulk-synchronization of all the storages has been finished
+ ///
+ /// \see storageResynchronized
+ void storagesBulkSynchronizationFinished();
private:
diff --git a/libs/resources/KisResourceModel.cpp b/libs/resources/KisResourceModel.cpp
index 3b48a9f49c..1289e56615 100644
--- a/libs/resources/KisResourceModel.cpp
+++ b/libs/resources/KisResourceModel.cpp
@@ -38,16 +38,16 @@ KisAllResourcesModel::KisAllResourcesModel(const QString &resourceType, QObject
/// we don't handle KisResourceLocator::storage{Added,Removed} signals
/// here, we use per-resource notifications from KisResourceLocator instead
- connect(KisStorageModel::instance(), SIGNAL(storageEnabled(const QString&)), this, SLOT(storageActiveStateChanged(const QString&)));
- connect(KisStorageModel::instance(), SIGNAL(storageDisabled(const QString&)), this, SLOT(storageActiveStateChanged(const QString&)));
- connect(KisStorageModel::instance(), SIGNAL(storageUpdated(const QString&)), this, SLOT(storageUpdated(const QString&)));
+ connect(KisStorageModel::instance(), &KisStorageModel::storageEnabled, this, &KisAllResourcesModel::storageActiveStateChanged);
+ connect(KisStorageModel::instance(), &KisStorageModel::storageDisabled, this, &KisAllResourcesModel::storageActiveStateChanged);
+ connect(KisStorageModel::instance(), &KisStorageModel::storageResynchronized, this, &KisAllResourcesModel::storageResynchronized);
+ connect(KisStorageModel::instance(), &KisStorageModel::storagesBulkSynchronizationFinished, this, &KisAllResourcesModel::storagesBulkSynchronizationFinished);
- connect(KisResourceLocator::instance(), SIGNAL(beginExternalResourceImport(QString, int)), this, SLOT(beginExternalResourceImport(QString, int)));
- connect(KisResourceLocator::instance(), SIGNAL(endExternalResourceImport(QString)), this, SLOT(endExternalResourceImport(QString)));
-
- connect(KisResourceLocator::instance(), SIGNAL(beginExternalResourceRemove(QString, QVector<int>)), this, SLOT(beginExternalResourceRemove(QString, QVector<int>)));
- connect(KisResourceLocator::instance(), SIGNAL(endExternalResourceRemove(QString)), this, SLOT(endExternalResourceRemove(QString)));
- connect(KisResourceLocator::instance(), SIGNAL(resourceActiveStateChanged(QString, int)), this, SLOT(slotResourceActiveStateChanged(QString, int)));
+ connect(KisResourceLocator::instance(), &KisResourceLocator::beginExternalResourceImport, this, &KisAllResourcesModel::beginExternalResourceImport);
+ connect(KisResourceLocator::instance(), &KisResourceLocator::endExternalResourceImport, this, &KisAllResourcesModel::endExternalResourceImport);
+ connect(KisResourceLocator::instance(), &KisResourceLocator::beginExternalResourceRemove, this, &KisAllResourcesModel::beginExternalResourceRemove);
+ connect(KisResourceLocator::instance(), &KisResourceLocator::endExternalResourceRemove, this, &KisAllResourcesModel::endExternalResourceRemove);
+ connect(KisResourceLocator::instance(), &KisResourceLocator::resourceActiveStateChanged, this, &KisAllResourcesModel::slotResourceActiveStateChanged);
d->resourceType = resourceType;
@@ -668,7 +668,21 @@ void KisAllResourcesModel::storageActiveStateChanged(const QString &location)
}
}
-void KisAllResourcesModel::storageUpdated(const QString &location)
+void KisAllResourcesModel::storageResynchronized(const QString &storage, bool isBulkResynchronization)
+{
+ Q_UNUSED(storage);
+
+ // we handle bulk-synchronization separately in slotStoragesBulkSynchronizationFinished()
+ if (isBulkResynchronization) return;
+
+ // TODO: ideally, we should use more fine-grained updates for
+ // updating the storages, but let's keep it this
+ beginResetModel();
+ resetQuery();
+ endResetModel();
+}
+
+void KisAllResourcesModel::storagesBulkSynchronizationFinished()
{
beginResetModel();
resetQuery();
diff --git a/libs/resources/KisResourceModel.h b/libs/resources/KisResourceModel.h
index b94898b711..4ba8776af6 100644
--- a/libs/resources/KisResourceModel.h
+++ b/libs/resources/KisResourceModel.h
@@ -271,7 +271,8 @@ public:
private Q_SLOTS:
void storageActiveStateChanged(const QString &location);
- void storageUpdated(const QString &location);
+ void storageResynchronized(const QString &storage, bool isBulkResynchronization);
+ void storagesBulkSynchronizationFinished();
/**
* A special connection for KisResourceLocator, which can import
diff --git a/libs/resources/KisStorageModel.cpp b/libs/resources/KisStorageModel.cpp
index 16c6924109..5a89ba3c8c 100644
--- a/libs/resources/KisStorageModel.cpp
+++ b/libs/resources/KisStorageModel.cpp
@@ -22,10 +22,11 @@
#include <kconfiggroup.h>
#include <ksharedconfig.h>
+#include <kis_debug.h>
+
Q_GLOBAL_STATIC(KisStorageModel, s_instance)
struct KisStorageModel::Private {
- int cachedRowCount {-1};
QList<QString> storages;
};
@@ -33,28 +34,12 @@ KisStorageModel::KisStorageModel(QObject *parent)
: QAbstractTableModel(parent)
, d(new Private())
{
- connect(KisResourceLocator::instance(), SIGNAL(storageAdded(const QString&)), this, SLOT(addStorage(const QString&)));
- connect(KisResourceLocator::instance(), SIGNAL(storageRemoved(const QString&)), this, SLOT(removeStorage(const QString&)));
- connect(KisResourceLocator::instance(), SIGNAL(storageUpdated(const QString&)), this, SLOT(updateStorage(const QString&)));
-
- QSqlQuery query;
-
- bool r = query.prepare("SELECT location\n"
- "FROM storages\n"
- "ORDER BY id");
- if (!r) {
- qWarning() << "Could not prepare KisStorageModel query" << query.lastError();
- }
+ connect(KisResourceLocator::instance(), &KisResourceLocator::storageAdded, this, &KisStorageModel::addStorage);
+ connect(KisResourceLocator::instance(), &KisResourceLocator::storageRemoved, this, &KisStorageModel::removeStorage);
+ connect(KisResourceLocator::instance(), &KisResourceLocator::storageResynchronized, this, &KisStorageModel::storageResynchronized);
+ connect(KisResourceLocator::instance(), &KisResourceLocator::storagesBulkSynchronizationFinished, this, &KisStorageModel::slotStoragesBulkSynchronizationFinished);
- r = query.exec();
-
- if (!r) {
- qWarning() << "Could not execute KisStorageModel query" << query.lastError();
- }
-
- while (query.next()) {
- d->storages << query.value(0).toString();
- }
+ resetQuery();
}
KisStorageModel *KisStorageModel::instance()
@@ -513,9 +498,35 @@ void KisStorageModel::removeStorage(const QString &location)
endRemoveRows();
}
-void KisStorageModel::updateStorage(const QString &location)
+void KisStorageModel::slotStoragesBulkSynchronizationFinished()
{
- emit storageUpdated(location);
+ beginResetModel();
+ resetQuery();
+ endResetModel();
+
+ Q_EMIT storagesBulkSynchronizationFinished();
}
+void KisStorageModel::resetQuery()
+{
+ QSqlQuery query;
+
+ bool r = query.prepare(
+ "SELECT location\n"
+ "FROM storages\n"
+ "ORDER BY id");
+ if (!r) {
+ qWarning() << "Could not prepare KisStorageModel query" << query.lastError();
+ }
+
+ r = query.exec();
+ if (!r) {
+ qWarning() << "Could not execute KisStorageModel query" << query.lastError();
+ }
+
+ d->storages.clear();
+ while (query.next()) {
+ d->storages << query.value(0).toString();
+ }
+}
\ No newline at end of file
diff --git a/libs/resources/KisStorageModel.h b/libs/resources/KisStorageModel.h
index c425f5a4be..9bc853787d 100644
--- a/libs/resources/KisStorageModel.h
+++ b/libs/resources/KisStorageModel.h
@@ -68,7 +68,12 @@ Q_SIGNALS:
void storageEnabled(const QString &storage);
void storageDisabled(const QString &storage);
- void storageUpdated(const QString &storage);
+
+ /// Emitted when an individual storage is initialized
+ void storageResynchronized(const QString &storage, bool isBulkResynchronization);
+
+ /// Emitted on loading when all the storages are finished initialization
+ void storagesBulkSynchronizationFinished();
private Q_SLOTS:
@@ -78,13 +83,11 @@ private Q_SLOTS:
/// This is called when a storage really is deleted both from database and anywhere else
void removeStorage(const QString &location);
- /// Called when the storage is updated and everything needs to be reloaded.
- void updateStorage(const QString &location);
-
-private:
+ /// called when storages finished synchronization process
+ void slotStoragesBulkSynchronizationFinished();
- KisStorageModel(const KisStorageModel&);
- KisStorageModel operator=(const KisStorageModel&);
+private :
+ void resetQuery();
static QImage getThumbnailFromQuery(const QSqlQuery &query);