Added translator comment about government overthrown message.
[freeciv.git] / client / gui-qt / themes.cpp
blob2770b7fb8f611e88a1b56bb2078946a9efc5be90
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 <QStyleFactory>
22 /* utility */
23 #include "mem.h"
25 /* client */
26 #include "themes_common.h"
28 // gui-qt
29 #include "qtg_cxxside.h"
31 /* client/include */
32 #include "themes_g.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)
46 QString name;
47 QString res_name;
48 QString path;
49 QString fake_dir;
50 QDir dir;
51 QFile f;
52 QString lnb = "LittleFinger";
53 QPalette pal;
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";
65 f.setFileName(name);
67 if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
68 if (QString(theme_name) != QString("NightStalker")) {
69 qtg_gui_clear_theme();
71 return;
73 /* Stylesheet uses UNIX separators */
74 fake_dir = real_data_dir;
75 fake_dir.replace(QString(DIR_SEPARATOR), "/");
76 QTextStream in(&f);
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));
82 } else {
83 QApplication::setStyle(QStyleFactory::create("Fusion"));
86 current_theme = theme_name;
87 current_app()->setStyleSheet(stylestring);
88 if (gui()) {
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()
101 QString name, str;
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(),
106 "NightStalker");
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)
117 char **array;
118 char *persistent = static_cast<char*>(fc_malloc(256));
120 *count = 1;
121 array = new char *[*count];
122 strncpy(persistent, fileinfoname(get_data_dirs(),""), 256);
123 array[0] = persistent;
124 return array;
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;
135 char **array;
136 char *data;
137 QByteArray qba;;
138 QString str;
139 QString name;
140 QDir dir;
141 QFile f;
143 str = QString("themes") + DIR_SEPARATOR + "gui-qt" + DIR_SEPARATOR;
144 name = fileinfoname(get_data_dirs(), str.toLocal8Bit().data());
146 dir.setPath(name);
147 sl << dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
148 name = name;
150 foreach(str, sl) {
151 f.setFileName(name + str + DIR_SEPARATOR + "resource.qss");
152 if (f.exists() == false) {
153 continue;
155 theme_list << str;
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());
165 array[i] = data;
168 return array;