File indexing completed on 2024-04-14 15:37:16

0001 /*
0002 *  Copyright 2017-2018 Michail Vourlakos <mvourlakos@gmail.com>
0003 *
0004 *  This file is part of Latte-Dock
0005 *
0006 *  Latte-Dock is free software; you can redistribute it and/or
0007 *  modify it under the terms of the GNU General Public License as
0008 *  published by the Free Software Foundation; either version 2 of
0009 *  the License, or (at your option) any later version.
0010 *
0011 *  Latte-Dock is distributed in the hope that it will be useful,
0012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 *  GNU General Public License for more details.
0015 *
0016 *  You should have received a copy of the GNU General Public License
0017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 
0020 #include "colorcmbboxitemdelegate.h"
0021 
0022 // Qt
0023 #include <QApplication>
0024 #include <QDebug>
0025 #include <QPainter>
0026 #include <QString>
0027 
0028 ColorCmbBoxItemDelegate::ColorCmbBoxItemDelegate(QObject *parent, QString iconsPath)
0029     : QAbstractItemDelegate(parent),
0030       m_iconsPath(iconsPath)
0031 {
0032 }
0033 
0034 QSize ColorCmbBoxItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
0035 {
0036     return QSize(option.rect.width(), 50);
0037 }
0038 
0039 void ColorCmbBoxItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0040 {
0041     QStyleOptionViewItem myOption = option;
0042     QVariant value = index.data(Qt::DisplayRole);
0043     QVariant data = index.data(Qt::UserRole);
0044 
0045     QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
0046 
0047     if (value.isValid()) {
0048         QString dataStr = data.toString();
0049         QString valueStr = value.toString();
0050 
0051         if ((dataStr == "select_image") || (dataStr == "text_color")) {
0052             myOption.text = valueStr;
0053             QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
0054         } else {
0055             QString colorPath = valueStr.startsWith("/") ? valueStr : m_iconsPath + valueStr + "print.jpg";
0056             QBrush colorBrush;
0057             colorBrush.setTextureImage(QImage(colorPath).scaled(QSize(50, 50)));
0058 
0059             painter->setBrush(colorBrush);
0060             painter->drawRect(option.rect - QMargins(5, 5, 5, 5));
0061         }
0062     }
0063 
0064     painter->restore();
0065 }
0066