File indexing completed on 2024-05-12 04:21:25

0001 /*
0002    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0003    All rights reserved.
0004 
0005    Redistribution and use in source and binary forms, with or without
0006    modification, are permitted provided that the following conditions
0007    are met:
0008 
0009    1. Redistributions of source code must retain the above copyright
0010       notice, this list of conditions and the following disclaimer.
0011    2. Redistributions in binary form must reproduce the above copyright
0012       notice, this list of conditions and the following disclaimer in the
0013       documentation and/or other materials provided with the distribution.
0014 
0015    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0016    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0017    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0018    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0019    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0020    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0021    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0022    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0023    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0024    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025 */
0026 
0027 
0028 #define DEBUG_KP_TOOL_COLOR_ERASER 0
0029 
0030 #include "kpToolColorEraser.h"
0031 
0032 #include <QApplication>
0033 
0034 #include "kpLogCategories.h"
0035 #include <KLocalizedString>
0036 
0037 #include "imagelib/kpColor.h"
0038 #include "commands/kpCommandHistory.h"
0039 #include "document/kpDocument.h"
0040 #include "commands/kpMacroCommand.h"
0041 #include "imagelib/kpPainter.h"
0042 #include "pixmapfx/kpPixmapFX.h"
0043 #include "commands/tools/flow/kpToolFlowCommand.h"
0044 #include "environments/tools/kpToolEnvironment.h"
0045 
0046 //--------------------------------------------------------------------------------
0047 
0048 kpToolColorEraser::kpToolColorEraser (kpToolEnvironment *environ, QObject *parent)
0049     : kpToolFlowBase (i18n ("Color Eraser"),
0050         i18n ("Replaces pixels of the foreground color with the background color"),
0051         Qt::Key_O,
0052         environ, parent,
0053         QStringLiteral("tool_color_eraser"))
0054 {
0055 }
0056 
0057 //--------------------------------------------------------------------------------
0058 
0059 kpToolColorEraser::~kpToolColorEraser () = default;
0060 
0061 //--------------------------------------------------------------------------------
0062 // public virtual [base kpTool]
0063 
0064 void kpToolColorEraser::globalDraw ()
0065 {
0066 #if DEBUG_KP_TOOL_COLOR_ERASER
0067     qCDebug(kpLogTools) << "kpToolColorEraser::globalDraw()";
0068 #endif
0069     if (!drawShouldProceed (QPoint ()/*unused*/, QPoint ()/*unused*/, QRect ()/*unused*/)) {
0070         return;
0071     }
0072 
0073     QApplication::setOverrideCursor (Qt::WaitCursor);
0074 
0075     environ ()->flashColorSimilarityToolBarItem ();
0076 
0077     kpToolFlowCommand *cmd = new kpToolFlowCommand (
0078         i18n ("Color Eraser"), environ ()->commandEnvironment ());
0079 
0080     const QRect dirtyRect = kpPainter::washRect (document ()->imagePointer (),
0081         0, 0, document ()->width (), document ()->height (),
0082         backgroundColor ()/*color to draw in*/,
0083         foregroundColor ()/*color to replace*/,
0084         processedColorSimilarity ());
0085 
0086     if (!dirtyRect.isEmpty ())
0087     {
0088         document ()->slotContentsChanged (dirtyRect);
0089 
0090 
0091         cmd->updateBoundingRect (dirtyRect);
0092         cmd->finalize ();
0093 
0094         commandHistory ()->addCommand (cmd, false /* don't exec */);
0095 
0096         // don't delete - it's up to the commandHistory
0097         cmd = nullptr;
0098     }
0099     else
0100     {
0101     #if DEBUG_KP_TOOL_COLOR_ERASER
0102         qCDebug(kpLogTools) << "\tisNOP";
0103     #endif
0104         delete cmd;
0105         cmd = nullptr;
0106     }
0107 
0108     QApplication::restoreOverrideCursor ();
0109 }
0110 
0111 //--------------------------------------------------------------------------------
0112 
0113 QString kpToolColorEraser::haventBegunDrawUserMessage () const
0114 {
0115     return i18n ("Click or drag to erase pixels of the foreground color.");
0116 }
0117 
0118 //--------------------------------------------------------------------------------
0119 
0120 bool kpToolColorEraser::drawShouldProceed (const QPoint & /*thisPoint*/,
0121     const QPoint & /*lastPoint*/,
0122     const QRect & /*normalizedRect*/)
0123 {
0124     return !(foregroundColor () == backgroundColor () &&
0125         processedColorSimilarity () == 0);
0126 }
0127 
0128 //--------------------------------------------------------------------------------
0129 
0130 QRect kpToolColorEraser::drawLine (const QPoint &thisPoint, const QPoint &lastPoint)
0131 {
0132 #if DEBUG_KP_TOOL_COLOR_ERASER
0133     qCDebug(kpLogTools) << "kpToolColorEraser::drawLine(thisPoint=" << thisPoint
0134         << ",lastPoint=" << lastPoint << ")";
0135 #endif
0136 
0137     environ ()->flashColorSimilarityToolBarItem ();
0138 
0139     const QRect dirtyRect = kpPainter::washLine (document ()->imagePointer (),
0140         lastPoint.x (), lastPoint.y (),
0141         thisPoint.x (), thisPoint.y (),
0142         color (mouseButton ())/*color to draw in*/,
0143         brushWidth (), brushHeight (),
0144         color (1 - mouseButton ())/*color to replace*/,
0145         processedColorSimilarity ());
0146 
0147 #if DEBUG_KP_TOOL_COLOR_ERASER
0148     qCDebug(kpLogTools) << "\tdirtyRect=" << dirtyRect;
0149 #endif
0150 
0151     if (!dirtyRect.isEmpty ())
0152     {
0153         document ()->slotContentsChanged (dirtyRect);
0154         return dirtyRect;
0155     }
0156 
0157     return  {};
0158 }
0159 
0160 //--------------------------------------------------------------------------------
0161 
0162 #include "moc_kpToolColorEraser.cpp"