Merge lp:~uriboni/unity-2d/support-removeRows-in-models into lp:unity-2d/3.0

Proposed by Ugo Riboni
Status: Merged
Approved by: Olivier Tilloy
Approved revision: 591
Merged at revision: 590
Proposed branch: lp:~uriboni/unity-2d/support-removeRows-in-models
Merge into: lp:unity-2d/3.0
Diff against target: 115 lines (+64/-1)
4 files modified
libunity-2d-private/Unity2d/listaggregatormodel.cpp (+41/-0)
libunity-2d-private/Unity2d/listaggregatormodel.h (+2/-0)
libunity-2d-private/Unity2d/windowslist.cpp (+18/-0)
libunity-2d-private/Unity2d/windowslist.h (+3/-1)
To merge this branch: bzr merge lp:~uriboni/unity-2d/support-removeRows-in-models
Reviewer Review Type Date Requested Status
Olivier Tilloy (community) Approve
Florian Boucault (community) Needs Fixing
Review via email: mp+63472@code.launchpad.net

Commit message

Add support for removing rows by implementing the virtual method QAbstractItemModel::removeRows in WindowsList and ListAggregatorModel.
Make the internal list in WindowsList protected instead of private.

Description of the change

Add support for removing rows by implementing the virtual method QAbstractItemModel::removeRows in WindowsList and ListAggregatorModel.

Make the internal list in WindowsList protected instead of private so that it is easier for other classes to inherit from this model to do useful things.

This change is not currently needed by Unity-2d, but I do need it for another project.
It can be however pretty useful for Unity-2d too in the future as it's just standard functionality on the models that wasn't simply implemented before.

To post a comment you must log in.
Revision history for this message
Florian Boucault (fboucault) wrote :

In WindowsList::removeRows:
- a debug statement was leftover
- using removeAt would probably simplify the code quite a lot

review: Needs Fixing
584. By Ugo Riboni

Remove debug statement

585. By Ugo Riboni

Merge changes from trunk

586. By Ugo Riboni

Fix a bug in the ListAggregatorModel removal code. Simplify the WindowsList removal code.

587. By Ugo Riboni

Fix missing brace

Revision history for this message
Olivier Tilloy (osomon) wrote :

Some style nitpicking to get started with.

+bool ListAggregatorModel::removeRows(int row, int count,
+ const QModelIndex& parent)

I know this isn’t in the style guidelines, but all the other methods in this file have their return type declared on a separate line, please keep it consistent. If we want to change this at some point, we’ll batch it, but it’s important to keep the whole file consistent for improved readability.
Also, the list of parameters doesn’t look that long to me that it should be cut into two lines.

-
+#include <QList>

Please add back the blank line that separated those groups of #includes.

review: Needs Fixing (code)
Revision history for this message
Olivier Tilloy (osomon) wrote :

In ListAggregatorModel::removeRows, I think the following line is wrong:

    int removeCount = qMin(count, model->rowCount() - removeAt);

It should probably be:

    int removeCount = qMin(count - removed, model->rowCount() - removeAt);

review: Needs Fixing
Revision history for this message
Olivier Tilloy (osomon) wrote :

In ListAggregatorModel::removeRows, the first test (if (row < 0)) is incomplete, it should also test whether row >= rowCount() or count <= 0, just like in the implementation of WindowsList::removeRows(…).

review: Needs Fixing
588. By Ugo Riboni

Add more proper boundary checks

589. By Ugo Riboni

Fix cosmetic code issues

590. By Ugo Riboni

Fix an error in the calculation of where to start removing items

Revision history for this message
Ugo Riboni (uriboni) wrote :

I fixed all your remarks.
While testing some more I also uncovered a bug in the way the position of the next item to be removed was calculated in cases where the removal spans more than one model.

Revision history for this message
Olivier Tilloy (osomon) wrote :

You didn’t take into account my first style nitpick:

« I know this isn’t in the style guidelines, but all the other methods in this file have their return type declared on a separate line, please keep it consistent. If we want to change this at some point, we’ll batch it, but it’s important to keep the whole file consistent for improved readability. »

Other typo remarks:

"By taking that into accountcalculation" : missing whitespace

s/semplified/simplified/

Revision history for this message
Olivier Tilloy (osomon) wrote :

In libunity-2d-private/Unity2d/windowslist.h, on the blank line before the 'protected' keyword, you added 4 extra whitespaces, please remove them.

review: Needs Fixing
Revision history for this message
Olivier Tilloy (osomon) wrote :

+ if (row < 0 || row >= rowCount(parent) || count <= 0) {

In rowCount(…), the 'parent' argument is unused, and it defaults to QModelIndex(), so you can safely call rowCount() without any argument.

Revision history for this message
Olivier Tilloy (osomon) wrote :

+ /* Please note that the offset of the current model is computed
+ by iterating over all previous models and making a sum of their
+ row count.
+ By taking that into accountcalculation for removeAt would be:
+ (row + removed) - (offset + removed)
+ This can be semplified to just row - offset as you see below */
+ int offset = computeOffset(model);
+ int removeAt = row - offset;

This is correct, but I don’t think it’s necessary to be that verbose. I don’t think this comment is needed at all (feel free to keep it if you disagree). What we really need is unit tests…

591. By Ugo Riboni

Fix more code style remarks

Revision history for this message
Ugo Riboni (uriboni) wrote :

Fixed the rest of the problems hopefully. I left the verbose comment. It will help me when i look at that code one month down the road.

Revision history for this message
Olivier Tilloy (osomon) wrote :

Good to go now!
Did I mention that we need unit tests? ;)

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'libunity-2d-private/Unity2d/listaggregatormodel.cpp'
2--- libunity-2d-private/Unity2d/listaggregatormodel.cpp 2011-06-07 16:34:46 +0000
3+++ libunity-2d-private/Unity2d/listaggregatormodel.cpp 2011-06-09 15:57:26 +0000
4@@ -218,3 +218,44 @@
5 {
6 return data(QAbstractListModel::index(row), 0);
7 }
8+
9+bool
10+ListAggregatorModel::removeRows(int row, int count, const QModelIndex& parent)
11+{
12+ if (row < 0 || row >= rowCount() || count <= 0) {
13+ return false;
14+ }
15+
16+ /* Note that since this is an aggregator, the underlying models will
17+ take care of signaling the addition and removal of rows and this
18+ model will update accordingly.
19+ Therefore we don't need to call beginRemoveRows */
20+
21+ int removed = 0;
22+ Q_FOREACH (QAbstractItemModel* model, m_models) {
23+ /* Please note that the offset of the current model is computed
24+ by iterating over all previous models and making a sum of their
25+ row count.
26+ By taking that into account the calculation for removeAt is:
27+ (row + removed) - (offset + removed)
28+ This can be simplified to just row - offset as you see below */
29+ int offset = computeOffset(model);
30+ int removeAt = row - offset;
31+
32+ if (removeAt < 0 || removeAt >= model->rowCount()) {
33+ // The item(s) that we need to remove are outside of this model
34+ // move on to the next one.
35+ continue;
36+ }
37+
38+ int removeCount = qMin(count - removed, model->rowCount() - removeAt);
39+ model->removeRows(removeAt, removeCount);
40+ removed += removeCount;
41+
42+ if (removed >= count) {
43+ break;
44+ }
45+ }
46+
47+ return true;
48+}
49
50=== modified file 'libunity-2d-private/Unity2d/listaggregatormodel.h'
51--- libunity-2d-private/Unity2d/listaggregatormodel.h 2011-06-07 16:34:46 +0000
52+++ libunity-2d-private/Unity2d/listaggregatormodel.h 2011-06-09 15:57:26 +0000
53@@ -44,6 +44,8 @@
54 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
55 Q_INVOKABLE int rowCount(const QModelIndex& parent = QModelIndex()) const;
56 Q_INVOKABLE QVariant get(int row) const;
57+ Q_INVOKABLE virtual bool removeRows(int row, int count,
58+ const QModelIndex& parent = QModelIndex());
59
60 /* This method is the QML equivalent of aggregateListModel.
61 The reason why aggregateListModel wasn't directly exposed to QML is that
62
63=== modified file 'libunity-2d-private/Unity2d/windowslist.cpp'
64--- libunity-2d-private/Unity2d/windowslist.cpp 2011-04-28 13:09:25 +0000
65+++ libunity-2d-private/Unity2d/windowslist.cpp 2011-06-09 15:57:26 +0000
66@@ -17,6 +17,7 @@
67 #include <QRegExp>
68 #include <QApplication>
69 #include <QWidget>
70+#include <QList>
71
72 #include <debug_p.h>
73 #include "windowslist.h"
74@@ -210,3 +211,20 @@
75 }
76 }
77 }
78+
79+bool WindowsList::removeRows(int row, int count, const QModelIndex& parent)
80+{
81+ if (row < 0 || row >= m_windows.count() || count <= 0) {
82+ return false;
83+ }
84+ count = qMin(count, m_windows.count() - row);
85+
86+ beginRemoveRows(parent, row, row + count - 1);
87+
88+ for (int i = 0; i < count; i++) {
89+ m_windows.removeAt(row);
90+ }
91+
92+ endRemoveRows();
93+ return true;
94+}
95
96=== modified file 'libunity-2d-private/Unity2d/windowslist.h'
97--- libunity-2d-private/Unity2d/windowslist.h 2011-02-08 11:48:16 +0000
98+++ libunity-2d-private/Unity2d/windowslist.h 2011-06-09 15:57:26 +0000
99@@ -37,6 +37,8 @@
100
101 QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
102 int rowCount(const QModelIndex & parent = QModelIndex()) const;
103+ Q_INVOKABLE virtual bool removeRows(int row, int count,
104+ const QModelIndex& parent = QModelIndex());
105
106 Q_INVOKABLE void load();
107 Q_INVOKABLE void unload();
108@@ -46,7 +48,7 @@
109 void removeWindow(BamfView *view);
110 void updateWorkspaceRole(int workspace);
111
112-private:
113+protected:
114 QList<WindowInfo*> m_windows;
115 };
116

Subscribers

People subscribed via source and target branches

to all changes: