~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to systemsettings/core/MenuItem.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright 2007 Will Stephenson <wstephenson@kde.org>
 
3
   Copyright (C) 2009 Ben Cooksley <bcooksley@kde.org> 
 
4
 
 
5
   This program is free software; you can redistribute it and/or
 
6
   modify it under the terms of the GNU General Public License as
 
7
   published by the Free Software Foundation; either version 2 of
 
8
   the License or (at your option) version 3 or any later version
 
9
   accepted by the membership of KDE e.V. (or its successor approved
 
10
   by the membership of KDE e.V.), which shall act as a proxy
 
11
   defined in Section 14 of version 3 of the license.
 
12
 
 
13
   This program is distributed in the hope that it will be useful,
 
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
   GNU General Public License for more details.
 
17
 
 
18
   You should have received a copy of the GNU General Public License
 
19
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
*/
 
21
 
 
22
#include "MenuItem.h"
 
23
 
 
24
#include <QList>
 
25
#include <QString>
 
26
 
 
27
#include <KCModuleInfo>
 
28
 
 
29
static bool childIsLessThan( MenuItem *left, MenuItem *right )
 
30
{
 
31
    return left->weight() < right->weight();
 
32
}
 
33
 
 
34
class MenuItem::Private {
 
35
public:
 
36
    Private() {}
 
37
 
 
38
    MenuItem *parent;
 
39
    QList<MenuItem*> children;
 
40
    bool menu;
 
41
    QString name;
 
42
    QString category;
 
43
    int weight;
 
44
    KService::Ptr service;
 
45
    KCModuleInfo item;
 
46
};
 
47
 
 
48
MenuItem::MenuItem( bool isMenu, MenuItem * itsParent )
 
49
    : d( new Private() )
 
50
{
 
51
    d->parent = itsParent;
 
52
    d->menu = isMenu;
 
53
 
 
54
    if ( d->parent ) {
 
55
        d->parent->children().append( this );
 
56
    }
 
57
}
 
58
 
 
59
MenuItem::~MenuItem()
 
60
{
 
61
    qDeleteAll( d->children );
 
62
    delete d;
 
63
}
 
64
 
 
65
void MenuItem::sortChildrenByWeight()
 
66
{
 
67
    qSort( d->children.begin(), d->children.end(), childIsLessThan );
 
68
}
 
69
 
 
70
MenuItem * MenuItem::child( int index )
 
71
{
 
72
    return d->children.at(index);
 
73
}
 
74
 
 
75
QStringList MenuItem::keywords()
 
76
{
 
77
    QStringList listOfKeywords;
 
78
 
 
79
    listOfKeywords << d->item.keywords() << d->name;
 
80
    foreach ( MenuItem * child, d->children ) {
 
81
        listOfKeywords += child->keywords();
 
82
    }
 
83
    return listOfKeywords;
 
84
}
 
85
 
 
86
MenuItem* MenuItem::parent() const
 
87
{
 
88
    return d->parent;
 
89
}
 
90
 
 
91
QList<MenuItem*>& MenuItem::children() const
 
92
{
 
93
    return d->children;
 
94
}
 
95
 
 
96
KService::Ptr& MenuItem::service() const
 
97
{
 
98
    return d->service;
 
99
}
 
100
 
 
101
KCModuleInfo& MenuItem::item() const
 
102
{
 
103
    return d->item;
 
104
}
 
105
 
 
106
QString& MenuItem::name() const
 
107
{
 
108
    return d->name;
 
109
}
 
110
 
 
111
QString& MenuItem::category() const
 
112
{
 
113
    return d->category;
 
114
}
 
115
 
 
116
int MenuItem::weight()
 
117
{
 
118
    return d->weight;
 
119
}
 
120
 
 
121
bool MenuItem::menu() const
 
122
{
 
123
    return d->menu;
 
124
}
 
125
 
 
126
void MenuItem::setService( const KService::Ptr& service )
 
127
{
 
128
    d->service = service;
 
129
    d->category = service->property("X-KDE-System-Settings-Category").toString();
 
130
    d->name = service->name();
 
131
    d->item = KCModuleInfo( service );
 
132
    const QVariant itemWeight = service->property( "X-KDE-Weight", QVariant::Int );
 
133
    if( itemWeight.isValid() ) {
 
134
        d->weight = itemWeight.toInt();
 
135
    } else {
 
136
        d->weight = 100;
 
137
    }
 
138
}