From 654ce36a8a49db2154094076f650ebc37d160b87 Mon Sep 17 00:00:00 2001 From: Igor Malinovskiy Date: Tue, 28 Dec 2021 10:07:15 +0200 Subject: [PATCH] Revert changes added to tree model --- src/app/models/connectionsmanager.cpp | 4 +- .../items/abstractnamespaceitem.cpp | 18 ++++--- .../connections-tree/items/databaseitem.cpp | 18 +++---- .../connections-tree/items/keyitem.cpp | 6 +-- .../connections-tree/items/namespaceitem.cpp | 6 +-- .../connections-tree/items/serveritem.cpp | 14 +++--- .../connections-tree/items/treeitem.cpp | 6 +-- src/modules/connections-tree/model.cpp | 47 +++++++------------ src/modules/connections-tree/model.h | 8 ++-- 9 files changed, 63 insertions(+), 64 deletions(-) diff --git a/src/app/models/connectionsmanager.cpp b/src/app/models/connectionsmanager.cpp index e95ed6a9..ea7e681b 100644 --- a/src/app/models/connectionsmanager.cpp +++ b/src/app/models/connectionsmanager.cpp @@ -58,7 +58,7 @@ void ConnectionsManager::updateGroup(const ConnectionGroup &group) return; } - itemChanged(serverGroup); + emit itemChanged(serverGroup); saveConfig(); @@ -352,7 +352,7 @@ void ConnectionsManager::createServerItemForConnection( [this, serverItem]() { if (!serverItem) return; - itemChanged( + emit itemChanged( serverItem.dynamicCast().toWeakRef()); }); diff --git a/src/modules/connections-tree/items/abstractnamespaceitem.cpp b/src/modules/connections-tree/items/abstractnamespaceitem.cpp index 1f92b0e1..29790460 100644 --- a/src/modules/connections-tree/items/abstractnamespaceitem.cpp +++ b/src/modules/connections-tree/items/abstractnamespaceitem.cpp @@ -268,7 +268,7 @@ void AbstractNamespaceItem::clearLoader() { } void AbstractNamespaceItem::showLoadingError(const QString &err) { - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); emit m_model.error(err); } @@ -298,7 +298,7 @@ void AbstractNamespaceItem::sortChilds() { m_model.beforeItemLayoutChanged(getSelf()); std::sort(m_childItems.begin(), m_childItems.end(), compareChilds); m_model.itemLayoutChanged(getSelf()); - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); } void AbstractNamespaceItem::renderRawKeys( @@ -374,9 +374,11 @@ QHash> AbstractNamespaceItem::eventHandlers() { } getMemoryUsage([this](qlonglong) { - sortChilds(); - unlock(); - m_runningOperation.clear(); + QTimer::singleShot(0, this, [this]() { + sortChilds(); + unlock(); + m_runningOperation.clear(); + }); }); }); }); @@ -407,7 +409,9 @@ void AbstractNamespaceItem::fetchMore() { int childsCount = m_childItems.size(); auto rawKeys = m_rawChildKeys; - m_model.itemChanged(getSelf()); + + emit m_model.itemChanged(getSelf()); + m_rawChildKeys.clear(); return renderRawKeys( @@ -451,7 +455,7 @@ void AbstractNamespaceItem::calculateUsedMemory( QMutexLocker locker(&m_updateUsedMemoryMutex); Q_UNUSED(locker); m_usedMemory += result; - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); (*resultsRemaining)--; diff --git a/src/modules/connections-tree/items/databaseitem.cpp b/src/modules/connections-tree/items/databaseitem.cpp index 86473860..2f289052 100644 --- a/src/modules/connections-tree/items/databaseitem.cpp +++ b/src/modules/connections-tree/items/databaseitem.cpp @@ -75,7 +75,7 @@ void DatabaseItem::loadKeys(std::function callback, bool partialReload) if (dbMapping.contains(m_dbIndex)) { m_keysCount = dbMapping[m_dbIndex]; - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); } }; @@ -90,7 +90,7 @@ void DatabaseItem::loadKeys(std::function callback, bool partialReload) m_model.expandItem(getSelf()); } - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); if (callback) { callback(); @@ -160,7 +160,7 @@ void DatabaseItem::setMetadata(const QString& key, QVariant value) { liveUpdateTimer()->start(); } - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); } } @@ -225,7 +225,9 @@ void DatabaseItem::performLiveUpdate() { if (m_childItems.size() >= settings.value("app/liveUpdateKeysLimit", 1000).toInt()) { liveUpdateTimer()->stop(); - m_model.itemChanged(getSelf()); + + emit m_model.itemChanged(getSelf()); + QMessageBox::warning( nullptr, QCoreApplication::translate("RDM", "Live update was disabled"), @@ -236,20 +238,20 @@ void DatabaseItem::performLiveUpdate() { "settings.")); } else { liveUpdateTimer()->start(); - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); } }, true); } void DatabaseItem::filterKeys(const QRegExp& filter) { m_filter = filter; - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); reload(); } void DatabaseItem::resetFilter() { m_filter = QRegExp(m_operations->defaultFilter()); - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); reload(); } @@ -271,7 +273,7 @@ QHash> DatabaseItem::eventHandlers() { events.insert("right-click", [this]() { if (m_childItems.size() != 0) return; - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); }); events.insert("add_key", [this]() { diff --git a/src/modules/connections-tree/items/keyitem.cpp b/src/modules/connections-tree/items/keyitem.cpp index 732d6fcd..693bddaf 100644 --- a/src/modules/connections-tree/items/keyitem.cpp +++ b/src/modules/connections-tree/items/keyitem.cpp @@ -92,7 +92,7 @@ int KeyItem::getDbIndex() const { void KeyItem::setRemoved() { m_removed = true; - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); } void KeyItem::getMemoryUsage(std::function callback) { @@ -104,14 +104,14 @@ void KeyItem::getMemoryUsage(std::function callback) { [this, callback](qlonglong result) { m_usedMemory = result; callback(result); - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); }, [](qlonglong){}); } void KeyItem::setFullPath(const QByteArray& p) { m_fullPath = p; - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); } QHash> KeyItem::eventHandlers() { diff --git a/src/modules/connections-tree/items/namespaceitem.cpp b/src/modules/connections-tree/items/namespaceitem.cpp index 00c33c2d..aea9b828 100644 --- a/src/modules/connections-tree/items/namespaceitem.cpp +++ b/src/modules/connections-tree/items/namespaceitem.cpp @@ -51,7 +51,7 @@ void NamespaceItem::setRemoved() { clear(); - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); } void NamespaceItem::load() { @@ -60,7 +60,7 @@ void NamespaceItem::load() { unlock(); setExpanded(true); - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); m_model.expandItem(getSelf()); }; @@ -115,7 +115,7 @@ QHash> NamespaceItem::eventHandlers() { load(); } else if (!isExpanded()) { setExpanded(true); - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); m_model.expandItem(getSelf()); } }); diff --git a/src/modules/connections-tree/items/serveritem.cpp b/src/modules/connections-tree/items/serveritem.cpp index 0d35cdb9..193d8d45 100644 --- a/src/modules/connections-tree/items/serveritem.cpp +++ b/src/modules/connections-tree/items/serveritem.cpp @@ -93,12 +93,14 @@ void ServerItem::load() { ++db; } - m_model.beforeChildLoaded(getSelf(), dbs.size()); - m_databases = dbs; - m_model.childLoaded(getSelf()); + QTimer::singleShot(0, this, [this, dbs]() { + m_model.beforeChildLoaded(getSelf(), dbs.size()); + m_databases = dbs; + m_model.childLoaded(getSelf()); - unlock(); - m_model.expandItem(getSelf()); + unlock(); + m_model.expandItem(getSelf()); + }); }; m_currentOperation = m_operations->getDatabases(callback); @@ -180,7 +182,7 @@ QHash > ServerItem::eventHandlers() { events.insert("reload", [this]() { reload(); - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); }); events.insert("unload", [this]() { unload(); }); diff --git a/src/modules/connections-tree/items/treeitem.cpp b/src/modules/connections-tree/items/treeitem.cpp index 32cd7895..b5562af2 100644 --- a/src/modules/connections-tree/items/treeitem.cpp +++ b/src/modules/connections-tree/items/treeitem.cpp @@ -45,13 +45,13 @@ ConnectionsTree::Model &ConnectionsTree::TreeItem::model() { return m_model; } void ConnectionsTree::TreeItem::lock() { m_locked = true; if (getSelf()) - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); } void ConnectionsTree::TreeItem::unlock() { m_locked = false; if (getSelf()) - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); } QHash> @@ -66,7 +66,7 @@ void ConnectionsTree::TreeItem::handleEvent(QString event) { if (isLocked() && event != "cancel") { qDebug() << "Item is locked. Ignore event: " << event; - m_model.itemChanged(getSelf()); + emit m_model.itemChanged(getSelf()); return; } diff --git a/src/modules/connections-tree/model.cpp b/src/modules/connections-tree/model.cpp index 07d459fe..f58bc2ae 100644 --- a/src/modules/connections-tree/model.cpp +++ b/src/modules/connections-tree/model.cpp @@ -14,6 +14,7 @@ Model::Model(QObject *parent) m_rawPointers(new QHash>()) { qRegisterMetaType>("QWeakPointer"); + QObject::connect(this, &Model::itemChanged, this, &Model::onItemChanged); } QVariant Model::data(const QModelIndex &index, int role) const { @@ -118,8 +119,8 @@ QModelIndex Model::getIndexFromItem(QWeakPointer item) { return createIndex(sRef->row(), 0, (void *)sRef.data()); } -void Model::itemChanged(QWeakPointer item) { - if (!item) return; +void Model::onItemChanged(QWeakPointer item) { + if (!item) return; auto index = getIndexFromItem(item); @@ -130,7 +131,7 @@ void Model::itemChanged(QWeakPointer item) { void Model::beforeItemChildsUnloaded(QWeakPointer item) { - if (!item) return; + if (!item) return; auto index = getIndexFromItem(item); @@ -146,7 +147,7 @@ void Model::beforeItemChildsUnloaded(QWeakPointer item) void Model::beforeChildLoadedAtPos(QWeakPointer item, int pos) { - if (!item) return; + if (!item) return; auto index = getIndexFromItem(item); @@ -157,7 +158,7 @@ void Model::beforeChildLoadedAtPos(QWeakPointer item, int pos) void Model::beforeChildLoaded(QWeakPointer item, int count) { - if (!item) return; + if (!item) return; auto index = getIndexFromItem(item); @@ -173,7 +174,7 @@ void Model::beforeChildLoaded(QWeakPointer item, int count) void Model::childLoaded(QWeakPointer item) { - if (!item) return; + if (!item) return; auto index = getIndexFromItem(item); @@ -184,26 +185,24 @@ void Model::childLoaded(QWeakPointer item) void Model::beforeItemChildRemoved(QWeakPointer item, int row) { - if (!item) return; + if (!item) return; auto index = getIndexFromItem(item); - if (!index.isValid()) return; - - qDebug() << "before child removal"; + if (!index.isValid()) return; beginRemoveRows(index, row, row); } void Model::itemChildRemoved(QWeakPointer childItem) { - if (!childItem) return; + if (!childItem) return; endRemoveRows(); } void Model::expandItem(QWeakPointer item) { - if (!item) return; + if (!item) return; auto index = getIndexFromItem(item); @@ -227,7 +226,7 @@ void Model::iterateAllChilds(QSharedPointer item, QList item) { - if (!item) return; + if (!item) return; auto itemS = item.toStrongRef(); @@ -237,23 +236,19 @@ void Model::beforeItemLayoutChanged(QWeakPointer item) { if (m_pendingChanges.contains(item)) { m_pendingChanges.remove(item); - } - - qDebug() << "Layout about to change"; + } emit layoutAboutToBeChanged({}, QAbstractItemModel::VerticalSortHint); QList pendingChanges; - iterateAllChilds(itemS, pendingChanges); - - qDebug() << "----- pending changes:" << pendingChanges.size(); + iterateAllChilds(itemS, pendingChanges); m_pendingChanges[item] = pendingChanges; } void Model::itemLayoutChanged(QWeakPointer item) { - if (!item) return; + if (!item) return; auto itemS = item.toStrongRef(); @@ -279,15 +274,9 @@ void Model::itemLayoutChanged(QWeakPointer item) { } auto from = change.second; - auto to = getIndexFromItem(child); - - qDebug() << "Update model index from " << from.row() << from.parent() << "to" << to.row() << to.parent(); + auto to = getIndexFromItem(child); changePersistentIndex(from, to); - } - - qDebug() << "layout changed"; - - QPersistentModelIndex indx(index); + } emit layoutChanged({}, QAbstractItemModel::VerticalSortHint); @@ -480,7 +469,7 @@ void Model::addRootItem(QSharedPointer item) { } void Model::removeRootItem(QSharedPointer item) { - if (!item) return; + if (!item) return; beginRemoveRows(QModelIndex(), item->row(), item->row()); m_treeItems.removeAll(item); diff --git a/src/modules/connections-tree/model.h b/src/modules/connections-tree/model.h index c853e085..e730e38d 100644 --- a/src/modules/connections-tree/model.h +++ b/src/modules/connections-tree/model.h @@ -68,9 +68,9 @@ class Model : public QAbstractItemModel { void error(const QString &err); - public: - void itemChanged(QWeakPointer item); + void itemChanged(QWeakPointer item); + public: void beforeItemChildsUnloaded(QWeakPointer item); void beforeChildLoadedAtPos(QWeakPointer item, int pos); @@ -90,6 +90,8 @@ class Model : public QAbstractItemModel { void itemLayoutChanged(QWeakPointer item); public slots: + void onItemChanged(QWeakPointer item); + void setMetadata(const QModelIndex &index, const QString &metaKey, QVariant value); @@ -114,7 +116,7 @@ class Model : public QAbstractItemModel { typedef QPair, QModelIndex> PendingIndexChange; - void iterateAllChilds(QSharedPointer item, QList &pendingChanges); + void iterateAllChilds(QSharedPointer item, QList &pendingChanges); protected: QList> m_treeItems;