3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
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>
35 #include <kconfiggroup.h>
37 #include <kfontaction.h>
38 #include <kfontsizeaction.h>
43 #include <ktoggleaction.h>
45 #include <kpColorToolBar.h>
47 #include <kpTextStyle.h>
48 #include <kpToolText.h>
49 #include <kpToolToolBar.h>
50 #include <kpToolWidgetOpaqueOrTransparent.h>
51 #include <kpZoomedView.h>
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);
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 ();
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 ();
123 void kpMainWindow::enableTextToolBarActions (bool enable
)
125 #if DEBUG_KP_MAIN_WINDOW
126 kDebug () << "kpMainWindow::enableTextToolBarActions(" << enable
<< ")";
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
);
138 #if DEBUG_KP_MAIN_WINDOW
139 kDebug () << "\thave toolbar - setShown";
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
);
150 void kpMainWindow::slotTextFontFamilyChanged ()
152 #if DEBUG_KP_MAIN_WINDOW
153 kDebug () << "kpMainWindow::slotTextFontFamilyChanged() alive="
154 << d
->isFullyConstructed
156 << d
->actionTextFontFamily
->font ()
157 << "action.currentItem="
158 << d
->actionTextFontFamily
->currentItem ()
162 if (!d
->isFullyConstructed
)
165 if (d
->toolText
&& d
->toolText
->hasBegun ())
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
175 d
->mainView
->setFocus ();
177 KConfigGroup
cfg (KGlobal::config (), kpSettingsGroupText
);
178 cfg
.writeEntry (kpSettingFontFamily
, d
->actionTextFontFamily
->font ());
181 d
->textOldFontFamily
= d
->actionTextFontFamily
->font ();
185 void kpMainWindow::slotTextFontSizeChanged ()
187 #if DEBUG_KP_MAIN_WINDOW
188 kDebug () << "kpMainWindow::slotTextFontSizeChanged() alive="
189 << d
->isFullyConstructed
191 << d
->actionTextFontSize
->fontSize ()
195 if (!d
->isFullyConstructed
)
198 if (d
->toolText
&& d
->toolText
->hasBegun ())
201 d
->toolText
->slotFontSizeChanged (d
->actionTextFontSize
->fontSize (),
205 // Since editable KSelectAction's steal focus from view, switch back to mainView
206 // TODO: back to the last view
208 d
->mainView
->setFocus ();
210 KConfigGroup
cfg (KGlobal::config (), kpSettingsGroupText
);
211 cfg
.writeEntry (kpSettingFontSize
, d
->actionTextFontSize
->fontSize ());
214 d
->textOldFontSize
= d
->actionTextFontSize
->fontSize ();
218 void kpMainWindow::slotTextBoldChanged ()
220 #if DEBUG_KP_MAIN_WINDOW
221 kDebug () << "kpMainWindow::slotTextFontBoldChanged() alive="
222 << d
->isFullyConstructed
224 << d
->actionTextBold
->isChecked ()
228 if (!d
->isFullyConstructed
)
231 if (d
->toolText
&& d
->toolText
->hasBegun ())
234 d
->toolText
->slotBoldChanged (d
->actionTextBold
->isChecked ());
237 KConfigGroup
cfg (KGlobal::config (), kpSettingsGroupText
);
238 cfg
.writeEntry (kpSettingBold
, d
->actionTextBold
->isChecked ());
243 void kpMainWindow::slotTextItalicChanged ()
245 #if DEBUG_KP_MAIN_WINDOW
246 kDebug () << "kpMainWindow::slotTextFontItalicChanged() alive="
247 << d
->isFullyConstructed
249 << d
->actionTextItalic
->isChecked ()
253 if (!d
->isFullyConstructed
)
256 if (d
->toolText
&& d
->toolText
->hasBegun ())
259 d
->toolText
->slotItalicChanged (d
->actionTextItalic
->isChecked ());
262 KConfigGroup
cfg (KGlobal::config (), kpSettingsGroupText
);
263 cfg
.writeEntry (kpSettingItalic
, d
->actionTextItalic
->isChecked ());
268 void kpMainWindow::slotTextUnderlineChanged ()
270 #if DEBUG_KP_MAIN_WINDOW
271 kDebug () << "kpMainWindow::slotTextFontUnderlineChanged() alive="
272 << d
->isFullyConstructed
274 << d
->actionTextUnderline
->isChecked ()
278 if (!d
->isFullyConstructed
)
281 if (d
->toolText
&& d
->toolText
->hasBegun ())
284 d
->toolText
->slotUnderlineChanged (d
->actionTextUnderline
->isChecked ());
287 KConfigGroup
cfg (KGlobal::config (), kpSettingsGroupText
);
288 cfg
.writeEntry (kpSettingUnderline
, d
->actionTextUnderline
->isChecked ());
293 void kpMainWindow::slotTextStrikeThruChanged ()
295 #if DEBUG_KP_MAIN_WINDOW
296 kDebug () << "kpMainWindow::slotTextStrikeThruChanged() alive="
297 << d
->isFullyConstructed
299 << d
->actionTextStrikeThru
->isChecked ()
303 if (!d
->isFullyConstructed
)
306 if (d
->toolText
&& d
->toolText
->hasBegun ())
309 d
->toolText
->slotStrikeThruChanged (d
->actionTextStrikeThru
->isChecked ());
312 KConfigGroup
cfg (KGlobal::config (), kpSettingsGroupText
);
313 cfg
.writeEntry (kpSettingStrikeThru
, d
->actionTextStrikeThru
->isChecked ());
319 KToolBar
*kpMainWindow::textToolBar ()
321 return toolBar ("textToolBar");
324 bool kpMainWindow::isTextStyleBackgroundOpaque () const
328 kpToolWidgetOpaqueOrTransparent
*oot
=
329 d
->toolToolBar
->toolWidgetOpaqueOrTransparent ();
333 return oot
->isOpaque ();
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 ());
355 void kpMainWindow::setTextStyle (const kpTextStyle
&textStyle_
)
357 #if DEBUG_KP_MAIN_WINDOW
358 kDebug () << "kpMainWindow::setTextStyle()";
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 ())
416 kpToolWidgetOpaqueOrTransparent
*oot
=
417 d
->toolToolBar
->toolWidgetOpaqueOrTransparent ();
421 oot
->setOpaque (textStyle_
.isBackgroundOpaque ());
427 d
->settingTextStyle
--;
431 int kpMainWindow::settingTextStyle () const
433 return d
->settingTextStyle
;