File indexing completed on 2024-12-22 04:07:22
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 #include "kpEffectCommandBase.h" 0030 0031 #include "kpDefs.h" 0032 #include "document/kpDocument.h" 0033 #include "generic/kpSetOverrideCursorSaver.h" 0034 0035 #include <KLocalizedString> 0036 0037 #include <QCursor> 0038 0039 //-------------------------------------------------------------------------------- 0040 0041 struct kpEffectCommandBasePrivate 0042 { 0043 QString name; 0044 bool actOnSelection{false}; 0045 0046 kpImage oldImage; 0047 }; 0048 0049 kpEffectCommandBase::kpEffectCommandBase (const QString &name, 0050 bool actOnSelection, 0051 kpCommandEnvironment *environ) 0052 : kpCommand (environ), 0053 d (new kpEffectCommandBasePrivate ()) 0054 { 0055 d->name = name; 0056 d->actOnSelection = actOnSelection; 0057 } 0058 0059 kpEffectCommandBase::~kpEffectCommandBase () 0060 { 0061 delete d; 0062 } 0063 0064 0065 // public virtual [base kpCommand] 0066 QString kpEffectCommandBase::name () const 0067 { 0068 return (d->actOnSelection) ? i18n ("Selection: %1", d->name) : d->name; 0069 } 0070 0071 0072 // public virtual [base kpCommand] 0073 kpCommandSize::SizeType kpEffectCommandBase::size () const 0074 { 0075 return ImageSize (d->oldImage); 0076 } 0077 0078 0079 // public virtual [base kpCommand] 0080 void kpEffectCommandBase::execute () 0081 { 0082 kpSetOverrideCursorSaver cursorSaver (Qt::WaitCursor); 0083 0084 kpDocument *doc = document (); 0085 Q_ASSERT (doc); 0086 0087 0088 const kpImage oldImage = doc->image (d->actOnSelection); 0089 0090 if (!isInvertible ()) 0091 { 0092 d->oldImage = oldImage; 0093 } 0094 0095 0096 kpImage newImage = /*pure virtual*/applyEffect (oldImage); 0097 0098 doc->setImage (d->actOnSelection, newImage); 0099 } 0100 0101 // public virtual [base kpCommand] 0102 void kpEffectCommandBase::unexecute () 0103 { 0104 kpSetOverrideCursorSaver cursorSaver (Qt::WaitCursor); 0105 0106 kpDocument *doc = document (); 0107 Q_ASSERT (doc); 0108 0109 0110 kpImage newImage; 0111 0112 if (!isInvertible ()) 0113 { 0114 newImage = d->oldImage; 0115 } 0116 else 0117 { 0118 newImage = /*pure virtual*/applyEffect (doc->image (d->actOnSelection)); 0119 } 0120 0121 doc->setImage (d->actOnSelection, newImage); 0122 0123 0124 d->oldImage = kpImage (); 0125 } 0126