1 /**********************************************************************
2 Freeciv - Copyright (C) 2005 The Freeciv Team
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
14 #include <fc_config.h>
18 #include <QApplication>
21 #include <QStyleFactory>
22 #include <QTextStream>
28 #include "themes_common.h"
34 #include "fc_client.h"
36 extern QApplication
*current_app();
37 extern QApplication
*qapp
;
38 extern QString current_theme
;
39 static QString def_app_style
;
40 static QString real_data_dir
;
41 static QString stylestring
;
43 /*****************************************************************************
44 Loads a qt theme directory/theme_name
45 *****************************************************************************/
46 void qtg_gui_load_theme(const char *directory
, const char *theme_name
)
53 QString lnb
= "LittleFinger";
56 if (def_app_style
.isEmpty()) {
57 def_app_style
= QApplication::style()->objectName();
60 if (real_data_dir
.isEmpty()) {
61 real_data_dir
= QString(directory
);
64 path
= real_data_dir
+ DIR_SEPARATOR
+ theme_name
+ DIR_SEPARATOR
;
65 name
= dir
.absolutePath() + QDir::separator() + real_data_dir
;
66 name
= path
+ "resource.qss";
69 if (!f
.open(QIODevice::ReadOnly
| QIODevice::Text
)) {
70 if (QString(theme_name
) != QString(FC_QT_DEFAULT_THEME_NAME
)) {
71 qtg_gui_clear_theme();
75 /* Stylesheet uses UNIX separators */
76 fake_dir
= real_data_dir
;
77 fake_dir
.replace(QString(DIR_SEPARATOR
), "/");
79 stylestring
= in
.readAll();
80 stylestring
.replace(lnb
, fake_dir
+ "/" + theme_name
+ "/");
82 if (QString(theme_name
) == QString("System")) {
83 QApplication::setStyle(QStyleFactory::create(def_app_style
));
85 QApplication::setStyle(QStyleFactory::create("Fusion"));
88 current_theme
= theme_name
;
89 current_app()->setStyleSheet(stylestring
);
91 gui()->reload_sidebar_icons();
93 pal
.setBrush(QPalette::Link
, QColor(92,170,229));
94 pal
.setBrush(QPalette::LinkVisited
, QColor(54,150,229));
95 QApplication::setPalette(pal
);
98 /*****************************************************************************
99 Clears a theme (sets default system theme)
100 *****************************************************************************/
101 void qtg_gui_clear_theme()
105 str
= QString("themes") + DIR_SEPARATOR
+ "gui-qt" + DIR_SEPARATOR
;
106 name
= fileinfoname(get_data_dirs(), str
.toLocal8Bit().data());
107 qtg_gui_load_theme(name
.toLocal8Bit().data(), FC_QT_DEFAULT_THEME_NAME
);
110 /*****************************************************************************
111 Each gui has its own themes directories.
113 Returns an array containing these strings and sets array size in count.
114 The caller is responsible for freeing the array and the paths.
115 *****************************************************************************/
116 char **qtg_get_gui_specific_themes_directories(int *count
)
119 char *persistent
= static_cast<char*>(fc_malloc(256));
122 /* array is deleted in C client code and shouln't
123 be allocated with new[] */
124 array
= static_cast<char **>(fc_malloc((*count
) * sizeof(char *)));
125 strncpy(persistent
, fileinfoname(get_data_dirs(),""), 256);
126 array
[0] = persistent
;
130 /*****************************************************************************
131 Return an array of names of usable themes in the given directory.
132 Array size is stored in count.
133 The caller is responsible for freeing the array and the names
134 *****************************************************************************/
135 char **qtg_get_useable_themes_in_directory(const char *directory
, int *count
)
137 QStringList sl
, theme_list
;
147 str
= QString("themes") + DIR_SEPARATOR
+ "gui-qt" + DIR_SEPARATOR
;
148 name
= fileinfoname(get_data_dirs(), str
.toLocal8Bit().data());
151 sl
<< dir
.entryList(QDir::AllDirs
| QDir::NoDotAndDotDot
);
155 f
.setFileName(name
+ str
+ DIR_SEPARATOR
+ "resource.qss");
156 if (f
.exists() == false) {
162 qtheme_name
= gui_options
.gui_qt_default_theme_name
;
163 /* move current theme on first position */
164 if (theme_list
.contains(qtheme_name
)) {
165 theme_list
.removeAll(qtheme_name
);
166 theme_list
.prepend(qtheme_name
);
168 array
= new char *[theme_list
.count()];
169 *count
= theme_list
.count();
171 for (int i
= 0; i
< *count
; i
++) {
172 qba
= theme_list
[i
].toLocal8Bit();
173 data
= new char[theme_list
[i
].toLocal8Bit().count() + 1];
174 strcpy(data
, theme_list
[i
].toLocal8Bit().data());