webperimental: killstack decides stack protects.
[freeciv.git] / client / gui-qt / themes.cpp
blobb731ccd718b503e6d799bb25ab6d1ec5f6e555ea
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)
6 any later version.
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 ***********************************************************************/
13 #ifdef HAVE_CONFIG_H
14 #include <fc_config.h>
15 #endif
17 // Qt
18 #include <QApplication>
19 #include <QDir>
20 #include <QPalette>
21 #include <QStyleFactory>
22 #include <QTextStream>
24 /* utility */
25 #include "mem.h"
27 /* client */
28 #include "themes_common.h"
30 /* client/include */
31 #include "themes_g.h"
33 // gui-qt
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)
48 QString name;
49 QString path;
50 QString fake_dir;
51 QDir dir;
52 QFile f;
53 QString lnb = "LittleFinger";
54 QPalette pal;
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";
67 f.setFileName(name);
69 if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
70 if (QString(theme_name) != QString(FC_QT_DEFAULT_THEME_NAME)) {
71 qtg_gui_clear_theme();
73 return;
75 /* Stylesheet uses UNIX separators */
76 fake_dir = real_data_dir;
77 fake_dir.replace(QString(DIR_SEPARATOR), "/");
78 QTextStream in(&f);
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));
84 } else {
85 QApplication::setStyle(QStyleFactory::create("Fusion"));
88 current_theme = theme_name;
89 current_app()->setStyleSheet(stylestring);
90 if (gui()) {
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()
103 QString name, str;
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)
118 char **array;
119 char *persistent = static_cast<char*>(fc_malloc(256));
121 *count = 1;
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;
127 return array;
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;
138 char **array;
139 char *data;
140 QByteArray qba;;
141 QString str;
142 QString name;
143 QString qtheme_name;
144 QDir dir;
145 QFile f;
147 str = QString("themes") + DIR_SEPARATOR + "gui-qt" + DIR_SEPARATOR;
148 name = fileinfoname(get_data_dirs(), str.toLocal8Bit().data());
150 dir.setPath(name);
151 sl << dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
152 name = name;
154 foreach(str, sl) {
155 f.setFileName(name + str + DIR_SEPARATOR + "resource.qss");
156 if (f.exists() == false) {
157 continue;
159 theme_list << str;
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());
175 array[i] = data;
178 return array;