-
Notifications
You must be signed in to change notification settings - Fork 55
fix: many issues with RoleGroupModel #1196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
修正 RoleGroupModel 的若干问题,包括 hasChildren() 不正确,无法被 QTreeView支持,QAbstractItemModelTester运行失败等. Log:
Reviewer's GuideThis PR refactors RoleGroupModel to correct grouping logic, tighten index/data safety checks, enhance data presentation for group nodes, and implement hasChildren, accompanied by an expanded test suite verifying model behavior under various scenarios. Sequence diagram for data retrieval in RoleGroupModel::data()sequenceDiagram
participant View
participant RoleGroupModel
participant SourceModel
View->>RoleGroupModel: data(index, role)
alt index is invalid
RoleGroupModel-->>View: return QVariant()
else index is group node (internalId == -1)
RoleGroupModel->>SourceModel: index(list->first(), 0).data(role)
alt role == Qt::DisplayRole
RoleGroupModel-->>View: return group label with count
else
RoleGroupModel-->>View: return first item's data
end
else index is child node
RoleGroupModel->>SourceModel: index(list->value(index.row()), 0).data(role)
RoleGroupModel-->>View: return child data
end
Sequence diagram for hasChildren() logic in RoleGroupModelsequenceDiagram
participant View
participant RoleGroupModel
View->>RoleGroupModel: hasChildren(parent)
alt no sourceModel
RoleGroupModel-->>View: false
else parent is invalid (root)
RoleGroupModel-->>View: m_rowMap.size() > 0
else parent is group node (internalId == -1)
RoleGroupModel-->>View: list && list->size() > 0
else parent is child node
RoleGroupModel-->>View: false
end
Class diagram for updated RoleGroupModelclassDiagram
class QAbstractProxyModel
class RoleGroupModel {
+int rowCount(const QModelIndex &parent) const
+int columnCount(const QModelIndex &parent) const
+QVariant data(const QModelIndex &index, int role) const
+bool hasChildren(const QModelIndex &parent) const
+bool hasIndex(int row, int column, const QModelIndex &parent) const
+QModelIndex index(int row, int column, const QModelIndex &parent) const
+QModelIndex parent(const QModelIndex &child) const
+QModelIndex mapToSource(const QModelIndex &proxyIndex) const
+QModelIndex mapFromSource(const QModelIndex &sourceIndex) const
+void setSourceModel(QAbstractItemModel *model)
+QHash<int, QByteArray> roleNames() const
+void rebuildTreeSource()
-QList<QList<int>*> m_rowMap
-QHash<QString, QList<int>*> m_map
-int m_roleForDeduplication
}
QAbstractProxyModel <|-- RoleGroupModel
Class diagram for QModelIndex usage in RoleGroupModelclassDiagram
class QModelIndex {
+int row() const
+int column() const
+bool isValid() const
+QVariant data(int role) const
+QModelIndex parent() const
+quintptr internalId() const
}
class RoleGroupModel
RoleGroupModel --> QModelIndex : uses
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @BLumia - I've reviewed your changes - here's some feedback:
- You should add handlers for rowsRemoved and modelReset on the source model so that m_map/m_rowMap stay in sync when items are deleted or the model is reset.
- Allocating QList* by hand risks memory leaks and complicates ownership; consider using value containers (e.g. QList<QList> or QVector<QVector>) instead of raw pointers.
- Relying on internalId() == -1 to distinguish group vs child nodes is not obvious—introduce helper methods or an explicit flag to clarify index/parent logic and reduce duplication.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- You should add handlers for rowsRemoved and modelReset on the source model so that m_map/m_rowMap stay in sync when items are deleted or the model is reset.
- Allocating QList<int>* by hand risks memory leaks and complicates ownership; consider using value containers (e.g. QList<QList<int>> or QVector<QVector<int>>) instead of raw pointers.
- Relying on internalId() == -1 to distinguish group vs child nodes is not obvious—introduce helper methods or an explicit flag to clarify index/parent logic and reduce duplication.
## Individual Comments
### Comment 1
<location> `tests/panels/dock/taskmanager/rolegroupmodeltests.cpp:211` </location>
<code_context>
+// 测试 hasChildren 方法的正确性
</code_context>
<issue_to_address>
Comprehensive hasChildren() test, but missing test for invalid parent index.
Please add a test for hasChildren() with an invalid parent index to verify correct handling of out-of-range or dangling indices.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| RoleGroupModel groupModel(&model, role); | ||
|
|
||
| // 初始状态:没有数据,根节点不应该有子节点 | ||
| EXPECT_FALSE(groupModel.hasChildren()); | ||
| EXPECT_FALSE(groupModel.hasChildren(QModelIndex())); | ||
|
|
||
| // 添加第一个项目 | ||
| QStandardItem *item1 = new QStandardItem; | ||
| item1->setData(QString("firefox.desktop"), role); | ||
| item1->setData(QString("Firefox - 页面1"), Qt::DisplayRole); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Comprehensive hasChildren() test, but missing test for invalid parent index.
Please add a test for hasChildren() with an invalid parent index to verify correct handling of out-of-range or dangling indices.
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: BLumia, tsic404 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
修正 RoleGroupModel 的若干问题,包括 hasChildren() 不正确,无法被QTreeView支持,QAbstractItemModelTester运行失败等.
Summary by Sourcery
Fix numerous issues in RoleGroupModel by adding boundary checks, proper hasChildren support, robust index/data/mapping logic, and enhance group display formatting, ensuring compatibility with QTreeView and QAbstractItemModelTester.
Bug Fixes:
Enhancements:
Tests: