compile
[kdegraphics.git] / kolourpaint / mainWindow / kpMainWindow_Text.cpp
blob588dd04e10030344e1949b90f5682b0f5ab62a19
2 /*
3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <kpMainWindow.h>
30 #include <kpMainWindowPrivate.h>
32 #include <kactioncollection.h>
33 #include <kapplication.h>
34 #include <kconfig.h>
35 #include <kconfiggroup.h>
36 #include <kdebug.h>
37 #include <kfontaction.h>
38 #include <kfontsizeaction.h>
39 #include <kglobal.h>
40 #include <kicon.h>
41 #include <klocale.h>
42 #include <KToolBar>
43 #include <ktoggleaction.h>
45 #include <kpColorToolBar.h>
46 #include <kpDefs.h>
47 #include <kpTextStyle.h>
48 #include <kpToolText.h>
49 #include <kpToolToolBar.h>
50 #include <kpToolWidgetOpaqueOrTransparent.h>
51 #include <kpZoomedView.h>
54 // private
55 void kpMainWindow::setupTextToolBarActions ()
57 KActionCollection *ac = actionCollection ();
59 d->actionTextFontFamily = ac->add<KFontAction> ("text_font_family");
60 d->actionTextFontFamily->setText (i18n ("Font Family"));
61 connect (d->actionTextFontFamily, SIGNAL (triggered (const QString &)),
62 this, SLOT (slotTextFontFamilyChanged ()));
64 d->actionTextFontSize = ac->add<KFontSizeAction> ("text_font_size");
65 d->actionTextFontSize->setText (i18n ("Font Size"));
66 connect (d->actionTextFontSize, SIGNAL (fontSizeChanged (int)),
67 this, SLOT (slotTextFontSizeChanged ()));
69 d->actionTextBold = ac->add<KToggleAction> ("text_bold");
70 d->actionTextBold->setIcon (KIcon ("format-text-bold"));
71 d->actionTextBold->setText (i18n ("Bold"));
72 connect (d->actionTextBold, SIGNAL (triggered (bool)),
73 SLOT (slotTextBoldChanged ()));
75 d->actionTextItalic = ac->add<KToggleAction> ("text_italic");
76 d->actionTextItalic->setIcon (KIcon ("format-text-italic"));
77 d->actionTextItalic->setText (i18n ("Italic"));
78 connect (d->actionTextItalic, SIGNAL (triggered (bool)),
79 SLOT (slotTextItalicChanged ()));
81 d->actionTextUnderline = ac->add<KToggleAction> ("text_underline");
82 d->actionTextUnderline->setIcon (KIcon ("format-text-underline"));
83 d->actionTextUnderline->setText (i18n ("Underline"));
84 connect (d->actionTextUnderline, SIGNAL (triggered (bool)),
85 SLOT (slotTextUnderlineChanged ()));
87 d->actionTextStrikeThru = ac->add<KToggleAction> ("text_strike_thru");
88 d->actionTextStrikeThru->setIcon (KIcon ("format-text-strikethrough"));
89 d->actionTextStrikeThru->setText (i18n ("Strike Through"));
90 connect (d->actionTextStrikeThru, SIGNAL (triggered (bool)),
91 SLOT (slotTextStrikeThruChanged ()));
94 readAndApplyTextSettings ();
97 enableTextToolBarActions (false);
100 // private
101 void kpMainWindow::readAndApplyTextSettings ()
103 KConfigGroup cfg (KGlobal::config (), kpSettingsGroupText);
105 const QString font (cfg.readEntry (kpSettingFontFamily, QString::fromLatin1 ("Times")));
106 d->actionTextFontFamily->setFont (font);
107 #if DEBUG_KP_MAIN_WINDOW
108 kDebug () << "asked setFont to set to=" << font
109 << "- got back=" << d->actionTextFontFamily->font ();
110 #endif
111 d->actionTextFontSize->setFontSize (cfg.readEntry (kpSettingFontSize, 14));
112 d->actionTextBold->setChecked (cfg.readEntry (kpSettingBold, false));
113 d->actionTextItalic->setChecked (cfg.readEntry (kpSettingItalic, false));
114 d->actionTextUnderline->setChecked (cfg.readEntry (kpSettingUnderline, false));
115 d->actionTextStrikeThru->setChecked (cfg.readEntry (kpSettingStrikeThru, false));
117 d->textOldFontFamily = d->actionTextFontFamily->font ();
118 d->textOldFontSize = d->actionTextFontSize->fontSize ();
122 // public
123 void kpMainWindow::enableTextToolBarActions (bool enable)
125 #if DEBUG_KP_MAIN_WINDOW
126 kDebug () << "kpMainWindow::enableTextToolBarActions(" << enable << ")";
127 #endif
129 d->actionTextFontFamily->setEnabled (enable);
130 d->actionTextFontSize->setEnabled (enable);
131 d->actionTextBold->setEnabled (enable);
132 d->actionTextItalic->setEnabled (enable);
133 d->actionTextUnderline->setEnabled (enable);
134 d->actionTextStrikeThru->setEnabled (enable);
136 if (textToolBar ())
138 #if DEBUG_KP_MAIN_WINDOW
139 kDebug () << "\thave toolbar - setShown";
140 #endif
141 // COMPAT: KDE4 does not place the Text Tool Bar in a new row, underneath
142 // the Main Tool Bar, if there isn't enough room. This makes
143 // accessing the Text Tool Bar's buttons difficult.
144 textToolBar ()->setVisible (enable);
149 // private slot
150 void kpMainWindow::slotTextFontFamilyChanged ()
152 #if DEBUG_KP_MAIN_WINDOW
153 kDebug () << "kpMainWindow::slotTextFontFamilyChanged() alive="
154 << d->isFullyConstructed
155 << "fontFamily="
156 << d->actionTextFontFamily->font ()
157 << "action.currentItem="
158 << d->actionTextFontFamily->currentItem ()
159 << endl;
160 #endif
162 if (!d->isFullyConstructed)
163 return;
165 if (d->toolText && d->toolText->hasBegun ())
167 toolEndShape ();
168 d->toolText->slotFontFamilyChanged (d->actionTextFontFamily->font (),
169 d->textOldFontFamily);
172 // Since editable KSelectAction's steal focus from view, switch back to mainView
173 // TODO: back to the last view
174 if (d->mainView)
175 d->mainView->setFocus ();
177 KConfigGroup cfg (KGlobal::config (), kpSettingsGroupText);
178 cfg.writeEntry (kpSettingFontFamily, d->actionTextFontFamily->font ());
179 cfg.sync ();
181 d->textOldFontFamily = d->actionTextFontFamily->font ();
184 // private slot
185 void kpMainWindow::slotTextFontSizeChanged ()
187 #if DEBUG_KP_MAIN_WINDOW
188 kDebug () << "kpMainWindow::slotTextFontSizeChanged() alive="
189 << d->isFullyConstructed
190 << " fontSize="
191 << d->actionTextFontSize->fontSize ()
192 << endl;
193 #endif
195 if (!d->isFullyConstructed)
196 return;
198 if (d->toolText && d->toolText->hasBegun ())
200 toolEndShape ();
201 d->toolText->slotFontSizeChanged (d->actionTextFontSize->fontSize (),
202 d->textOldFontSize);
205 // Since editable KSelectAction's steal focus from view, switch back to mainView
206 // TODO: back to the last view
207 if (d->mainView)
208 d->mainView->setFocus ();
210 KConfigGroup cfg (KGlobal::config (), kpSettingsGroupText);
211 cfg.writeEntry (kpSettingFontSize, d->actionTextFontSize->fontSize ());
212 cfg.sync ();
214 d->textOldFontSize = d->actionTextFontSize->fontSize ();
217 // private slot
218 void kpMainWindow::slotTextBoldChanged ()
220 #if DEBUG_KP_MAIN_WINDOW
221 kDebug () << "kpMainWindow::slotTextFontBoldChanged() alive="
222 << d->isFullyConstructed
223 << " bold="
224 << d->actionTextBold->isChecked ()
225 << endl;
226 #endif
228 if (!d->isFullyConstructed)
229 return;
231 if (d->toolText && d->toolText->hasBegun ())
233 toolEndShape ();
234 d->toolText->slotBoldChanged (d->actionTextBold->isChecked ());
237 KConfigGroup cfg (KGlobal::config (), kpSettingsGroupText);
238 cfg.writeEntry (kpSettingBold, d->actionTextBold->isChecked ());
239 cfg.sync ();
242 // private slot
243 void kpMainWindow::slotTextItalicChanged ()
245 #if DEBUG_KP_MAIN_WINDOW
246 kDebug () << "kpMainWindow::slotTextFontItalicChanged() alive="
247 << d->isFullyConstructed
248 << " bold="
249 << d->actionTextItalic->isChecked ()
250 << endl;
251 #endif
253 if (!d->isFullyConstructed)
254 return;
256 if (d->toolText && d->toolText->hasBegun ())
258 toolEndShape ();
259 d->toolText->slotItalicChanged (d->actionTextItalic->isChecked ());
262 KConfigGroup cfg (KGlobal::config (), kpSettingsGroupText);
263 cfg.writeEntry (kpSettingItalic, d->actionTextItalic->isChecked ());
264 cfg.sync ();
267 // private slot
268 void kpMainWindow::slotTextUnderlineChanged ()
270 #if DEBUG_KP_MAIN_WINDOW
271 kDebug () << "kpMainWindow::slotTextFontUnderlineChanged() alive="
272 << d->isFullyConstructed
273 << " underline="
274 << d->actionTextUnderline->isChecked ()
275 << endl;
276 #endif
278 if (!d->isFullyConstructed)
279 return;
281 if (d->toolText && d->toolText->hasBegun ())
283 toolEndShape ();
284 d->toolText->slotUnderlineChanged (d->actionTextUnderline->isChecked ());
287 KConfigGroup cfg (KGlobal::config (), kpSettingsGroupText);
288 cfg.writeEntry (kpSettingUnderline, d->actionTextUnderline->isChecked ());
289 cfg.sync ();
292 // private slot
293 void kpMainWindow::slotTextStrikeThruChanged ()
295 #if DEBUG_KP_MAIN_WINDOW
296 kDebug () << "kpMainWindow::slotTextStrikeThruChanged() alive="
297 << d->isFullyConstructed
298 << " strikeThru="
299 << d->actionTextStrikeThru->isChecked ()
300 << endl;
301 #endif
303 if (!d->isFullyConstructed)
304 return;
306 if (d->toolText && d->toolText->hasBegun ())
308 toolEndShape ();
309 d->toolText->slotStrikeThruChanged (d->actionTextStrikeThru->isChecked ());
312 KConfigGroup cfg (KGlobal::config (), kpSettingsGroupText);
313 cfg.writeEntry (kpSettingStrikeThru, d->actionTextStrikeThru->isChecked ());
314 cfg.sync ();
318 // public
319 KToolBar *kpMainWindow::textToolBar ()
321 return toolBar ("textToolBar");
324 bool kpMainWindow::isTextStyleBackgroundOpaque () const
326 if (d->toolToolBar)
328 kpToolWidgetOpaqueOrTransparent *oot =
329 d->toolToolBar->toolWidgetOpaqueOrTransparent ();
331 if (oot)
333 return oot->isOpaque ();
337 return true;
340 // public
341 kpTextStyle kpMainWindow::textStyle () const
343 return kpTextStyle (d->actionTextFontFamily->font (),
344 d->actionTextFontSize->fontSize (),
345 d->actionTextBold->isChecked (),
346 d->actionTextItalic->isChecked (),
347 d->actionTextUnderline->isChecked (),
348 d->actionTextStrikeThru->isChecked (),
349 d->colorToolBar ? d->colorToolBar->foregroundColor () : kpColor::Invalid,
350 d->colorToolBar ? d->colorToolBar->backgroundColor () : kpColor::Invalid,
351 isTextStyleBackgroundOpaque ());
354 // public
355 void kpMainWindow::setTextStyle (const kpTextStyle &textStyle_)
357 #if DEBUG_KP_MAIN_WINDOW
358 kDebug () << "kpMainWindow::setTextStyle()";
359 #endif
361 d->settingTextStyle++;
364 if (textStyle_.fontFamily () != d->actionTextFontFamily->font ())
366 d->actionTextFontFamily->setFont (textStyle_.fontFamily ());
367 slotTextFontFamilyChanged ();
370 if (textStyle_.fontSize () != d->actionTextFontSize->fontSize ())
372 d->actionTextFontSize->setFontSize (textStyle_.fontSize ());
373 slotTextFontSizeChanged ();
376 if (textStyle_.isBold () != d->actionTextBold->isChecked ())
378 d->actionTextBold->setChecked (textStyle_.isBold ());
379 slotTextBoldChanged ();
382 if (textStyle_.isItalic () != d->actionTextItalic->isChecked ())
384 d->actionTextItalic->setChecked (textStyle_.isItalic ());
385 slotTextItalicChanged ();
388 if (textStyle_.isUnderline () != d->actionTextUnderline->isChecked ())
390 d->actionTextUnderline->setChecked (textStyle_.isUnderline ());
391 slotTextUnderlineChanged ();
394 if (textStyle_.isStrikeThru () != d->actionTextStrikeThru->isChecked ())
396 d->actionTextStrikeThru->setChecked (textStyle_.isStrikeThru ());
397 slotTextStrikeThruChanged ();
401 if (textStyle_.foregroundColor () != d->colorToolBar->foregroundColor ())
403 d->colorToolBar->setForegroundColor (textStyle_.foregroundColor ());
406 if (textStyle_.backgroundColor () != d->colorToolBar->backgroundColor ())
408 d->colorToolBar->setBackgroundColor (textStyle_.backgroundColor ());
412 if (textStyle_.isBackgroundOpaque () != isTextStyleBackgroundOpaque ())
414 if (d->toolToolBar)
416 kpToolWidgetOpaqueOrTransparent *oot =
417 d->toolToolBar->toolWidgetOpaqueOrTransparent ();
419 if (oot)
421 oot->setOpaque (textStyle_.isBackgroundOpaque ());
427 d->settingTextStyle--;
430 // public
431 int kpMainWindow::settingTextStyle () const
433 return d->settingTextStyle;