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.

kpview.h 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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_VIEW_H
  24. #define KP_VIEW_H
  25. #include <tqwidget.h>
  26. #include <kpdefs.h>
  27. class kpDocument;
  28. class kpSelection;
  29. class kpTool;
  30. class kpToolToolBar;
  31. class kpViewManager;
  32. class kpViewScrollableContainer;
  33. /**
  34. * @short Abstract base class for all views.
  35. *
  36. * This is the abstract base class for all views. A view is a widget that
  37. * renders a possibly zoomed onscreen representation of a document.
  38. *
  39. * 1 view corresponds to 1 document.
  40. * 1 document corresponds to 0 or more views.
  41. *
  42. * @see kpViewManager
  43. *
  44. * @author Clarence Dang <dang@kde.org>
  45. */
  46. class kpView : public TQWidget
  47. {
  48. Q_OBJECT
  49. public:
  50. /**
  51. * Constructs a view.
  52. *
  53. * @param document The document this view is representing.
  54. * @param toolToolBar The tool tool bar.
  55. * @param viewManager The view manager.
  56. * @param buddyView The view this view watches over (e.g. a thumbnail
  57. * view would watch over the main view). May be 0.
  58. * See for example, highlightBuddyViewRectangle().
  59. * @param scrollableContainer This view's scrollable container.
  60. * May be 0.
  61. *
  62. * You must call adjustEnvironment() at the end of your constructor.
  63. */
  64. kpView (kpDocument *document,
  65. kpToolToolBar *toolToolBar,
  66. kpViewManager *viewManager,
  67. kpView *buddyView,
  68. kpViewScrollableContainer *scrollableContainer,
  69. TQWidget *parent, const char *name);
  70. /**
  71. * Destructs this view. Informs the viewManager() that the mouse
  72. * cursor is no longer above this view.
  73. */
  74. virtual ~kpView ();
  75. /**
  76. * @returns the document.
  77. */
  78. kpDocument *document () const;
  79. protected:
  80. /**
  81. * @returns the document's selection.
  82. */
  83. kpSelection *selection () const;
  84. public:
  85. /**
  86. * @returns the tool tool bar.
  87. */
  88. kpToolToolBar *toolToolBar () const;
  89. protected:
  90. /**
  91. * @returns the currently selected tool.
  92. */
  93. kpTool *tool () const;
  94. public:
  95. /**
  96. * @returns the view manager.
  97. */
  98. kpViewManager *viewManager () const;
  99. /**
  100. * @returns the buddy view.
  101. */
  102. kpView *buddyView () const;
  103. /**
  104. * @returns the buddyView()'s scrollable container.
  105. */
  106. kpViewScrollableContainer *buddyViewScrollableContainer () const;
  107. /**
  108. * @returns this view's scrollable container.
  109. */
  110. kpViewScrollableContainer *scrollableContainer () const;
  111. /**
  112. * @returns the horizontal zoom level (100 is unzoomed).
  113. */
  114. int zoomLevelX (void) const;
  115. /**
  116. * @returns the vertical zoom level (100 is unzoomed).
  117. */
  118. int zoomLevelY (void) const;
  119. /**
  120. * Sets the horizontal and vertical zoom levels.
  121. *
  122. * @param hzoom Horizontal zoom level.
  123. * @param vzoom Vertical zoom level.
  124. *
  125. * If reimplementing, you must call this base implementation.
  126. */
  127. virtual void setZoomLevel (int hzoom, int vzoom);
  128. /**
  129. * @returns in views coordinates, where the top-left document() pixel
  130. * will be rendered (default: (0,0)).
  131. */
  132. TQPoint origin () const;
  133. /**
  134. * Sets the origin.
  135. *
  136. * @param origin New origin.
  137. *
  138. * If reimplementing, you must call this base implementation.
  139. */
  140. virtual void setOrigin (const TQPoint &origin);
  141. /**
  142. * @returns whether at this zoom level, the grid can be enabled.
  143. * This is based on whether the grid can be sensibly rendered.
  144. */
  145. bool canShowGrid () const;
  146. /**
  147. * @returns whether the grid is currently shown.
  148. */
  149. bool isGridShown () const;
  150. /**
  151. * Turns on/off the grid.
  152. *
  153. * @param yes Whether to enable the grid.
  154. */
  155. void showGrid (bool yes = true);
  156. /**
  157. * @returns whether to draw a rectangle highlighting the area of
  158. * buddyView() visible through buddyViewScrollableContainer().
  159. */
  160. bool isBuddyViewScrollableContainerRectangleShown () const;
  161. /**
  162. * Turns on/off the rectangle highlighting the area of buddyView()
  163. * visible through buddyViewScrollableContainer() and redraws.
  164. *
  165. * @param yes Whether to turn on the rectangle.
  166. */
  167. void showBuddyViewScrollableContainerRectangle (bool yes = true);
  168. protected:
  169. /**
  170. * @returns the current rectangle highlighting the area of buddyView()
  171. * visible through buddyViewScrollableContainer(), that is being
  172. * rendered by this view.
  173. */
  174. TQRect buddyViewScrollableContainerRectangle () const;
  175. protected slots:
  176. /**
  177. * Updates the buddyViewScrollableContainerRectangle() and redraws
  178. * appropriately.
  179. *
  180. * This is already connected to zoomLevelChanged() and originChanged();
  181. * buddyView() and buddyViewScrollableContainer() signals. There is probably no
  182. * need to call this function directly.
  183. */
  184. void updateBuddyViewScrollableContainerRectangle ();
  185. public:
  186. /**
  187. * @param viewX Horizontal position in view coordinates.
  188. *
  189. * @returns viewX transformed to document coordinates, based on the
  190. * origin() and zoomLevelX().
  191. */
  192. double transformViewToDocX (double viewX) const;
  193. /**
  194. * @param viewY Vertical position in view coordinates.
  195. *
  196. * @returns viewY transformed to document coordinates, based on the
  197. * origin() and zoomLevelY().
  198. */
  199. double transformViewToDocY (double viewY) const;
  200. /**
  201. * @param viewPoint Position in view coordinates.
  202. *
  203. * @returns viewPoint transformed to document coordinates, based on the
  204. * origin(), zoomLevelX() and zoomLevelY().
  205. */
  206. TQPoint transformViewToDoc (const TQPoint &viewPoint) const;
  207. /**
  208. * @param viewRect Rectangle in view coordinates.
  209. *
  210. * @returns viewRect transformed to document coordinates based on the
  211. * origin(), zoomLevelX() and zoomLevelY().
  212. *
  213. * For bounding rectangles, you should use this function instead of
  214. * transformViewToDocX(), transformViewToDocY() or
  215. * transformViewToDoc(const TQPoint &) which act on coordinates only.
  216. */
  217. TQRect transformViewToDoc (const TQRect &viewRect) const;
  218. /**
  219. * @param docX Horizontal position in document coordinates.
  220. *
  221. * @returns docX transformed to view coordinates, based on the origin()
  222. * and zoomLevelX().
  223. */
  224. double transformDocToViewX (double docX) const;
  225. /**
  226. * @param docY Vertical position in document coordinates.
  227. *
  228. * @returns docY transformed to view coordinates, based on the origin()
  229. * and zoomLevelY().
  230. */
  231. double transformDocToViewY (double docY) const;
  232. /**
  233. * @param docPoint Position in document coordinates.
  234. *
  235. * @returns docPoint transformed to view coordinates, based on the
  236. * origin(), zoomLevelX(), zoomLevelY().
  237. */
  238. TQPoint transformDocToView (const TQPoint &docPoint) const;
  239. /**
  240. * @param docRect Rectangle in document coordinates.
  241. *
  242. * @return docRect transformed to view coordinates, based on the
  243. * origin(), zoomLevelX() and zoomLevelY().
  244. *
  245. * For bounding rectangles, you should use this function instead of
  246. * transformDocToViewX(), transformDocToViewY() or
  247. * transformDocToView(const TQPoint &) which act on coordinates only.
  248. */
  249. TQRect transformDocToView (const TQRect &docRect) const;
  250. /**
  251. * @param viewPoint Position in view coordinates.
  252. * @param otherView View whose coordinate system the return value will
  253. * be in.
  254. *
  255. * @returns viewPoint transformed to the coordinate system of
  256. * @param otherView based on this and otherView's origin(),
  257. * zoomLevelX() and zoomLevelY(). This has less rounding
  258. * error than otherView->transformDocToView (transformViewToDoc (viewPoint)).
  259. */
  260. TQPoint transformViewToOtherView (const TQPoint &viewPoint,
  261. const kpView *otherView);
  262. /**
  263. * @returns the approximate view width required to display the entire
  264. * document(), based on the zoom level only.
  265. */
  266. int zoomedDocWidth () const;
  267. /**
  268. * @returns the approximate view height required to display the entire
  269. * document(), based on the zoom level only.
  270. */
  271. int zoomedDocHeight () const;
  272. protected:
  273. /**
  274. * Updates the viewManager() on whether or not the mouse is directly
  275. * above this view. Among other things, this ensures the brush cursor
  276. * is updated.
  277. *
  278. * This should be called in event handlers.
  279. *
  280. * @param yes Whether the mouse is directly above this view.
  281. */
  282. void setHasMouse (bool yes = true);
  283. public:
  284. /**
  285. * Adds a region (in view coordinates) to the dirty area that is
  286. * repainted when the parent @ref kpViewManager is set not to queue
  287. * updates.
  288. *
  289. * @param region Region (in view coordinates) that needs repainting.
  290. */
  291. void addToQueuedArea (const TQRegion &region);
  292. /**
  293. * Convenience function. Same as above.
  294. *
  295. * Adds a rectangle (in view coordinates) to the dirty area that is
  296. * repainted when the parent @ref kpViewManager is set not to queue
  297. * updates.
  298. *
  299. * @param rect Rectangle (in view coordinates) that needs repainting.
  300. */
  301. void addToQueuedArea (const TQRect &rect);
  302. /**
  303. * Removes the dirty region that has been queued for updating.
  304. * Does not update the view.
  305. */
  306. void invalidateQueuedArea ();
  307. /**
  308. * Updates the part of the view described by dirty region and then
  309. * calls invalidateQueuedArea(). Does nothing if @ref kpViewManager
  310. * is set to queue updates.
  311. */
  312. void updateQueuedArea ();
  313. void updateMicroFocusHint (const TQRect &microFocusHint);
  314. public slots:
  315. /**
  316. * Call this when the "environment" (e.g. document size) changes. The
  317. * environment is defined by the caller and should be based on the type
  318. * of view. For instance, an unzoomed thumbnail view would also
  319. * include in its environment the contents position of an associated
  320. * scrollable container.
  321. *
  322. * This is never called by the kpView base class.
  323. *
  324. * Implementors should change whatever state is neccessary for their
  325. * type of view. For instance, an unzoomed view would resize itself;
  326. * a zoomed thumbnail would change the zoom level.
  327. */
  328. virtual void adjustToEnvironment () = 0;
  329. public:
  330. TQRect selectionViewRect () const;
  331. // (if <viewPoint> is KP_INVALID_POINT, it uses TQCursor::pos())
  332. TQPoint mouseViewPoint (const TQPoint &returnViewPoint = KP_INVALID_POINT) const;
  333. TQPoint mouseViewPointRelativeToSelection (const TQPoint &viewPoint = KP_INVALID_POINT) const;
  334. bool mouseOnSelection (const TQPoint &viewPoint = KP_INVALID_POINT) const;
  335. int textSelectionMoveBorderAtomicSize () const;
  336. bool mouseOnSelectionToMove (const TQPoint &viewPoint = KP_INVALID_POINT) const;
  337. protected:
  338. bool selectionLargeEnoughToHaveResizeHandlesIfAtomicSize (int atomicSize) const;
  339. public:
  340. int selectionResizeHandleAtomicSize () const;
  341. bool selectionLargeEnoughToHaveResizeHandles () const;
  342. TQRegion selectionResizeHandlesViewRegion (bool forRenderer = false) const;
  343. enum SelectionResizeType
  344. {
  345. None = 0,
  346. Left = 1,
  347. Right = 2,
  348. Top = 4,
  349. Bottom = 8
  350. };
  351. // Returns a bitwise OR of the SelectionResizeType's
  352. int mouseOnSelectionResizeHandle (const TQPoint &viewPoint = KP_INVALID_POINT) const;
  353. bool mouseOnSelectionToSelectText (const TQPoint &viewPoint = KP_INVALID_POINT) const;
  354. signals:
  355. /**
  356. * Emitted after all zooming code has been executed.
  357. *
  358. * @param zoomLevelX New zoomLevelX()
  359. * @param zoomLevelY New zoomLevelY()
  360. */
  361. void zoomLevelChanged (int zoomLevelX, int zoomLevelY);
  362. /**
  363. * Emitted after all resizing code has been executed.
  364. *
  365. * @param size New view size.
  366. */
  367. void sizeChanged (const TQSize &size);
  368. /**
  369. * Convenience signal - same as above.
  370. *
  371. * Emitted after all resizing code has been executed.
  372. *
  373. * @param width New view width.
  374. * @param height New view height.
  375. */
  376. void sizeChanged (int width, int height);
  377. /**
  378. * Emitted after all origin changing code has been executed.
  379. *
  380. * @param origin The new origin.
  381. */
  382. void originChanged (const TQPoint &origin);
  383. protected:
  384. virtual void mouseMoveEvent (TQMouseEvent *e);
  385. virtual void mousePressEvent (TQMouseEvent *e);
  386. virtual void mouseReleaseEvent (TQMouseEvent *e);
  387. public:
  388. // (needs to be public as it may also get event from
  389. // TQScrollView::contentsWheelEvent())
  390. virtual void wheelEvent (TQWheelEvent *e);
  391. protected:
  392. virtual void keyPressEvent (TQKeyEvent *e);
  393. virtual void keyReleaseEvent (TQKeyEvent *e);
  394. virtual void focusInEvent (TQFocusEvent *e);
  395. virtual void focusOutEvent (TQFocusEvent *e);
  396. virtual void enterEvent (TQEvent *e);
  397. virtual void leaveEvent (TQEvent *e);
  398. virtual void dragEnterEvent (TQDragEnterEvent *);
  399. virtual void dragLeaveEvent (TQDragLeaveEvent *);
  400. virtual void imStartEvent (TQIMEvent *e);
  401. virtual void imComposeEvent (TQIMEvent *e);
  402. virtual void imEndEvent (TQIMEvent *e);
  403. public:
  404. virtual void resize (int w, int h);
  405. protected:
  406. virtual void resizeEvent (TQResizeEvent *e);
  407. protected:
  408. TQRect paintEventGetDocRect (const TQRect &viewRect) const;
  409. public:
  410. /**
  411. * Draws an opaque background representing transparency. Currently,
  412. * it draws a checkerboard.
  413. *
  414. * @param painter Painter.
  415. * @param viewWidth Total width of the view visible to the user.
  416. * @param viewHeight Total height of the view visible to the user.
  417. * @param rect Rectangle to paint in relative to the painter. Note
  418. * that this function does not clip and may draw slightly
  419. * more than the requested rectangle. TODO: why not?
  420. * @param isPreview Whether the view is for a preview as opposed to
  421. * e.g. editing. If set, this function may render
  422. * slightly differently.
  423. */
  424. static void drawTransparentBackground (TQPainter *painter,
  425. int viewWidth, int viewHeight,
  426. const TQRect &rect,
  427. bool isPreview = false);
  428. protected:
  429. void paintEventDrawCheckerBoard (TQPainter *painter, const TQRect &viewRect);
  430. void paintEventDrawSelection (TQPixmap *destPixmap, const TQRect &docRect);
  431. bool selectionResizeHandleAtomicSizeCloseToZoomLevel () const;
  432. void paintEventDrawSelectionResizeHandles (TQPainter *painter, const TQRect &viewRect);
  433. void paintEventDrawTempPixmap (TQPixmap *destPixmap, const TQRect &docRect);
  434. void paintEventDrawGridLines (TQPainter *painter, const TQRect &viewRect);
  435. void paintEventDrawRect (const TQRect &viewRect);
  436. virtual void paintEvent (TQPaintEvent *e);
  437. private:
  438. struct kpViewPrivate *d;
  439. };
  440. #endif // KP_VIEW_H