File indexing completed on 2024-12-22 04:13:05

0001 /*
0002  * KDE. Krita Project.
0003  *
0004  * SPDX-FileCopyrightText: 2020 Deif Lou <ginoba@gmail.com>
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include <QVBoxLayout>
0010 #include <QHBoxLayout>
0011 #include <QPushButton>
0012 #include <QLabel>
0013 #include <QToolButton>
0014 #include <QWidgetAction>
0015 #include <QSlider>
0016 
0017 #include <KoStopGradient.h>
0018 #include <KoSegmentGradient.h>
0019 #include <KisStopGradientEditor.h>
0020 #include <KisSegmentGradientEditor.h>
0021 #include <KisGradientChooser.h>
0022 #include <kis_icon_utils.h>
0023 #include <KisGradientConversion.h>
0024 #include <kis_signals_blocker.h>
0025 #include <KoResourceServerProvider.h>
0026 #include <KisResourceItemChooser.h>
0027 #include <KisResourceUserOperations.h>
0028 #include <KisPopupButton.h>
0029 #include <QMenu>
0030 
0031 #include <ksharedconfig.h>
0032 #include <kconfiggroup.h>
0033 
0034 #include "KisGenericGradientEditor.h"
0035 
0036 class Q_DECL_HIDDEN KisGenericGradientEditor::Private
0037 {
0038 public:
0039     KoAbstractGradientSP gradient;
0040     KoCanvasResourcesInterfaceSP canvasResourcesInterface;
0041 
0042     KisGenericGradientEditor *q;
0043     QPushButton *buttonConvertGradient;
0044     QPushButton *buttonUpdateGradient;
0045     QPushButton *buttonAddGradient;
0046     QLabel *labelConvertGradientWarning;
0047     KisGradientChooser *widgetGradientPresetChooser;
0048     QToolButton *toolButtonGradientPresetChooser;
0049     QWidget *widgetToolButtonGradientPresetChooser;
0050     KisPopupButton *toolButtonGradientPresetChooserOptions;
0051     QAction *actionUseGradientPresetChooserPopUp;
0052     QAction *actionCompactGradientPresetChooserMode;
0053     QWidget *widgetGradientEditor;
0054 
0055     bool compactMode;
0056     bool isConvertGradientButtonVisible;
0057     bool isUpdateGradientButtonVisible;
0058     bool isAddGradientButtonVisible;
0059     bool isGradientPresetChooserVisible;
0060     bool isGradientPresetChooserOptionsButtonVisible;
0061     bool useGradientPresetChooserPopUp;
0062     bool compactGradientPresetChooserMode;
0063     bool compactGradientEditorMode;
0064 
0065     QSize cachedSizeHint, cachedMinimumSizeHint;
0066 
0067     void updateCachedSizeHint()
0068     {
0069         static bool editorWidthHintComputed = false;
0070         static int editorWidthHint;
0071         static int editorMinimumWidthHint;
0072         if (!editorWidthHintComputed) {
0073             KisStopGradientEditor stopGradientEditor(nullptr);
0074             KisSegmentGradientEditor segmentGradientEditor(nullptr);
0075             editorWidthHint = qMax(stopGradientEditor.sizeHint().width(), segmentGradientEditor.sizeHint().width());
0076             editorMinimumWidthHint = qMax(stopGradientEditor.minimumSizeHint().width(), segmentGradientEditor.minimumSizeHint().width());
0077             editorWidthHintComputed = true;
0078         }
0079 
0080         const bool presetChooserHidden = useGradientPresetChooserPopUp || !isGradientPresetChooserVisible;
0081 
0082         cachedSizeHint = QSize(
0083             qMax(widgetGradientPresetChooser->sizeHint().width(), editorWidthHint),
0084 
0085             (presetChooserHidden ? 0 : widgetGradientPresetChooser->minimumHeight()) +
0086             buttonConvertGradient->sizeHint().height() +
0087             (widgetGradientEditor ? widgetGradientEditor->sizeHint().height() : 0) +
0088             (presetChooserHidden ? 15 : 25) // spacing
0089         );
0090 
0091         cachedMinimumSizeHint = QSize(
0092             qMax(widgetGradientPresetChooser->minimumSizeHint().width(), editorMinimumWidthHint),
0093 
0094             (presetChooserHidden ? 0 : widgetGradientPresetChooser->minimumHeight()) +
0095             buttonConvertGradient->minimumSizeHint().height() +
0096             (widgetGradientEditor ? widgetGradientEditor->minimumSizeHint().height() : 0) +
0097             (presetChooserHidden ? 15 : 25) // spacing
0098         );
0099     }
0100 };
0101 
0102 KisGenericGradientEditor::KisGenericGradientEditor(QWidget* parent)
0103     : QWidget(parent)
0104     , m_d(new Private)
0105 {
0106     m_d->q = this;
0107     m_d->gradient = nullptr;
0108     m_d->canvasResourcesInterface = nullptr;
0109     m_d->widgetGradientEditor = nullptr;
0110 
0111     QVBoxLayout *layoutMain = new QVBoxLayout;
0112     layoutMain->setContentsMargins(0, 0, 0, 0);
0113     layoutMain->setSpacing(10);
0114 
0115     QHBoxLayout *layoutButtons = new QHBoxLayout;
0116     layoutButtons->setContentsMargins(0, 0, 0, 0);
0117     layoutButtons->setSpacing(5);
0118 
0119     m_d->buttonConvertGradient = new QPushButton(this);
0120     m_d->labelConvertGradientWarning = new QLabel(this);
0121     m_d->labelConvertGradientWarning->setPixmap(KisIconUtils::loadIcon("warning").pixmap(16, 16));
0122     m_d->labelConvertGradientWarning->setToolTip(
0123         i18nc(
0124             "Warning text shown when converting from a segment gradient to a stop gradient",
0125             "Converting a segment gradient to a stop gradient may cause loss of information"
0126         )
0127     );
0128 
0129     m_d->widgetGradientPresetChooser = new KisGradientChooser(this);
0130     m_d->widgetGradientPresetChooser->setNameLabelVisible(false);
0131     m_d->widgetGradientPresetChooser->setEditOptionsVisible(false);
0132 
0133     m_d->toolButtonGradientPresetChooser = new QToolButton(this);
0134     m_d->toolButtonGradientPresetChooser->setText(
0135         i18nc("Choose a preset gradient from the button popup", "Choose Gradient Preset")
0136     );
0137     m_d->toolButtonGradientPresetChooser->setPopupMode(QToolButton::InstantPopup);
0138     m_d->widgetToolButtonGradientPresetChooser = new QWidget(this);
0139     QVBoxLayout *layoutWidgetToolButtonGradientPresetChooser = new QVBoxLayout;
0140     layoutWidgetToolButtonGradientPresetChooser->setContentsMargins(0, 0, 0, 0);
0141     layoutWidgetToolButtonGradientPresetChooser->setSpacing(0);
0142     m_d->widgetToolButtonGradientPresetChooser->setLayout(layoutWidgetToolButtonGradientPresetChooser);
0143     QWidgetAction *widgetActionToolButtonGradientPresetChooser = new QWidgetAction(this);
0144     widgetActionToolButtonGradientPresetChooser->setDefaultWidget(m_d->widgetToolButtonGradientPresetChooser);
0145     m_d->toolButtonGradientPresetChooser->addAction(widgetActionToolButtonGradientPresetChooser);
0146     
0147     m_d->buttonUpdateGradient = new QPushButton(this);
0148     m_d->buttonUpdateGradient->setIcon(KisIconUtils::loadIcon("document-save"));
0149     m_d->buttonUpdateGradient->setToolTip(
0150         i18nc(
0151             "Update the current gradient in the presets with the one in the generic gradient editor",
0152             "Update the selected gradient preset with the current gradient"
0153         )
0154     );
0155     
0156     m_d->buttonAddGradient = new QPushButton(this);
0157     m_d->buttonAddGradient->setIcon(KisIconUtils::loadIcon("list-add"));
0158     m_d->buttonAddGradient->setToolTip(
0159         i18nc(
0160             "Add the current gradient in the generic gradient editor to the presets",
0161             "Add the current gradient to the presets"
0162         )
0163     );
0164 
0165     m_d->toolButtonGradientPresetChooserOptions = new KisPopupButton(this);
0166     m_d->toolButtonGradientPresetChooserOptions->setArrowVisible(false);
0167     m_d->toolButtonGradientPresetChooserOptions->setAutoRaise(true);
0168     m_d->toolButtonGradientPresetChooserOptions->setIcon(KisIconUtils::loadIcon("view-choose"));
0169     m_d->actionUseGradientPresetChooserPopUp = new QAction(this);
0170     m_d->actionUseGradientPresetChooserPopUp->setCheckable(true);
0171     m_d->actionUseGradientPresetChooserPopUp->setText(
0172         i18nc(
0173             "Show the gradient preset chooser as a pop-up in a button or inline",
0174             "Use a pop-up gradient preset chooser"
0175         )
0176     );
0177     m_d->actionCompactGradientPresetChooserMode = new QAction(this);
0178     m_d->actionCompactGradientPresetChooserMode->setCheckable(true);
0179     m_d->actionCompactGradientPresetChooserMode->setText(
0180         i18nc(
0181             "Hide/show option widgets around the gradient preset chooser",
0182             "Show compact gradient preset chooser"
0183         )
0184     );
0185     QMenu *toolButtonGradientPresetChooserOptionsMenu = new QMenu;
0186     toolButtonGradientPresetChooserOptionsMenu->addAction(m_d->actionUseGradientPresetChooserPopUp);
0187     toolButtonGradientPresetChooserOptionsMenu->addAction(m_d->actionCompactGradientPresetChooserMode);
0188     m_d->toolButtonGradientPresetChooserOptions->setPopupWidget(toolButtonGradientPresetChooserOptionsMenu);
0189 
0190     layoutButtons->addWidget(m_d->buttonAddGradient, 0);
0191     layoutButtons->addWidget(m_d->buttonUpdateGradient, 0);
0192     layoutButtons->addWidget(m_d->buttonConvertGradient, 0);
0193     layoutButtons->addWidget(m_d->labelConvertGradientWarning, 0);
0194     layoutButtons->addStretch(1);
0195     layoutButtons->addWidget(m_d->toolButtonGradientPresetChooser, 0);
0196     layoutButtons->addWidget(m_d->toolButtonGradientPresetChooserOptions, 0);
0197 
0198     layoutMain->addWidget(m_d->widgetGradientPresetChooser, 1);
0199     layoutMain->addLayout(layoutButtons, 0);
0200     layoutMain->addStretch();
0201 
0202     setLayout(layoutMain);
0203 
0204     m_d->compactMode = false;
0205     m_d->isConvertGradientButtonVisible = true;
0206     m_d->isUpdateGradientButtonVisible = true;
0207     m_d->isAddGradientButtonVisible = true;
0208     m_d->isGradientPresetChooserVisible = true;
0209     m_d->isGradientPresetChooserOptionsButtonVisible = true;
0210     m_d->useGradientPresetChooserPopUp = true;
0211     m_d->compactGradientPresetChooserMode = false;
0212     m_d->compactGradientEditorMode = false;
0213 
0214     updateConvertGradientButton();
0215     updateUpdateGradientButton();
0216     updateAddGradientButton();
0217     updateGradientPresetChooser();
0218     updateGradientEditor();
0219 
0220     connect(
0221         m_d->buttonConvertGradient,
0222         SIGNAL(clicked()),
0223         this,
0224         SLOT(on_buttonConvertGradient_clicked())
0225     );
0226 
0227     connect(
0228         m_d->buttonUpdateGradient,
0229         SIGNAL(clicked()),
0230         this,
0231         SLOT(on_buttonUpdateGradient_clicked())
0232     );
0233 
0234     connect(
0235         m_d->buttonAddGradient,
0236         SIGNAL(clicked()),
0237         this,
0238         SLOT(on_buttonAddGradient_clicked())
0239     );
0240 
0241     connect(
0242         m_d->widgetGradientPresetChooser,
0243         SIGNAL(resourceClicked(KoResourceSP)),
0244         this,
0245         SLOT(on_widgetGradientPresetChooser_resourceClicked(KoResourceSP))
0246     );
0247 
0248     connect(
0249         m_d->actionUseGradientPresetChooserPopUp,
0250         SIGNAL(toggled(bool)),
0251         this,
0252         SLOT(setUseGradientPresetChooserPopUp(bool))
0253     );
0254 
0255     connect(
0256         m_d->actionCompactGradientPresetChooserMode,
0257         SIGNAL(toggled(bool)),
0258         this,
0259         SLOT(setCompactGradientPresetChooserMode(bool))
0260     );
0261 
0262     m_d->updateCachedSizeHint();
0263     setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);
0264 }
0265 
0266 KisGenericGradientEditor::~KisGenericGradientEditor()
0267 {}
0268 
0269 void KisGenericGradientEditor::loadUISettings(const QString &prefix)
0270 {
0271     KConfigGroup configGroup(KSharedConfig::openConfig(), "GenericGradientEditor");
0272     const QString pfx = prefix.isEmpty() ? QString("global/") : prefix + "/";
0273 
0274     m_d->useGradientPresetChooserPopUp = configGroup.readEntry(pfx + "useGradientPresetChooserPopUp", m_d->useGradientPresetChooserPopUp);
0275     m_d->compactGradientPresetChooserMode = configGroup.readEntry(pfx + "compactGradientPresetChooserMode", m_d->compactGradientPresetChooserMode);
0276     
0277     updateGradientPresetChooser();
0278 }
0279 
0280 void KisGenericGradientEditor::saveUISettings(const QString &prefix)
0281 {
0282     KConfigGroup configGroup(KSharedConfig::openConfig(), "GenericGradientEditor");
0283     const QString pfx = prefix.isEmpty() ? QString("global/") : prefix + "/";
0284 
0285     configGroup.writeEntry(pfx + "useGradientPresetChooserPopUp", m_d->useGradientPresetChooserPopUp);
0286     configGroup.writeEntry(pfx + "compactGradientPresetChooserMode", m_d->compactGradientPresetChooserMode);
0287 }
0288 
0289 KoAbstractGradientSP KisGenericGradientEditor::gradient() const
0290 {
0291     if (m_d->gradient) {
0292         return m_d->gradient->clone().dynamicCast<KoAbstractGradient>();
0293     }
0294     return nullptr;
0295 }
0296 
0297 KoCanvasResourcesInterfaceSP KisGenericGradientEditor::canvasResourcesInterface() const
0298 {
0299     return m_d->canvasResourcesInterface;
0300 }
0301 
0302 bool KisGenericGradientEditor::compactMode() const
0303 {
0304     return m_d->compactMode;
0305 }
0306 
0307 bool KisGenericGradientEditor::isConvertGradientButtonVisible() const
0308 {
0309     return m_d->isConvertGradientButtonVisible;
0310 }
0311 
0312 bool KisGenericGradientEditor::isUpdateGradientButtonVisible() const
0313 {
0314     return m_d->isUpdateGradientButtonVisible;
0315 }
0316 
0317 bool KisGenericGradientEditor::isAddGradientButtonVisible() const
0318 {
0319     return m_d->isAddGradientButtonVisible;
0320 }
0321 
0322 bool KisGenericGradientEditor::isGradientPresetChooserVisible() const
0323 {
0324     return m_d->isGradientPresetChooserVisible;
0325 }
0326 
0327 bool KisGenericGradientEditor::isGradientPresetChooserOptionsButtonVisible() const
0328 {
0329     return m_d->isGradientPresetChooserOptionsButtonVisible;
0330 }
0331 
0332 bool KisGenericGradientEditor::useGradientPresetChooserPopUp() const
0333 {
0334     return m_d->useGradientPresetChooserPopUp;
0335 }
0336 
0337 bool KisGenericGradientEditor::compactGradientPresetChooserMode() const
0338 {
0339     return m_d->compactGradientPresetChooserMode;
0340 }
0341 
0342 bool KisGenericGradientEditor::compactGradientEditorMode() const
0343 {
0344     return m_d->compactGradientEditorMode;
0345 }
0346 
0347 void KisGenericGradientEditor::setGradient(KoAbstractGradientSP newGradient)
0348 {
0349     if (newGradient == m_d->gradient) {
0350         return;
0351     }
0352 
0353     if (!newGradient || !newGradient->valid()) {
0354         if (m_d->widgetGradientEditor) {
0355             layout()->removeWidget(m_d->widgetGradientEditor);
0356             delete m_d->widgetGradientEditor;
0357             m_d->widgetGradientEditor = nullptr;
0358         }
0359         m_d->gradient = nullptr;
0360         updateConvertGradientButton();
0361         updateUpdateGradientButton();
0362         updateAddGradientButton();
0363         updateGradientEditor();
0364         return;
0365     }
0366 
0367     m_d->gradient = newGradient->clone().staticCast<KoAbstractGradient>();
0368     if (m_d->canvasResourcesInterface) {
0369         m_d->gradient->updateVariableColors(m_d->canvasResourcesInterface);
0370     }
0371     
0372     QWidget *newGradientEditorWidget = nullptr;
0373     if (m_d->gradient.dynamicCast<KoStopGradient>()) {
0374         if (dynamic_cast<KisStopGradientEditor*>(m_d->widgetGradientEditor)) {
0375             dynamic_cast<KisStopGradientEditor*>(m_d->widgetGradientEditor)->setGradient(m_d->gradient.dynamicCast<KoStopGradient>());
0376         } else {
0377             newGradientEditorWidget =
0378                 new KisStopGradientEditor(m_d->gradient.dynamicCast<KoStopGradient>(), nullptr, "", "", m_d->canvasResourcesInterface);
0379         }
0380     } else if (m_d->gradient.dynamicCast<KoSegmentGradient>()) {
0381         if (dynamic_cast<KisSegmentGradientEditor*>(m_d->widgetGradientEditor)) {
0382             dynamic_cast<KisSegmentGradientEditor*>(m_d->widgetGradientEditor)->setGradient(m_d->gradient.dynamicCast<KoSegmentGradient>());
0383         } else {
0384             newGradientEditorWidget =
0385                 new KisSegmentGradientEditor(m_d->gradient.dynamicCast<KoSegmentGradient>(), nullptr, "", "", m_d->canvasResourcesInterface);
0386         }
0387     }
0388 
0389     if (newGradientEditorWidget) {
0390         QWidget *oldGradientEditorWidget = m_d->widgetGradientEditor;
0391         m_d->widgetGradientEditor = newGradientEditorWidget;
0392         m_d->widgetGradientEditor->layout()->setContentsMargins(0, 0, 0, 0);
0393         m_d->widgetGradientEditor->setMinimumSize(0, 0);
0394         m_d->widgetGradientEditor->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
0395         if (dynamic_cast<KisStopGradientEditor*>(m_d->widgetGradientEditor)) {
0396             dynamic_cast<KisStopGradientEditor*>(m_d->widgetGradientEditor)->setCompactMode(m_d->compactGradientPresetChooserMode);
0397         } else {
0398             dynamic_cast<KisSegmentGradientEditor*>(m_d->widgetGradientEditor)->setCompactMode(m_d->compactGradientPresetChooserMode);
0399         }
0400         if (oldGradientEditorWidget) {
0401             setUpdatesEnabled(false);
0402             layout()->replaceWidget(oldGradientEditorWidget, m_d->widgetGradientEditor);
0403             dynamic_cast<QVBoxLayout*>(layout())->setStretchFactor(m_d->widgetGradientEditor, 0);
0404             layout()->activate();
0405             delete oldGradientEditorWidget;
0406             setUpdatesEnabled(true);
0407         } else {
0408             dynamic_cast<QVBoxLayout*>(layout())->insertWidget(
0409                 m_d->useGradientPresetChooserPopUp ? 1 : 2,
0410                 m_d->widgetGradientEditor,
0411                 0
0412             );
0413         }
0414         connect(m_d->widgetGradientEditor, SIGNAL(sigGradientChanged()), this, SLOT(on_widgetGradientEditor_sigGradientChanged()));
0415         updateConvertGradientButton();
0416         updateUpdateGradientButton();
0417         updateAddGradientButton();
0418         updateGradientEditor();
0419     }
0420     m_d->updateCachedSizeHint();
0421 
0422     emit sigGradientChanged();
0423 }
0424 
0425 void KisGenericGradientEditor::setCanvasResourcesInterface(KoCanvasResourcesInterfaceSP newCanvasResourcesInterface)
0426 {
0427     m_d->canvasResourcesInterface = newCanvasResourcesInterface;
0428 
0429     if (dynamic_cast<KisStopGradientEditor*>(m_d->widgetGradientEditor)) {
0430         dynamic_cast<KisStopGradientEditor*>(m_d->widgetGradientEditor)->setCanvasResourcesInterface(m_d->canvasResourcesInterface);
0431     } else if (dynamic_cast<KisSegmentGradientEditor*>(m_d->widgetGradientEditor)) {
0432         dynamic_cast<KisSegmentGradientEditor*>(m_d->widgetGradientEditor)->setCanvasResourcesInterface(m_d->canvasResourcesInterface);
0433     }
0434 
0435     m_d->widgetGradientPresetChooser->setCanvasResourcesInterface(newCanvasResourcesInterface);
0436 }
0437 
0438 void KisGenericGradientEditor::setCompactMode(bool compact)
0439 {
0440     m_d->compactMode = compact;
0441     
0442     updateConvertGradientButton();
0443     updateUpdateGradientButton();
0444     updateAddGradientButton();
0445     updateGradientPresetChooser();
0446 }
0447 
0448 void KisGenericGradientEditor::setConvertGradientButtonVisible(bool visible)
0449 {
0450     m_d->isConvertGradientButtonVisible = visible;
0451     updateConvertGradientButton();
0452 }
0453 
0454 void KisGenericGradientEditor::setUpdateGradientButtonVisible(bool visible)
0455 {
0456     m_d->isUpdateGradientButtonVisible = visible;
0457     updateUpdateGradientButton();
0458 }
0459 
0460 void KisGenericGradientEditor::setAddGradientButtonVisible(bool visible)
0461 {
0462     m_d->isAddGradientButtonVisible = visible;
0463     updateAddGradientButton();
0464 }
0465 
0466 void KisGenericGradientEditor::setGradientPresetChooserVisible(bool visible)
0467 {
0468     m_d->isGradientPresetChooserVisible = visible;
0469     updateGradientPresetChooser();
0470     m_d->updateCachedSizeHint();
0471 }
0472 
0473 void KisGenericGradientEditor::setGradientPresetChooserOptionsButtonVisible(bool visible)
0474 {
0475     m_d->isGradientPresetChooserOptionsButtonVisible = visible;
0476     updateGradientPresetChooser();
0477 }
0478 
0479 void KisGenericGradientEditor::setUseGradientPresetChooserPopUp(bool use)
0480 {
0481     m_d->useGradientPresetChooserPopUp = use;
0482     updateGradientPresetChooser();
0483     m_d->updateCachedSizeHint();
0484 }
0485 
0486 void KisGenericGradientEditor::setCompactGradientPresetChooserMode(bool compact)
0487 {
0488     m_d->compactGradientPresetChooserMode = compact;
0489     updateGradientPresetChooser();
0490     m_d->updateCachedSizeHint();
0491 }
0492 
0493 void KisGenericGradientEditor::setCompactGradientEditorMode(bool compact)
0494 {
0495     m_d->compactGradientEditorMode = compact;
0496     updateGradientEditor();
0497     m_d->updateCachedSizeHint();
0498 }
0499 
0500 
0501 QSize KisGenericGradientEditor::sizeHint() const
0502 {
0503     return m_d->cachedSizeHint;
0504 }
0505 
0506 QSize KisGenericGradientEditor::minimumSizeHint() const
0507 {
0508     return m_d->cachedMinimumSizeHint;
0509 }
0510 
0511 bool KisGenericGradientEditor::event(QEvent *e)
0512 {
0513     if (e->type() == QEvent::StyleChange || e->type() == QEvent::FontChange) {
0514         m_d->updateCachedSizeHint();
0515     }
0516     return QWidget::event(e);
0517 }
0518 
0519 void KisGenericGradientEditor::updateConvertGradientButton()
0520 {
0521     bool isSegmentGradient = dynamic_cast<KisSegmentGradientEditor*>(m_d->widgetGradientEditor);
0522 
0523     m_d->buttonConvertGradient->setVisible(!m_d->compactMode && m_d->isConvertGradientButtonVisible);
0524     m_d->labelConvertGradientWarning->setVisible(!m_d->compactMode && m_d->isConvertGradientButtonVisible && isSegmentGradient);
0525     
0526     m_d->buttonConvertGradient->setText(
0527         m_d->gradient
0528         ?   isSegmentGradient
0529             ? i18nc("Convert the segment gradient to a stop gradient", "Convert to Stop Gradient")
0530             : i18nc("Convert the stop gradient to a segment gradient", "Convert to Segment Gradient")
0531         :   i18nc("A gradient wasn't set in the generic gradient editor", "No Gradient Set")
0532     );
0533 
0534     m_d->buttonConvertGradient->setEnabled(m_d->gradient);
0535 }
0536 
0537 void KisGenericGradientEditor::updateUpdateGradientButton()
0538 {
0539     m_d->buttonUpdateGradient->setVisible(!m_d->compactMode && m_d->isUpdateGradientButtonVisible);
0540     KoResourceSP selectedGradient = m_d->widgetGradientPresetChooser->currentResource();
0541     m_d->buttonUpdateGradient->setEnabled(
0542         m_d->gradient && selectedGradient &&
0543         m_d->gradient->resourceId() == selectedGradient->resourceId() &&
0544         !selectedGradient->permanent()
0545     );
0546 }
0547 
0548 void KisGenericGradientEditor::updateAddGradientButton()
0549 {
0550     m_d->buttonAddGradient->setVisible(!m_d->compactMode && m_d->isAddGradientButtonVisible);
0551     m_d->buttonAddGradient->setEnabled(m_d->gradient && !m_d->gradient->name().isEmpty());
0552 }
0553 
0554 void KisGenericGradientEditor::updateGradientPresetChooser()
0555 {
0556     m_d->widgetGradientPresetChooser->resourceItemChooser()->showImportExportBtns(!m_d->compactGradientPresetChooserMode);
0557     m_d->widgetGradientPresetChooser->resourceItemChooser()->showStorageBtn(!m_d->compactGradientPresetChooserMode);
0558     m_d->widgetGradientPresetChooser->resourceItemChooser()->showTaggingBar(!m_d->compactGradientPresetChooserMode);
0559     m_d->widgetGradientPresetChooser->resourceItemChooser()->showViewModeBtn(!m_d->compactGradientPresetChooserMode);
0560 
0561     {
0562         int margin = !m_d->useGradientPresetChooserPopUp || m_d->compactGradientPresetChooserMode ? 0 : 10;
0563         m_d->widgetGradientPresetChooser->setContentsMargins(margin, margin, margin, margin);
0564 
0565         if (m_d->useGradientPresetChooserPopUp) {
0566             if (!m_d->widgetToolButtonGradientPresetChooser->children().contains(m_d->widgetGradientPresetChooser)) {
0567                 layout()->removeWidget(m_d->widgetGradientPresetChooser);
0568                 m_d->widgetToolButtonGradientPresetChooser->layout()->addWidget(m_d->widgetGradientPresetChooser);
0569             }
0570             m_d->widgetGradientPresetChooser->setMinimumSize(
0571                 m_d->compactGradientPresetChooserMode ? 300 : 320,
0572                 m_d->compactGradientPresetChooserMode ? 300 : 320
0573             );
0574             m_d->widgetGradientPresetChooser->adjustSize();
0575         } else {
0576             if (!children().contains(m_d->widgetGradientPresetChooser)) {
0577                 m_d->widgetToolButtonGradientPresetChooser->layout()->removeWidget(m_d->widgetGradientPresetChooser);
0578                 static_cast<QVBoxLayout*>(layout())->insertWidget(0, m_d->widgetGradientPresetChooser, 1);
0579             }
0580             m_d->widgetGradientPresetChooser->setMinimumSize(
0581                 0,
0582                 m_d->compactGradientPresetChooserMode ? 100 : 200
0583             );
0584             m_d->widgetGradientPresetChooser->show();
0585         }
0586     }
0587 
0588     m_d->toolButtonGradientPresetChooser->setVisible(
0589         !m_d->compactMode &&
0590         m_d->isGradientPresetChooserVisible &&
0591         m_d->useGradientPresetChooserPopUp
0592     );
0593     m_d->toolButtonGradientPresetChooserOptions->setVisible(
0594         !m_d->compactMode &&
0595         m_d->isGradientPresetChooserVisible &&
0596         m_d->isGradientPresetChooserOptionsButtonVisible
0597     );
0598     m_d->actionUseGradientPresetChooserPopUp->setChecked(m_d->useGradientPresetChooserPopUp);
0599     m_d->actionCompactGradientPresetChooserMode->setChecked(m_d->compactGradientPresetChooserMode);
0600 }
0601 
0602 void KisGenericGradientEditor::updateGradientEditor()
0603 {
0604     if (dynamic_cast<KisStopGradientEditor*>(m_d->widgetGradientEditor)) {
0605         dynamic_cast<KisStopGradientEditor*>(m_d->widgetGradientEditor)->setCompactMode(m_d->compactGradientEditorMode);
0606     }
0607     else if (dynamic_cast<KisSegmentGradientEditor*>(m_d->widgetGradientEditor)) {
0608         dynamic_cast<KisSegmentGradientEditor*>(m_d->widgetGradientEditor)->setCompactMode(m_d->compactGradientEditorMode);
0609     }
0610 }
0611 
0612 void KisGenericGradientEditor::on_buttonConvertGradient_clicked()
0613 {
0614     if (m_d->gradient.dynamicCast<KoStopGradient>()) {
0615         setGradient(KisGradientConversion::toSegmentGradient(m_d->gradient));
0616     }
0617     else if (m_d->gradient.dynamicCast<KoSegmentGradient>()) {
0618         setGradient(KisGradientConversion::toStopGradient(m_d->gradient));
0619     }
0620 }
0621 
0622 void KisGenericGradientEditor::on_buttonUpdateGradient_clicked()
0623 {
0624     if (!m_d->gradient || !m_d->gradient->valid() || !(m_d->gradient->resourceId() > -1)) {
0625         return;
0626     }
0627 
0628     KoResourceSP selectedGradient = m_d->widgetGradientPresetChooser->currentResource();
0629     if (!selectedGradient ||
0630         !(m_d->gradient->resourceId() == selectedGradient->resourceId()) ||
0631         selectedGradient->permanent()) {
0632         return;
0633     }
0634 
0635     m_d->gradient->updatePreview();
0636     KisResourceUserOperations::updateResourceWithUserInput(this, m_d->gradient);
0637 }
0638 
0639 void KisGenericGradientEditor::on_buttonAddGradient_clicked()
0640 {
0641     qDebug() << m_d->gradient << m_d->gradient->valid() << m_d->gradient->name();
0642 
0643     if (!m_d->gradient || !m_d->gradient->valid() || m_d->gradient->name().isEmpty()) {
0644         return;
0645     }
0646     
0647     m_d->gradient->updatePreview();
0648     KisResourceModel model(ResourceType::Gradients);
0649     KisResourceUserOperations::addResourceWithUserInput(this, m_d->gradient);
0650 }
0651 
0652 void KisGenericGradientEditor::on_widgetGradientPresetChooser_resourceClicked(KoResourceSP resource)
0653 {
0654     setGradient(resource.dynamicCast<KoAbstractGradient>());
0655 }
0656 
0657 void KisGenericGradientEditor::on_widgetGradientEditor_sigGradientChanged()
0658 {
0659     updateUpdateGradientButton();
0660     updateAddGradientButton();
0661     emit sigGradientChanged();
0662 }