File indexing completed on 2024-12-22 04:13:12

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 #include "kis_paintop_preset_icon_library.h"
0007 #include <QImage>
0008 #include <QStandardItem>
0009 #include <QSlider>
0010 #include <QPainter>
0011 #include <QLabel>
0012 #include <QListView>
0013 #include <kis_icon.h>
0014 #include <QDebug>
0015 #include <KoResourcePaths.h>
0016 
0017 KisPaintopPresetIconLibrary::KisPaintopPresetIconLibrary(QWidget *parent): QWidget(parent), ui(new Ui_wdgpreseticonlibrary)
0018 {
0019     ui->setupUi(this);
0020 
0021     ui->sldHue->setRange(0.0,360.0,1);
0022     ui->sldHue->setSingleStep(1.0);
0023     ui->sldHue->setPrefix(i18n("Hue:"));
0024 
0025     ui->sldSat->setRange(-50.0,50.0,1);
0026     ui->sldSat->setSingleStep(1.0);
0027     ui->sldSat->setPrefix(i18n("Saturation:"));
0028 
0029     ui->sldLevels->setRange(-50.0,50.0,1);
0030     ui->sldLevels->setSingleStep(1.0);
0031     ui->sldLevels->setPrefix(i18n("Mid-gray level:"));
0032 
0033 
0034     m_baseModel = new QStandardItemModel();
0035     ui->vwBase->setModel(m_baseModel);
0036 
0037     m_optionalModel = new QStandardItemModel();
0038     ui->vwOptional->setModel(m_optionalModel);
0039 
0040     QStringList background_paths = KoResourcePaths::findAllAssets("data", "preset_icons/*.png");
0041     if (background_paths.size()>0) {
0042         m_background.load(background_paths.at(0));
0043         m_background = m_background.scaled(200, 200);
0044     }
0045     ui->lblIconPreview->setPixmap(QPixmap::fromImage(m_background));
0046 
0047 
0048     //empty default:
0049 
0050     QImage empty = QImage(200, 200, QImage::Format_ARGB32);
0051     empty.fill(Qt::transparent);
0052     m_baseModel->appendRow(new QStandardItem(QIcon(QPixmap::fromImage(empty)), NULL));
0053 
0054     QStringList toolIcon_paths = KoResourcePaths::findAllAssets("data", "preset_icons/tool_icons/*.png");
0055     for (int i=0; i<toolIcon_paths.size(); i++) {
0056         QImage pix;
0057         pix.load(toolIcon_paths.at(i));
0058         QStandardItem *image = new QStandardItem(QIcon(QPixmap::fromImage(pix)), NULL);
0059         m_baseModel->appendRow(image);
0060     }
0061 
0062 
0063     empty = QImage(40, 40, QImage::Format_ARGB32);
0064     empty.fill(Qt::transparent);
0065     m_optionalModel->appendRow(new QStandardItem(QIcon(QPixmap::fromImage(empty)), NULL));
0066 
0067     QStringList emblemIcon_paths = KoResourcePaths::findAllAssets("data", "preset_icons/emblem_icons/*.png");
0068     for (int i=0; i<emblemIcon_paths.size(); i++) {
0069         QImage pix;
0070         pix.load(emblemIcon_paths.at(i));
0071         QStandardItem *image = new QStandardItem(QIcon(QPixmap::fromImage(pix)), NULL);
0072         m_optionalModel->appendRow(image);
0073     }
0074 
0075     connect(ui->vwBase->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(updateIcon()));
0076     connect(ui->vwOptional->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(updateIcon()));
0077     connect(ui->sldHue, SIGNAL(valueChanged(qreal)), this, SLOT(updateIcon()));
0078     connect(ui->sldSat, SIGNAL(valueChanged(qreal)), this, SLOT(updateIcon()));
0079     connect(ui->sldLevels, SIGNAL(valueChanged(qreal)), this, SLOT(updateIcon()));
0080 }
0081 
0082 KisPaintopPresetIconLibrary::~KisPaintopPresetIconLibrary()
0083 {
0084     delete ui;
0085     m_optionalModel->clear();
0086     delete m_optionalModel;
0087     m_baseModel->clear();
0088     delete m_baseModel;
0089 }
0090 
0091 QImage KisPaintopPresetIconLibrary::getImage()
0092 {
0093     QImage preset = m_background;
0094     QImage base = QImage(200, 200, QImage::Format_ARGB32);
0095     base.fill(Qt::transparent);
0096     if (ui->vwBase->currentIndex().isValid()) {
0097         base = m_baseModel->itemFromIndex(ui->vwBase->currentIndex())->icon().pixmap(QSize(200, 200)).toImage();
0098     }
0099     if (ui->sldHue->value()>0 || ui->sldSat->value()!=0.0 || ui->sldLevels->value()!=0.0) {
0100         base = hueTransform(base);
0101     }
0102     QImage optional = QImage(40, 40, QImage::Format_ARGB32);
0103     optional.fill(Qt::transparent);
0104     if (ui->vwOptional->currentIndex().isValid()) {
0105          optional = m_optionalModel->itemFromIndex(ui->vwOptional->currentIndex())->icon().pixmap(QSize(40, 40)).toImage();
0106     }
0107 
0108     QPainter p(&preset);
0109     p.drawImage(0, 0, base);
0110     int x = 35;
0111     int y = 31;
0112     p.drawImage(x, y, optional);
0113     return preset;
0114 }
0115 
0116 QImage KisPaintopPresetIconLibrary::hueTransform(QImage img)
0117 {
0118     //This is a super simple levels operation instead of a regular lightness adjustment.
0119     //This is so that we can keep the contrasts in the icon instead of making it
0120     //just darker or lighter.
0121     QVector<int> values(256);
0122     values.fill(255);
0123     int level = qMax(qMin( 28 + ( (int(ui->sldLevels->value()) + 50) * 2), 255), 0);
0124     for (int v = 0; v < level; v++) {
0125         values[v] = int((128.0 / qreal(level))*v);
0126     }
0127     for (int v = level; v < 255; v++) {
0128         values[v] = qMax(qMin(int( (128.0 / qreal(255 - level)) *(v - level)+128.0), 255), 0);
0129     }
0130 
0131     //This is very slow by Krita standards, but we cannot get hsv transforms, ad the image is only 200x200.
0132     for (int x = 0; x < img.width(); x++) {
0133         for (int y = 0; y < img.height(); y++) {
0134             QColor c = img.pixelColor(x, y);
0135             int hue = c.hslHue()+(int)ui->sldHue->value();
0136             if (hue > 360) {
0137                 hue -= 360;
0138             }
0139             int sat = qMax(qMin(c.hslSaturation() + int(ui->sldSat->value() * (255.0 / 100.0)), 255), 0);
0140             c.setHsl(hue, sat, values.at(c.lightness()), c.alpha());
0141             img.setPixelColor(x, y, c);
0142         }
0143     }
0144     return img;
0145 }
0146 
0147 void KisPaintopPresetIconLibrary::updateIcon()
0148 {
0149     ui->lblIconPreview->setPixmap(QPixmap::fromImage(getImage()));
0150 }
0151 
0152