TDE graphics utilities
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kptool.h 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. Copyright (c) 2003,2004,2005 Clarence Dang <dang@kde.org>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  13. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  14. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  16. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  17. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  18. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  19. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  21. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #ifndef __kp_tool_h__
  24. #define __kp_tool_h__
  25. #include <tqobject.h>
  26. #include <tqpoint.h>
  27. #include <tqrect.h>
  28. #include <tqsize.h>
  29. #include <tqstring.h>
  30. #include <kpdefs.h>
  31. class TQIconSet;
  32. class TQPixmap;
  33. class KKeySequence;
  34. class TDEShortcut;
  35. class kpColor;
  36. class kpColorToolBar;
  37. class kpCommandHistory;
  38. class kpDocument;
  39. class kpView;
  40. class kpViewManager;
  41. class kpMainWindow;
  42. class kpToolAction;
  43. class kpToolToolBar;
  44. // Base class for all tools
  45. class kpTool : public TQObject
  46. {
  47. Q_OBJECT
  48. public:
  49. kpTool (const TQString &text, const TQString &description,
  50. int key,
  51. kpMainWindow *mainWindow, const char *name);
  52. virtual ~kpTool ();
  53. private:
  54. void init (const TQString &text, const TQString &description,
  55. int key,
  56. kpMainWindow *mainWindow, const char *name);
  57. protected:
  58. void createAction ();
  59. int m_key;
  60. kpToolAction *m_action;
  61. signals:
  62. void actionToolTipChanged (const TQString &string);
  63. protected slots:
  64. void slotActionToolTipChanged (const TQString &string);
  65. public:
  66. TQString text () const;
  67. void setText (const TQString &text);
  68. static TQString toolTipForTextAndShortcut (const TQString &text,
  69. const TDEShortcut &shortcut);
  70. TQString toolTip () const;
  71. TQString description () const;
  72. void setDescription (const TQString &description);
  73. int key () const;
  74. void setKey (int key);
  75. // Given a single <key>, returns a shortcut with <key>
  76. // (disabled when the user is editing text) and as an alternate,
  77. // <some modifiers>+<key>.
  78. static TDEShortcut shortcutForKey (int key);
  79. TDEShortcut shortcut () const;
  80. static bool keyIsText (int key);
  81. static bool containsSingleKeyTrigger (const KKeySequence &seq);
  82. static bool containsSingleKeyTrigger (const TDEShortcut &shortcut,
  83. TDEShortcut *shortcutWithoutSingleKeyTriggers);
  84. bool singleKeyTriggersEnabled () const;
  85. void enableSingleKeyTriggers (bool enable = true);
  86. const char *name () const;
  87. static TQRect neededRect (const TQRect &rect, int lineWidth);
  88. static TQPixmap neededPixmap (const TQPixmap &pixmap, const TQRect &boundingRect);
  89. bool hasCurrentPoint () const;
  90. // Returns the position of the cursor relative to the topleft point of
  91. // the current view (viewUnderStartPoint() or viewUnderCursor() otherwise).
  92. //
  93. // If neither viewUnderStartPoint() nor viewUnderCursor()
  94. // (i.e. !hasCurrentPoint()), then it returns KP_INVALID_POINT.
  95. //
  96. // If <zoomToDoc> is set (default), then it returns the position in the
  97. // document. This theoretically == m_currentPoint (when m_currentPoint
  98. // is defined) but I wouldn't bet on it. This function is useful when
  99. // m_currentPoint isn't necessarily defined (outside of beginDraw(),draw()
  100. // and hover()).
  101. //
  102. // If <zoomToDoc> is not set, then it returns an unzoomed view coordinate.
  103. //
  104. // Keep in mind that if viewUnderStartPoint(), this can return coordinates
  105. // outside of the document/view.
  106. TQPoint currentPoint (bool zoomToDoc = true) const;
  107. public slots:
  108. // Call this when something below the mouse cursor may have changed
  109. // and/or if the view has moved relative to the cursor (as opposed to
  110. // the cursor moving relative to the view, which would trigger a
  111. // mouseMoveEvent and all would be well without such hacks)
  112. // e.g. when zooming or scrolling the view or when deleting a selection.
  113. //
  114. // This calls hover() or draw() to let the tool know. The Brush Tool
  115. // can then update the position of the Brush Cursor. The Selection
  116. // Tool can update the real cursor. The Line Tool can update the current
  117. // line. The statubar gets correct coordinates. etc. etc.
  118. void somethingBelowTheCursorChanged ();
  119. private:
  120. // Same as above except that you claim you know better than currentPoint()
  121. void somethingBelowTheCursorChanged (const TQPoint &currentPoint_,
  122. const TQPoint &currentViewPoint_);
  123. public:
  124. // called when the tool is selected
  125. virtual void begin ();
  126. // called when the tool is deselected
  127. virtual void end ();
  128. // set after begin() has been called, unset after end() has been called
  129. bool hasBegun () const { return m_began; }
  130. bool hasBegunDraw () const { return m_beganDraw; }
  131. virtual bool hasBegunShape () const { return hasBegunDraw (); }
  132. // called when user double-left-clicks on Tool Icon (not view)
  133. virtual void globalDraw ();
  134. // called when the user clicks on the Tool Icon even though it's already
  135. // the current tool (used by the selection tools to deselect)
  136. virtual void reselect ();
  137. signals:
  138. // emitted after beginDraw() has been called
  139. void beganDraw (const TQPoint &point);
  140. // Emitted just before draw() is called in mouseMoveEvent(). Slots
  141. // connected to this signal should return in <scrolled> whether the
  142. // mouse pos may have changed. Used by drag scrolling.
  143. void movedAndAboutToDraw (const TQPoint &currentPoint, const TQPoint &lastPoint,
  144. int zoomLevel,
  145. bool *scrolled);
  146. // emitted after endDraw() has been called
  147. void endedDraw (const TQPoint &point);
  148. // emitted after cancelShape() has been called
  149. void cancelledShape (const TQPoint &point);
  150. public:
  151. TQIconSet iconSet (int forceSize = 0) const;
  152. TQString iconName () const;
  153. kpToolAction *action ();
  154. signals:
  155. // User clicked on the tool's action - i.e. select this tool
  156. void actionActivated ();
  157. protected slots:
  158. void slotActionActivated ();
  159. protected:
  160. virtual bool returnToPreviousToolAfterEndDraw () const { return false; }
  161. virtual bool careAboutModifierState () const { return false; }
  162. virtual bool careAboutColorsSwapped () const { return false; }
  163. virtual void beginDraw ();
  164. // mouse move without button pressed
  165. // (only m_currentPoint & m_currentViewPoint is defined)
  166. virtual void hover (const TQPoint &point);
  167. // this is useful for "instant" tools like the Pen & Eraser
  168. virtual void draw (const TQPoint &thisPoint, const TQPoint &lastPoint,
  169. const TQRect &normalizedRect);
  170. // (m_mouseButton will not change from beginDraw())
  171. virtual void cancelShape ();
  172. virtual void releasedAllButtons ();
  173. virtual void endDraw (const TQPoint &thisPoint, const TQRect &normalizedRect);
  174. virtual void endShape (const TQPoint &thisPoint = TQPoint (),
  175. const TQRect &normalizedRect = TQRect ())
  176. {
  177. endDraw (thisPoint, normalizedRect);
  178. }
  179. kpMainWindow *mainWindow () const;
  180. kpDocument *document () const;
  181. kpViewManager *viewManager () const;
  182. kpToolToolBar *toolToolBar () const;
  183. kpView *viewUnderStartPoint () const { return m_viewUnderStartPoint; }
  184. kpView *viewUnderCursor () const;
  185. kpCommandHistory *commandHistory () const;
  186. kpColor color (int which) const;
  187. kpColor foregroundColor () const;
  188. kpColor backgroundColor () const;
  189. double colorSimilarity () const;
  190. int processedColorSimilarity () const;
  191. protected:
  192. int m_ignoreColorSignals;
  193. protected slots:
  194. void slotColorsSwappedInternal (const kpColor &newForegroundColor,
  195. const kpColor &newBackgroundColor);
  196. void slotForegroundColorChangedInternal (const kpColor &color);
  197. void slotBackgroundColorChangedInternal (const kpColor &color);
  198. void slotColorSimilarityChangedInternal (double similarity, int processedSimilarity);
  199. protected slots: // TODO: there is no reason why these should be slots
  200. virtual void slotColorsSwapped (const kpColor & /*newForegroundColor*/, const kpColor & /*newBackgroundColor*/) {}
  201. virtual void slotForegroundColorChanged (const kpColor & /*color*/) {}
  202. virtual void slotBackgroundColorChanged (const kpColor & /*color*/) {}
  203. virtual void slotColorSimilarityChanged (double /*similarity*/, int /*processedSimilarity*/) {};
  204. protected:
  205. // (only valid in slots connected to the respective signals above)
  206. kpColor oldForegroundColor () const;
  207. kpColor oldBackgroundColor () const;
  208. double oldColorSimilarity () const;
  209. protected:
  210. // returns true if m_currentPoint <= 1 pixel away from m_lastPoint
  211. // or if there was no lastPoint
  212. bool currentPointNextToLast () const; // (includes diagonal adjacency)
  213. bool currentPointCardinallyNextToLast () const; // (only cardinally adjacent i.e. horiz & vert; no diag)
  214. int m_mouseButton; /* 0 = left, 1 = right */
  215. bool m_shiftPressed, m_controlPressed, m_altPressed; // m_altPressed is unreliable
  216. bool m_beganDraw; // set after beginDraw() is called, unset before endDraw() is called
  217. TQPoint m_startPoint,
  218. m_currentPoint, m_currentViewPoint,
  219. m_lastPoint;
  220. protected:
  221. friend class kpCommandHistory;
  222. friend class kpMainWindow;
  223. friend class kpToolToolBar;
  224. void beginInternal ();
  225. void endInternal ();
  226. void beginDrawInternal ();
  227. void endDrawInternal (const TQPoint &thisPoint, const TQRect &normalizedRect,
  228. bool wantEndShape = false);
  229. void cancelShapeInternal ();
  230. void endShapeInternal (const TQPoint &thisPoint = TQPoint (),
  231. const TQRect &normalizedRect = TQRect ());
  232. friend class kpView;
  233. // If you're reimplementing any of these, you probably don't know what
  234. // you're doing - reimplement begin(),beginDraw(),draw(),cancelShape(),
  235. // endDraw() etc. instead.
  236. virtual void mousePressEvent (TQMouseEvent *e);
  237. virtual void mouseMoveEvent (TQMouseEvent *e);
  238. virtual void mouseReleaseEvent (TQMouseEvent *e);
  239. virtual void wheelEvent (TQWheelEvent *e);
  240. virtual void keyPressEvent (TQKeyEvent *e);
  241. virtual void keyReleaseEvent (TQKeyEvent *e);
  242. virtual void imStartEvent(TQIMEvent *){}
  243. virtual void imComposeEvent(TQIMEvent *){}
  244. virtual void imEndEvent(TQIMEvent *){}
  245. private:
  246. void keyUpdateModifierState (TQKeyEvent *e);
  247. void notifyModifierStateChanged ();
  248. protected:
  249. virtual void setShiftPressed (bool pressed);
  250. virtual void setControlPressed (bool pressed);
  251. virtual void setAltPressed (bool pressed);
  252. virtual void focusInEvent (TQFocusEvent *e);
  253. virtual void focusOutEvent (TQFocusEvent *e);
  254. virtual void enterEvent (TQEvent *e);
  255. virtual void leaveEvent (TQEvent *e);
  256. // 0 = left, 1 = right, -1 = other (none, left+right, mid)
  257. static int mouseButton (const TQt::ButtonState &buttonState);
  258. TQString m_text, m_description;
  259. const char *m_name;
  260. kpMainWindow *m_mainWindow;
  261. bool m_began;
  262. kpView *m_viewUnderStartPoint;
  263. /*
  264. * User Notifications (Status Bar)
  265. */
  266. public:
  267. // Returns "(Left|Right) click to cancel." where Left or Right is chosen
  268. // depending on which one is the _opposite_ of <mouseButton>
  269. static TQString cancelUserMessage (int mouseButton);
  270. TQString cancelUserMessage () const;
  271. TQString userMessage () const;
  272. void setUserMessage (const TQString &userMessage = TQString());
  273. TQPoint userShapeStartPoint () const;
  274. TQPoint userShapeEndPoint () const;
  275. static int calculateLength (int start, int end);
  276. void setUserShapePoints (const TQPoint &startPoint = KP_INVALID_POINT,
  277. const TQPoint &endPoint = KP_INVALID_POINT,
  278. bool setSize = true);
  279. TQSize userShapeSize () const;
  280. int userShapeWidth () const;
  281. int userShapeHeight () const;
  282. void setUserShapeSize (const TQSize &size = KP_INVALID_SIZE);
  283. void setUserShapeSize (int width, int height);
  284. signals:
  285. void userMessageChanged (const TQString &userMessage);
  286. void userShapePointsChanged (const TQPoint &startPoint = KP_INVALID_POINT,
  287. const TQPoint &endPoint = KP_INVALID_POINT);
  288. void userShapeSizeChanged (const TQSize &size);
  289. void userShapeSizeChanged (int width, int height);
  290. protected:
  291. TQString m_userMessage;
  292. TQPoint m_userShapeStartPoint, m_userShapeEndPoint;
  293. TQSize m_userShapeSize;
  294. public:
  295. // Call this before the user tries to cause the document or selection
  296. // to resize from <oldWidth>x<oldHeight> to <newWidth>x<newHeight>.
  297. // If at least one dimension increases, the new dimensions will take a
  298. // large amount of memory (which causes thrashing, instability) and
  299. // the old dimensions did not take a large amount of memory, ask the
  300. // user if s/he really wants to perform the operation.
  301. //
  302. // Returns true if the operation should proceed, false otherwise.
  303. //
  304. // In order to make the translators' lives possible, this function cannot
  305. // generate the <text>,<caption> nor <continueButtonText> (without
  306. // concantenating sentences and words with tense). However, it is
  307. // recommended that you give them the following values:
  308. //
  309. // e.g.:
  310. // text = i18n ("<qt><p>(Rotating|Skewing) the (image|selection) to"
  311. // " %1x%2 may take a substantial amount of memory."
  312. // " This can reduce system"
  313. // " responsiveness and cause other application resource"
  314. // " problems.</p>").arg (newWidth, newHeight)
  315. //
  316. // "<p>Are you sure want to (rotate|skew) the"
  317. // " (image|selection)?</p></qt>");
  318. // caption = i18n ("Rotate (Image|Selection)?");
  319. // continueButtonText = i18n ("Rotat&e (Image|Selection)");
  320. static bool warnIfBigImageSize (int oldWidth, int oldHeight,
  321. int newWidth, int newHeight,
  322. const TQString &text,
  323. const TQString &caption,
  324. const TQString &continueButtonText,
  325. TQWidget *parent);
  326. protected:
  327. // There is no need to maintain binary compatibility at this stage.
  328. // The d-pointer is just so that you can experiment without recompiling
  329. // the kitchen sink.
  330. class kpToolPrivate *d;
  331. };
  332. #endif // __kp_tool_h__