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>
22#include <kis_generator_layer.h>
23#include <KisViewManager.h>
24#include <KisDocument.h>
26#include <kis_icon_utils.h>
27
28#define UPDATE_DELAY 100 /*ms */
29
30KisDlgGeneratorLayer::KisDlgGeneratorLayer(const QString & defaultName, KisViewManager *view, QWidget *parent, KisGeneratorLayerSP glayer = 0, const KisFilterConfigurationSP previousConfig = 0, const KisStrokeId stroke = KisStrokeId())
31 : QDialog(parent)
32 , layer(glayer)
33 , m_view(view)
34 , isEditing(layer && previousConfig)
35 , m_customName(false)
36 , m_freezeName(false)
37 , m_stroke(stroke)
38 , m_compressor(UPDATE_DELAY, KisSignalCompressor::FIRST_INACTIVE)
39{
40 if(isEditing){
41 setModal(false);
42 configBefore = previousConfig->cloneWithResourcesSnapshot();
43 }
44
45 dlgWidget.setupUi(this);
46 dlgWidget.wdgGenerator->initialize(m_view);
47 dlgWidget.btnBox->button(QDialogButtonBox::Ok)->setDefault(true);
48 KGuiItem::assign(dlgWidget.btnBox->button(QDialogButtonBox::Ok), KStandardGuiItem::ok());
49 KGuiItem::assign(dlgWidget.btnBox->button(QDialogButtonBox::Cancel), KStandardGuiItem::cancel());
50
51 dlgWidget.txtLayerName->setText( isEditing ? layer->name() : defaultName );
52 connect(dlgWidget.txtLayerName, SIGNAL(textChanged(QString)),
53 this, SLOT(slotNameChanged(QString)));
54 connect(dlgWidget.wdgGenerator, SIGNAL(previewConfiguration()), this, SLOT(previewGenerator()));
55 connect(&m_compressor, SIGNAL(timeout()), this, SLOT(slotDelayedPreviewGenerator()));
56
57 dlgWidget.filterGalleryToggle->setIcon(KisIconUtils::loadIcon("sidebaricon"));
58 dlgWidget.filterGalleryToggle->setChecked(true);
59 connect(dlgWidget.filterGalleryToggle, SIGNAL(toggled(bool)), dlgWidget.wdgGenerator, SLOT(showFilterGallery(bool)));
60
61 connect(dlgWidget.btnBox, SIGNAL(accepted()), this, SLOT(accept()));
62 connect(dlgWidget.btnBox, SIGNAL(rejected()), this, SLOT(reject()));
63 // Call these in the destructor instead to avoid a crash where the object was deleted mid `saveLayer()` call
64 // connect(this, SIGNAL(accepted()), this, SLOT(saveLayer()));
65 // connect(this, SIGNAL(rejected()), this, SLOT(restoreLayer()));
66
67 if (layer && !isEditing) {
69 }
70
71 restoreGeometry(KisConfig(true).readEntry("generatordialog/geometry", QByteArray()));
72}
73
75{
76 /*
77 * Editing a layer should be using the show function with automatic deletion on close.
78 * Because of this, the action should be taken care of when the window is closed and
79 * the user has accepted the changes.
80 */
81 if (isEditing) {
83
85 Q_ASSERT(configAfter);
86 QString xmlBefore = configBefore->toXML();
87 QString xmlAfter = configAfter->toXML();
88
89 if (xmlBefore != xmlAfter) {
93 configAfter->cloneWithResourcesSnapshot());
94
96 m_view->document()->setModified(true);
97 }
98 } else {
100 layer->setFilter(configuration()->cloneWithResourcesSnapshot());
101 }
102
103}
104
106{
107 if (isEditing)
108 {
110 }
111}
112
114{
115 //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
116 if (result() == QDialog:: Accepted) {
117 saveLayer();
118 }
119 else{
120 restoreLayer();
121 }
122
123 KisConfig(false).writeEntry("generatordialog/geometry", saveGeometry());
124}
125
127{
128 if (m_freezeName)
129 return;
130
131 m_customName = !text.isEmpty();
132 dlgWidget.btnBox->button(QDialogButtonBox::Ok)->setEnabled(m_customName);
133}
134
136{
137 if (!m_stroke.isNull()) {
138 if (m_updateCookie) {
140 } else {
141 layer->setFilterWithoutUpdate(configuration()->cloneWithResourcesSnapshot());
143 }
144 } else {
146 layer->setFilter(configuration()->cloneWithResourcesSnapshot());
147 }
148}
149
154
156{
157 dlgWidget.wdgGenerator->setConfiguration(config);
158 // hack! forcibly re-render the layer
160}
161
163{
164 return dlgWidget.wdgGenerator->configuration();
165}
166
168{
169 return dlgWidget.txtLayerName->text();
170}
171
void writeEntry(const QString &name, const T &value)
Definition kis_config.h:809
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
void setModified(bool _mod)
virtual void addCommand(KUndo2Command *cmd)=0
KisDocument * document() const
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
QIcon loadIcon(const QString &name)
void setName(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