Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_paintop_preset_icon_library.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
7#include <QImage>
8#include <QStandardItem>
9#include <QSlider>
10#include <QPainter>
11#include <kis_icon.h>
12#include <QDebug>
13#include <KoResourcePaths.h>
14
15KisPaintopPresetIconLibrary::KisPaintopPresetIconLibrary(QWidget *parent): QWidget(parent), ui(new Ui_wdgpreseticonlibrary)
16{
17 ui->setupUi(this);
18
19 ui->sldHue->setRange(0.0,360.0,1);
20 ui->sldHue->setSingleStep(1.0);
21 ui->sldHue->setPrefix(i18n("Hue:"));
22
23 ui->sldSat->setRange(-50.0,50.0,1);
24 ui->sldSat->setSingleStep(1.0);
25 ui->sldSat->setPrefix(i18n("Saturation:"));
26
27 ui->sldLevels->setRange(-50.0,50.0,1);
28 ui->sldLevels->setSingleStep(1.0);
29 ui->sldLevels->setPrefix(i18n("Mid-gray level:"));
30
31
32 m_baseModel = new QStandardItemModel();
33 ui->vwBase->setModel(m_baseModel);
34
35 m_optionalModel = new QStandardItemModel();
36 ui->vwOptional->setModel(m_optionalModel);
37
38 QStringList background_paths = KoResourcePaths::findAllAssets("data", "preset_icons/*.png");
39 if (background_paths.size()>0) {
40 m_background.load(background_paths.at(0));
41 m_background = m_background.scaled(200, 200);
42 }
43 ui->lblIconPreview->setPixmap(QPixmap::fromImage(m_background));
44
45
46 //empty default:
47
48 QImage empty = QImage(200, 200, QImage::Format_ARGB32);
49 empty.fill(Qt::transparent);
50 m_baseModel->appendRow(new QStandardItem(QIcon(QPixmap::fromImage(empty)), NULL));
51
52 QStringList toolIcon_paths = KoResourcePaths::findAllAssets("data", "preset_icons/tool_icons/*.png");
53 for (int i=0; i<toolIcon_paths.size(); i++) {
54 QImage pix;
55 pix.load(toolIcon_paths.at(i));
56 QStandardItem *image = new QStandardItem(QIcon(QPixmap::fromImage(pix)), NULL);
57 m_baseModel->appendRow(image);
58 }
59
60
61 empty = QImage(40, 40, QImage::Format_ARGB32);
62 empty.fill(Qt::transparent);
63 m_optionalModel->appendRow(new QStandardItem(QIcon(QPixmap::fromImage(empty)), NULL));
64
65 QStringList emblemIcon_paths = KoResourcePaths::findAllAssets("data", "preset_icons/emblem_icons/*.png");
66 for (int i=0; i<emblemIcon_paths.size(); i++) {
67 QImage pix;
68 pix.load(emblemIcon_paths.at(i));
69 QStandardItem *image = new QStandardItem(QIcon(QPixmap::fromImage(pix)), NULL);
70 m_optionalModel->appendRow(image);
71 }
72
73 connect(ui->vwBase->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(updateIcon()));
74 connect(ui->vwOptional->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(updateIcon()));
75 connect(ui->sldHue, SIGNAL(valueChanged(qreal)), this, SLOT(updateIcon()));
76 connect(ui->sldSat, SIGNAL(valueChanged(qreal)), this, SLOT(updateIcon()));
77 connect(ui->sldLevels, SIGNAL(valueChanged(qreal)), this, SLOT(updateIcon()));
78}
79
88
90{
91 QImage preset = m_background;
92 QImage base = QImage(200, 200, QImage::Format_ARGB32);
93 base.fill(Qt::transparent);
94 if (ui->vwBase->currentIndex().isValid()) {
95 base = m_baseModel->itemFromIndex(ui->vwBase->currentIndex())->icon().pixmap(QSize(200, 200)).toImage();
96 }
97 if (ui->sldHue->value()>0 || ui->sldSat->value()!=0.0 || ui->sldLevels->value()!=0.0) {
98 base = hueTransform(base);
99 }
100 QImage optional = QImage(40, 40, QImage::Format_ARGB32);
101 optional.fill(Qt::transparent);
102 if (ui->vwOptional->currentIndex().isValid()) {
103 optional = m_optionalModel->itemFromIndex(ui->vwOptional->currentIndex())->icon().pixmap(QSize(40, 40)).toImage();
104 }
105
106 QPainter p(&preset);
107 p.drawImage(0, 0, base);
108 int x = 35;
109 int y = 31;
110 p.drawImage(x, y, optional);
111 return preset;
112}
113
115{
116 //This is a super simple levels operation instead of a regular lightness adjustment.
117 //This is so that we can keep the contrasts in the icon instead of making it
118 //just darker or lighter.
119 QVector<int> values(256);
120 values.fill(255);
121 int level = qMax(qMin( 28 + ( (int(ui->sldLevels->value()) + 50) * 2), 255), 0);
122 for (int v = 0; v < level; v++) {
123 values[v] = int((128.0 / qreal(level))*v);
124 }
125 for (int v = level; v < 255; v++) {
126 values[v] = qMax(qMin(int( (128.0 / qreal(255 - level)) *(v - level)+128.0), 255), 0);
127 }
128
129 //This is very slow by Krita standards, but we cannot get hsv transforms, ad the image is only 200x200.
130 for (int x = 0; x < img.width(); x++) {
131 for (int y = 0; y < img.height(); y++) {
132 QColor c = img.pixelColor(x, y);
133 int hue = c.hslHue()+(int)ui->sldHue->value();
134 if (hue > 360) {
135 hue -= 360;
136 }
137 int sat = qMax(qMin(c.hslSaturation() + int(ui->sldSat->value() * (255.0 / 100.0)), 255), 0);
138 c.setHsl(hue, sat, values.at(c.lightness()), c.alpha());
139 img.setPixelColor(x, y, c);
140 }
141 }
142 return img;
143}
144
146{
147 ui->lblIconPreview->setPixmap(QPixmap::fromImage(getImage()));
148}
149
150
const Params2D p
qreal v
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
static QStringList findAllAssets(const QString &type, const QString &filter=QString(), SearchOptions options=NoSearchOptions)