summaryrefslogtreecommitdiff
path: root/Bootstrap.cxx
blob: 99a8639a59f3453aaed34bd453a2918764c9209a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "tomlc99/toml.h"
#include "Bootstrap.hxx"

#define OVERRIDE_COLORS(A, B) \
	colors = toml_array_in(bootstrap, A);\
	if (colors) for (int i = 0; ; i++) {\
		auto color = toml_string_at(colors, i);\
		if (!color.ok) break;\
		B[i] = QColor{color.u.s};\
		free(color.u.s);\
	}

Bootstrap::Bootstrap(QObject *parent):
	QObject{parent},
	bsMode{Mode::Light},
	bsTheme{Theme::None},
#ifdef __ANDROID__
	bsFontSansSerif{"Roboto"},
#elif defined(__APPLE__)
	bsFontSansSerif{"Helvetica Neue"},
#elif defined(__EMSCRIPTEN__)
	bsFontSansSerif{"Arial"},
#elif defined(_WIN32) || defined(__WIN32__) || defined(WIN32) \
		|| defined(_WIN64) || defined(__WIN64)
	bsFontSansSerif{"Segoe UI"},
#else
	bsFontSansSerif{"Liberation Sans"},
#endif
	bsBodyFont{bsFontSansSerif},
	bodyColors{{"#212529", "#adb5bd"}},
	bodyBgs{{"#fff", "#212529"}},
	borderColors{{"#dee2e6", "#495057"}}
{
	bsBodyFont.setStyleHint(QFont::SansSerif);
	bsBodyFont.setPointSizeF(16.0);
	bsBodyFont.setWeight(QFont::Normal);

	QFile conf{QStringLiteral(":/qtquickcontrols2.conf")};
	if (!conf.open(QIODevice::ReadOnly | QIODevice::Text)) return;
	auto toml = toml_parse(conf.readAll().data(), nullptr, 0);
	conf.close();

	auto bootstrap = toml_table_in(toml, "Bootstrap");
	if (!bootstrap) {
		toml_free(toml);
		return;
	}

	auto mode = toml_int_in(bootstrap, "Mode");
	if (mode.ok) bsMode = static_cast<Mode>(mode.u.i);

	auto fontFamily = toml_string_in(bootstrap, "BodyFontFamily");
	if (fontFamily.ok) {
		bsBodyFont.setFamily(fontFamily.u.s);
		free(fontFamily.u.s);
		bsBodyFont.setStyleHint(QFont::AnyStyle);
	}
	auto fontSize = toml_double_in(bootstrap, "BodyFontSize");
	if (fontSize.ok) bsBodyFont.setPointSizeF(fontSize.u.d);
	auto fontWeight = toml_int_in(bootstrap, "BodyFontWeight");
	if (fontWeight.ok) bsBodyFont.setWeight(fontWeight.u.i);

	toml_array_t *colors;
	OVERRIDE_COLORS("BodyColors", bodyColors);
	OVERRIDE_COLORS("BodyBgs", bodyBgs);
	OVERRIDE_COLORS("BorderColors", borderColors);

	toml_free(toml);
}

Bootstrap *Bootstrap::qmlAttachedProperties(QObject *object)
{
	return new Bootstrap(object);
}

Bootstrap::Mode Bootstrap::mode() const
{
	return bsMode;
}

void Bootstrap::setMode(Mode mode)
{
	if (mode == bsMode) return;
	bsMode = mode;
	emit modeChanged();
	emit bodyColorChanged();
	emit bodyBgChanged();
	emit borderColorChanged();
}

Bootstrap::Theme Bootstrap::theme() const
{
	return bsTheme;
}

void Bootstrap::setTheme(Theme theme)
{
	if (theme == bsTheme) return;
	bsTheme = theme;
	emit themeChanged();
}

QString Bootstrap::fontSansSerif() const
{
	return bsFontSansSerif;
}

QFont Bootstrap::bodyFont() const
{
	return bsBodyFont;
}

QColor Bootstrap::bodyColor() const
{
	return bodyColors.at(static_cast<int>(bsMode));
}

QColor Bootstrap::bodyBg() const
{
	return bodyBgs.at(static_cast<int>(bsMode));
}

QColor Bootstrap::borderColor() const
{
	return borderColors.at(static_cast<int>(bsMode));
}