File indexing completed on 2024-04-28 04:20:27

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_FLOOD_FILL 0
0030 
0031 
0032 #include "kpToolFloodFill.h"
0033 
0034 #include "commands/kpCommandHistory.h"
0035 #include "kpDefs.h"
0036 #include "document/kpDocument.h"
0037 #include "environments/tools/kpToolEnvironment.h"
0038 #include "commands/tools/kpToolFloodFillCommand.h"
0039 
0040 #include "kpLogCategories.h"
0041 #include <KLocalizedString>
0042 
0043 #include <QApplication>
0044 
0045 //---------------------------------------------------------------------
0046 
0047 struct kpToolFloodFillPrivate
0048 {
0049     kpToolFloodFillCommand *currentCommand;
0050 };
0051 
0052 //---------------------------------------------------------------------
0053 
0054 kpToolFloodFill::kpToolFloodFill (kpToolEnvironment *environ, QObject *parent)
0055     : kpTool (i18n ("Flood Fill"), i18n ("Fills regions in the image"),
0056               Qt::Key_F,
0057               environ, parent, QStringLiteral("tool_flood_fill")),
0058       d (new kpToolFloodFillPrivate ())
0059 {
0060     d->currentCommand = nullptr;
0061 }
0062 
0063 //---------------------------------------------------------------------
0064 
0065 kpToolFloodFill::~kpToolFloodFill ()
0066 {
0067     delete d;
0068 }
0069 
0070 //---------------------------------------------------------------------
0071 
0072 // private
0073 QString kpToolFloodFill::haventBegunDrawUserMessage () const
0074 {
0075     return i18n ("Click to fill a region.");
0076 }
0077 
0078 //---------------------------------------------------------------------
0079 
0080 // public virtual [base kpTool]
0081 void kpToolFloodFill::begin ()
0082 {
0083     setUserMessage (haventBegunDrawUserMessage ());
0084 }
0085 
0086 //---------------------------------------------------------------------
0087 
0088 // public virtual [base kpTool]
0089 void kpToolFloodFill::beginDraw ()
0090 {
0091 #if DEBUG_KP_TOOL_FLOOD_FILL && 1
0092     qCDebug(kpLogTools) << "kpToolFloodFill::beginDraw()";
0093 #endif
0094 
0095     QApplication::setOverrideCursor (Qt::WaitCursor);
0096     {
0097         environ ()->flashColorSimilarityToolBarItem ();
0098 
0099         // Flood Fill is an expensive CPU operation so we only fill at a
0100         // mouse click (beginDraw ()), not on mouse move (virtually draw())
0101         d->currentCommand = new kpToolFloodFillCommand (
0102             currentPoint ().x (), currentPoint ().y (),
0103             color (mouseButton ()), processedColorSimilarity (),
0104             environ ()->commandEnvironment ());
0105 
0106     #if DEBUG_KP_TOOL_FLOOD_FILL && 1
0107         qCDebug(kpLogTools) << "\tperforming new-doc-corner-case check";
0108     #endif
0109 
0110         if (document ()->url ().isEmpty () && !document ()->isModified ())
0111         {
0112             // Collect the colour that gets changed before we change the pixels
0113             // (execute() below).  Needed in unexecute().
0114             d->currentCommand->prepareColorToChange ();
0115 
0116             d->currentCommand->setFillEntireImage ();
0117         }
0118 
0119         d->currentCommand->execute ();
0120     }
0121     QApplication::restoreOverrideCursor ();
0122 
0123     setUserMessage (cancelUserMessage ());
0124 }
0125 
0126 //---------------------------------------------------------------------
0127 
0128 // public virtual [base kpTool]
0129 void kpToolFloodFill::draw (const QPoint &thisPoint, const QPoint &, const QRect &)
0130 {
0131     setUserShapePoints (thisPoint);
0132 }
0133 
0134 //---------------------------------------------------------------------
0135 
0136 // public virtual [base kpTool]
0137 void kpToolFloodFill::cancelShape ()
0138 {
0139     d->currentCommand->unexecute ();
0140 
0141     delete d->currentCommand;
0142     d->currentCommand = nullptr;
0143 
0144     setUserMessage (i18n ("Let go of all the mouse buttons."));
0145 }
0146 
0147 //---------------------------------------------------------------------
0148 
0149 // public virtual [base kpTool]
0150 void kpToolFloodFill::releasedAllButtons ()
0151 {
0152     setUserMessage (haventBegunDrawUserMessage ());
0153 }
0154 
0155 //---------------------------------------------------------------------
0156 
0157 // public virtual [base kpTool]
0158 void kpToolFloodFill::endDraw (const QPoint &, const QRect &)
0159 {
0160     environ ()->commandHistory ()->addCommand (d->currentCommand,
0161         false/*no exec - we already did it up there*/);
0162 
0163     // Don't delete - it just got added to the history.
0164     d->currentCommand = nullptr;
0165     setUserMessage (haventBegunDrawUserMessage ());
0166 }
0167 
0168 //---------------------------------------------------------------------
0169 
0170 #include "moc_kpToolFloodFill.cpp"