1 /*
2     SPDX-FileCopyrightText: 2021 Michail Vourlakos <mvourlakos@gmail.com>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #include "schemescombobox.h"
7 
8 // local
9 #include "../generic/generictools.h"
10 
11 // Qt
12 #include <QApplication>
13 #include <QDebug>
14 #include <QPalette>
15 #include <QStyleOptionComboBox>
16 #include <QStylePainter>
17 
18 namespace Latte {
19 namespace Settings {
20 
21 const int MARGIN = 2;
22 const int VERTMARGIN = 3;
23 
SchemesComboBox(QWidget * parent)24 SchemesComboBox::SchemesComboBox(QWidget *parent)
25     : QComboBox (parent)
26 {
27 }
28 
backgroundColor() const29 QColor SchemesComboBox::backgroundColor() const
30 {
31     return m_backgroundColor;
32 }
33 
setBackgroundColor(const QColor & color)34 void SchemesComboBox::setBackgroundColor(const QColor &color)
35 {
36     if (m_backgroundColor == color) {
37         return;
38     }
39 
40     m_backgroundColor = color;
41     update();
42 }
43 
textColor() const44 QColor SchemesComboBox::textColor() const
45 {
46     return m_textColor;
47 }
48 
setTextColor(const QColor & color)49 void SchemesComboBox::setTextColor(const QColor &color)
50 {
51     if (m_textColor == color) {
52         return;
53     }
54 
55     m_textColor = color;
56     update();
57 }
58 
59 
paintEvent(QPaintEvent * event)60 void SchemesComboBox::paintEvent(QPaintEvent *event)
61 {
62     QStylePainter painter(this);
63     painter.setPen(palette().color(QPalette::Text));
64 
65     // draw the combobox frame, focusrect and selected etc.
66     QStyleOptionComboBox opt;
67     initStyleOption(&opt);
68 
69     // background
70     painter.drawComplexControl(QStyle::CC_ComboBox, opt);
71 
72     // icon
73     QRect remained = Latte::remainedFromColorSchemeIcon(opt, Qt::AlignLeft, 3, 5);
74     Latte::drawColorSchemeIcon(&painter, opt, m_textColor, m_backgroundColor, Qt::AlignLeft, 7, 6);
75     opt.rect = remained;
76 
77     // adjust text place, move it a bit to the left
78     QRect textRect;
79     int textnegativepad = MARGIN + 1;
80     if (qApp->layoutDirection() == Qt::LeftToRight) {
81         textRect = QRect(remained.x() - textnegativepad, opt.rect.y(), remained.width() + 2*textnegativepad, opt.rect.height());
82     } else {
83         textRect = QRect(remained.x(), opt.rect.y(), remained.width() + 2 * textnegativepad, opt.rect.height());
84     }
85     opt.rect = textRect;
86 
87     // text
88     painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
89 
90 }
91 
92 
93 }
94 }
95