Freeciv-3.3: /fast/freeciv/nightly/main/client/gui-qt/themes.cpp Source File

Freeciv-3.3

Loading...
Searching...
No Matches
themes.cpp
Go to the documentation of this file.
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.
7
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
16
17// Qt
18#include <QApplication>
19#include <QDir>
20#include <QPalette>
21#include <QStyleFactory>
22#include <QTextStream>
23
24// utility
25#include "mem.h"
26
27// client
28#include "themes_common.h"
29
30// client/include
31#include "themes_g.h"
32
33// gui-qt
34#include "fc_client.h"
35#include "gui_main.h"
36
37extern QString current_theme;
38 static QString def_app_style;
39 static QString stylestring;
40
41/*************************************************************************/
44 void qtg_gui_load_theme(const char *directory, const char *theme_name)
45{
46 QString name;
47 QString path;
48 QString fake_dir;
49 QString data_dir;
50 QDir dir;
51 QFile f;
52 QString lnb = "LittleFinger";
53 QPalette pal;
54
55 if (def_app_style.isEmpty()) {
56 def_app_style = QApplication::style()->objectName();
57 }
58
59 data_dir = QString(directory);
60
61 path = data_dir + DIR_SEPARATOR + theme_name + DIR_SEPARATOR;
62 name = path + "resource.qss";
63 f.setFileName(name);
64
65 if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
66 if (QString(theme_name) != QString(FC_QT_DEFAULT_THEME_NAME)) {
67 qtg_gui_clear_theme();
68 }
69 return;
70 }
71 // Stylesheet uses UNIX separators
72 fake_dir = data_dir;
73 fake_dir.replace(QString(DIR_SEPARATOR), "/");
74 QTextStream in(&f);
75 stylestring = in.readAll();
76 stylestring.replace(lnb, fake_dir + "/" + theme_name + "/");
77
78 if (QString(theme_name) == QString("System")) {
79 QApplication::setStyle(QStyleFactory::create(def_app_style));
80 } else {
81 QStyle *fstyle = QStyleFactory::create("Fusion");
82
83 if (fstyle != nullptr) {
84 QApplication::setStyle(fstyle);
85 } else {
86 QApplication::setStyle(QStyleFactory::create(def_app_style));
87 }
88 }
89
90 current_theme = theme_name;
91 QPixmapCache::clear();
92 current_app()->setStyleSheet(stylestring);
93 if (gui()) {
94 gui()->reload_sidebar_icons();
95 }
96 pal.setBrush(QPalette::Link, QColor(92,170,229));
97 pal.setBrush(QPalette::LinkVisited, QColor(54,150,229));
98 QApplication::setPalette(pal);
99}
100
101/*************************************************************************/
104 void qtg_gui_clear_theme()
105{
106 if (!load_theme(FC_QT_DEFAULT_THEME_NAME)) {
107 // TRANS: No full stop after the URL, could cause confusion.
108 log_fatal(_("No Qt-client theme was found. For instructions on how to "
109 "get one, please visit %s"), HOMEPAGE_URL);
110 exit(EXIT_FAILURE);
111 }
112}
113
114/*************************************************************************/
120 char **qtg_get_gui_specific_themes_directories(int *count)
121{
122 const struct strvec *data_dirs = get_data_dirs();
123 char **directories = (char **)fc_malloc(strvec_size(data_dirs)
124 * sizeof(char *));
125 int i = 0;
126
127 *count = strvec_size(data_dirs);
128 strvec_iterate(data_dirs, data_dir) {
129 int len = strlen(data_dir) + strlen("/themes/gui-qt") + 1;
130 char *buf = (char *)fc_malloc(len);
131
132 fc_snprintf(buf, len, "%s/themes/gui-qt", data_dir);
133
134 directories[i++] = buf;
135 } strvec_iterate_end;
136
137 return directories;
138}
139
140/*************************************************************************/
145 char **qtg_get_usable_themes_in_directory(const char *directory, int *count)
146{
147 QStringList sl, theme_list;
148 char **array;
149 char *data;
150 QByteArray qba;
151 QString str;
152 QString name;
153 QString qtheme_name;
154 QDir dir;
155 QFile f;
156
157 dir.setPath(directory);
158 sl << dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
159 name = QString(directory);
160
161 foreach(str, sl) {
162 f.setFileName(name + DIR_SEPARATOR + str
163 + DIR_SEPARATOR + "resource.qss");
164 if (!f.exists()) {
165 continue;
166 }
167 theme_list << str;
168 }
169
170 qtheme_name = gui_options.gui_qt_default_theme_name;
171 // Move current theme on first position
172 if (theme_list.contains(qtheme_name)) {
173 theme_list.removeAll(qtheme_name);
174 theme_list.prepend(qtheme_name);
175 }
176 array = new char *[theme_list.count()];
177 *count = theme_list.count();
178
179 for (int i = 0; i < *count; i++) {
180 QByteArray tn_bytes;
181
182 qba = theme_list[i].toUtf8();
183 data = new char[theme_list[i].toUtf8().length() + 1];
184 tn_bytes = theme_list[i].toUtf8();
185 strcpy(data, tn_bytes.data());
186 array[i] = data;
187 }
188
189 return array;
190}
str
#define str
Definition astring.c:76
incite_cost
char * incite_cost
Definition comments.c:74
fc_client.h
_
#define _(String)
Definition fcintl.h:67
load_theme
static bool load_theme
Definition theme_dlg.c:30
gui_main.h
current_app
QApplication * current_app()
Definition gui_main.cpp:226
name
const char * name
Definition inputfile.c:127
log_fatal
#define log_fatal(message,...)
Definition log.h:100
mem.h
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
gui
static mpgui * gui
Definition mpgui_qt.cpp:52
gui_options
struct client_options gui_options
Definition options.c:71
FC_QT_DEFAULT_THEME_NAME
#define FC_QT_DEFAULT_THEME_NAME
Definition options.h:442
len
int len
Definition packhand.c:127
get_data_dirs
const struct strvec * get_data_dirs(void)
Definition shared.c:893
DIR_SEPARATOR
#define DIR_SEPARATOR
Definition shared.h:127
strvec_size
size_t strvec_size(const struct strvec *psv)
Definition string_vector.c:344
strvec_iterate
#define strvec_iterate(psv, str)
Definition string_vector.h:61
strvec_iterate_end
#define strvec_iterate_end
Definition string_vector.h:68
client_options::gui_qt_default_theme_name
char gui_qt_default_theme_name[512]
Definition options.h:447
strvec
Definition string_vector.c:29
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
def_app_style
static QString def_app_style
Definition themes.cpp:38
qtg_get_usable_themes_in_directory
char ** qtg_get_usable_themes_in_directory(const char *directory, int *count)
Definition themes.cpp:145
current_theme
QString current_theme
Definition fc_client.cpp:64
qtg_gui_clear_theme
void qtg_gui_clear_theme()
Definition themes.cpp:104
stylestring
static QString stylestring
Definition themes.cpp:39
qtg_gui_load_theme
void qtg_gui_load_theme(const char *directory, const char *theme_name)
Definition themes.cpp:44
qtg_get_gui_specific_themes_directories
char ** qtg_get_gui_specific_themes_directories(int *count)
Definition themes.cpp:120
directories
struct theme_directory * directories
Definition themes_common.c:60
themes_common.h
themes_g.h
directory
const char * directory
Definition themes_g.h:23