Freeciv-3.4
Loading...
Searching...
No Matches
main
client
gui-qt
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
37
extern
QString
current_theme
;
38
static
QString
def_app_style
;
39
static
QString
stylestring
;
40
41
static
QStyle
*
current_style
=
nullptr
;
42
43
/*************************************************************************/
46
void
qtg_gui_load_theme
(
const
char
*
directory
,
const
char
*
theme_name
)
47
{
48
QString
name
;
49
QString
path;
50
QString
fake_dir
;
51
QString
data_dir
;
52
QDir
dir;
53
QFile
f
;
54
QString
lnb
=
"LittleFinger"
;
55
QPalette
pal
;
56
57
if
(
def_app_style
.isEmpty()) {
58
def_app_style
= QApplication::style()->objectName();
59
}
60
61
data_dir
=
QString
(
directory
);
62
63
path =
data_dir
+
DIR_SEPARATOR
+
theme_name
+
DIR_SEPARATOR
;
64
name
= path +
"resource.qss"
;
65
f
.setFileName(
name
);
66
67
if
(!
f
.open(QIODevice::ReadOnly | QIODevice::Text)) {
68
if
(
QString
(
theme_name
) !=
QString
(
FC_QT_DEFAULT_THEME_NAME
)) {
69
qtg_gui_clear_theme
();
70
}
71
return
;
72
}
73
// Stylesheet uses UNIX separators
74
fake_dir
=
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
+
"/"
);
79
80
if
(
QString
(
theme_name
) ==
QString
(
"System"
)) {
81
current_style
= QStyleFactory::create(
def_app_style
);
82
}
else
{
83
QStyle
*
fstyle
= QStyleFactory::create(
"Fusion"
);
84
85
if
(
fstyle
!=
nullptr
) {
86
current_style
=
fstyle
;
87
}
else
{
88
current_style
= QStyleFactory::create(
def_app_style
);
89
}
90
}
91
QApplication::setStyle(
current_style
);
92
93
current_theme
=
theme_name
;
94
QPixmapCache::clear();
95
current_app
()->setStyleSheet(
stylestring
);
96
if
(
gui
()) {
97
gui
()->reload_sidebar_icons();
98
}
99
pal
.setBrush(QPalette::Link,
QColor
(92,170,229));
100
pal
.setBrush(QPalette::LinkVisited,
QColor
(54,150,229));
101
QApplication::setPalette(
pal
);
102
}
103
104
/*************************************************************************/
107
void
set_theme_style
()
108
{
109
QApplication::setStyle(
current_style
);
110
}
111
112
/*************************************************************************/
115
void
qtg_gui_clear_theme
()
116
{
117
if
(!
load_theme
(
FC_QT_DEFAULT_THEME_NAME
)) {
118
// TRANS: No full stop after the URL, could cause confusion.
119
log_fatal
(
_
(
"No Qt-client theme was found. For instructions on how to "
120
"get one, please visit %s"
),
HOMEPAGE_URL
);
121
exit
(
EXIT_FAILURE
);
122
}
123
}
124
125
/*************************************************************************/
131
char
**
qtg_get_gui_specific_themes_directories
(
int
*count)
132
{
133
const
struct
strvec
*
data_dirs
=
get_data_dirs
();
134
char
**
directories
= (
char
**)
fc_malloc
(
strvec_size
(
data_dirs
)
135
*
sizeof
(
char
*));
136
int
i
= 0;
137
138
*count =
strvec_size
(
data_dirs
);
139
strvec_iterate
(
data_dirs
,
data_dir
) {
140
int
len
=
strlen
(
data_dir
) +
strlen
(
"/themes/gui-qt"
) + 1;
141
char
*
buf
= (
char
*)
fc_malloc
(
len
);
142
143
fc_snprintf
(
buf
,
len
,
"%s/themes/gui-qt"
,
data_dir
);
144
145
directories
[
i
++] =
buf
;
146
}
strvec_iterate_end
;
147
148
return
directories
;
149
}
150
151
/*************************************************************************/
156
char
**
qtg_get_usable_themes_in_directory
(
const
char
*
directory
,
int
*count)
157
{
158
QStringList
sl
,
theme_list
;
159
char
**array;
160
char
*data;
161
QByteArray
qba
;
162
QString
str
;
163
QString
name
;
164
QString
qtheme_name
;
165
QDir
dir;
166
QFile
f
;
167
168
dir.setPath(
directory
);
169
sl
<< dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot);
170
name
=
QString
(
directory
);
171
172
foreach
(
str
,
sl
) {
173
f
.setFileName(
name
+
DIR_SEPARATOR
+
str
174
+
DIR_SEPARATOR
+
"resource.qss"
);
175
if
(!
f
.exists()) {
176
continue
;
177
}
178
theme_list
<<
str
;
179
}
180
181
qtheme_name
=
gui_options
.
gui_qt_default_theme_name
;
182
// Move current theme on first position
183
if
(
theme_list
.contains(
qtheme_name
)) {
184
theme_list
.removeAll(
qtheme_name
);
185
theme_list
.prepend(
qtheme_name
);
186
}
187
array =
new
char
*[
theme_list
.count()];
188
*count =
theme_list
.count();
189
190
for
(
int
i
= 0;
i
< *count;
i
++) {
191
QByteArray
tn_bytes
;
192
193
qba
=
theme_list
[
i
].toUtf8();
194
data =
new
char
[
theme_list
[
i
].toUtf8().length() + 1];
195
tn_bytes
=
theme_list
[
i
].toUtf8();
196
strcpy
(data,
tn_bytes
.data());
197
array[
i
] = data;
198
}
199
200
return
array;
201
}
str
#define str
Definition
astring.c:76
incite_cost
char * incite_cost
Definition
comments.c:77
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:227
name
const char * name
Definition
inputfile.c:127
log_fatal
#define log_fatal(message,...)
Definition
log.h:101
mem.h
fc_malloc
#define fc_malloc(sz)
Definition
mem.h:34
gui
static mpgui * gui
Definition
mpgui_qt.cpp:53
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:453
len
int len
Definition
packhand.c:128
get_data_dirs
const struct strvec * get_data_dirs(void)
Definition
shared.c:886
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:79
strvec_iterate_end
#define strvec_iterate_end
Definition
string_vector.h:86
client_options::gui_qt_default_theme_name
char gui_qt_default_theme_name[512]
Definition
options.h:458
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:156
current_theme
QString current_theme
Definition
fc_client.cpp:59
qtg_gui_clear_theme
void qtg_gui_clear_theme()
Definition
themes.cpp:115
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:46
set_theme_style
void set_theme_style()
Definition
themes.cpp:107
qtg_get_gui_specific_themes_directories
char ** qtg_get_gui_specific_themes_directories(int *count)
Definition
themes.cpp:131
current_style
static QStyle * current_style
Definition
themes.cpp:41
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
Generated on Sun Dec 7 2025 22:31:28 for Freeciv-3.4 by
1.9.8