From 5360224d2f8be82d9c910eec962b6ca3d70bb527 Mon Sep 17 00:00:00 2001 From: zhangkun Date: Tue, 7 May 2024 11:48:28 +0800 Subject: [PATCH] fix: appItem without icon field clicking to start causing crash Show default icon when icon is empty, and add a field to distinguish between folders and appitems Issue: https://github.com/linuxdeepin/developer-center/issues/8396 --- qml/IconItemDelegate.qml | 6 +++--- qml/windowed/FreeSortListView.qml | 4 ++-- src/models/appitem.cpp | 2 +- src/models/itemarrangementproxymodel.cpp | 13 +++++++++++-- src/models/itemarrangementproxymodel.h | 9 ++++++++- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/qml/IconItemDelegate.qml b/qml/IconItemDelegate.qml index ec3cda7c..38e4a487 100644 --- a/qml/IconItemDelegate.qml +++ b/qml/IconItemDelegate.qml @@ -191,7 +191,7 @@ Control { TapHandler { acceptedButtons: Qt.LeftButton onTapped: { - if (root.icons) { + if (model.itemType === ItemArrangementProxyModel.FolderItemType) { root.folderClicked() } else { root.itemClicked() @@ -210,7 +210,7 @@ Control { background: DebugBounding { } Keys.onSpacePressed: { - if (root.icons !== undefined) { + if (model.itemType === ItemArrangementProxyModel.FolderItemType) { root.folderClicked() } else { root.itemClicked() @@ -218,7 +218,7 @@ Control { } Keys.onReturnPressed: { - if (root.icons !== undefined) { + if (model.itemType === ItemArrangementProxyModel.FolderItemType) { root.folderClicked() } else { root.itemClicked() diff --git a/qml/windowed/FreeSortListView.qml b/qml/windowed/FreeSortListView.qml index 2cc39b89..a33997f1 100644 --- a/qml/windowed/FreeSortListView.qml +++ b/qml/windowed/FreeSortListView.qml @@ -133,7 +133,7 @@ Item { id: itemDelegate text: model.display.startsWith("internal/category/") ? getCategoryName(model.display.substring(18)) : model.display checkable: false - icon.name: iconName === undefined ? "folder" : iconName + icon.name: itemType === ItemArrangementProxyModel.FolderItemType ? "folder" : iconName DciIcon.mode: DTK.NormalState anchors.fill: parent anchors.leftMargin: 10 @@ -182,7 +182,7 @@ Item { if (iconName) showContextMenu(itemDelegate, model, false, false, false) } else { - if (!iconName) { + if (itemType === ItemArrangementProxyModel.FolderItemType) { console.log("freesort view folder clicked:", desktopId); let idStr = model.desktopId let strFolderId = Number(idStr.replace("internal/folders/", "")) diff --git a/src/models/appitem.cpp b/src/models/appitem.cpp index 64d6284a..33d783de 100644 --- a/src/models/appitem.cpp +++ b/src/models/appitem.cpp @@ -47,7 +47,7 @@ const QString AppItem::iconName() const void AppItem::setIconName(const QString &iconName) { - setData(iconName, AppItem::IconNameRole); + setData(iconName.isEmpty() ? "application-x-desktop" : iconName, AppItem::IconNameRole); } const QStringList AppItem::categories() const diff --git a/src/models/itemarrangementproxymodel.cpp b/src/models/itemarrangementproxymodel.cpp index 71b7cbb2..48b8df89 100644 --- a/src/models/itemarrangementproxymodel.cpp +++ b/src/models/itemarrangementproxymodel.cpp @@ -24,7 +24,12 @@ int ItemArrangementProxyModel::pageCount(int folderId) const QString fullId("internal/folders/" + QString::number(folderId)); Q_ASSERT(m_folders.contains(fullId)); - return m_folders.value(fullId)->pageCount(); + if (auto itemPage = m_folders.value(fullId); itemPage) { + return itemPage->pageCount(); + } else { + qWarning() << "itemPage is null, return 0. fullId is" << fullId; + return 0; + } } void ItemArrangementProxyModel::updateFolderName(int folderId, const QString &name) @@ -152,6 +157,8 @@ QVariant ItemArrangementProxyModel::data(const QModelIndex &index, int role) con return folder; case IconsNameRole: return QVariant(); + case ItemTypeRole: + return AppItemType; } } else { // a folder @@ -184,9 +191,10 @@ QVariant ItemArrangementProxyModel::data(const QModelIndex &index, int role) con } return icons;//QStringList({"deepin-music"}); } + case ItemTypeRole: + return FolderItemType; } } - return QConcatenateTablesProxyModel::data(index, role); } @@ -194,6 +202,7 @@ QHash ItemArrangementProxyModel::roleNames() const { auto existingRoleNames = AppsModel::instance().roleNames(); existingRoleNames.insert(IconsNameRole, QByteArrayLiteral("folderIcons")); + existingRoleNames.insert(ItemTypeRole, QByteArrayLiteral("itemType")); return existingRoleNames; } diff --git a/src/models/itemarrangementproxymodel.h b/src/models/itemarrangementproxymodel.h index 7589e928..9e9e2026 100644 --- a/src/models/itemarrangementproxymodel.h +++ b/src/models/itemarrangementproxymodel.h @@ -17,11 +17,18 @@ class ItemArrangementProxyModel : public QConcatenateTablesProxyModel QML_NAMED_ELEMENT(ItemArrangementProxyModel) QML_SINGLETON public: + enum ItemType{ + AppItemType = 0, + FolderItemType = 1 + }; + Q_ENUM(ItemType) + enum Roles { PageRole = AppsModel::ProxyModelExtendedRole, IndexInPageRole, FolderIdNumberRole, - IconsNameRole + IconsNameRole, + ItemTypeRole }; Q_ENUM(Roles)