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_COLOR_PICKER 0
0030 
0031 
0032 #include "kpToolColorPicker.h"
0033 #include "kpLogCategories.h"
0034 #include "widgets/toolbars/kpColorToolBar.h"
0035 #include "commands/kpCommandHistory.h"
0036 #include "kpDefs.h"
0037 #include "document/kpDocument.h"
0038 #include "pixmapfx/kpPixmapFX.h"
0039 #include "commands/tools/kpToolColorPickerCommand.h"
0040 #include "environments/tools/kpToolEnvironment.h"
0041 
0042 #include <KLocalizedString>
0043 
0044 kpToolColorPicker::kpToolColorPicker (kpToolEnvironment *environ, QObject *parent)
0045     : kpTool (i18n ("Color Picker"), i18n ("Lets you select a color from the image"),
0046               Qt::Key_C,
0047               environ, parent, QStringLiteral("tool_color_picker"))
0048 {
0049 }
0050 
0051 kpToolColorPicker::~kpToolColorPicker () = default;
0052 
0053 
0054 // private
0055 kpColor kpToolColorPicker::colorAtPixel (const QPoint &p)
0056 {
0057 #if DEBUG_KP_TOOL_COLOR_PICKER && 0
0058     qCDebug(kpLogTools) << "kpToolColorPicker::colorAtPixel" << p;
0059 #endif
0060 
0061     return kpPixmapFX::getColorAtPixel (document ()->image (), p);
0062 }
0063 
0064 
0065 // private
0066 QString kpToolColorPicker::haventBegunDrawUserMessage () const
0067 {
0068     return i18n ("Click to select a color.");
0069 }
0070 
0071 
0072 // public virtual [base kpTool]
0073 void kpToolColorPicker::begin ()
0074 {
0075     setUserMessage (haventBegunDrawUserMessage ());
0076 }
0077 
0078 // public virtual [base kpTool]
0079 void kpToolColorPicker::beginDraw ()
0080 {
0081     m_oldColor = color (mouseButton ());
0082 
0083     setUserMessage (cancelUserMessage ());
0084 }
0085 
0086 // public virtual [base kpTool]
0087 void kpToolColorPicker::draw (const QPoint &thisPoint, const QPoint &, const QRect &)
0088 {
0089     const kpColor color = colorAtPixel (thisPoint);
0090 
0091     if (color.isValid ())
0092     {
0093         environ ()->setColor (mouseButton (), color);
0094         setUserShapePoints (thisPoint);
0095     }
0096     else
0097     {
0098         environ ()->setColor (mouseButton (), m_oldColor);
0099         setUserShapePoints ();
0100     }
0101 }
0102 
0103 // public virtual [base kpTool]
0104 void kpToolColorPicker::cancelShape ()
0105 {
0106     environ ()->setColor (mouseButton (), m_oldColor);
0107 
0108     setUserMessage (i18n ("Let go of all the mouse buttons."));
0109 }
0110 
0111 // public virtual [base kpTool]
0112 void kpToolColorPicker::releasedAllButtons ()
0113 {
0114     setUserMessage (haventBegunDrawUserMessage ());
0115 
0116 }
0117 
0118 // public virtual [base kpTool]
0119 void kpToolColorPicker::endDraw (const QPoint &thisPoint, const QRect &)
0120 {
0121     const kpColor color = colorAtPixel (thisPoint);
0122 
0123     if (color.isValid ())
0124     {
0125         auto *cmd = new kpToolColorPickerCommand (  mouseButton (), color, m_oldColor,
0126                                                     environ ()->commandEnvironment ());
0127 
0128         environ ()->commandHistory ()->addCommand (cmd, false/*no exec*/);
0129         setUserMessage (haventBegunDrawUserMessage ());
0130     }
0131     else
0132     {
0133         cancelShape ();
0134     }
0135 }
0136 
0137 #include "moc_kpToolColorPicker.cpp"