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
9#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
10#include <MauiMan3/thememanager.h>
11#include <MauiMan3/backgroundmanager.h>
12#include <MauiMan3/accessibilitymanager.h>
13#else
14#include <MauiMan4/thememanager.h>
15#include <MauiMan4/backgroundmanager.h>
16#include <MauiMan4/accessibilitymanager.h>
17
18#include <QStyleHints>
19#endif
20
21#ifdef Q_OS_ANDROID
22#include "mauiandroid.h"
23#endif
24
25Q_GLOBAL_STATIC(Style, styleInstance)
26
27void Style::styleChanged()
28{
29 // It should be safe to use qApp->style() unguarded here, because the signal
30 // will only have been connected if qApp is a QApplication.
31 Q_ASSERT(qobject_cast<QApplication *>(QCoreApplication::instance()));
32 auto *style = qApp->style();
33 if (!style || QCoreApplication::closingDown()) {
34 return;
35 }
36
37 Q_ASSERT(style != sender());
38
39 connect(style, &QObject::destroyed, this, &Style::styleChanged);
40
41 m_currentIconTheme = QIcon::themeName();
42 Q_EMIT currentIconThemeChanged(m_currentIconTheme);
43
45 Q_EMIT monospacedFontChanged();
46}
47
48
49Style::Style(QObject *parent) : QObject(parent)
50 ,m_iconSizes (new GroupSizes(8,16, 22, 32, 48, 64, 128, this))
51 ,m_space( new GroupSizes(4, 6, 8, 16, 24, 32, 40, this))
52 ,m_fontSizes(new GroupSizes(this))
53 ,m_units(new Units(this))
54 ,m_accentColor(QColor("#26c6da"))
55 ,m_themeSettings( new MauiMan::ThemeManager(this))
56 ,m_backgroundSettings( new MauiMan::BackgroundManager(this))
57 ,m_accessibilitySettings( new MauiMan::AccessibilityManager(this))
58{
59 connect(qGuiApp, &QGuiApplication::fontChanged, [this](const QFont &font)
60 {
61 m_defaultFont = font;
62 setFontSizes();
63 Q_EMIT defaultFontChanged();
64 // Q_EMIT m_fontSizes->sizesChanged();
65 Q_EMIT fontSizesChanged();
66 Q_EMIT h1FontChanged();
67 Q_EMIT h2FontChanged();
68 Q_EMIT
69 });
70
71#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
72 connect(m_themeSettings, &MauiMan::ThemeManager::styleTypeChanged, [this](int type)
73 {
74 if(m_styleType_blocked)
75 return;
76
77 m_styleType = static_cast<Style::StyleType>(type);
78 Q_EMIT styleTypeChanged(m_styleType);
79 });
80#else
81
83 {
84 qDebug() << "Color schem style type changed<<"<< type;
85 if(m_styleType_blocked)
86 return;
87
88 switch(type)
89 {
90 case Qt::ColorScheme::Unknown:
91 m_styleType = static_cast<Style::StyleType>(m_themeSettings->styleType());
92 break;
93 case Qt::ColorScheme::Light:
94 m_styleType = Style::StyleType::Light;
95 break;
96 case Qt::ColorScheme::Dark:
97 m_styleType = Style::StyleType::Dark;
98 break;
99 }
100
101 Q_EMIT styleTypeChanged(m_styleType);
102 });
103
104 connect(m_themeSettings, &MauiMan::ThemeManager::styleTypeChanged, [this](int type)
105 {
106 if(m_styleType_blocked)
107 return;
108
109 m_styleType = static_cast<Style::StyleType>(type);
110 Q_EMIT styleTypeChanged(m_styleType);
111 });
112
113#endif
114
115 connect(m_themeSettings, &MauiMan::ThemeManager::accentColorChanged, [this](QString color)
116 {
117 m_accentColor = color;
118 Q_EMIT this->accentColorChanged(m_accentColor);
119 });
120
121 connect(m_themeSettings, &MauiMan::ThemeManager::borderRadiusChanged, [this](uint radius)
122 {
123 m_radiusV = radius;
124 Q_EMIT this->radiusVChanged(m_radiusV);
125 });
126
127 connect(m_themeSettings, &MauiMan::ThemeManager::iconSizeChanged, [this](uint size)
128 {
129 m_iconSize = size;
130 Q_EMIT this->iconSizeChanged(m_iconSize);
131 });
132
133 connect(m_themeSettings, &MauiMan::ThemeManager::paddingSizeChanged, [this](uint size)
134 {
135 m_defaultPadding = size;
136 Q_EMIT this->defaultPaddingChanged();
137 });
138
139 connect(m_themeSettings, &MauiMan::ThemeManager::marginSizeChanged, [this](uint size)
140 {
141 qDebug() << "ContentMARGINS CHANGED" << size;
142 m_contentMargins = size;
143 Q_EMIT this->contentMarginsChanged();
144 });
145
146 connect(m_themeSettings, &MauiMan::ThemeManager::spacingSizeChanged, [this](uint size)
147 {
148 m_defaultSpacing = size;
149 Q_EMIT this->defaultSpacingChanged();
150 });
151
152 connect(m_themeSettings, &MauiMan::ThemeManager::enableEffectsChanged, [this](bool value)
153 {
154 m_enableEffects = value;
155 Q_EMIT this->enableEffectsChanged(m_enableEffects);
156 });
157
158 connect(m_backgroundSettings, &MauiMan::BackgroundManager::wallpaperSourceChanged, [this](QString source)
159 {
160 m_adaptiveColorSchemeSource = QUrl::fromUserInput(source).toLocalFile();
161 Q_EMIT this->adaptiveColorSchemeSourceChanged(m_adaptiveColorSchemeSource);
162 });
163
164 connect(m_themeSettings, &MauiMan::ThemeManager::enableEffectsChanged, [this](bool value)
165 {
166 m_enableEffects = value;
167 Q_EMIT this->enableEffectsChanged(m_enableEffects);
168 });
169
170 connect(m_accessibilitySettings, &MauiMan::AccessibilityManager::scrollBarPolicyChanged, [this](uint state)
171 {
172 qDebug() << "SCROLBAR POLICY CHANGED" << state;
173 Q_EMIT scrollBarPolicyChanged(state);
174 });
175
177 {
178 connect(m_themeSettings, &MauiMan::ThemeManager::iconThemeChanged, [this](QString name)
179 {
180 qDebug() << "Ask to change the icon theme";
181 m_currentIconTheme = name;
182 Q_EMIT currentIconThemeChanged(m_currentIconTheme);
183 });
184 }else
185 {
186 // //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?
187 // QStyle *style = qApp->style();
188 // if (style)
189 // {
190 // connect(style, &QObject::destroyed, this, &Style::styleChanged);
191 // }
192 }
193
194 m_defaultFont = qGuiApp->font();
196 setFontSizes();
197
198 m_radiusV = m_themeSettings->borderRadius();
199 m_iconSize = m_themeSettings->iconSize();
200 m_accentColor = m_themeSettings->accentColor();
201
202 m_contentMargins = m_themeSettings->marginSize();
203 m_defaultPadding = m_themeSettings->paddingSize();
204 m_defaultSpacing = m_themeSettings->spacingSize();
205
206 m_currentIconTheme = QIcon::themeName();
207
208 //TODO Use new Qt6 StyelHint properties for this
209
210#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
211
212#ifdef Q_OS_ANDROID
213 MAUIAndroid android;
214 m_styleType = android.darkModeEnabled() ? StyleType::Dark : StyleType::Light;
215#else
216 m_styleType = static_cast<Style::StyleType>(m_themeSettings->styleType());
217#endif
218
219#else
220 //For Maui Session we want to use MauiMan
222 {
224 {
225 case Qt::ColorScheme::Unknown:
226 m_styleType = static_cast<Style::StyleType>(m_themeSettings->styleType());
227 break;
228 case Qt::ColorScheme::Light:
229 m_styleType = Style::StyleType::Light;
230 break;
231 case Qt::ColorScheme::Dark:
232 m_styleType = Style::StyleType::Dark;
233 break;
234 }
235 }
236 else
237 {
238 m_styleType = static_cast<Style::StyleType>(m_themeSettings->styleType());
239 }
240#endif
241
242 m_adaptiveColorSchemeSource = QUrl::fromUserInput(m_backgroundSettings->wallpaperSource()).toLocalFile();
243 m_enableEffects = m_themeSettings->enableEffects();
244}
245
246void Style::setFontSizes()
247{
248 qDebug() << m_defaultFont << m_defaultFont.pointSize();
249
250 m_defaultFontSize = m_defaultFont.pointSize ();
251
252 m_fontSizes->m_tiny = m_defaultFont.pointSize ()-2;
253 m_fontSizes->m_small = m_defaultFont.pointSize ()-1;
254 m_fontSizes->m_medium = m_defaultFont.pointSize ();
255 m_fontSizes->m_big = m_defaultFont.pointSize ()+1;
256 m_fontSizes->m_large = m_defaultFont.pointSize ()+2;
257 m_fontSizes->m_huge = m_defaultFont.pointSize ()+3;
258 m_fontSizes->m_enormous = m_defaultFont.pointSize ()+4;
259
260 m_h1Font.setPointSize(m_fontSizes->m_enormous);
261 m_h1Font.setWeight(QFont::Black);
262 m_h1Font.setBold(true);
263
264 m_h2Font.setPointSize(m_fontSizes->m_big);
265 m_h2Font.setWeight(QFont::DemiBold);
266 // m_h2Font.setBold(false);
267}
268
269void Style::setRadiusV(const uint& radius)
270{
271 if(m_radiusV == radius)
272 {
273 return;
274 }
275
276 m_radiusV = radius;
277 Q_EMIT radiusVChanged(m_radiusV);
278}
279
280bool Style::enableEffects() const
281{
282 return m_enableEffects;
283}
284
286{
287 return m_translucencyAvailable;
288}
289
290void Style::setTranslucencyAvailable(const bool &value)
291{
292 if(value == m_translucencyAvailable)
293 {
294 return;
295 }
296
297 m_translucencyAvailable = value;
298 Q_EMIT this->translucencyAvailableChanged(m_translucencyAvailable);
299}
300
301
302Style *Style::qmlAttachedProperties(QObject *object)
303{
304 Q_UNUSED(object)
305 return Style::instance();
306}
307
308Style *Style::instance()
309{
310 return styleInstance();
311}
312
313int getClosest(int, int, int);
314
315// Returns element closest to target in arr[]
316int findClosest(int arr[], int n, int target)
317{
318 // Corner cases
319 if (target <= arr[0])
320 return arr[0];
321 if (target >= arr[n - 1])
322 return arr[n - 1];
323
324 // Doing binary search
325 int i = 0, j = n, mid = 0;
326 while (i < j) {
327 mid = (i + j) / 2;
328
329 if (arr[mid] == target)
330 return arr[mid];
331
332 /* If target is less than array element,
333 * then search in left */
334 if (target < arr[mid]) {
335
336 // If target is greater than previous
337 // to mid, return closest of two
338 if (mid > 0 && target > arr[mid - 1])
339 return getClosest(arr[mid - 1],
340 arr[mid], target);
341
342 /* Repeat for left half */
343 j = mid;
344 }
345
346 // If target is greater than mid
347 else {
348 if (mid < n - 1 && target < arr[mid + 1])
349 return getClosest(arr[mid],
350 arr[mid + 1], target);
351 // update i
352 i = mid + 1;
353 }
354 }
355
356 // Only single element left after search
357 return arr[mid];
358}
359
360// Method to compare which one is the more close.
361// We find the closest by taking the difference
362// between the target and both values. It assumes
363// that val2 is greater than val1 and target lies
364// between these two.
365int getClosest(int val1, int val2,
366 int target)
367{
368 if (target - val1 >= val2 - target)
369 return val2;
370 else
371 return val1;
372}
373
374
375int Style::mapToIconSizes(const int &size)
376{
377 int values[] = {8, 16, 22, 32, 48, 64, 128};
378 int n = sizeof(values) / sizeof(values[0]);
379 return findClosest (values, n, size);
380}
381
382GroupSizes::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)
383 ,m_tiny(tiny)
384 ,m_small(small)
385 ,m_medium(medium)
386 ,m_big(big)
387 ,m_large(large)
388 ,m_huge(huge)
389 ,m_enormous(enormous)
390
391{
392
393}
394
395GroupSizes::GroupSizes(QObject* parent) : QObject(parent)
396{
397}
398
399
401{
402 return m_adaptiveColorSchemeSource;
403}
404
405void Style::setAdaptiveColorSchemeSource(const QVariant& source)
406{
407 m_adaptiveColorSchemeSource_blocked = true;
408 if(source == m_adaptiveColorSchemeSource)
409 {
410 return;
411 }
412
413 m_adaptiveColorSchemeSource = source;
414 Q_EMIT adaptiveColorSchemeSourceChanged(m_adaptiveColorSchemeSource);
415}
416
417void Style::unsetAdaptiveColorSchemeSource()
418{
419 m_adaptiveColorSchemeSource_blocked = false;
420 m_adaptiveColorSchemeSource = QUrl::fromUserInput(m_backgroundSettings->wallpaperSource()).toLocalFile();
421 Q_EMIT adaptiveColorSchemeSourceChanged(m_adaptiveColorSchemeSource);
422}
423
425{
426 return m_accentColor;
427}
428
429void Style::setAccentColor(const QColor& color)
430{
431 m_accentColor_blocked = true;
432
433 if(m_accentColor == color)
434 {
435 return;
436 }
437
438 m_accentColor = color;
439 Q_EMIT accentColorChanged(m_accentColor);
440}
441
442void Style::unsetAccentColor()
443{
444 m_accentColor_blocked = false;
445 m_accentColor = m_themeSettings->accentColor();
446 Q_EMIT accentColorChanged(m_accentColor);
447}
448
450{
451 return m_styleType;
452}
453
454void Style::setStyleType(const Style::StyleType &type)
455{
456 m_styleType_blocked = true;
457
458 if (m_styleType == type)
459 return;
460
461 m_styleType = type;
462 Q_EMIT styleTypeChanged(m_styleType);
463}
464
465void Style::unsetStyeType()
466{
467 m_styleType_blocked = false;
468 m_styleType = static_cast<Style::StyleType>(m_themeSettings->styleType());
469 Q_EMIT styleTypeChanged(m_styleType);
470}
471
472Units::Units(QObject *parent) : QObject(parent)
473 , m_fontMetrics(QFontMetricsF(QGuiApplication::font()))
474 , m_gridUnit(m_fontMetrics.height())
475 , m_veryLongDuration(400)
476 , m_longDuration(200)
477 , m_shortDuration(100)
478 , m_veryShortDuration(50)
479 , m_humanMoment(2000)
480 , m_toolTipDelay(700)
481{
482
483}
484
485uint Style::iconSize() const
486{
487 return m_iconSize;
488}
489
491{
492 return m_currentIconTheme;
493}
494
495bool Style::menusHaveIcons() const
496{
497 return !qApp->testAttribute(Qt::AA_DontShowIconsInMenus);
498}
499
500uint Style::scrollBarPolicy() const
501{
502// return m_accessibilitySettings->scrollBarPolicy();
503 return 2;
504}
505
506bool Style::playSounds() const
507{
508 return m_accessibilitySettings->playSounds();
509}
510
511
512
513
The sizes group for some Style properties, such as Style::iconSize, Style::space, etc.
Definition style.h:54
The MAUIAndroid class.
Definition mauiandroid.h:40
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:375
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
Type type(const QSqlDatabase &db)
QString name(StandardShortcut id)
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)
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 May 3 2024 11:47:05 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.