File indexing completed on 2024-02-18 05:31:08

0001 /*
0002     SPDX-FileCopyrightText: 2021 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "layoutscombobox.h"
0007 
0008 // local
0009 #include "generictools.h"
0010 
0011 // Qt
0012 #include <QApplication>
0013 #include <QDebug>
0014 #include <QPalette>
0015 #include <QStyleOptionComboBox>
0016 #include <QStylePainter>
0017 
0018 namespace Latte {
0019 namespace Settings {
0020 
0021 const int MARGIN = 2;
0022 const int VERTMARGIN = 3;
0023 
0024 LayoutsComboBox::LayoutsComboBox(QWidget *parent)
0025     : QComboBox (parent)
0026 {
0027 }
0028 
0029 Latte::Data::LayoutIcon LayoutsComboBox::layoutIcon() const
0030 {
0031     return m_layoutIcon;
0032 }
0033 
0034 void LayoutsComboBox::setLayoutIcon(const Latte::Data::LayoutIcon &icon)
0035 {
0036     if (m_layoutIcon == icon) {
0037         return;
0038     }
0039 
0040     m_layoutIcon = icon;
0041     update();
0042 }
0043 
0044 void LayoutsComboBox::paintEvent(QPaintEvent *event)
0045 {
0046     QStylePainter painter(this);
0047     painter.setPen(palette().color(QPalette::Text));
0048 
0049     // draw the combobox frame, focusrect and selected etc.
0050     QStyleOptionComboBox opt;
0051     initStyleOption(&opt);
0052 
0053     // background
0054     painter.drawComplexControl(QStyle::CC_ComboBox, opt);
0055 
0056     // icon
0057     QRect remained = Latte::remainedFromLayoutIcon(opt, Qt::AlignLeft, 3, 5);
0058     Latte::drawLayoutIcon(&painter, opt, m_layoutIcon.isBackgroundFile, m_layoutIcon.name, Qt::AlignLeft, 7, 6);
0059     opt.rect = remained;
0060 
0061     // adjust text place, move it a bit to the left
0062     QRect textRect;
0063     int textnegativepad = MARGIN + 1;
0064     if (qApp->layoutDirection() == Qt::LeftToRight) {
0065         textRect = QRect(remained.x() - textnegativepad, opt.rect.y(), remained.width() + 2*textnegativepad, opt.rect.height());
0066     } else {
0067         textRect = QRect(remained.x(), opt.rect.y(), remained.width() + 2 * textnegativepad, opt.rect.height());
0068     }
0069     opt.rect = textRect;
0070 
0071     // text
0072     painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
0073 
0074 }
0075 
0076 
0077 }
0078 }