~blue-shell/blue-shell/kde-workspace-systemsettings-qml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**************************************************************************
 * Copyright (C) 2009 Ben Cooksley <bcooksley@kde.org>                    *
 * Copyright (C) 2007 Will Stephenson <wstephenson@kde.org>               *
 *                                                                        *
 * This program is free software; you can redistribute it and/or          *
 * modify it under the terms of the GNU General Public License            *
 * as published by the Free Software Foundation; either version 2         *
 * of the License, or (at your option) any later version.                 *
 *                                                                        *
 * This program is distributed in the hope that it will be useful,        *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 * GNU General Public License for more details.                           *
 *                                                                        *
 * You should have received a copy of the GNU General Public License      *
 * along with this program; if not, write to the Free Software            *
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA          *
 * 02110-1301, USA.                                                       *
***************************************************************************/

#include "MenuModel.h"

#include <KIcon>
#include <KCategorizedSortFilterProxyModel>

#include "MenuItem.h"

const int MenuModel::UserFilterRole = 0x015D1AE6;
const int MenuModel::UserSortRole = 0x03A8CC00;

class MenuModel::Private {
public:
    Private() {}

    MenuItem *rootItem;
    QList<MenuItem*> exceptions;
};

MenuModel::MenuModel( MenuItem * menuRoot, QObject *parent )
    : QAbstractItemModel( parent )
    , d( new Private() )
{
    d->rootItem = menuRoot;

    QHash<int, QByteArray> roles;
    roles.insert(Qt::DisplayRole, "DisplayRole");
    roles.insert(Qt::DecorationRole, "DecorationRole");
    setRoleNames(roles);
}

MenuModel::~MenuModel()
{
    d->exceptions.clear();
    delete d;
}

int MenuModel::columnCount( const QModelIndex &parent ) const
{
    Q_UNUSED( parent );
    return 1;
}

int MenuModel::rowCount( const QModelIndex &parent ) const
{
    MenuItem * mi;
    if ( parent.isValid() ) {
        mi = static_cast<MenuItem*>( parent.internalPointer() );
    } else {
        mi = d->rootItem;
    }

    return childrenList(mi).count();
}

QVariant MenuModel::data( const QModelIndex &index, int role ) const
{
    MenuItem * mi = 0;
    QVariant theData;
    if ( !index.isValid() ) {
        return QVariant();
    }

    mi = static_cast<MenuItem*>( index.internalPointer() );
    switch ( role ) {
        case Qt::DisplayRole:
            theData.setValue( mi->name() );
            break;
        case Qt::ToolTipRole:
            theData.setValue( mi->service()->comment() );
            break;
        case Qt::DecorationRole:
            theData = QVariant( KIcon( mi->service()->icon() ) );
            break;
        case KCategorizedSortFilterProxyModel::CategorySortRole:
            if ( mi->parent() ) {
                theData.setValue( QString("%1%2").arg( QString::number(mi->parent()->weight()), 5, '0' ).arg( mi->parent()->name() ) );
            }
            break;
        case KCategorizedSortFilterProxyModel::CategoryDisplayRole:
            if ( mi->parent() ) {
                theData.setValue( mi->parent()->name() );
            }
            break;
        case Qt::UserRole:
            theData.setValue( mi );
            break;
        case MenuModel::UserFilterRole:
            theData.setValue( mi->keywords().join( QString() ) );
            break;
        case MenuModel::UserSortRole:
            theData.setValue( QString("%1").arg( QString::number(mi->weight()), 5, '0' ) );
            break;
        default:
            break;
    }
    return theData;
}

Qt::ItemFlags MenuModel::flags( const QModelIndex &index ) const
{
    if ( !index.isValid() ) {
        return 0;
    }

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QModelIndex MenuModel::index( int row, int column, const QModelIndex &parent ) const
{
    if ( !hasIndex(row, column, parent) ) {
        return QModelIndex();
    }

    MenuItem *parentItem;
    if ( !parent.isValid() ) {
        parentItem = d->rootItem;
    } else {
        parentItem = static_cast<MenuItem*>( parent.internalPointer() );
    }

    MenuItem *childItem = childrenList(parentItem).value(row);
    if ( childItem ) {
        return createIndex( row, column, childItem );
    } else {
        return QModelIndex();
    }
}

QModelIndex MenuModel::parent( const QModelIndex &index ) const
{
    MenuItem *childItem = static_cast<MenuItem*>( index.internalPointer() );
    if( !childItem ) {
        return QModelIndex();
    }

    MenuItem * parent = parentItem(childItem);
    MenuItem * grandParent = parentItem(parent);

    int childRow = 0;
    if( grandParent ) {
        childRow = childrenList( grandParent ).indexOf( parent );
    }

    if ( parent == d->rootItem ) {
        return QModelIndex();
    }
    return createIndex( childRow, 0, parent );
}

QList<MenuItem*> MenuModel::childrenList( MenuItem * parent ) const
{
    QList<MenuItem*> children = parent->children();
    foreach( MenuItem * child, children ) {
        if( d->exceptions.contains( child ) ) {
            children.removeOne(child);
            children.append(child->children());
        }
    }
    return children;
}

MenuItem * MenuModel::parentItem( MenuItem * child ) const
{
    MenuItem * parent = child->parent();
    if( d->exceptions.contains(parent) ) {
        parent = parentItem(parent);
    }
    return parent;
}

MenuItem * MenuModel::rootItem() const
{
    return d->rootItem;
}

void MenuModel::addException( MenuItem * exception )
{
    if( exception == d->rootItem ) {
        return;
    }
    d->exceptions.append(exception);
}

void MenuModel::removeException( MenuItem * exception )
{
    d->exceptions.removeAll(exception);
}

#include "MenuModel.moc"