1
/**************************************************************************
2
* Copyright (C) 2009 Ben Cooksley <bcooksley@kde.org> *
3
* Copyright (C) 2008 Mathias Soeken <msoeken@informatik.uni-bremen.de> *
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. *
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. *
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 *
19
***************************************************************************/
21
#include "ClassicMode.h"
22
#include "ui_configClassic.h"
27
#include <QModelIndex>
28
#include <QStackedWidget>
29
#include <QAbstractItemModel>
32
#include <KCModuleInfo>
33
#include <KConfigDialog>
34
#include <KGlobalSettings>
37
#include "MenuModel.h"
38
#include "ModuleView.h"
39
#include "CategoryList.h"
40
#include "MenuProxyModel.h"
42
K_PLUGIN_FACTORY(ClassicModeFactory, registerPlugin<ClassicMode>();)
43
K_EXPORT_PLUGIN(ClassicModeFactory("classic_mode"))
45
class ClassicMode::Private {
47
Private() : moduleView( 0 ) {}
52
QSplitter * classicWidget;
53
QTreeView * classicTree;
54
Ui::ConfigClassic classicConfig;
55
CategoryList * classicCategory;
56
QStackedWidget * stackedWidget;
57
ModuleView * moduleView;
59
MenuProxyModel * proxyModel;
61
KAboutData * aboutClassic;
64
ClassicMode::ClassicMode( QObject * parent, const QVariantList& )
65
: BaseMode( parent ), d( new Private() )
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");
75
ClassicMode::~ClassicMode()
77
if( !d->classicTree ) {
78
delete d->classicWidget;
83
void ClassicMode::initEvent()
86
d->model = new MenuModel( rootItem(), this );
88
// Move items that are the sole child of a category up....
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 );
101
QWidget * ClassicMode::mainWidget()
103
if( !d->classicTree ) {
106
return d->classicWidget;
109
KAboutData * ClassicMode::aboutData()
111
return d->aboutClassic;
114
ModuleView * ClassicMode::moduleView() const
116
return d->moduleView;
119
QList<QAbstractItemView*> ClassicMode::views() const
121
QList<QAbstractItemView*> theViews;
122
theViews << d->classicTree;
126
void ClassicMode::saveState()
128
config().writeEntry( "viewLayout", d->classicWidget->sizes() );
132
void ClassicMode::expandColumns()
134
d->classicTree->resizeColumnToContents(0);
137
void ClassicMode::searchChanged( const QString& text )
139
d->proxyModel->setFilterRegExp(text);
140
if( d->classicTree ) {
141
d->classicCategory->changeModule( d->classicTree->currentIndex() );
145
void ClassicMode::selectModule( const QModelIndex& selectedModule )
147
d->classicTree->setCurrentIndex( selectedModule );
148
if( d->proxyModel->rowCount(selectedModule) > 0 ) {
149
d->classicTree->setExpanded(selectedModule, true);
151
changeModule( selectedModule );
154
void ClassicMode::changeModule( const QModelIndex& activeModule )
156
if( !d->moduleView->resolveChanges() ) {
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 );
165
d->moduleView->loadModule( activeModule );
169
void ClassicMode::moduleLoaded()
171
d->stackedWidget->setCurrentWidget( d->moduleView );
174
void ClassicMode::initWidget()
177
d->classicTree = new QTreeView( d->classicWidget );
178
d->classicCategory = new CategoryList( d->classicWidget, d->proxyModel );
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 );
185
d->classicWidget->addWidget( d->classicTree );
186
d->classicWidget->addWidget( d->stackedWidget );
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 );
197
d->classicCategory->changeModule( d->classicTree->rootIndex() );
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()) );
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&)) );
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 );
217
QList<int> defaultSizes;
218
defaultSizes << 250 << 500;
219
d->classicWidget->setSizes( config().readEntry( "viewLayout", defaultSizes ) );
222
void ClassicMode::leaveModuleView()
224
d->moduleView->closeModules();
225
d->stackedWidget->setCurrentWidget( d->classicCategory );
228
void ClassicMode::giveFocus()
230
d->classicTree->setFocus();
233
void ClassicMode::addConfiguration( KConfigDialog * config )
235
QWidget * configWidget = new QWidget( config );
236
d->classicConfig.setupUi( configWidget );
237
config->addPage( configWidget, i18n("Tree View"), aboutData()->programIconName() );
240
void ClassicMode::loadConfiguration()
242
d->classicConfig.CbExpand->setChecked( config().readEntry( "autoExpandOneLevel", false ) );
245
void ClassicMode::saveConfiguration()
247
config().writeEntry("autoExpandOneLevel", d->classicConfig.CbExpand->isChecked());
250
void ClassicMode::moveUp( MenuItem * item )
252
foreach( MenuItem * child, item->children() ) {
253
if( child->children().count() == 1 ) {
254
d->model->addException( child );
260
#include "ClassicMode.moc"