3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
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
38 #include <kpToolPrivate.h>
42 #include <qapplication.h>
43 #include <qclipboard.h>
50 #include <kactioncollection.h>
51 #include <kapplication.h>
53 #include <kiconloader.h>
55 #include <kmessagebox.h>
59 #include <kpColorToolBar.h>
61 #include <kpPixmapFX.h>
62 #include <kpToolAction.h>
63 #include <kpToolEnvironment.h>
64 #include <kpToolToolBar.h>
66 #include <kpViewManager.h>
67 #undef environ // macro on win32
70 kpTool::kpTool (const QString
&text
, const QString
&description
,
72 kpToolEnvironment
*environ
,
73 QObject
*parent
, const QString
&name
)
75 d (new kpToolPrivate ())
79 d
->ignoreColorSignals
= 0;
80 d
->shiftPressed
= false, d
->controlPressed
= false, d
->altPressed
= false; // set in beginInternal()
82 d
->text
= text
, d
->description
= description
; setObjectName (name
);
84 d
->viewUnderStartPoint
= 0;
85 d
->userShapeStartPoint
= KP_INVALID_POINT
;
86 d
->userShapeEndPoint
= KP_INVALID_POINT
;
87 d
->userShapeSize
= KP_INVALID_SIZE
;
96 // before destructing, stop using the tool
107 void kpTool::initAction ()
109 #if DEBUG_KP_TOOL && 0
110 kDebug () << "kpTool(" << objectName () << "::initAction()";
113 KActionCollection
*ac
= d
->environ
->actionCollection ();
117 d
->action
= new kpToolAction (text (), iconName (), shortcutForKey (d
->key
),
118 this, SLOT (slotActionActivated ()),
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
&)));
132 QString
kpTool::text () const
138 void kpTool::setText (const QString
&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
));
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 ());
167 QString
kpTool::toolTip () const
169 return toolTipForTextAndShortcut (text (), shortcut ());
174 int kpTool::key () const
180 void kpTool::setKey (int key
)
184 // TODO: this probably not wise since it nukes the user's settings
185 d
->action
->setShortcut (shortcutForKey (d
->key
));
189 KShortcut
kpTool::shortcutForKey (int 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
);
205 KShortcut
kpTool::shortcut () const
207 return d
->action
->shortcut ();
212 QString
kpTool::description () const
214 return d
->description
;
218 void kpTool::setDescription (const QString
&description
)
220 d
->description
= description
;
222 d
->action
->setWhatsThis (d
->description
);
227 QIcon
kpTool::iconSet (int forceSize
) const
229 #if DEBUG_KP_TOOL && 0
230 kDebug () << "kpTool(" << objectName () << ")::iconSet(forceSize=" << forceSize
<< ") iconName=" << iconName ();
232 // (robust in case BarIcon() default arg changes)
234 return BarIconSet (iconName (), forceSize
);
236 return KIcon (iconName ());
240 QString
kpTool::iconName () const
242 return objectName ();
246 kpToolAction
*kpTool::action () const
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
280 kpColor
kpTool::backgroundColor () const
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
327 #include <kpTool.moc>