Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_wdg_seexpr_presets_save.cpp
Go to the documentation of this file.
1/*
2 * This file is part of Krita
3 *
4 * SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include <KStandardGuiItem>
10#include <QDate>
11#include <QMessageBox>
12#include <QTime>
13
17#include <KoFileDialog.h>
19
20#include <kis_fill_painter.h>
21#include <kis_paint_device.h>
22#include <kis_default_bounds.h>
23
24#include "KisResourceTypes.h"
27
30 , m_currentPreset(nullptr)
31{
32 // we will default to reusing the previous preset thumbnail
33 // have that checked by default, hide the other elements, and load the last preset image
34 connect(loadExistingThumbnailButton, SIGNAL(clicked(bool)), this, SLOT(loadExistingThumbnail()));
35 connect(loadImageIntoThumbnailButton, SIGNAL(clicked(bool)), this, SLOT(loadImageFromFile()));
36 connect(loadRenderFromScriptButton, SIGNAL(clicked(bool)), this, SLOT(renderScriptToThumbnail()));
37 connect(clearPresetThumbnailButton, SIGNAL(clicked(bool)), presetThumbnailWidget, SLOT(clear()));
38
39 KGuiItem::assign(buttons->button(QDialogButtonBox::Save), KStandardGuiItem::save());
40 KGuiItem::assign(buttons->button(QDialogButtonBox::Cancel), KStandardGuiItem::cancel());
41
42 connect(buttons, SIGNAL(accepted()), this, SLOT(savePreset()));
43 connect(buttons, SIGNAL(rejected()), this, SLOT(close()));
44}
45
49
54
59
61{
62 setModal(true);
63
64 // set the name of the current preset area.
66
67 // UI will look a bit different if we are saving a new preset
69 setWindowTitle(i18n("Save New SeExpr Preset"));
70
71 if (preset) {
72 // If the id is -1, this is a new preset that has never been saved, so it cannot be a copy
73 QString name = preset->name().replace("_", " ");
74 if (preset->resourceId() > -1) {
75 name = QString("%1 %2").arg(name, i18n("Copy"));
76 }
77 newPresetNameTextField->setText(name);
78 newPresetNameTextField->setReadOnly(false);
79 newPresetNameTextField->setEnabled(true);
80 }
81 } else {
82 setWindowTitle(i18n("Save SeExpr Preset"));
83
84 if (preset) {
85 const QString name(preset->name().replace("_", " "));
86 newPresetNameTextField->setText(name);
87 newPresetNameTextField->setReadOnly(true);
88 newPresetNameTextField->setEnabled(false);
89 }
90 }
91
92 if (preset) {
93 presetThumbnailWidget->setPixmap(QPixmap::fromImage(preset->image()));
94 }
95
96 open();
97}
98
100{
101 // create a dialog to retrieve an image file.
102 KoFileDialog dialog(0, KoFileDialog::OpenFile, "OpenDocument");
104 dialog.setDefaultDir(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
105 QString filename = dialog.filename(); // the filename() returns the entire path & file name, not just the file name
106
107 if (!filename.isEmpty()) { // empty if "cancel" is pressed
108 // take that file and load it into the thumbnail are
109 const QImage imageToLoad(filename);
110
111 presetThumbnailWidget->clear(); // clear the background in case our new image has transparency
112 presetThumbnailWidget->setPixmap(QPixmap::fromImage(imageToLoad));
113 }
114}
115
117{
118 presetThumbnailWidget->setPixmap(QPixmap::fromImage(m_currentPreset->image()));
119}
120
122{
124 // TODO add some sort of progress marker?
127 src->setDefaultBounds(bounds);
128 src->setSupportsWraparoundMode(true);
129 KisFillPainter fillPainter(src);
130 fillPainter.fillRect(0, 0, 256, 256, m_currentConfiguration);
131
132 QImage thumbnail = src->convertToQImage(nullptr, 0, 0, 256, 256);
133 presetThumbnailWidget->setPixmap(QPixmap::fromImage(thumbnail));
134 }
135}
136
138{
140
142 QModelIndex idx = model.indexForResourceId(m_currentPreset->resourceId());
143 bool r = true;
144
145 if (idx.isValid() && !m_useNewPresetDialog) {
146 // saving a preset that is replacing an existing one
147 const QString presetFileName = m_currentPreset->name().split(" ").join("_");
148#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
149 if (!presetThumbnailWidget->pixmap()->isNull()) {
150 m_currentPreset->setImage(presetThumbnailWidget->pixmap()->toImage());
151 }
152#else
153 if (!presetThumbnailWidget->pixmap().isNull()) {
154 m_currentPreset->setImage(presetThumbnailWidget->pixmap().toImage());
155 }
156#endif
157 m_currentPreset->setScript(m_currentConfiguration->getString("script"));
158 m_currentPreset->setFilename(presetFileName + m_currentPreset->defaultFileExtension());
159 m_currentPreset->setValid(true);
161 if (r) {
163 }
164 } else {
165 // Saving a completely new preset
166 // Clone the preset, otherwise the modifications will impact the existing resource
167 KisSeExprScriptSP newPreset = m_currentPreset->clone().dynamicCast<KisSeExprScript>();
168
169 if (newPreset) {
170 const QString presetFileName = newPresetNameTextField->text().split(" ").join("_");
171 newPreset->setName(newPresetNameTextField->text());
172 newPreset->setFilename(presetFileName + newPreset->defaultFileExtension());
173#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
174 if (!presetThumbnailWidget->pixmap()->isNull()) {
175 newPreset->setImage(presetThumbnailWidget->pixmap()->toImage());
176 }
177#else
178 if (!presetThumbnailWidget->pixmap().isNull()) {
179 newPreset->setImage(presetThumbnailWidget->pixmap().toImage());
180 }
181#endif
182 newPreset->setScript(m_currentConfiguration->getString("script"));
183 newPreset->setValid(true);
184
186
187 // trying to get brush preset to load after it is created
188 if (r) {
189 m_currentPreset = newPreset;
191 }
192 }
193 }
194
195 close(); // we are done... so close the save brush dialog
196}
197
202
203#include "moc_kis_wdg_seexpr_presets_save.cpp"
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
void fillRect(qint32 x, qint32 y, qint32 w, qint32 h, const KoColor &c, quint8 opacity)
static QStringList supportedMimeTypes(Direction direction)
The KisResourceModel class provides the main access to resources. It is possible to filter the resour...
QModelIndex indexForResourceId(int resourceId) const override
indexFromResource
static bool addResourceWithUserInput(QWidget *widgetParent, KoResourceSP resource, QString storageLocation="")
static bool updateResourceWithUserInput(QWidget *widgetParent, KoResourceSP resource)
KisFilterConfigurationSP m_currentConfiguration
void setCurrentPreset(KisSeExprScriptSP resource)
void resourceSelected(KoResourceSP resource)
void setCurrentRenderConfiguration(KisFilterConfigurationSP config)
void useNewPresetDialog(bool show)
determines if we should show the save as dialog (true) or save in the background (false)
#define KIS_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:75
#define bounds(x, a, b)
QString buttons(const T &ev)
const QString SeExprScripts
The KisWrapAroundBoundsWrapper class wrapper around a KisDefaultBoundsBaseSP to enable wraparound....
static KoColorSpaceRegistry * instance()