File indexing completed on 2025-01-12 04:00:36
0001 0002 /* 0003 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org> 0004 All rights reserved. 0005 0006 Redistribution and use in source and binary forms, with or without 0007 modification, are permitted provided that the following conditions 0008 are met: 0009 0010 1. Redistributions of source code must retain the above copyright 0011 notice, this list of conditions and the following disclaimer. 0012 2. Redistributions in binary form must reproduce the above copyright 0013 notice, this list of conditions and the following disclaimer in the 0014 documentation and/or other materials provided with the distribution. 0015 0016 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 0017 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 0018 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 0019 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 0020 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 0021 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0022 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0023 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0024 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 0025 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0026 */ 0027 0028 0029 #define DEBUG_KP_EFFECT_BLUR_SHARPEN 0 0030 0031 0032 #include "kpEffectBlurSharpenWidget.h" 0033 0034 #include "commands/imagelib/effects/kpEffectBlurSharpenCommand.h" 0035 0036 #include "kpLogCategories.h" 0037 #include <KLocalizedString> 0038 #include "kpNumInput.h" 0039 0040 #include <QGridLayout> 0041 #include <QLabel> 0042 0043 0044 kpEffectBlurSharpenWidget::kpEffectBlurSharpenWidget (bool actOnSelection, 0045 QWidget *parent) 0046 : kpEffectWidgetBase (actOnSelection, parent) 0047 { 0048 auto *lay = new QGridLayout (this); 0049 lay->setContentsMargins(0, 0, 0, 0); 0050 0051 auto *amountLabel = new QLabel (i18n ("&Amount:"), this); 0052 m_amountInput = new kpIntNumInput (this); 0053 m_amountInput->setRange (-kpEffectBlurSharpen::MaxStrength/*- for blur*/, 0054 +kpEffectBlurSharpen::MaxStrength/*+ for sharpen*/); 0055 0056 m_typeLabel = new QLabel (this); 0057 0058 // Make sure <m_typeLabel> doesn't expand when the effect type changes, 0059 // as otherwise, that would cause the preview pixmap label in the 0060 // "More Effects" dialog (which our widget is inside) to contract, 0061 // which would look a bit weird. 0062 // 0063 // We do this by setting the label to every possible string it could 0064 // contain and fixing its height to the maximum seen size hint height. 0065 0066 auto h = m_typeLabel->sizeHint ().height (); 0067 #if DEBUG_KP_EFFECT_BLUR_SHARPEN 0068 qCDebug(kpLogWidgets) << "initial size hint height=" << h; 0069 #endif 0070 0071 m_typeLabel->setText ( 0072 kpEffectBlurSharpenCommand::nameForType (kpEffectBlurSharpen::Blur)); 0073 h = qMax (h, m_typeLabel->sizeHint ().height ()); 0074 0075 m_typeLabel->setText ( 0076 kpEffectBlurSharpenCommand::nameForType (kpEffectBlurSharpen::Sharpen)); 0077 h = qMax (h, m_typeLabel->sizeHint ().height ()); 0078 0079 // Set this text last as the label's text needs to reflect the default 0080 // effect of "None". 0081 m_typeLabel->setText ( 0082 kpEffectBlurSharpenCommand::nameForType (kpEffectBlurSharpen::None)); 0083 h = qMax (h, m_typeLabel->sizeHint ().height ()); 0084 0085 #if DEBUG_KP_EFFECT_BLUR_SHARPEN 0086 qCDebug(kpLogWidgets) << "maximum size hint height" << h; 0087 #endif 0088 m_typeLabel->setFixedHeight (h); 0089 m_typeLabel->setAlignment (Qt::AlignCenter); 0090 0091 0092 amountLabel->setBuddy (m_amountInput); 0093 0094 0095 lay->addWidget (amountLabel, 0, 0); 0096 lay->addWidget (m_amountInput, 0, 1); 0097 0098 lay->addWidget (m_typeLabel, 1, 0, 1, 2, Qt::AlignCenter); 0099 0100 lay->setColumnStretch (1, 1); 0101 0102 0103 connect (m_amountInput, &kpIntNumInput::valueChanged, 0104 this, &kpEffectBlurSharpenWidget::settingsChangedDelayed); 0105 0106 connect (m_amountInput, &kpIntNumInput::valueChanged, 0107 this, &kpEffectBlurSharpenWidget::slotUpdateTypeLabel); 0108 } 0109 0110 kpEffectBlurSharpenWidget::~kpEffectBlurSharpenWidget () = default; 0111 0112 0113 // public virtual [base kpEffectWidgetBase] 0114 QString kpEffectBlurSharpenWidget::caption () const 0115 { 0116 return {}; 0117 } 0118 0119 0120 // public virtual [base kpEffectWidgetBase] 0121 bool kpEffectBlurSharpenWidget::isNoOp () const 0122 { 0123 return (type () == kpEffectBlurSharpen::None); 0124 } 0125 0126 // public virtual [base kpEffectWidgetBase] 0127 kpImage kpEffectBlurSharpenWidget::applyEffect (const kpImage &image) 0128 { 0129 return kpEffectBlurSharpen::applyEffect (image, 0130 type (), strength ()); 0131 } 0132 0133 // public virtual [base kpEffectWidgetBase] 0134 kpEffectCommandBase *kpEffectBlurSharpenWidget::createCommand ( 0135 kpCommandEnvironment *cmdEnviron) const 0136 { 0137 return new kpEffectBlurSharpenCommand (type (), strength (), 0138 m_actOnSelection, 0139 cmdEnviron); 0140 } 0141 0142 0143 // protected slot 0144 void kpEffectBlurSharpenWidget::slotUpdateTypeLabel () 0145 { 0146 QString text = kpEffectBlurSharpenCommand::nameForType (type ()); 0147 0148 #if DEBUG_KP_EFFECT_BLUR_SHARPEN 0149 qCDebug(kpLogWidgets) << "kpEffectBlurSharpenWidget::slotUpdateTypeLabel() text=" 0150 << text; 0151 #endif 0152 const int h = m_typeLabel->height (); 0153 m_typeLabel->setText (text); 0154 if (m_typeLabel->height () != h) 0155 { 0156 qCCritical(kpLogWidgets) << "Label changed height despite the hack in ctor:" 0157 << "was=" << h 0158 << "now=" << m_typeLabel->height (); 0159 } 0160 } 0161 0162 0163 // protected 0164 kpEffectBlurSharpen::Type kpEffectBlurSharpenWidget::type () const 0165 { 0166 if (m_amountInput->value () == 0) { 0167 return kpEffectBlurSharpen::None; 0168 } 0169 0170 if (m_amountInput->value () < 0) { 0171 return kpEffectBlurSharpen::Blur; 0172 } 0173 0174 return kpEffectBlurSharpen::Sharpen; 0175 } 0176 0177 // protected 0178 int kpEffectBlurSharpenWidget::strength () const 0179 { 0180 return qAbs (m_amountInput->value ()); 0181 } 0182 0183 #include "moc_kpEffectBlurSharpenWidget.cpp"