File indexing completed on 2024-06-02 04:23:48
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_TOOL_SELECTION 0 0030 0031 0032 #include "kpToolSelectionResizeScaleCommand.h" 0033 0034 #include "layers/selections/kpAbstractSelection.h" 0035 #include "layers/selections/image/kpAbstractImageSelection.h" 0036 #include "environments/commands/kpCommandEnvironment.h" 0037 #include "kpDefs.h" 0038 #include "document/kpDocument.h" 0039 #include "layers/selections/image/kpRectangularImageSelection.h" 0040 #include "layers/selections/text/kpTextSelection.h" 0041 #include "kpLogCategories.h" 0042 0043 #include <QApplication> 0044 #include <QTimer> 0045 0046 #include <KLocalizedString> 0047 0048 //-------------------------------------------------------------------------------- 0049 0050 kpToolSelectionResizeScaleCommand::kpToolSelectionResizeScaleCommand ( 0051 kpCommandEnvironment *environ) 0052 : kpNamedCommand (environ->textSelection () ? 0053 i18n ("Text: Resize Box") : 0054 i18n ("Selection: Smooth Scale"), 0055 environ), 0056 m_smoothScaleTimer (new QTimer (this)) 0057 { 0058 m_originalSelectionPtr = selection ()->clone (); 0059 0060 m_newTopLeft = selection ()->topLeft (); 0061 m_newWidth = selection ()->width (); 0062 m_newHeight = selection ()->height (); 0063 0064 m_smoothScaleTimer->setSingleShot (true); 0065 connect (m_smoothScaleTimer, &QTimer::timeout, this, [this]{resizeScaleAndMove(false);}); 0066 } 0067 0068 kpToolSelectionResizeScaleCommand::~kpToolSelectionResizeScaleCommand () 0069 { 0070 delete m_originalSelectionPtr; 0071 } 0072 0073 0074 // public virtual 0075 kpCommandSize::SizeType kpToolSelectionResizeScaleCommand::size () const 0076 { 0077 return SelectionSize (m_originalSelectionPtr); 0078 } 0079 0080 0081 // public 0082 const kpAbstractSelection *kpToolSelectionResizeScaleCommand::originalSelection () const 0083 { 0084 return m_originalSelectionPtr; 0085 } 0086 0087 0088 // public 0089 QPoint kpToolSelectionResizeScaleCommand::topLeft () const 0090 { 0091 return m_newTopLeft; 0092 } 0093 0094 // public 0095 void kpToolSelectionResizeScaleCommand::moveTo (const QPoint &point) 0096 { 0097 if (point == m_newTopLeft) { 0098 return; 0099 } 0100 0101 m_newTopLeft = point; 0102 selection ()->moveTo (m_newTopLeft); 0103 } 0104 0105 0106 // public 0107 int kpToolSelectionResizeScaleCommand::width () const 0108 { 0109 return m_newWidth; 0110 } 0111 0112 // public 0113 int kpToolSelectionResizeScaleCommand::height () const 0114 { 0115 return m_newHeight; 0116 } 0117 0118 // public 0119 void kpToolSelectionResizeScaleCommand::resize (int width, int height, 0120 bool delayed) 0121 { 0122 if (width == m_newWidth && height == m_newHeight) { 0123 return; 0124 } 0125 0126 m_newWidth = width; 0127 m_newHeight = height; 0128 0129 resizeScaleAndMove (delayed); 0130 } 0131 0132 0133 // public 0134 void kpToolSelectionResizeScaleCommand::resizeAndMoveTo (int width, int height, 0135 const QPoint &point, 0136 bool delayed) 0137 { 0138 if (width == m_newWidth && height == m_newHeight && 0139 point == m_newTopLeft) 0140 { 0141 return; 0142 } 0143 0144 m_newWidth = width; 0145 m_newHeight = height; 0146 m_newTopLeft = point; 0147 0148 resizeScaleAndMove (delayed); 0149 } 0150 0151 0152 // protected 0153 void kpToolSelectionResizeScaleCommand::killSmoothScaleTimer () 0154 { 0155 m_smoothScaleTimer->stop (); 0156 } 0157 0158 0159 // protected 0160 void kpToolSelectionResizeScaleCommand::resizeScaleAndMove (bool delayed) 0161 { 0162 #if DEBUG_KP_TOOL_SELECTION 0163 qCDebug(kpLogCommands) << "kpToolSelectionResizeScaleCommand::resizeScaleAndMove(delayed=" 0164 << delayed << ")"; 0165 #endif 0166 0167 killSmoothScaleTimer (); 0168 0169 kpAbstractSelection *newSelPtr = nullptr; 0170 0171 if (textSelection ()) 0172 { 0173 Q_ASSERT (dynamic_cast <kpTextSelection *> (m_originalSelectionPtr)); 0174 auto *orgTextSel = dynamic_cast <kpTextSelection *> (m_originalSelectionPtr); 0175 0176 newSelPtr = orgTextSel->resized (m_newWidth, m_newHeight); 0177 } 0178 else 0179 { 0180 Q_ASSERT (dynamic_cast <kpAbstractImageSelection *> (m_originalSelectionPtr)); 0181 auto *imageSel = dynamic_cast <kpAbstractImageSelection *> (m_originalSelectionPtr); 0182 0183 newSelPtr = new kpRectangularImageSelection ( 0184 QRect (imageSel->x (), 0185 imageSel->y (), 0186 m_newWidth, 0187 m_newHeight), 0188 kpPixmapFX::scale (imageSel->baseImage (), 0189 m_newWidth, m_newHeight, 0190 !delayed/*if not delayed, smooth*/), 0191 imageSel->transparency ()); 0192 0193 if (delayed) 0194 { 0195 // Call self (once) with delayed==false in 200ms 0196 m_smoothScaleTimer->start (200/*ms*/); 0197 } 0198 } 0199 0200 Q_ASSERT (newSelPtr); 0201 newSelPtr->moveTo (m_newTopLeft); 0202 0203 document ()->setSelection (*newSelPtr); 0204 0205 delete newSelPtr; 0206 } 0207 0208 0209 // public 0210 void kpToolSelectionResizeScaleCommand::finalize () 0211 { 0212 #if DEBUG_KP_TOOL_SELECTION 0213 qCDebug(kpLogCommands) << "kpToolSelectionResizeScaleCommand::finalize()" 0214 << " smoothScaleTimer->isActive=" 0215 << m_smoothScaleTimer->isActive (); 0216 #endif 0217 0218 // Make sure the selection contains the final image and the timer won't 0219 // fire afterwards. 0220 if (m_smoothScaleTimer->isActive ()) 0221 { 0222 resizeScaleAndMove (); 0223 Q_ASSERT (!m_smoothScaleTimer->isActive ()); 0224 } 0225 } 0226 0227 0228 // public virtual [base kpToolResizeScaleCommand] 0229 void kpToolSelectionResizeScaleCommand::execute () 0230 { 0231 QApplication::setOverrideCursor (Qt::WaitCursor); 0232 0233 killSmoothScaleTimer (); 0234 0235 resizeScaleAndMove (); 0236 0237 environ ()->somethingBelowTheCursorChanged (); 0238 0239 QApplication::restoreOverrideCursor (); 0240 } 0241 0242 // public virtual [base kpToolResizeScaleCommand] 0243 void kpToolSelectionResizeScaleCommand::unexecute () 0244 { 0245 QApplication::setOverrideCursor (Qt::WaitCursor); 0246 0247 killSmoothScaleTimer (); 0248 0249 document ()->setSelection (*m_originalSelectionPtr); 0250 0251 environ ()->somethingBelowTheCursorChanged (); 0252 0253 QApplication::restoreOverrideCursor (); 0254 } 0255 0256 #include "moc_kpToolSelectionResizeScaleCommand.cpp"