2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.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.
13 #include <QStringList>
14 #include <QListWidgetItem>
16 #include <KStandardDirs>
19 #include "mastersettings.h"
20 #include "luaapi/loader.h"
23 #include "pref_theme.h"
26 PrefTheme::ThemeInfoList
PrefTheme::to_theme_info_list(const QStringList
& files
, const Settings
& s
) {
27 //kDebug() << "about to examine " << files.size() << " desktop files";
28 std::map
<QString
, ThemeInfo
> cached_info
;
30 SettingArray themes
= s
.group("themes").array("theme");
31 foreach (Settings s_theme
, themes
) {
32 ThemeInfo info
= ThemeInfo::fromSettings(s_theme
);
33 cached_info
[info
.desktopFile
] = info
;
36 ThemeInfoList all_themes
;
39 for(int i
= 0; i
< files
.size(); i
++) {
40 QFileInfo
file_info(files
[i
]);
41 std::map
<QString
, ThemeInfo
>::iterator it
= cached_info
.find(files
[i
]);
43 if (it
!= cached_info
.end()
45 && it
->second
.last_modified
== file_info
.lastModified() ) {
46 all_themes
<< it
->second
;
47 cached_info
.erase(it
);
52 ThemeInfo theme_info
= ThemeInfo::fromDesktopFile(files
[i
]);
53 all_themes
<< theme_info
;
55 if (theme_info
.name
.isEmpty()) {
56 kError() << "No name property in" << files
[i
];
61 if(!cached_info
.empty())
64 /* rewrite the cached configuration */
66 SettingArray themes
= s
.group("themes").newArray("theme");
68 for (int i
= 0; i
< all_themes
.size(); i
++) {
69 Settings s_theme
= themes
.append();
70 all_themes
[i
].save(s_theme
);
78 OptList
PrefTheme::get_file_options(const QString
& f
, bool reload_defaults
) {
79 //kDebug() << "get file options for " << f;
81 if(!reload_defaults
) {
82 std::map
<QString
, OptList
>::iterator it
= m_new_theme_options
.find(f
);
84 if(it
!= m_new_theme_options
.end())
88 LuaApi::Loader lua_context
;
89 lua_context
.runFile(f
);
91 if(lua_context
.error()) {
92 kError() << lua_context
.errorString();
93 lua_context
.clearError();
95 m_new_theme_options
[f
] = OptList();
99 OptList o
= lua_context
.getValue
<OptList
>("options", 0, NULL
, true);
100 if(lua_context
.error()) {
101 kError() << lua_context
.errorString();
102 lua_context
.clearError();
105 if(!reload_defaults
) {
106 SettingMap
<QString
> s_lua
= settings().group("lua-settings").map
<QString
>("entry", "file-name");
107 Settings entry
= s_lua
.insert(f
);
108 options_list_load_from_settings(o
, entry
.group("options"));
110 m_new_theme_options
[f
] = o
;
116 int PrefTheme::theme_ok_for_variant(const ThemeInfo
& theme_info
, const QString
& variant_name
) {
117 if(theme_info
.variants
.contains(variant_name
+"[default]", Qt::CaseInsensitive
))
119 if(theme_info
.variants
.contains(variant_name
, Qt::CaseInsensitive
))
121 if(theme_info
.variants
.contains("any[default]", Qt::CaseInsensitive
))
123 if(theme_info
.variants
.contains("any", Qt::CaseInsensitive
))
129 PrefTheme::PrefTheme(const QString
& currentVariant
, QWidget
*parent
)
135 c
= new Category(NULL
, this);
136 m_categories
["pieces"] = c
;
137 tabWidget
->addTab(c
, "&Pieces");
139 c
= new Category(NULL
, this);
140 m_categories
["squares"] = c
;
141 tabWidget
->addTab(c
, "&Squares");
143 c
= new Category(NULL
, this);
144 m_categories
["controls"] = c
;
145 tabWidget
->addTab(c
, "&Controls");
147 MasterSettings
cached_theme_info("tagua_config_cache.xml");
148 connect(comboVariant
, SIGNAL(currentIndexChanged(int)), this, SLOT(variantChanged()));
150 for(CategoryMap::iterator cit
= m_categories
.begin(); cit
!= m_categories
.end(); ++cit
) {
151 Category
* c
= cit
->second
;
153 c
->m_list
->setSelectionMode(QAbstractItemView::SingleSelection
);
154 connect(c
->m_list
, SIGNAL(itemSelectionChanged()), c
, SLOT(themeChanged()));
155 connect(c
->m_check
, SIGNAL(toggled(bool)), c
, SLOT(themeChecked(bool)));
156 c
->m_opt_layout
= new QHBoxLayout(c
->m_widget
);
157 c
->m_opt_layout
->setMargin(0);
159 c
->m_themes
= to_theme_info_list(
160 KGlobal::dirs()->findAllResources("appdata",
161 "themes/"+cit
->first
+"/*.desktop",
162 KStandardDirs::Recursive
),
163 cached_theme_info
.group(cit
->first
)
166 //kDebug() << "loaded " << c->m_themes.size() << " themes";
169 QStringList all
= Variants::instance().all();
172 foreach (QString variant
, all
) {
173 if (variant
== currentVariant
) {
176 comboVariant
->addItem(variant
, QVariant(variant
));
181 comboVariant
->setCurrentIndex(current
);
186 PrefTheme::~PrefTheme() {
190 void PrefTheme::apply() {
191 SettingMap
<QString
> variants
= settings().group("variants").map
<QString
>("variant", "name");
193 for(CategoryMap::iterator cit
= m_categories
.begin(); cit
!= m_categories
.end(); ++cit
) {
194 Category
* c
= cit
->second
;
196 for(std::map
<QString
, QString
>::iterator it
= c
->m_new_themes
.begin();
197 it
!= c
->m_new_themes
.end(); ++it
) {
198 Settings var
= variants
.insert(it
->first
);
199 var
[cit
->first
+"-theme"] = it
->second
;
201 for(std::map
<QString
, bool>::iterator it
= c
->m_new_use_def
.begin();
202 it
!= c
->m_new_use_def
.end(); ++it
) {
203 Settings var
= variants
.insert(it
->first
);
204 var
[cit
->first
+"-use-def"] = it
->second
;
208 for(std::map
<QString
, OptList
>::iterator it
= m_new_theme_options
.begin();
209 it
!= m_new_theme_options
.end(); ++it
) {
210 SettingMap
<QString
> s_lua
= settings().group("lua-settings").map
<QString
>("entry", "file-name");
211 Settings entry
= s_lua
.insert(it
->first
);
212 options_list_save_to_settings(it
->second
, entry
.group("options"));
216 void PrefTheme::update_list_view(QListWidget
* list
, const ThemeInfoList
& themes
,
217 QString variant_name
, QString settings_theme
) {
221 QListWidgetItem
*item_to_select
= NULL
;
223 for (int i
= 0; i
< themes
.size(); i
++) {
224 int ok
= theme_ok_for_variant(themes
[i
], variant_name
);
228 ok
= (themes
[i
].desktopFile
== settings_theme
) ? 5 : ok
;
229 QListWidgetItem
*list_item
= new QListWidgetItem(themes
[i
].name
, list
);
231 list_item
->setData(Qt::UserRole
, i
);
232 if(ok
> selected_ok
) {
233 item_to_select
= list_item
;
239 list
->setItemSelected(item_to_select
, true);
242 void PrefTheme::variantChanged() {
243 QString category
= comboVariant
->itemData(comboVariant
->currentIndex()).toString();
244 VariantFactory
* vi
= Variants::instance().getFactory(category
);
247 for (CategoryMap::iterator cit
= m_categories
.begin(); cit
!= m_categories
.end(); ++cit
) {
248 Category
* c
= cit
->second
;
252 c
->m_list
->setEnabled(false);
253 c
->m_label
->setText(QString());
254 c
->m_label
->setEnabled(false);
260 QString variant_name
= vi
->name();
261 QString variant_proxy
= vi
->themeProxy();
262 SettingMap
<QString
> variants
= settings().group("variants").map
<QString
>("variant", "name");
263 Settings var
= variants
.insert(variant_name
);
264 bool check_proxy
= variant_name
!= variant_proxy
;
266 for(CategoryMap::iterator cit
= m_categories
.begin(); cit
!= m_categories
.end(); ++cit
) {
267 Category
* c
= cit
->second
;
269 c
->m_check
->setVisible(check_proxy
);
271 bool use_def
= c
->m_new_use_def
.count(variant_name
)
272 ? c
->m_new_use_def
[variant_name
]
273 : (var
[cit
->first
+"-use-def"] | true).value();
274 c
->m_check
->setText("Same as "+variant_proxy
);
275 c
->m_check
->setChecked(use_def
);
276 c
->m_list
->setEnabled(!use_def
);
277 c
->m_label
->setEnabled(!use_def
);
280 c
->m_list
->setEnabled(true);
281 c
->m_label
->setEnabled(true);
284 QString settings_theme
= c
->m_new_themes
.count(variant_name
)
285 ? c
->m_new_themes
[variant_name
]
286 : (var
[cit
->first
+"-theme"] | QString()).value();
287 update_list_view(c
->m_list
, c
->m_themes
, variant_proxy
, settings_theme
);
291 ThemeInfo
PrefTheme::getBestTheme(const VariantPtr
& vi
, const QString
& category
) {
292 QString tag
= category
+ "-theme";
293 QString deftag
= category
+ "-use-def";
294 QString variant_name
= vi
->name();
295 QString variant_proxy_name
= vi
->themeProxy();
296 SettingMap
<QString
> variants
= settings().group("variants").map
<QString
>("variant", "name");
297 if (variant_name
!= vi
->themeProxy() &&
298 (variants
.insert(variant_name
)[deftag
] | true) )
299 variant_name
= vi
->themeProxy();
301 Settings var_settings
= variants
.insert(variant_name
);
302 if (var_settings
[tag
] && QFile::exists(var_settings
[tag
].value
<QString
>()) ) {
304 // there is a theme in the settings, so pick this
305 ThemeInfo res
= ThemeInfo::fromDesktopFile(var_settings
[tag
].value
<QString
>());
306 if(theme_ok_for_variant(res
, variant_proxy_name
))
310 MasterSettings
cached_theme_info("tagua_config_cache.xml");
311 KStandardDirs
* std_dirs
= KGlobal::dirs();
312 ThemeInfoList themes
= to_theme_info_list(std_dirs
->findAllResources("appdata",
313 "themes/" + category
+ "/*.desktop",
314 KStandardDirs::Recursive
),
315 cached_theme_info
.group(category
));
319 for(int i
= 0; i
< themes
.size(); i
++) {
320 int ok
= theme_ok_for_variant(themes
[i
], variant_proxy_name
);
332 var_settings
[tag
] = retv
->desktopFile
;
335 return retv
? *retv
: ThemeInfo();
338 PrefThemeCategory::PrefThemeCategory(QWidget
* parent
, PrefTheme
* owner
)
342 , m_opt_widget(NULL
) {
344 m_reset
= new QAction(KIcon("edit-clear"), i18n("&Reset to Default"), this);
345 m_reset
->setShortcut(Qt::CTRL
+Qt::Key_Z
);
346 connect(m_reset
, SIGNAL(triggered()), this, SLOT(reset()));
347 m_resetButton
->setDefaultAction(m_reset
);
348 m_resetButton
->setToolButtonStyle(Qt::ToolButtonIconOnly
);
351 void PrefThemeCategory::reset() {
355 QList
<QListWidgetItem
*> l
= m_list
->selectedItems();
359 int i
= l
[0]->data(Qt::UserRole
).toInt();
360 if (i
>= 0 && i
< m_themes
.size()) {
361 OptList ol
= m_parent
->get_file_options(m_themes
[i
].file_name
, true);
362 qobject_cast
<OptionWidget
*>(m_opt_widget
)->setValues(ol
);
366 void PrefThemeCategory::themeChanged() {
367 QList
<QListWidgetItem
*> l
= m_list
->selectedItems();
369 int i
= l
[0]->data(Qt::UserRole
).toInt();
370 if(i
>=0 && i
<m_themes
.size()) {
371 m_label
->setText(m_themes
[i
].description
);
373 QString c
= m_parent
->comboVariant
->itemData(m_parent
->comboVariant
->currentIndex()).toString();
374 VariantFactory
* vi
= Variants::instance().getFactory(c
);
376 m_new_themes
[vi
->name()] = m_themes
[i
].desktopFile
;
382 OptList ol
= m_parent
->get_file_options(m_themes
[i
].file_name
);
383 if (ol
.size() != 0 && m_list
->isEnabled()) {
384 m_opt_widget
= new OptionWidget(ol
, m_widget
);
385 m_opt_layout
->addWidget(m_opt_widget
);
386 m_reset
->setEnabled(true);
389 m_reset
->setEnabled(false);
393 m_label
->setText(QString());
396 void PrefThemeCategory::themeChecked(bool ck
) {
397 m_list
->setEnabled(!ck
);
398 m_label
->setEnabled(!ck
);
400 QString c
= m_parent
->comboVariant
->itemData(m_parent
->comboVariant
->currentIndex()).toString();
401 VariantFactory
* vi
= Variants::instance().getFactory(c
);
403 m_new_use_def
[vi
->name()] = ck
;