1 | /* This file is part of the KDE libraries |
2 | Copyright (C) 2004 Frans Englich <frans.englich@telia.com> |
3 | |
4 | This library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public |
6 | License as published by the Free Software Foundation; either |
7 | version 2 of the License, or (at your option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | Library General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to |
16 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | Boston, MA 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "kcmodulecontainer.h" |
21 | #include "kcmodulecontainer.moc" |
22 | |
23 | #include <QtGui/QLayout> |
24 | #include <QtGui/QPixmap> |
25 | #include <QtCore/QStringList> |
26 | |
27 | #include <kcmodule.h> |
28 | #include <kcmoduleinfo.h> |
29 | #include <kcmoduleloader.h> |
30 | #include <kcmoduleproxy.h> |
31 | #include <kdebug.h> |
32 | #include <kdialog.h> |
33 | #include <kglobal.h> |
34 | #include <kguiitem.h> |
35 | #include <kicon.h> |
36 | #include <kiconloader.h> |
37 | #include <kpushbutton.h> |
38 | #include <kservice.h> |
39 | #include <kstandardguiitem.h> |
40 | #include <ktabwidget.h> |
41 | |
42 | /***********************************************************************/ |
43 | class KCModuleContainer::KCModuleContainerPrivate |
44 | { |
45 | public: |
46 | KCModuleContainerPrivate( const QStringList& mods ) |
47 | : modules( mods ) |
48 | , tabWidget( 0 ) |
49 | , topLayout( 0 ) |
50 | {} |
51 | |
52 | QStringList modules; |
53 | KTabWidget *tabWidget; |
54 | KCModule::Buttons buttons; |
55 | QVBoxLayout *topLayout; |
56 | |
57 | |
58 | }; |
59 | /***********************************************************************/ |
60 | |
61 | |
62 | |
63 | // The KCModuleContainer is only a wrapper around real KCModules. Therefore it doesn't need a |
64 | // special KComponentData and can just use the global instance. The contained KCModules create their own |
65 | // KComponentData objects when needed. |
66 | /***********************************************************************/ |
67 | KCModuleContainer::KCModuleContainer( QWidget* parent, const QString& mods ) |
68 | : KCModule( KGlobal::mainComponent(), parent ), |
69 | d(new KCModuleContainerPrivate( QString(mods).remove( ' ' ).split( ',', QString::SkipEmptyParts ) )) |
70 | { |
71 | init(); |
72 | } |
73 | |
74 | KCModuleContainer::KCModuleContainer( QWidget* parent, const QStringList& mods ) |
75 | : KCModule( KGlobal::mainComponent(), parent ), |
76 | d( new KCModuleContainerPrivate( mods ) ) |
77 | { |
78 | init(); |
79 | } |
80 | |
81 | void KCModuleContainer::init() |
82 | { |
83 | d->topLayout = new QVBoxLayout( this ); |
84 | d->topLayout->setMargin( 0 ); |
85 | d->topLayout->setObjectName( "topLayout" ); |
86 | d->tabWidget = new KTabWidget(this); |
87 | d->tabWidget->setObjectName( "tabWidget" ); |
88 | connect( d->tabWidget, SIGNAL(currentChanged(int)), SLOT(tabSwitched(int))); |
89 | d->topLayout->addWidget( d->tabWidget ); |
90 | |
91 | if ( !d->modules.isEmpty() ) |
92 | { |
93 | /* Add our modules */ |
94 | for ( QStringList::const_iterator it = d->modules.constBegin(); it != d->modules.constEnd(); ++it ) |
95 | addModule( (*it) ); |
96 | } |
97 | } |
98 | |
99 | void KCModuleContainer::addModule( const QString& module ) |
100 | { |
101 | /* In case it doesn't exist we just silently drop it. |
102 | * This allows people to easily extend containers. |
103 | * For example, KCM monitor gamma can be in kdegraphics. |
104 | */ |
105 | KService::Ptr service = KService::serviceByDesktopName( module ); |
106 | if ( !service ) |
107 | { |
108 | kDebug(713) << "KCModuleContainer: module '" << |
109 | module << "' was not found and thus not loaded" << endl; |
110 | return; |
111 | } |
112 | |
113 | if ( service->noDisplay() ) |
114 | return; |
115 | |
116 | KCModuleProxy* proxy = new KCModuleProxy( service, d->tabWidget ); |
117 | allModules.append( proxy ); |
118 | |
119 | proxy->setObjectName( module.toLatin1() ); |
120 | |
121 | d->tabWidget->addTab( proxy, KIcon( proxy->moduleInfo().icon() ), |
122 | /* Qt eats ampersands for dinner. But not this time. */ |
123 | proxy->moduleInfo().moduleName().replace( '&', "&&" )); |
124 | |
125 | d->tabWidget->setTabToolTip( d->tabWidget->indexOf( proxy ), proxy->moduleInfo().comment() ); |
126 | |
127 | connect( proxy, SIGNAL(changed(KCModuleProxy*)), SLOT(moduleChanged(KCModuleProxy*))); |
128 | |
129 | /* Collect our buttons - we go for the common deliminator */ |
130 | setButtons( buttons() | proxy->realModule()->buttons() ); |
131 | } |
132 | |
133 | void KCModuleContainer::tabSwitched(int index) |
134 | { |
135 | KCModuleProxy* mod = static_cast<KCModuleProxy *>(d->tabWidget->widget(index)); |
136 | setQuickHelp( mod->quickHelp() ); |
137 | setAboutData( mod->aboutData() ); |
138 | } |
139 | |
140 | void KCModuleContainer::save() |
141 | { |
142 | ModuleList list = changedModules; |
143 | ModuleList::iterator it; |
144 | for ( it = list.begin() ; it !=list.end() ; ++it ) |
145 | { |
146 | (*it)->save(); |
147 | } |
148 | |
149 | emit changed( false ); |
150 | |
151 | } |
152 | |
153 | void KCModuleContainer::load() |
154 | { |
155 | ModuleList list = allModules; |
156 | ModuleList::iterator it; |
157 | for ( it = list.begin() ; it !=list.end() ; ++it ) |
158 | { |
159 | (*it)->load(); |
160 | } |
161 | |
162 | emit changed( false ); |
163 | } |
164 | |
165 | void KCModuleContainer::defaults() |
166 | { |
167 | ModuleList list = allModules; |
168 | ModuleList::iterator it; |
169 | for ( it = list.begin() ; it !=list.end() ; ++it ) |
170 | { |
171 | (*it)->defaults(); |
172 | } |
173 | |
174 | emit changed( true ); |
175 | } |
176 | |
177 | |
178 | void KCModuleContainer::moduleChanged(KCModuleProxy * proxy) |
179 | { |
180 | changedModules.append( proxy ); |
181 | if( changedModules.isEmpty() ) |
182 | return; |
183 | |
184 | emit changed(true); |
185 | } |
186 | |
187 | KCModuleContainer::~KCModuleContainer() |
188 | { |
189 | delete d; |
190 | } |
191 | |
192 | /***********************************************************************/ |
193 | |
194 | |
195 | |
196 | |
197 | |