MauiKit Controls

style.cpp
1#include "style.h"
2#include <QCoreApplication>
3#include <QGuiApplication>
4#include <QIcon>
5#include <QStyle>
6#include <QApplication>
7#include <QFontDatabase>
8#include <QStyleHints>
9
10#include <MauiMan4/thememanager.h>
11#include <MauiMan4/backgroundmanager.h>
12#include <MauiMan4/accessibilitymanager.h>
13
14#ifdef Q_OS_ANDROID
15#include "mauiandroid.h"
16#endif
17
18Q_GLOBAL_STATIC(Style, styleInstance)
19
20void Style::styleChanged()
21{
22 // It should be safe to use qApp->style() unguarded here, because the signal
23 // will only have been connected if qApp is a QApplication.
25 auto *style = qApp->style();
26 if (!style || QCoreApplication::closingDown()) {
27 return;
28 }
29
30 Q_ASSERT(style != sender());
31
32 connect(style, &QObject::destroyed, this, &Style::styleChanged);
33
34 m_currentIconTheme = QIcon::themeName();
35 Q_EMIT currentIconThemeChanged(m_currentIconTheme);
36
38 Q_EMIT monospacedFontChanged();
39}
40
41
42Style::Style(QObject *parent) : QObject(parent)
43 ,m_iconSizes (new GroupSizes(8,16, 22, 32, 48, 64, 128, this))
44 ,m_space( new GroupSizes(4, 6, 8, 16, 24, 32, 40, this))
45 ,m_fontSizes(new GroupSizes(this))
46 ,m_units(new Units(this))
47 ,m_accentColor(QColor("#26c6da"))
48 ,m_themeSettings( new MauiMan::ThemeManager(this))
49 ,m_backgroundSettings( new MauiMan::BackgroundManager(this))
50 ,m_accessibilitySettings( new MauiMan::AccessibilityManager(this))
51{
52 connect(qGuiApp, &QGuiApplication::fontChanged, [this](const QFont &font)
53 {
54 m_defaultFont = font;
55 setFontSizes();
56 Q_EMIT defaultFontChanged();
57 // Q_EMIT m_fontSizes->sizesChanged();
58 Q_EMIT fontSizesChanged();
59 Q_EMIT h1FontChanged();
60 Q_EMIT h2FontChanged();
61 Q_EMIT
62 });
63
65 {
66 qDebug() << "Color schem style type changed<<"<< type;
67 if(m_styleType_blocked)
68 return;
69
70 switch(type)
71 {
72 case Qt::ColorScheme::Unknown:
73 m_styleType = static_cast<Style::StyleType>(m_themeSettings->styleType());
74 break;
75 case Qt::ColorScheme::Light:
76 m_styleType = Style::StyleType::Light;
77 break;
78 case Qt::ColorScheme::Dark:
79 m_styleType = Style::StyleType::Dark;
80 break;
81 }
82
83 Q_EMIT styleTypeChanged(m_styleType);
84 });
85
86 connect(m_themeSettings, &MauiMan::ThemeManager::styleTypeChanged, [this](int type)
87 {
88 if(m_styleType_blocked)
89 return;
90
91 m_styleType = static_cast<Style::StyleType>(type);
92 Q_EMIT styleTypeChanged(m_styleType);
93 });
94
95 connect(m_themeSettings, &MauiMan::ThemeManager::accentColorChanged, [this](QString color)
96 {
97 m_accentColor = color;
98 Q_EMIT this->accentColorChanged(m_accentColor);
99 });
100
101 connect(m_themeSettings, &MauiMan::ThemeManager::borderRadiusChanged, [this](uint radius)
102 {
103 m_radiusV = radius;
104 Q_EMIT this->radiusVChanged(m_radiusV);
105 });
106
107 connect(m_themeSettings, &MauiMan::ThemeManager::iconSizeChanged, [this](uint size)
108 {
109 m_iconSize = size;
110 Q_EMIT this->iconSizeChanged(m_iconSize);
111 });
112
113 connect(m_themeSettings, &MauiMan::ThemeManager::paddingSizeChanged, [this](uint size)
114 {
115 m_defaultPadding = size;
116 Q_EMIT this->defaultPaddingChanged();
117 });
118
119 connect(m_themeSettings, &MauiMan::ThemeManager::marginSizeChanged, [this](uint size)
120 {
121 qDebug() << "ContentMARGINS CHANGED" << size;
122 m_contentMargins = size;
123 Q_EMIT this->contentMarginsChanged();
124 });
125
126 connect(m_themeSettings, &MauiMan::ThemeManager::spacingSizeChanged, [this](uint size)
127 {
128 m_defaultSpacing = size;
129 Q_EMIT this->defaultSpacingChanged();
130 });
131
132 connect(m_themeSettings, &MauiMan::ThemeManager::enableEffectsChanged, [this](bool value)
133 {
134 m_enableEffects = value;
135 Q_EMIT this->enableEffectsChanged(m_enableEffects);
136 });
137
138 connect(m_backgroundSettings, &MauiMan::BackgroundManager::wallpaperSourceChanged, [this](QString source)
139 {
140 m_adaptiveColorSchemeSource = QUrl::fromUserInput(source).toLocalFile();
141 Q_EMIT this->adaptiveColorSchemeSourceChanged(m_adaptiveColorSchemeSource);
142 });
143
144 connect(m_themeSettings, &MauiMan::ThemeManager::enableEffectsChanged, [this](bool value)
145 {
146 m_enableEffects = value;
147 Q_EMIT this->enableEffectsChanged(m_enableEffects);
148 });
149
150 connect(m_accessibilitySettings, &MauiMan::AccessibilityManager::scrollBarPolicyChanged, [this](uint state)
151 {
152 qDebug() << "SCROLBAR POLICY CHANGED" << state;
153 Q_EMIT scrollBarPolicyChanged(state);
154 });
155
157 {
158 connect(m_themeSettings, &MauiMan::ThemeManager::iconThemeChanged, [this](QString name)
159 {
160 qDebug() << "Ask to change the icon theme";
161 m_currentIconTheme = name;
162 Q_EMIT currentIconThemeChanged(m_currentIconTheme);
163 });
164 }else
165 {
166 // //to be able to check and icon theme change rely on the style being reset, this not even works on Plasma, so do we need it?
167 // QStyle *style = qApp->style();
168 // if (style)
169 // {
170 // connect(style, &QObject::destroyed, this, &Style::styleChanged);
171 // }
172 }
173
174 m_defaultFont = qGuiApp->font();
176 setFontSizes();
177
178 m_radiusV = m_themeSettings->borderRadius();
179 m_iconSize = m_themeSettings->iconSize();
180 m_accentColor = m_themeSettings->accentColor();
181
182 m_contentMargins = m_themeSettings->marginSize();
183 m_defaultPadding = m_themeSettings->paddingSize();
184 m_defaultSpacing = m_themeSettings->spacingSize();
185
186 m_currentIconTheme = QIcon::themeName();
187
188 //TODO Use new Qt6 StyelHint properties for this
189
190 //For Maui Session we want to use MauiMan
192 {
193 switch(QGuiApplication::styleHints()->colorScheme())
194 {
195 case Qt::ColorScheme::Unknown:
196 m_styleType = static_cast<Style::StyleType>(m_themeSettings->styleType());
197 break;
198 case Qt::ColorScheme::Light:
199 m_styleType = Style::StyleType::Light;
200 break;
201 case Qt::ColorScheme::Dark:
202 m_styleType = Style::StyleType::Dark;
203 break;
204 }
205 }
206 else
207 {
208 m_styleType = static_cast<Style::StyleType>(m_themeSettings->styleType());
209 }
210
211 m_adaptiveColorSchemeSource = QUrl::fromUserInput(m_backgroundSettings->wallpaperSource()).toLocalFile();
212 m_enableEffects = m_themeSettings->enableEffects();
213}
214
215void Style::setFontSizes()
216{
217 qDebug() << m_defaultFont << m_defaultFont.pointSize();
218
219 m_defaultFontSize = m_defaultFont.pointSize ();
220
221 m_fontSizes->m_tiny = m_defaultFont.pointSize ()-2;
222 m_fontSizes->m_small = m_defaultFont.pointSize ()-1;
223 m_fontSizes->m_medium = m_defaultFont.pointSize ();
224 m_fontSizes->m_big = m_defaultFont.pointSize ()+1;
225 m_fontSizes->m_large = m_defaultFont.pointSize ()+2;
226 m_fontSizes->m_huge = m_defaultFont.pointSize ()+3;
227 m_fontSizes->m_enormous = m_defaultFont.pointSize ()+4;
228
229 m_h1Font.setPointSize(m_fontSizes->m_enormous);
230 m_h1Font.setWeight(QFont::Black);
231 m_h1Font.setBold(true);
232
233 m_h2Font.setPointSize(m_fontSizes->m_big);
234 m_h2Font.setWeight(QFont::DemiBold);
235 // m_h2Font.setBold(false);
236}
237
238void Style::setRadiusV(const uint& radius)
239{
240 if(m_radiusV == radius)
241 {
242 return;
243 }
244
245 m_radiusV = radius;
246 Q_EMIT radiusVChanged(m_radiusV);
247}
248
249bool Style::enableEffects() const
250{
251 return m_enableEffects;
252}
253
255{
256 return m_translucencyAvailable;
257}
258
259void Style::setTranslucencyAvailable(const bool &value)
260{
261 if(value == m_translucencyAvailable)
262 {
263 return;
264 }
265
266 m_translucencyAvailable = value;
267 Q_EMIT this->translucencyAvailableChanged(m_translucencyAvailable);
268}
269
270
271Style *Style::qmlAttachedProperties(QObject *object)
272{
273 Q_UNUSED(object)
274 return Style::instance();
275}
276
277Style *Style::instance()
278{
279 return styleInstance();
280}
281
282int getClosest(int, int, int);
283
284// Returns element closest to target in arr[]
285int findClosest(int arr[], int n, int target)
286{
287 // Corner cases
288 if (target <= arr[0])
289 return arr[0];
290 if (target >= arr[n - 1])
291 return arr[n - 1];
292
293 // Doing binary search
294 int i = 0, j = n, mid = 0;
295 while (i < j) {
296 mid = (i + j) / 2;
297
298 if (arr[mid] == target)
299 return arr[mid];
300
301 /* If target is less than array element,
302 * then search in left */
303 if (target < arr[mid]) {
304
305 // If target is greater than previous
306 // to mid, return closest of two
307 if (mid > 0 && target > arr[mid - 1])
308 return getClosest(arr[mid - 1],
309 arr[mid], target);
310
311 /* Repeat for left half */
312 j = mid;
313 }
314
315 // If target is greater than mid
316 else {
317 if (mid < n - 1 && target < arr[mid + 1])
318 return getClosest(arr[mid],
319 arr[mid + 1], target);
320 // update i
321 i = mid + 1;
322 }
323 }
324
325 // Only single element left after search
326 return arr[mid];
327}
328
329// Method to compare which one is the more close.
330// We find the closest by taking the difference
331// between the target and both values. It assumes
332// that val2 is greater than val1 and target lies
333// between these two.
334int getClosest(int val1, int val2,
335 int target)
336{
337 if (target - val1 >= val2 - target)
338 return val2;
339 else
340 return val1;
341}
342
343
344int Style::mapToIconSizes(const int &size)
345{
346 int values[] = {8, 16, 22, 32, 48, 64, 128};
347 int n = sizeof(values) / sizeof(values[0]);
348 return findClosest (values, n, size);
349}
350
351GroupSizes::GroupSizes(const uint tiny, const uint small, const uint medium, const uint big, const uint large, const uint huge, const uint enormous, QObject *parent) : QObject(parent)
352 ,m_tiny(tiny)
353 ,m_small(small)
354 ,m_medium(medium)
355 ,m_big(big)
356 ,m_large(large)
357 ,m_huge(huge)
358 ,m_enormous(enormous)
359
360{
361
362}
363
364GroupSizes::GroupSizes(QObject* parent) : QObject(parent)
365{
366}
367
368
370{
371 return m_adaptiveColorSchemeSource;
372}
373
374void Style::setAdaptiveColorSchemeSource(const QVariant& source)
375{
376 m_adaptiveColorSchemeSource_blocked = true;
377 if(source == m_adaptiveColorSchemeSource)
378 {
379 return;
380 }
381
382 m_adaptiveColorSchemeSource = source;
383 Q_EMIT adaptiveColorSchemeSourceChanged(m_adaptiveColorSchemeSource);
384}
385
386void Style::unsetAdaptiveColorSchemeSource()
387{
388 m_adaptiveColorSchemeSource_blocked = false;
389 m_adaptiveColorSchemeSource = QUrl::fromUserInput(m_backgroundSettings->wallpaperSource()).toLocalFile();
390 Q_EMIT adaptiveColorSchemeSourceChanged(m_adaptiveColorSchemeSource);
391}
392
394{
395 return m_accentColor;
396}
397
398void Style::setAccentColor(const QColor& color)
399{
400 m_accentColor_blocked = true;
401
402 if(m_accentColor == color)
403 {
404 return;
405 }
406
407 m_accentColor = color;
408 Q_EMIT accentColorChanged(m_accentColor);
409}
410
411void Style::unsetAccentColor()
412{
413 m_accentColor_blocked = false;
414 m_accentColor = m_themeSettings->accentColor();
415 Q_EMIT accentColorChanged(m_accentColor);
416}
417
419{
420 return m_styleType;
421}
422
423void Style::setStyleType(const Style::StyleType &type)
424{
425 m_styleType_blocked = true;
426
427 if (m_styleType == type)
428 return;
429
430 m_styleType = type;
431 Q_EMIT styleTypeChanged(m_styleType);
432}
433
434void Style::unsetStyeType()
435{
436 m_styleType_blocked = false;
437 m_styleType = static_cast<Style::StyleType>(m_themeSettings->styleType());
438 Q_EMIT styleTypeChanged(m_styleType);
439}
440
441Units::Units(QObject *parent) : QObject(parent)
442 , m_fontMetrics(QFontMetricsF(QGuiApplication::font()))
443 , m_gridUnit(m_fontMetrics.height())
444 , m_veryLongDuration(400)
445 , m_longDuration(200)
446 , m_shortDuration(100)
447 , m_veryShortDuration(50)
448 , m_humanMoment(2000)
449 , m_toolTipDelay(700)
450{
451
452}
453
454uint Style::iconSize() const
455{
456 return m_iconSize;
457}
458
460{
461 return m_currentIconTheme;
462}
463
464bool Style::menusHaveIcons() const
465{
466 return !qApp->testAttribute(Qt::AA_DontShowIconsInMenus);
467}
468
469uint Style::scrollBarPolicy() const
470{
471// return m_accessibilitySettings->scrollBarPolicy();
472 return 2;
473}
474
475bool Style::playSounds() const
476{
477 return m_accessibilitySettings->playSounds();
478}
479
480
481
482
The sizes group for some Style properties, such as Style::iconSize, Style::space, etc.
Definition style.h:54
static bool isMauiSession()
The MauiKit Style preferences singleton object.
Definition style.h:85
QString currentIconTheme
The current system icon theme picked up by the user.
Definition style.h:233
QVariant adaptiveColorSchemeSource
The source for picking up the application color palette when the style type is set to Style....
Definition style.h:211
uint iconSize
The preferred size for painting the icons in places, such as menus, buttons and delegates.
Definition style.h:115
bool menusHaveIcons
Whether the menu entries should display the icon image.
Definition style.h:239
bool playSounds
Whether the user desires for the application to play sounds or not.
Definition style.h:251
bool enableEffects
Whether special effects are desired.
Definition style.h:226
StyleType
The different options for the color scheme style.
Definition style.h:270
@ Light
A light variant designed for Maui.
Definition style.h:274
@ Dark
A dark variant designed for Maui.
Definition style.h:279
uint scrollBarPolicy
The preferred scroll bars policy for displaying them or not.
Definition style.h:245
int mapToIconSizes(const int &size)
Given a size as argument this function will return the best fitted icon size from the standard icon s...
Definition style.cpp:344
StyleType styleType
The preferred style type for setting the color scheme of the application.
Definition style.h:219
bool translucencyAvailable
Whether the application window surface should be transparent and request the compositor to blur the b...
Definition style.h:257
QColor accentColor
Sets the color to be used for highlighted, active, checked and such states of the UI elements.
Definition style.h:204
The Unit group properties.
Definition style.h:24
QString name(GameStandardAction id)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
QCoreApplication * instance()
int pointSize() const const
void setBold(bool enable)
void setPointSize(int pointSize)
void setWeight(Weight weight)
QFont systemFont(SystemFont type)
void fontChanged(const QFont &font)
QStyleHints * styleHints()
QString themeName()
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void destroyed(QObject *obj)
T qobject_cast(QObject *object)
QObject * sender() const const
void colorSchemeChanged(Qt::ColorScheme colorScheme)
AA_DontShowIconsInMenus
ColorScheme
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QUrl fromUserInput(const QString &userInput, const QString &workingDirectory, UserInputResolutionOptions options)
QString toLocalFile() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:50:53 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.