add more spacing
[personal-kdebase.git] / workspace / systemsettings / modulesview.cpp
blob3a4550864926a53f83d034d0313a597ecbfd190f
1 /**
2 * This file is part of the System Settings package
3 * Copyright (C) 2005 Benjamin C Meyer (ben+systempreferences at meyerhome dot net)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This library 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 GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 #include "modulesview.h"
23 #include <qlabel.h>
24 #include <QListWidget>
25 #include <qlayout.h>
26 #include <kiconloader.h>
27 #include <kdebug.h>
28 #include <kservicetypetrader.h>
29 #include <QApplication>
30 #include <kdialog.h>
32 #include "kcmsearch.h"
33 #include "moduleiconitem.h"
34 #include "kcmodulemenu.h"
36 ModulesView::ModulesView( KCModuleMenu *rootMenu, const QString &menuPath, QWidget *parent ) : QWidget( parent ), rootMenu( NULL )
38 this->rootMenu = rootMenu;
39 this->menuPath = menuPath;
40 this->categories = KServiceTypeTrader::self()->query("SystemSettingsCategory");
42 QVBoxLayout *layout = new QVBoxLayout( this );
43 layout->setMargin( KDialog::marginHint() );
44 layout->setSpacing( KDialog::spacingHint() );
45 layout->setObjectName( QLatin1String( "layout" ) );
47 displayName = this->rootMenu->caption;
49 QList<MenuItem> subMenus = rootMenu->menuList(menuPath);
50 QList<MenuItem>::const_iterator it;
51 for ( it = subMenus.begin(); it != subMenus.end(); ++it ){
52 if( !(*it).menu )
53 continue;
55 // After the first time around add a line
56 if( it != subMenus.begin() ){
57 QFrame *line = new QFrame( this );
58 line->setObjectName( QLatin1String( "line" ) );
59 line->setFrameShadow( QFrame::Sunken );
60 line->setFrameShape( QFrame::HLine );
61 layout->addWidget( line );
64 // Build the row of modules/icons
65 createRow( (*it).subMenu, layout );
67 layout->addStretch(1);
69 setBackgroundRole(QPalette::Base);
70 setForegroundRole(QPalette::Text);
73 // Align them up!
74 uint most = 0;
75 QList<RowIconView*>::iterator it2;
76 for ( it2 = groups.begin(); it2 != groups.end(); ++it2 ){
77 for (int i = 0; i < (*it2)->count(); ++i ) {
78 QListWidgetItem * item = (*it2)->item( i );
79 if ( item && item->sizeHint().width() > most ) {
80 most = item->sizeHint().width();
85 /*FIXME
86 for ( it = groups.begin(); it != groups.end(); ++it )
87 (*it)->setGridX(most);
93 ModulesView::~ModulesView()
97 void ModulesView::createRow( const QString &parentPath, QBoxLayout *boxLayout )
99 //find the category name and search for it
100 QString categoryName = parentPath.section('/', -2, -2);
101 QString iconName;
102 for (int i = 0; i < categories.size(); ++i) {
103 const KService* entry = categories.at(i).data();
104 if (entry->name() == categoryName) {
105 iconName = entry->icon();
106 break;
110 // Make header
111 QHBoxLayout *rowLayout = new QHBoxLayout();
112 rowLayout->setMargin( 0 );
113 rowLayout->setSpacing( 6 );
114 rowLayout->setObjectName( QLatin1String( "rowLayout" ) );
116 // Header Icon
117 QLabel *icon = new QLabel( this );
118 icon->setObjectName( QLatin1String( "groupicon" ) );
119 icon->setPixmap( SmallIcon( iconName ));
120 QSizePolicy sp( QSizePolicy::Minimum, QSizePolicy::Preferred );
121 sp.setHeightForWidth( icon->sizePolicy().hasHeightForWidth() );
122 icon->setSizePolicy( sp );
123 rowLayout->addWidget( icon );
125 // Header Name
126 QLabel *textLabel = new QLabel( this );
127 textLabel->setObjectName( QLatin1String( "groupcaption" ) );
128 textLabel->setText( categoryName );
129 QSizePolicy sp1( QSizePolicy::Expanding, QSizePolicy::Preferred );
130 sp1.setHeightForWidth( textLabel->sizePolicy().hasHeightForWidth() );
131 textLabel->setSizePolicy( sp1 );
132 QFont textLabel_font( textLabel->font() );
133 textLabel_font.setBold( true );
134 textLabel->setFont( textLabel_font );
135 rowLayout->addWidget( textLabel );
137 boxLayout->addLayout( rowLayout );
139 // Make IconView
140 RowIconView* iconWidget = new RowIconView( this );
141 connect(iconWidget, SIGNAL( itemClicked( QListWidgetItem* ) ),
142 this, SIGNAL( itemSelected( QListWidgetItem* ) ) );
143 connect(iconWidget, SIGNAL( itemActivated( QListWidgetItem* ) ),
144 this, SIGNAL( itemSelected( QListWidgetItem* ) ) );
145 groups.append( iconWidget );
146 boxLayout->addWidget( iconWidget );
148 int height = 0;
149 // Add all the items in their proper order
150 QList<MenuItem> list = rootMenu->menuList( parentPath );
151 QList<MenuItem>::const_iterator it;
152 for ( it = list.begin(); it != list.end(); ++it ){
153 ModuleIconItem *item = NULL;
154 if( !(*it).menu ) {
155 item = new ModuleIconItem( iconWidget, (*it).item );
156 } else {
157 QString path = (*it).subMenu;
159 QString categoryCaption = (*it).caption;
160 QString iconFile;
161 for (int i = 0; i < categories.size(); ++i) {
162 const KService* entry = categories.at(i).data();
163 if (entry->name() == categoryCaption) {
164 iconFile = entry->icon();
165 break;
169 const QList<KCModuleInfo> &modules = rootMenu->modules( path );
170 if ( modules.count() > 0 ) {
171 item = new ModuleIconItem( iconWidget, categoryCaption, iconFile);
172 item->modules = modules;
175 if (item) height = qMax(height, item->data(Qt::SizeHintRole).toSize().height());
178 // give the proper height to make all the items visible
179 iconWidget->setMinimumHeight(height);
182 void ModulesView::clearSelection() {
183 QList<RowIconView*>::const_iterator it;
184 for ( it = groups.begin(); it != groups.end(); ++it ) {
185 (*it)->clearSelection();
189 #include "modulesview.moc"