Changeset View
Changeset View
Standalone View
Standalone View
core/MenuModel.cpp
Show All 16 Lines | |||||
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 | 25 | #include <QDebug> | |||
ngraham: Remove | |||||
26 | #include "MenuItem.h" | 26 | #include "MenuItem.h" | ||
27 | 27 | | |||
28 | const int MenuModel::UserFilterRole = 0x015D1AE6; | | |||
29 | const int MenuModel::UserSortRole = 0x03A8CC00; | | |||
30 | 28 | | |||
31 | class MenuModel::Private { | 29 | class MenuModel::Private { | ||
32 | public: | 30 | public: | ||
33 | Private() {} | 31 | Private() {} | ||
34 | 32 | | |||
35 | MenuItem *rootItem = nullptr; | 33 | MenuItem *rootItem = nullptr; | ||
36 | QList<MenuItem*> exceptions; | 34 | QList<MenuItem*> exceptions; | ||
37 | }; | 35 | }; | ||
38 | 36 | | |||
39 | MenuModel::MenuModel( MenuItem * menuRoot, QObject *parent ) | 37 | MenuModel::MenuModel( MenuItem * menuRoot, QObject *parent ) | ||
40 | : QAbstractItemModel( parent ) | 38 | : QAbstractItemModel( parent ) | ||
41 | , d( new Private() ) | 39 | , d( new Private() ) | ||
42 | { | 40 | { | ||
43 | d->rootItem = menuRoot; | 41 | d->rootItem = menuRoot; | ||
44 | } | 42 | } | ||
45 | 43 | | |||
46 | MenuModel::~MenuModel() | 44 | MenuModel::~MenuModel() | ||
47 | { | 45 | { | ||
48 | d->exceptions.clear(); | 46 | d->exceptions.clear(); | ||
49 | delete d; | 47 | delete d; | ||
50 | } | 48 | } | ||
51 | 49 | | |||
50 | 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… | |||||
51 | { | ||||
52 | QHash<int, QByteArray> names = QAbstractItemModel::roleNames(); | ||||
53 | names[DepthRole] = "DepthRole"; | ||||
54 | names[IsCategoryRole] = "IsCategoryRole"; | ||||
55 | return names; | ||||
56 | } | ||||
57 | | ||||
52 | int MenuModel::columnCount( const QModelIndex &parent ) const | 58 | int MenuModel::columnCount( const QModelIndex &parent ) const | ||
53 | { | 59 | { | ||
54 | Q_UNUSED( parent ); | 60 | Q_UNUSED( parent ); | ||
55 | return 1; | 61 | return 1; | ||
56 | } | 62 | } | ||
57 | 63 | | |||
58 | int MenuModel::rowCount( const QModelIndex &parent ) const | 64 | int MenuModel::rowCount( const QModelIndex &parent ) const | ||
59 | { | 65 | { | ||
Show All 26 Lines | 85 | switch ( role ) { | |||
86 | case Qt::DecorationRole: | 92 | case Qt::DecorationRole: | ||
87 | theData = QVariant( QIcon::fromTheme( mi->service()->icon() ) ); | 93 | theData = QVariant( QIcon::fromTheme( mi->service()->icon() ) ); | ||
88 | break; | 94 | break; | ||
89 | case KCategorizedSortFilterProxyModel::CategorySortRole: | 95 | case KCategorizedSortFilterProxyModel::CategorySortRole: | ||
90 | if ( mi->parent() ) { | 96 | if ( mi->parent() ) { | ||
91 | theData.setValue( QStringLiteral("%1%2").arg( QString::number(mi->parent()->weight()), 5, QLatin1Char('0') ).arg( mi->parent()->name() ) ); | 97 | theData.setValue( QStringLiteral("%1%2").arg( QString::number(mi->parent()->weight()), 5, QLatin1Char('0') ).arg( mi->parent()->name() ) ); | ||
92 | } | 98 | } | ||
93 | break; | 99 | break; | ||
94 | case KCategorizedSortFilterProxyModel::CategoryDisplayRole: | 100 | case KCategorizedSortFilterProxyModel::CategoryDisplayRole: { | ||
95 | if ( mi->parent() ) { | 101 | MenuItem *candidate = mi->parent(); | ||
96 | theData.setValue( mi->parent()->name() ); | 102 | while ( candidate && candidate->parent() && candidate->parent()->parent() ) { | ||
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… | |||||
103 | candidate = candidate->parent(); | ||||
104 | } | ||||
105 | if (candidate) { | ||||
106 | theData.setValue( candidate->name() ); | ||||
97 | } | 107 | } | ||
108 | //if (mi->parent()) theData.setValue( mi->parent()->name() ); | ||||
davidedmundson: can be deleted | |||||
98 | break; | 109 | break; | ||
99 | case Qt::UserRole: | 110 | } | ||
111 | case MenuModel::MenuItemRole: | ||||
100 | theData.setValue( mi ); | 112 | theData.setValue( mi ); | ||
101 | break; | 113 | break; | ||
102 | case MenuModel::UserFilterRole: | 114 | case MenuModel::UserFilterRole: | ||
103 | theData.setValue( mi->keywords().join( QString() ) ); | 115 | theData.setValue( mi->keywords().join( QString() ) ); | ||
104 | break; | 116 | break; | ||
105 | case MenuModel::UserSortRole: | 117 | case MenuModel::UserSortRole: | ||
106 | theData.setValue( QStringLiteral("%1").arg( QString::number(mi->weight()), 5, QLatin1Char('0') ) ); | 118 | theData.setValue( QStringLiteral("%1").arg( QString::number(mi->weight()), 5, QLatin1Char('0') ) ); | ||
107 | break; | 119 | break; | ||
120 | case MenuModel::DepthRole: { | ||||
121 | MenuItem *candidate = mi; | ||||
122 | int 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… | |||||
123 | while ( candidate && candidate->parent() ) { | ||||
124 | candidate = candidate->parent(); | ||||
125 | ++depth; | ||||
126 | } | ||||
127 | theData.setValue( depth ); | ||||
128 | break; | ||||
129 | } | ||||
130 | case MenuModel::IsCategoryRole: | ||||
131 | theData.setValue( mi->menu() ); | ||||
132 | break; | ||||
108 | default: | 133 | default: | ||
109 | break; | 134 | break; | ||
110 | } | 135 | } | ||
111 | return theData; | 136 | return theData; | ||
112 | } | 137 | } | ||
113 | 138 | | |||
114 | Qt::ItemFlags MenuModel::flags( const QModelIndex &index ) const | 139 | Qt::ItemFlags MenuModel::flags( const QModelIndex &index ) const | ||
115 | { | 140 | { | ||
▲ Show 20 Lines • Show All 89 Lines • Show Last 20 Lines |
Remove