1 /*
2  *  Copyright (c) 2004 Adrian Page <adrian@pagenet.plus.com>
3  *  Copyright (C) 2011 Srikanth Tiyyagura <srikanth.tulasiram@gmail.com>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "widgets/kis_gradient_chooser.h"
21 
22 #include <QLabel>
23 #include <QPushButton>
24 #include <QVBoxLayout>
25 #include <QMenu>
26 #include <QSlider>
27 #include <QHeaderView>
28 #include <QWidgetAction>
29 #include <QSet>
30 
31 #include <klocalizedstring.h>
32 #include <resources/KoAbstractGradient.h>
33 #include <resources/KoResource.h>
34 #include <resources/KoSegmentGradient.h>
35 #include <KoResourceItemView.h>
36 #include <KoStopGradient.h>
37 #include <KoResourceItemChooser.h>
38 #include <KoResourceServerProvider.h>
39 #include <KoResourceServerAdapter.h>
40 #include <kis_icon.h>
41 #include <kis_config.h>
42 #include <kis_signals_blocker.h>
43 #include <KoDialog.h>
44 
45 #include "kis_global.h"
46 #include "kis_autogradient.h"
47 #include "kis_canvas_resource_provider.h"
48 #include "kis_stopgradient_editor.h"
49 
50 #include <ksharedconfig.h>
51 #include <kconfiggroup.h>
52 #include <ksqueezedtextlabel.h>
53 
54 class KisCustomGradientDialog : public KoDialog
55 {
56     Q_OBJECT
57 public:
58     KisCustomGradientDialog(KoAbstractGradient* gradient,
59                             QWidget *parent,
60                             const char *name,
61                             const KoColor &fgColor,
62                             const KoColor &bgColor);
63 
64 private:
65     QWidget *m_page {0};
66 };
67 
KisCustomGradientDialog(KoAbstractGradient * gradient,QWidget * parent,const char * name,const KoColor & fgColor,const KoColor & bgColor)68 KisCustomGradientDialog::KisCustomGradientDialog(KoAbstractGradient* gradient,
69                                                  QWidget *parent,
70                                                  const char *name,
71                                                  const KoColor &fgColor,
72                                                  const KoColor &bgColor)
73     : KoDialog(parent, Qt::Dialog)
74 {
75     setButtons(Close);
76     setDefaultButton(Close);
77     setObjectName(name);
78     setModal(false);
79 
80     KoStopGradient* stopGradient = dynamic_cast<KoStopGradient*>(gradient);
81     if (stopGradient) {
82         m_page = new KisStopGradientEditor(stopGradient, this, "autogradient", i18n("Custom Stop Gradient"), fgColor, bgColor);
83     }
84     else {
85         KoSegmentGradient* segmentedGradient = dynamic_cast<KoSegmentGradient*>(gradient);
86         if (segmentedGradient) {
87             m_page = new KisAutogradientEditor(segmentedGradient, this, "autogradient", i18n("Custom Segmented Gradient"), fgColor, bgColor);
88         }
89     }
90     setCaption(m_page->windowTitle());
91     setMainWidget(m_page);
92 }
93 
94 class Q_DECL_HIDDEN KisGradientChooser::Private : public QObject
95 {
96     Q_OBJECT
97 
98 public:
99     struct ViewOptions
100     {
101         ViewMode viewMode{ViewMode_Icon};
102         ItemSize itemSize{ItemSize_Medium};
103         int itemSizeCustom{32};
104         static constexpr int itemSizeSmall{32};
105         static constexpr int itemSizeMedium{48};
106         static constexpr int itemSizeLarge{64};
107         static constexpr qreal itemSizeWidthFactor{2.0};
108     };
109 
110     KisGradientChooser *q;
111     KSqueezedTextLabel *labelName;
112     KoResourceItemChooser * itemChooser;
113 
114     KoColor foregroundColor, backgroundColor;
115 
116     QWidget *containerEditWidgets;
117     QToolButton *buttonAddGradient;
118     QPushButton *buttonEditGradient;
119 
120     QAction *actionViewModeIcon;
121     QAction *actionViewModeList;
122     QAction *actionItemSizeSmall;
123     QAction *actionItemSizeMedium;
124     QAction *actionItemSizeLarge;
125     QAction *actionItemSizeCustom;
126     QSlider *sliderItemSizeCustom;
127     QWidget *containerSliderItemSizeCustom;
128 
129     bool useGlobalViewOptions;
130     static ViewOptions globalViewOptions;
131     static QSet<KisGradientChooser*> globalChoosers;
132     ViewOptions *viewOptions;
133 
134     bool isNameLabelVisible;
135     bool areEditOptionsVisible;
136 
137 public Q_SLOTS:
138     void update(KoResource * resource);
139     void addStopGradient();
140     void addSegmentedGradient();
141     void editGradient();
142     void addGradient(KoAbstractGradient* gradient);
143 
144     void on_itemChooserItemView_sigSizeChanged();
145     void on_actionGroupViewMode_triggered(QAction *triggeredAction);
146     void on_actionGroupItemSize_triggered(QAction *triggeredAction);
147     void on_sliderItemSizeCustom_valueChanged(int newValue);
148 
149 public:
150     void updatePresetChooser(bool globalUpdate = true);
151     void updatePresetChooserIcons();
152     void updateContainerSliderItemSizeCustom();
153 };
154 KisGradientChooser::Private::ViewOptions KisGradientChooser::Private::globalViewOptions;
155 QSet<KisGradientChooser*> KisGradientChooser::Private::globalChoosers;
156 
KisGradientChooser(QWidget * parent,const char * name,bool useGlobalViewOptions)157 KisGradientChooser::KisGradientChooser(QWidget *parent, const char *name, bool useGlobalViewOptions)
158     : QFrame(parent)
159     , m_d(new Private)
160 {
161     setObjectName(name);
162 
163     m_d->q = this;
164 
165     // Name label
166     m_d->labelName = new KSqueezedTextLabel;
167 
168     // Resource Item Chooser
169     KoResourceServer<KoAbstractGradient> * rserver = KoResourceServerProvider::instance()->gradientServer();
170     QSharedPointer<KoAbstractResourceServerAdapter> adapter (new KoResourceServerAdapter<KoAbstractGradient>(rserver));
171     m_d->itemChooser = new KoResourceItemChooser(adapter, this);
172     m_d->itemChooser->showTaggingBar(true);
173     m_d->itemChooser->setViewModeButtonVisible(true);
174     m_d->itemChooser->itemView()->keepAspectRatio(false);
175     m_d->itemChooser->itemView()->setShowGrid(false);
176     m_d->itemChooser->itemView()->horizontalHeader()->setMinimumSectionSize(0);
177     m_d->itemChooser->itemView()->verticalHeader()->setMinimumSectionSize(0);
178 
179     // View menu
180     QActionGroup *actionGroupViewMode = new QActionGroup(this);
181     m_d->actionViewModeIcon = new QAction(this);
182     m_d->actionViewModeIcon->setCheckable(true);
183     m_d->actionViewModeIcon->setActionGroup(actionGroupViewMode);
184     m_d->actionViewModeIcon->setText(
185         i18nc("Set the gradient chooser to show icons instead of a list", "Icon view")
186     );
187     m_d->actionViewModeList = new QAction(this);
188     m_d->actionViewModeList->setCheckable(true);
189     m_d->actionViewModeList->setActionGroup(actionGroupViewMode);
190     m_d->actionViewModeList->setText(
191         i18nc("Set the gradient chooser to show a list instead of icons", "List view")
192     );
193     QAction *separatorViewMode1 = new QAction(this);
194     separatorViewMode1->setSeparator(true);
195     QActionGroup *actionGroupItemSize = new QActionGroup(this);
196     m_d->actionItemSizeSmall = new QAction(this);
197     m_d->actionItemSizeSmall->setCheckable(true);
198     m_d->actionItemSizeSmall->setActionGroup(actionGroupItemSize);
199     m_d->actionItemSizeSmall->setText(
200         i18nc("Set the gradient chooser to show small items", "Small items")
201     );
202     m_d->actionItemSizeMedium = new QAction(this);
203     m_d->actionItemSizeMedium->setCheckable(true);
204     m_d->actionItemSizeMedium->setActionGroup(actionGroupItemSize);
205     m_d->actionItemSizeMedium->setText(
206         i18nc("Set the gradient chooser to show medium size items", "Medium size items")
207     );
208     m_d->actionItemSizeLarge = new QAction(this);
209     m_d->actionItemSizeLarge->setCheckable(true);
210     m_d->actionItemSizeLarge->setActionGroup(actionGroupItemSize);
211     m_d->actionItemSizeLarge->setText(
212         i18nc("Set the gradient chooser to show large items", "Large items")
213     );
214     m_d->actionItemSizeCustom = new QAction(this);
215     m_d->actionItemSizeCustom->setCheckable(true);
216     m_d->actionItemSizeCustom->setActionGroup(actionGroupItemSize);
217     m_d->actionItemSizeCustom->setText(
218         i18nc("Set the gradient chooser to show custom size items", "Custom size items")
219     );
220     m_d->sliderItemSizeCustom = new QSlider(this);
221     m_d->sliderItemSizeCustom->setRange(16, 128);
222     m_d->sliderItemSizeCustom->setOrientation(Qt::Horizontal);
223     m_d->containerSliderItemSizeCustom = new QWidget(this);
224     QVBoxLayout *layoutContainerSliderItemSizeCustom = new QVBoxLayout;
225     layoutContainerSliderItemSizeCustom->addWidget(m_d->sliderItemSizeCustom);
226     m_d->containerSliderItemSizeCustom->setLayout(layoutContainerSliderItemSizeCustom);
227     QWidgetAction *widgetActionSliderItemSizeCustom = new QWidgetAction(this);
228     widgetActionSliderItemSizeCustom->setDefaultWidget(m_d->containerSliderItemSizeCustom);
229     QToolButton *toolButtonItemChooserViewMode = m_d->itemChooser->viewModeButton();
230     toolButtonItemChooserViewMode->addActions(actionGroupViewMode->actions());
231     toolButtonItemChooserViewMode->addAction(separatorViewMode1);
232     toolButtonItemChooserViewMode->addActions(actionGroupItemSize->actions());
233     toolButtonItemChooserViewMode->addAction(widgetActionSliderItemSizeCustom);
234 
235     // Edit widgets
236     QHBoxLayout* layoutEditWidgets = new QHBoxLayout;
237     layoutEditWidgets->setMargin(0);
238     m_d->containerEditWidgets = new QWidget(this);
239 
240     m_d->buttonAddGradient = new QToolButton(this);
241     m_d->buttonAddGradient->setText(i18n("Add..."));
242     m_d->buttonAddGradient->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
243     layoutEditWidgets->addWidget(m_d->buttonAddGradient);
244 
245     QMenu *menuAddGradient = new QMenu(m_d->buttonAddGradient);
246     QAction* addStopGradient = new QAction(i18n("Stop gradient"), this);
247     menuAddGradient->addAction(addStopGradient);
248     QAction* addSegmentedGradient = new QAction(i18n("Segmented gradient"), this);
249     menuAddGradient->addAction(addSegmentedGradient);
250 
251     m_d->buttonAddGradient->setMenu(menuAddGradient);
252     m_d->buttonAddGradient->setPopupMode(QToolButton::MenuButtonPopup);
253 
254     m_d->buttonEditGradient = new QPushButton();
255     m_d->buttonEditGradient->setText(i18n("Edit..."));
256     m_d->buttonEditGradient->setEnabled(false);
257     layoutEditWidgets->addWidget(m_d->buttonEditGradient);
258 
259     m_d->containerEditWidgets->setLayout(layoutEditWidgets);
260 
261     // Layout
262     QVBoxLayout *mainLayout = new QVBoxLayout(this);
263     mainLayout->setObjectName("main layout");
264     mainLayout->setMargin(0);
265     mainLayout->addWidget(m_d->labelName);
266     mainLayout->addWidget(m_d->itemChooser);
267     mainLayout->addWidget(m_d->containerEditWidgets);
268     setLayout(mainLayout);
269 
270     // Resource item chooser connections
271     connect(m_d->itemChooser, SIGNAL(resourceSelected(KoResource*)),
272             m_d.data(), SLOT(update(KoResource*)));
273     connect(m_d->itemChooser, SIGNAL(resourceSelected(KoResource*)),
274             this, SIGNAL(resourceSelected(KoResource*)));
275     connect(m_d->itemChooser, SIGNAL(resourceClicked(KoResource*)),
276             this, SIGNAL(resourceClicked(KoResource*)));
277     connect(m_d->itemChooser->itemView(), SIGNAL(sigSizeChanged()),
278             m_d.data(), SLOT(on_itemChooserItemView_sigSizeChanged()));
279     // View menu
280     connect(actionGroupViewMode, SIGNAL(triggered(QAction*)),
281             m_d.data(), SLOT(on_actionGroupViewMode_triggered(QAction*)));
282     connect(actionGroupItemSize, SIGNAL(triggered(QAction*)),
283             m_d.data(), SLOT(on_actionGroupItemSize_triggered(QAction*)));
284     connect(m_d->sliderItemSizeCustom, SIGNAL(valueChanged(int)),
285             m_d.data(), SLOT(on_sliderItemSizeCustom_valueChanged(int)));
286     // Edit widgets
287     connect(m_d->buttonAddGradient, SIGNAL(clicked()), m_d.data(), SLOT(addStopGradient()));
288     connect(addStopGradient, SIGNAL(triggered(bool)), m_d.data(), SLOT(addStopGradient()));
289     connect(addSegmentedGradient, SIGNAL(triggered(bool)), m_d.data(), SLOT(addSegmentedGradient()));
290     connect(m_d->buttonEditGradient, SIGNAL(clicked()), m_d.data(), SLOT(editGradient()));
291 
292     m_d->isNameLabelVisible = true;
293     m_d->areEditOptionsVisible = true;
294     slotUpdateIcons();
295 
296     m_d->useGlobalViewOptions = useGlobalViewOptions;
297     if (useGlobalViewOptions) {
298         m_d->viewOptions = &(m_d->globalViewOptions);
299         if (m_d->globalChoosers.size() == 0) {
300             loadViewSettings();
301         }
302         m_d->globalChoosers.insert(this);
303     } else {
304         m_d->viewOptions = new Private::ViewOptions;
305     }
306     m_d->updatePresetChooser();
307     m_d->updateContainerSliderItemSizeCustom();
308 }
309 
~KisGradientChooser()310 KisGradientChooser::~KisGradientChooser()
311 {
312     if (m_d->useGlobalViewOptions) {
313         m_d->globalChoosers.remove(this);
314         if (m_d->globalChoosers.size() == 0) {
315             saveViewSettings();
316         }
317     } else {
318         delete m_d->viewOptions;
319     }
320 }
321 
currentResource()322 KoResource *KisGradientChooser::currentResource()
323 {
324     return m_d->itemChooser->currentResource();
325 }
326 
setCurrentResource(KoResource * resource)327 void KisGradientChooser::setCurrentResource(KoResource *resource)
328 {
329     m_d->itemChooser->setCurrentResource(resource);
330 }
331 
setCurrentItem(int row,int column)332 void KisGradientChooser::setCurrentItem(int row, int column)
333 {
334     m_d->itemChooser->setCurrentItem(row, column);
335     if (currentResource()) {
336         m_d->update(currentResource());
337     }
338 }
339 
loadViewSettings(const QString & prefix)340 void KisGradientChooser::loadViewSettings(const QString &prefix)
341 {
342     KConfigGroup configGroup(KSharedConfig::openConfig(), "GradientChooser");
343     const QString pfx = prefix.isEmpty() ? QString("global/") : prefix + "/";
344 
345     QString strViewMode = configGroup.readEntry(pfx + "viewMode", QString());
346     if (strViewMode == "icon") {
347         m_d->viewOptions->viewMode = ViewMode_Icon;
348     } else if (strViewMode == "list") {
349         m_d->viewOptions->viewMode = ViewMode_List;
350     }
351     QString strItemSize = configGroup.readEntry(pfx + "itemSize", QString());
352     if (strItemSize == "small") {
353         m_d->viewOptions->itemSize = ItemSize_Small;
354     } else if (strItemSize == "medium") {
355         m_d->viewOptions->itemSize = ItemSize_Medium;
356     } else if (strItemSize == "large") {
357         m_d->viewOptions->itemSize = ItemSize_Large;
358     } else if (strItemSize == "custom") {
359         m_d->viewOptions->itemSize = ItemSize_Custom;
360     }
361     m_d->viewOptions->itemSizeCustom = configGroup.readEntry(pfx + "itemSizeCustom", m_d->viewOptions->itemSizeCustom);
362 
363     m_d->updatePresetChooser();
364 }
365 
saveViewSettings(const QString & prefix)366 void KisGradientChooser::saveViewSettings(const QString &prefix)
367 {
368     KConfigGroup configGroup(KSharedConfig::openConfig(), "GradientChooser");
369     const QString pfx = prefix.isEmpty() ? QString("global/") : prefix + "/";
370 
371     if (m_d->viewOptions->viewMode == ViewMode_Icon) {
372         configGroup.writeEntry(pfx + "viewMode", "icon");
373     } else {
374         configGroup.writeEntry(pfx + "viewMode", "list");
375     }
376     if (m_d->viewOptions->itemSize == ItemSize_Small) {
377         configGroup.writeEntry(pfx + "itemSize", "small");
378     } else if (m_d->viewOptions->itemSize == ItemSize_Medium) {
379         configGroup.writeEntry(pfx + "itemSize", "medium");
380     } else if (m_d->viewOptions->itemSize == ItemSize_Large) {
381         configGroup.writeEntry(pfx + "itemSize", "large");
382     } else {
383         configGroup.writeEntry(pfx + "itemSize", "custom");
384     }
385     configGroup.writeEntry(pfx + "itemSizeCustom", m_d->viewOptions->itemSizeCustom);
386 }
387 
viewMode() const388 KisGradientChooser::ViewMode KisGradientChooser::viewMode() const
389 {
390     return m_d->viewOptions->viewMode;
391 }
392 
itemSize() const393 KisGradientChooser::ItemSize KisGradientChooser::itemSize() const
394 {
395     return m_d->viewOptions->itemSize;
396 }
397 
itemSizeCustom() const398 int KisGradientChooser::itemSizeCustom() const
399 {
400     return m_d->viewOptions->itemSizeCustom;
401 }
402 
resourceItemChooser() const403 KoResourceItemChooser* KisGradientChooser::resourceItemChooser() const
404 {
405     return m_d->itemChooser;
406 }
407 
isNameLabelVisible() const408 bool KisGradientChooser::isNameLabelVisible() const
409 {
410     return m_d->isNameLabelVisible;
411 }
412 
areEditOptionsVisible() const413 bool KisGradientChooser::areEditOptionsVisible() const
414 {
415     return m_d->areEditOptionsVisible;
416 }
417 
setViewMode(ViewMode newViewMode)418 void KisGradientChooser::setViewMode(ViewMode newViewMode)
419 {
420     m_d->viewOptions->viewMode = newViewMode;
421     m_d->updatePresetChooser();
422 }
423 
setItemSize(ItemSize newItemSize)424 void KisGradientChooser::setItemSize(ItemSize newItemSize)
425 {
426     m_d->viewOptions->itemSize = newItemSize;
427     m_d->updatePresetChooser();
428 }
429 
setItemSizeCustom(int newSize)430 void KisGradientChooser::setItemSizeCustom(int newSize)
431 {
432     if (newSize == m_d->viewOptions->itemSizeCustom) {
433         return;
434     }
435 
436     m_d->viewOptions->itemSizeCustom = newSize;
437     m_d->updatePresetChooser();
438 }
439 
setNameLabelVisible(bool newNameLabelVisible)440 void KisGradientChooser::setNameLabelVisible(bool newNameLabelVisible)
441 {
442     m_d->isNameLabelVisible = newNameLabelVisible;
443     m_d->labelName->setVisible(newNameLabelVisible);
444 }
445 
setEditOptionsVisible(bool newEditOptionsVisible)446 void KisGradientChooser::setEditOptionsVisible(bool newEditOptionsVisible)
447 {
448     m_d->areEditOptionsVisible = newEditOptionsVisible;
449     m_d->containerEditWidgets->setVisible(newEditOptionsVisible);
450 }
451 
slotUpdateIcons()452 void KisGradientChooser::slotUpdateIcons()
453 {
454     if (m_d->buttonAddGradient && m_d->buttonEditGradient) {
455         m_d->buttonAddGradient->setIcon(KisIconUtils::loadIcon("list-add"));
456         m_d->buttonEditGradient->setIcon(KisIconUtils::loadIcon("configure"));
457     }
458 }
459 
setForegroundColor(KoColor color)460 void KisGradientChooser::setForegroundColor(KoColor color) {
461     m_d->foregroundColor = color;
462 }
463 
setBackgroundColor(KoColor color)464 void KisGradientChooser::setBackgroundColor(KoColor color) {
465     m_d->backgroundColor = color;
466 }
467 
event(QEvent * e)468 bool KisGradientChooser::event(QEvent *e)
469 {
470     if (e->type() == QEvent::StyleChange) {
471         m_d->updateContainerSliderItemSizeCustom();
472     } else if (e->type() == QEvent::Resize) {
473         QFrame::resizeEvent(static_cast<QResizeEvent*>(e));
474         m_d->labelName->setMaximumWidth(width());
475         return true;
476     }
477     return false;
478 }
479 
update(KoResource * resource)480 void KisGradientChooser::Private::update(KoResource * resource)
481 {
482     KoAbstractGradient *gradient = static_cast<KoAbstractGradient *>(resource);
483     labelName->setFixedWidth(itemChooser->width());
484     labelName->setText(gradient ? i18n(gradient->name().toUtf8().data()) : "");
485     buttonEditGradient->setEnabled(gradient && gradient->removable());
486 }
487 
addStopGradient()488 void KisGradientChooser::Private::addStopGradient()
489 {
490     KoStopGradient* gradient = new KoStopGradient("");
491 
492     QList<KoGradientStop> stops;
493     stops << KoGradientStop(0.0, KoColor(QColor(250, 250, 0), KoColorSpaceRegistry::instance()->rgb8()), COLORSTOP)
494         << KoGradientStop(1.0, KoColor(QColor(255, 0, 0, 255), KoColorSpaceRegistry::instance()->rgb8()), COLORSTOP);
495     gradient->setType(QGradient::LinearGradient);
496     gradient->setStops(stops);
497     addGradient(gradient);
498 }
499 
addSegmentedGradient()500 void KisGradientChooser::Private::addSegmentedGradient()
501 {
502     KoSegmentGradient* gradient = new KoSegmentGradient("");
503     gradient->createSegment(INTERP_LINEAR, COLOR_INTERP_RGB, 0.0, 1.0, 0.5, Qt::black, Qt::white);
504     gradient->setName(i18n("unnamed"));
505     addGradient(gradient);
506 }
507 
addGradient(KoAbstractGradient * gradient)508 void KisGradientChooser::Private::addGradient(KoAbstractGradient* gradient)
509 {
510     if (!gradient) return;
511 
512     KoResourceServer<KoAbstractGradient> * rserver = KoResourceServerProvider::instance()->gradientServer();
513     QString saveLocation = rserver->saveLocation();
514 
515     KisCustomGradientDialog dialog(gradient, q, "KisCustomGradientDialog", foregroundColor, backgroundColor);
516     dialog.exec();
517     gradient->setFilename(saveLocation + gradient->name() + gradient->defaultFileExtension());
518     gradient->setValid(true);
519     rserver->addResource(gradient);
520     itemChooser->setCurrentResource(gradient);
521 }
522 
editGradient()523 void KisGradientChooser::Private::editGradient()
524 {
525     KoAbstractGradient *gradient = dynamic_cast<KoAbstractGradient*>(q->currentResource());
526     if (!gradient) return;
527 
528     KisCustomGradientDialog dialog(gradient, q, "KisCustomGradientDialog", foregroundColor, backgroundColor);
529     dialog.exec();
530 
531     KoResourceServer<KoAbstractGradient> * rserver = KoResourceServerProvider::instance()->gradientServer();
532     QString saveLocation = rserver->saveLocation();
533     rserver->updateResource(gradient);
534     gradient->setFilename(saveLocation + gradient->name() + gradient->defaultFileExtension());
535     gradient->save();
536 }
537 
on_itemChooserItemView_sigSizeChanged()538 void KisGradientChooser::Private::on_itemChooserItemView_sigSizeChanged()
539 {
540     updatePresetChooserIcons();
541 }
542 
on_actionGroupViewMode_triggered(QAction * triggeredAction)543 void KisGradientChooser::Private::on_actionGroupViewMode_triggered(QAction *triggeredAction)
544 {
545     if (triggeredAction == actionViewModeIcon) {
546         q->setViewMode(ViewMode_Icon);
547     } else {
548         q->setViewMode(ViewMode_List);
549     }
550 }
551 
on_actionGroupItemSize_triggered(QAction * triggeredAction)552 void KisGradientChooser::Private::on_actionGroupItemSize_triggered(QAction *triggeredAction)
553 {
554     if (triggeredAction == actionItemSizeSmall) {
555         q->setItemSize(ItemSize_Small);
556     } else if (triggeredAction == actionItemSizeMedium) {
557         q->setItemSize(ItemSize_Medium);
558     } else if (triggeredAction == actionItemSizeLarge) {
559         q->setItemSize(ItemSize_Large);
560     } else {
561         q->setItemSize(ItemSize_Custom);
562     }
563 }
564 
on_sliderItemSizeCustom_valueChanged(int newValue)565 void KisGradientChooser::Private::on_sliderItemSizeCustom_valueChanged(int newValue)
566 {
567     q->setItemSizeCustom(newValue);
568     q->setItemSize(ItemSize_Custom);
569 }
570 
updatePresetChooser(bool globalUpdate)571 void KisGradientChooser::Private::updatePresetChooser(bool globalUpdate)
572 {
573     if (useGlobalViewOptions && globalUpdate) {
574         for (KisGradientChooser *chooser : globalChoosers) {
575             chooser->m_d->updatePresetChooser(false);
576         }
577         return;
578     }
579 
580     updatePresetChooserIcons();
581 
582     if (viewOptions->viewMode == ViewMode_Icon) {
583         actionViewModeIcon->setChecked(true);
584     } else {
585         actionViewModeList->setChecked(true);
586     }
587     if (viewOptions->itemSize == ItemSize_Small) {
588         actionItemSizeSmall->setChecked(true);
589     } else if (viewOptions->itemSize == ItemSize_Medium) {
590         actionItemSizeMedium->setChecked(true);
591     } else if (viewOptions->itemSize == ItemSize_Large) {
592         actionItemSizeLarge->setChecked(true);
593     } else {
594         actionItemSizeCustom->setChecked(true);
595     }
596     {
597         KisSignalsBlocker signalsBlocker(sliderItemSizeCustom);
598         sliderItemSizeCustom->setValue(viewOptions->itemSizeCustom);
599     }
600 }
601 
updatePresetChooserIcons()602 void KisGradientChooser::Private::updatePresetChooserIcons()
603 {
604     int rowHeight, columnWidth;
605     if (viewOptions->itemSize == ItemSize_Small) {
606         rowHeight = viewOptions->itemSizeSmall;
607     } else if (viewOptions->itemSize == ItemSize_Medium) {
608         rowHeight = viewOptions->itemSizeMedium;
609     } else if (viewOptions->itemSize == ItemSize_Large) {
610         rowHeight = viewOptions->itemSizeLarge;
611     } else {
612         rowHeight = viewOptions->itemSizeCustom;
613     }
614 
615     int nColumns;
616     const int scrollbarWidth = q->style()->pixelMetric(QStyle::PM_ScrollBarExtent);
617     const int frameWidth = itemChooser->itemView()->frameWidth();
618     const int itemViewWidth = itemChooser->itemView()->width();
619     if (viewOptions->viewMode == ViewMode_Icon) {
620         columnWidth = static_cast<int>(qRound(rowHeight * viewOptions->itemSizeWidthFactor));
621         nColumns = (itemViewWidth - frameWidth - scrollbarWidth) / columnWidth;
622         if (nColumns == 0) {
623             nColumns = 1;
624         }
625     } else {
626         columnWidth = itemViewWidth - frameWidth - scrollbarWidth;
627         nColumns = 1;
628     }
629 
630     itemChooser->setRowHeight(rowHeight);
631     itemChooser->setColumnWidth(columnWidth);
632     itemChooser->setColumnCount(nColumns);
633 }
634 
updateContainerSliderItemSizeCustom()635 void KisGradientChooser::Private::updateContainerSliderItemSizeCustom()
636 {
637     const int marginSize = q->style()->pixelMetric(QStyle::PM_ButtonMargin);
638     const int indicatorSize = q->style()->pixelMetric(QStyle::PM_ExclusiveIndicatorWidth);
639     const int spacingSize = q->style()->pixelMetric(QStyle::PM_RadioButtonLabelSpacing);
640     containerSliderItemSizeCustom->layout()->setContentsMargins(
641         indicatorSize + spacingSize, marginSize, marginSize, marginSize
642     );
643 }
644 
645 #include "kis_gradient_chooser.moc"
646