Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_dlg_generator_layer.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2008 Boudewijn Rempt <boud@valdyas.org>
3 * SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
9
10#include <QGroupBox>
11#include <QLayout>
12#include <QGridLayout>
13
14#include <klocalizedstring.h>
15#include <kstandardguiitem.h>
16
17#include <kis_config_widget.h>
19#include <kis_paint_device.h>
20#include <kis_transaction.h>
23#include <kis_generator_layer.h>
24#include <KisViewManager.h>
25#include <KisDocument.h>
27#include <kis_command_utils.h>
28#include <kis_icon_utils.h>
29
30#define UPDATE_DELAY 100 /*ms */
31
32KisDlgGeneratorLayer::KisDlgGeneratorLayer(const QString & defaultName, KisViewManager *view, QWidget *parent, KisGeneratorLayerSP glayer = 0, const KisFilterConfigurationSP previousConfig = 0, const KisStrokeId stroke = KisStrokeId())
33 : QDialog(parent)
34 , layer(glayer)
35 , m_view(view)
36 , isEditing(layer && previousConfig)
37 , m_customName(false)
38 , m_freezeName(false)
39 , m_stroke(stroke)
40 , m_compressor(UPDATE_DELAY, KisSignalCompressor::FIRST_INACTIVE)
41{
42 if(isEditing){
43 setModal(false);
44 configBefore = previousConfig->cloneWithResourcesSnapshot();
45 }
46
47 dlgWidget.setupUi(this);
48 dlgWidget.wdgGenerator->initialize(m_view);
49 dlgWidget.btnBox->button(QDialogButtonBox::Ok)->setDefault(true);
50 KGuiItem::assign(dlgWidget.btnBox->button(QDialogButtonBox::Ok), KStandardGuiItem::ok());
51 KGuiItem::assign(dlgWidget.btnBox->button(QDialogButtonBox::Cancel), KStandardGuiItem::cancel());
52
53 dlgWidget.txtLayerName->setText( isEditing ? layer->name() : defaultName );
54 connect(dlgWidget.txtLayerName, SIGNAL(textChanged(QString)),
55 this, SLOT(slotNameChanged(QString)));
56 connect(dlgWidget.wdgGenerator, SIGNAL(previewConfiguration()), this, SLOT(previewGenerator()));
57 connect(&m_compressor, SIGNAL(timeout()), this, SLOT(slotDelayedPreviewGenerator()));
58
59 dlgWidget.filterGalleryToggle->setIcon(KisIconUtils::loadIcon("sidebaricon"));
60 dlgWidget.filterGalleryToggle->setChecked(true);
61 connect(dlgWidget.filterGalleryToggle, SIGNAL(toggled(bool)), dlgWidget.wdgGenerator, SLOT(showFilterGallery(bool)));
62
63 connect(dlgWidget.btnBox, SIGNAL(accepted()), this, SLOT(accept()));
64 connect(dlgWidget.btnBox, SIGNAL(rejected()), this, SLOT(reject()));
65 // Call these in the destructor instead to avoid a crash where the object was deleted mid `saveLayer()` call
66 // connect(this, SIGNAL(accepted()), this, SLOT(saveLayer()));
67 // connect(this, SIGNAL(rejected()), this, SLOT(restoreLayer()));
68
69 if (layer && !isEditing) {
71 }
72
73 restoreGeometry(KisConfig(true).readEntry("generatordialog/geometry", QByteArray()));
74}
75
77{
78 /*
79 * Editing a layer should be using the show function with automatic deletion on close.
80 * Because of this, the action should be taken care of when the window is closed and
81 * the user has accepted the changes.
82 */
83 if (isEditing) {
84 KUndo2Command *cmd = nullptr;
85
87 Q_ASSERT(configAfter);
88 QString xmlBefore = configBefore->toXML();
89 QString xmlAfter = configAfter->toXML();
90
91 if (xmlBefore != xmlAfter) {
95 configAfter->cloneWithResourcesSnapshot()));
96
97 }
98
99 if (layer->name() != layerName()) {
102 layer->name(),
103 layerName()));
104 }
105
106 if (cmd) {
108 }
109 } else {
111 layer->setFilter(configuration()->cloneWithResourcesSnapshot());
112 }
113
114}
115
117{
118 if (isEditing)
119 {
121 }
122}
123
125{
126 //Qt doc says not to use `result()` if `WA_DeleteOnClose` flag is set, but since we're doing this inside the dialog, before it is being deleted, it's fine
127 if (result() == QDialog:: Accepted) {
128 saveLayer();
129 }
130 else{
131 restoreLayer();
132 }
133
134 KisConfig(false).writeEntry("generatordialog/geometry", saveGeometry());
135}
136
138{
139 if (m_freezeName)
140 return;
141
142 m_customName = !text.isEmpty();
143 dlgWidget.btnBox->button(QDialogButtonBox::Ok)->setEnabled(m_customName);
144}
145
147{
148 if (!m_stroke.isNull()) {
149 if (m_updateCookie) {
151 } else {
152 layer->setFilterWithoutUpdate(configuration()->cloneWithResourcesSnapshot());
154 }
155 } else {
157 layer->setFilter(configuration()->cloneWithResourcesSnapshot());
158 }
159}
160
165
167{
168 dlgWidget.wdgGenerator->setConfiguration(config);
169 // hack! forcibly re-render the layer
171}
172
174{
175 return dlgWidget.wdgGenerator->configuration();
176}
177
179{
180 return dlgWidget.txtLayerName->text();
181}
182
void writeEntry(const QString &name, const T &value)
Definition kis_config.h:822
KisFilterConfigurationSP configuration() const
void setConfiguration(const KisFilterConfigurationSP config)
KisThreadSafeSignalCompressor m_compressor
QWeakPointer< boost::none_t > m_updateCookie
Ui_WdgDlgGeneratorLayer dlgWidget
void slotNameChanged(const QString &)
KisDlgGeneratorLayer(const QString &defaultLayerName, KisViewManager *arg_view, QWidget *parent, KisGeneratorLayerSP glayer, const KisFilterConfigurationSP previousConfig, const KisStrokeId stroke)
KisFilterConfigurationSP configBefore
The command for setting the node's name.
virtual void addCommand(KUndo2Command *cmd)=0
KisUndoAdapter * undoAdapter()
The undo adapter is used to add commands to the undo stack.
#define KIS_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:75
#define UPDATE_DELAY
KisStrokeWSP KisStrokeId
Definition kis_types.h:272
KUndo2Command * composeCommands(KUndo2Command *parent, KUndo2Command *cmd)
QIcon loadIcon(const QString &name)
QString name() const
void setFilterWithoutUpdate(KisFilterConfigurationSP filterConfig, bool checkCompareConfig=true)
QWeakPointer< boost::none_t > previewWithStroke(const KisStrokeId stroke)
void setFilter(KisFilterConfigurationSP filterConfig, bool checkCompareConfig=true) override