Replaced all std::cout with kDebug.
[tagua/yd.git] / src / pref_theme.cpp
blobe824a37f6025054799c4804103f5718dc4e25f97
1 /*
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.
9 // */
11 #include <QDir>
12 #include <QFileInfo>
13 #include <QStringList>
14 #include <QListWidgetItem>
15 #include <KDebug>
16 #include <KStandardDirs>
18 #include "foreach.h"
19 #include "mastersettings.h"
20 #include "luaapi/loader.h"
21 #include "variants.h"
22 #include "tagua.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;
37 bool updated = false;
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()
44 && file_info.exists()
45 && it->second.last_modified == file_info.lastModified() ) {
46 all_themes << it->second;
47 cached_info.erase(it);
49 else {
50 updated = true;
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())
62 updated = true;
64 /* rewrite the cached configuration */
65 if(updated) {
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);
74 return all_themes;
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())
85 return it->second;
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();
96 return 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;
112 return 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))
118 return 4;
119 if(theme_info.variants.contains(variant_name, Qt::CaseInsensitive))
120 return 3;
121 if(theme_info.variants.contains("any[default]", Qt::CaseInsensitive))
122 return 2;
123 if(theme_info.variants.contains("any", Qt::CaseInsensitive))
124 return 1;
125 return 0;
129 PrefTheme::PrefTheme(const QString& currentVariant, QWidget *parent)
130 : QWidget(parent) {
131 setupUi(this);
133 Category *c;
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();
170 int index = 0;
171 int current = -1;
172 foreach (QString variant, all) {
173 if (variant == currentVariant) {
174 current = index;
176 comboVariant->addItem(variant, QVariant(variant));
177 index++;
180 if (current != -1)
181 comboVariant->setCurrentIndex(current);
183 variantChanged();
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) {
218 list->clear();
220 int selected_ok = 0;
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);
225 if (!ok)
226 continue;
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;
234 selected_ok = ok;
238 if(item_to_select)
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);
246 if (!vi) {
247 for (CategoryMap::iterator cit = m_categories.begin(); cit != m_categories.end(); ++cit) {
248 Category* c = cit->second;
250 c->m_check->hide();
251 c->m_list->clear();
252 c->m_list->setEnabled(false);
253 c->m_label->setText(QString());
254 c->m_label->setEnabled(false);
257 return;
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);
270 if(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);
279 else {
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))
307 return res;
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));
317 int best = 0;
318 ThemeInfo* retv = 0;
319 for(int i = 0; i < themes.size(); i++) {
320 int ok = theme_ok_for_variant(themes[i], variant_proxy_name);
321 if (!ok)
322 continue;
324 if (ok > best) {
325 retv = &themes[i];
326 best = ok;
331 if (retv && *retv) {
332 var_settings[tag] = retv->desktopFile;
335 return retv ? *retv : ThemeInfo();
338 PrefThemeCategory::PrefThemeCategory(QWidget* parent, PrefTheme* owner)
339 : QWidget(parent)
340 , m_parent(owner)
341 , m_opt_layout(NULL)
342 , m_opt_widget(NULL) {
343 setupUi(this);
344 m_reset = new QAction(KIcon("eraser"), "&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() {
352 if(!m_opt_widget)
353 return;
355 QList<QListWidgetItem *> l = m_list->selectedItems();
356 if(l.isEmpty())
357 return;
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();
368 if(!l.isEmpty()) {
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);
375 if(vi)
376 m_new_themes[vi->name()] = m_themes[i].desktopFile;
378 if(m_opt_widget) {
379 delete m_opt_widget;
380 m_opt_widget = NULL;
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);
388 else
389 m_reset->setEnabled(false);
390 return;
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);
402 if (vi)
403 m_new_use_def[vi->name()] = ck;
404 themeChanged();