File indexing completed on 2025-01-26 04:10:27

0001 /*
0002  *  dlg_shrink_selection.cc - part of Krita
0003  *
0004  *  SPDX-FileCopyrightText: 2006 Michael Thaler <michael.thaler@physik.tu-muenchen.de>
0005  *  SPDX-FileCopyrightText: 2013 Juan Palacios <jpalaciosdev@gmail.com>
0006  *
0007  *  SPDX-License-Identifier: GPL-2.0-or-later
0008  */
0009 
0010 #include "dlg_shrink_selection.h"
0011 
0012 #include <KoUnit.h>
0013 #include <kis_size_group.h>
0014 #include <KisViewManager.h>
0015 #include <kis_image.h>
0016 #include <operations/kis_operation_configuration.h>
0017 
0018 WdgShrinkSelection::WdgShrinkSelection(QWidget* parent, KisViewManager *view, KisOperationConfigurationSP config)
0019     : KisOperationUIWidget(i18n("Shrink Selection"), parent)
0020     , m_shrinkValue(config->getInt("x-radius", 1))
0021     , m_shrinkFromImageBorder(!config->getBool("edgeLock", false))
0022 {
0023     Q_ASSERT(view);
0024     KisImageWSP image = view->image();
0025     Q_ASSERT(image);
0026     m_resolution = image->yRes();
0027 
0028     setupUi(this);
0029 
0030     spbShrinkValue->setValue(m_shrinkValue);
0031     spbShrinkValue->setFocus();
0032     spbShrinkValue->setVisible(true);
0033     spbShrinkValueDouble->setVisible(false);
0034 
0035     cmbUnit->addItems(KoUnit::listOfUnitNameForUi());
0036     cmbUnit->setCurrentIndex(KoUnit(KoUnit::Pixel).indexInListForUi());
0037     ckbShrinkFromImageBorder->setChecked(m_shrinkFromImageBorder);
0038 
0039     // ensure that both spinboxes request the same horizontal size
0040     KisSizeGroup *spbGroup = new KisSizeGroup(this);
0041     spbGroup->addWidget(spbShrinkValue);
0042     spbGroup->addWidget(spbShrinkValueDouble);
0043 
0044     connect(spbShrinkValue, SIGNAL(valueChanged(int)), this, SLOT(slotShrinkValueChanged(int)));
0045     connect(spbShrinkValueDouble, SIGNAL(valueChanged(double)), this, SLOT(slotShrinkValueChanged(double)));
0046     connect(cmbUnit, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUnitChanged(int)));
0047     connect(ckbShrinkFromImageBorder, SIGNAL(toggled(bool)), this, SLOT(slotShrinkFromImageBorderChanged(bool)));
0048 }
0049 
0050 void WdgShrinkSelection::slotShrinkValueChanged(int value)
0051 {
0052     slotShrinkValueChanged((double) value);
0053 }
0054 
0055 void WdgShrinkSelection::slotShrinkValueChanged(double value)
0056 {
0057     const KoUnit selectedUnit = KoUnit::fromListForUi(cmbUnit->currentIndex());
0058     const double resValue = (selectedUnit == KoUnit(KoUnit::Pixel)) ? value : (value * m_resolution);
0059     m_shrinkValue = qRound(selectedUnit.fromUserValue(resValue));
0060 }
0061 
0062 void WdgShrinkSelection::slotUnitChanged(int index)
0063 {
0064     updateShrinkUIValue(m_shrinkValue);
0065 
0066     const KoUnit selectedUnit = KoUnit::fromListForUi(index);
0067     if (selectedUnit != KoUnit(KoUnit::Pixel)) {
0068         spbShrinkValue->setVisible(false);
0069         spbShrinkValueDouble->setVisible(true);
0070     } else {
0071         spbShrinkValue->setVisible(true);
0072         spbShrinkValueDouble->setVisible(false);
0073     }
0074 }
0075 
0076 void WdgShrinkSelection::updateShrinkUIValue(double value)
0077 {
0078     const KoUnit selectedUnit = KoUnit::fromListForUi(cmbUnit->currentIndex());
0079     if (selectedUnit != KoUnit(KoUnit::Pixel)) {
0080         spbShrinkValueDouble->blockSignals(true);
0081         spbShrinkValueDouble->setValue(selectedUnit.toUserValue(value / m_resolution));
0082         spbShrinkValueDouble->blockSignals(false);
0083     } else {
0084         const int finalValue = (selectedUnit == KoUnit(KoUnit::Point)) ? qRound(value / m_resolution) : value;
0085         spbShrinkValue->blockSignals(true);
0086         spbShrinkValue->setValue(selectedUnit.toUserValue(finalValue));
0087         spbShrinkValue->blockSignals(false);
0088     }
0089 }
0090 void WdgShrinkSelection::slotShrinkFromImageBorderChanged(bool value)
0091 {
0092     m_shrinkFromImageBorder = value;
0093 }
0094 
0095 void WdgShrinkSelection::getConfiguration(KisOperationConfigurationSP config)
0096 {
0097     config->setProperty("x-radius", m_shrinkValue);
0098     config->setProperty("y-radius", m_shrinkValue);
0099     config->setProperty("edgeLock", !m_shrinkFromImageBorder);
0100 }
0101