use kDebug
[kdegraphics.git] / kolourpaint / tools / kpTool.cpp
blobc71f730a048e8d90ed883fe78dbc2a1e272e41fa
2 /*
3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // Tool initialisation and basic accessors.
33 #define DEBUG_KP_TOOL 0
36 // TODO: reduce number of includes
37 #include <kpTool.h>
38 #include <kpToolPrivate.h>
40 #include <limits.h>
42 #include <qapplication.h>
43 #include <qclipboard.h>
44 #include <qcursor.h>
45 #include <qevent.h>
46 #include <qlayout.h>
47 #include <qpainter.h>
48 #include <qpixmap.h>
50 #include <kactioncollection.h>
51 #include <kapplication.h>
52 #include <kdebug.h>
53 #include <kiconloader.h>
54 #include <klocale.h>
55 #include <kmessagebox.h>
57 #include <kpBug.h>
58 #include <kpColor.h>
59 #include <kpColorToolBar.h>
60 #include <kpDefs.h>
61 #include <kpPixmapFX.h>
62 #include <kpToolAction.h>
63 #include <kpToolEnvironment.h>
64 #include <kpToolToolBar.h>
65 #include <kpView.h>
66 #include <kpViewManager.h>
67 #undef environ // macro on win32
70 kpTool::kpTool (const QString &text, const QString &description,
71 int key,
72 kpToolEnvironment *environ,
73 QObject *parent, const QString &name)
74 : QObject (parent),
75 d (new kpToolPrivate ())
77 d->key = key;
78 d->action = 0;
79 d->ignoreColorSignals = 0;
80 d->shiftPressed = false, d->controlPressed = false, d->altPressed = false; // set in beginInternal()
81 d->beganDraw = false;
82 d->text = text, d->description = description; setObjectName (name);
83 d->began = false;
84 d->viewUnderStartPoint = 0;
85 d->userShapeStartPoint = KP_INVALID_POINT;
86 d->userShapeEndPoint = KP_INVALID_POINT;
87 d->userShapeSize = KP_INVALID_SIZE;
89 d->environ = environ;
91 initAction ();
94 kpTool::~kpTool ()
96 // before destructing, stop using the tool
97 if (d->began)
98 endInternal ();
100 delete d->action;
102 delete d;
106 // private
107 void kpTool::initAction ()
109 #if DEBUG_KP_TOOL && 0
110 kDebug () << "kpTool(" << objectName () << "::initAction()";
111 #endif
113 KActionCollection *ac = d->environ->actionCollection ();
114 Q_ASSERT (ac);
117 d->action = new kpToolAction (text (), iconName (), shortcutForKey (d->key),
118 this, SLOT (slotActionActivated ()),
119 ac, objectName ());
121 // Make tools mutually exclusive by placing them in the same group.
122 d->action->setActionGroup (d->environ->toolsActionGroup ());
124 d->action->setWhatsThis (description ());
126 connect (d->action, SIGNAL (toolTipChanged (const QString &)),
127 this, SLOT (slotActionToolTipChanged (const QString &)));
131 // public
132 QString kpTool::text () const
134 return d->text;
137 // public
138 void kpTool::setText (const QString &text)
140 d->text = text;
142 d->action->setText (d->text);
146 static bool KeyIsText (int key)
148 // TODO: should work like !QKeyEvent::text().isEmpty()
149 return !(key & (Qt::KeyboardModifierMask ^ Qt::ShiftModifier));
152 // public static
153 QString kpTool::toolTipForTextAndShortcut (const QString &text,
154 const KShortcut &shortcut)
156 foreach(const QKeySequence &seq, shortcut.toList())
158 if (seq.count () == 1 && ::KeyIsText (seq [0]))
159 return i18nc ("<Tool Name> (<Single Accel Key>)",
160 "%1 (%2)", text, seq.toString ().toUpper ());
163 return text;
166 // public static
167 QString kpTool::toolTip () const
169 return toolTipForTextAndShortcut (text (), shortcut ());
173 // public
174 int kpTool::key () const
176 return d->key;
179 // public
180 void kpTool::setKey (int key)
182 d->key = key;
184 // TODO: this probably not wise since it nukes the user's settings
185 d->action->setShortcut (shortcutForKey (d->key));
188 // public static
189 KShortcut kpTool::shortcutForKey (int key)
191 KShortcut shortcut;
193 if (key)
195 shortcut.setPrimary (key);
196 // (CTRL+<key>, ALT+<key>, CTRL+ALT+<key>, CTRL+SHIFT+<key>
197 // all clash with global KDE shortcuts)
198 shortcut.setAlternate (Qt::ALT + Qt::SHIFT + key);
201 return shortcut;
204 // public
205 KShortcut kpTool::shortcut () const
207 return d->action->shortcut ();
211 // public
212 QString kpTool::description () const
214 return d->description;
217 // public
218 void kpTool::setDescription (const QString &description)
220 d->description = description;
222 d->action->setWhatsThis (d->description);
226 // public
227 QIcon kpTool::iconSet (int forceSize) const
229 #if DEBUG_KP_TOOL && 0
230 kDebug () << "kpTool(" << objectName () << ")::iconSet(forceSize=" << forceSize << ") iconName=" << iconName ();
231 #endif
232 // (robust in case BarIcon() default arg changes)
233 if (forceSize > 0)
234 return BarIconSet (iconName (), forceSize);
235 else
236 return KIcon (iconName ());
239 // public virtual
240 QString kpTool::iconName () const
242 return objectName ();
245 // public
246 kpToolAction *kpTool::action () const
248 return d->action;
253 // REFACTOR: need to add access specifier comments (like "public virtual [base AmOverridingThisClass'Method]") not just in kpTool but all over KolourPaint source.
255 kpDocument *kpTool::document () const
257 return d->environ->document ();
260 kpViewManager *kpTool::viewManager () const
262 return d->environ->viewManager ();
265 kpToolToolBar *kpTool::toolToolBar () const
267 return d->environ->toolToolBar ();
270 kpColor kpTool::color (int which) const
272 return d->environ->color (which);
275 kpColor kpTool::foregroundColor () const
277 return color (0);
280 kpColor kpTool::backgroundColor () const
282 return color (1);
286 // TODO: Some of these might not be common enough.
287 // Just put in kpToolEnvironment?
289 double kpTool::colorSimilarity () const
291 return d->environ->colorSimilarity ();
294 int kpTool::processedColorSimilarity () const
296 return d->environ->processedColorSimilarity ();
300 kpColor kpTool::oldForegroundColor () const
302 return d->environ->oldForegroundColor ();
305 kpColor kpTool::oldBackgroundColor () const
307 return d->environ->oldBackgroundColor ();
310 double kpTool::oldColorSimilarity () const
312 return d->environ->oldColorSimilarity ();
315 kpCommandHistory *kpTool::commandHistory () const
317 return d->environ->commandHistory ();
321 kpToolEnvironment *kpTool::environ () const
323 return d->environ;
327 #include <kpTool.moc>