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.

kptoolskew.cpp 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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_SKEW 0
  24. #define DEBUG_KP_TOOL_SKEW_DIALOG 0
  25. #include <kptoolskew.h>
  26. #include <tqapplication.h>
  27. #include <tqgroupbox.h>
  28. #include <tqlabel.h>
  29. #include <tqlayout.h>
  30. #include <tqpushbutton.h>
  31. #include <tqwmatrix.h>
  32. #include <kdebug.h>
  33. #include <kiconloader.h>
  34. #include <tdelocale.h>
  35. #include <knuminput.h>
  36. #include <kpdefs.h>
  37. #include <kpdocument.h>
  38. #include <kpmainwindow.h>
  39. #include <kppixmapfx.h>
  40. #include <kpselection.h>
  41. #include <kptool.h>
  42. /*
  43. * kpToolSkewCommand
  44. */
  45. kpToolSkewCommand::kpToolSkewCommand (bool actOnSelection,
  46. int hangle, int vangle,
  47. kpMainWindow *mainWindow)
  48. : kpCommand (mainWindow),
  49. m_actOnSelection (actOnSelection),
  50. m_hangle (hangle), m_vangle (vangle),
  51. m_backgroundColor (mainWindow ? mainWindow->backgroundColor (actOnSelection) : kpColor::invalid),
  52. m_oldPixmapPtr (0)
  53. {
  54. }
  55. kpToolSkewCommand::~kpToolSkewCommand ()
  56. {
  57. delete m_oldPixmapPtr;
  58. }
  59. // public virtual [base kpCommand]
  60. TQString kpToolSkewCommand::name () const
  61. {
  62. TQString opName = i18n ("Skew");
  63. if (m_actOnSelection)
  64. return i18n ("Selection: %1").arg (opName);
  65. else
  66. return opName;
  67. }
  68. // public virtual [base kpCommand]
  69. int kpToolSkewCommand::size () const
  70. {
  71. return kpPixmapFX::pixmapSize (m_oldPixmapPtr) +
  72. m_oldSelection.size ();
  73. }
  74. // public virtual [base kpCommand]
  75. void kpToolSkewCommand::execute ()
  76. {
  77. kpDocument *doc = document ();
  78. if (!doc)
  79. return;
  80. TQApplication::setOverrideCursor (TQt::waitCursor);
  81. m_oldPixmapPtr = new TQPixmap ();
  82. *m_oldPixmapPtr = *doc->pixmap (m_actOnSelection);
  83. TQPixmap newPixmap = kpPixmapFX::skew (*doc->pixmap (m_actOnSelection),
  84. kpToolSkewDialog::horizontalAngleForPixmapFX (m_hangle),
  85. kpToolSkewDialog::verticalAngleForPixmapFX (m_vangle),
  86. m_backgroundColor);
  87. if (m_actOnSelection)
  88. {
  89. kpSelection *sel = doc->selection ();
  90. // Save old selection
  91. m_oldSelection = *sel;
  92. // Calculate skewed points
  93. TQPointArray currentPoints = sel->points ();
  94. currentPoints.translate (-currentPoints.boundingRect ().x (),
  95. -currentPoints.boundingRect ().y ());
  96. TQWMatrix skewMatrix = kpPixmapFX::skewMatrix (
  97. *doc->pixmap (m_actOnSelection),
  98. kpToolSkewDialog::horizontalAngleForPixmapFX (m_hangle),
  99. kpToolSkewDialog::verticalAngleForPixmapFX (m_vangle));
  100. currentPoints = skewMatrix.map (currentPoints);
  101. currentPoints.translate (-currentPoints.boundingRect ().x () + m_oldSelection.x (),
  102. -currentPoints.boundingRect ().y () + m_oldSelection.y ());
  103. if (currentPoints.boundingRect ().width () == newPixmap.width () &&
  104. currentPoints.boundingRect ().height () == newPixmap.height ())
  105. {
  106. doc->setSelection (kpSelection (currentPoints, newPixmap,
  107. m_oldSelection.transparency ()));
  108. }
  109. else
  110. {
  111. // TODO: fix the latter "victim of" problem in kpSelection by
  112. // allowing the border width & height != pixmap width & height
  113. // Or maybe autocrop?
  114. #if DEBUG_KP_TOOL_SKEW
  115. kdDebug () << "kpToolSkewCommand::execute() currentPoints.boundingRect="
  116. << currentPoints.boundingRect ()
  117. << " newPixmap: w=" << newPixmap.width ()
  118. << " h=" << newPixmap.height ()
  119. << " (victim of rounding error and/or skewed-a-(rectangular)-pixmap-that-was-transparent-in-the-corners-making-sel-uselessly-bigger-than-needs-be))"
  120. << endl;
  121. #endif
  122. doc->setSelection (kpSelection (kpSelection::Rectangle,
  123. TQRect (currentPoints.boundingRect ().x (),
  124. currentPoints.boundingRect ().y (),
  125. newPixmap.width (),
  126. newPixmap.height ()),
  127. newPixmap,
  128. m_oldSelection.transparency ()));
  129. }
  130. if (m_mainWindow->tool ())
  131. m_mainWindow->tool ()->somethingBelowTheCursorChanged ();
  132. }
  133. else
  134. {
  135. doc->setPixmap (newPixmap);
  136. }
  137. TQApplication::restoreOverrideCursor ();
  138. }
  139. // public virtual [base kpCommand]
  140. void kpToolSkewCommand::unexecute ()
  141. {
  142. kpDocument *doc = document ();
  143. if (!doc)
  144. return;
  145. TQApplication::setOverrideCursor (TQt::waitCursor);
  146. TQPixmap oldPixmap = *m_oldPixmapPtr;
  147. delete m_oldPixmapPtr; m_oldPixmapPtr = 0;
  148. if (!m_actOnSelection)
  149. doc->setPixmap (oldPixmap);
  150. else
  151. {
  152. kpSelection oldSelection = m_oldSelection;
  153. doc->setSelection (oldSelection);
  154. if (m_mainWindow->tool ())
  155. m_mainWindow->tool ()->somethingBelowTheCursorChanged ();
  156. }
  157. TQApplication::restoreOverrideCursor ();
  158. }
  159. /*
  160. * kpToolSkewDialog
  161. */
  162. // private static
  163. int kpToolSkewDialog::s_lastWidth = -1,
  164. kpToolSkewDialog::s_lastHeight = -1;
  165. // private static
  166. int kpToolSkewDialog::s_lastHorizontalAngle = 0,
  167. kpToolSkewDialog::s_lastVerticalAngle = 0;
  168. kpToolSkewDialog::kpToolSkewDialog (bool actOnSelection, kpMainWindow *parent,
  169. const char *name)
  170. : kpToolPreviewDialog (kpToolPreviewDialog::AllFeatures,
  171. false/*don't reserve top row*/,
  172. actOnSelection ? i18n ("Skew Selection") : i18n ("Skew Image"),
  173. i18n ("After Skew:"),
  174. actOnSelection, parent, name)
  175. {
  176. // Too confusing - disable for now
  177. s_lastHorizontalAngle = s_lastVerticalAngle = 0;
  178. createAngleGroupBox ();
  179. if (s_lastWidth > 0 && s_lastHeight > 0)
  180. resize (s_lastWidth, s_lastHeight);
  181. slotUpdate ();
  182. m_horizontalSkewInput->setEditFocus ();
  183. }
  184. kpToolSkewDialog::~kpToolSkewDialog ()
  185. {
  186. s_lastWidth = width (), s_lastHeight = height ();
  187. }
  188. // private
  189. void kpToolSkewDialog::createAngleGroupBox ()
  190. {
  191. TQGroupBox *angleGroupBox = new TQGroupBox (i18n ("Angle"), mainWidget ());
  192. addCustomWidget (angleGroupBox);
  193. TQLabel *horizontalSkewPixmapLabel = new TQLabel (angleGroupBox);
  194. horizontalSkewPixmapLabel->setPixmap (UserIcon ("image_skew_horizontal"));
  195. TQLabel *horizontalSkewLabel = new TQLabel (i18n ("&Horizontal:"), angleGroupBox);
  196. m_horizontalSkewInput = new KIntNumInput (s_lastHorizontalAngle, angleGroupBox);
  197. m_horizontalSkewInput->setMinValue (-89);
  198. m_horizontalSkewInput->setMaxValue (+89);
  199. TQLabel *horizontalSkewDegreesLabel = new TQLabel (i18n ("degrees"), angleGroupBox);
  200. TQLabel *verticalSkewPixmapLabel = new TQLabel (angleGroupBox);
  201. verticalSkewPixmapLabel->setPixmap (UserIcon ("image_skew_vertical"));
  202. TQLabel *verticalSkewLabel = new TQLabel (i18n ("&Vertical:"), angleGroupBox);
  203. m_verticalSkewInput = new KIntNumInput (s_lastVerticalAngle, angleGroupBox);
  204. m_verticalSkewInput->setMinValue (-89);
  205. m_verticalSkewInput->setMaxValue (+89);
  206. TQLabel *verticalSkewDegreesLabel = new TQLabel (i18n ("degrees"), angleGroupBox);
  207. horizontalSkewLabel->setBuddy (m_horizontalSkewInput);
  208. verticalSkewLabel->setBuddy (m_verticalSkewInput);
  209. TQGridLayout *angleLayout = new TQGridLayout (angleGroupBox, 4, 4,
  210. marginHint () * 2, spacingHint ());
  211. angleLayout->addWidget (horizontalSkewPixmapLabel, 0, 0);
  212. angleLayout->addWidget (horizontalSkewLabel, 0, 1);
  213. angleLayout->addWidget (m_horizontalSkewInput, 0, 2);
  214. angleLayout->addWidget (horizontalSkewDegreesLabel, 0, 3);
  215. angleLayout->addWidget (verticalSkewPixmapLabel, 1, 0);
  216. angleLayout->addWidget (verticalSkewLabel, 1, 1);
  217. angleLayout->addWidget (m_verticalSkewInput, 1, 2);
  218. angleLayout->addWidget (verticalSkewDegreesLabel, 1, 3);
  219. connect (m_horizontalSkewInput, TQT_SIGNAL (valueChanged (int)),
  220. this, TQT_SLOT (slotUpdate ()));
  221. connect (m_verticalSkewInput, TQT_SIGNAL (valueChanged (int)),
  222. this, TQT_SLOT (slotUpdate ()));
  223. }
  224. // private virtual [base kpToolPreviewDialog]
  225. TQSize kpToolSkewDialog::newDimensions () const
  226. {
  227. kpDocument *doc = document ();
  228. if (!doc)
  229. return TQSize ();
  230. TQWMatrix skewMatrix = kpPixmapFX::skewMatrix (*doc->pixmap (),
  231. horizontalAngleForPixmapFX (),
  232. verticalAngleForPixmapFX ());
  233. // TODO: Should we be using TQWMatrix::Areas?
  234. TQRect skewRect = skewMatrix.mapRect (doc->rect (m_actOnSelection));
  235. return TQSize (skewRect.width (), skewRect.height ());
  236. }
  237. // private virtual [base kpToolPreviewDialog]
  238. TQPixmap kpToolSkewDialog::transformPixmap (const TQPixmap &pixmap,
  239. int targetWidth, int targetHeight) const
  240. {
  241. return kpPixmapFX::skew (pixmap,
  242. horizontalAngleForPixmapFX (),
  243. verticalAngleForPixmapFX (),
  244. m_mainWindow ? m_mainWindow->backgroundColor (m_actOnSelection) : kpColor::invalid,
  245. targetWidth,
  246. targetHeight);
  247. }
  248. // private
  249. void kpToolSkewDialog::updateLastAngles ()
  250. {
  251. s_lastHorizontalAngle = horizontalAngle ();
  252. s_lastVerticalAngle = verticalAngle ();
  253. }
  254. // private slot virtual [base kpToolPreviewDialog]
  255. void kpToolSkewDialog::slotUpdate ()
  256. {
  257. updateLastAngles ();
  258. kpToolPreviewDialog::slotUpdate ();
  259. }
  260. // public
  261. int kpToolSkewDialog::horizontalAngle () const
  262. {
  263. return m_horizontalSkewInput->value ();
  264. }
  265. // public
  266. int kpToolSkewDialog::verticalAngle () const
  267. {
  268. return m_verticalSkewInput->value ();
  269. }
  270. // public static
  271. int kpToolSkewDialog::horizontalAngleForPixmapFX (int hangle)
  272. {
  273. return -hangle;
  274. }
  275. // public static
  276. int kpToolSkewDialog::verticalAngleForPixmapFX (int vangle)
  277. {
  278. return -vangle;
  279. }
  280. // public
  281. int kpToolSkewDialog::horizontalAngleForPixmapFX () const
  282. {
  283. return kpToolSkewDialog::horizontalAngleForPixmapFX (horizontalAngle ());
  284. }
  285. // public
  286. int kpToolSkewDialog::verticalAngleForPixmapFX () const
  287. {
  288. return kpToolSkewDialog::verticalAngleForPixmapFX (verticalAngle ());
  289. }
  290. // public virtual [base kpToolPreviewDialog]
  291. bool kpToolSkewDialog::isNoOp () const
  292. {
  293. return (horizontalAngle () == 0) && (verticalAngle () == 0);
  294. }
  295. // private slot virtual [base KDialogBase]
  296. void kpToolSkewDialog::slotOk ()
  297. {
  298. TQString message, caption, continueButtonText;
  299. if (document ()->selection ())
  300. {
  301. if (!document ()->selection ()->isText ())
  302. {
  303. message =
  304. i18n ("<qt><p>Skewing the selection to %1x%2"
  305. " may take a substantial amount of memory."
  306. " This can reduce system"
  307. " responsiveness and cause other application resource"
  308. " problems.</p>"
  309. "<p>Are you sure want to skew the selection?</p></qt>");
  310. caption = i18n ("Skew Selection?");
  311. continueButtonText = i18n ("Sk&ew Selection");
  312. }
  313. }
  314. else
  315. {
  316. message =
  317. i18n ("<qt><p>Skewing the image to %1x%2"
  318. " may take a substantial amount of memory."
  319. " This can reduce system"
  320. " responsiveness and cause other application resource"
  321. " problems.</p>"
  322. "<p>Are you sure want to skew the image?</p></qt>");
  323. caption = i18n ("Skew Image?");
  324. continueButtonText = i18n ("Sk&ew Image");
  325. }
  326. const int newWidth = newDimensions ().width ();
  327. const int newHeight = newDimensions ().height ();
  328. if (kpTool::warnIfBigImageSize (m_oldWidth,
  329. m_oldHeight,
  330. newWidth, newHeight,
  331. message.arg (newWidth).arg (newHeight),
  332. caption,
  333. continueButtonText,
  334. this))
  335. {
  336. KDialogBase::slotOk ();
  337. }
  338. }
  339. #include <kptoolskew.moc>