use kDebug
[kdegraphics.git] / kolourpaint / tools / kpTool.h
blob2cfc7888a156ff792ccb7823b4f90a4ec16d5b79
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 #ifndef KP_TOOL_H
30 #define KP_TOOL_H
33 #include <qobject.h>
34 #include <qpoint.h>
35 #include <qrect.h>
36 #include <qsize.h>
37 #include <qstring.h>
39 #include <kpDefs.h>
40 #ifdef Q_OS_WIN
41 #include <stdlib.h>
42 #undef environ // macro on win32
43 #endif
46 class QFocusEvent;
47 class QIcon;
48 class QInputMethodEvent;
49 class QKeyEvent;
50 class QMouseEvent;
51 class QPixmap;
52 class QWheelEvent;
54 class KShortcut;
56 class kpColor;
57 class kpCommandHistory;
58 class kpDocument;
59 class kpView;
60 class kpViewManager;
61 class kpToolAction;
62 class kpToolEnvironment;
63 class kpToolToolBar;
66 struct kpToolPrivate;
68 // Base class for all tools.
69 // REFACTOR: rearrange method order to make sense and reflect kpTool_*.cpp split.
70 class kpTool : public QObject
72 Q_OBJECT
74 public:
75 // <text> = user-visible name of the tool e.g. "Color Picker"
76 // <description> = user-visible description used for tooltips
77 // e.g. "Lets you select a color from the image"
78 // <key> = optional shortcut key for switching to the tool, or 0 otherwise
79 // e.g. Qt::Key_C
80 // <name> = internal QObject name (not user-visible) e.g. "tool_color_picker"
81 // used for fetching the icon(), the name of the action() and
82 // debug printing.
83 kpTool (const QString &text, const QString &description,
84 int key,
85 kpToolEnvironment *environ,
86 QObject *parent, const QString &name);
87 virtual ~kpTool ();
89 private:
90 // Only called by ctor to create action().
91 void initAction ();
93 signals:
94 void actionToolTipChanged (const QString &string);
96 protected slots:
97 void slotActionToolTipChanged (const QString &string);
99 public:
100 QString text () const;
101 void setText (const QString &text);
103 static QString toolTipForTextAndShortcut (const QString &text,
104 const KShortcut &shortcut);
105 QString toolTip () const;
107 QString description () const;
108 void setDescription (const QString &description);
110 int key () const;
111 void setKey (int key);
113 // Given a single <key>, returns a shortcut with <key>
114 // (disabled when the user is editing text) and as an alternate,
115 // <some modifiers>+<key>.
116 static KShortcut shortcutForKey (int key);
117 KShortcut shortcut () const;
120 static QRect neededRect (const QRect &rect, int lineWidth);
121 static QPixmap neededPixmap (const QPixmap &pixmap, const QRect &boundingRect);
123 bool hasCurrentPoint () const;
124 // Returns the position of the cursor relative to the topleft point of
125 // the current view (viewUnderStartPoint() or viewUnderCursor() otherwise).
127 // If neither viewUnderStartPoint() nor viewUnderCursor()
128 // (i.e. !hasCurrentPoint()), then it returns KP_INVALID_POINT.
130 // If <zoomToDoc> is set (default), then it returns the position in the
131 // document. This theoretically == m_currentPoint (when m_currentPoint
132 // is defined) but I wouldn't bet on it. This function is useful when
133 // m_currentPoint isn't necessarily defined (outside of beginDraw(),draw()
134 // and hover()).
136 // If <zoomToDoc> is not set, then it returns an unzoomed view coordinate.
138 // Keep in mind that if viewUnderStartPoint(), this can return coordinates
139 // outside of the document/view.
140 QPoint calculateCurrentPoint (bool zoomToDoc = true) const;
142 public slots:
143 // Call this when something below the mouse cursor may have changed
144 // and/or if the view has moved relative to the cursor (as opposed to
145 // the cursor moving relative to the view, which would trigger a
146 // mouseMoveEvent and all would be well without such hacks)
147 // e.g. when zooming or scrolling the view or when deleting a selection.
149 // This calls hover() or draw() to let the tool know. The Brush Tool
150 // can then update the position of the Brush Cursor. The Selection
151 // Tool can update the real cursor. The Line Tool can update the current
152 // line. The statubar gets correct coordinates. etc. etc.
153 void somethingBelowTheCursorChanged ();
155 private:
156 // Same as above except that you claim you know better than currentPoint()
157 void somethingBelowTheCursorChanged (const QPoint &currentPoint_,
158 const QPoint &currentViewPoint_);
160 protected:
161 int mouseButton () const;
163 bool shiftPressed () const;
164 bool controlPressed () const;
165 bool altPressed () const;
167 QPoint startPoint () const;
169 QPoint currentPoint () const;
170 QPoint currentViewPoint () const;
172 QRect normalizedRect () const;
174 QPoint lastPoint () const;
176 public: // for kpMainWindow
177 kpView *viewUnderStartPoint () const;
178 protected:
179 kpView *viewUnderCursor () const;
182 public:
183 // Called when the tool is selected.
184 virtual void begin ();
186 // Called when the tool is deselected.
187 virtual void end ();
189 // Returns true after begin() has been called but returns false after end()
190 // after end() has been called.
191 bool hasBegun () const;
193 bool hasBegunDraw () const;
195 virtual bool hasBegunShape () const;
197 // Called when user double-left-clicks on a tool in the Tool Box.
198 virtual void globalDraw ();
200 // Called when the user clicks on a tool in the Tool Box even though it's
201 // already the current tool (used by the selection tools to deselect).
202 virtual void reselect ();
204 signals:
205 // emitted after beginDraw() has been called
206 void beganDraw (const QPoint &point);
208 // Emitted just before draw() is called in mouseMoveEvent(). Slots
209 // connected to this signal should return in <scrolled> whether the
210 // mouse pos may have changed. Used by drag scrolling.
211 void movedAndAboutToDraw (const QPoint &currentPoint, const QPoint &lastPoint,
212 int zoomLevel,
213 bool *scrolled);
215 // emitted after endDraw() has been called
216 void endedDraw (const QPoint &point);
218 // emitted after cancelShape() has been called
219 void cancelledShape (const QPoint &point);
222 public:
223 QIcon iconSet (int forceSize = 0) const;
225 // Override this to use an icon whose name is not the same as the tool's
226 // <name> as passed to the constructor.
227 virtual QString iconName () const;
229 kpToolAction *action () const;
231 signals:
232 // User clicked on the tool's action - i.e. select this tool
233 void actionActivated ();
235 protected slots:
236 void slotActionActivated ();
239 protected:
240 // (this method is called by kpTool just as it is needed - its value
241 // is not cached, so it is allowed to return different things at
242 // different times)
243 // REFACTOR: Misleadingly named as it's also called in cancelShapeInternal().
244 // And it seems to be called in endShapeInternal() as well?
245 virtual bool returnToPreviousToolAfterEndDraw () const { return false; }
247 virtual bool careAboutModifierState () const { return false; }
248 virtual bool careAboutColorsSwapped () const { return false; }
250 virtual void beginDraw ();
252 // mouse move without button pressed
253 // (only m_currentPoint & m_currentViewPoint is defined)
254 virtual void hover (const QPoint &point);
256 // this is useful for "instant" tools like the Pen & Eraser
257 virtual void draw (const QPoint &thisPoint, const QPoint &lastPoint,
258 const QRect &normalizedRect);
260 private:
261 void drawInternal ();
263 protected:
264 // (m_mouseButton will not change from beginDraw())
265 virtual void cancelShape ();
266 virtual void releasedAllButtons ();
268 virtual void endDraw (const QPoint &thisPoint, const QRect &normalizedRect);
270 // TODO: I think reimplementations of this should be calling this base
271 // implementation, so that endDraw() happens before the custom
272 // endShape() logic of the reimplementation.
273 virtual void endShape (const QPoint &thisPoint = QPoint (),
274 const QRect &normalizedRect = QRect ())
276 endDraw (thisPoint, normalizedRect);
279 kpDocument *document () const;
280 kpViewManager *viewManager () const;
281 kpToolToolBar *toolToolBar () const;
282 kpCommandHistory *commandHistory () const;
283 kpToolEnvironment *environ () const;
285 kpColor color (int which) const;
287 // TODO: does anyone actually use these?
288 kpColor foregroundColor () const;
289 kpColor backgroundColor () const;
291 double colorSimilarity () const;
292 int processedColorSimilarity () const;
294 protected slots:
295 void slotColorsSwappedInternal (const kpColor &newForegroundColor,
296 const kpColor &newBackgroundColor);
297 void slotForegroundColorChangedInternal (const kpColor &color);
298 void slotBackgroundColorChangedInternal (const kpColor &color);
299 void slotColorSimilarityChangedInternal (double similarity, int processedSimilarity);
301 protected slots: // TODO: there is no reason why these should be slots
302 virtual void slotColorsSwapped (const kpColor & /*newForegroundColor*/, const kpColor & /*newBackgroundColor*/) {}
303 virtual void slotForegroundColorChanged (const kpColor & /*color*/) {}
304 virtual void slotBackgroundColorChanged (const kpColor & /*color*/) {}
305 virtual void slotColorSimilarityChanged (double /*similarity*/, int /*processedSimilarity*/) {}
307 protected:
308 // (only valid in slots connected to the respective signals above)
309 kpColor oldForegroundColor () const;
310 kpColor oldBackgroundColor () const;
311 double oldColorSimilarity () const;
313 protected:
314 // returns true if m_currentPoint <= 1 pixel away from m_lastPoint
315 // or if there was no lastPoint
316 bool currentPointNextToLast () const; // (includes diagonal adjacency)
317 bool currentPointCardinallyNextToLast () const; // (only cardinally adjacent i.e. horiz & vert; no diag)
319 // TODO: We should rename these.
320 // These are accessed from kpTool logic and our friends, kpCommandHistory,
321 // kpMainWindow, kpToolToolBar and kpView.
322 public:
323 void beginInternal ();
324 void endInternal ();
326 void beginDrawInternal ();
327 void endDrawInternal (const QPoint &thisPoint, const QRect &normalizedRect,
328 bool wantEndShape = false);
329 void cancelShapeInternal ();
330 // TODO: Who is actually calling endShapeInternal()?
331 // Tools seem to call endShape() directly.
332 void endShapeInternal (const QPoint &thisPoint = QPoint (),
333 const QRect &normalizedRect = QRect ());
337 // Mouse Events
340 public:
341 // Note: _All_ events are forwarded from a kpView.
342 // The existence of a kpView implies the existence of a kpDocument.
344 // If you're reimplementing any of these, you probably don't know what
345 // you're doing - reimplement begin(),beginDraw(),draw(),cancelShape(),
346 // endDraw() etc. instead.
347 virtual void mousePressEvent (QMouseEvent *e);
348 virtual void mouseMoveEvent (QMouseEvent *e);
349 virtual void mouseReleaseEvent (QMouseEvent *e);
351 virtual void wheelEvent (QWheelEvent *e);
355 // Keyboard Events
358 // REFACTOR: Make these private?
359 protected:
360 void seeIfAndHandleModifierKey (QKeyEvent *e);
362 void arrowKeyPressDirection (const QKeyEvent *e, int *dx, int *dy);
363 void seeIfAndHandleArrowKeyPress (QKeyEvent *e);
365 bool isDrawKey (int key);
366 void seeIfAndHandleBeginDrawKeyPress (QKeyEvent *e);
367 void seeIfAndHandleEndDrawKeyPress (QKeyEvent *e);
369 public:
370 virtual void keyPressEvent (QKeyEvent *e);
371 virtual void keyReleaseEvent (QKeyEvent *e);
373 // COMPAT: Need to update InputMethod support.
374 // virtual void inputMethodEvent (QInputMethodEvent *) {}
376 private:
377 void keyUpdateModifierState (QKeyEvent *e);
378 void notifyModifierStateChanged ();
380 protected:
381 virtual void setShiftPressed (bool pressed);
382 virtual void setControlPressed (bool pressed);
384 virtual void setAltPressed (bool pressed);
388 // Other Events - 1. View Events
391 public:
392 // WARNING: Do not call this "event()" as our QObject parent has a
393 // virtual function called that, that will pass us
394 // QObject events. We only care about events forwarded by
395 // kpView.
396 // REFACTOR: rename mousePressEvent() -> viewMousePressEvent() etc.
397 // to remind us that events are coming from the view - the tool
398 // is not a visible object.
399 virtual bool viewEvent (QEvent *e);
401 public:
402 virtual void focusInEvent (QFocusEvent *e);
403 virtual void focusOutEvent (QFocusEvent *e);
405 public:
406 virtual void enterEvent (QEvent *e);
407 virtual void leaveEvent (QEvent *e);
411 // Other Events - 2. Non-view Events
412 // REFACTOR: Group methods under this.
415 protected:
416 // 0 = left, 1 = right, -1 = other (none, left+right, mid)
417 static int mouseButton (Qt::MouseButtons mouseButtons);
419 public:
420 static int calculateLength (int start, int end);
423 // User Notifications (Status Bar)
426 public:
427 // Returns "(Left|Right) click to cancel." where Left or Right is chosen
428 // depending on which one is the _opposite_ of <mouseButton>
429 static QString cancelUserMessage (int mouseButton);
430 QString cancelUserMessage () const;
432 QString userMessage () const;
433 // WARNING: setUserMessage() will store a message different to <userMessage>,
434 // in unspecified ways (e.g. the name of the tool, followed
435 // by a colon and a space, will be prepended). userMessage()
436 // will return this different string.
437 void setUserMessage (const QString &userMessage = QString ());
439 QPoint userShapeStartPoint () const;
440 QPoint userShapeEndPoint () const;
441 void setUserShapePoints (const QPoint &startPoint = KP_INVALID_POINT,
442 const QPoint &endPoint = KP_INVALID_POINT,
443 bool setSize = true);
445 QSize userShapeSize () const;
446 int userShapeWidth () const;
447 int userShapeHeight () const;
448 void setUserShapeSize (const QSize &size = KP_INVALID_SIZE);
449 void setUserShapeSize (int width, int height);
451 signals:
452 void userMessageChanged (const QString &userMessage);
453 void userShapePointsChanged (const QPoint &startPoint = KP_INVALID_POINT,
454 const QPoint &endPoint = KP_INVALID_POINT);
455 void userShapeSizeChanged (const QSize &size);
456 void userShapeSizeChanged (int width, int height);
459 public:
460 // Call this before the user tries to cause the document or selection
461 // to resize from <oldWidth>x<oldHeight> to <newWidth>x<newHeight>.
462 // If at least one dimension increases, the new dimensions will take a
463 // large amount of memory (which causes thrashing, instability) and
464 // the old dimensions did not take a large amount of memory, ask the
465 // user if s/he really wants to perform the operation.
467 // Returns true if the operation should proceed, false otherwise.
469 // In order to make the translators' lives possible, this function cannot
470 // generate the <text>,<caption> nor <continueButtonText> (without
471 // concantenating sentences and words with tense). However, it is
472 // recommended that you give them the following values:
474 // e.g.:
475 // text = i18n ("<qt><p>(Rotating|Skewing) the (image|selection) to"
476 // " %1x%2 may take a substantial amount of memory."
477 // " This can reduce system"
478 // " responsiveness and cause other application resource"
479 // " problems.</p>").arg (newWidth, newHeight)
481 // "<p>Are you sure want to (rotate|skew) the"
482 // " (image|selection)?</p></qt>");
483 // caption = i18n ("Rotate (Image|Selection)?");
484 // continueButtonText = i18n ("Rotat&e (Image|Selection)");
485 static bool warnIfBigImageSize (int oldWidth, int oldHeight,
486 int newWidth, int newHeight,
487 const QString &text,
488 const QString &caption,
489 const QString &continueButtonText,
490 QWidget *parent);
492 private:
493 kpToolPrivate *d;
497 #endif // KP_TOOL_H