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

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2010 Sven Langkamp <sven.langkamp@gmail.com>
0003  * SPDX-FileCopyrightText: 2011 Srikanth Tiyyagura <srikanth.tulasiram@gmail.com>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 
0008 #include "kis_paintop_presets_chooser_popup.h"
0009 
0010 #include <QToolButton>
0011 #include <QCompleter>
0012 #include <QMenu>
0013 #include <QWidgetAction>
0014 #include <QSlider>
0015 
0016 #include <KoResource.h>
0017 #include <KisResourceItemChooser.h>
0018 
0019 #include <ui_wdgpaintoppresets.h>
0020 #include <kis_config.h>
0021 #include <KisResourceServerProvider.h>
0022 #include <brushengine/kis_paintop_preset.h>
0023 #include <kis_icon.h>
0024 #include <brushengine/kis_paintop_settings.h>
0025 #include "KisPopupButton.h"
0026 
0027 struct KisPaintOpPresetsChooserPopup::Private
0028 {
0029 public:
0030     Ui_WdgPaintOpPresets uiWdgPaintOpPresets;
0031     bool firstShown {true};
0032     QSlider* iconSizeSlider {nullptr};
0033     KisPopupButton *viewModeButton {nullptr};
0034 };
0035 
0036 KisPaintOpPresetsChooserPopup::KisPaintOpPresetsChooserPopup(QWidget * parent)
0037     : QWidget(parent)
0038     , m_d(new Private())
0039 {
0040     m_d->uiWdgPaintOpPresets.setupUi(this);
0041     QMenu* menu = new QMenu(this);
0042     menu->setStyleSheet("margin: 6px");
0043 
0044     menu->addSection(i18nc("@title Which elements to display (e.g., thumbnails or details)", "Display"));
0045 
0046     QActionGroup *actionGroup = new QActionGroup(this);
0047 
0048     KisPresetChooser::ViewMode mode = (KisPresetChooser::ViewMode)KisConfig(true).presetChooserViewMode();
0049 
0050     QAction* action = menu->addAction(KisIconUtils::loadIcon("view-preview"), i18n("Thumbnails"), this, SLOT(slotThumbnailMode()));
0051     action->setCheckable(true);
0052     action->setChecked(mode == KisPresetChooser::THUMBNAIL);
0053     action->setActionGroup(actionGroup);
0054 
0055     action = menu->addAction(KisIconUtils::loadIcon("view-list-details"), i18n("Details"), this, SLOT(slotDetailMode()));
0056     action->setCheckable(true);
0057     action->setChecked(mode == KisPresetChooser::DETAIL);
0058     action->setActionGroup(actionGroup);
0059 
0060     // add widget slider to control icon size
0061     QSlider* iconSizeSlider = new QSlider(this);
0062     iconSizeSlider->setOrientation(Qt::Horizontal);
0063     iconSizeSlider->setRange(30, 80);
0064     iconSizeSlider->setValue(m_d->uiWdgPaintOpPresets.wdgPresetChooser->iconSize());
0065     iconSizeSlider->setMinimumHeight(20);
0066     iconSizeSlider->setMinimumWidth(40);
0067     iconSizeSlider->setTickInterval(10);
0068     m_d->iconSizeSlider = iconSizeSlider;
0069 
0070     QWidgetAction *sliderAction= new QWidgetAction(this);
0071     sliderAction->setDefaultWidget(iconSizeSlider);
0072 
0073     menu->addSection(i18n("Icon Size"));
0074     menu->addAction(sliderAction);
0075 
0076 
0077 
0078     // setting the view mode
0079     m_d->uiWdgPaintOpPresets.wdgPresetChooser->setViewMode(mode);
0080     m_d->uiWdgPaintOpPresets.wdgPresetChooser->showTaggingBar(true);
0081 
0082     m_d->uiWdgPaintOpPresets.wdgPresetChooser->itemChooser()->showViewModeBtn(true);
0083     m_d->viewModeButton = m_d->uiWdgPaintOpPresets.wdgPresetChooser->itemChooser()->viewModeButton();
0084     m_d->viewModeButton->setPopupWidget(menu);
0085 
0086 
0087     connect(m_d->uiWdgPaintOpPresets.wdgPresetChooser, SIGNAL(resourceSelected(KoResourceSP )),
0088             this, SIGNAL(resourceSelected(KoResourceSP )));
0089     connect(m_d->uiWdgPaintOpPresets.wdgPresetChooser, SIGNAL(resourceClicked(KoResourceSP )),
0090             this, SIGNAL(resourceClicked(KoResourceSP ))) ;
0091 
0092 
0093     connect(iconSizeSlider, SIGNAL(valueChanged(int)),
0094             m_d->uiWdgPaintOpPresets.wdgPresetChooser, SLOT(setIconSize(int)));
0095     connect(menu, SIGNAL(aboutToHide()),
0096             m_d->uiWdgPaintOpPresets.wdgPresetChooser, SLOT(saveIconSize()));
0097     connect(m_d->viewModeButton, SIGNAL(pressed()), this, SLOT(slotUpdateMenu()));
0098 
0099 }
0100 
0101 KisPaintOpPresetsChooserPopup::~KisPaintOpPresetsChooserPopup()
0102 {
0103     delete m_d;
0104 }
0105 
0106 void KisPaintOpPresetsChooserPopup::slotThumbnailMode()
0107 {
0108     KisConfig(false).setPresetChooserViewMode(KisPresetChooser::THUMBNAIL);
0109     m_d->uiWdgPaintOpPresets.wdgPresetChooser->setViewMode(KisPresetChooser::THUMBNAIL);
0110 }
0111 
0112 void KisPaintOpPresetsChooserPopup::slotDetailMode()
0113 {
0114     KisConfig(false).setPresetChooserViewMode(KisPresetChooser::DETAIL);
0115     m_d->uiWdgPaintOpPresets.wdgPresetChooser->setViewMode(KisPresetChooser::DETAIL);
0116 }
0117 
0118 void KisPaintOpPresetsChooserPopup::slotUpdateMenu()
0119 {
0120     QSignalBlocker b(m_d->iconSizeSlider);
0121     m_d->iconSizeSlider->setValue(KisConfig(true).presetIconSize());
0122 }
0123 
0124 void KisPaintOpPresetsChooserPopup::paintEvent(QPaintEvent* event)
0125 {
0126     QWidget::paintEvent(event);
0127     //Workaround to get the column and row size right
0128     if(m_d->firstShown) {
0129         m_d->uiWdgPaintOpPresets.wdgPresetChooser->updateViewSettings();
0130         m_d->firstShown = false;
0131     }
0132 }
0133 
0134 void KisPaintOpPresetsChooserPopup::canvasResourceChanged(KisPaintOpPresetSP  preset)
0135 {
0136     if (preset) {
0137         blockSignals(true);
0138         m_d->uiWdgPaintOpPresets.wdgPresetChooser->setCurrentResource(preset);
0139         blockSignals(false);
0140     }
0141     m_d->uiWdgPaintOpPresets.wdgPresetChooser->updateViewSettings();
0142 }
0143 
0144 void KisPaintOpPresetsChooserPopup::slotThemeChanged()
0145 {
0146    m_d->viewModeButton->setIcon(KisIconUtils::loadIcon("view-choose"));
0147    m_d->uiWdgPaintOpPresets.wdgPresetChooser->itemChooser()->updateView(); // updates other icons
0148 }
0149 
0150 void KisPaintOpPresetsChooserPopup::updateViewSettings()
0151 {
0152    m_d->uiWdgPaintOpPresets.wdgPresetChooser->updateViewSettings();
0153 }
0154 
0155 void KisPaintOpPresetsChooserPopup::setResponsiveness(bool value)
0156 {
0157     m_d->uiWdgPaintOpPresets.wdgPresetChooser->itemChooser()->setResponsiveness(value);
0158 }