diff --git a/plugins/extensions/imagesize/dlg_layersize.cc b/plugins/extensions/imagesize/dlg_layersize.cc index 429dcdd72c..0ad543fd65 100644 --- a/plugins/extensions/imagesize/dlg_layersize.cc +++ b/plugins/extensions/imagesize/dlg_layersize.cc @@ -1,260 +1,197 @@ /* * dlg_layersize.cc - part of Krita * * Copyright (c) 2004 Boudewijn Rempt * Copyright (c) 2005 Sven Langkamp * Copyright (c) 2013 Juan Palacios * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "dlg_layersize.h" #include #include +#include + #include // XXX: I'm really real bad at arithmetic, let alone math. Here // be rounding errors. (Boudewijn) static const QString pixelStr(KoUnit::unitDescription(KoUnit::Pixel)); static const QString percentStr(i18n("Percent (%)")); DlgLayerSize::DlgLayerSize(QWidget * parent, const char * name, int width, int height, double resolution) : KoDialog(parent) , m_aspectRatio(((double) width) / height) , m_originalWidth(width) , m_originalHeight(height) , m_width(width) , m_height(height) , m_resolution(resolution) , m_keepAspect(true) { setCaption(i18n("Layer Size")); setObjectName(name); setButtons(Ok | Cancel); setDefaultButton(Ok); m_page = new WdgLayerSize(this); Q_CHECK_PTR(m_page); m_page->layout()->setMargin(0); m_page->setObjectName(name); - m_page->newWidth->setValue(width); - m_page->newWidth->setFocus(); - m_page->newHeight->setValue(height); + _widthUnitManager = new KisDocumentAwareSpinBoxUnitManager(this); + _heightUnitManager = new KisDocumentAwareSpinBoxUnitManager(this, KisDocumentAwareSpinBoxUnitManager::PIX_DIR_Y); + + _widthUnitManager->setApparentUnitFromSymbol("px"); + _heightUnitManager->setApparentUnitFromSymbol("px"); - m_page->newWidthDouble->setVisible(false); - m_page->newHeightDouble->setVisible(false); + m_page->newWidthDouble->setUnitManager(_widthUnitManager); + m_page->newHeightDouble->setUnitManager(_heightUnitManager); + m_page->newWidthDouble->setDecimals(2); + m_page->newHeightDouble->setDecimals(2); + m_page->newWidthDouble->setDisplayUnit(false); + m_page->newHeightDouble->setDisplayUnit(false); + + m_page->newWidthDouble->setValue(width); + m_page->newWidthDouble->setFocus(); + m_page->newHeightDouble->setValue(height); m_page->filterCmb->setIDList(KisFilterStrategyRegistry::instance()->listKeys()); m_page->filterCmb->setToolTip(KisFilterStrategyRegistry::instance()->formatedDescriptions()); m_page->filterCmb->setCurrent("Bicubic"); - m_page->newWidthUnit->addItems(KoUnit::listOfUnitNameForUi()); - m_page->newWidthUnit->addItem(percentStr); - - m_page->newHeightUnit->addItems(KoUnit::listOfUnitNameForUi()); - m_page->newHeightUnit->addItem(percentStr); + m_page->newWidthUnit->setModel(_widthUnitManager); + m_page->newHeightUnit->setModel(_heightUnitManager); - const int pixelUnitIndex = KoUnit(KoUnit::Pixel).indexInListForUi(); + const int pixelUnitIndex = _widthUnitManager->getsUnitSymbolList().indexOf("px"); //TODO: have a better way to identify units. m_page->newWidthUnit->setCurrentIndex(pixelUnitIndex); m_page->newHeightUnit->setCurrentIndex(pixelUnitIndex); m_page->aspectRatioBtn->setKeepAspectRatio(true); m_page->constrainProportionsCkb->setChecked(true); setMainWidget(m_page); connect(this, SIGNAL(okClicked()), this, SLOT(accept())); connect(m_page->aspectRatioBtn, SIGNAL(keepAspectRatioChanged(bool)), this, SLOT(slotAspectChanged(bool))); connect(m_page->constrainProportionsCkb, SIGNAL(toggled(bool)), this, SLOT(slotAspectChanged(bool))); - connect(m_page->newWidth, SIGNAL(valueChanged(int)), this, SLOT(slotWidthChanged(int))); - connect(m_page->newHeight, SIGNAL(valueChanged(int)), this, SLOT(slotHeightChanged(int))); - connect(m_page->newWidthDouble, SIGNAL(valueChanged(double)), this, SLOT(slotWidthChanged(double))); - connect(m_page->newHeightDouble, SIGNAL(valueChanged(double)), this, SLOT(slotHeightChanged(double))); - connect(m_page->newWidthUnit, SIGNAL(currentIndexChanged(int)), this, SLOT(slotWidthUnitChanged(int))); - connect(m_page->newHeightUnit, SIGNAL(currentIndexChanged(int)), this, SLOT(slotHeightUnitChanged(int))); + connect(m_page->newWidthDouble, SIGNAL(valueChangedPt(double)), this, SLOT(slotWidthChanged(double))); + connect(m_page->newHeightDouble, SIGNAL(valueChangedPt(double)), this, SLOT(slotHeightChanged(double))); + + connect(m_page->newWidthUnit, SIGNAL(currentIndexChanged(int)), _widthUnitManager, SLOT(selectApparentUnitFromIndex(int))); + connect(m_page->newHeightUnit, SIGNAL(currentIndexChanged(int)), _heightUnitManager, SLOT(selectApparentUnitFromIndex(int))); + connect(_widthUnitManager, SIGNAL(unitChanged(int)), m_page->newWidthUnit, SLOT(setCurrentIndex(int))); + connect(_heightUnitManager, SIGNAL(unitChanged(int)), m_page->newHeightUnit, SLOT(setCurrentIndex(int))); } DlgLayerSize::~DlgLayerSize() { delete m_page; } qint32 DlgLayerSize::width() { return (qint32)m_width; } qint32 DlgLayerSize::height() { return (qint32)m_height; } KisFilterStrategy *DlgLayerSize::filterType() { KoID filterID = m_page->filterCmb->currentItem(); KisFilterStrategy *filter = KisFilterStrategyRegistry::instance()->value(filterID.id()); return filter; } // SLOTS -void DlgLayerSize::slotWidthChanged(int w) +void DlgLayerSize::slotWidthChanged(double w) { - slotWidthChanged((double) w); -} -void DlgLayerSize::slotHeightChanged(int h) -{ - slotHeightChanged((double) h); -} + //this slot receiv values in pt from the unitspinbox, so just use the resolution. + const double resValue = w*_widthUnitManager->getConversionFactor(KisSpinBoxUnitManager::LENGTH, "px"); + m_width = qRound(resValue); -void DlgLayerSize::slotWidthChanged(double w) -{ - if (m_page->newWidthUnit->currentText() == percentStr) { - m_width = qRound((w * m_originalWidth) / 100.0); - } else { - const KoUnit selectedUnit = KoUnit::fromListForUi(m_page->newWidthUnit->currentIndex()); - const double resValue = (selectedUnit == KoUnit(KoUnit::Pixel)) ? w : (w * m_resolution); - m_width = qRound(selectedUnit.fromUserValue(resValue)); - } + if (m_keepAspect) { + m_height = qRound(m_width / m_aspectRatio); + m_page->newHeightDouble->blockSignals(true); + m_page->newHeightDouble->changeValue(w / m_aspectRatio); + m_page->newHeightDouble->blockSignals(false); + } - if (m_keepAspect) { - m_height = qRound(m_width / m_aspectRatio); - updateHeightUIValue(m_height); - } } void DlgLayerSize::slotHeightChanged(double h) { - if (m_page->newHeightUnit->currentText() == percentStr) { - m_height = qRound((h * m_originalHeight) / 100.0); - } else { - const KoUnit selectedUnit = KoUnit::fromListForUi(m_page->newHeightUnit->currentIndex()); - const double resValue = (selectedUnit == KoUnit(KoUnit::Pixel)) ? h : (h * m_resolution); - m_height = qRound(selectedUnit.fromUserValue(resValue)); - } - if (m_keepAspect) { - m_width = qRound(m_height * m_aspectRatio); - updateWidthUIValue(m_width); - } -} + //this slot receiv values in pt from the unitspinbox, so just use the resolution. + const double resValue = h*_widthUnitManager->getConversionFactor(KisSpinBoxUnitManager::LENGTH, "px"); + m_height = qRound(resValue); -void DlgLayerSize::slotWidthUnitChanged(int index) -{ - updateWidthUIValue(m_width); - - if (m_page->newWidthUnit->currentText() == percentStr) { - m_page->newWidth->setVisible(false); - m_page->newWidthDouble->setVisible(true); - } else { - const KoUnit selectedUnit = KoUnit::fromListForUi(index); - if (selectedUnit != KoUnit(KoUnit::Pixel)) { - m_page->newWidth->setVisible(false); - m_page->newWidthDouble->setVisible(true); - } else { - m_page->newWidth->setVisible(true); - m_page->newWidthDouble->setVisible(false); - } - } -} - -void DlgLayerSize::slotHeightUnitChanged(int index) -{ - updateHeightUIValue(m_height); - - if (m_page->newHeightUnit->currentText() == percentStr) { - m_page->newHeight->setVisible(false); - m_page->newHeightDouble->setVisible(true); - } else { - const KoUnit selectedUnit = KoUnit::fromListForUi(index); - if (selectedUnit != KoUnit(KoUnit::Pixel)) { - m_page->newHeight->setVisible(false); - m_page->newHeightDouble->setVisible(true); - } else { - m_page->newHeight->setVisible(true); - m_page->newHeightDouble->setVisible(false); - } - } + if (m_keepAspect) { + m_width = qRound(m_height / m_aspectRatio); + m_page->newWidthDouble->blockSignals(true); + m_page->newWidthDouble->changeValue(h / m_aspectRatio); + m_page->newWidthDouble->blockSignals(false); + } } void DlgLayerSize::slotAspectChanged(bool keep) { m_page->aspectRatioBtn->blockSignals(true); m_page->constrainProportionsCkb->blockSignals(true); m_page->aspectRatioBtn->setKeepAspectRatio(keep); m_page->constrainProportionsCkb->setChecked(keep); m_page->aspectRatioBtn->blockSignals(false); m_page->constrainProportionsCkb->blockSignals(false); m_keepAspect = keep; if (keep) { // values may be out of sync, so we need to reset it to defaults m_width = m_originalWidth; m_height = m_originalHeight; updateWidthUIValue(m_width); updateHeightUIValue(m_height); } } void DlgLayerSize::updateWidthUIValue(double value) { - if (m_page->newWidthUnit->currentText() == percentStr) { - m_page->newWidthDouble->blockSignals(true); - m_page->newWidthDouble->setValue((value * 100.0) / m_originalWidth); - m_page->newWidthDouble->blockSignals(false); - } else { - const KoUnit selectedUnit = KoUnit::fromListForUi(m_page->newWidthUnit->currentIndex()); - if (selectedUnit != KoUnit(KoUnit::Pixel)) { - m_page->newWidthDouble->blockSignals(true); - m_page->newWidthDouble->setValue(selectedUnit.toUserValue(value / m_resolution)); - m_page->newWidthDouble->blockSignals(false); - } else { - m_page->newWidth->blockSignals(true); - m_page->newWidth->setValue(selectedUnit.toUserValue(value)); - m_page->newWidth->blockSignals(false); - } - } + m_page->newWidthDouble->blockSignals(true); + const double resValue = value/_widthUnitManager->getConversionFactor(KisSpinBoxUnitManager::LENGTH, "px"); + m_page->newWidthDouble->changeValue(resValue); + m_page->newWidthDouble->blockSignals(false); } void DlgLayerSize::updateHeightUIValue(double value) { - if (m_page->newHeightUnit->currentText() == percentStr) { - m_page->newHeightDouble->blockSignals(true); - m_page->newHeightDouble->setValue((value * 100.0) / m_originalHeight); - m_page->newHeightDouble->blockSignals(false); - } else { - const KoUnit selectedUnit = KoUnit::fromListForUi(m_page->newHeightUnit->currentIndex()); - if (selectedUnit != KoUnit(KoUnit::Pixel)) { - m_page->newHeightDouble->blockSignals(true); - m_page->newHeightDouble->setValue(selectedUnit.toUserValue(value / m_resolution)); - m_page->newHeightDouble->blockSignals(false); - } else { - m_page->newHeight->blockSignals(true); - m_page->newHeight->setValue(selectedUnit.toUserValue(value)); - m_page->newHeight->blockSignals(false); - } - } + m_page->newHeightDouble->blockSignals(true); + const double resValue = value/_heightUnitManager->getConversionFactor(KisSpinBoxUnitManager::LENGTH, "px"); + m_page->newHeightDouble->changeValue(resValue); + m_page->newHeightDouble->blockSignals(false); } diff --git a/plugins/extensions/imagesize/dlg_layersize.h b/plugins/extensions/imagesize/dlg_layersize.h index 73c943b10a..e53521f1a5 100644 --- a/plugins/extensions/imagesize/dlg_layersize.h +++ b/plugins/extensions/imagesize/dlg_layersize.h @@ -1,78 +1,79 @@ /* * dlg_layersize.h -- part of Krita * * Copyright (c) 2004 Boudewijn Rempt * Copyright (c) 2005 Sven Langkamp * Copyright (c) 2013 Juan Palacios * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef DLG_LAYERSIZE #define DLG_LAYERSIZE #include #include "ui_wdg_layersize.h" +class KisDocumentAwareSpinBoxUnitManager; + class WdgLayerSize : public QWidget, public Ui::WdgLayerSize { Q_OBJECT public: WdgLayerSize(QWidget *parent) : QWidget(parent) { setupUi(this); } }; class KisFilterStrategy; class DlgLayerSize: public KoDialog { Q_OBJECT public: DlgLayerSize(QWidget * parent, const char* name, int width, int height, double resolution); ~DlgLayerSize(); qint32 width(); qint32 height(); KisFilterStrategy *filterType(); private Q_SLOTS: - void slotWidthChanged(int w); - void slotHeightChanged(int h); void slotWidthChanged(double w); - void slotHeightChanged(double h); - void slotWidthUnitChanged(int index); - void slotHeightUnitChanged(int index); + void slotHeightChanged(double h); void slotAspectChanged(bool keep); private: void updateWidthUIValue(double value); void updateHeightUIValue(double value); WdgLayerSize * m_page; const double m_aspectRatio; const int m_originalWidth, m_originalHeight; int m_width, m_height; const double m_resolution; bool m_keepAspect; + + KisDocumentAwareSpinBoxUnitManager* _widthUnitManager; + KisDocumentAwareSpinBoxUnitManager* _heightUnitManager; }; #endif // DLG_IMAGESIZE diff --git a/plugins/extensions/imagesize/wdg_layersize.ui b/plugins/extensions/imagesize/wdg_layersize.ui index 58170c58ad..cf4bdce75c 100644 --- a/plugins/extensions/imagesize/wdg_layersize.ui +++ b/plugins/extensions/imagesize/wdg_layersize.ui @@ -1,248 +1,209 @@ WdgLayerSize 0 0 366 201 Layer Size New Size true Qt::Horizontal QSizePolicy::MinimumExpanding 40 20 W&idth: Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - newWidth - - + 80 0 4 0.000100000000000 10000.000000000000000 0.100000000000000 - + 80 0 4 0.000100000000000 10000.000000000000000 0.100000000000000 - - - - - 80 - 0 - - - - 1 - - - 100000 - - - - - - - 1 - - - 100000 - - - Filter: Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter &Height: Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - newHeight - Constrain aspect ratio Constrain proportions true Qt::Horizontal QSizePolicy::Fixed 25 20 Qt::Vertical QSizePolicy::MinimumExpanding 20 30 KisCmbIDList
widgets/kis_cmb_idlist.h
KoAspectButton QWidget
KoAspectButton.h
1
- KisIntParseSpinBox - QSpinBox -
kis_int_parse_spin_box.h
-
- - KisDoubleParseSpinBox + KisDoubleParseUnitSpinBox QDoubleSpinBox -
kis_double_parse_spin_box.h
+
kis_double_parse_unit_spin_box.h
- newWidth newWidthDouble newWidthUnit - newHeight newHeightDouble newHeightUnit filterCmb