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.cpp 42KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  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. #define DEBUG_KP_TOOL 0
  24. #include <kptool.h>
  25. #include <limits.h>
  26. #include <tqapplication.h>
  27. #include <tqclipboard.h>
  28. #include <tqcursor.h>
  29. #include <tqevent.h>
  30. #include <tqlayout.h>
  31. #include <tqpainter.h>
  32. #include <tqpixmap.h>
  33. #include <tdeapplication.h>
  34. #include <kdebug.h>
  35. #include <kiconloader.h>
  36. #include <tdelocale.h>
  37. #include <tdemessagebox.h>
  38. #include <kpcolor.h>
  39. #include <kpcolortoolbar.h>
  40. #include <kpdefs.h>
  41. #include <kpmainwindow.h>
  42. #include <kppixmapfx.h>
  43. #include <kpsinglekeytriggersaction.h>
  44. #include <kptoolaction.h>
  45. #include <kptooltoolbar.h>
  46. #include <kpview.h>
  47. #include <kpviewmanager.h>
  48. //
  49. // kpTool
  50. //
  51. struct kpToolPrivate
  52. {
  53. };
  54. kpTool::kpTool (const TQString &text, const TQString &description,
  55. int key,
  56. kpMainWindow *mainWindow, const char *name)
  57. {
  58. init (text, description, key, mainWindow, name);
  59. }
  60. kpTool::~kpTool ()
  61. {
  62. // before destructing, stop using the tool
  63. if (m_began)
  64. endInternal ();
  65. if (m_action)
  66. {
  67. if (m_mainWindow && m_mainWindow->actionCollection ())
  68. m_mainWindow->actionCollection ()->remove (m_action);
  69. else
  70. delete m_action;
  71. }
  72. delete d;
  73. }
  74. // private
  75. void kpTool::init (const TQString &text, const TQString &description,
  76. int key,
  77. kpMainWindow *mainWindow, const char *name)
  78. {
  79. d = new kpToolPrivate ();
  80. m_key = key;
  81. m_action = 0;
  82. m_ignoreColorSignals = 0;
  83. m_shiftPressed = false, m_controlPressed = false, m_altPressed = false; // set in beginInternal()
  84. m_beganDraw = false;
  85. m_text = text, m_description = description, m_name = name;
  86. m_mainWindow = mainWindow;
  87. m_began = false;
  88. m_viewUnderStartPoint = 0;
  89. m_userShapeStartPoint = KP_INVALID_POINT;
  90. m_userShapeEndPoint = KP_INVALID_POINT;
  91. m_userShapeSize = KP_INVALID_SIZE;
  92. createAction ();
  93. }
  94. // private
  95. void kpTool::createAction ()
  96. {
  97. #if DEBUG_KP_TOOL && 0
  98. kdDebug () << "kpTool(" << name () << "::createAction()" << endl;
  99. #endif
  100. if (!m_mainWindow)
  101. {
  102. kdError () << "kpTool::createAction() without mw" << endl;
  103. return;
  104. }
  105. TDEActionCollection *ac = m_mainWindow->actionCollection ();
  106. if (!ac)
  107. {
  108. kdError () << "kpTool::createAction() without ac" << endl;
  109. return;
  110. }
  111. if (m_action)
  112. {
  113. // TODO: I don't think this will ever be executed as we are not called
  114. // outside of the constructor.
  115. #if DEBUG_KP_TOOL
  116. kdDebug () << "\tdeleting existing" << endl;
  117. #endif
  118. ac->remove (m_action);
  119. m_action = 0;
  120. }
  121. m_action = new kpToolAction (text (), iconName (), shortcutForKey (m_key),
  122. this, TQT_SLOT (slotActionActivated ()),
  123. m_mainWindow->actionCollection (), name ());
  124. m_action->setExclusiveGroup (TQString::fromLatin1 ("Tool Box Actions"));
  125. m_action->setWhatsThis (description ());
  126. connect (m_action, TQT_SIGNAL (toolTipChanged (const TQString &)),
  127. this, TQT_SLOT (slotActionToolTipChanged (const TQString &)));
  128. }
  129. // protected slot
  130. void kpTool::slotActionToolTipChanged (const TQString &string)
  131. {
  132. emit actionToolTipChanged (string);
  133. }
  134. // public
  135. TQString kpTool::text () const
  136. {
  137. return m_text;
  138. }
  139. // public
  140. void kpTool::setText (const TQString &text)
  141. {
  142. m_text = text;
  143. if (m_action)
  144. m_action->setText (m_text);
  145. else
  146. createAction ();
  147. }
  148. // public static
  149. TQString kpTool::toolTipForTextAndShortcut (const TQString &text,
  150. const TDEShortcut &shortcut)
  151. {
  152. for (int i = 0; i < (int) shortcut.count (); i++)
  153. {
  154. const KKeySequence seq = shortcut.seq (i);
  155. if (seq.count () == 1 && containsSingleKeyTrigger (seq))
  156. {
  157. return i18n ("<Tool Name> (<Single Accel Key>)",
  158. "%1 (%2)")
  159. .arg (text, seq.toString ());
  160. }
  161. }
  162. return text;
  163. }
  164. // public static
  165. TQString kpTool::toolTip () const
  166. {
  167. return toolTipForTextAndShortcut (text (), shortcut ());
  168. }
  169. // public
  170. int kpTool::key () const
  171. {
  172. return m_key;
  173. }
  174. // public
  175. void kpTool::setKey (int key)
  176. {
  177. m_key = key;
  178. if (m_action)
  179. // TODO: this probably not wise since it nukes the user's settings
  180. m_action->setShortcut (shortcutForKey (m_key));
  181. else
  182. createAction ();
  183. }
  184. // public static
  185. TDEShortcut kpTool::shortcutForKey (int key)
  186. {
  187. TDEShortcut shortcut;
  188. if (key)
  189. {
  190. shortcut.append (KKeySequence (KKey (key)));
  191. // (CTRL+<key>, ALT+<key>, CTRL+ALT+<key>, CTRL+SHIFT+<key>
  192. // all clash with global KDE shortcuts)
  193. shortcut.append (KKeySequence (KKey (TQt::ALT + TQt::SHIFT + key)));
  194. }
  195. return shortcut;
  196. }
  197. // public
  198. TDEShortcut kpTool::shortcut () const
  199. {
  200. return m_action ? m_action->shortcut () : TDEShortcut ();
  201. }
  202. // public static
  203. bool kpTool::keyIsText (int key)
  204. {
  205. // TODO: should work like !TQKeyEvent::text().isEmpty()
  206. return !(key & (TQt::MODIFIER_MASK ^ TQt::SHIFT));
  207. }
  208. // public static
  209. bool kpTool::containsSingleKeyTrigger (const KKeySequence &seq)
  210. {
  211. for (int i = 0; i < (int) seq.count (); i++)
  212. {
  213. const KKey key = seq.key (i);
  214. if (keyIsText (key.keyCodeQt ()))
  215. return true;
  216. }
  217. return false;
  218. }
  219. // public static
  220. bool kpTool::containsSingleKeyTrigger (const TDEShortcut &shortcut,
  221. TDEShortcut *shortcutWithoutSingleKeyTriggers)
  222. {
  223. if (shortcutWithoutSingleKeyTriggers)
  224. *shortcutWithoutSingleKeyTriggers = shortcut;
  225. TDEShortcut newShortcut;
  226. bool needNewShortcut = false;
  227. for (int i = 0; i < (int) shortcut.count (); i++)
  228. {
  229. const KKeySequence seq = shortcut.seq (i);
  230. if (containsSingleKeyTrigger (seq))
  231. {
  232. needNewShortcut = true;
  233. }
  234. else
  235. {
  236. newShortcut.append (seq);
  237. }
  238. }
  239. if (needNewShortcut && shortcutWithoutSingleKeyTriggers)
  240. *shortcutWithoutSingleKeyTriggers = newShortcut;
  241. return needNewShortcut;
  242. }
  243. // public
  244. bool kpTool::singleKeyTriggersEnabled () const
  245. {
  246. return (m_action ? m_action->singleKeyTriggersEnabled () : true);
  247. }
  248. // public
  249. void kpTool::enableSingleKeyTriggers (bool enable)
  250. {
  251. #if DEBUG_KP_TOOL && 0
  252. kdDebug () << "kpTool(" << name () << ")::enableSingleKeyTriggers("
  253. << enable << ")" << endl;
  254. #endif
  255. if (!m_action)
  256. {
  257. #if DEBUG_KP_TOOL && 0
  258. kdDebug () << "\tno action" << endl;
  259. #endif
  260. return;
  261. }
  262. m_action->enableSingleKeyTriggers (enable);
  263. }
  264. // public
  265. TQString kpTool::description () const
  266. {
  267. return m_description;
  268. }
  269. // public
  270. void kpTool::setDescription (const TQString &description)
  271. {
  272. m_description = description;
  273. if (m_action)
  274. m_action->setWhatsThis (m_description);
  275. else
  276. createAction ();
  277. }
  278. // public
  279. const char *kpTool::name () const
  280. {
  281. return m_name;
  282. }
  283. // static
  284. TQRect kpTool::neededRect (const TQRect &rect, int lineWidth)
  285. {
  286. int x1, y1, x2, y2;
  287. rect.coords (&x1, &y1, &x2, &y2);
  288. if (lineWidth < 1)
  289. lineWidth = 1;
  290. return TQRect (TQPoint (x1 - lineWidth + 1, y1 - lineWidth + 1),
  291. TQPoint (x2 + lineWidth - 1, y2 + lineWidth - 1));
  292. }
  293. // static
  294. TQPixmap kpTool::neededPixmap (const TQPixmap &pixmap, const TQRect &boundingRect)
  295. {
  296. return kpPixmapFX::getPixmapAt (pixmap, boundingRect);
  297. }
  298. // public
  299. bool kpTool::hasCurrentPoint () const
  300. {
  301. return (viewUnderStartPoint () || viewUnderCursor ());
  302. }
  303. // public
  304. TQPoint kpTool::currentPoint (bool zoomToDoc) const
  305. {
  306. #if DEBUG_KP_TOOL && 0
  307. kdDebug () << "kpTool::currentPoint(zoomToDoc=" << zoomToDoc << ")" << endl;
  308. kdDebug () << "\tviewUnderStartPoint="
  309. << (viewUnderStartPoint () ? viewUnderStartPoint ()->name () : "(none)")
  310. << " viewUnderCursor="
  311. << (viewUnderCursor () ? viewUnderCursor ()->name () : "(none)")
  312. << endl;
  313. #endif
  314. kpView *v = viewUnderStartPoint ();
  315. if (!v)
  316. {
  317. v = viewUnderCursor ();
  318. if (!v)
  319. {
  320. #if DEBUG_KP_TOOL && 0
  321. kdDebug () << "\tno view - returning sentinel" << endl;
  322. #endif
  323. return KP_INVALID_POINT;
  324. }
  325. }
  326. const TQPoint globalPos = TQCursor::pos ();
  327. const TQPoint viewPos = v->mapFromGlobal (globalPos);
  328. #if DEBUG_KP_TOOL && 0
  329. kdDebug () << "\tglobalPos=" << globalPos
  330. << " viewPos=" << viewPos
  331. << endl;
  332. #endif
  333. if (!zoomToDoc)
  334. return viewPos;
  335. const TQPoint docPos = v->transformViewToDoc (viewPos);
  336. #if DEBUG_KP_TOOL && 0
  337. kdDebug () << "\tdocPos=" << docPos << endl;
  338. #endif
  339. return docPos;
  340. }
  341. // public slot
  342. void kpTool::somethingBelowTheCursorChanged ()
  343. {
  344. somethingBelowTheCursorChanged (currentPoint (),
  345. currentPoint (false/*view point*/));
  346. }
  347. // private
  348. // TODO: don't dup code from mouseMoveEvent()
  349. void kpTool::somethingBelowTheCursorChanged (const TQPoint &currentPoint_,
  350. const TQPoint &currentViewPoint_)
  351. {
  352. #if DEBUG_KP_TOOL && 0
  353. kdDebug () << "kpTool::somethingBelowTheCursorChanged(docPoint="
  354. << currentPoint_
  355. << " viewPoint="
  356. << currentViewPoint_
  357. << ")" << endl;
  358. kdDebug () << "\tviewUnderStartPoint="
  359. << (viewUnderStartPoint () ? viewUnderStartPoint ()->name () : "(none)")
  360. << " viewUnderCursor="
  361. << (viewUnderCursor () ? viewUnderCursor ()->name () : "(none)")
  362. << endl;
  363. kdDebug () << "\tbegan draw=" << m_beganDraw << endl;
  364. #endif
  365. m_currentPoint = currentPoint_;
  366. m_currentViewPoint = currentViewPoint_;
  367. if (m_beganDraw)
  368. {
  369. if (m_currentPoint != KP_INVALID_POINT)
  370. {
  371. draw (m_currentPoint, m_lastPoint, TQRect (m_startPoint, m_currentPoint).normalize ());
  372. m_lastPoint = m_currentPoint;
  373. }
  374. }
  375. else
  376. {
  377. hover (m_currentPoint);
  378. }
  379. }
  380. void kpTool::beginInternal ()
  381. {
  382. #if DEBUG_KP_TOOL
  383. kdDebug () << "kpTool::beginInternal()" << endl;
  384. #endif
  385. if (!m_began)
  386. {
  387. // clear leftover statusbar messages
  388. setUserMessage ();
  389. m_currentPoint = currentPoint ();
  390. m_currentViewPoint = currentPoint (false/*view point*/);
  391. setUserShapePoints (m_currentPoint);
  392. // TODO: Audit all the code in this file - states like "m_began" &
  393. // "m_beganDraw" should be set before calling user func.
  394. // Also, m_currentPoint should be more frequently initialised.
  395. // call user virtual func
  396. begin ();
  397. // we've starting using the tool...
  398. m_began = true;
  399. // but we haven't started drawing with it
  400. m_beganDraw = false;
  401. uint keyState = TDEApplication::keyboardModifiers ();
  402. m_shiftPressed = (keyState & TDEApplication::ShiftModifier);
  403. m_controlPressed = (keyState & TDEApplication::ControlModifier);
  404. // TODO: Can't do much about ALT - unless it's always TDEApplication::Modifier1?
  405. // Ditto for everywhere else where I set SHIFT & CTRL but not alt.
  406. m_altPressed = false;
  407. }
  408. }
  409. void kpTool::endInternal ()
  410. {
  411. if (m_began)
  412. {
  413. // before we can stop using the tool, we must stop the current drawing operation (if any)
  414. if (hasBegunShape ())
  415. endShapeInternal (m_currentPoint, TQRect (m_startPoint, m_currentPoint).normalize ());
  416. // call user virtual func
  417. end ();
  418. // clear leftover statusbar messages
  419. setUserMessage ();
  420. setUserShapePoints (currentPoint ());
  421. // we've stopped using the tool...
  422. m_began = false;
  423. // and so we can't be drawing with it
  424. m_beganDraw = false;
  425. if (m_mainWindow)
  426. {
  427. kpToolToolBar *tb = m_mainWindow->toolToolBar ();
  428. if (tb)
  429. {
  430. tb->hideAllToolWidgets ();
  431. }
  432. }
  433. }
  434. }
  435. // virtual
  436. void kpTool::begin ()
  437. {
  438. #if DEBUG_KP_TOOL
  439. kdDebug () << "kpTool::begin() base implementation" << endl;
  440. #endif
  441. }
  442. // virtual
  443. void kpTool::end ()
  444. {
  445. #if DEBUG_KP_TOOL
  446. kdDebug () << "kpTool::end() base implementation" << endl;
  447. #endif
  448. }
  449. void kpTool::beginDrawInternal ()
  450. {
  451. if (!m_beganDraw)
  452. {
  453. beginDraw ();
  454. m_beganDraw = true;
  455. emit beganDraw (m_currentPoint);
  456. }
  457. }
  458. // virtual
  459. void kpTool::beginDraw ()
  460. {
  461. }
  462. // virtual
  463. void kpTool::hover (const TQPoint &point)
  464. {
  465. #if DEBUG_KP_TOOL
  466. kdDebug () << "kpTool::hover" << point
  467. << " base implementation"
  468. << endl;
  469. #endif
  470. setUserShapePoints (point);
  471. }
  472. // virtual
  473. void kpTool::globalDraw ()
  474. {
  475. }
  476. // virtual
  477. void kpTool::reselect ()
  478. {
  479. #if DEBUG_KP_TOOL
  480. kdDebug () << "kpTool::reselect() base implementation" << endl;
  481. #endif
  482. }
  483. // public
  484. TQIconSet kpTool::iconSet (int forceSize) const
  485. {
  486. #if DEBUG_KP_TOOL && 0
  487. kdDebug () << "kpTool(" << name () << ")::iconSet(forceSize=" << forceSize << ")" << endl;
  488. #endif
  489. // (robust in case BarIcon() default arg changes)
  490. if (forceSize > 0)
  491. return BarIconSet (name (), forceSize);
  492. else
  493. return BarIconSet (name ());
  494. }
  495. // public
  496. TQString kpTool::iconName () const
  497. {
  498. return name ();
  499. }
  500. // public
  501. kpToolAction *kpTool::action ()
  502. {
  503. if (!m_action)
  504. createAction ();
  505. return m_action;
  506. }
  507. // protected slots
  508. void kpTool::slotActionActivated ()
  509. {
  510. emit actionActivated ();
  511. }
  512. // virtual
  513. void kpTool::draw (const TQPoint &, const TQPoint &, const TQRect &)
  514. {
  515. }
  516. // also called by kpView
  517. void kpTool::cancelShapeInternal ()
  518. {
  519. if (hasBegunShape ())
  520. {
  521. m_beganDraw = false;
  522. cancelShape ();
  523. m_viewUnderStartPoint = 0;
  524. emit cancelledShape (viewUnderCursor () ? m_currentPoint : KP_INVALID_POINT);
  525. if (viewUnderCursor ())
  526. hover (m_currentPoint);
  527. else
  528. {
  529. m_currentPoint = KP_INVALID_POINT;
  530. m_currentViewPoint = KP_INVALID_POINT;
  531. hover (m_currentPoint);
  532. }
  533. if (returnToPreviousToolAfterEndDraw ())
  534. {
  535. kpToolToolBar *tb = mainWindow ()->toolToolBar ();
  536. // (don't end up with no tool selected)
  537. if (tb->previousTool ())
  538. {
  539. // endInternal() will be called by kpMainWindow (thanks to this line)
  540. // so we won't have the view anymore
  541. tb->selectPreviousTool ();
  542. }
  543. }
  544. }
  545. }
  546. // virtual
  547. void kpTool::cancelShape ()
  548. {
  549. kdWarning () << "Tool cannot cancel operation!" << endl;
  550. }
  551. void kpTool::releasedAllButtons ()
  552. {
  553. }
  554. void kpTool::endDrawInternal (const TQPoint &thisPoint, const TQRect &normalizedRect,
  555. bool wantEndShape)
  556. {
  557. #if DEBUG_KP_TOOL && 1
  558. kdDebug () << "kpTool::endDrawInternal() wantEndShape=" << wantEndShape << endl;
  559. #endif
  560. if (wantEndShape && !hasBegunShape ())
  561. return;
  562. else if (!wantEndShape && !hasBegunDraw ())
  563. return;
  564. m_beganDraw = false;
  565. if (wantEndShape)
  566. {
  567. #if DEBUG_KP_TOOL && 0
  568. kdDebug () << "\tcalling endShape()" << endl;
  569. #endif
  570. endShape (thisPoint, normalizedRect);
  571. }
  572. else
  573. {
  574. #if DEBUG_KP_TOOL && 0
  575. kdDebug () << "\tcalling endDraw()" << endl;
  576. #endif
  577. endDraw (thisPoint, normalizedRect);
  578. }
  579. m_viewUnderStartPoint = 0;
  580. emit endedDraw (m_currentPoint);
  581. if (viewUnderCursor ())
  582. hover (m_currentPoint);
  583. else
  584. {
  585. m_currentPoint = KP_INVALID_POINT;
  586. m_currentViewPoint = KP_INVALID_POINT;
  587. hover (m_currentPoint);
  588. }
  589. if (returnToPreviousToolAfterEndDraw ())
  590. {
  591. kpToolToolBar *tb = mainWindow ()->toolToolBar ();
  592. // (don't end up with no tool selected)
  593. if (tb->previousTool ())
  594. {
  595. // endInternal() will be called by kpMainWindow (thanks to this line)
  596. // so we won't have the view anymore
  597. tb->selectPreviousTool ();
  598. }
  599. }
  600. }
  601. // private
  602. void kpTool::endShapeInternal (const TQPoint &thisPoint, const TQRect &normalizedRect)
  603. {
  604. endDrawInternal (thisPoint, normalizedRect, true/*end shape*/);
  605. }
  606. // virtual
  607. void kpTool::endDraw (const TQPoint &, const TQRect &)
  608. {
  609. }
  610. kpMainWindow *kpTool::mainWindow () const
  611. {
  612. return m_mainWindow;
  613. }
  614. kpDocument *kpTool::document () const
  615. {
  616. return m_mainWindow ? m_mainWindow->document () : 0;
  617. }
  618. kpView *kpTool::viewUnderCursor () const
  619. {
  620. kpViewManager *vm = viewManager ();
  621. return vm ? vm->viewUnderCursor () : 0;
  622. }
  623. kpViewManager *kpTool::viewManager () const
  624. {
  625. return m_mainWindow ? m_mainWindow->viewManager () : 0;
  626. }
  627. kpToolToolBar *kpTool::toolToolBar () const
  628. {
  629. return m_mainWindow ? m_mainWindow->toolToolBar () : 0;
  630. }
  631. kpColor kpTool::color (int which) const
  632. {
  633. if (m_mainWindow)
  634. return m_mainWindow->colorToolBar ()->color (which);
  635. else
  636. {
  637. kdError () << "kpTool::color () without mainWindow" << endl;
  638. return kpColor::invalid;
  639. }
  640. }
  641. kpColor kpTool::foregroundColor () const
  642. {
  643. return color (0);
  644. }
  645. kpColor kpTool::backgroundColor () const
  646. {
  647. return color (1);
  648. }
  649. double kpTool::colorSimilarity () const
  650. {
  651. if (m_mainWindow)
  652. return m_mainWindow->colorToolBar ()->colorSimilarity ();
  653. else
  654. {
  655. kdError () << "kpTool::colorSimilarity() without mainWindow" << endl;
  656. return 0;
  657. }
  658. }
  659. int kpTool::processedColorSimilarity () const
  660. {
  661. if (m_mainWindow)
  662. return m_mainWindow->colorToolBar ()->processedColorSimilarity ();
  663. else
  664. {
  665. kdError () << "kpTool::processedColorSimilarity() without mainWindow" << endl;
  666. return kpColor::Exact;
  667. }
  668. }
  669. kpColor kpTool::oldForegroundColor () const
  670. {
  671. if (m_mainWindow)
  672. return m_mainWindow->colorToolBar ()->oldForegroundColor ();
  673. else
  674. {
  675. kdError () << "kpTool::oldForegroundColor() without mainWindow" << endl;
  676. return kpColor::invalid;
  677. }
  678. }
  679. kpColor kpTool::oldBackgroundColor () const
  680. {
  681. if (m_mainWindow)
  682. return m_mainWindow->colorToolBar ()->oldBackgroundColor ();
  683. else
  684. {
  685. kdError () << "kpTool::oldBackgroundColor() without mainWindow" << endl;
  686. return kpColor::invalid;
  687. }
  688. }
  689. double kpTool::oldColorSimilarity () const
  690. {
  691. if (m_mainWindow)
  692. return m_mainWindow->colorToolBar ()->oldColorSimilarity ();
  693. else
  694. {
  695. kdError () << "kpTool::oldColorSimilarity() without mainWindow" << endl;
  696. return 0;
  697. }
  698. }
  699. void kpTool::slotColorsSwappedInternal (const kpColor &newForegroundColor,
  700. const kpColor &newBackgroundColor)
  701. {
  702. if (careAboutColorsSwapped ())
  703. {
  704. slotColorsSwapped (newForegroundColor, newBackgroundColor);
  705. m_ignoreColorSignals = 2;
  706. }
  707. else
  708. m_ignoreColorSignals = 0;
  709. }
  710. void kpTool::slotForegroundColorChangedInternal (const kpColor &color)
  711. {
  712. if (m_ignoreColorSignals > 0)
  713. {
  714. #if DEBUG_KP_TOOL && 1
  715. kdDebug () << "kpTool::slotForegroundColorChangedInternal() ignoreColorSignals=" << m_ignoreColorSignals << endl;
  716. #endif
  717. m_ignoreColorSignals--;
  718. return;
  719. }
  720. slotForegroundColorChanged (color);
  721. }
  722. void kpTool::slotBackgroundColorChangedInternal (const kpColor &color)
  723. {
  724. if (m_ignoreColorSignals > 0)
  725. {
  726. #if DEBUG_KP_TOOL && 1
  727. kdDebug () << "kpTool::slotBackgroundColorChangedInternal() ignoreColorSignals=" << m_ignoreColorSignals << endl;
  728. #endif
  729. m_ignoreColorSignals--;
  730. return;
  731. }
  732. slotBackgroundColorChanged (color);
  733. }
  734. void kpTool::slotColorSimilarityChangedInternal (double similarity, int processedSimilarity)
  735. {
  736. slotColorSimilarityChanged (similarity, processedSimilarity);
  737. }
  738. bool kpTool::currentPointNextToLast () const
  739. {
  740. if (m_lastPoint == TQPoint (-1, -1))
  741. return true;
  742. int dx = kAbs (m_currentPoint.x () - m_lastPoint.x ());
  743. int dy = kAbs (m_currentPoint.y () - m_lastPoint.y ());
  744. return (dx <= 1 && dy <= 1);
  745. }
  746. bool kpTool::currentPointCardinallyNextToLast () const
  747. {
  748. if (m_lastPoint == TQPoint (-1, -1))
  749. return true;
  750. int dx = kAbs (m_currentPoint.x () - m_lastPoint.x ());
  751. int dy = kAbs (m_currentPoint.y () - m_lastPoint.y ());
  752. return (dx + dy <= 1);
  753. }
  754. kpCommandHistory *kpTool::commandHistory () const
  755. {
  756. return m_mainWindow ? m_mainWindow->commandHistory () : 0;
  757. }
  758. void kpTool::mousePressEvent (TQMouseEvent *e)
  759. {
  760. #if DEBUG_KP_TOOL && 1
  761. kdDebug () << "kpTool::mousePressEvent pos=" << e->pos ()
  762. << " btnStateBefore=" << (int) e->state ()
  763. << " btnStateAfter=" << (int) e->stateAfter ()
  764. << " button=" << (int) e->button ()
  765. << " beganDraw=" << m_beganDraw << endl;
  766. #endif
  767. // state of all the buttons - not just the one that triggered the event (button())
  768. TQt::ButtonState buttonState = e->stateAfter ();
  769. if (m_mainWindow && e->button () == Qt::MidButton)
  770. {
  771. const TQString text = TQApplication::clipboard ()->text (TQClipboard::Selection);
  772. #if DEBUG_KP_TOOL && 1
  773. kdDebug () << "\tMMB pasteText='" << text << "'" << endl;
  774. #endif
  775. if (!text.isEmpty ())
  776. {
  777. if (hasBegunShape ())
  778. {
  779. #if DEBUG_KP_TOOL && 1
  780. kdDebug () << "\t\thasBegunShape - end" << endl;
  781. #endif
  782. endShapeInternal (m_currentPoint,
  783. TQRect (m_startPoint, m_currentPoint).normalize ());
  784. }
  785. if (viewUnderCursor ())
  786. {
  787. m_mainWindow->pasteTextAt (text,
  788. viewUnderCursor ()->transformViewToDoc (e->pos ()),
  789. true/*adjust topLeft so that cursor isn't
  790. on top of resize handle*/);
  791. }
  792. return;
  793. }
  794. }
  795. int mb = mouseButton (buttonState);
  796. #if DEBUG_KP_TOOL && 1
  797. kdDebug () << "\tmb=" << mb << " m_beganDraw=" << m_beganDraw << endl;
  798. #endif
  799. if (mb == -1 && !m_beganDraw) return; // ignore
  800. if (m_beganDraw)
  801. {
  802. if (mb == -1 || mb != m_mouseButton)
  803. {
  804. #if DEBUG_KP_TOOL && 1
  805. kdDebug () << "\tCancelling operation as " << mb << " == -1 or != " << m_mouseButton << endl;
  806. #endif
  807. kpView *view = viewUnderStartPoint ();
  808. if (!view)
  809. {
  810. kdError () << "kpTool::mousePressEvent() cancel without a view under the start point!" << endl;
  811. }
  812. // if we get a mousePressEvent when we're drawing, then the other
  813. // mouse button must have been pressed
  814. m_currentPoint = view ? view->transformViewToDoc (e->pos ()) : TQPoint (-1, -1);
  815. m_currentViewPoint = view ? e->pos () : TQPoint (-1, -1);
  816. cancelShapeInternal ();
  817. }
  818. return;
  819. }
  820. kpView *view = viewUnderCursor ();
  821. if (!view)
  822. {
  823. kdError () << "kpTool::mousePressEvent() without a view under the cursor!" << endl;
  824. }
  825. #if DEBUG_KP_TOOL && 1
  826. if (view)
  827. kdDebug () << "\tview=" << view->name () << endl;
  828. #endif
  829. // let user know what mouse button is being used for entire draw
  830. m_mouseButton = mouseButton (buttonState);
  831. m_shiftPressed = (buttonState & TQt::ShiftButton);
  832. m_controlPressed = (buttonState & TQt::ControlButton);
  833. m_altPressed = (buttonState & TQt::AltButton);
  834. m_startPoint = m_currentPoint = view ? view->transformViewToDoc (e->pos ()) : TQPoint (-1, -1);
  835. m_currentViewPoint = view ? e->pos () : TQPoint (-1, -1);
  836. m_viewUnderStartPoint = view;
  837. m_lastPoint = TQPoint (-1, -1);
  838. #if DEBUG_KP_TOOL && 1
  839. kdDebug () << "\tBeginning draw @ " << m_currentPoint << endl;
  840. #endif
  841. beginDrawInternal ();
  842. draw (m_currentPoint, m_lastPoint, TQRect (m_currentPoint, m_currentPoint));
  843. m_lastPoint = m_currentPoint;
  844. }
  845. void kpTool::mouseMoveEvent (TQMouseEvent *e)
  846. {
  847. #if DEBUG_KP_TOOL && 0
  848. kdDebug () << "kpTool::mouseMoveEvent pos=" << e->pos ()
  849. << " btnStateAfter=" << (int) e->stateAfter () << endl;
  850. kpView *v0 = viewUnderCursor (),
  851. *v1 = viewManager ()->viewUnderCursor (true/*use TQt*/),
  852. *v2 = viewUnderStartPoint ();
  853. kdDebug () << "\tviewUnderCursor=" << (v0 ? v0->name () : "(none)")
  854. << " viewUnderCursorTQt=" << (v1 ? v1->name () : "(none)")
  855. << " viewUnderStartPoint=" << (v2 ? v2->name () : "(none)")
  856. << endl;
  857. kdDebug () << "\tfocusWidget=" << kapp->focusWidget () << endl;
  858. #endif
  859. TQt::ButtonState buttonState = e->stateAfter ();
  860. m_shiftPressed = (buttonState & TQt::ShiftButton);
  861. m_controlPressed = (buttonState & TQt::ControlButton);
  862. m_altPressed = (buttonState & TQt::AltButton);
  863. if (m_beganDraw)
  864. {
  865. kpView *view = viewUnderStartPoint ();
  866. if (!view)
  867. {
  868. kdError () << "kpTool::mouseMoveEvent() without a view under the start point!" << endl;
  869. return;
  870. }
  871. m_currentPoint = view->transformViewToDoc (e->pos ());
  872. m_currentViewPoint = e->pos ();
  873. #if DEBUG_KP_TOOL && 0
  874. kdDebug () << "\tDraw!" << endl;
  875. #endif
  876. bool dragScrolled = false;
  877. movedAndAboutToDraw (m_currentPoint, m_lastPoint, view->zoomLevelX (), &dragScrolled);
  878. if (dragScrolled)
  879. {
  880. m_currentPoint = currentPoint ();
  881. m_currentViewPoint = currentPoint (false/*view point*/);
  882. // Scrollview has scrolled contents and has scheduled an update
  883. // for the newly exposed region. If draw() schedules an update
  884. // as well (instead of immediately updating), the scrollview's
  885. // update will be executed first and it'll only update part of
  886. // the screen resulting in ugly tearing of the viewManager's
  887. // tempPixmap.
  888. viewManager ()->setFastUpdates ();
  889. }
  890. draw (m_currentPoint, m_lastPoint, TQRect (m_startPoint, m_currentPoint).normalize ());
  891. if (dragScrolled)
  892. viewManager ()->restoreFastUpdates ();
  893. m_lastPoint = m_currentPoint;
  894. }
  895. else
  896. {
  897. kpView *view = viewUnderCursor ();
  898. if (!view) // possible if cancelShape()'ed but still holding down initial mousebtn
  899. {
  900. m_currentPoint = KP_INVALID_POINT;
  901. m_currentViewPoint = KP_INVALID_POINT;
  902. return;
  903. }
  904. m_currentPoint = view->transformViewToDoc (e->pos ());
  905. m_currentViewPoint = e->pos ();
  906. hover (m_currentPoint);
  907. }
  908. }
  909. void kpTool::mouseReleaseEvent (TQMouseEvent *e)
  910. {
  911. #if DEBUG_KP_TOOL && 1
  912. kdDebug () << "kpTool::mouseReleaseEvent pos=" << e->pos ()
  913. << " btnStateBefore=" << (int) e->state ()
  914. << " btnStateAfter=" << (int) e->stateAfter ()
  915. << " button=" << (int) e->button () << endl;
  916. #endif
  917. if (m_beganDraw) // didn't cancelShape()
  918. {
  919. kpView *view = viewUnderStartPoint ();
  920. if (!view)
  921. {
  922. kdError () << "kpTool::mouseReleaseEvent() without a view under the start point!" << endl;
  923. return;
  924. }
  925. m_currentPoint = view ? view->transformViewToDoc (e->pos ()) : TQPoint (-1, -1);
  926. m_currentViewPoint = view ? e->pos () : TQPoint (-1, -1);
  927. endDrawInternal (m_currentPoint, TQRect (m_startPoint, m_currentPoint).normalize ());
  928. }
  929. if ((e->stateAfter () & Qt::MouseButtonMask) == 0)
  930. {
  931. releasedAllButtons ();
  932. }
  933. }
  934. void kpTool::wheelEvent (TQWheelEvent *e)
  935. {
  936. #if DEBUG_KP_TOOL
  937. kdDebug () << "kpTool::wheelEvent() state=" << e->state ()
  938. << " hasBegunDraw=" << hasBegunDraw ()
  939. << " delta=" << e->delta ()
  940. << endl;
  941. #endif
  942. e->ignore ();
  943. // If CTRL not pressed, bye.
  944. if ((e->state () & TQt::ControlButton) == 0)
  945. return;
  946. // If drawing, bye; don't care if a shape in progress though.
  947. if (hasBegunDraw ())
  948. return;
  949. // Zoom in/out depending on wheel direction.
  950. // Moved wheel away from user?
  951. if (e->delta () > 0)
  952. {
  953. m_mainWindow->zoomIn (true/*center under cursor*/);
  954. e->accept ();
  955. }
  956. // Moved wheel towards user?
  957. else if (e->delta () < 0)
  958. {
  959. #if 1
  960. m_mainWindow->zoomOut (true/*center under cursor - make zoom in/out
  961. stay under same doc pos*/);
  962. #else
  963. m_mainWindow->zoomOut (false/*don't center under cursor - as is
  964. confusing behaviour when zooming
  965. out*/);
  966. #endif
  967. e->accept ();
  968. }
  969. }
  970. void kpTool::keyPressEvent (TQKeyEvent *e)
  971. {
  972. #if DEBUG_KP_TOOL && 0
  973. kdDebug () << "kpTool::keyPressEvent() e->key=" << e->key () << endl;
  974. #endif
  975. int dx = 0, dy = 0;
  976. e->ignore ();
  977. switch (e->key ())
  978. {
  979. case 0:
  980. case TQt::Key_unknown:
  981. #if DEBUG_KP_TOOL && 0
  982. kdDebug () << "kpTool::keyPressEvent() picked up unknown key!" << endl;
  983. #endif
  984. // --- fall thru and update all modifiers ---
  985. case TQt::Key_Alt:
  986. case TQt::Key_Shift:
  987. case TQt::Key_Control:
  988. keyUpdateModifierState (e);
  989. e->accept ();
  990. break;
  991. case TQt::Key_Delete:
  992. m_mainWindow->slotDelete ();
  993. break;
  994. /*
  995. * TQCursor::setPos conveniently causes mouseMoveEvents :)
  996. */
  997. case TQt::Key_Home: dx = -1, dy = -1; break;
  998. case TQt::Key_Up: dy = -1; break;
  999. case TQt::Key_PageUp: dx = +1, dy = -1; break;
  1000. case TQt::Key_Left: dx = -1; break;
  1001. case TQt::Key_Right: dx = +1; break;
  1002. case TQt::Key_End: dx = -1, dy = +1; break;
  1003. case TQt::Key_Down: dy = +1; break;
  1004. case TQt::Key_PageDown: dx = +1, dy = +1; break;
  1005. case TQt::Key_Enter:
  1006. case TQt::Key_Return:
  1007. case TQt::Key_Insert:
  1008. case TQt::Key_Clear/*Numpad 5 Key*/:
  1009. {
  1010. kpView *view = viewUnderCursor (); // TODO: wrong for dragging lines outside of view (for e.g.)
  1011. if (view)
  1012. {
  1013. // TODO: what about the modifiers
  1014. TQMouseEvent me (TQEvent::MouseButtonPress,
  1015. view->mapFromGlobal (TQCursor::pos ()),
  1016. Qt::LeftButton,
  1017. 0);
  1018. mousePressEvent (&me);
  1019. e->accept ();
  1020. }
  1021. break;
  1022. }}
  1023. kpView *view = viewUnderCursor ();
  1024. if (view && (dx || dy))
  1025. {
  1026. TQPoint oldPoint = view->mapFromGlobal (TQCursor::pos ());
  1027. #if DEBUG_KP_TOOL && 0
  1028. kdDebug () << "\toldPoint=" << oldPoint
  1029. << " dx=" << dx << " dy=" << dy << endl;
  1030. #endif
  1031. const int viewIncX = (dx ? TQMAX (1, view->zoomLevelX () / 100) * dx : 0);
  1032. const int viewIncY = (dy ? TQMAX (1, view->zoomLevelY () / 100) * dy : 0);
  1033. int newViewX = oldPoint.x () + viewIncX;
  1034. int newViewY = oldPoint.y () + viewIncY;
  1035. #if DEBUG_KP_TOOL && 0
  1036. kdDebug () << "\tnewPoint=" << TQPoint (newViewX, newViewY) << endl;
  1037. #endif
  1038. if (view->transformViewToDoc (TQPoint (newViewX, newViewY)) ==
  1039. view->transformViewToDoc (oldPoint))
  1040. {
  1041. newViewX += viewIncX, newViewY += viewIncY;
  1042. #if DEBUG_KP_TOOL && 0
  1043. kdDebug () << "\tneed adjust for doc - newPoint="
  1044. << TQPoint (newViewX, newViewY) << endl;
  1045. #endif
  1046. }
  1047. // TODO: visible width/height (e.g. with scrollbars)
  1048. int x = TQMIN (TQMAX (newViewX, 0), view->width () - 1);
  1049. int y = TQMIN (TQMAX (newViewY, 0), view->height () - 1);
  1050. TQCursor::setPos (view->mapToGlobal (TQPoint (x, y)));
  1051. e->accept ();
  1052. }
  1053. }
  1054. void kpTool::keyReleaseEvent (TQKeyEvent *e)
  1055. {
  1056. #if DEBUG_KP_TOOL && 0
  1057. kdDebug () << "kpTool::keyReleaseEvent() e->key=" << e->key () << endl;
  1058. #endif
  1059. e->ignore ();
  1060. switch (e->key ())
  1061. {
  1062. case 0:
  1063. case TQt::Key_unknown:
  1064. #if DEBUG_KP_TOOL
  1065. kdDebug () << "kpTool::keyReleaseEvent() picked up unknown key!" << endl;
  1066. #endif
  1067. // HACK: around TQt bug: if you hold a modifier before you start the
  1068. // program and then release it over the view,
  1069. // TQt reports it as the release of an unknown key
  1070. // --- fall thru and update all modifiers ---
  1071. case TQt::Key_Alt:
  1072. case TQt::Key_Shift:
  1073. case TQt::Key_Control:
  1074. keyUpdateModifierState (e);
  1075. e->accept ();
  1076. break;
  1077. case TQt::Key_Escape:
  1078. if (hasBegunDraw ())
  1079. {
  1080. cancelShapeInternal ();
  1081. e->accept ();
  1082. }
  1083. break;
  1084. case TQt::Key_Enter:
  1085. case TQt::Key_Return:
  1086. case TQt::Key_Insert:
  1087. case TQt::Key_Clear/*Numpad 5 Key*/:
  1088. {
  1089. kpView *view = viewUnderCursor ();
  1090. if (view)
  1091. {
  1092. TQMouseEvent me (TQEvent::MouseButtonRelease,
  1093. view->mapFromGlobal (TQCursor::pos ()),
  1094. Qt::LeftButton,
  1095. Qt::LeftButton);
  1096. mouseReleaseEvent (&me);
  1097. e->accept ();
  1098. }
  1099. break;
  1100. }}
  1101. }
  1102. // private
  1103. void kpTool::keyUpdateModifierState (TQKeyEvent *e)
  1104. {
  1105. #if DEBUG_KP_TOOL && 0
  1106. kdDebug () << "kpTool::updateModifierState() e->key=" << e->key () << endl;
  1107. kdDebug () << "\tshift="
  1108. << (e->stateAfter () & TQt::ShiftButton)
  1109. << " control="
  1110. << (e->stateAfter () & TQt::ControlButton)
  1111. << " alt="
  1112. << (e->stateAfter () & TQt::AltButton)
  1113. << endl;
  1114. #endif
  1115. if (e->key () & (TQt::Key_Alt | TQt::Key_Shift | TQt::Key_Control))
  1116. {
  1117. #if DEBUG_KP_TOOL && 0
  1118. kdDebug () << "\t\tmodifier changed - use e's claims" << endl;
  1119. #endif
  1120. setShiftPressed (e->stateAfter () & TQt::ShiftButton);
  1121. setControlPressed (e->stateAfter () & TQt::ControlButton);
  1122. setAltPressed (e->stateAfter () & TQt::AltButton);
  1123. }
  1124. else
  1125. {
  1126. #if DEBUG_KP_TOOL && 0
  1127. kdDebug () << "\t\tmodifiers not changed - figure out the truth" << endl;
  1128. #endif
  1129. uint keyState = TDEApplication::keyboardModifiers ();
  1130. setShiftPressed (keyState & TDEApplication::ShiftModifier);
  1131. setControlPressed (keyState & TDEApplication::ControlModifier);
  1132. // TODO: Can't do much about ALT - unless it's always TDEApplication::Modifier1?
  1133. // Ditto for everywhere else where I set SHIFT & CTRL but not alt.
  1134. setAltPressed (e->stateAfter () & TQt::AltButton);
  1135. }
  1136. }
  1137. void kpTool::notifyModifierStateChanged ()
  1138. {
  1139. if (careAboutModifierState ())
  1140. {
  1141. if (m_beganDraw)
  1142. draw (m_currentPoint, m_lastPoint, TQRect (m_startPoint, m_currentPoint).normalize ());
  1143. else
  1144. {
  1145. m_currentPoint = currentPoint ();
  1146. m_currentViewPoint = currentPoint (false/*view point*/);
  1147. hover (m_currentPoint);
  1148. }
  1149. }
  1150. }
  1151. void kpTool::setShiftPressed (bool pressed)
  1152. {
  1153. if (pressed == m_shiftPressed)
  1154. return;
  1155. m_shiftPressed = pressed;
  1156. notifyModifierStateChanged ();
  1157. }
  1158. void kpTool::setControlPressed (bool pressed)
  1159. {
  1160. if (pressed == m_controlPressed)
  1161. return;
  1162. m_controlPressed = pressed;
  1163. notifyModifierStateChanged ();
  1164. }
  1165. void kpTool::setAltPressed (bool pressed)
  1166. {
  1167. if (pressed = m_altPressed)
  1168. return;
  1169. m_altPressed = pressed;
  1170. notifyModifierStateChanged ();
  1171. }
  1172. void kpTool::focusInEvent (TQFocusEvent *)
  1173. {
  1174. }
  1175. void kpTool::focusOutEvent (TQFocusEvent *)
  1176. {
  1177. #if DEBUG_KP_TOOL && 0
  1178. kdDebug () << "kpTool::focusOutEvent() beganDraw=" << m_beganDraw << endl;
  1179. #endif
  1180. if (m_beganDraw)
  1181. endDrawInternal (m_currentPoint, TQRect (m_startPoint, m_currentPoint).normalize ());
  1182. }
  1183. void kpTool::enterEvent (TQEvent *)
  1184. {
  1185. #if DEBUG_KP_TOOL && 1
  1186. kdDebug () << "kpTool::enterEvent() beganDraw=" << m_beganDraw << endl;
  1187. #endif
  1188. }
  1189. void kpTool::leaveEvent (TQEvent *)
  1190. {
  1191. #if DEBUG_KP_TOOL && 1
  1192. kdDebug () << "kpTool::leaveEvent() beganDraw=" << m_beganDraw << endl;
  1193. #endif
  1194. // if we haven't started drawing (e.g. dragging a rectangle)...
  1195. if (!m_beganDraw)
  1196. {
  1197. m_currentPoint = KP_INVALID_POINT;
  1198. m_currentViewPoint = KP_INVALID_POINT;
  1199. hover (m_currentPoint);
  1200. }
  1201. }
  1202. // static
  1203. int kpTool::mouseButton (const TQt::ButtonState &buttonState)
  1204. {
  1205. // we have nothing to do with mid-buttons
  1206. if (buttonState & Qt::MidButton)
  1207. return -1;
  1208. // both left & right together is quite meaningless...
  1209. TQt::ButtonState bothButtons = (TQt::ButtonState) (Qt::LeftButton | Qt::RightButton);
  1210. if ((buttonState & bothButtons) == bothButtons)
  1211. return -1;
  1212. if (buttonState & Qt::LeftButton)
  1213. return 0;
  1214. else if (buttonState & Qt::RightButton)
  1215. return 1;
  1216. else
  1217. return -1;
  1218. }
  1219. /*
  1220. * User Notifications
  1221. */
  1222. // public static
  1223. TQString kpTool::cancelUserMessage (int mouseButton)
  1224. {
  1225. if (mouseButton == 0)
  1226. return i18n ("Right click to cancel.");
  1227. else
  1228. return i18n ("Left click to cancel.");
  1229. }
  1230. // public
  1231. TQString kpTool::cancelUserMessage () const
  1232. {
  1233. return cancelUserMessage (m_mouseButton);
  1234. }
  1235. // public
  1236. TQString kpTool::userMessage () const
  1237. {
  1238. return m_userMessage;
  1239. }
  1240. // public
  1241. void kpTool::setUserMessage (const TQString &userMessage)
  1242. {
  1243. m_userMessage = userMessage;
  1244. if (m_userMessage.isEmpty ())
  1245. m_userMessage = text ();
  1246. else
  1247. m_userMessage.prepend (i18n ("%1: ").arg (text ()));
  1248. emit userMessageChanged (m_userMessage);
  1249. }
  1250. // public
  1251. TQPoint kpTool::userShapeStartPoint () const
  1252. {
  1253. return m_userShapeStartPoint;
  1254. }
  1255. // public
  1256. TQPoint kpTool::userShapeEndPoint () const
  1257. {
  1258. return m_userShapeEndPoint;
  1259. }
  1260. // public static
  1261. int kpTool::calculateLength (int start, int end)
  1262. {
  1263. if (start <= end)
  1264. {
  1265. return end - start + 1;
  1266. }
  1267. else
  1268. {
  1269. return end - start - 1;
  1270. }
  1271. }
  1272. // public
  1273. void kpTool::setUserShapePoints (const TQPoint &startPoint,
  1274. const TQPoint &endPoint,
  1275. bool setSize)
  1276. {
  1277. m_userShapeStartPoint = startPoint;
  1278. m_userShapeEndPoint = endPoint;
  1279. emit userShapePointsChanged (m_userShapeStartPoint, m_userShapeEndPoint);
  1280. if (setSize)
  1281. {
  1282. if (startPoint != KP_INVALID_POINT &&
  1283. endPoint != KP_INVALID_POINT)
  1284. {
  1285. setUserShapeSize (calculateLength (startPoint.x (), endPoint.x ()),
  1286. calculateLength (startPoint.y (), endPoint.y ()));
  1287. }
  1288. else
  1289. {
  1290. setUserShapeSize ();
  1291. }
  1292. }
  1293. }
  1294. // public
  1295. TQSize kpTool::userShapeSize () const
  1296. {
  1297. return m_userShapeSize;
  1298. }
  1299. // public
  1300. int kpTool::userShapeWidth () const
  1301. {
  1302. return m_userShapeSize.width ();
  1303. }
  1304. // public
  1305. int kpTool::userShapeHeight () const
  1306. {
  1307. return m_userShapeSize.height ();
  1308. }
  1309. // public
  1310. void kpTool::setUserShapeSize (const TQSize &size)
  1311. {
  1312. m_userShapeSize = size;
  1313. emit userShapeSizeChanged (m_userShapeSize);
  1314. emit userShapeSizeChanged (m_userShapeSize.width (),
  1315. m_userShapeSize.height ());
  1316. }
  1317. // public
  1318. void kpTool::setUserShapeSize (int width, int height)
  1319. {
  1320. setUserShapeSize (TQSize (width, height));
  1321. }
  1322. // public static
  1323. bool kpTool::warnIfBigImageSize (int oldWidth, int oldHeight,
  1324. int newWidth, int newHeight,
  1325. const TQString &text,
  1326. const TQString &caption,
  1327. const TQString &continueButtonText,
  1328. TQWidget *parent)
  1329. {
  1330. #if DEBUG_KP_TOOL
  1331. kdDebug () << "kpTool::warnIfBigImageSize()"
  1332. << " old: w=" << oldWidth << " h=" << oldWidth
  1333. << " new: w=" << newWidth << " h=" << newHeight
  1334. << " pixmapSize="
  1335. << kpPixmapFX::pixmapSize (newWidth,
  1336. newHeight,
  1337. TQPixmap::defaultDepth ())
  1338. << " vs BigImageSize=" << KP_BIG_IMAGE_SIZE
  1339. << endl;
  1340. #endif
  1341. // Only got smaller or unchanged - don't complain
  1342. if (!(newWidth > oldWidth || newHeight > oldHeight))
  1343. {
  1344. return true;
  1345. }
  1346. // Was already large - user was warned before, don't annoy him/her again
  1347. if (kpPixmapFX::pixmapSize (oldWidth, oldHeight, TQPixmap::defaultDepth ()) >=
  1348. KP_BIG_IMAGE_SIZE)
  1349. {
  1350. return true;
  1351. }
  1352. if (kpPixmapFX::pixmapSize (newWidth, newHeight, TQPixmap::defaultDepth ()) >=
  1353. KP_BIG_IMAGE_SIZE)
  1354. {
  1355. int accept = KMessageBox::warningContinueCancel (parent,
  1356. text,
  1357. caption,
  1358. continueButtonText,
  1359. TQString::fromLatin1 ("BigImageDontAskAgain"));
  1360. return (accept == KMessageBox::Continue);
  1361. }
  1362. else
  1363. {
  1364. return true;
  1365. }
  1366. }
  1367. #include <kptool.moc>