Krita Source Code Documentation
Loading...
Searching...
No Matches
KisDlgSavePreset.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2017 Scott Petrovic <scottpetrovic@gmail.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
8
9#include <QDebug>
10#include <QDate>
11#include <QTime>
12#include <QVBoxLayout>
13#include <QDialogButtonBox>
14#include <QMessageBox>
15
16#include <KoFileDialog.h>
18#include "QDesktopServices"
22
23#include <kstandardguiitem.h>
24
25
28{
29 // this is setting the area we will "capture" for saving the brush preset. It can potentially be a different
30 // area that the entire scratchpad
31 brushPresetThumbnailWidget->setCutoutOverlayRect(QRect(0, 0, brushPresetThumbnailWidget->height(), brushPresetThumbnailWidget->width()));
32
33
34 // we will default to reusing the previous preset thumbnail
35 // have that checked by default, hide the other elements, and load the last preset image
36 connect(clearBrushPresetThumbnailButton, SIGNAL(clicked(bool)), brushPresetThumbnailWidget, SLOT(fillDefault()));
37 connect(loadImageIntoThumbnailButton, SIGNAL(clicked(bool)), this, SLOT(loadImageFromFile()));
38
39 connect(loadScratchPadThumbnailButton, SIGNAL(clicked(bool)), this, SLOT(loadScratchpadThumbnail()));
40 connect(loadExistingThumbnailButton, SIGNAL(clicked(bool)), this, SLOT(loadExistingThumbnail()));
41 connect(loadIconLibraryThumbnailButton, SIGNAL(clicked(bool)), this, SLOT(loadImageFromLibrary()));
42
43 KGuiItem::assign(buttons->button(QDialogButtonBox::Save), KStandardGuiItem::save());
44 KGuiItem::assign(buttons->button(QDialogButtonBox::Cancel), KStandardGuiItem::cancel());
45 connect(buttons, SIGNAL(accepted()), this, SLOT(savePreset()));
46 connect(buttons, SIGNAL(rejected()), this, SLOT(close()));
47}
48
53
55{
56 m_resourceProvider = resourceProvider;
57
58 brushPresetThumbnailWidget->setupScratchPad(m_resourceProvider, Qt::white);
59}
60
62{
63 setModal(true);
64
65 // set the name of the current brush preset area.
67
68 // UI will look a bit different if we are saving a new brush
70 setWindowTitle(i18n("Save New Brush Preset"));
71 newBrushNameTexField->setVisible(true);
72 clearBrushPresetThumbnailButton->setVisible(true);
73 loadImageIntoThumbnailButton->setVisible(true);
74 currentBrushNameLabel->setVisible(false);
75
76 // If the id is -1, this is a new preset that has never been saved, so it cannot be a copy
77 QString name = preset->name();
78 if (preset && preset->resourceId() > -1) {
79 name.append(" ").append(i18n("Copy"));
80 }
81 newBrushNameTexField->setText(name);
82
83
84 } else {
85 setWindowTitle(i18n("Save Brush Preset"));
86
87 if (preset) {
88 currentBrushNameLabel->setText(preset->name());
89 }
90
91 newBrushNameTexField->setVisible(false);
92 currentBrushNameLabel->setVisible(true);
93 }
94
95 brushPresetThumbnailWidget->paintPresetImage();
96
97 open();
98}
99
101{
102 // create a dialog to retrieve an image file.
103 KoFileDialog dialog(0, KoFileDialog::OpenFile, "OpenDocument");
105 dialog.setDefaultDir(QStandardPaths::writableLocation(QStandardPaths::HomeLocation));
106 QString filename = dialog.filename(); // the filename() returns the entire path & file name, not just the file name
107
108
109 if (filename != "") { // empty if "cancel" is pressed
110 // take that file and load it into the thumbnail are
111 const QImage imageToLoad(filename);
112
113 brushPresetThumbnailWidget->fillTransparent(); // clear the background in case our new image has transparency
114 brushPresetThumbnailWidget->paintCustomImage(imageToLoad);
115 }
116
117}
118
120{
121 brushPresetThumbnailWidget->paintCustomImage(scratchPadThumbnailArea);
122}
123
125{
126 brushPresetThumbnailWidget->paintPresetImage();
127}
128
130{
131 //add dialog code here.
132 QDialog dialog;
133 dialog.setWindowTitle(i18n("Preset Icon Library"));
134 QVBoxLayout *layout = new QVBoxLayout(&dialog);
135
137 layout->addWidget(libWidget);
138 QDialogButtonBox *buttons = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel);
139 KGuiItem::assign(buttons->button(QDialogButtonBox::Ok), KStandardGuiItem::ok());
140 KGuiItem::assign(buttons->button(QDialogButtonBox::Cancel), KStandardGuiItem::cancel());
141 connect(buttons, SIGNAL(accepted()), &dialog, SLOT(accept()));
142 connect(buttons, SIGNAL(rejected()), &dialog, SLOT(reject()));
143 layout->addWidget(buttons);
144
145 //if dialog accepted, get image.
146 if (dialog.exec() == QDialog::Accepted) {
147
148 QImage presetImage = libWidget->getImage();
149 brushPresetThumbnailWidget->paintCustomImage(presetImage);
150 }
151}
152
157
159{
161 if (!curPreset) {
162 return;
163 }
164
165 // if we are saving a new brush, use what we type in for the input
166 QString presetFileName = m_useNewBrushDialog ? newBrushNameTexField->text() : curPreset->name();
167 // We don't want dots or spaces in the filenames
168 presetFileName = presetFileName.replace(' ', '_').replace('.', '_');
169 QString extension = curPreset->defaultFileExtension();
170
171 // Ensure the filename has the corresponding extension
172 if (!presetFileName.endsWith(extension)) {
173 presetFileName.append(extension);
174 }
175
176 bool success = true;
177
179 KisPaintOpPresetSP newPreset = curPreset->clone().dynamicCast<KisPaintOpPreset>();
180 newPreset->setResourceId(-1); // so it won't confuse anything into overwriting
181 newPreset->setFilename(presetFileName);
182 newPreset->setName(m_useNewBrushDialog ? newBrushNameTexField->text() : curPreset->name());
183 newPreset->setImage(brushPresetThumbnailWidget->cutoutOverlay());
184 newPreset->setValid(true);
185 newPreset->setStorageLocation("");
187 success = false;
188 }
189
190 // trying to get brush preset to load after it is created
191 if (success) Q_EMIT resourceSelected(newPreset);
192
193 }
194 else { // saving a preset that is replacing an existing one
195 curPreset->setName(m_useNewBrushDialog ? newBrushNameTexField->text() : curPreset->name());
196 curPreset->setImage(brushPresetThumbnailWidget->cutoutOverlay());
197 // Ensure it has the updated name (for mypaint serialization) -- BUG 445282
198 curPreset->setFilename(presetFileName);
199
201 success = false;
202 }
203
204 // this helps updating the thumbnail in the big label in the editor
205 if (success) Q_EMIT resourceSelected(curPreset);
206 }
207
208
209 // // HACK ALERT! the server does not notify the observers
210 // // automatically, so we need to call the update manually!
211 // rServer->tagCategoryMembersChanged();
212 if (success) {
214 close(); // we are done... so close the save brush dialog
215 }
216
217}
218
219
220
225
226
231
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
KisPaintOpPresetSP currentPreset() const
static QStringList supportedMimeTypes(Direction direction)
KisFavoriteResourceManager * m_favoriteResourceManager
void scratchPadSetup(KisCanvasResourceProvider *resourceProvider)
void setFavoriteResourceManager(KisFavoriteResourceManager *favManager)
void saveScratchPadThumbnailArea(const QImage image)
void resourceSelected(KoResourceSP resource)
KisPresetSaveWidget(QWidget *parent)
void useNewBrushDialog(bool show)
determines if we should show the save as dialog (true) or save in the background (false)
KisCanvasResourceProvider * m_resourceProvider
static bool addResourceWithUserInput(QWidget *widgetParent, KoResourceSP resource, QString storageLocation="")
static bool updateResourceWithUserInput(QWidget *widgetParent, KoResourceSP resource)
QString buttons(const T &ev)