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 <QListWidgetItem>
14 #include <QStringList>
17 #include <KStandardDirs>
19 #include <core/variant.h>
22 #include "mastersettings.h"
23 #include "luaapi/loader.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
;
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()
48 && it
->second
.last_modified
== file_info
.lastModified() ) {
49 all_themes
<< it
->second
;
50 cached_info
.erase(it
);
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())
66 /* rewrite the cached configuration */
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
);
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())
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();
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
;
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
))
121 if(theme_info
.variants
.contains(variant_name
, Qt::CaseInsensitive
))
123 if(theme_info
.variants
.contains("any[default]", Qt::CaseInsensitive
))
125 if(theme_info
.variants
.contains("any", Qt::CaseInsensitive
))
131 PrefTheme::PrefTheme(const QString
& currentVariant
, QWidget
*parent
)
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();
174 foreach (QString variant
, all
) {
175 if (variant
== currentVariant
) {
178 comboVariant
->addItem(variant
, QVariant(variant
));
183 comboVariant
->setCurrentIndex(current
);
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
) {
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
);
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
;
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
));
249 for (CategoryMap::iterator cit
= m_categories
.begin(); cit
!= m_categories
.end(); ++cit
) {
250 Category
* c
= cit
->second
;
254 c
->m_list
->setEnabled(false);
255 c
->m_label
->setText(QString());
256 c
->m_label
->setEnabled(false);
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
);
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
);
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
))
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
));
321 for(int i
= 0; i
< themes
.size(); i
++) {
322 int ok
= theme_ok_for_variant(themes
[i
], variant_proxy_name
);
334 var_settings
[tag
] = retv
->desktopFile
;
337 return retv
? *retv
: ThemeInfo();
340 PrefThemeCategory::PrefThemeCategory(QWidget
* parent
, PrefTheme
* owner
)
344 , m_opt_widget(NULL
) {
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() {
357 QList
<QListWidgetItem
*> l
= m_list
->selectedItems();
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();
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
));
378 m_new_themes
[vi
->name()] = m_themes
[i
].desktopFile
;
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);
391 m_reset
->setEnabled(false);
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
));
405 m_new_use_def
[vi
->name()] = ck
;