Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qandroidstyle.cpp
Go to the documentation of this file.
1// Copyright (C) 2013 BogDan Vatra <bogdan@kde.org>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <QFile>
8#include <QFont>
9#include <QApplication>
10#include <qdrawutil.h>
11#include <QPixmapCache>
12#include <QFileInfo>
13#include <QStyleOption>
14#include <QPainter>
15#include <QJsonDocument>
16#include <QJsonObject>
17#include <QDebug>
18
19#include <QGuiApplication>
20#include <qpa/qplatformnativeinterface.h>
21#include <qpa/qplatformtheme.h>
22
23QT_BEGIN_NAMESPACE
24
25namespace {
26 const quint32 NO_COLOR = 1;
28}
29
30QAndroidStyle::QAndroidStyle()
31 : QFusionStyle()
32{
33 QPixmapCache::clear();
34 QPlatformNativeInterface *nativeInterface = QGuiApplication::platformNativeInterface();
35 QPalette *standardPalette = reinterpret_cast<QPalette *>(nativeInterface->nativeResourceForIntegration("AndroidStandardPalette"));
36 if (standardPalette)
37 m_standardPalette = *standardPalette;
38
39 QHash<QByteArray, QFont> *qwidgetsFonts = reinterpret_cast<QHash<QByteArray, QFont> *>(nativeInterface->nativeResourceForIntegration("AndroidQWidgetFonts"));
40 if (qwidgetsFonts) {
41 for (auto it = qwidgetsFonts->constBegin(); it != qwidgetsFonts->constEnd(); ++it)
42 QApplication::setFont(it.value(), it.key());
43 qwidgetsFonts->clear(); // free the memory
44 }
45
46 QJsonObject *object = reinterpret_cast<QJsonObject *>(nativeInterface->nativeResourceForIntegration("AndroidStyleData"));
47 if (!object)
48 return;
49
50 for (QJsonObject::const_iterator objectIterator = object->constBegin();
51 objectIterator != object->constEnd();
52 ++objectIterator) {
53 QString key = objectIterator.key();
54 QJsonValue value = objectIterator.value();
55 if (Q_UNLIKELY(!value.isObject())) {
56 qWarning("Style.json structure is unrecognized.");
57 continue;
58 }
59
60 QJsonObject item = value.toObject();
61 QAndroidStyle::ItemType itemType = qtControl(key);
62 if (QC_UnknownType == itemType)
63 continue;
64
65 switch (itemType) {
66 case QC_Checkbox:
67 m_checkBoxControl = new AndroidCompoundButtonControl(item.toVariantMap(), itemType);
68 m_androidControlsHash[int(itemType)] = m_checkBoxControl;
69 break;
70 case QC_RadioButton:
71 m_androidControlsHash[int(itemType)] = new AndroidCompoundButtonControl(item.toVariantMap(),
72 itemType);
73 break;
74
75 case QC_ProgressBar:
76 m_androidControlsHash[int(itemType)] = new AndroidProgressBarControl(item.toVariantMap(),
77 itemType);
78 break;
79
80 case QC_Slider:
81 m_androidControlsHash[int(itemType)] = new AndroidSeekBarControl(item.toVariantMap(),
82 itemType);
83 break;
84
85 case QC_Combobox:
86 m_androidControlsHash[int(itemType)] = new AndroidSpinnerControl(item.toVariantMap(),
87 itemType);
88 break;
89
90 default:
91 m_androidControlsHash[int(itemType)] = new AndroidControl(item.toVariantMap(),
92 itemType);
93 break;
94 }
95 }
96 *object = QJsonObject(); // free memory
97}
98
99QAndroidStyle::~QAndroidStyle()
100{
101 qDeleteAll(m_androidControlsHash);
102}
103
104QAndroidStyle::ItemType QAndroidStyle::qtControl(const QString &android)
105{
106 if (android == QLatin1String("buttonStyle"))
107 return QC_Button;
108 if (android == QLatin1String("editTextStyle"))
109 return QC_EditText;
110 if (android == QLatin1String("radioButtonStyle"))
111 return QC_RadioButton;
112 if (android == QLatin1String("checkboxStyle"))
113 return QC_Checkbox;
114 if (android == QLatin1String("textViewStyle"))
115 return QC_View;
116 if (android == QLatin1String("buttonStyleToggle"))
117 return QC_Switch;
118 if (android == QLatin1String("spinnerStyle"))
119 return QC_Combobox;
120 if (android == QLatin1String("progressBarStyleHorizontal"))
121 return QC_ProgressBar;
122 if (android == QLatin1String("seekBarStyle"))
123 return QC_Slider;
124
125 return QC_UnknownType;
126}
127
128QAndroidStyle::ItemType QAndroidStyle::qtControl(QStyle::ComplexControl control)
129{
130 switch (control) {
131 case CC_ComboBox:
132 return QC_Combobox;
133 case CC_Slider:
134 return QC_Slider;
135 default:
136 return QC_UnknownType;
137 }
138}
139
140QAndroidStyle::ItemType QAndroidStyle::qtControl(QStyle::ContentsType contentsType)
141{
142 switch (contentsType) {
143 case CT_PushButton:
144 return QC_Button;
145 case CT_CheckBox:
146 return QC_Checkbox;
147 case CT_RadioButton:
148 return QC_RadioButton;
149 case CT_ComboBox:
150 return QC_Combobox;
151 case CT_ProgressBar:
152 return QC_ProgressBar;
153 case CT_Slider:
154 return QC_Slider;
155 case CT_ScrollBar:
156 return QC_Slider;
157 case CT_TabWidget:
158 return QC_Tab;
159 case CT_TabBarTab:
160 return QC_TabButton;
161 case CT_LineEdit:
162 return QC_EditText;
163 case CT_GroupBox:
164 return QC_GroupBox;
165 default:
166 return QC_UnknownType;
167 }
168}
169
170QAndroidStyle::ItemType QAndroidStyle::qtControl(QStyle::ControlElement controlElement)
171{
172 switch (controlElement) {
173 case CE_PushButton:
174 case CE_PushButtonBevel:
175 case CE_PushButtonLabel:
176 return QC_Button;
177
178 case CE_CheckBox:
179 case CE_CheckBoxLabel:
180 return QC_Checkbox;
181
182 case CE_RadioButton:
183 case CE_RadioButtonLabel:
184 return QC_RadioButton;
185
186 case CE_TabBarTab:
187 case CE_TabBarTabShape:
188 case CE_TabBarTabLabel:
189 return QC_Tab;
190
191 case CE_ProgressBar:
192 case CE_ProgressBarGroove:
193 case CE_ProgressBarContents:
194 case CE_ProgressBarLabel:
195 return QC_ProgressBar;
196
197 case CE_ComboBoxLabel:
198 return QC_Combobox;
199
200 case CE_ShapedFrame:
201 return QC_View;
202
203 default:
204 return QC_UnknownType;
205 }
206}
207
208QAndroidStyle::ItemType QAndroidStyle::qtControl(QStyle::PrimitiveElement primitiveElement)
209{
210 switch (primitiveElement) {
211 case QStyle::PE_PanelLineEdit:
212 case QStyle::PE_FrameLineEdit:
213 return QC_EditText;
214
215 case QStyle::PE_IndicatorItemViewItemCheck:
216 case QStyle::PE_IndicatorCheckBox:
217 return QC_Checkbox;
218
219 case QStyle::PE_FrameWindow:
220 case QStyle::PE_Widget:
221 case QStyle::PE_Frame:
222 case QStyle::PE_FrameFocusRect:
223 return QC_View;
224 default:
225 return QC_UnknownType;
226 }
227}
228
229QAndroidStyle::ItemType QAndroidStyle::qtControl(QStyle::SubElement subElement)
230{
231 switch (subElement) {
232 case QStyle::SE_LineEditContents:
233 return QC_EditText;
234
235 case QStyle::SE_PushButtonContents:
236 case QStyle::SE_PushButtonFocusRect:
237 return QC_Button;
238
239 case SE_RadioButtonContents:
240 return QC_RadioButton;
241
242 case SE_CheckBoxContents:
243 return QC_Checkbox;
244
245 default:
246 return QC_UnknownType;
247 }
248}
249
250void QAndroidStyle::drawPrimitive(PrimitiveElement pe,
251 const QStyleOption *opt,
252 QPainter *p,
253 const QWidget *w) const
254{
255 const ItemType itemType = qtControl(pe);
256 AndroidControlsHash::const_iterator it = itemType != QC_UnknownType
257 ? m_androidControlsHash.find(itemType)
258 : m_androidControlsHash.end();
259 if (it != m_androidControlsHash.end()) {
260 if (itemType != QC_EditText) {
261 it.value()->drawControl(opt, p, w);
262 } else {
263 QStyleOption copy(*opt);
264 copy.state &= ~QStyle::State_Sunken;
265 it.value()->drawControl(&copy, p, w);
266 }
267 } else if (pe == PE_FrameGroupBox) {
268 if (const QStyleOptionFrame *frame = qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
269 if (frame->features & QStyleOptionFrame::Flat) {
270 QRect fr = frame->rect;
271 QPoint p1(fr.x(), fr.y() + 1);
272 QPoint p2(fr.x() + fr.width(), p1.y());
273 qDrawShadeLine(p, p1, p2, frame->palette, true,
274 frame->lineWidth, frame->midLineWidth);
275 } else {
276 qDrawShadeRect(p, frame->rect.x(), frame->rect.y(), frame->rect.width(),
277 frame->rect.height(), frame->palette, true,
278 frame->lineWidth, frame->midLineWidth);
279 }
280 }
281 } else {
282 QFusionStyle::drawPrimitive(pe, opt, p, w);
283 }
284}
285
286
287void QAndroidStyle::drawControl(QStyle::ControlElement element,
288 const QStyleOption *opt,
289 QPainter *p,
290 const QWidget *w) const
291{
292 const ItemType itemType = qtControl(element);
293 AndroidControlsHash::const_iterator it = itemType != QC_UnknownType
294 ? m_androidControlsHash.find(itemType)
295 : m_androidControlsHash.end();
296 if (it != m_androidControlsHash.end()) {
297 AndroidControl *androidControl = it.value();
298
299 if (element != QStyle::CE_CheckBoxLabel
300 && element != QStyle::CE_PushButtonLabel
301 && element != QStyle::CE_RadioButtonLabel
302 && element != QStyle::CE_TabBarTabLabel
303 && element != QStyle::CE_ProgressBarLabel) {
304 androidControl->drawControl(opt, p, w);
305 }
306
307 if (element != QStyle::CE_PushButtonBevel
308 && element != QStyle::CE_TabBarTabShape
309 && element != QStyle::CE_ProgressBarGroove) {
310 switch (itemType) {
311 case QC_Button:
312 if (const QStyleOptionButton *buttonOption =
313 qstyleoption_cast<const QStyleOptionButton *>(opt)) {
314 QMargins padding = androidControl->padding();
315 QStyleOptionButton copy(*buttonOption);
316 copy.rect.adjust(padding.left(), padding.top(), -padding.right(), -padding.bottom());
317 QFusionStyle::drawControl(CE_PushButtonLabel, &copy, p, w);
318 }
319 break;
320 case QC_Checkbox:
321 case QC_RadioButton:
322 if (const QStyleOptionButton *btn =
323 qstyleoption_cast<const QStyleOptionButton *>(opt)) {
324 const bool isRadio = (element == CE_RadioButton);
325 QStyleOptionButton subopt(*btn);
326 subopt.rect = subElementRect(isRadio ? SE_RadioButtonContents
327 : SE_CheckBoxContents, btn, w);
328 QFusionStyle::drawControl(isRadio ? CE_RadioButtonLabel : CE_CheckBoxLabel, &subopt, p, w);
329 }
330 break;
331 case QC_Combobox:
332 if (const QStyleOptionComboBox *comboboxOption =
333 qstyleoption_cast<const QStyleOptionComboBox *>(opt)) {
334 QMargins padding = androidControl->padding();
335 QStyleOptionComboBox copy (*comboboxOption);
336 copy.rect.adjust(padding.left(), padding.top(), -padding.right(), -padding.bottom());
337 QFusionStyle::drawControl(CE_ComboBoxLabel, comboboxOption, p, w);
338 }
339 break;
340 default:
341 QFusionStyle::drawControl(element, opt, p, w);
342 break;
343 }
344 }
345 } else {
346 QFusionStyle::drawControl(element, opt, p, w);
347 }
348}
349
350QRect QAndroidStyle::subElementRect(SubElement subElement,
351 const QStyleOption *option,
352 const QWidget *widget) const
353{
354 const ItemType itemType = qtControl(subElement);
355 AndroidControlsHash::const_iterator it = itemType != QC_UnknownType
356 ? m_androidControlsHash.find(itemType)
357 : m_androidControlsHash.end();
358 if (it != m_androidControlsHash.end())
359 return it.value()->subElementRect(subElement, option, widget);
360 return QFusionStyle::subElementRect(subElement, option, widget);
361}
362
363void QAndroidStyle::drawComplexControl(ComplexControl cc,
364 const QStyleOptionComplex *opt,
365 QPainter *p,
366 const QWidget *widget) const
367{
368 const ItemType itemType = qtControl(cc);
369 AndroidControlsHash::const_iterator it = itemType != QC_UnknownType
370 ? m_androidControlsHash.find(itemType)
371 : m_androidControlsHash.end();
372 if (it != m_androidControlsHash.end()) {
373 it.value()->drawControl(opt, p, widget);
374 return;
375 }
376 if (cc == CC_GroupBox) {
377 if (!m_checkBoxControl) {
378 QFusionStyle::drawComplexControl(cc, opt, p, widget);
379 return;
380 }
381 if (const QStyleOptionGroupBox *groupBox = qstyleoption_cast<const QStyleOptionGroupBox *>(opt)) {
382 // Draw frame
383 QRect textRect = subControlRect(CC_GroupBox, opt, SC_GroupBoxLabel, widget);
384 QRect checkBoxRect;
385 if (groupBox->subControls & SC_GroupBoxCheckBox)
386 checkBoxRect = subControlRect(CC_GroupBox, opt, SC_GroupBoxCheckBox, widget);
387 if (groupBox->subControls & QStyle::SC_GroupBoxFrame) {
388 QStyleOptionFrame frame;
389 frame.QStyleOption::operator=(*groupBox);
390 frame.features = groupBox->features;
391 frame.lineWidth = groupBox->lineWidth;
392 frame.midLineWidth = groupBox->midLineWidth;
393 frame.rect = subControlRect(CC_GroupBox, opt, SC_GroupBoxFrame, widget);
394 p->save();
395 QRegion region(groupBox->rect);
396 if (!groupBox->text.isEmpty()) {
397 bool ltr = groupBox->direction == Qt::LeftToRight;
398 QRect finalRect;
399 if (groupBox->subControls & QStyle::SC_GroupBoxCheckBox) {
400 finalRect = checkBoxRect.united(textRect);
401 finalRect.adjust(ltr ? -4 : 0, 0, ltr ? 0 : 4, 0);
402 } else {
403 finalRect = textRect;
404 }
405 region -= finalRect;
406 }
407 p->setClipRegion(region);
408 drawPrimitive(PE_FrameGroupBox, &frame, p, widget);
409 p->restore();
410 }
411
412 // Draw title
413 if ((groupBox->subControls & QStyle::SC_GroupBoxLabel) && !groupBox->text.isEmpty()) {
414 QColor textColor = groupBox->textColor;
415 if (textColor.isValid())
416 p->setPen(textColor);
417 int alignment = int(groupBox->textAlignment);
418 if (!styleHint(QStyle::SH_UnderlineShortcut, opt, widget))
419 alignment |= Qt::TextHideMnemonic;
420
421 drawItemText(p, textRect, Qt::TextShowMnemonic | Qt::AlignHCenter | alignment,
422 groupBox->palette, groupBox->state & State_Enabled, groupBox->text,
423 textColor.isValid() ? QPalette::NoRole : QPalette::WindowText);
424
425 if (groupBox->state & State_HasFocus) {
426 QStyleOptionFocusRect fropt;
427 fropt.QStyleOption::operator=(*groupBox);
428 fropt.rect = textRect;
429 drawPrimitive(PE_FrameFocusRect, &fropt, p, widget);
430 }
431 }
432
433 // Draw checkbox
434 if (groupBox->subControls & SC_GroupBoxCheckBox) {
435 QStyleOptionButton box;
436 box.QStyleOption::operator=(*groupBox);
437 box.rect = checkBoxRect;
438 m_checkBoxControl->drawControl(&box, p, widget);
439 }
440 }
441 return;
442 }
443 QFusionStyle::drawComplexControl(cc, opt, p, widget);
444}
445
446QStyle::SubControl QAndroidStyle::hitTestComplexControl(ComplexControl cc,
447 const QStyleOptionComplex *opt,
448 const QPoint &pt,
449 const QWidget *widget) const
450{
451 const ItemType itemType = qtControl(cc);
452 AndroidControlsHash::const_iterator it = itemType != QC_UnknownType
453 ? m_androidControlsHash.find(itemType)
454 : m_androidControlsHash.end();
455 if (it != m_androidControlsHash.end()) {
456 switch (cc) {
457 case CC_Slider:
458 if (const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
459 QRect r = it.value()->subControlRect(slider, SC_SliderHandle, widget);
460 if (r.isValid() && r.contains(pt)) {
461 return SC_SliderHandle;
462 } else {
463 r = it.value()->subControlRect(slider, SC_SliderGroove, widget);
464 if (r.isValid() && r.contains(pt))
465 return SC_SliderGroove;
466 }
467 }
468 break;
469 default:
470 break;
471 }
472 }
473 return QFusionStyle::hitTestComplexControl(cc, opt, pt, widget);
474}
475
476QRect QAndroidStyle::subControlRect(ComplexControl cc,
477 const QStyleOptionComplex *opt,
478 SubControl sc,
479 const QWidget *widget) const
480{
481 if (cc == CC_GroupBox && !m_checkBoxControl)
482 return QFusionStyle::subControlRect(cc, opt, sc, widget);
483
484 const ItemType itemType = qtControl(cc);
485 AndroidControlsHash::const_iterator it = itemType != QC_UnknownType
486 ? m_androidControlsHash.find(itemType)
487 : m_androidControlsHash.end();
488 if (it != m_androidControlsHash.end())
489 return it.value()->subControlRect(opt, sc, widget);
490 QRect rect = opt->rect;
491 switch (cc) {
492 case CC_GroupBox: {
493 if (const QStyleOptionGroupBox *groupBox = qstyleoption_cast<const QStyleOptionGroupBox *>(opt)) {
494 QSize textSize = opt->fontMetrics.boundingRect(groupBox->text).size() + QSize(2, 2);
495 QSize checkBoxSize = m_checkBoxControl->size(opt);
496 int indicatorWidth = checkBoxSize.width();
497 int indicatorHeight = checkBoxSize.height();
498 QRect checkBoxRect;
499 if (opt->subControls & QStyle::SC_GroupBoxCheckBox) {
500 checkBoxRect.setWidth(indicatorWidth);
501 checkBoxRect.setHeight(indicatorHeight);
502 }
503 checkBoxRect.moveLeft(1);
504 QRect textRect = checkBoxRect;
505 textRect.setSize(textSize);
506 if (opt->subControls & QStyle::SC_GroupBoxCheckBox)
507 textRect.translate(indicatorWidth + 5, (indicatorHeight - textSize.height()) / 2);
508 if (sc == SC_GroupBoxFrame) {
509 rect = opt->rect.adjusted(0, 0, 0, 0);
510 rect.translate(0, textRect.height() / 2);
511 rect.setHeight(rect.height() - textRect.height() / 2);
512 } else if (sc == SC_GroupBoxContents) {
513 QRect frameRect = opt->rect.adjusted(0, 0, 0, -groupBox->lineWidth);
514 int margin = 3;
515 int leftMarginExtension = 0;
516 int topMargin = qMax(pixelMetric(PM_ExclusiveIndicatorHeight), opt->fontMetrics.height()) + groupBox->lineWidth;
517 frameRect.adjust(leftMarginExtension + margin, margin + topMargin, -margin, -margin - groupBox->lineWidth);
518 frameRect.translate(0, textRect.height() / 2);
519 rect = frameRect;
520 rect.setHeight(rect.height() - textRect.height() / 2);
521 } else if (sc == SC_GroupBoxCheckBox) {
522 rect = checkBoxRect;
523 } else if (sc == SC_GroupBoxLabel) {
524 rect = textRect;
525 }
526 return visualRect(opt->direction, opt->rect, rect);
527 }
528
529 return rect;
530 }
531
532 default:
533 break;
534 }
535
536
537 return QFusionStyle::subControlRect(cc, opt, sc, widget);
538}
539
540int QAndroidStyle::pixelMetric(PixelMetric metric, const QStyleOption *option,
541 const QWidget *widget) const
542{
543 switch (metric) {
544 case PM_ButtonMargin:
545 case PM_FocusFrameVMargin:
546 case PM_FocusFrameHMargin:
547 case PM_ComboBoxFrameWidth:
548 case PM_SpinBoxFrameWidth:
549 case PM_ScrollBarExtent:
550 return 0;
551 case PM_IndicatorWidth:
552 if (m_checkBoxControl)
553 return m_checkBoxControl->size(option).width();
554 return QFusionStyle::pixelMetric(metric, option, widget);
555 case PM_IndicatorHeight:
556 if (m_checkBoxControl)
557 return m_checkBoxControl->size(option).height();
558 return QFusionStyle::pixelMetric(metric, option, widget);
559 default:
560 return QFusionStyle::pixelMetric(metric, option, widget);
561 }
562
563}
564
565QSize QAndroidStyle::sizeFromContents(ContentsType ct,
566 const QStyleOption *opt,
567 const QSize &contentsSize,
568 const QWidget *w) const
569{
570 QSize sz = QFusionStyle::sizeFromContents(ct, opt, contentsSize, w);
571 if (ct == CT_HeaderSection) {
572 if (const QStyleOptionHeader *hdr = qstyleoption_cast<const QStyleOptionHeader *>(opt)) {
573 bool nullIcon = hdr->icon.isNull();
574 int margin = pixelMetric(QStyle::PM_HeaderMargin, hdr, w);
575 int iconSize = nullIcon ? 0 : pixelMetric(QStyle::PM_IndicatorWidth, opt, w);
576 QSize txt;
577/*
578 * These next 4 lines are a bad hack to fix a bug in case a QStyleSheet is applied at QApplication level.
579 * In that case, even if the stylesheet does not refer to headers, the header font is changed to application
580 * font, which is wrong. Even worst, hdr->fontMetrics(...) does not report the proper size.
581 */
582 if (qApp->styleSheet().isEmpty())
583 txt = hdr->fontMetrics.size(0, hdr->text);
584 else
585 txt = QFontMetrics(QApplication::font()).size(0, hdr->text);
586
587 sz.setHeight(margin + qMax(iconSize, txt.height()) + margin);
588 sz.setWidth((nullIcon ? 0 : margin) + iconSize
589 + (hdr->text.isNull() ? 0 : margin) + txt.width() + margin);
590 if (hdr->sortIndicator != QStyleOptionHeader::None) {
591 int margin = pixelMetric(QStyle::PM_HeaderMargin, hdr, w);
592 if (hdr->orientation == Qt::Horizontal)
593 sz.rwidth() += sz.height() + margin;
594 else
595 sz.rheight() += sz.width() + margin;
596 }
597 return sz;
598 }
599 }
600 const ItemType itemType = qtControl(ct);
601 AndroidControlsHash::const_iterator it = itemType != QC_UnknownType
602 ? m_androidControlsHash.find(itemType)
603 : m_androidControlsHash.end();
604 if (it != m_androidControlsHash.end())
605 return it.value()->sizeFromContents(opt, sz, w);
606 if (ct == CT_GroupBox && m_checkBoxControl) {
607 if (const QStyleOptionGroupBox *groupBox = qstyleoption_cast<const QStyleOptionGroupBox *>(opt)) {
608 QSize textSize = opt->fontMetrics.boundingRect(groupBox->text).size() + QSize(2, 2);
609 QSize checkBoxSize = m_checkBoxControl->size(opt);
610 int indicatorWidth = checkBoxSize.width();
611 int indicatorHeight = checkBoxSize.height();
612 QRect checkBoxRect;
613 if (groupBox->subControls & QStyle::SC_GroupBoxCheckBox) {
614 checkBoxRect.setWidth(indicatorWidth);
615 checkBoxRect.setHeight(indicatorHeight);
616 }
617 checkBoxRect.moveLeft(1);
618 QRect textRect = checkBoxRect;
619 textRect.setSize(textSize);
620 if (groupBox->subControls & QStyle::SC_GroupBoxCheckBox)
621 textRect.translate(indicatorWidth + 5, (indicatorHeight - textSize.height()) / 2);
622 QRect u = textRect.united(checkBoxRect);
623 sz = QSize(sz.width(), sz.height() + u.height());
624 }
625 }
626 return sz;
627}
628
629QPixmap QAndroidStyle::standardPixmap(StandardPixmap standardPixmap,
630 const QStyleOption *opt,
631 const QWidget *widget) const
632{
633 return QFusionStyle::standardPixmap(standardPixmap, opt, widget);
634}
635
636QPixmap QAndroidStyle::generatedIconPixmap(QIcon::Mode iconMode,
637 const QPixmap &pixmap,
638 const QStyleOption *opt) const
639{
640 return QFusionStyle::generatedIconPixmap(iconMode, pixmap, opt);
641}
642
643int QAndroidStyle::styleHint(QStyle::StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const
644{
645 switch (hint) {
646 case SH_Slider_AbsoluteSetButtons:
647 return Qt::LeftButton;
648
649 case SH_Slider_PageSetButtons:
650 return 0;
651
652 case SH_RequestSoftwareInputPanel:
653 return RSIP_OnMouseClick;
654
655 case SH_SpinBox_SelectOnStep:
656 return 0;
657
658 default:
659 return QFusionStyle::styleHint(hint, option, widget, returnData);
660 }
661}
662
663QPalette QAndroidStyle::standardPalette() const
664{
665 return m_standardPalette;
666}
667
668void QAndroidStyle::polish(QWidget *widget)
669{
670 widget->setAttribute(Qt::WA_StyledBackground, true);
671}
672
673void QAndroidStyle::unpolish(QWidget *widget)
674{
675 widget->setAttribute(Qt::WA_StyledBackground, false);
676}
677
678QAndroidStyle::AndroidDrawable::AndroidDrawable(const QVariantMap &drawable,
679 QAndroidStyle::ItemType itemType)
680{
681 initPadding(drawable);
682 m_itemType = itemType;
683}
684
685QAndroidStyle::AndroidDrawable::~AndroidDrawable()
686{
687}
688
689void QAndroidStyle::AndroidDrawable::initPadding(const QVariantMap &drawable)
690{
691 QVariantMap::const_iterator it = drawable.find(QLatin1String("padding"));
692 if (it != drawable.end())
693 m_padding = extractMargins(it.value().toMap());
694}
695
696const QMargins &QAndroidStyle::AndroidDrawable::padding() const
697{
698 return m_padding;
699}
700
701QSize QAndroidStyle::AndroidDrawable::size() const
702{
703 if (type() == Image || type() == NinePatch)
704 return static_cast<const QAndroidStyle::AndroidImageDrawable *>(this)->size();
705
706 return QSize();
707}
708
709QAndroidStyle::AndroidDrawable * QAndroidStyle::AndroidDrawable::fromMap(const QVariantMap &drawable,
710 ItemType itemType)
711{
712 const QString type = drawable.value(QLatin1String("type")).toString();
713 if (type == QLatin1String("image"))
714 return new QAndroidStyle::AndroidImageDrawable(drawable, itemType);
715 if (type == QLatin1String("9patch"))
716 return new QAndroidStyle::Android9PatchDrawable(drawable, itemType);
717 if (type == QLatin1String("stateslist"))
718 return new QAndroidStyle::AndroidStateDrawable(drawable, itemType);
719 if (type == QLatin1String("layer"))
720 return new QAndroidStyle::AndroidLayerDrawable(drawable, itemType);
721 if (type == QLatin1String("gradient"))
722 return new QAndroidStyle::AndroidGradientDrawable(drawable, itemType);
723 if (type == QLatin1String("clipDrawable"))
724 return new QAndroidStyle::AndroidClipDrawable(drawable, itemType);
725 if (type == QLatin1String("color"))
726 return new QAndroidStyle::AndroidColorDrawable(drawable, itemType);
727 return 0;
728}
729
730QMargins QAndroidStyle::AndroidDrawable::extractMargins(const QVariantMap &value)
731{
732 QMargins m;
733 m.setLeft(value.value(QLatin1String("left")).toInt());
734 m.setRight(value.value(QLatin1String("right")).toInt());
735 m.setTop(value.value(QLatin1String("top")).toInt());
736 m.setBottom(value.value(QLatin1String("bottom")).toInt());
737 return m;
738}
739
740void QAndroidStyle::AndroidDrawable::setPaddingLeftToSizeWidth()
741{
742 QSize sz = size();
743 if (m_padding.isNull() && !sz.isNull())
744 m_padding.setLeft(sz.width());
745}
746
747
748QAndroidStyle::AndroidImageDrawable::AndroidImageDrawable(const QVariantMap &drawable,
749 QAndroidStyle::ItemType itemType)
750 : AndroidDrawable(drawable, itemType)
751{
752 m_filePath = drawable.value(QLatin1String("path")).toString();
753 m_size.setHeight(drawable.value(QLatin1String("height")).toInt());
754 m_size.setWidth(drawable.value(QLatin1String("width")).toInt());
755}
756
757QAndroidStyle::AndroidDrawableType QAndroidStyle::AndroidImageDrawable::type() const
758{
759 return QAndroidStyle::Image;
760}
761
762void QAndroidStyle::AndroidImageDrawable::draw(QPainter *painter, const QStyleOption *opt) const
763{
764 if (m_hashKey.isEmpty())
765 m_hashKey = QFileInfo(m_filePath).fileName();
766
767 QPixmap pm;
768 if (!QPixmapCache::find(m_hashKey, &pm)) {
769 pm.load(m_filePath);
770 QPixmapCache::insert(m_hashKey, pm);
771 }
772
773 painter->drawPixmap(opt->rect.x(), opt->rect.y() + (opt->rect.height() - pm.height()) / 2, pm);
774}
775
776QSize QAndroidStyle::AndroidImageDrawable::size() const
777{
778 return m_size;
779}
780
781QAndroidStyle::AndroidColorDrawable::AndroidColorDrawable(const QVariantMap &drawable,
782 ItemType itemType)
783 : AndroidDrawable(drawable, itemType)
784{
785 m_color.setRgba(QRgb(drawable.value(QLatin1String("color")).toInt()));
786}
787
788QAndroidStyle::AndroidDrawableType QAndroidStyle::AndroidColorDrawable::type() const
789{
790 return QAndroidStyle::Color;
791}
792
793void QAndroidStyle::AndroidColorDrawable::draw(QPainter *painter, const QStyleOption *opt) const
794{
795 painter->fillRect(opt->rect, m_color);
796}
797
798QAndroidStyle::Android9PatchDrawable::Android9PatchDrawable(const QVariantMap &drawable,
799 QAndroidStyle::ItemType itemType)
800 : AndroidImageDrawable(drawable.value(QLatin1String("drawable")).toMap(), itemType)
801{
802 initPadding(drawable);
803 QVariantMap chunk = drawable.value(QLatin1String("chunkInfo")).toMap();
804 extractIntArray(chunk.value(QLatin1String("xdivs")).toList(), m_chunkData.xDivs);
805 extractIntArray(chunk.value(QLatin1String("ydivs")).toList(), m_chunkData.yDivs);
806 extractIntArray(chunk.value(QLatin1String("colors")).toList(), m_chunkData.colors);
807}
808
809QAndroidStyle::AndroidDrawableType QAndroidStyle::Android9PatchDrawable::type() const
810{
811 return QAndroidStyle::NinePatch;
812}
813
814int QAndroidStyle::Android9PatchDrawable::calculateStretch(int boundsLimit,
815 int startingPoint,
816 int srcSpace,
817 int numStrechyPixelsRemaining,
818 int numFixedPixelsRemaining)
819{
820 int spaceRemaining = boundsLimit - startingPoint;
821 int stretchySpaceRemaining = spaceRemaining - numFixedPixelsRemaining;
822 return (float(srcSpace) * stretchySpaceRemaining / numStrechyPixelsRemaining + .5);
823}
824
825void QAndroidStyle::Android9PatchDrawable::extractIntArray(const QVariantList &values,
826 QList<int> & array)
827{
828 for (const QVariant &value : values)
829 array << value.toInt();
830}
831
832
833void QAndroidStyle::Android9PatchDrawable::draw(QPainter *painter, const QStyleOption *opt) const
834{
835 if (m_hashKey.isEmpty())
836 m_hashKey = QFileInfo(m_filePath).fileName();
837
838 QPixmap pixmap;
839 if (!QPixmapCache::find(m_hashKey, &pixmap)) {
840 pixmap.load(m_filePath);
841 QPixmapCache::insert(m_hashKey, pixmap);
842 }
843
844 const QRect &bounds = opt->rect;
845
846 // shamelessly stolen from Android's sources (NinepatchImpl.cpp) and adapted for Qt
847 const int pixmapWidth = pixmap.width();
848 const int pixmapHeight = pixmap.height();
849
850 if (bounds.isNull() || !pixmapWidth || !pixmapHeight)
851 return;
852
853 QPainter::RenderHints savedHints = painter->renderHints();
854
855 // The patchs doesn't need smooth transform !
856 painter->setRenderHints(QPainter::SmoothPixmapTransform, false);
857
858 QRectF dst;
859 QRectF src;
860
861 const qint32 x0 = m_chunkData.xDivs[0];
862 const qint32 y0 = m_chunkData.yDivs[0];
863 const quint8 numXDivs = m_chunkData.xDivs.size();
864 const quint8 numYDivs = m_chunkData.yDivs.size();
865 int i;
866 int j;
867 int colorIndex = 0;
868 quint32 color;
869 bool xIsStretchable;
870 const bool initialXIsStretchable = (x0 == 0);
871 bool yIsStretchable = (y0 == 0);
872 const int bitmapWidth = pixmap.width();
873 const int bitmapHeight = pixmap.height();
874
875 int *dstRights = static_cast<int *>(alloca((numXDivs + 1) * sizeof(int)));
876 bool dstRightsHaveBeenCached = false;
877
878 int numStretchyXPixelsRemaining = 0;
879 for (i = 0; i < numXDivs; i += 2)
880 numStretchyXPixelsRemaining += m_chunkData.xDivs[i + 1] - m_chunkData.xDivs[i];
881
882 int numFixedXPixelsRemaining = bitmapWidth - numStretchyXPixelsRemaining;
883 int numStretchyYPixelsRemaining = 0;
884 for (i = 0; i < numYDivs; i += 2)
885 numStretchyYPixelsRemaining += m_chunkData.yDivs[i + 1] - m_chunkData.yDivs[i];
886
887 int numFixedYPixelsRemaining = bitmapHeight - numStretchyYPixelsRemaining;
888 src.setTop(0);
889 dst.setTop(bounds.top());
890 // The first row always starts with the top being at y=0 and the bottom
891 // being either yDivs[1] (if yDivs[0]=0) of yDivs[0]. In the former case
892 // the first row is stretchable along the Y axis, otherwise it is fixed.
893 // The last row always ends with the bottom being bitmap.height and the top
894 // being either yDivs[numYDivs-2] (if yDivs[numYDivs-1]=bitmap.height) or
895 // yDivs[numYDivs-1]. In the former case the last row is stretchable along
896 // the Y axis, otherwise it is fixed.
897 //
898 // The first and last columns are similarly treated with respect to the X
899 // axis.
900 //
901 // The above is to help explain some of the special casing that goes on the
902 // code below.
903
904 // The initial yDiv and whether the first row is considered stretchable or
905 // not depends on whether yDiv[0] was zero or not.
906 for (j = yIsStretchable ? 1 : 0;
907 j <= numYDivs && src.top() < bitmapHeight;
908 j++, yIsStretchable = !yIsStretchable) {
909 src.setLeft(0);
910 dst.setLeft(bounds.left());
911 if (j == numYDivs) {
912 src.setBottom(bitmapHeight);
913 dst.setBottom(bounds.bottom());
914 } else {
915 src.setBottom(m_chunkData.yDivs[j]);
916 const int srcYSize = src.bottom() - src.top();
917 if (yIsStretchable) {
918 dst.setBottom(dst.top() + calculateStretch(bounds.bottom(), dst.top(),
919 srcYSize,
920 numStretchyYPixelsRemaining,
921 numFixedYPixelsRemaining));
922 numStretchyYPixelsRemaining -= srcYSize;
923 } else {
924 dst.setBottom(dst.top() + srcYSize);
925 numFixedYPixelsRemaining -= srcYSize;
926 }
927 }
928
929 xIsStretchable = initialXIsStretchable;
930 // The initial xDiv and whether the first column is considered
931 // stretchable or not depends on whether xDiv[0] was zero or not.
932 for (i = xIsStretchable ? 1 : 0;
933 i <= numXDivs && src.left() < bitmapWidth;
934 i++, xIsStretchable = !xIsStretchable) {
935 color = m_chunkData.colors[colorIndex++];
936 if (color != TRANSPARENT_COLOR)
937 color = NO_COLOR;
938 if (i == numXDivs) {
939 src.setRight(bitmapWidth);
940 dst.setRight(bounds.right());
941 } else {
942 src.setRight(m_chunkData.xDivs[i]);
943 if (dstRightsHaveBeenCached) {
944 dst.setRight(dstRights[i]);
945 } else {
946 const int srcXSize = src.right() - src.left();
947 if (xIsStretchable) {
948 dst.setRight(dst.left() + calculateStretch(bounds.right(), dst.left(),
949 srcXSize,
950 numStretchyXPixelsRemaining,
951 numFixedXPixelsRemaining));
952 numStretchyXPixelsRemaining -= srcXSize;
953 } else {
954 dst.setRight(dst.left() + srcXSize);
955 numFixedXPixelsRemaining -= srcXSize;
956 }
957 dstRights[i] = dst.right();
958 }
959 }
960 // If this horizontal patch is too small to be displayed, leave
961 // the destination left edge where it is and go on to the next patch
962 // in the source.
963 if (src.left() >= src.right()) {
964 src.setLeft(src.right());
965 continue;
966 }
967 // Make sure that we actually have room to draw any bits
968 if (dst.right() <= dst.left() || dst.bottom() <= dst.top()) {
969 goto nextDiv;
970 }
971 // If this patch is transparent, skip and don't draw.
972 if (color == TRANSPARENT_COLOR)
973 goto nextDiv;
974 if (color != NO_COLOR)
975 painter->fillRect(dst, QRgb(color));
976 else
977 painter->drawPixmap(dst, pixmap, src);
978nextDiv:
979 src.setLeft(src.right());
980 dst.setLeft(dst.right());
981 }
982 src.setTop(src.bottom());
983 dst.setTop(dst.bottom());
984 dstRightsHaveBeenCached = true;
985 }
986 painter->setRenderHints(savedHints);
987}
988
989QAndroidStyle::AndroidGradientDrawable::AndroidGradientDrawable(const QVariantMap &drawable,
990 QAndroidStyle::ItemType itemType)
991 : AndroidDrawable(drawable, itemType), m_orientation(TOP_BOTTOM)
992{
993 m_radius = drawable.value(QLatin1String("radius")).toInt();
994 if (m_radius < 0)
995 m_radius = 0;
996
997 QVariantList colors = drawable.value(QLatin1String("colors")).toList();
998 QVariantList positions = drawable.value(QLatin1String("positions")).toList();
999 int min = colors.size() < positions.size() ? colors.size() : positions.size();
1000 for (int i = 0; i < min; i++)
1001 m_gradient.setColorAt(positions.at(i).toDouble(), QRgb(colors.at(i).toInt()));
1002
1003 QByteArray orientation = drawable.value(QLatin1String("orientation")).toByteArray();
1004 if (orientation == "TOP_BOTTOM") // draw the gradient from the top to the bottom
1005 m_orientation = TOP_BOTTOM;
1006 else if (orientation == "TR_BL") // draw the gradient from the top-right to the bottom-left
1007 m_orientation = TR_BL;
1008 else if (orientation == "RIGHT_LEFT") // draw the gradient from the right to the left
1009 m_orientation = RIGHT_LEFT;
1010 else if (orientation == "BR_TL") // draw the gradient from the bottom-right to the top-left
1011 m_orientation = BR_TL;
1012 else if (orientation == "BOTTOM_TOP") // draw the gradient from the bottom to the top
1013 m_orientation = BOTTOM_TOP;
1014 else if (orientation == "BL_TR") // draw the gradient from the bottom-left to the top-right
1015 m_orientation = BL_TR;
1016 else if (orientation == "LEFT_RIGHT") // draw the gradient from the left to the right
1017 m_orientation = LEFT_RIGHT;
1018 else if (orientation == "TL_BR") // draw the gradient from the top-left to the bottom-right
1019 m_orientation = TL_BR;
1020 else
1021 qWarning("AndroidGradientDrawable: unknown orientation");
1022}
1023
1024QAndroidStyle::AndroidDrawableType QAndroidStyle::AndroidGradientDrawable::type() const
1025{
1026 return QAndroidStyle::Gradient;
1027}
1028
1029void QAndroidStyle::AndroidGradientDrawable::draw(QPainter *painter, const QStyleOption *opt) const
1030{
1031 const int width = opt->rect.width();
1032 const int height = opt->rect.height();
1033 switch (m_orientation) {
1034 case TOP_BOTTOM:
1035 // draw the gradient from the top to the bottom
1036 m_gradient.setStart(width / 2, 0);
1037 m_gradient.setFinalStop(width / 2, height);
1038 break;
1039 case TR_BL:
1040 // draw the gradient from the top-right to the bottom-left
1041 m_gradient.setStart(width, 0);
1042 m_gradient.setFinalStop(0, height);
1043 break;
1044 case RIGHT_LEFT:
1045 // draw the gradient from the right to the left
1046 m_gradient.setStart(width, height / 2);
1047 m_gradient.setFinalStop(0, height / 2);
1048 break;
1049 case BR_TL:
1050 // draw the gradient from the bottom-right to the top-left
1051 m_gradient.setStart(width, height);
1052 m_gradient.setFinalStop(0, 0);
1053 break;
1054 case BOTTOM_TOP:
1055 // draw the gradient from the bottom to the top
1056 m_gradient.setStart(width / 2, height);
1057 m_gradient.setFinalStop(width / 2, 0);
1058 break;
1059 case BL_TR:
1060 // draw the gradient from the bottom-left to the top-right
1061 m_gradient.setStart(0, height);
1062 m_gradient.setFinalStop(width, 0);
1063 break;
1064 case LEFT_RIGHT:
1065 // draw the gradient from the left to the right
1066 m_gradient.setStart(0, height / 2);
1067 m_gradient.setFinalStop(width, height / 2);
1068 break;
1069 case TL_BR:
1070 // draw the gradient from the top-left to the bottom-right
1071 m_gradient.setStart(0, 0);
1072 m_gradient.setFinalStop(width, height);
1073 break;
1074 }
1075
1076 const QBrush &oldBrush = painter->brush();
1077 const QPen oldPen = painter->pen();
1078 painter->setPen(Qt::NoPen);
1079 painter->setBrush(m_gradient);
1080 painter->drawRoundedRect(opt->rect, m_radius, m_radius);
1081 painter->setBrush(oldBrush);
1082 painter->setPen(oldPen);
1083}
1084
1085QSize QAndroidStyle::AndroidGradientDrawable::size() const
1086{
1087 return QSize(m_radius * 2, m_radius * 2);
1088}
1089
1090QAndroidStyle::AndroidClipDrawable::AndroidClipDrawable(const QVariantMap &drawable,
1091 QAndroidStyle::ItemType itemType)
1092 : AndroidDrawable(drawable, itemType)
1093{
1094 m_drawable = fromMap(drawable.value(QLatin1String("drawable")).toMap(), itemType);
1095 m_factor = 0;
1096 m_orientation = Qt::Horizontal;
1097}
1098
1099QAndroidStyle::AndroidClipDrawable::~AndroidClipDrawable()
1100{
1101 delete m_drawable;
1102}
1103
1104QAndroidStyle::AndroidDrawableType QAndroidStyle::AndroidClipDrawable::type() const
1105{
1106 return QAndroidStyle::Clip;
1107}
1108
1109void QAndroidStyle::AndroidClipDrawable::setFactor(double factor, Qt::Orientation orientation)
1110{
1111 m_factor = factor;
1112 m_orientation = orientation;
1113}
1114
1115void QAndroidStyle::AndroidClipDrawable::draw(QPainter *painter, const QStyleOption *opt) const
1116{
1117 QStyleOption copy(*opt);
1118 if (m_orientation == Qt::Horizontal)
1119 copy.rect.setWidth(copy.rect.width() * m_factor);
1120 else
1121 copy.rect.setHeight(copy.rect.height() * m_factor);
1122
1123 m_drawable->draw(painter, &copy);
1124}
1125
1126QAndroidStyle::AndroidStateDrawable::AndroidStateDrawable(const QVariantMap &drawable,
1127 QAndroidStyle::ItemType itemType)
1128 : AndroidDrawable(drawable, itemType)
1129{
1130 const QVariantList states = drawable.value(QLatin1String("stateslist")).toList();
1131 for (const QVariant &stateVariant : states) {
1132 QVariantMap state = stateVariant.toMap();
1133 const int s = extractState(state.value(QLatin1String("states")).toMap());
1134 if (-1 == s)
1135 continue;
1136 const AndroidDrawable *ad = fromMap(state.value(QLatin1String("drawable")).toMap(), itemType);
1137 if (!ad)
1138 continue;
1139 StateType item;
1140 item.first = s;
1141 item.second = ad;
1142 m_states<<item;
1143 }
1144}
1145
1146QAndroidStyle::AndroidStateDrawable::~AndroidStateDrawable()
1147{
1148 for (const StateType &type : std::as_const(m_states))
1149 delete type.second;
1150}
1151
1152QAndroidStyle::AndroidDrawableType QAndroidStyle::AndroidStateDrawable::type() const
1153{
1154 return QAndroidStyle::State;
1155}
1156
1157void QAndroidStyle::AndroidStateDrawable::draw(QPainter *painter, const QStyleOption *opt) const
1158{
1159 const AndroidDrawable *drawable = bestAndroidStateMatch(opt);
1160 if (drawable)
1161 drawable->draw(painter, opt);
1162}
1163QSize QAndroidStyle::AndroidStateDrawable::sizeImage(const QStyleOption *opt) const
1164{
1165 QSize s;
1166 const AndroidDrawable *drawable = bestAndroidStateMatch(opt);
1167 if (drawable)
1168 s = drawable->size();
1169 return s;
1170}
1171
1172const QAndroidStyle::AndroidDrawable * QAndroidStyle::AndroidStateDrawable::bestAndroidStateMatch(const QStyleOption *opt) const
1173{
1174 const AndroidDrawable *bestMatch = nullptr;
1175 if (!opt) {
1176 if (m_states.size())
1177 return m_states[0].second;
1178 return bestMatch;
1179 }
1180
1181 uint bestCost = 0xffff;
1182 for (const StateType & state : m_states) {
1183 if (int(opt->state) == state.first)
1184 return state.second;
1185 uint cost = 1;
1186
1187 int difference = int(opt->state^state.first);
1188
1189 if (difference & QStyle::State_Active)
1190 cost <<= 1;
1191
1192 if (difference & QStyle::State_Enabled)
1193 cost <<= 1;
1194
1195 if (difference & QStyle::State_Raised)
1196 cost <<= 1;
1197
1198 if (difference & QStyle::State_Sunken)
1199 cost <<= 1;
1200
1201 if (difference & QStyle::State_Off)
1202 cost <<= 1;
1203
1204 if (difference & QStyle::State_On)
1205 cost <<= 1;
1206
1207 if (difference & QStyle::State_HasFocus)
1208 cost <<= 1;
1209
1210 if (difference & QStyle::State_Selected)
1211 cost <<= 1;
1212
1213 if (cost < bestCost) {
1214 bestCost = cost;
1215 bestMatch = state.second;
1216 }
1217 }
1218 return bestMatch;
1219}
1220
1221int QAndroidStyle::AndroidStateDrawable::extractState(const QVariantMap &value)
1222{
1223 QStyle::State state = QStyle::State_Enabled | QStyle::State_Active;
1224 for (auto it = value.cbegin(), end = value.cend(); it != end; ++it) {
1225 const QString &key = it.key();
1226 bool val = it.value().toString() == QLatin1String("true");
1227 if (key == QLatin1String("enabled")) {
1228 state.setFlag(QStyle::State_Enabled, val);
1229 continue;
1230 }
1231
1232 if (key == QLatin1String("window_focused")) {
1233 state.setFlag(QStyle::State_Active, val);
1234 continue;
1235 }
1236
1237 if (key == QLatin1String("focused")) {
1238 state.setFlag(QStyle::State_HasFocus, val);
1239 continue;
1240 }
1241
1242 if (key == QLatin1String("checked")) {
1243 state |= val ? QStyle::State_On : QStyle::State_Off;
1244 continue;
1245 }
1246
1247 if (key == QLatin1String("pressed")) {
1248 state |= val ? QStyle::State_Sunken : QStyle::State_Raised;
1249 continue;
1250 }
1251
1252 if (key == QLatin1String("selected")) {
1253 state.setFlag(QStyle::State_Selected, val);
1254 continue;
1255 }
1256
1257 if (key == QLatin1String("active")) {
1258 state.setFlag(QStyle::State_Active, val);
1259 continue;
1260 }
1261
1262 if (key == QLatin1String("multiline"))
1263 return 0;
1264
1265 if (key == QLatin1String("background") && val)
1266 return -1;
1267 }
1268 return static_cast<int>(state);
1269}
1270
1271void QAndroidStyle::AndroidStateDrawable::setPaddingLeftToSizeWidth()
1272{
1273 for (const StateType &type : std::as_const(m_states))
1274 const_cast<AndroidDrawable *>(type.second)->setPaddingLeftToSizeWidth();
1275}
1276
1277QAndroidStyle::AndroidLayerDrawable::AndroidLayerDrawable(const QVariantMap &drawable,
1278 QAndroidStyle::ItemType itemType)
1279 : AndroidDrawable(drawable, itemType)
1280{
1281 m_id = 0;
1282 m_factor = 1;
1283 m_orientation = Qt::Horizontal;
1284 const QVariantList layers = drawable.value(QLatin1String("layers")).toList();
1285 for (const QVariant &layer : layers) {
1286 QVariantMap layerMap = layer.toMap();
1287 AndroidDrawable *ad = fromMap(layerMap, itemType);
1288 if (ad) {
1289 LayerType l;
1290 l.second = ad;
1291 l.first = layerMap.value(QLatin1String("id")).toInt();
1292 m_layers << l;
1293 }
1294 }
1295}
1296
1297QAndroidStyle::AndroidLayerDrawable::~AndroidLayerDrawable()
1298{
1299 for (const LayerType &layer : std::as_const(m_layers))
1300 delete layer.second;
1301}
1302
1303QAndroidStyle::AndroidDrawableType QAndroidStyle::AndroidLayerDrawable::type() const
1304{
1305 return QAndroidStyle::Layer;
1306}
1307
1308void QAndroidStyle::AndroidLayerDrawable::setFactor(int id, double factor, Qt::Orientation orientation)
1309{
1310 m_id = id;
1311 m_factor = factor;
1312 m_orientation = orientation;
1313}
1314
1315void QAndroidStyle::AndroidLayerDrawable::draw(QPainter *painter, const QStyleOption *opt) const
1316{
1317 for (const LayerType &layer : m_layers) {
1318 if (layer.first == m_id) {
1319 QStyleOption copy(*opt);
1320 if (m_orientation == Qt::Horizontal)
1321 copy.rect.setWidth(copy.rect.width() * m_factor);
1322 else
1323 copy.rect.setHeight(copy.rect.height() * m_factor);
1324 layer.second->draw(painter, &copy);
1325 } else {
1326 layer.second->draw(painter, opt);
1327 }
1328 }
1329}
1330
1331QAndroidStyle::AndroidDrawable *QAndroidStyle::AndroidLayerDrawable::layer(int id) const
1332{
1333 for (const LayerType &layer : m_layers) {
1334 if (layer.first == id)
1335 return layer.second;
1336 }
1337 return 0;
1338}
1339
1340QSize QAndroidStyle::AndroidLayerDrawable::size() const
1341{
1342 QSize sz;
1343 for (const LayerType &layer : m_layers)
1344 sz = sz.expandedTo(layer.second->size());
1345 return sz;
1346}
1347
1348QAndroidStyle::AndroidControl::AndroidControl(const QVariantMap &control,
1349 QAndroidStyle::ItemType itemType)
1350{
1351 QVariantMap::const_iterator it = control.find(QLatin1String("View_background"));
1352 if (it != control.end())
1353 m_background = AndroidDrawable::fromMap(it.value().toMap(), itemType);
1354 else
1355 m_background = 0;
1356
1357 it = control.find(QLatin1String("View_minWidth"));
1358 if (it != control.end())
1359 m_minSize.setWidth(it.value().toInt());
1360
1361 it = control.find(QLatin1String("View_minHeight"));
1362 if (it != control.end())
1363 m_minSize.setHeight(it.value().toInt());
1364
1365 it = control.find(QLatin1String("View_maxWidth"));
1366 if (it != control.end())
1367 m_maxSize.setWidth(it.value().toInt());
1368
1369 it = control.find(QLatin1String("View_maxHeight"));
1370 if (it != control.end())
1371 m_maxSize.setHeight(it.value().toInt());
1372}
1373
1374QAndroidStyle::AndroidControl::~AndroidControl()
1375{
1376 delete m_background;
1377}
1378
1379void QAndroidStyle::AndroidControl::drawControl(const QStyleOption *opt, QPainter *p, const QWidget * /* w */)
1380{
1381 if (m_background) {
1382 m_background->draw(p, opt);
1383 } else {
1384 if (const QStyleOptionFrame *frame = qstyleoption_cast<const QStyleOptionFrame *>(opt)) {
1385 if ((frame->state & State_Sunken) || (frame->state & State_Raised)) {
1386 qDrawShadePanel(p, frame->rect, frame->palette, frame->state & State_Sunken,
1387 frame->lineWidth);
1388 } else {
1389 qDrawPlainRect(p, frame->rect, frame->palette.windowText().color(), frame->lineWidth);
1390 }
1391 } else {
1392 if (const QStyleOptionFocusRect *fropt = qstyleoption_cast<const QStyleOptionFocusRect *>(opt)) {
1393 QColor bg = fropt->backgroundColor;
1394 QPen oldPen = p->pen();
1395 if (bg.isValid()) {
1396 int h, s, v;
1397 bg.getHsv(&h, &s, &v);
1398 if (v >= 128)
1399 p->setPen(Qt::black);
1400 else
1401 p->setPen(Qt::white);
1402 } else {
1403 p->setPen(opt->palette.windowText().color());
1404 }
1405 QRect focusRect = opt->rect.adjusted(1, 1, -1, -1);
1406 p->drawRect(focusRect.adjusted(0, 0, -1, -1)); //draw pen inclusive
1407 p->setPen(oldPen);
1408 } else {
1409 p->fillRect(opt->rect, opt->palette.window());
1410 }
1411 }
1412 }
1413}
1414
1415QRect QAndroidStyle::AndroidControl::subElementRect(QStyle::SubElement /* subElement */,
1416 const QStyleOption *option,
1417 const QWidget * /* widget */) const
1418{
1419 if (const AndroidDrawable *drawable = backgroundDrawable()) {
1420 if (drawable->type() == State)
1421 drawable = static_cast<const AndroidStateDrawable *>(backgroundDrawable())->bestAndroidStateMatch(option);
1422
1423 const QMargins &padding = drawable->padding();
1424
1425 QRect r = option->rect.adjusted(padding.left(), padding.top(),
1426 -padding.right(), -padding.bottom());
1427
1428 if (r.width() < m_minSize.width())
1429 r.setWidth(m_minSize.width());
1430
1431 if (r.height() < m_minSize.height())
1432 r.setHeight(m_minSize.height());
1433
1434 return visualRect(option->direction, option->rect, r);
1435 }
1436 return option->rect;
1437}
1438
1439QRect QAndroidStyle::AndroidControl::subControlRect(const QStyleOptionComplex *option,
1440 QStyle::SubControl /*sc*/,
1441 const QWidget *widget) const
1442{
1443 return subElementRect(QStyle::SE_CustomBase, option, widget);
1444}
1445
1446QSize QAndroidStyle::AndroidControl::sizeFromContents(const QStyleOption *opt,
1447 const QSize &contentsSize,
1448 const QWidget * /* w */) const
1449{
1450 QSize sz;
1451 if (const AndroidDrawable *drawable = backgroundDrawable()) {
1452
1453 if (drawable->type() == State)
1454 drawable = static_cast<const AndroidStateDrawable*>(backgroundDrawable())->bestAndroidStateMatch(opt);
1455 const QMargins &padding = drawable->padding();
1456 sz.setWidth(padding.left() + padding.right());
1457 sz.setHeight(padding.top() + padding.bottom());
1458 if (sz.isEmpty())
1459 sz = drawable->size();
1460 }
1461 sz += contentsSize;
1462 if (contentsSize.height() < opt->fontMetrics.height())
1463 sz.setHeight(sz.height() + (opt->fontMetrics.height() - contentsSize.height()));
1464 if (sz.height() < m_minSize.height())
1465 sz.setHeight(m_minSize.height());
1466 if (sz.width() < m_minSize.width())
1467 sz.setWidth(m_minSize.width());
1468 return sz;
1469}
1470
1471QMargins QAndroidStyle::AndroidControl::padding()
1472{
1473 if (const AndroidDrawable *drawable = m_background) {
1474 if (drawable->type() == State)
1475 drawable = static_cast<const AndroidStateDrawable *>(m_background)->bestAndroidStateMatch(0);
1476 return drawable->padding();
1477 }
1478 return QMargins();
1479}
1480
1481QSize QAndroidStyle::AndroidControl::size(const QStyleOption *option)
1482{
1483 if (const AndroidDrawable *drawable = backgroundDrawable()) {
1484 if (drawable->type() == State)
1485 drawable = static_cast<const AndroidStateDrawable *>(backgroundDrawable())->bestAndroidStateMatch(option);
1486 return drawable->size();
1487 }
1488 return QSize();
1489}
1490
1491const QAndroidStyle::AndroidDrawable *QAndroidStyle::AndroidControl::backgroundDrawable() const
1492{
1493 return m_background;
1494}
1495
1496QAndroidStyle::AndroidCompoundButtonControl::AndroidCompoundButtonControl(const QVariantMap &control,
1497 ItemType itemType)
1498 : AndroidControl(control, itemType)
1499{
1500 QVariantMap::const_iterator it = control.find(QLatin1String("CompoundButton_button"));
1501 if (it != control.end()) {
1502 m_button = AndroidDrawable::fromMap(it.value().toMap(), itemType);
1503 const_cast<AndroidDrawable *>(m_button)->setPaddingLeftToSizeWidth();
1504 } else {
1505 m_button = 0;
1506 }
1507}
1508
1509QAndroidStyle::AndroidCompoundButtonControl::~AndroidCompoundButtonControl()
1510{
1511 delete m_button;
1512}
1513
1514void QAndroidStyle::AndroidCompoundButtonControl::drawControl(const QStyleOption *opt,
1515 QPainter *p,
1516 const QWidget *w)
1517{
1518 AndroidControl::drawControl(opt, p, w);
1519 if (m_button)
1520 m_button->draw(p, opt);
1521}
1522
1523QMargins QAndroidStyle::AndroidCompoundButtonControl::padding()
1524{
1525 if (m_button)
1526 return m_button->padding();
1527 return AndroidControl::padding();
1528}
1529
1530QSize QAndroidStyle::AndroidCompoundButtonControl::size(const QStyleOption *option)
1531{
1532 if (m_button) {
1533 if (m_button->type() == State)
1534 return static_cast<const AndroidStateDrawable *>(m_button)->bestAndroidStateMatch(option)->size();
1535 return m_button->size();
1536 }
1537 return AndroidControl::size(option);
1538}
1539
1540const QAndroidStyle::AndroidDrawable * QAndroidStyle::AndroidCompoundButtonControl::backgroundDrawable() const
1541{
1542 return m_background ? m_background : m_button;
1543}
1544
1545QAndroidStyle::AndroidProgressBarControl::AndroidProgressBarControl(const QVariantMap &control,
1546 ItemType itemType)
1547 : AndroidControl(control, itemType)
1548{
1549 QVariantMap::const_iterator it = control.find(QLatin1String("ProgressBar_indeterminateDrawable"));
1550 if (it != control.end())
1551 m_indeterminateDrawable = AndroidDrawable::fromMap(it.value().toMap(), itemType);
1552 else
1553 m_indeterminateDrawable = 0;
1554
1555 it = control.find(QLatin1String("ProgressBar_progressDrawable"));
1556 if (it != control.end())
1557 m_progressDrawable = AndroidDrawable::fromMap(it.value().toMap(), itemType);
1558 else
1559 m_progressDrawable = 0;
1560
1561 it = control.find(QLatin1String("ProgressBar_progress_id"));
1562 if (it != control.end())
1563 m_progressId = it.value().toInt();
1564
1565 it = control.find(QLatin1String("ProgressBar_secondaryProgress_id"));
1566 if (it != control.end())
1567 m_secondaryProgress_id = it.value().toInt();
1568
1569 it = control.find(QLatin1String("ProgressBar_minWidth"));
1570 if (it != control.end())
1571 m_minSize.setWidth(it.value().toInt());
1572
1573 it = control.find(QLatin1String("ProgressBar_minHeight"));
1574 if (it != control.end())
1575 m_minSize.setHeight(it.value().toInt());
1576
1577 it = control.find(QLatin1String("ProgressBar_maxWidth"));
1578 if (it != control.end())
1579 m_maxSize.setWidth(it.value().toInt());
1580
1581 it = control.find(QLatin1String("ProgressBar_maxHeight"));
1582 if (it != control.end())
1583 m_maxSize.setHeight(it.value().toInt());
1584}
1585
1586QAndroidStyle::AndroidProgressBarControl::~AndroidProgressBarControl()
1587{
1588 delete m_progressDrawable;
1589 delete m_indeterminateDrawable;
1590}
1591
1592void QAndroidStyle::AndroidProgressBarControl::drawControl(const QStyleOption *option, QPainter *p, const QWidget * /* w */)
1593{
1594 if (!m_progressDrawable)
1595 return;
1596
1597 if (const QStyleOptionProgressBar *pb = qstyleoption_cast<const QStyleOptionProgressBar *>(option)) {
1598 if (m_progressDrawable->type() == QAndroidStyle::Layer) {
1599 const double fraction = double(qint64(pb->progress) - pb->minimum) / (qint64(pb->maximum) - pb->minimum);
1600 QAndroidStyle::AndroidDrawable *clipDrawable = static_cast<QAndroidStyle::AndroidLayerDrawable *>(m_progressDrawable)->layer(m_progressId);
1601 const Qt::Orientation orientation = pb->state & QStyle::State_Horizontal ? Qt::Horizontal : Qt::Vertical;
1602 if (clipDrawable->type() == QAndroidStyle::Clip)
1603 static_cast<AndroidClipDrawable *>(clipDrawable)->setFactor(fraction, orientation);
1604 else
1605 static_cast<AndroidLayerDrawable *>(m_progressDrawable)->setFactor(m_progressId, fraction, orientation);
1606 }
1607 m_progressDrawable->draw(p, option);
1608 }
1609}
1610
1611QRect QAndroidStyle::AndroidProgressBarControl::subElementRect(QStyle::SubElement subElement,
1612 const QStyleOption *option,
1613 const QWidget *widget) const
1614{
1615 if (const QStyleOptionProgressBar *progressBarOption =
1616 qstyleoption_cast<const QStyleOptionProgressBar *>(option)) {
1617 const bool horizontal = progressBarOption->state & QStyle::State_Horizontal;
1618 if (!m_background)
1619 return option->rect;
1620
1621 QMargins padding = m_background->padding();
1622 QRect p(padding.left(), padding.top(), padding.right() - padding.left(), padding.bottom() - padding.top());
1623 padding = m_indeterminateDrawable->padding();
1624 p |= QRect(padding.left(), padding.top(), padding.right() - padding.left(), padding.bottom() - padding.top());
1625 padding = m_progressDrawable->padding();
1626 p |= QRect(padding.left(), padding.top(), padding.right() - padding.left(), padding.bottom() - padding.top());
1627 QRect r = option->rect.adjusted(p.left(), p.top(), -p.right(), -p.bottom());
1628
1629 if (horizontal) {
1630 if (r.height()<m_minSize.height())
1631 r.setHeight(m_minSize.height());
1632
1633 if (r.height()>m_maxSize.height())
1634 r.setHeight(m_maxSize.height());
1635 } else {
1636 if (r.width()<m_minSize.width())
1637 r.setWidth(m_minSize.width());
1638
1639 if (r.width()>m_maxSize.width())
1640 r.setWidth(m_maxSize.width());
1641 }
1642 return visualRect(option->direction, option->rect, r);
1643 }
1644 return AndroidControl::subElementRect(subElement, option, widget);
1645}
1646
1647QSize QAndroidStyle::AndroidProgressBarControl::sizeFromContents(const QStyleOption *opt,
1648 const QSize &contentsSize,
1649 const QWidget * /* w */) const
1650{
1651 QSize sz(contentsSize);
1652 if (sz.height() < m_minSize.height())
1653 sz.setHeight(m_minSize.height());
1654 if (sz.width() < m_minSize.width())
1655 sz.setWidth(m_minSize.width());
1656
1657 if (const QStyleOptionProgressBar *progressBarOption =
1658 qstyleoption_cast<const QStyleOptionProgressBar *>(opt)) {
1659 if (progressBarOption->state & QStyle::State_Horizontal) {
1660 if (sz.width() > m_maxSize.width())
1661 sz.setWidth(m_maxSize.width());
1662 } else {
1663 if (sz.height() > m_maxSize.height())
1664 sz.setHeight(m_maxSize.height());
1665 }
1666 }
1667 return contentsSize;
1668}
1669
1670QAndroidStyle::AndroidSeekBarControl::AndroidSeekBarControl(const QVariantMap &control,
1671 ItemType itemType)
1672 : AndroidProgressBarControl(control, itemType)
1673{
1674 QVariantMap::const_iterator it = control.find(QLatin1String("SeekBar_thumb"));
1675 if (it != control.end())
1676 m_seekBarThumb = AndroidDrawable::fromMap(it.value().toMap(), itemType);
1677 else
1678 m_seekBarThumb = 0;
1679}
1680
1681QAndroidStyle::AndroidSeekBarControl::~AndroidSeekBarControl()
1682{
1683 delete m_seekBarThumb;
1684}
1685
1686void QAndroidStyle::AndroidSeekBarControl::drawControl(const QStyleOption *option,
1687 QPainter *p,
1688 const QWidget * /* w */)
1689{
1690 if (!m_seekBarThumb || !m_progressDrawable)
1691 return;
1692
1693 if (const QStyleOptionSlider *styleOption =
1694 qstyleoption_cast<const QStyleOptionSlider *>(option)) {
1695 double factor = double(styleOption->sliderPosition - styleOption->minimum)
1696 / double(styleOption->maximum - styleOption->minimum);
1697
1698 // Android does not have a vertical slider. To support the vertical orientation, we rotate
1699 // the painter and pretend that we are horizontal.
1700 if (styleOption->orientation == Qt::Vertical)
1701 factor = 1 - factor;
1702
1703 if (m_progressDrawable->type() == QAndroidStyle::Layer) {
1704 QAndroidStyle::AndroidDrawable *clipDrawable = static_cast<QAndroidStyle::AndroidLayerDrawable *>(m_progressDrawable)->layer(m_progressId);
1705 if (clipDrawable->type() == QAndroidStyle::Clip)
1706 static_cast<QAndroidStyle::AndroidClipDrawable *>(clipDrawable)->setFactor(factor, Qt::Horizontal);
1707 else
1708 static_cast<QAndroidStyle::AndroidLayerDrawable *>(m_progressDrawable)->setFactor(m_progressId, factor, Qt::Horizontal);
1709 }
1710 const AndroidDrawable *drawable = m_seekBarThumb;
1711 if (drawable->type() == State)
1712 drawable = static_cast<const QAndroidStyle::AndroidStateDrawable *>(m_seekBarThumb)->bestAndroidStateMatch(option);
1713 QStyleOption copy(*option);
1714
1715 p->save();
1716
1717 if (styleOption->orientation == Qt::Vertical) {
1718 // rotate the painter, and transform the rectangle to match
1719 p->rotate(90);
1720 copy.rect = QRect(copy.rect.y(), copy.rect.x() - copy.rect.width(), copy.rect.height(), copy.rect.width());
1721 }
1722
1723 copy.rect.setHeight(m_progressDrawable->size().height());
1724 copy.rect.setWidth(copy.rect.width() - drawable->size().width());
1725 const int yTranslate = abs(drawable->size().height() - copy.rect.height()) / 2;
1726 copy.rect.translate(drawable->size().width() / 2, yTranslate);
1727 m_progressDrawable->draw(p, &copy);
1728 int pos = copy.rect.width() * factor - drawable->size().width() / 2;
1729 copy.rect.translate(pos, -yTranslate);
1730 copy.rect.setSize(drawable->size());
1731 m_seekBarThumb->draw(p, &copy);
1732
1733 p->restore();
1734 }
1735}
1736
1737QSize QAndroidStyle::AndroidSeekBarControl::sizeFromContents(const QStyleOption *opt,
1738 const QSize &contentsSize,
1739 const QWidget *w) const
1740{
1741 QSize sz = AndroidProgressBarControl::sizeFromContents(opt, contentsSize, w);
1742 if (!m_seekBarThumb)
1743 return sz;
1744 const AndroidDrawable *drawable = m_seekBarThumb;
1745 if (drawable->type() == State)
1746 drawable = static_cast<const QAndroidStyle::AndroidStateDrawable *>(m_seekBarThumb)->bestAndroidStateMatch(opt);
1747 return sz.expandedTo(drawable->size());
1748}
1749
1750QRect QAndroidStyle::AndroidSeekBarControl::subControlRect(const QStyleOptionComplex *option,
1751 SubControl sc,
1752 const QWidget * /* widget */) const
1753{
1754 const QStyleOptionSlider *styleOption =
1755 qstyleoption_cast<const QStyleOptionSlider *>(option);
1756
1757 if (m_seekBarThumb && sc == SC_SliderHandle && styleOption) {
1758 const AndroidDrawable *drawable = m_seekBarThumb;
1759 if (drawable->type() == State)
1760 drawable = static_cast<const QAndroidStyle::AndroidStateDrawable *>(m_seekBarThumb)->bestAndroidStateMatch(option);
1761
1762 QRect r(option->rect);
1763 double factor = double(styleOption->sliderPosition - styleOption->minimum)
1764 / (styleOption->maximum - styleOption->minimum);
1765 if (styleOption->orientation == Qt::Vertical) {
1766 int pos = option->rect.height() * (1 - factor) - double(drawable->size().height() / 2);
1767 r.setY(r.y() + pos);
1768 } else {
1769 int pos = option->rect.width() * factor - double(drawable->size().width() / 2);
1770 r.setX(r.x() + pos);
1771 }
1772 r.setSize(drawable->size());
1773 return r;
1774 }
1775 return option->rect;
1776}
1777
1778QAndroidStyle::AndroidSpinnerControl::AndroidSpinnerControl(const QVariantMap &control,
1779 QAndroidStyle::ItemType itemType)
1780 : AndroidControl(control, itemType)
1781{}
1782
1783QRect QAndroidStyle::AndroidSpinnerControl::subControlRect(const QStyleOptionComplex *option,
1784 SubControl sc,
1785 const QWidget *widget) const
1786{
1787 if (sc == QStyle::SC_ComboBoxListBoxPopup)
1788 return option->rect;
1789 if (sc == QStyle::SC_ComboBoxArrow) {
1790 const QRect editField = subControlRect(option, QStyle::SC_ComboBoxEditField, widget);
1791 return QRect(editField.topRight(), QSize(option->rect.width() - editField.width(), option->rect.height()));
1792 }
1793 return AndroidControl::subControlRect(option, sc, widget);
1794}
1795
1796QT_END_NAMESPACE
const quint32 TRANSPARENT_COLOR
const quint32 NO_COLOR