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.

370 lines
11KB

  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_EFFECTS_DIALOG 0
  24. #include <kpeffectsdialog.h>
  25. #include <tqgroupbox.h>
  26. #include <tqhbox.h>
  27. #include <tqlabel.h>
  28. #include <tqlayout.h>
  29. #include <tqtimer.h>
  30. #include <kapplication.h>
  31. #include <kcombobox.h>
  32. #include <tdeconfig.h>
  33. #include <kdebug.h>
  34. #include <klocale.h>
  35. #include <kpdefs.h>
  36. #include <kpdocument.h>
  37. #include <kpeffectbalance.h>
  38. #include <kpeffectblursharpen.h>
  39. #include <kpeffectemboss.h>
  40. #include <kpeffectflatten.h>
  41. #include <kpeffectinvert.h>
  42. #include <kpeffectreducecolors.h>
  43. #include <kppixmapfx.h>
  44. // protected static
  45. int kpEffectsDialog::s_lastWidth = 640;
  46. int kpEffectsDialog::s_lastHeight = 620;
  47. kpEffectsDialog::kpEffectsDialog (bool actOnSelection,
  48. kpMainWindow *parent,
  49. const char *name)
  50. : kpToolPreviewDialog (kpToolPreviewDialog::Preview,
  51. true/*reserve top row*/,
  52. TQString()/*caption*/,
  53. TQString()/*afterActionText (no Dimensions Group Box)*/,
  54. actOnSelection,
  55. parent,
  56. name),
  57. m_delayedUpdateTimer (new TQTimer (this)),
  58. m_effectsComboBox (0),
  59. m_settingsGroupBox (0),
  60. m_settingsLayout (0),
  61. m_colorEffectWidget (0)
  62. {
  63. #if DEBUG_KP_EFFECTS_DIALOG
  64. kdDebug () << "kpEffectsDialog::kpEffectsDialog()" << endl;
  65. #endif
  66. if (actOnSelection)
  67. setCaption (i18n ("More Image Effects (Selection)"));
  68. else
  69. setCaption (i18n ("More Image Effects"));
  70. connect (m_delayedUpdateTimer, TQT_SIGNAL (timeout ()),
  71. this, TQT_SLOT (slotUpdateWithWaitCursor ()));
  72. TQHBox *effectContainer = new TQHBox (mainWidget ());
  73. effectContainer->setSpacing (spacingHint () * 4
  74. /*need more space for TQGroupBox titles*/);
  75. effectContainer->setMargin (0);
  76. TQLabel *label = new TQLabel (i18n ("&Effect:"), effectContainer);
  77. m_effectsComboBox = new KComboBox (effectContainer);
  78. m_effectsComboBox->insertItem (i18n ("Balance"));
  79. m_effectsComboBox->insertItem (i18n ("Emboss"));
  80. m_effectsComboBox->insertItem (i18n ("Flatten"));
  81. m_effectsComboBox->insertItem (i18n ("Invert"));
  82. m_effectsComboBox->insertItem (i18n ("Reduce Colors"));
  83. m_effectsComboBox->insertItem (i18n ("Soften & Sharpen"));
  84. label->setBuddy (m_effectsComboBox);
  85. effectContainer->setStretchFactor (m_effectsComboBox, 1);
  86. addCustomWidgetToFront (effectContainer);
  87. m_settingsGroupBox = new TQGroupBox (mainWidget ());
  88. m_settingsLayout = new TQVBoxLayout (m_settingsGroupBox,
  89. marginHint () * 2,
  90. spacingHint ());
  91. addCustomWidgetToBack (m_settingsGroupBox);
  92. connect (m_effectsComboBox, TQT_SIGNAL (activated (int)),
  93. this, TQT_SLOT (selectEffect (int)));
  94. selectEffect (0);
  95. resize (s_lastWidth, s_lastHeight);
  96. #if DEBUG_KP_EFFECTS_DIALOG
  97. kdDebug () << "\tabout to slotUpdate()" << endl;
  98. #endif
  99. slotUpdate ();
  100. }
  101. kpEffectsDialog::~kpEffectsDialog ()
  102. {
  103. s_lastWidth = width ();
  104. s_lastHeight = height ();
  105. }
  106. // public virtual [base kpToolPreviewDialog]
  107. bool kpEffectsDialog::isNoOp () const
  108. {
  109. if (!m_colorEffectWidget)
  110. return true;
  111. return m_colorEffectWidget->isNoOp ();
  112. }
  113. // public
  114. kpColorEffectCommand *kpEffectsDialog::createCommand () const
  115. {
  116. if (!m_colorEffectWidget)
  117. return 0;
  118. return m_colorEffectWidget->createCommand ();
  119. }
  120. // protected virtual [base kpToolPreviewDialog]
  121. TQSize kpEffectsDialog::newDimensions () const
  122. {
  123. kpDocument *doc = document ();
  124. if (!doc)
  125. return TQSize ();
  126. return TQSize (doc->width (m_actOnSelection),
  127. doc->height (m_actOnSelection));
  128. }
  129. // protected virtual [base kpToolPreviewDialog]
  130. TQPixmap kpEffectsDialog::transformPixmap (const TQPixmap &pixmap,
  131. int targetWidth, int targetHeight) const
  132. {
  133. TQPixmap pixmapWithEffect;
  134. if (m_colorEffectWidget)
  135. pixmapWithEffect = m_colorEffectWidget->applyColorEffect (pixmap);
  136. else
  137. pixmapWithEffect = pixmap;
  138. return kpPixmapFX::scale (pixmapWithEffect, targetWidth, targetHeight);
  139. }
  140. // public
  141. int kpEffectsDialog::selectedEffect () const
  142. {
  143. return m_effectsComboBox->currentItem ();
  144. }
  145. // public slot
  146. void kpEffectsDialog::selectEffect (int which)
  147. {
  148. #if DEBUG_KP_EFFECTS_DIALOG
  149. kdDebug () << "kpEffectsDialog::selectEffect(" << which << ")" << endl;
  150. #endif
  151. if (which < 0 ||
  152. which >= m_effectsComboBox->count ())
  153. {
  154. return;
  155. }
  156. if (which != m_effectsComboBox->currentItem ())
  157. m_effectsComboBox->setCurrentItem (which);
  158. delete m_colorEffectWidget;
  159. m_colorEffectWidget = 0;
  160. m_settingsGroupBox->setCaption (TQString());
  161. #define CREATE_EFFECT_WIDGET(name) \
  162. m_colorEffectWidget = new name (m_actOnSelection, \
  163. m_mainWindow, \
  164. m_settingsGroupBox)
  165. switch (which)
  166. {
  167. case 0:
  168. CREATE_EFFECT_WIDGET (kpEffectBalanceWidget);
  169. break;
  170. case 1:
  171. CREATE_EFFECT_WIDGET (kpEffectEmbossWidget);
  172. break;
  173. case 2:
  174. CREATE_EFFECT_WIDGET (kpEffectFlattenWidget);
  175. break;
  176. case 3:
  177. CREATE_EFFECT_WIDGET (kpEffectInvertWidget);
  178. break;
  179. case 4:
  180. CREATE_EFFECT_WIDGET (kpEffectReduceColorsWidget);
  181. break;
  182. case 5:
  183. CREATE_EFFECT_WIDGET (kpEffectBlurSharpenWidget);
  184. break;
  185. }
  186. #undef CREATE_EFFECT_WIDGET
  187. if (m_colorEffectWidget)
  188. {
  189. #if DEBUG_KP_EFFECTS_DIALOG
  190. kdDebug () << "\twidget exists for effect #" << endl;
  191. #endif
  192. m_settingsGroupBox->setTitle (m_colorEffectWidget->caption ());
  193. // Don't resize the preview when showing the widget:
  194. // TODO: actually work
  195. TQSize previewGroupBoxMinSize = m_previewGroupBox->minimumSize ();
  196. TQSize previewGroupBoxMaxSize = m_previewGroupBox->maximumSize ();
  197. TQLayout::ResizeMode previewGroupBoxResizeMode =
  198. m_previewGroupBox->layout () ?
  199. m_previewGroupBox->layout ()->resizeMode () :
  200. TQLayout::Auto;
  201. #if DEBUG_KP_EFFECTS_DIALOG
  202. kdDebug () << "\tpreviewGroupBox: minSize=" << previewGroupBoxMinSize
  203. << " maxSize=" << previewGroupBoxMaxSize
  204. << " size=" << m_previewGroupBox->size ()
  205. << " layout=" << m_previewGroupBox->layout ()
  206. << " resizeMode=" << previewGroupBoxResizeMode
  207. << endl;
  208. #endif
  209. if (m_previewGroupBox->layout ())
  210. m_previewGroupBox->layout ()->setResizeMode (TQLayout::FreeResize);
  211. #if DEBUG_KP_EFFECTS_DIALOG
  212. kdDebug () << "\tafter set resizeMode, previewGroupBox.size="
  213. << m_previewGroupBox->size () << endl;
  214. #endif
  215. m_previewGroupBox->setFixedSize (m_previewGroupBox->size ());
  216. #if DEBUG_KP_EFFECTS_DIALOG
  217. kdDebug () << "\tafter set fixedSize, previewGroupBox.size="
  218. << m_previewGroupBox->size () << endl;
  219. #endif
  220. // Show widget
  221. m_settingsLayout->addWidget (m_colorEffectWidget);
  222. #if DEBUG_KP_EFFECTS_DIALOG
  223. kdDebug () << "\tafter addWidget, previewGroupBox.size="
  224. << m_previewGroupBox->size () << endl;
  225. #endif
  226. m_colorEffectWidget->show ();
  227. #if DEBUG_KP_EFFECTS_DIALOG
  228. kdDebug () << "\tafter addWidget show, previewGroupBox.size="
  229. << m_previewGroupBox->size () << endl;
  230. #endif
  231. m_previewGroupBox->setMinimumSize (previewGroupBoxMinSize);
  232. m_previewGroupBox->setMaximumSize (previewGroupBoxMaxSize);
  233. #if DEBUG_KP_EFFECTS_DIALOG
  234. kdDebug () << "\tafter set fixedSize, previewGroupBox.size="
  235. << m_previewGroupBox->size () << endl;
  236. #endif
  237. if (m_previewGroupBox->layout ())
  238. m_previewGroupBox->layout ()->setResizeMode (previewGroupBoxResizeMode);
  239. #if DEBUG_KP_EFFECTS_DIALOG
  240. kdDebug () << "\tafter restore resizeMode, previewGroupBox.size="
  241. << m_previewGroupBox->size () << endl;
  242. #endif
  243. connect (m_colorEffectWidget, TQT_SIGNAL (settingsChangedNoWaitCursor ()),
  244. this, TQT_SLOT (slotUpdate ()));
  245. connect (m_colorEffectWidget, TQT_SIGNAL (settingsChanged ()),
  246. this, TQT_SLOT (slotUpdateWithWaitCursor ()));
  247. connect (m_colorEffectWidget, TQT_SIGNAL (settingsChangedDelayed ()),
  248. this, TQT_SLOT (slotDelayedUpdate ()));
  249. slotUpdateWithWaitCursor ();
  250. #if DEBUG_KP_EFFECTS_DIALOG
  251. kdDebug () << "\tafter slotUpdateWithWaitCursor, previewGroupBox.size="
  252. << m_previewGroupBox->size () << endl;
  253. #endif
  254. }
  255. }
  256. // protected slot virtual [base kpToolPreviewDialog]
  257. void kpEffectsDialog::slotUpdate ()
  258. {
  259. #if DEBUG_KP_EFFECTS_DIALOG
  260. kdDebug () << "kpEffectsDialog::slotUpdate()"
  261. << " timerActive=" << m_delayedUpdateTimer->isActive ()
  262. << endl;
  263. #endif
  264. m_delayedUpdateTimer->stop ();
  265. kpToolPreviewDialog::slotUpdate ();
  266. }
  267. // protected slot virtual [base kpToolPreviewDialog]
  268. void kpEffectsDialog::slotUpdateWithWaitCursor ()
  269. {
  270. #if DEBUG_KP_EFFECTS_DIALOG
  271. kdDebug () << "kpEffectsDialog::slotUpdateWithWaitCursor()"
  272. << " timerActive=" << m_delayedUpdateTimer->isActive ()
  273. << endl;
  274. #endif
  275. m_delayedUpdateTimer->stop ();
  276. kpToolPreviewDialog::slotUpdateWithWaitCursor ();
  277. }
  278. // protected slot
  279. void kpEffectsDialog::slotDelayedUpdate ()
  280. {
  281. #if DEBUG_KP_EFFECTS_DIALOG
  282. kdDebug () << "kpEffectsDialog::slotDelayedUpdate()"
  283. << " timerActive=" << m_delayedUpdateTimer->isActive ()
  284. << endl;
  285. #endif
  286. m_delayedUpdateTimer->stop ();
  287. m_delayedUpdateTimer->start (400/*ms*/, true/*single shot*/);
  288. }
  289. #include <kpeffectsdialog.moc>