Changeset View
Changeset View
Standalone View
Standalone View
core/MenuModel.cpp
Show All 15 Lines | |||||
16 | * along with this program; if not, write to the Free Software * | 16 | * along with this program; if not, write to the Free Software * | ||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * | ||
18 | * 02110-1301, USA. * | 18 | * 02110-1301, USA. * | ||
19 | ***************************************************************************/ | 19 | ***************************************************************************/ | ||
20 | 20 | | |||
21 | #include "MenuModel.h" | 21 | #include "MenuModel.h" | ||
22 | 22 | | |||
23 | #include <KCategorizedSortFilterProxyModel> | 23 | #include <KCategorizedSortFilterProxyModel> | ||
24 | #include <QIcon> | 24 | #include <QIcon> | ||
25 | | ||||
26 | #include "MenuItem.h" | 25 | #include "MenuItem.h" | ||
ngraham: Remove | |||||
27 | 26 | | |||
28 | const int MenuModel::UserFilterRole = 0x015D1AE6; | | |||
29 | const int MenuModel::UserSortRole = 0x03A8CC00; | | |||
30 | 27 | | |||
31 | class MenuModel::Private { | 28 | class MenuModel::Private { | ||
32 | public: | 29 | public: | ||
33 | Private() {} | 30 | Private() {} | ||
34 | 31 | | |||
35 | MenuItem *rootItem = nullptr; | 32 | MenuItem *rootItem = nullptr; | ||
36 | QList<MenuItem*> exceptions; | 33 | QList<MenuItem*> exceptions; | ||
37 | }; | 34 | }; | ||
38 | 35 | | |||
39 | MenuModel::MenuModel( MenuItem * menuRoot, QObject *parent ) | 36 | MenuModel::MenuModel( MenuItem * menuRoot, QObject *parent ) | ||
40 | : QAbstractItemModel( parent ) | 37 | : QAbstractItemModel( parent ) | ||
41 | , d( new Private() ) | 38 | , d( new Private() ) | ||
42 | { | 39 | { | ||
43 | d->rootItem = menuRoot; | 40 | d->rootItem = menuRoot; | ||
44 | } | 41 | } | ||
45 | 42 | | |||
46 | MenuModel::~MenuModel() | 43 | MenuModel::~MenuModel() | ||
47 | { | 44 | { | ||
48 | d->exceptions.clear(); | 45 | d->exceptions.clear(); | ||
49 | delete d; | 46 | delete d; | ||
50 | } | 47 | } | ||
51 | 48 | | |||
49 | QHash<int, QByteArray> MenuModel::roleNames() const | ||||
In other models we tend to drop the suffix "Role" from the rolename i.e Qt::DisplayRole -> "display" but it's not super important. davidedmundson: In other models we tend to drop the suffix "Role" from the rolename
i.e Qt::DisplayRole ->… | |||||
I followed what it was doing previously, MenuItemRols->MenuItem would clash with the class MenuItem mart: I followed what it was doing previously, MenuItemRols->MenuItem would clash with the class… | |||||
50 | { | ||||
51 | QHash<int, QByteArray> names = QAbstractItemModel::roleNames(); | ||||
52 | names[DepthRole] = "DepthRole"; | ||||
53 | names[IsCategoryRole] = "IsCategoryRole"; | ||||
54 | return names; | ||||
55 | } | ||||
56 | | ||||
52 | int MenuModel::columnCount( const QModelIndex &parent ) const | 57 | int MenuModel::columnCount( const QModelIndex &parent ) const | ||
53 | { | 58 | { | ||
54 | Q_UNUSED( parent ); | 59 | Q_UNUSED( parent ); | ||
55 | return 1; | 60 | return 1; | ||
56 | } | 61 | } | ||
57 | 62 | | |||
58 | int MenuModel::rowCount( const QModelIndex &parent ) const | 63 | int MenuModel::rowCount( const QModelIndex &parent ) const | ||
59 | { | 64 | { | ||
Show All 26 Lines | 84 | switch ( role ) { | |||
86 | case Qt::DecorationRole: | 91 | case Qt::DecorationRole: | ||
87 | theData = QVariant( QIcon::fromTheme( mi->service()->icon() ) ); | 92 | theData = QVariant( QIcon::fromTheme( mi->service()->icon() ) ); | ||
88 | break; | 93 | break; | ||
89 | case KCategorizedSortFilterProxyModel::CategorySortRole: | 94 | case KCategorizedSortFilterProxyModel::CategorySortRole: | ||
90 | if ( mi->parent() ) { | 95 | if ( mi->parent() ) { | ||
91 | theData.setValue( QStringLiteral("%1%2").arg( QString::number(mi->parent()->weight()), 5, QLatin1Char('0') ).arg( mi->parent()->name() ) ); | 96 | theData.setValue( QStringLiteral("%1%2").arg( QString::number(mi->parent()->weight()), 5, QLatin1Char('0') ).arg( mi->parent()->name() ) ); | ||
92 | } | 97 | } | ||
93 | break; | 98 | break; | ||
94 | case KCategorizedSortFilterProxyModel::CategoryDisplayRole: | 99 | case KCategorizedSortFilterProxyModel::CategoryDisplayRole: { | ||
95 | if ( mi->parent() ) { | 100 | MenuItem *candidate = mi->parent(); | ||
96 | theData.setValue( mi->parent()->name() ); | 101 | // The model has an invisible single root item. | ||
More comments are needed throughout. You forgot why we went to the second level of heirachy only 10 minutes after coding it when I asked :P davidedmundson: More comments are needed throughout.
You forgot why we went to the second level of heirachy… | |||||
102 | // So to get the "root category" we don't go up all the way | ||||
103 | // To the actual root, but to the list of the first childs. | ||||
104 | // That's why we check for candidate->parent()->parent() | ||||
105 | while ( candidate && candidate->parent() && candidate->parent()->parent() ) { | ||||
106 | candidate = candidate->parent(); | ||||
107 | } | ||||
108 | if (candidate) { | ||||
109 | theData.setValue( candidate->name() ); | ||||
97 | } | 110 | } | ||
98 | break; | 111 | break; | ||
davidedmundson: can be deleted | |||||
99 | case Qt::UserRole: | 112 | } | ||
113 | case MenuModel::MenuItemRole: | ||||
100 | theData.setValue( mi ); | 114 | theData.setValue( mi ); | ||
101 | break; | 115 | break; | ||
102 | case MenuModel::UserFilterRole: | 116 | case MenuModel::UserFilterRole: | ||
103 | theData.setValue( mi->keywords().join( QString() ) ); | 117 | theData.setValue( mi->keywords().join( QString() ) ); | ||
104 | break; | 118 | break; | ||
105 | case MenuModel::UserSortRole: | 119 | case MenuModel::UserSortRole: | ||
106 | theData.setValue( QStringLiteral("%1").arg( QString::number(mi->weight()), 5, QLatin1Char('0') ) ); | 120 | theData.setValue( QStringLiteral("%1").arg( QString::number(mi->weight()), 5, QLatin1Char('0') ) ); | ||
107 | break; | 121 | break; | ||
122 | case MenuModel::DepthRole: { | ||||
123 | MenuItem *candidate = mi; | ||||
124 | // -1 excludes the invisible root, having main categories at depth 0 | ||||
I find it un-intuitive that they effectively start at 1, not 0. The hidden root object is 0, but that can't be accessed via an index. It took me a while to understand why the delegate spacing logic had depth-2 davidedmundson: I find it un-intuitive that they effectively start at 1, not 0.
The hidden root object is 0… | |||||
125 | int depth = -1; | ||||
126 | while ( candidate && candidate->parent() ) { | ||||
127 | candidate = candidate->parent(); | ||||
128 | ++depth; | ||||
129 | } | ||||
130 | theData.setValue( depth ); | ||||
131 | break; | ||||
132 | } | ||||
133 | case MenuModel::IsCategoryRole: | ||||
134 | theData.setValue( mi->menu() ); | ||||
135 | break; | ||||
108 | default: | 136 | default: | ||
109 | break; | 137 | break; | ||
110 | } | 138 | } | ||
111 | return theData; | 139 | return theData; | ||
112 | } | 140 | } | ||
113 | 141 | | |||
114 | Qt::ItemFlags MenuModel::flags( const QModelIndex &index ) const | 142 | Qt::ItemFlags MenuModel::flags( const QModelIndex &index ) const | ||
115 | { | 143 | { | ||
▲ Show 20 Lines • Show All 89 Lines • Show Last 20 Lines |
Remove