Skip to content

Commit 33c862c

Browse files
committed
chore: remove AppsModel::appItem(desktopId)
此接口会先调用 appItems() 获取所有应用的列表,然后再遍历以进行查找. 一方面是效率问题(可以直接查找返回),一方面可能存在遍历时原有指针可能 失效的问题. 原本相关类提供了 itemFromDesktopId() 接口提供了完全相同的功能,故直 接移除 appItem(desktopId) 接口. Log:
1 parent ead060e commit 33c862c

2 files changed

Lines changed: 3 additions & 27 deletions

File tree

src/models/appsmodel.cpp

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ AppsModel::AppsModel(QObject *parent)
7676
if (AppMgr::instance()->isValid()) {
7777
connect(AppMgr::instance(), &AppMgr::changed, m_tmUpdateCache, qOverload<>(&QTimer::start));
7878
connect(AppMgr::instance(), &AppMgr::itemDataChanged, this, [this](const QString &id) {
79-
const auto appItem = this->appItem(id);
79+
const auto appItem = this->itemFromDesktopId(id);
8080
if (!appItem) {
8181
qWarning() << "Not existing item in AppsModel for the desktopId" << id;
8282
return;
@@ -98,27 +98,6 @@ AppsModel::AppsModel(QObject *parent)
9898
connect(m_tmUpdateCache, &QTimer::timeout, this, &AppsModel::updateModelData);
9999
}
100100

101-
QList<AppItem *> AppsModel::appItems() const
102-
{
103-
QList<AppItem *> items;
104-
for (int i = 0; i < rowCount(); i++) {
105-
if (auto appItem = dynamic_cast<AppItem*>(item(i))) {
106-
items.append(appItem);
107-
}
108-
}
109-
110-
return items;
111-
}
112-
113-
AppItem *AppsModel::appItem(const QString &desktopId) const
114-
{
115-
const auto items = appItems();
116-
auto iter = std::find_if(items.begin(), items.end(), [desktopId](AppItem *item) {
117-
return item->freedesktopId() == desktopId;
118-
});
119-
return iter != items.end() ? *iter : nullptr;
120-
}
121-
122101
void AppsModel::appendRows(const QList<AppItem *> items)
123102
{
124103
// TODO: preformance improvement?
@@ -127,7 +106,7 @@ void AppsModel::appendRows(const QList<AppItem *> items)
127106
}
128107
}
129108

130-
AppItem *AppsModel::itemFromDesktopId(const QString freedesktopId)
109+
AppItem *AppsModel::itemFromDesktopId(const QString freedesktopId) const
131110
{
132111
QModelIndexList indexes = match(index(0, 0, QModelIndex()),
133112
AppItem::DesktopIdRole, freedesktopId, 1, Qt::MatchExactly);

src/models/appsmodel.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,12 @@ class AppsModel : public QStandardItemModel
4646

4747
void appendRows(const QList<AppItem *> items);
4848

49-
AppItem * itemFromDesktopId(const QString freedesktopId);
49+
AppItem * itemFromDesktopId(const QString freedesktopId) const;
5050
[[nodiscard("might need to free them")]] const QList<AppItem *> addItems(const QList<AppItem *> &items);
5151
[[nodiscard("might need to free them")]] const QList<AppItem *> updateItems(const QList<AppItem *> &items);
5252

5353
// QAbstractItemModel interface
5454
QVariant data(const QModelIndex &index, int role) const override;
55-
56-
AppItem *appItem(const QString &desktopId) const;
5755
private slots:
5856
void updateModelData();
5957

@@ -62,7 +60,6 @@ private slots:
6260

6361
QList<AppItem *> allAppInfosShouldBeShown() const;
6462
void cleanUpInvalidApps(const QList<AppItem *> knownExistedApps);
65-
QList<AppItem *> appItems() const;
6663

6764
Dtk::Core::DConfig * m_dconfig;
6865
QStringList m_excludedAppIdList;

0 commit comments

Comments
 (0)