File indexing completed on 2024-04-21 16:17:10

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 "colorcmbboxdelegate.h"
0021 
0022 // local
0023 #include "colorcmbboxitemdelegate.h"
0024 #include "../settingsdialog.h"
0025 
0026 // Qt
0027 #include <QComboBox>
0028 #include <QDebug>
0029 #include <QDir>
0030 #include <QFileDialog>
0031 #include <QWidget>
0032 #include <QModelIndex>
0033 #include <QApplication>
0034 #include <QPainter>
0035 #include <QString>
0036 
0037 // KDE
0038 #include <KLocalizedString>
0039 
0040 ColorCmbBoxDelegate::ColorCmbBoxDelegate(QObject *parent, QString iconsPath, QStringList colors)
0041     : QItemDelegate(parent),
0042       m_parent(parent),
0043       m_iconsPath(iconsPath),
0044       Colors(colors)
0045 {
0046 }
0047 
0048 QWidget *ColorCmbBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
0049 {
0050     QComboBox *editor = new QComboBox(parent);
0051     editor->setItemDelegate(new ColorCmbBoxItemDelegate(editor, m_iconsPath));
0052 
0053     for (unsigned int i = 0; i < Colors.count(); ++i) {
0054         if (Colors[i] != "sepia") {
0055             QPixmap pixmap(50, 50);
0056             pixmap.fill(QColor(Colors[i]));
0057             QIcon icon(pixmap);
0058 
0059             editor->addItem(icon, Colors[i]);
0060         }
0061     }
0062 
0063     QString value = index.model()->data(index, Qt::BackgroundRole).toString();
0064 
0065     const QModelIndex &indexOriginal = index;
0066 
0067     bool showTextColor{false};
0068 
0069     //! add the background if exists
0070     if (value.startsWith("/")) {
0071         QIcon icon(value);
0072         editor->addItem(icon, value);
0073         showTextColor = true;
0074     }
0075 
0076     editor->addItem(" " + i18n("Custom background..."), "select_image");
0077 
0078     if (showTextColor) {
0079         editor->addItem(" " + i18n("Custom text color..."), "text_color");
0080     }
0081 
0082     connect(editor, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated), [ = ](int index) {
0083         editor->clearFocus();
0084 
0085         if ((showTextColor && index == editor->count() - 2)
0086             || (!showTextColor && index == editor->count() - 1)) {
0087             Latte::SettingsDialog *settings = qobject_cast<Latte::SettingsDialog *>(m_parent);
0088 
0089             if (settings) {
0090                 settings->requestImagesDialog(indexOriginal.row());
0091             }
0092         } else if (showTextColor && index == editor->count() - 1) {
0093             Latte::SettingsDialog *settings = qobject_cast<Latte::SettingsDialog *>(m_parent);
0094 
0095             if (settings) {
0096                 settings->requestColorsDialog(indexOriginal.row());
0097             }
0098         }
0099     });
0100 
0101     return editor;
0102 }
0103 
0104 void ColorCmbBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
0105 {
0106     QComboBox *comboBox = static_cast<QComboBox *>(editor);
0107     QString value = index.model()->data(index, Qt::BackgroundRole).toString();
0108     QString userData = index.model()->data(index, Qt::UserRole).toString();
0109 
0110     int pos = Colors.indexOf(value);
0111 
0112     if (pos == -1 && value.startsWith("/")) {
0113         comboBox->setCurrentIndex(Colors.count());
0114     } else {
0115         comboBox->setCurrentIndex(Colors.indexOf(value));
0116     }
0117 }
0118 
0119 void ColorCmbBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
0120 {
0121     QComboBox *comboBox = static_cast<QComboBox *>(editor);
0122 
0123     QString itemData = comboBox->currentData().toString();
0124 
0125     if (itemData != "select_image" && itemData != "text_color") {
0126         model->setData(index, comboBox->currentText(), Qt::BackgroundRole);
0127     }
0128 }
0129 
0130 void ColorCmbBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
0131 {
0132     editor->setGeometry(option.rect);
0133 }
0134 
0135 void ColorCmbBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0136 {
0137     QStyleOptionViewItem myOption = option;
0138     QVariant value = index.data(Qt::BackgroundRole);
0139     QVariant data = index.data(Qt::UserRole);
0140     QString dataStr = data.toString();
0141 
0142     if (value.isValid() && (dataStr != "select_image") && (dataStr != "text_color")) {
0143         QString valueStr = value.toString();
0144 
0145         QString colorPath = valueStr.startsWith("/") ? valueStr : m_iconsPath + value.toString() + "print.jpg";
0146 
0147         if (QFileInfo(colorPath).exists()) {
0148             QBrush colorBrush;
0149             colorBrush.setTextureImage(QImage(colorPath).scaled(QSize(50, 50)));
0150 
0151             painter->setBrush(colorBrush);
0152             painter->drawRect(QRect(option.rect.x(), option.rect.y(),
0153                                     option.rect.width(), option.rect.height()));
0154         }
0155     }
0156 
0157     //QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
0158 }
0159