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>
20 #include <QStyleFactory>
26 #include "themes_common.h"
29 #include "qtg_cxxside.h"
34 static QString stylestring
;
35 static QString real_data_dir
;
36 extern QApplication
*qapp
;
37 extern QString current_theme
;
38 extern QApplication
*current_app();
39 static QString def_app_style
;
41 /*****************************************************************************
42 Loads a qt theme directory/theme_name
43 *****************************************************************************/
44 void qtg_gui_load_theme(const char *directory
, const char *theme_name
)
52 QString lnb
= "LittleFinger";
55 if (def_app_style
.isEmpty()) {
56 def_app_style
= QApplication::style()->objectName();
59 if (real_data_dir
.isEmpty()) {
60 real_data_dir
= QString(directory
);
63 path
= real_data_dir
+ DIR_SEPARATOR
+ theme_name
+ DIR_SEPARATOR
;
64 name
= path
+ "resource.qss";
67 if (!f
.open(QIODevice::ReadOnly
| QIODevice::Text
)) {
68 if (QString(theme_name
) != QString("NightStalker")) {
69 qtg_gui_clear_theme();
73 /* Stylesheet uses UNIX separators */
74 fake_dir
= real_data_dir
;
75 fake_dir
.replace(QString(DIR_SEPARATOR
), "/");
77 stylestring
= in
.readAll();
78 stylestring
.replace(lnb
, fake_dir
+ "/" + theme_name
+ "/");
80 if (QString(theme_name
) == QString("System")) {
81 QApplication::setStyle(QStyleFactory::create(def_app_style
));
83 QApplication::setStyle(QStyleFactory::create("Fusion"));
86 current_theme
= theme_name
;
87 current_app()->setStyleSheet(stylestring
);
89 gui()->reload_sidebar_icons();
91 pal
.setBrush(QPalette::Link
, QColor(92,170,229));
92 pal
.setBrush(QPalette::LinkVisited
, QColor(54,150,229));
93 QApplication::setPalette(pal
);
96 /*****************************************************************************
97 Clears a theme (sets default system theme)
98 *****************************************************************************/
99 void qtg_gui_clear_theme()
103 str
= QString("themes") + DIR_SEPARATOR
+ "gui-qt" + DIR_SEPARATOR
;
104 name
= fileinfoname(get_data_dirs(), str
.toLocal8Bit().data());
105 qtg_gui_load_theme(name
.toLocal8Bit().data(),
109 /*****************************************************************************
110 Each gui has its own themes directories.
112 Returns an array containing these strings and sets array size in count.
113 The caller is responsible for freeing the array and the paths.
114 *****************************************************************************/
115 char **qtg_get_gui_specific_themes_directories(int *count
)
118 char *persistent
= static_cast<char*>(fc_malloc(256));
121 array
= new char *[*count
];
122 strncpy(persistent
, fileinfoname(get_data_dirs(),""), 256);
123 array
[0] = persistent
;
127 /*****************************************************************************
128 Return an array of names of usable themes in the given directory.
129 Array size is stored in count.
130 The caller is responsible for freeing the array and the names
131 *****************************************************************************/
132 char **qtg_get_useable_themes_in_directory(const char *directory
, int *count
)
134 QStringList sl
, theme_list
;
143 str
= QString("themes") + DIR_SEPARATOR
+ "gui-qt" + DIR_SEPARATOR
;
144 name
= fileinfoname(get_data_dirs(), str
.toLocal8Bit().data());
147 sl
<< dir
.entryList(QDir::AllDirs
| QDir::NoDotAndDotDot
);
151 f
.setFileName(name
+ str
+ DIR_SEPARATOR
+ "resource.qss");
152 if (f
.exists() == false) {
158 array
= new char *[theme_list
.count()];
159 *count
= theme_list
.count();
161 for (int i
= 0; i
< *count
; i
++) {
162 qba
= theme_list
[i
].toLocal8Bit();
163 data
= new char[theme_list
[i
].toLocal8Bit().count() + 1];
164 strcpy(data
, theme_list
[i
].toLocal8Bit().data());