Initial porting to the new component API.
[tagua/yd.git] / src / pref_theme.cpp
blob203abcd006dbb8ea101b685f109dcfcbbc17b0b6
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 <QListWidgetItem>
14 #include <QStringList>
16 #include <KDebug>
17 #include <KStandardDirs>
19 #include <core/variant.h>
21 #include "foreach.h"
22 #include "mastersettings.h"
23 #include "luaapi/loader.h"
24 #include "variants.h"
25 #include "pref_theme.h"
27 typedef boost::shared_ptr<Variant> VariantPtr;
29 PrefTheme::ThemeInfoList PrefTheme::to_theme_info_list(const QStringList& files, const Settings& s) {
30 //std::cout << "about to examine " << files.size() << " desktop files" << std::endl;
31 std::map<QString, ThemeInfo> cached_info;
33 SettingArray themes = s.group("themes").array("theme");
34 foreach (Settings s_theme, themes) {
35 ThemeInfo info = ThemeInfo::fromSettings(s_theme);
36 cached_info[info.desktopFile] = info;
39 ThemeInfoList all_themes;
40 bool updated = false;
42 for(int i = 0; i < files.size(); i++) {
43 QFileInfo file_info(files[i]);
44 std::map<QString, ThemeInfo>::iterator it = cached_info.find(files[i]);
46 if (it != cached_info.end()
47 && file_info.exists()
48 && it->second.last_modified == file_info.lastModified() ) {
49 all_themes << it->second;
50 cached_info.erase(it);
52 else {
53 updated = true;
55 ThemeInfo theme_info = ThemeInfo::fromDesktopFile(files[i]);
56 all_themes << theme_info;
58 if (theme_info.name.isEmpty())
59 kError() << "No name property in" << files[i];
63 if(!cached_info.empty())
64 updated = true;
66 /* rewrite the cached configuration */
67 if(updated) {
68 SettingArray themes = s.group("themes").newArray("theme");
70 for (int i = 0; i < all_themes.size(); i++) {
71 Settings s_theme = themes.append();
72 all_themes[i].save(s_theme);
76 return all_themes;
80 OptList PrefTheme::get_file_options(const QString& f, bool reload_defaults) {
81 //std::cout << "get file options for " << f << std::endl;
83 if(!reload_defaults) {
84 std::map<QString, OptList>::iterator it = m_new_theme_options.find(f);
86 if(it != m_new_theme_options.end())
87 return it->second;
90 LuaApi::Loader lua_context;
91 lua_context.runFile(f);
93 if(lua_context.error()) {
94 kError() << lua_context.errorString();
95 lua_context.clearError();
97 m_new_theme_options[f] = OptList();
98 return OptList();
101 OptList o = lua_context.getValue<OptList>("options", 0, NULL, true);
102 if(lua_context.error()) {
103 kError() << lua_context.errorString();
104 lua_context.clearError();
107 if(!reload_defaults) {
108 SettingMap<QString> s_lua = settings().group("lua-settings").map<QString>("entry", "file-name");
109 Settings entry = s_lua.insert(f);
110 options_list_load_from_settings(o, entry.group("options"));
112 m_new_theme_options[f] = o;
114 return o;
118 int PrefTheme::theme_ok_for_variant(const ThemeInfo& theme_info, const QString& variant_name) {
119 if(theme_info.variants.contains(variant_name+"[default]", Qt::CaseInsensitive))
120 return 4;
121 if(theme_info.variants.contains(variant_name, Qt::CaseInsensitive))
122 return 3;
123 if(theme_info.variants.contains("any[default]", Qt::CaseInsensitive))
124 return 2;
125 if(theme_info.variants.contains("any", Qt::CaseInsensitive))
126 return 1;
127 return 0;
131 PrefTheme::PrefTheme(const QString& currentVariant, QWidget *parent)
132 : QWidget(parent) {
133 setupUi(this);
135 Category *c;
137 c = new Category(NULL, this);
138 m_categories["pieces"] = c;
139 tabWidget->addTab(c, "&Pieces");
141 c = new Category(NULL, this);
142 m_categories["squares"] = c;
143 tabWidget->addTab(c, "&Squares");
145 c = new Category(NULL, this);
146 m_categories["controls"] = c;
147 tabWidget->addTab(c, "&Controls");
149 MasterSettings cached_theme_info("tagua_config_cache.xml");
150 connect(comboVariant, SIGNAL(currentIndexChanged(int)), this, SLOT(variantChanged()));
152 for(CategoryMap::iterator cit = m_categories.begin(); cit != m_categories.end(); ++cit) {
153 Category* c = cit->second;
155 c->m_list->setSelectionMode(QAbstractItemView::SingleSelection);
156 connect(c->m_list, SIGNAL(itemSelectionChanged()), c, SLOT(themeChanged()));
157 connect(c->m_check, SIGNAL(toggled(bool)), c, SLOT(themeChecked(bool)));
158 c->m_opt_layout = new QHBoxLayout(c->m_widget);
159 c->m_opt_layout->setMargin(0);
161 c->m_themes = to_theme_info_list(
162 KGlobal::dirs()->findAllResources("appdata",
163 "themes/"+cit->first+"/*.desktop",
164 KStandardDirs::Recursive),
165 cached_theme_info.group(cit->first)
168 //std::cout << "loaded " << c->m_themes.size() << " themes" << std::endl;
171 QStringList all = Variants::instance().all();
172 int index = 0;
173 int current = -1;
174 foreach (QString variant, all) {
175 if (variant == currentVariant) {
176 current = index;
178 comboVariant->addItem(variant, QVariant(variant));
179 index++;
182 if (current != -1)
183 comboVariant->setCurrentIndex(current);
185 variantChanged();
188 PrefTheme::~PrefTheme() {
192 void PrefTheme::apply() {
193 SettingMap<QString> variants = settings().group("variants").map<QString>("variant", "name");
195 for(CategoryMap::iterator cit = m_categories.begin(); cit != m_categories.end(); ++cit) {
196 Category* c = cit->second;
198 for(std::map<QString, QString>::iterator it = c->m_new_themes.begin();
199 it != c->m_new_themes.end(); ++it) {
200 Settings var = variants.insert(it->first);
201 var[cit->first+"-theme"] = it->second;
203 for(std::map<QString, bool>::iterator it = c->m_new_use_def.begin();
204 it != c->m_new_use_def.end(); ++it) {
205 Settings var = variants.insert(it->first);
206 var[cit->first+"-use-def"] = it->second;
210 for(std::map<QString, OptList>::iterator it = m_new_theme_options.begin();
211 it != m_new_theme_options.end(); ++it) {
212 SettingMap<QString> s_lua = settings().group("lua-settings").map<QString>("entry", "file-name");
213 Settings entry = s_lua.insert(it->first);
214 options_list_save_to_settings(it->second, entry.group("options"));
218 void PrefTheme::update_list_view(QListWidget* list, const ThemeInfoList& themes,
219 QString variant_name, QString settings_theme) {
220 list->clear();
222 int selected_ok = 0;
223 QListWidgetItem *item_to_select = NULL;
225 for (int i = 0; i < themes.size(); i++) {
226 int ok = theme_ok_for_variant(themes[i], variant_name);
227 if (!ok)
228 continue;
230 ok = (themes[i].desktopFile == settings_theme) ? 5 : ok;
231 QListWidgetItem *list_item = new QListWidgetItem(themes[i].name, list);
233 list_item->setData(Qt::UserRole, i);
234 if(ok > selected_ok) {
235 item_to_select = list_item;
236 selected_ok = ok;
240 if(item_to_select)
241 list->setItemSelected(item_to_select, true);
244 void PrefTheme::variantChanged() {
245 QString category = comboVariant->itemData(comboVariant->currentIndex()).toString();
246 VariantPtr vi(Variants::instance().get(category));
248 if (!vi) {
249 for (CategoryMap::iterator cit = m_categories.begin(); cit != m_categories.end(); ++cit) {
250 Category* c = cit->second;
252 c->m_check->hide();
253 c->m_list->clear();
254 c->m_list->setEnabled(false);
255 c->m_label->setText(QString());
256 c->m_label->setEnabled(false);
259 return;
262 QString variant_name = vi->name();
263 QString variant_proxy = vi->proxy();
264 SettingMap<QString> variants = settings().group("variants").map<QString>("variant", "name");
265 Settings var = variants.insert(variant_name);
266 bool check_proxy = variant_name != variant_proxy;
268 for(CategoryMap::iterator cit = m_categories.begin(); cit != m_categories.end(); ++cit) {
269 Category* c = cit->second;
271 c->m_check->setVisible(check_proxy);
272 if(check_proxy) {
273 bool use_def = c->m_new_use_def.count(variant_name)
274 ? c->m_new_use_def[variant_name]
275 : (var[cit->first+"-use-def"] | true).value();
276 c->m_check->setText("Same as "+variant_proxy);
277 c->m_check->setChecked(use_def);
278 c->m_list->setEnabled(!use_def);
279 c->m_label->setEnabled(!use_def);
281 else {
282 c->m_list->setEnabled(true);
283 c->m_label->setEnabled(true);
286 QString settings_theme = c->m_new_themes.count(variant_name)
287 ? c->m_new_themes[variant_name]
288 : (var[cit->first+"-theme"] | QString()).value();
289 update_list_view(c->m_list, c->m_themes, variant_proxy, settings_theme);
293 ThemeInfo PrefTheme::getBestTheme(Variant* vi, const QString& category) {
294 QString tag = category + "-theme";
295 QString deftag = category + "-use-def";
296 QString variant_name = vi->name();
297 QString variant_proxy_name = vi->proxy();
298 SettingMap<QString> variants = settings().group("variants").map<QString>("variant", "name");
299 if (variant_name != vi->proxy() &&
300 (variants.insert(variant_name)[deftag] | true) )
301 variant_name = vi->proxy();
303 Settings var_settings = variants.insert(variant_name);
304 if (var_settings[tag] && QFile::exists(var_settings[tag].value<QString>()) ) {
306 // there is a theme in the settings, so pick this
307 ThemeInfo res = ThemeInfo::fromDesktopFile(var_settings[tag].value<QString>());
308 if(theme_ok_for_variant(res, variant_proxy_name))
309 return res;
312 MasterSettings cached_theme_info("tagua_config_cache.xml");
313 KStandardDirs* std_dirs = KGlobal::dirs();
314 ThemeInfoList themes = to_theme_info_list(std_dirs->findAllResources("appdata",
315 "themes/" + category + "/*.desktop",
316 KStandardDirs::Recursive ),
317 cached_theme_info.group(category));
319 int best = 0;
320 ThemeInfo* retv = 0;
321 for(int i = 0; i < themes.size(); i++) {
322 int ok = theme_ok_for_variant(themes[i], variant_proxy_name);
323 if (!ok)
324 continue;
326 if (ok > best) {
327 retv = &themes[i];
328 best = ok;
333 if (retv && *retv) {
334 var_settings[tag] = retv->desktopFile;
337 return retv ? *retv : ThemeInfo();
340 PrefThemeCategory::PrefThemeCategory(QWidget* parent, PrefTheme* owner)
341 : QWidget(parent)
342 , m_parent(owner)
343 , m_opt_layout(NULL)
344 , m_opt_widget(NULL) {
345 setupUi(this);
346 m_reset = new QAction(KIcon("eraser"), "&Reset to default", this);
347 m_reset->setShortcut(Qt::CTRL+Qt::Key_Z);
348 connect(m_reset, SIGNAL(triggered()), this, SLOT(reset()));
349 m_resetButton->setDefaultAction(m_reset);
350 m_resetButton->setToolButtonStyle(Qt::ToolButtonIconOnly);
353 void PrefThemeCategory::reset() {
354 if(!m_opt_widget)
355 return;
357 QList<QListWidgetItem *> l = m_list->selectedItems();
358 if(l.isEmpty())
359 return;
361 int i = l[0]->data(Qt::UserRole).toInt();
362 if (i >= 0 && i < m_themes.size()) {
363 OptList ol = m_parent->get_file_options(m_themes[i].file_name, true);
364 qobject_cast<OptionWidget*>(m_opt_widget)->setValues(ol);
368 void PrefThemeCategory::themeChanged() {
369 QList<QListWidgetItem *> l = m_list->selectedItems();
370 if(!l.isEmpty()) {
371 int i = l[0]->data(Qt::UserRole).toInt();
372 if(i>=0 && i<m_themes.size()) {
373 m_label->setText(m_themes[i].description);
375 QString c = m_parent->comboVariant->itemData(m_parent->comboVariant->currentIndex()).toString();
376 VariantPtr vi(Variants::instance().get(c));
377 if(vi)
378 m_new_themes[vi->name()] = m_themes[i].desktopFile;
380 if(m_opt_widget) {
381 delete m_opt_widget;
382 m_opt_widget = NULL;
384 OptList ol = m_parent->get_file_options(m_themes[i].file_name);
385 if (ol.size() != 0 && m_list->isEnabled()) {
386 m_opt_widget = new OptionWidget(ol, m_widget);
387 m_opt_layout->addWidget(m_opt_widget);
388 m_reset->setEnabled(true);
390 else
391 m_reset->setEnabled(false);
392 return;
395 m_label->setText(QString());
398 void PrefThemeCategory::themeChecked(bool ck) {
399 m_list->setEnabled(!ck);
400 m_label->setEnabled(!ck);
402 QString c = m_parent->comboVariant->itemData(m_parent->comboVariant->currentIndex()).toString();
403 VariantPtr vi(Variants::instance().get(c));
404 if (vi)
405 m_new_use_def[vi->name()] = ck;
406 themeChanged();