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.

kpmainwindow_edit.cpp 32KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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. #include <kpmainwindow.h>
  24. #include <tqapplication.h>
  25. #include <tqclipboard.h>
  26. #include <tqdatetime.h>
  27. #include <tqfontmetrics.h>
  28. #include <tqimage.h>
  29. #include <tqpixmap.h>
  30. #include <tqvaluevector.h>
  31. #include <tdeaction.h>
  32. #include <kdebug.h>
  33. #include <tdelocale.h>
  34. #include <tdemessagebox.h>
  35. #include <kstdaction.h>
  36. #include <kpcommandhistory.h>
  37. #include <kpdocument.h>
  38. #include <kpdocumentmetainfo.h>
  39. #include <kpdocumentsaveoptions.h>
  40. #include <kppixmapfx.h>
  41. #include <kpselection.h>
  42. #include <kpselectiondrag.h>
  43. #include <kpselectiontransparency.h>
  44. #include <kptool.h>
  45. #include <kptoolcrop.h>
  46. #include <kptoolresizescale.h>
  47. #include <kptoolselection.h>
  48. #include <kptooltext.h>
  49. #include <kpviewmanager.h>
  50. #include <kpviewscrollablecontainer.h>
  51. #include <kpzoomedview.h>
  52. // private
  53. kpPixmapFX::WarnAboutLossInfo kpMainWindow::pasteWarnAboutLossInfo ()
  54. {
  55. return kpPixmapFX::WarnAboutLossInfo (
  56. i18n ("The image to be pasted"
  57. " may have more colors than the current screen mode."
  58. " In order to display it, some colors may be changed."
  59. " Try increasing your screen depth to at least %1bpp."
  60. "\nIt also"
  61. " contains translucency which is not fully"
  62. " supported. The translucency data will be"
  63. " approximated with a 1-bit transparency mask."),
  64. i18n ("The image to be pasted"
  65. " may have more colors than the current screen mode."
  66. " In order to display it, some colors may be changed."
  67. " Try increasing your screen depth to at least %1bpp."),
  68. i18n ("The image to be pasted"
  69. " contains translucency which is not fully"
  70. " supported. The translucency data will be"
  71. " approximated with a 1-bit transparency mask."),
  72. "paste",
  73. this);
  74. }
  75. // private
  76. void kpMainWindow::setupEditMenuActions ()
  77. {
  78. TDEActionCollection *ac = actionCollection ();
  79. // Undo/Redo
  80. // CONFIG: need GUI
  81. m_commandHistory = new kpCommandHistory (true/*read config*/, this);
  82. if (m_configFirstTime)
  83. {
  84. // (so that cfg-file-editing user can modify in the meantime)
  85. m_commandHistory->writeConfig ();
  86. }
  87. m_actionCut = KStdAction::cut (TQT_TQOBJECT(this), TQT_SLOT (slotCut ()), ac);
  88. m_actionCopy = KStdAction::copy (TQT_TQOBJECT(this), TQT_SLOT (slotCopy ()), ac);
  89. m_actionPaste = KStdAction::paste (TQT_TQOBJECT(this), TQT_SLOT (slotPaste ()), ac);
  90. m_actionPasteInNewWindow = new TDEAction (i18n ("Paste in &New Window"),
  91. TQt::CTRL + TQt::SHIFT + TQt::Key_V,
  92. TQT_TQOBJECT(this), TQT_SLOT (slotPasteInNewWindow ()), ac, "edit_paste_in_new_window");
  93. //m_actionDelete = KStdAction::clear (this, TQT_SLOT (slotDelete ()), ac);
  94. m_actionDelete = new TDEAction (i18n ("&Delete Selection"), 0,
  95. TQT_TQOBJECT(this), TQT_SLOT (slotDelete ()), ac, "edit_clear");
  96. m_actionSelectAll = KStdAction::selectAll (TQT_TQOBJECT(this), TQT_SLOT (slotSelectAll ()), ac);
  97. m_actionDeselect = KStdAction::deselect (TQT_TQOBJECT(this), TQT_SLOT (slotDeselect ()), ac);
  98. m_actionCopyToFile = new TDEAction (i18n ("C&opy to File..."), 0,
  99. TQT_TQOBJECT(this), TQT_SLOT (slotCopyToFile ()), ac, "edit_copy_to_file");
  100. m_actionPasteFromFile = new TDEAction (i18n ("Paste &From File..."), 0,
  101. TQT_TQOBJECT(this), TQT_SLOT (slotPasteFromFile ()), ac, "edit_paste_from_file");
  102. m_editMenuDocumentActionsEnabled = false;
  103. enableEditMenuDocumentActions (false);
  104. // Paste should always be enabled, as long as there is something paste
  105. // (independent of whether we have a document or not)
  106. connect (TQApplication::clipboard (), TQT_SIGNAL (dataChanged ()),
  107. TQT_TQOBJECT(this), TQT_SLOT (slotEnablePaste ()));
  108. slotEnablePaste ();
  109. }
  110. // private
  111. void kpMainWindow::enableEditMenuDocumentActions (bool enable)
  112. {
  113. // m_actionCut
  114. // m_actionCopy
  115. // m_actionPaste
  116. // m_actionPasteInNewWindow
  117. // m_actionDelete
  118. m_actionSelectAll->setEnabled (enable);
  119. // m_actionDeselect
  120. m_editMenuDocumentActionsEnabled = enable;
  121. // m_actionCopyToFile
  122. // Unlike m_actionPaste, we disable this if there is no document.
  123. // This is because "File / Open" would do the same thing, if there is
  124. // no document.
  125. m_actionPasteFromFile->setEnabled (enable);
  126. }
  127. // public
  128. TQPopupMenu *kpMainWindow::selectionToolRMBMenu ()
  129. {
  130. return (TQPopupMenu *) guiFactory ()->container ("selectionToolRMBMenu", this);
  131. }
  132. // private slot
  133. void kpMainWindow::slotCut ()
  134. {
  135. #if DEBUG_KP_MAIN_WINDOW && 1
  136. kdDebug () << "kpMainWindow::slotCut() CALLED" << endl;
  137. #endif
  138. if (!m_document || !m_document->selection ())
  139. {
  140. kdError () << "kpMainWindow::slotCut () doc=" << m_document
  141. << " sel=" << (m_document ? m_document->selection () : 0)
  142. << endl;
  143. return;
  144. }
  145. TQApplication::setOverrideCursor (TQt::waitCursor);
  146. if (toolHasBegunShape ())
  147. tool ()->endShapeInternal ();
  148. slotCopy ();
  149. slotDelete ();
  150. TQApplication::restoreOverrideCursor ();
  151. }
  152. // private slot
  153. void kpMainWindow::slotCopy ()
  154. {
  155. #if DEBUG_KP_MAIN_WINDOW && 1
  156. kdDebug () << "kpMainWindow::slotCopy() CALLED" << endl;
  157. #endif
  158. if (!m_document || !m_document->selection ())
  159. {
  160. kdError () << "kpMainWindow::slotCopy () doc=" << m_document
  161. << " sel=" << (m_document ? m_document->selection () : 0)
  162. << endl;
  163. return;
  164. }
  165. TQApplication::setOverrideCursor (TQt::waitCursor);
  166. if (toolHasBegunShape ())
  167. tool ()->endShapeInternal ();
  168. kpSelection sel = *m_document->selection ();
  169. // Transparency doesn't get sent across the aether so nuke it now
  170. // so that transparency mask doesn't get needlessly recalculated
  171. // if we ever call sel.setPixmap().
  172. sel.setTransparency (kpSelectionTransparency ());
  173. if (sel.isText ())
  174. {
  175. if (!sel.text ().isEmpty ())
  176. {
  177. TQApplication::clipboard ()->setData (new TQTextDrag (sel.text ()),
  178. TQClipboard::Clipboard);
  179. // SYNC: Normally, users highlight text and press CTRL+C.
  180. // Highlighting text copies it to the X11 "middle
  181. // mouse button" clipboard. CTRL+C copies it to the
  182. // separate, Windows-like "CTRL+V" clipboard.
  183. //
  184. // However, KolourPaint doesn't support highlighting.
  185. // So when they press CTRL+C to copy all text, simulate
  186. // the highlighting by copying the text to the "middle
  187. // mouse button" clipboard. We don't do this for images
  188. // as no one ever middle-mouse-pastes images.
  189. //
  190. // Note that we don't share the TQTextDrag pointer with
  191. // the above in case TQt doesn't expect it.
  192. //
  193. // Once we change KolourPaint to support highlighted text
  194. // and CTRL+C to copy only the highlighted text, delete
  195. // this code.
  196. TQApplication::clipboard ()->setData (new TQTextDrag (sel.text ()),
  197. TQClipboard::Selection);
  198. }
  199. }
  200. else
  201. {
  202. TQPixmap rawPixmap;
  203. if (sel.pixmap ())
  204. rawPixmap = *sel.pixmap ();
  205. else
  206. rawPixmap = m_document->getSelectedPixmap ();
  207. // Some apps, such as OpenOffice.org 2.0.4, ignore the image mask
  208. // when pasting. For transparent pixels, the uninitialized RGB
  209. // values are used. Fix this by initializing those values to a
  210. // neutral color -- white.
  211. //
  212. // Strangely enough, OpenOffice.org respects the mask when inserting
  213. // an image from a file, as opposed to pasting one from the clipboard.
  214. sel.setPixmap (
  215. kpPixmapFX::pixmapWithDefinedTransparentPixels (
  216. rawPixmap,
  217. TQt::white)); // CONFIG
  218. TQApplication::clipboard ()->setData (new kpSelectionDrag (sel),
  219. TQClipboard::Clipboard);
  220. }
  221. TQApplication::restoreOverrideCursor ();
  222. }
  223. static bool HasSomethingToPaste (kpMainWindow *mw)
  224. {
  225. #if DEBUG_KP_MAIN_WINDOW
  226. kdDebug () << "kpMainWindow(" << mw->name () << "):HasSomethingToPaste()" << endl;
  227. TQTime timer;
  228. timer.start ();
  229. #else
  230. (void) mw;
  231. #endif
  232. bool hasSomething = false;
  233. TQMimeSource *ms = TQApplication::clipboard ()->data (TQClipboard::Clipboard);
  234. if (ms)
  235. {
  236. // It's faster to test for TQTextDrag::canDecode() first due to the
  237. // lazy evaluation of the '||' operator.
  238. hasSomething = (TQTextDrag::canDecode (ms) ||
  239. kpSelectionDrag::canDecode (ms));
  240. #if DEBUG_KP_MAIN_WINDOW
  241. kdDebug () << "\t" << mw->name () << "***canDecode=" << timer.restart () << endl;
  242. for (int i = 0; ; i++)
  243. {
  244. const char *fmt = ms->format (i);
  245. if (!fmt)
  246. break;
  247. kdDebug () << "\t'" << fmt << "'" << endl;
  248. }
  249. #endif
  250. }
  251. return hasSomething;
  252. }
  253. // HACK: SYNC: Non-TQt apps do not cause TQApplication::clipboard() to
  254. // emit dataChanged(). We don't want to have our paste
  255. // action disabled when we can actually paste something.
  256. //
  257. // So we make sure the paste action is always enabled and
  258. // before any paste, we check if there's actually something
  259. // to paste (HasSomethingToPasteWithDialogIfNot ()).
  260. #if 1 // Hack code path
  261. // Call before any paste only.
  262. static bool HasSomethingToPasteWithDialogIfNot (kpMainWindow *mw)
  263. {
  264. if (::HasSomethingToPaste (mw))
  265. return true;
  266. // STRING: Unfortunately, we are in a string freeze.
  267. #if 1
  268. (void) mw;
  269. #else
  270. TQApplication::setOverrideCursor (TQt::arrowCursor);
  271. KMessageBox::sorry (mw,
  272. STRING_FREEZE_i18n ("<qt><p>There is nothing in the clipboard to paste.</p></qt>"),
  273. STRING_FREEZE_i18n ("Cannot Paste"));
  274. TQApplication::restoreOverrideCursor ();
  275. #endif
  276. return false;
  277. }
  278. // private slot
  279. void kpMainWindow::slotEnablePaste ()
  280. {
  281. const bool shouldEnable = true;
  282. m_actionPasteInNewWindow->setEnabled (shouldEnable);
  283. m_actionPaste->setEnabled (shouldEnable);
  284. }
  285. #else // No hack
  286. // Call before any paste only.
  287. static bool HasSomethingToPasteWithDialogIfNot (kpMainWindow *)
  288. {
  289. // We will not be called if there's nothing to paste, as the paste
  290. // action would have been disabled. But do _not_ assert that
  291. // (see the slotPaste() "data unexpectedly disappeared" KMessageBox).
  292. return true;
  293. }
  294. // private slot
  295. void kpMainWindow::slotEnablePaste ()
  296. {
  297. const bool shouldEnable = ::HasSomethingToPaste (this);
  298. m_actionPasteInNewWindow->setEnabled (shouldEnable);
  299. m_actionPaste->setEnabled (shouldEnable);
  300. }
  301. #endif
  302. // private
  303. TQRect kpMainWindow::calcUsefulPasteRect (int pixmapWidth, int pixmapHeight)
  304. {
  305. #if DEBUG_KP_MAIN_WINDOW && 1
  306. kdDebug () << "kpMainWindow::calcUsefulPasteRect("
  307. << pixmapWidth << "," << pixmapHeight
  308. << ")"
  309. << endl;
  310. #endif
  311. if (!m_document)
  312. {
  313. kdError () << "kpMainWindow::calcUsefulPasteRect() without doc" << endl;
  314. return TQRect ();
  315. }
  316. // TODO: 1st choice is to paste sel near but not overlapping last deselect point
  317. if (m_mainView && m_scrollView)
  318. {
  319. const TQPoint viewTopLeft (m_scrollView->contentsX (),
  320. m_scrollView->contentsY ());
  321. const TQPoint docTopLeft = m_mainView->transformViewToDoc (viewTopLeft);
  322. if ((docTopLeft.x () + pixmapWidth <= m_document->width () &&
  323. docTopLeft.y () + pixmapHeight <= m_document->height ()) ||
  324. pixmapWidth <= docTopLeft.x () ||
  325. pixmapHeight <= docTopLeft.y ())
  326. {
  327. return TQRect (docTopLeft.x (), docTopLeft.y (),
  328. pixmapWidth, pixmapHeight);
  329. }
  330. }
  331. return TQRect (0, 0, pixmapWidth, pixmapHeight);
  332. }
  333. // private
  334. void kpMainWindow::paste (const kpSelection &sel, bool forceTopLeft)
  335. {
  336. #if DEBUG_KP_MAIN_WINDOW && 1
  337. kdDebug () << "kpMainWindow::paste(forceTopLeft=" << forceTopLeft << ")"
  338. << endl;
  339. #endif
  340. if (!sel.pixmap ())
  341. {
  342. kdError () << "kpMainWindow::paste() with sel without pixmap" << endl;
  343. return;
  344. }
  345. TQApplication::setOverrideCursor (TQt::waitCursor);
  346. if (toolHasBegunShape ())
  347. tool ()->endShapeInternal ();
  348. //
  349. // Make sure we've got a document (esp. with File/Close)
  350. //
  351. if (!m_document)
  352. {
  353. kpDocument *newDoc = new kpDocument (
  354. sel.width (), sel.height (), this);
  355. // will also create viewManager
  356. setDocument (newDoc);
  357. }
  358. //
  359. // Paste as new selection
  360. //
  361. kpSelection selInUsefulPos = sel;
  362. if (!forceTopLeft)
  363. selInUsefulPos.moveTo (calcUsefulPasteRect (sel.width (), sel.height ()).topLeft ());
  364. addDeselectFirstCommand (new kpToolSelectionCreateCommand (
  365. selInUsefulPos.isText () ?
  366. i18n ("Text: Create Box") :
  367. i18n ("Selection: Create"),
  368. selInUsefulPos,
  369. this));
  370. #if DEBUG_KP_MAIN_WINDOW && 1
  371. kdDebug () << "sel.size=" << TQSize (sel.width (), sel.height ())
  372. << " document.size="
  373. << TQSize (m_document->width (), m_document->height ())
  374. << endl;
  375. #endif
  376. // If the selection is bigger than the document, automatically
  377. // resize the document (with the option of Undo'ing) to fit
  378. // the selection.
  379. //
  380. // No annoying dialog necessary.
  381. //
  382. if (sel.width () > m_document->width () ||
  383. sel.height () > m_document->height ())
  384. {
  385. m_commandHistory->addCommand (
  386. new kpToolResizeScaleCommand (
  387. false/*act on doc, not sel*/,
  388. TQMAX (sel.width (), m_document->width ()),
  389. TQMAX (sel.height (), m_document->height ()),
  390. kpToolResizeScaleCommand::Resize,
  391. this));
  392. }
  393. TQApplication::restoreOverrideCursor ();
  394. }
  395. // public
  396. void kpMainWindow::pasteText (const TQString &text,
  397. bool forceNewTextSelection,
  398. const TQPoint &newTextSelectionTopLeft)
  399. {
  400. #if DEBUG_KP_MAIN_WINDOW && 1
  401. kdDebug () << "kpMainWindow::pasteText(" << text
  402. << ",forceNewTextSelection=" << forceNewTextSelection
  403. << ",newTextSelectionTopLeft=" << newTextSelectionTopLeft
  404. << ")" << endl;
  405. #endif
  406. if (text.isEmpty ())
  407. return;
  408. // sync: restoreOverrideCursor() in all exit paths
  409. TQApplication::setOverrideCursor (TQt::waitCursor);
  410. if (toolHasBegunShape ())
  411. tool ()->endShapeInternal ();
  412. TQValueVector <TQString> textLines (1, TQString());
  413. for (int i = 0; i < (int) text.length (); i++)
  414. {
  415. if (text [i] == '\n')
  416. textLines.push_back (TQString());
  417. else
  418. textLines [textLines.size () - 1].append (text [i]);
  419. }
  420. if (!forceNewTextSelection &&
  421. m_document && m_document->selection () &&
  422. m_document->selection ()->isText () &&
  423. m_commandHistory && m_viewManager)
  424. {
  425. #if DEBUG_KP_MAIN_WINDOW && 1
  426. kdDebug () << "\treusing existing Text Selection" << endl;
  427. #endif
  428. kpMacroCommand *macroCmd = new kpMacroCommand (i18n ("Text: Paste"),
  429. this);
  430. for (int i = 0; i < (int) textLines.size (); i++)
  431. {
  432. if (i > 0)
  433. {
  434. macroCmd->addCommand (
  435. new kpToolTextEnterCommand (
  436. TQString()/*uninteresting child of macroCmd*/,
  437. m_viewManager->textCursorRow (),
  438. m_viewManager->textCursorCol (),
  439. this));
  440. }
  441. macroCmd->addCommand (
  442. new kpToolTextInsertCommand (
  443. TQString()/*uninteresting child of macroCmd*/,
  444. m_viewManager->textCursorRow (),
  445. m_viewManager->textCursorCol (),
  446. textLines [i],
  447. this));
  448. }
  449. m_commandHistory->addCommand (macroCmd, false/*no exec*/);
  450. }
  451. else
  452. {
  453. #if DEBUG_KP_MAIN_WINDOW && 1
  454. kdDebug () << "\tcreating Text Selection" << endl;
  455. #endif
  456. const kpTextStyle ts = textStyle ();
  457. const TQFontMetrics fontMetrics = ts.fontMetrics ();
  458. int height = textLines.size () * fontMetrics.height ();
  459. if (textLines.size () >= 1)
  460. height += (textLines.size () - 1) * fontMetrics.leading ();
  461. int width = 0;
  462. for (TQValueVector <TQString>::const_iterator it = textLines.begin ();
  463. it != textLines.end ();
  464. it++)
  465. {
  466. const int w = fontMetrics.width (*it);
  467. if (w > width)
  468. width = w;
  469. }
  470. const int selWidth = TQMAX (kpSelection::minimumWidthForTextStyle (ts),
  471. width + kpSelection::textBorderSize () * 2);
  472. const int selHeight = TQMAX (kpSelection::minimumHeightForTextStyle (ts),
  473. height + kpSelection::textBorderSize () * 2);
  474. kpSelection sel (TQRect (0, 0, selWidth, selHeight),
  475. textLines,
  476. ts);
  477. if (newTextSelectionTopLeft != KP_INVALID_POINT)
  478. {
  479. sel.moveTo (newTextSelectionTopLeft);
  480. paste (sel, true/*force topLeft*/);
  481. }
  482. else
  483. {
  484. paste (sel);
  485. }
  486. }
  487. TQApplication::restoreOverrideCursor ();
  488. }
  489. // public
  490. void kpMainWindow::pasteTextAt (const TQString &text, const TQPoint &point,
  491. bool allowNewTextSelectionPointShift)
  492. {
  493. #if DEBUG_KP_MAIN_WINDOW && 1
  494. kdDebug () << "kpMainWindow::pasteTextAt(" << text
  495. << ",point=" << point
  496. << ",allowNewTextSelectionPointShift="
  497. << allowNewTextSelectionPointShift
  498. << ")" << endl;
  499. #endif
  500. TQApplication::setOverrideCursor (TQt::waitCursor);
  501. if (toolHasBegunShape ())
  502. tool ()->endShapeInternal ();
  503. if (m_document &&
  504. m_document->selection () &&
  505. m_document->selection ()->isText () &&
  506. m_document->selection ()->pointIsInTextArea (point))
  507. {
  508. kpSelection *sel = m_document->selection ();
  509. const int row = sel->textRowForPoint (point);
  510. const int col = sel->textColForPoint (point);
  511. m_viewManager->setTextCursorPosition (row, col);
  512. pasteText (text);
  513. }
  514. else
  515. {
  516. TQPoint pointToUse = point;
  517. if (allowNewTextSelectionPointShift)
  518. {
  519. // TODO: In terms of doc pixels, would be inconsistent behaviour
  520. // based on zoomLevel of view.
  521. // pointToUse -= TQPoint (-view->selectionResizeHandleAtomicSize (),
  522. // -view->selectionResizeHandleAtomicSize ());
  523. }
  524. pasteText (text, true/*force new text selection*/, pointToUse);
  525. }
  526. TQApplication::restoreOverrideCursor ();
  527. }
  528. // public slot
  529. void kpMainWindow::slotPaste ()
  530. {
  531. #if DEBUG_KP_MAIN_WINDOW && 1
  532. kdDebug () << "kpMainWindow::slotPaste() CALLED" << endl;
  533. #endif
  534. // sync: restoreOverrideCursor() in all exit paths
  535. TQApplication::setOverrideCursor (TQt::waitCursor);
  536. if (toolHasBegunShape ())
  537. tool ()->endShapeInternal ();
  538. if (!::HasSomethingToPasteWithDialogIfNot (this))
  539. {
  540. TQApplication::restoreOverrideCursor ();
  541. return;
  542. }
  543. //
  544. // Acquire the pixmap
  545. //
  546. TQMimeSource *ms = TQApplication::clipboard ()->data (TQClipboard::Clipboard);
  547. if (!ms)
  548. {
  549. kdError () << "kpMainWindow::slotPaste() without mimeSource" << endl;
  550. TQApplication::restoreOverrideCursor ();
  551. return;
  552. }
  553. kpSelection sel;
  554. TQString text;
  555. if (kpSelectionDrag::decode (ms, sel/*ref*/, pasteWarnAboutLossInfo ()))
  556. {
  557. sel.setTransparency (selectionTransparency ());
  558. paste (sel);
  559. }
  560. else if (TQTextDrag::decode (ms, text/*ref*/))
  561. {
  562. pasteText (text);
  563. }
  564. else
  565. {
  566. TQApplication::restoreOverrideCursor ();
  567. kdDebug () << "kpMainWindow::slotPaste() could not decode selection" << endl;
  568. kdDebug () << "\tFormats supported:" << endl;
  569. for (int i = 0; ms->format (i); i++)
  570. {
  571. kdDebug () << "\t\t" << i << ":" << ms->format (i) << endl;
  572. }
  573. // TODO: fix Klipper
  574. KMessageBox::sorry (this,
  575. i18n ("<qt><p>KolourPaint cannot paste the contents of"
  576. " the clipboard as the data unexpectedly disappeared.</p>"
  577. "<p>This usually occurs if the application which was"
  578. " responsible"
  579. " for the clipboard contents has been closed.</p></qt>"),
  580. i18n ("Cannot Paste"));
  581. // TODO: PROPAGATE: interprocess
  582. if (TDEMainWindow::memberList)
  583. {
  584. #if DEBUG_KP_MAIN_WINDOW
  585. kdDebug () << "\thave memberList" << endl;
  586. #endif
  587. for (TQPtrList <TDEMainWindow>::const_iterator it = TDEMainWindow::memberList->begin ();
  588. it != TDEMainWindow::memberList->end ();
  589. it++)
  590. {
  591. kpMainWindow *mw = dynamic_cast <kpMainWindow *> (*it);
  592. if (!mw)
  593. {
  594. kdError () << "kpMainWindow::slotPaste() given fake kpMainWindow: " << (*it) << endl;
  595. continue;
  596. }
  597. #if DEBUG_KP_MAIN_WINDOW
  598. kdDebug () << "\t\tmw=" << mw << endl;
  599. #endif
  600. mw->slotEnablePaste ();
  601. }
  602. }
  603. return;
  604. }
  605. TQApplication::restoreOverrideCursor ();
  606. }
  607. // private slot
  608. void kpMainWindow::slotPasteInNewWindow ()
  609. {
  610. #if DEBUG_KP_MAIN_WINDOW && 1
  611. kdDebug () << "kpMainWindow::slotPasteInNewWindow() CALLED" << endl;
  612. #endif
  613. // sync: restoreOverrideCursor() in all exit paths
  614. TQApplication::setOverrideCursor (TQt::waitCursor);
  615. if (toolHasBegunShape ())
  616. tool ()->endShapeInternal ();
  617. if (!::HasSomethingToPasteWithDialogIfNot (this))
  618. {
  619. TQApplication::restoreOverrideCursor ();
  620. return;
  621. }
  622. //
  623. // Pasting must ensure that:
  624. //
  625. // Requirement 1. the document is the same size as the image to be pasted.
  626. // Requirement 2. transparent pixels in the image must remain as transparent.
  627. //
  628. kpMainWindow *win = new kpMainWindow (0/*no document*/);
  629. win->show ();
  630. // Make "Edit / Paste in New Window" always paste white pixels as white.
  631. // Don't let selection transparency get in the way and paste them as
  632. // transparent.
  633. kpSelectionTransparency transparency = win->selectionTransparency ();
  634. if (transparency.isTransparent ())
  635. {
  636. #if DEBUG_KP_MAIN_WINDOW && 1
  637. kdDebug () << "\tchanging selection transparency to opaque" << endl;
  638. #endif
  639. transparency.setOpaque ();
  640. // Since we are setting selection transparency programmatically
  641. // -- as opposed to in response to user input -- this will not
  642. // affect the selection transparency tool option widget's "last used"
  643. // config setting.
  644. win->setSelectionTransparency (transparency);
  645. }
  646. // (this handles Requirement 1. above)
  647. win->slotPaste ();
  648. // (this handles Requirement 2. above;
  649. // slotDeselect() is not enough unless the document is filled with the
  650. // transparent color in advance)
  651. win->slotCrop ();
  652. TQApplication::restoreOverrideCursor ();
  653. }
  654. // public slot
  655. void kpMainWindow::slotDelete ()
  656. {
  657. #if DEBUG_KP_MAIN_WINDOW && 1
  658. kdDebug () << "kpMainWindow::slotDelete() CALLED" << endl;
  659. #endif
  660. if (!m_actionDelete->isEnabled ())
  661. {
  662. #if DEBUG_KP_MAIN_WINDOW && 1
  663. kdDebug () << "\taction not enabled - was probably called from kpTool::keyPressEvent()" << endl;
  664. #endif
  665. return;
  666. }
  667. if (!m_document || !m_document->selection ())
  668. {
  669. kdError () << "kpMainWindow::slotDelete () doc=" << m_document
  670. << " sel=" << (m_document ? m_document->selection () : 0)
  671. << endl;
  672. return;
  673. }
  674. if (toolHasBegunShape ())
  675. tool ()->endShapeInternal ();
  676. addImageOrSelectionCommand (new kpToolSelectionDestroyCommand (
  677. m_document->selection ()->isText () ?
  678. i18n ("Text: Delete Box") : // not to be confused with i18n ("Text: Delete")
  679. i18n ("Selection: Delete"),
  680. false/*no push onto doc*/,
  681. this));
  682. }
  683. // private slot
  684. void kpMainWindow::slotSelectAll ()
  685. {
  686. #if DEBUG_KP_MAIN_WINDOW && 1
  687. kdDebug () << "kpMainWindow::slotSelectAll() CALLED" << endl;
  688. #endif
  689. if (!m_document)
  690. {
  691. kdError () << "kpMainWindow::slotSelectAll() without doc" << endl;
  692. return;
  693. }
  694. if (toolHasBegunShape ())
  695. tool ()->endShapeInternal ();
  696. if (m_document->selection ())
  697. slotDeselect ();
  698. // just the border - don't actually pull pixmap from doc yet
  699. m_document->setSelection (kpSelection (kpSelection::Rectangle, m_document->rect (), selectionTransparency ()));
  700. if (tool ())
  701. tool ()->somethingBelowTheCursorChanged ();
  702. }
  703. // private
  704. void kpMainWindow::addDeselectFirstCommand (kpCommand *cmd)
  705. {
  706. #if DEBUG_KP_MAIN_WINDOW && 1
  707. kdDebug () << "kpMainWindow::addDeselectFirstCommand("
  708. << cmd
  709. << ")"
  710. << endl;
  711. #endif
  712. kpSelection *sel = m_document->selection ();
  713. #if DEBUG_KP_MAIN_WINDOW && 1
  714. kdDebug () << "\tsel=" << sel << endl;
  715. #endif
  716. if (sel)
  717. {
  718. // if you just dragged out something with no action then
  719. // forget the drag
  720. if (!sel->pixmap ())
  721. {
  722. #if DEBUG_KP_MAIN_WINDOW && 1
  723. kdDebug () << "\tjust a fresh border - was nop - delete" << endl;
  724. #endif
  725. m_document->selectionDelete ();
  726. if (tool ())
  727. tool ()->somethingBelowTheCursorChanged ();
  728. if (cmd)
  729. m_commandHistory->addCommand (cmd);
  730. }
  731. else
  732. {
  733. #if DEBUG_KP_MAIN_WINDOW && 1
  734. kdDebug () << "\treal selection with pixmap - push onto doc cmd" << endl;
  735. #endif
  736. kpCommand *deselectCommand = new kpToolSelectionDestroyCommand (
  737. sel->isText () ?
  738. i18n ("Text: Finish") :
  739. i18n ("Selection: Deselect"),
  740. true/*push onto document*/,
  741. this);
  742. if (cmd)
  743. {
  744. kpMacroCommand *macroCmd = new kpMacroCommand (cmd->name (), this);
  745. macroCmd->addCommand (deselectCommand);
  746. macroCmd->addCommand (cmd);
  747. m_commandHistory->addCommand (macroCmd);
  748. }
  749. else
  750. m_commandHistory->addCommand (deselectCommand);
  751. }
  752. }
  753. else
  754. {
  755. if (cmd)
  756. m_commandHistory->addCommand (cmd);
  757. }
  758. }
  759. // public slot
  760. void kpMainWindow::slotDeselect ()
  761. {
  762. #if DEBUG_KP_MAIN_WINDOW && 1
  763. kdDebug () << "kpMainWindow::slotDeselect() CALLED" << endl;
  764. #endif
  765. if (!m_document || !m_document->selection ())
  766. {
  767. kdError () << "kpMainWindow::slotDeselect() doc=" << m_document
  768. << " sel=" << (m_document ? m_document->selection () : 0)
  769. << endl;
  770. return;
  771. }
  772. if (toolHasBegunShape ())
  773. tool ()->endShapeInternal ();
  774. addDeselectFirstCommand (0);
  775. }
  776. // private slot
  777. void kpMainWindow::slotCopyToFile ()
  778. {
  779. #if DEBUG_KP_MAIN_WINDOW
  780. kdDebug () << "kpMainWindow::slotCopyToFile()" << endl;
  781. #endif
  782. if (toolHasBegunShape ())
  783. tool ()->endShapeInternal ();
  784. if (!m_document->selection ())
  785. return;
  786. kpSelection sel = *m_document->selection ();
  787. TQPixmap pixmapToSave;
  788. if (!sel.pixmap ())
  789. {
  790. // Not a floating selection - user has just selected a region;
  791. // haven't pulled it off yet so probably don't expect and can't
  792. // visualise selection transparency so give opaque, not transparent
  793. // pixmap.
  794. pixmapToSave = m_document->getSelectedPixmap ();
  795. }
  796. else
  797. pixmapToSave = sel.transparentPixmap ();
  798. kpDocumentSaveOptions chosenSaveOptions;
  799. bool allowOverwritePrompt, allowLossyPrompt;
  800. KURL chosenURL = askForSaveURL (i18n ("Copy to File"),
  801. m_lastCopyToURL.url (),
  802. pixmapToSave,
  803. m_lastCopyToSaveOptions,
  804. kpDocumentMetaInfo (),
  805. kpSettingsGroupEditCopyTo,
  806. false/*allow remote files*/,
  807. &chosenSaveOptions,
  808. m_copyToFirstTime,
  809. &allowOverwritePrompt,
  810. &allowLossyPrompt);
  811. if (chosenURL.isEmpty ())
  812. return;
  813. if (!kpDocument::savePixmapToFile (pixmapToSave,
  814. chosenURL,
  815. chosenSaveOptions, kpDocumentMetaInfo (),
  816. allowOverwritePrompt,
  817. allowLossyPrompt,
  818. this))
  819. {
  820. return;
  821. }
  822. addRecentURL (chosenURL);
  823. m_lastCopyToURL = chosenURL;
  824. m_lastCopyToSaveOptions = chosenSaveOptions;
  825. m_copyToFirstTime = false;
  826. }
  827. // private slot
  828. void kpMainWindow::slotPasteFromFile ()
  829. {
  830. #if DEBUG_KP_MAIN_WINDOW
  831. kdDebug () << "kpMainWindow::slotPasteFromFile()" << endl;
  832. #endif
  833. if (toolHasBegunShape ())
  834. tool ()->endShapeInternal ();
  835. KURL::List urls = askForOpenURLs (i18n ("Paste From File"),
  836. m_lastPasteFromURL.url (),
  837. false/*only 1 URL*/);
  838. if (urls.count () != 1)
  839. return;
  840. KURL url = urls.first ();
  841. m_lastPasteFromURL = url;
  842. TQPixmap pixmap = kpDocument::getPixmapFromFile (url,
  843. false/*show error message if doesn't exist*/,
  844. this);
  845. if (pixmap.isNull ())
  846. return;
  847. addRecentURL (url);
  848. paste (kpSelection (kpSelection::Rectangle,
  849. TQRect (0, 0, pixmap.width (), pixmap.height ()),
  850. pixmap,
  851. selectionTransparency ()));
  852. }