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_text.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 <tdeactionclasses.h>
  25. #include <tdeapplication.h>
  26. #include <tdeconfig.h>
  27. #include <kdebug.h>
  28. #include <tdelocale.h>
  29. #include <kpcolortoolbar.h>
  30. #include <kpdefs.h>
  31. #include <kptextstyle.h>
  32. #include <kptooltext.h>
  33. #include <kptooltoolbar.h>
  34. #include <kptoolwidgetopaqueortransparent.h>
  35. #include <kpzoomedview.h>
  36. // private
  37. void kpMainWindow::setupTextToolBarActions ()
  38. {
  39. TDEActionCollection *ac = actionCollection ();
  40. m_actionTextFontFamily = new TDEFontAction (i18n ("Font Family"), 0/*shortcut*/,
  41. TQT_TQOBJECT(this), TQT_SLOT (slotTextFontFamilyChanged ()), ac, "text_font_family");
  42. m_actionTextFontSize = new TDEFontSizeAction (i18n ("Font Size"), 0/*shortcut*/,
  43. TQT_TQOBJECT(this), TQT_SLOT (slotTextFontSizeChanged ()), ac, "text_font_size");
  44. m_actionTextBold = new TDEToggleAction (i18n ("Bold"),
  45. "format-text-bold"/*icon*/, 0/*shortcut*/,
  46. TQT_TQOBJECT(this), TQT_SLOT (slotTextBoldChanged ()), ac, "format-text-bold");
  47. m_actionTextItalic = new TDEToggleAction (i18n ("Italic"),
  48. "format-text-italic"/*icon*/, 0/*shortcut*/,
  49. TQT_TQOBJECT(this), TQT_SLOT (slotTextItalicChanged ()), ac, "format-text-italic");
  50. m_actionTextUnderline = new TDEToggleAction (i18n ("Underline"),
  51. "format-text-underline"/*icon*/, 0/*shortcut*/,
  52. TQT_TQOBJECT(this), TQT_SLOT (slotTextUnderlineChanged ()), ac, "text_underline");
  53. m_actionTextStrikeThru = new TDEToggleAction (i18n ("Strike Through"),
  54. "format-text-strikethrough"/*icon*/, 0/*shortcut*/,
  55. TQT_TQOBJECT(this), TQT_SLOT (slotTextStrikeThruChanged ()), ac, "text_strike_thru");
  56. readAndApplyTextSettings ();
  57. enableTextToolBarActions (false);
  58. }
  59. // private
  60. void kpMainWindow::readAndApplyTextSettings ()
  61. {
  62. TDEConfigGroupSaver cfgGroupSaver (kapp->config (), kpSettingsGroupText);
  63. TDEConfigBase *cfg = cfgGroupSaver.config ();
  64. m_actionTextFontFamily->setFont (cfg->readEntry (kpSettingFontFamily, TQString::fromLatin1 ("Times")));
  65. m_actionTextFontSize->setFontSize (cfg->readNumEntry (kpSettingFontSize, 14));
  66. m_actionTextBold->setChecked (cfg->readBoolEntry (kpSettingBold, false));
  67. m_actionTextItalic->setChecked (cfg->readBoolEntry (kpSettingItalic, false));
  68. m_actionTextUnderline->setChecked (cfg->readBoolEntry (kpSettingUnderline, false));
  69. m_actionTextStrikeThru->setChecked (cfg->readBoolEntry (kpSettingStrikeThru, false));
  70. m_textOldFontFamily = m_actionTextFontFamily->font ();
  71. m_textOldFontSize = m_actionTextFontSize->fontSize ();
  72. }
  73. // public
  74. void kpMainWindow::enableTextToolBarActions (bool enable)
  75. {
  76. #if DEBUG_KP_MAIN_WINDOW
  77. kdDebug () << "kpMainWindow::enableTextToolBarActions(" << enable << ")" << endl;
  78. #endif
  79. m_actionTextFontFamily->setEnabled (enable);
  80. m_actionTextFontSize->setEnabled (enable);
  81. m_actionTextBold->setEnabled (enable);
  82. m_actionTextItalic->setEnabled (enable);
  83. m_actionTextUnderline->setEnabled (enable);
  84. m_actionTextStrikeThru->setEnabled (enable);
  85. if (textToolBar ())
  86. {
  87. #if DEBUG_KP_MAIN_WINDOW
  88. kdDebug () << "\thave toolbar - setShown" << endl;
  89. #endif
  90. textToolBar ()->setShown (enable);
  91. }
  92. }
  93. // private slot
  94. void kpMainWindow::slotTextFontFamilyChanged ()
  95. {
  96. #if DEBUG_KP_MAIN_WINDOW
  97. kdDebug () << "kpMainWindow::slotTextFontFamilyChanged() alive="
  98. << m_isFullyConstructed
  99. << " fontFamily="
  100. << m_actionTextFontFamily->font ()
  101. << endl;
  102. #endif
  103. if (!m_isFullyConstructed)
  104. return;
  105. if (m_toolText && m_toolText->hasBegun ())
  106. {
  107. m_toolText->slotFontFamilyChanged (m_actionTextFontFamily->font (),
  108. m_textOldFontFamily);
  109. }
  110. // Since editable TDESelectAction's steal focus from view, switch back to mainView
  111. // TODO: back to the last view
  112. if (m_mainView)
  113. m_mainView->setFocus ();
  114. TDEConfigGroupSaver cfgGroupSaver (kapp->config (), kpSettingsGroupText);
  115. TDEConfigBase *cfg = cfgGroupSaver.config ();
  116. cfg->writeEntry (kpSettingFontFamily, m_actionTextFontFamily->font ());
  117. cfg->sync ();
  118. m_textOldFontFamily = m_actionTextFontFamily->font ();
  119. }
  120. // private slot
  121. void kpMainWindow::slotTextFontSizeChanged ()
  122. {
  123. #if DEBUG_KP_MAIN_WINDOW
  124. kdDebug () << "kpMainWindow::slotTextFontSizeChanged() alive="
  125. << m_isFullyConstructed
  126. << " fontSize="
  127. << m_actionTextFontSize->fontSize ()
  128. << endl;
  129. #endif
  130. if (!m_isFullyConstructed)
  131. return;
  132. if (m_toolText && m_toolText->hasBegun ())
  133. {
  134. m_toolText->slotFontSizeChanged (m_actionTextFontSize->fontSize (),
  135. m_textOldFontSize);
  136. }
  137. // Since editable TDESelectAction's steal focus from view, switch back to mainView
  138. // TODO: back to the last view
  139. if (m_mainView)
  140. m_mainView->setFocus ();
  141. TDEConfigGroupSaver cfgGroupSaver (kapp->config (), kpSettingsGroupText);
  142. TDEConfigBase *cfg = cfgGroupSaver.config ();
  143. cfg->writeEntry (kpSettingFontSize, m_actionTextFontSize->fontSize ());
  144. cfg->sync ();
  145. m_textOldFontSize = m_actionTextFontSize->fontSize ();
  146. }
  147. // private slot
  148. void kpMainWindow::slotTextBoldChanged ()
  149. {
  150. #if DEBUG_KP_MAIN_WINDOW
  151. kdDebug () << "kpMainWindow::slotTextFontBoldChanged() alive="
  152. << m_isFullyConstructed
  153. << " bold="
  154. << m_actionTextBold->isChecked ()
  155. << endl;
  156. #endif
  157. if (!m_isFullyConstructed)
  158. return;
  159. if (m_toolText && m_toolText->hasBegun ())
  160. m_toolText->slotBoldChanged (m_actionTextBold->isChecked ());
  161. TDEConfigGroupSaver cfgGroupSaver (kapp->config (), kpSettingsGroupText);
  162. TDEConfigBase *cfg = cfgGroupSaver.config ();
  163. cfg->writeEntry (kpSettingBold, m_actionTextBold->isChecked ());
  164. cfg->sync ();
  165. }
  166. // private slot
  167. void kpMainWindow::slotTextItalicChanged ()
  168. {
  169. #if DEBUG_KP_MAIN_WINDOW
  170. kdDebug () << "kpMainWindow::slotTextFontItalicChanged() alive="
  171. << m_isFullyConstructed
  172. << " bold="
  173. << m_actionTextItalic->isChecked ()
  174. << endl;
  175. #endif
  176. if (!m_isFullyConstructed)
  177. return;
  178. if (m_toolText && m_toolText->hasBegun ())
  179. m_toolText->slotItalicChanged (m_actionTextItalic->isChecked ());
  180. TDEConfigGroupSaver cfgGroupSaver (kapp->config (), kpSettingsGroupText);
  181. TDEConfigBase *cfg = cfgGroupSaver.config ();
  182. cfg->writeEntry (kpSettingItalic, m_actionTextItalic->isChecked ());
  183. cfg->sync ();
  184. }
  185. // private slot
  186. void kpMainWindow::slotTextUnderlineChanged ()
  187. {
  188. #if DEBUG_KP_MAIN_WINDOW
  189. kdDebug () << "kpMainWindow::slotTextFontUnderlineChanged() alive="
  190. << m_isFullyConstructed
  191. << " underline="
  192. << m_actionTextUnderline->isChecked ()
  193. << endl;
  194. #endif
  195. if (!m_isFullyConstructed)
  196. return;
  197. if (m_toolText && m_toolText->hasBegun ())
  198. m_toolText->slotUnderlineChanged (m_actionTextUnderline->isChecked ());
  199. TDEConfigGroupSaver cfgGroupSaver (kapp->config (), kpSettingsGroupText);
  200. TDEConfigBase *cfg = cfgGroupSaver.config ();
  201. cfg->writeEntry (kpSettingUnderline, m_actionTextUnderline->isChecked ());
  202. cfg->sync ();
  203. }
  204. // private slot
  205. void kpMainWindow::slotTextStrikeThruChanged ()
  206. {
  207. #if DEBUG_KP_MAIN_WINDOW
  208. kdDebug () << "kpMainWindow::slotTextStrikeThruChanged() alive="
  209. << m_isFullyConstructed
  210. << " strikeThru="
  211. << m_actionTextStrikeThru->isChecked ()
  212. << endl;
  213. #endif
  214. if (!m_isFullyConstructed)
  215. return;
  216. if (m_toolText && m_toolText->hasBegun ())
  217. m_toolText->slotStrikeThruChanged (m_actionTextStrikeThru->isChecked ());
  218. TDEConfigGroupSaver cfgGroupSaver (kapp->config (), kpSettingsGroupText);
  219. TDEConfigBase *cfg = cfgGroupSaver.config ();
  220. cfg->writeEntry (kpSettingStrikeThru, m_actionTextStrikeThru->isChecked ());
  221. cfg->sync ();
  222. }
  223. // public
  224. TDEToolBar *kpMainWindow::textToolBar ()
  225. {
  226. return toolBar ("textToolBar");
  227. }
  228. bool kpMainWindow::isTextStyleBackgroundOpaque () const
  229. {
  230. if (m_toolToolBar)
  231. {
  232. kpToolWidgetOpaqueOrTransparent *oot =
  233. m_toolToolBar->toolWidgetOpaqueOrTransparent ();
  234. if (oot)
  235. {
  236. return oot->isOpaque ();
  237. }
  238. }
  239. return true;
  240. }
  241. // public
  242. kpTextStyle kpMainWindow::textStyle () const
  243. {
  244. return kpTextStyle (m_actionTextFontFamily->font (),
  245. m_actionTextFontSize->fontSize (),
  246. m_actionTextBold->isChecked (),
  247. m_actionTextItalic->isChecked (),
  248. m_actionTextUnderline->isChecked (),
  249. m_actionTextStrikeThru->isChecked (),
  250. m_colorToolBar ? m_colorToolBar->foregroundColor () : kpColor::invalid,
  251. m_colorToolBar ? m_colorToolBar->backgroundColor () : kpColor::invalid,
  252. isTextStyleBackgroundOpaque ());
  253. }
  254. // public
  255. void kpMainWindow::setTextStyle (const kpTextStyle &textStyle_)
  256. {
  257. #if DEBUG_KP_MAIN_WINDOW
  258. kdDebug () << "kpMainWindow::setTextStyle()" << endl;
  259. #endif
  260. m_settingTextStyle++;
  261. if (textStyle_.fontFamily () != m_actionTextFontFamily->font ())
  262. {
  263. m_actionTextFontFamily->setFont (textStyle_.fontFamily ());
  264. slotTextFontFamilyChanged ();
  265. }
  266. if (textStyle_.fontSize () != m_actionTextFontSize->fontSize ())
  267. {
  268. m_actionTextFontSize->setFontSize (textStyle_.fontSize ());
  269. slotTextFontSizeChanged ();
  270. }
  271. if (textStyle_.isBold () != m_actionTextBold->isChecked ())
  272. {
  273. m_actionTextBold->setChecked (textStyle_.isBold ());
  274. slotTextBoldChanged ();
  275. }
  276. if (textStyle_.isItalic () != m_actionTextItalic->isChecked ())
  277. {
  278. m_actionTextItalic->setChecked (textStyle_.isItalic ());
  279. slotTextItalicChanged ();
  280. }
  281. if (textStyle_.isUnderline () != m_actionTextUnderline->isChecked ())
  282. {
  283. m_actionTextUnderline->setChecked (textStyle_.isUnderline ());
  284. slotTextUnderlineChanged ();
  285. }
  286. if (textStyle_.isStrikeThru () != m_actionTextStrikeThru->isChecked ())
  287. {
  288. m_actionTextStrikeThru->setChecked (textStyle_.isStrikeThru ());
  289. slotTextStrikeThruChanged ();
  290. }
  291. if (textStyle_.foregroundColor () != m_colorToolBar->foregroundColor ())
  292. {
  293. m_colorToolBar->setForegroundColor (textStyle_.foregroundColor ());
  294. }
  295. if (textStyle_.backgroundColor () != m_colorToolBar->backgroundColor ())
  296. {
  297. m_colorToolBar->setBackgroundColor (textStyle_.backgroundColor ());
  298. }
  299. if (textStyle_.isBackgroundOpaque () != isTextStyleBackgroundOpaque ())
  300. {
  301. if (m_toolToolBar)
  302. {
  303. kpToolWidgetOpaqueOrTransparent *oot =
  304. m_toolToolBar->toolWidgetOpaqueOrTransparent ();
  305. if (oot)
  306. {
  307. oot->setOpaque (textStyle_.isBackgroundOpaque ());
  308. }
  309. }
  310. }
  311. m_settingTextStyle--;
  312. }
  313. // public
  314. int kpMainWindow::settingTextStyle () const
  315. {
  316. return m_settingTextStyle;
  317. }