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

« back to all changes in this revision

Viewing changes to systemsettings/classic/ClassicMode.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
/**************************************************************************
 
2
 * Copyright (C) 2009 Ben Cooksley <bcooksley@kde.org>                    *
 
3
 * Copyright (C) 2008 Mathias Soeken <msoeken@informatik.uni-bremen.de>   *
 
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            *
 
7
 * as published by the Free Software Foundation; either version 2         *
 
8
 * of the License, or (at your option) any later version.                 *
 
9
 *                                                                        *
 
10
 * This program is distributed in the hope that it will be useful,        *
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 
13
 * GNU General Public License for more details.                           *
 
14
 *                                                                        *
 
15
 * You should have received a copy of the GNU General Public License      *
 
16
 * along with this program; if not, write to the Free Software            *
 
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA          *
 
18
 * 02110-1301, USA.                                                       *
 
19
***************************************************************************/
 
20
 
 
21
#include "ClassicMode.h"
 
22
#include "ui_configClassic.h"
 
23
 
 
24
#include <QLayout>
 
25
#include <QSplitter>
 
26
#include <QTreeView>
 
27
#include <QModelIndex>
 
28
#include <QStackedWidget>
 
29
#include <QAbstractItemModel>
 
30
 
 
31
#include <KAboutData>
 
32
#include <KCModuleInfo>
 
33
#include <KConfigDialog>
 
34
#include <KGlobalSettings>
 
35
 
 
36
#include "MenuItem.h"
 
37
#include "MenuModel.h"
 
38
#include "ModuleView.h"
 
39
#include "CategoryList.h"
 
40
#include "MenuProxyModel.h"
 
41
 
 
42
K_PLUGIN_FACTORY(ClassicModeFactory, registerPlugin<ClassicMode>();)
 
43
K_EXPORT_PLUGIN(ClassicModeFactory("classic_mode"))
 
44
 
 
45
class ClassicMode::Private {
 
46
public:
 
47
    Private() : moduleView( 0 ) {}
 
48
    virtual ~Private() {
 
49
        delete aboutClassic;
 
50
    }
 
51
 
 
52
    QSplitter * classicWidget;
 
53
    QTreeView * classicTree;
 
54
    Ui::ConfigClassic classicConfig;
 
55
    CategoryList * classicCategory;
 
56
    QStackedWidget * stackedWidget;
 
57
    ModuleView * moduleView;
 
58
 
 
59
    MenuProxyModel * proxyModel;
 
60
    MenuModel * model;
 
61
    KAboutData * aboutClassic;
 
62
};
 
63
 
 
64
ClassicMode::ClassicMode( QObject * parent, const QVariantList& )
 
65
    : BaseMode( parent ), d( new Private() )
 
66
{
 
67
    d->aboutClassic = new KAboutData( "TreeView", 0, ki18n("Tree View"),
 
68
                                   "1.0", ki18n("Provides a classic tree-based view of control modules."),
 
69
                                   KAboutData::License_GPL, ki18n("(c) 2009, Ben Cooksley"));
 
70
    d->aboutClassic->addAuthor(ki18n("Ben Cooksley"), ki18n("Author"), "bcooksley@kde.org");
 
71
    d->aboutClassic->addAuthor(ki18n("Mathias Soeken"), ki18n("Developer"), "msoeken@informatik.uni-bremen.de");
 
72
    d->aboutClassic->setProgramIconName("view-list-tree");
 
73
}
 
74
 
 
75
ClassicMode::~ClassicMode()
 
76
{
 
77
    if( !d->classicTree ) {
 
78
        delete d->classicWidget;
 
79
    }
 
80
    delete d;
 
81
}
 
82
 
 
83
void ClassicMode::initEvent()
 
84
{
 
85
    // Create the model
 
86
    d->model = new MenuModel( rootItem(), this );
 
87
    
 
88
    // Move items that are the sole child of a category up....
 
89
    moveUp( rootItem() );
 
90
 
 
91
    // Create the proxy model
 
92
    d->proxyModel = new MenuProxyModel( this );
 
93
    d->proxyModel->setSourceModel( d->model );
 
94
    d->proxyModel->sort( 0 );
 
95
    d->classicWidget = new QSplitter( Qt::Horizontal, 0 );
 
96
    d->classicWidget->setChildrenCollapsible( false );
 
97
    d->moduleView = new ModuleView( d->classicWidget );
 
98
    d->classicTree = 0;
 
99
}
 
100
 
 
101
QWidget * ClassicMode::mainWidget()
 
102
{
 
103
    if( !d->classicTree ) {
 
104
        initWidget();
 
105
    }
 
106
    return d->classicWidget;
 
107
}
 
108
 
 
109
KAboutData * ClassicMode::aboutData()
 
110
{
 
111
    return d->aboutClassic;
 
112
}
 
113
 
 
114
ModuleView * ClassicMode::moduleView() const
 
115
{
 
116
    return d->moduleView;
 
117
}
 
118
 
 
119
QList<QAbstractItemView*> ClassicMode::views() const
 
120
{
 
121
    QList<QAbstractItemView*> theViews;
 
122
    theViews << d->classicTree;
 
123
    return theViews;
 
124
}
 
125
 
 
126
void ClassicMode::saveState()
 
127
{
 
128
    config().writeEntry( "viewLayout", d->classicWidget->sizes() );
 
129
    config().sync();
 
130
}
 
131
 
 
132
void ClassicMode::expandColumns()
 
133
{
 
134
    d->classicTree->resizeColumnToContents(0);
 
135
}
 
136
 
 
137
void ClassicMode::searchChanged( const QString& text )
 
138
{
 
139
    d->proxyModel->setFilterRegExp(text);
 
140
    if( d->classicTree ) {
 
141
        d->classicCategory->changeModule( d->classicTree->currentIndex() );
 
142
    }
 
143
}
 
144
 
 
145
void ClassicMode::selectModule( const QModelIndex& selectedModule )
 
146
{
 
147
    d->classicTree->setCurrentIndex( selectedModule );
 
148
    if( d->proxyModel->rowCount(selectedModule) > 0 ) {
 
149
        d->classicTree->setExpanded(selectedModule, true);
 
150
    }
 
151
    changeModule( selectedModule );
 
152
}
 
153
 
 
154
void ClassicMode::changeModule( const QModelIndex& activeModule )
 
155
{
 
156
    if( !d->moduleView->resolveChanges() ) {
 
157
        return;
 
158
    }
 
159
    d->moduleView->closeModules();
 
160
    if( d->proxyModel->rowCount(activeModule) > 0 ) {
 
161
        d->stackedWidget->setCurrentWidget( d->classicCategory );
 
162
        d->classicCategory->changeModule(activeModule);
 
163
        emit viewChanged( false );
 
164
    } else {
 
165
        d->moduleView->loadModule( activeModule );
 
166
    }
 
167
}
 
168
 
 
169
void ClassicMode::moduleLoaded()
 
170
{
 
171
    d->stackedWidget->setCurrentWidget( d->moduleView );
 
172
}
 
173
 
 
174
void ClassicMode::initWidget()
 
175
{
 
176
    // Create the widget
 
177
    d->classicTree = new QTreeView( d->classicWidget );
 
178
    d->classicCategory = new CategoryList( d->classicWidget, d->proxyModel );
 
179
 
 
180
    d->stackedWidget = new QStackedWidget( d->classicWidget );
 
181
    d->stackedWidget->layout()->setMargin(0);
 
182
    d->stackedWidget->addWidget( d->classicCategory );
 
183
    d->stackedWidget->addWidget( d->moduleView );
 
184
 
 
185
    d->classicWidget->addWidget( d->classicTree );
 
186
    d->classicWidget->addWidget( d->stackedWidget );
 
187
 
 
188
    d->classicTree->setModel( d->proxyModel );
 
189
    d->classicTree->setHeaderHidden( true );
 
190
    d->classicTree->setIconSize( QSize( 24, 24 ) );
 
191
    d->classicTree->setSortingEnabled( true );
 
192
    d->classicTree->setMouseTracking( true );
 
193
    d->classicTree->setMinimumWidth( 200 );
 
194
    d->classicTree->setSelectionMode( QAbstractItemView::SingleSelection );
 
195
    d->classicTree->sortByColumn( 0, Qt::AscendingOrder );
 
196
 
 
197
    d->classicCategory->changeModule( d->classicTree->rootIndex() );
 
198
 
 
199
    connect( d->classicCategory, SIGNAL(moduleSelected( QModelIndex )), this, SLOT(selectModule( QModelIndex )) );
 
200
    connect( d->classicTree, SIGNAL(activated(const QModelIndex&)), this, SLOT(changeModule(const QModelIndex&)) );
 
201
    connect( d->classicTree, SIGNAL(collapsed(QModelIndex)), this, SLOT(expandColumns()) );
 
202
    connect( d->classicTree, SIGNAL(expanded(QModelIndex)), this, SLOT(expandColumns()) );
 
203
    connect( d->moduleView, SIGNAL(moduleChanged(bool)), this, SLOT(moduleLoaded()) );
 
204
 
 
205
    if( !KGlobalSettings::singleClick() ) {
 
206
        // Needed because otherwise activated() is not fired with single click, which is apparently expected for tree views
 
207
        connect( d->classicTree, SIGNAL(clicked(const QModelIndex&)), this, SLOT(changeModule(const QModelIndex&)) );
 
208
    }
 
209
 
 
210
    if( config().readEntry( "autoExpandOneLevel", false ) ) {
 
211
        for( int processed = 0; d->proxyModel->rowCount() > processed; processed++ ) {
 
212
            d->classicTree->setExpanded( d->proxyModel->index( processed, 0 ), true );
 
213
        }
 
214
    }
 
215
 
 
216
    expandColumns();
 
217
    QList<int> defaultSizes;
 
218
    defaultSizes << 250 << 500;
 
219
    d->classicWidget->setSizes( config().readEntry( "viewLayout", defaultSizes ) );
 
220
}
 
221
 
 
222
void ClassicMode::leaveModuleView()
 
223
{
 
224
    d->moduleView->closeModules();
 
225
    d->stackedWidget->setCurrentWidget( d->classicCategory );
 
226
}
 
227
 
 
228
void ClassicMode::giveFocus()
 
229
{
 
230
    d->classicTree->setFocus();
 
231
}
 
232
 
 
233
void ClassicMode::addConfiguration( KConfigDialog * config )
 
234
{
 
235
    QWidget * configWidget = new QWidget( config );
 
236
    d->classicConfig.setupUi( configWidget );
 
237
    config->addPage( configWidget, i18n("Tree View"), aboutData()->programIconName() );
 
238
}
 
239
 
 
240
void ClassicMode::loadConfiguration()
 
241
{
 
242
    d->classicConfig.CbExpand->setChecked( config().readEntry( "autoExpandOneLevel", false ) );
 
243
}
 
244
 
 
245
void ClassicMode::saveConfiguration()
 
246
{
 
247
    config().writeEntry("autoExpandOneLevel", d->classicConfig.CbExpand->isChecked());
 
248
}
 
249
 
 
250
void ClassicMode::moveUp( MenuItem * item )
 
251
{
 
252
    foreach( MenuItem * child, item->children() ) {
 
253
        if( child->children().count() == 1 ) {
 
254
            d->model->addException( child );
 
255
        }
 
256
        moveUp( child );
 
257
    }
 
258
}
 
259
 
 
260
#include "ClassicMode.moc"