From 5f75a6d7d8c5526378ce612c30551db4eb26acee Mon Sep 17 00:00:00 2001 From: Jeremy HU Date: Thu, 6 Oct 2022 21:16:21 +1100 Subject: [PATCH] Fix component unselectable issue If the initial Qt::DecorationRole give an empty QPixmap, the item will be unselectable sometimes. So if nothing to show, should return an transparent image with the same size. --- application/sources/component_list_model.cc | 22 +++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/application/sources/component_list_model.cc b/application/sources/component_list_model.cc index 860e97ad..3fa121d5 100644 --- a/application/sources/component_list_model.cc +++ b/application/sources/component_list_model.cc @@ -27,14 +27,23 @@ ComponentListModel::ComponentListModel(const Document *document, QObject *parent void ComponentListModel::reload() { + std::pair beginEnd; + beginResetModel(); m_componentIdToIndexMap.clear(); const SkeletonComponent *listingComponent = m_document->findComponent(m_listingComponentId); if (nullptr != listingComponent) { for (int i = 0; i < (int)listingComponent->childrenIds.size(); ++i) { - m_componentIdToIndexMap[listingComponent->childrenIds[i]] = index(i); + m_componentIdToIndexMap[listingComponent->childrenIds[i]] = createIndex(i, 0); + } + if (!listingComponent->childrenIds.empty()) { + beginEnd.first = m_componentIdToIndexMap[listingComponent->childrenIds.front()]; + beginEnd.second = m_componentIdToIndexMap[listingComponent->childrenIds.back()]; } } - emit this->layoutChanged(); + endResetModel(); + if (!m_componentIdToIndexMap.empty()) + emit dataChanged(beginEnd.first, beginEnd.second); + emit layoutChanged(); } int ComponentListModel::rowCount(const QModelIndex &parent) const @@ -98,6 +107,15 @@ QVariant ComponentListModel::data(const QModelIndex &index, int role) const case Qt::DecorationRole: { const SkeletonComponent *component = modelIndexToComponent(index); if (nullptr != component) { + if (0 == component->previewPixmap.width()) { + static QPixmap s_emptyPixmap; + if (0 == s_emptyPixmap.width()) { + QImage image((int)Theme::partPreviewImageSize, (int)Theme::partPreviewImageSize, QImage::Format_ARGB32); + image.fill(Qt::transparent); + s_emptyPixmap = QPixmap::fromImage(image); + } + return s_emptyPixmap; + } return component->previewPixmap; } }