File indexing completed on 2024-12-08 10:56:41
0001 /* 0002 SPDX-FileCopyrightText: 2020 Michail Vourlakos <mvourlakos@gmail.com> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "colorcmbitemdelegate.h" 0007 0008 // local 0009 #include "../colorsmodel.h" 0010 #include "../../generic/generictools.h" 0011 0012 // Qt 0013 #include <QDebug> 0014 #include <QImage> 0015 #include <QMargins> 0016 #include <QPainter> 0017 #include <QPainterPath> 0018 #include <QString> 0019 0020 namespace Latte { 0021 namespace Settings { 0022 namespace Details { 0023 namespace Delegate { 0024 0025 ColorCmbBoxItem::ColorCmbBoxItem(QObject *parent) 0026 : QAbstractItemDelegate(parent) 0027 { 0028 m_pattern = new Widget::PatternWidget(); 0029 } 0030 0031 ColorCmbBoxItem::~ColorCmbBoxItem() 0032 { 0033 m_pattern->deleteLater(); 0034 } 0035 0036 QSize ColorCmbBoxItem::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const 0037 { 0038 return QSize(option.rect.width(), 60); 0039 } 0040 0041 void ColorCmbBoxItem::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const 0042 { 0043 QStyleOptionViewItem myOption = option; 0044 QString name = index.data(Model::Colors::NAMEROLE).toString(); 0045 QString colorPath = index.data(Model::Colors::PATHROLE).toString(); 0046 QString textColor = index.data(Model::Colors::TEXTCOLORROLE).toString(); 0047 0048 bool selected = Latte::isSelected(option); 0049 painter->setRenderHint(QPainter::Antialiasing, true); 0050 0051 //! draw background 0052 if (selected) { 0053 QPalette::ColorRole selectedColorRole = QPalette::Highlight; 0054 QColor selectedColor = option.palette.brush(Latte::colorGroup(option), selectedColorRole).color(); 0055 0056 QPainterPath back; 0057 back.addRect(option.rect); 0058 painter->fillPath(back, selectedColor); 0059 } 0060 0061 //! draw pattern control 0062 myOption.rect -= QMargins(4,4,4,4); 0063 0064 m_pattern->setText(name); 0065 m_pattern->setBackground(colorPath); 0066 m_pattern->setTextColor(textColor); 0067 m_pattern->resize(myOption.rect.size()); 0068 0069 myOption.rect.moveTo(option.rect.x() + 6, option.rect.y() + 6); 0070 m_pattern->render(painter, myOption.rect.topLeft(), QRegion(), QWidget::DrawChildren ); 0071 } 0072 0073 } 0074 } 0075 } 0076 } 0077