From fedb80e1cd4cc272b03723463d37f135b5a86d82 Mon Sep 17 00:00:00 2001 From: youdiansaodongxi Date: Wed, 20 Nov 2024 15:04:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(src/libappdata):=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E7=95=8C=E9=9D=A2=E5=BA=94=E7=94=A8=E5=8F=B3?= =?UTF-8?q?=E9=94=AE=E6=95=B0=E6=8D=AE=E4=B8=8D=E8=83=BD=E5=8F=8A=E6=97=B6?= =?UTF-8?q?=E5=88=B7=E6=96=B0=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/libappdata/app-search-plugin.cpp | 73 ++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 15 deletions(-) diff --git a/src/libappdata/app-search-plugin.cpp b/src/libappdata/app-search-plugin.cpp index bd05991..a7ecbc7 100644 --- a/src/libappdata/app-search-plugin.cpp +++ b/src/libappdata/app-search-plugin.cpp @@ -37,7 +37,7 @@ public: explicit AppSearchPluginPrivate(QObject *parent = nullptr); Q_SIGNALS: - void searchedOne(DataEntity app); + void searchedOne(const QString &id); public Q_SLOTS: void startSearch(const QString &keyword); @@ -99,12 +99,8 @@ void AppSearchPluginPrivate::run() } else { m_timer->stop(); if (result.getSearchId() == m_searchId) { - DataEntity app; QString id = result.getValue(UkuiSearch::SearchProperty::ApplicationDesktopPath).toString(); - if (!BasicAppModel::instance()->getAppById(id, app)) { - BasicAppModel::instance()->databaseInterface()->getApp(id, app); - }; - Q_EMIT this->searchedOne(app); + Q_EMIT this->searchedOne(id); } } @@ -124,16 +120,21 @@ public: int columnCount(const QModelIndex &parent) const override; QVariant data(const QModelIndex &index, int role) const override; - void appendApp(const DataEntity &app); + void appendApp(const QString &id); void clear(); private: - QVector m_apps; + QVector m_apps; + +private Q_SLOTS: + void onAppRemoved(const QModelIndex &parent, int first, int last); + void onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles = QVector()); }; AppSearchModel::AppSearchModel(QObject *parent) : QAbstractListModel(parent) { - + connect(BasicAppModel::instance(), &BasicAppModel::rowsAboutToBeRemoved, this, &AppSearchModel::onAppRemoved); + connect(BasicAppModel::instance(), &BasicAppModel::dataChanged, this, &AppSearchModel::onDataChanged); } int AppSearchModel::rowCount(const QModelIndex &parent) const @@ -148,18 +149,29 @@ int AppSearchModel::columnCount(const QModelIndex &parent) const QVariant AppSearchModel::data(const QModelIndex &index, int role) const { - if (!checkIndex(index, CheckIndexOption::IndexIsValid)) { + if (!index.isValid() || index.row() >= m_apps.size()) { return {}; } - const DataEntity &app = m_apps[index.row()]; - return app.getValue(static_cast(role)); + if (index.row() < m_apps.count()) { + return m_apps.at(index.row()).data(role); + } + return QVariant(); } -void AppSearchModel::appendApp(const DataEntity &app) +void AppSearchModel::appendApp(const QString &id) { - beginInsertRows(QModelIndex(), m_apps.size(), m_apps.size()); - m_apps.append(app); + int index = BasicAppModel::instance()->indexOfApp(id); + if (index < 0) return; + + QPersistentModelIndex modelIndex(BasicAppModel::instance()->index(index, 0, QModelIndex())); + + if (m_apps.contains(modelIndex) || !modelIndex.isValid()) { + return; + } + + beginInsertRows(QModelIndex(), m_apps.count(), m_apps.count()); + m_apps.append(modelIndex); endInsertRows(); } @@ -170,6 +182,37 @@ void AppSearchModel::clear() endResetModel(); } +void AppSearchModel::onAppRemoved(const QModelIndex &parent, int first, int last) +{ + for(int row = first; row <= last; ++row) { + QModelIndex sourceIndex = BasicAppModel::instance()->index(row, 0, {}); + QPersistentModelIndex index(sourceIndex); + + if (!m_apps.contains(index)) { + return; + } + + beginRemoveRows(QModelIndex(), m_apps.indexOf(index), m_apps.indexOf(index)); + m_apps.removeOne(index); + endRemoveRows(); + } +} + +void AppSearchModel::onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles) +{ + QModelIndex sourceIndex; + QPersistentModelIndex persistentModelIndex; + int indexRow; + + for (int row = topLeft.row(); row <= bottomRight.row(); row ++) { + sourceIndex = BasicAppModel::instance()->index(row, 0, {}); + persistentModelIndex = QPersistentModelIndex(sourceIndex); + indexRow = m_apps.indexOf(persistentModelIndex); + if (indexRow == -1) continue; + Q_EMIT dataChanged(index(indexRow, 0, QModelIndex()), index(indexRow, 0, QModelIndex())); + } +} + // ====== AppSearchPlugin ====== // AppSearchPlugin::AppSearchPlugin(QObject *parent) : AppListPluginInterface(parent) , m_searchPluginPrivate(new AppSearchPluginPrivate(this)), m_model(new AppSearchModel(this)) -- Gitee