File indexing completed on 2024-04-28 04:20:21

0001 /*
0002    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0003    All rights reserved.
0004 
0005    Redistribution and use in source and binary forms, with or without
0006    modification, are permitted provided that the following conditions
0007    are met:
0008 
0009    1. Redistributions of source code must retain the above copyright
0010       notice, this list of conditions and the following disclaimer.
0011    2. Redistributions in binary form must reproduce the above copyright
0012       notice, this list of conditions and the following disclaimer in the
0013       documentation and/or other materials provided with the distribution.
0014 
0015    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0016    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0017    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0018    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0019    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0020    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0021    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0022    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0023    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0024    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025 */
0026 
0027 
0028 #include "mainWindow/kpMainWindow.h"
0029 #include "kpMainWindowPrivate.h"
0030 
0031 #include <KActionCollection>
0032 #include <KSharedConfig>
0033 #include <KConfigGroup>
0034 #include "kpLogCategories.h"
0035 #include <KFontAction>
0036 #include <KFontSizeAction>
0037 #include <KToolBar>
0038 #include <KToggleAction>
0039 #include <KLocalizedString>
0040 
0041 #include "widgets/toolbars/kpColorToolBar.h"
0042 #include "kpDefs.h"
0043 #include "layers/selections/text/kpTextStyle.h"
0044 #include "tools/selection/text/kpToolText.h"
0045 #include "widgets/toolbars/kpToolToolBar.h"
0046 #include "widgets/toolbars/options/kpToolWidgetOpaqueOrTransparent.h"
0047 #include "views/kpZoomedView.h"
0048 
0049 
0050 // private
0051 void kpMainWindow::setupTextToolBarActions ()
0052 {
0053     KActionCollection *ac = actionCollection ();
0054 
0055     d->actionTextFontFamily = ac->add<KFontAction> (QStringLiteral("text_font_family"));
0056     d->actionTextFontFamily->setText (i18n ("Font Family"));
0057     connect (d->actionTextFontFamily,
0058              &KSelectAction::textTriggered,
0059              this, &kpMainWindow::slotTextFontFamilyChanged);
0060 
0061     d->actionTextFontSize = ac->add<KFontSizeAction> (QStringLiteral("text_font_size"));
0062     d->actionTextFontSize->setText (i18n ("Font Size"));
0063     connect (d->actionTextFontSize,
0064              &KSelectAction::indexTriggered,
0065              this, &kpMainWindow::slotTextFontSizeChanged);
0066 
0067     d->actionTextBold = ac->add<KToggleAction> (QStringLiteral("text_bold"));
0068     d->actionTextBold->setIcon(QIcon::fromTheme(QStringLiteral("format-text-bold")));
0069     d->actionTextBold->setText (i18n ("Bold"));
0070     connect (d->actionTextBold, &KToggleAction::triggered,
0071              this, &kpMainWindow::slotTextBoldChanged);
0072 
0073     d->actionTextItalic = ac->add<KToggleAction> (QStringLiteral("text_italic"));
0074     d->actionTextItalic->setIcon (QIcon::fromTheme(QStringLiteral("format-text-italic")));
0075     d->actionTextItalic->setText (i18n ("Italic"));
0076     connect (d->actionTextItalic, &KToggleAction::triggered,
0077              this, &kpMainWindow::slotTextItalicChanged);
0078 
0079     d->actionTextUnderline = ac->add<KToggleAction> (QStringLiteral("text_underline"));
0080     d->actionTextUnderline->setIcon (QIcon::fromTheme(QStringLiteral("format-text-underline")));
0081     d->actionTextUnderline->setText (i18n ("Underline"));
0082     connect (d->actionTextUnderline, &KToggleAction::triggered,
0083              this, &kpMainWindow::slotTextUnderlineChanged);
0084 
0085     d->actionTextStrikeThru = ac->add<KToggleAction> (QStringLiteral("text_strike_thru"));
0086     d->actionTextStrikeThru->setIcon(QIcon::fromTheme(QStringLiteral("format-text-strikethrough")));
0087     d->actionTextStrikeThru->setText (i18n ("Strike Through"));
0088     connect (d->actionTextStrikeThru, &KToggleAction::triggered,
0089              this, &kpMainWindow::slotTextStrikeThruChanged);
0090 
0091 
0092     readAndApplyTextSettings ();
0093 
0094 
0095     enableTextToolBarActions (false);
0096 }
0097 
0098 // private
0099 void kpMainWindow::readAndApplyTextSettings ()
0100 {
0101     KConfigGroup cfg (KSharedConfig::openConfig (), QStringLiteral(kpSettingsGroupText));
0102 
0103     const QString font (cfg.readEntry (kpSettingFontFamily, QStringLiteral ("Times")));
0104     d->actionTextFontFamily->setFont (font);
0105 #if DEBUG_KP_MAIN_WINDOW
0106     qCDebug(kpLogMainWindow) << "asked setFont to set to=" << font
0107               << "- got back=" << d->actionTextFontFamily->font ();
0108 #endif
0109     d->actionTextFontSize->setFontSize (cfg.readEntry (kpSettingFontSize, 14));
0110     d->actionTextBold->setChecked (cfg.readEntry (kpSettingBold, false));
0111     d->actionTextItalic->setChecked (cfg.readEntry (kpSettingItalic, false));
0112     d->actionTextUnderline->setChecked (cfg.readEntry (kpSettingUnderline, false));
0113     d->actionTextStrikeThru->setChecked (cfg.readEntry (kpSettingStrikeThru, false));
0114 
0115     d->textOldFontFamily = d->actionTextFontFamily->font ();
0116     d->textOldFontSize = d->actionTextFontSize->fontSize ();
0117 }
0118 
0119 
0120 // public
0121 void kpMainWindow::enableTextToolBarActions (bool enable)
0122 {
0123 #if DEBUG_KP_MAIN_WINDOW
0124     qCDebug(kpLogMainWindow) << "kpMainWindow::enableTextToolBarActions(" << enable << ")";
0125 #endif
0126 
0127     d->actionTextFontFamily->setEnabled (enable);
0128     d->actionTextFontSize->setEnabled (enable);
0129     d->actionTextBold->setEnabled (enable);
0130     d->actionTextItalic->setEnabled (enable);
0131     d->actionTextUnderline->setEnabled (enable);
0132     d->actionTextStrikeThru->setEnabled (enable);
0133 
0134     if (textToolBar ())
0135     {
0136     #if DEBUG_KP_MAIN_WINDOW
0137         qCDebug(kpLogMainWindow) << "\thave toolbar - setShown";
0138     #endif
0139         // COMPAT: KDE4 does not place the Text Tool Bar in a new row, underneath
0140         //         the Main Tool Bar, if there isn't enough room.  This makes
0141         //         accessing the Text Tool Bar's buttons difficult.
0142         textToolBar ()->setVisible (enable);
0143     }
0144 }
0145 
0146 
0147 // private slot
0148 void kpMainWindow::slotTextFontFamilyChanged ()
0149 {
0150 #if DEBUG_KP_MAIN_WINDOW
0151     qCDebug(kpLogMainWindow) << "kpMainWindow::slotTextFontFamilyChanged() alive="
0152                << d->isFullyConstructed
0153                << "fontFamily="
0154                << d->actionTextFontFamily->font ()
0155                << "action.currentItem="
0156                << d->actionTextFontFamily->currentItem ();
0157 #endif
0158 
0159     if (!d->isFullyConstructed) {
0160         return;
0161     }
0162 
0163     if (d->toolText && d->toolText->hasBegun ())
0164     {
0165         toolEndShape ();
0166         d->toolText->slotFontFamilyChanged (d->actionTextFontFamily->font (),
0167                                            d->textOldFontFamily);
0168     }
0169 
0170     // Since editable KSelectAction's steal focus from view, switch back to mainView
0171     // TODO: back to the last view
0172     if (d->mainView) {
0173         d->mainView->setFocus ();
0174     }
0175 
0176     KConfigGroup cfg (KSharedConfig::openConfig (), QStringLiteral(kpSettingsGroupText));
0177     cfg.writeEntry (kpSettingFontFamily, d->actionTextFontFamily->font ());
0178     cfg.sync ();
0179 
0180     d->textOldFontFamily = d->actionTextFontFamily->font ();
0181 }
0182 
0183 // private slot
0184 void kpMainWindow::slotTextFontSizeChanged ()
0185 {
0186 #if DEBUG_KP_MAIN_WINDOW
0187     qCDebug(kpLogMainWindow) << "kpMainWindow::slotTextFontSizeChanged() alive="
0188                << d->isFullyConstructed
0189                << " fontSize="
0190                << d->actionTextFontSize->fontSize ();
0191 #endif
0192 
0193     if (!d->isFullyConstructed) {
0194         return;
0195     }
0196 
0197     if (d->toolText && d->toolText->hasBegun ())
0198     {
0199         toolEndShape ();
0200         d->toolText->slotFontSizeChanged (d->actionTextFontSize->fontSize (),
0201                                          d->textOldFontSize);
0202     }
0203 
0204     // Since editable KSelectAction's steal focus from view, switch back to mainView
0205     // TODO: back to the last view
0206     if (d->mainView) {
0207         d->mainView->setFocus ();
0208     }
0209 
0210     KConfigGroup cfg (KSharedConfig::openConfig (), QStringLiteral(kpSettingsGroupText));
0211     cfg.writeEntry (kpSettingFontSize, d->actionTextFontSize->fontSize ());
0212     cfg.sync ();
0213 
0214     d->textOldFontSize = d->actionTextFontSize->fontSize ();
0215 }
0216 
0217 // private slot
0218 void kpMainWindow::slotTextBoldChanged ()
0219 {
0220 #if DEBUG_KP_MAIN_WINDOW
0221     qCDebug(kpLogMainWindow) << "kpMainWindow::slotTextFontBoldChanged() alive="
0222                << d->isFullyConstructed
0223                << " bold="
0224                << d->actionTextBold->isChecked ();
0225 #endif
0226 
0227     if (!d->isFullyConstructed) {
0228         return;
0229     }
0230 
0231     if (d->toolText && d->toolText->hasBegun ())
0232     {
0233         toolEndShape ();
0234         d->toolText->slotBoldChanged (d->actionTextBold->isChecked ());
0235     }
0236 
0237     KConfigGroup cfg (KSharedConfig::openConfig (), QStringLiteral(kpSettingsGroupText));
0238     cfg.writeEntry (kpSettingBold, d->actionTextBold->isChecked ());
0239     cfg.sync ();
0240 }
0241 
0242 // private slot
0243 void kpMainWindow::slotTextItalicChanged ()
0244 {
0245 #if DEBUG_KP_MAIN_WINDOW
0246     qCDebug(kpLogMainWindow) << "kpMainWindow::slotTextFontItalicChanged() alive="
0247                << d->isFullyConstructed
0248                << " bold="
0249                << d->actionTextItalic->isChecked ();
0250 #endif
0251 
0252     if (!d->isFullyConstructed) {
0253         return;
0254     }
0255 
0256     if (d->toolText && d->toolText->hasBegun ())
0257     {
0258         toolEndShape ();
0259         d->toolText->slotItalicChanged (d->actionTextItalic->isChecked ());
0260     }
0261 
0262     KConfigGroup cfg (KSharedConfig::openConfig (), QStringLiteral(kpSettingsGroupText));
0263     cfg.writeEntry (kpSettingItalic, d->actionTextItalic->isChecked ());
0264     cfg.sync ();
0265 }
0266 
0267 // private slot
0268 void kpMainWindow::slotTextUnderlineChanged ()
0269 {
0270 #if DEBUG_KP_MAIN_WINDOW
0271     qCDebug(kpLogMainWindow) << "kpMainWindow::slotTextFontUnderlineChanged() alive="
0272                << d->isFullyConstructed
0273                << " underline="
0274                << d->actionTextUnderline->isChecked ();
0275 #endif
0276 
0277     if (!d->isFullyConstructed) {
0278         return;
0279     }
0280 
0281     if (d->toolText && d->toolText->hasBegun ())
0282     {
0283         toolEndShape ();
0284         d->toolText->slotUnderlineChanged (d->actionTextUnderline->isChecked ());
0285     }
0286 
0287     KConfigGroup cfg (KSharedConfig::openConfig (), QStringLiteral(kpSettingsGroupText));
0288     cfg.writeEntry (kpSettingUnderline, d->actionTextUnderline->isChecked ());
0289     cfg.sync ();
0290 }
0291 
0292 // private slot
0293 void kpMainWindow::slotTextStrikeThruChanged ()
0294 {
0295 #if DEBUG_KP_MAIN_WINDOW
0296     qCDebug(kpLogMainWindow) << "kpMainWindow::slotTextStrikeThruChanged() alive="
0297                << d->isFullyConstructed
0298                << " strikeThru="
0299                << d->actionTextStrikeThru->isChecked ();
0300 #endif
0301 
0302     if (!d->isFullyConstructed) {
0303         return;
0304     }
0305 
0306     if (d->toolText && d->toolText->hasBegun ())
0307     {
0308         toolEndShape ();
0309         d->toolText->slotStrikeThruChanged (d->actionTextStrikeThru->isChecked ());
0310     }
0311 
0312     KConfigGroup cfg (KSharedConfig::openConfig (), QStringLiteral(kpSettingsGroupText));
0313     cfg.writeEntry (kpSettingStrikeThru, d->actionTextStrikeThru->isChecked ());
0314     cfg.sync ();
0315 }
0316 
0317 
0318 // public
0319 KToolBar *kpMainWindow::textToolBar ()
0320 {
0321     return toolBar (QStringLiteral("textToolBar"));
0322 }
0323 
0324 bool kpMainWindow::isTextStyleBackgroundOpaque () const
0325 {
0326     if (d->toolToolBar)
0327     {
0328         kpToolWidgetOpaqueOrTransparent *oot =
0329             d->toolToolBar->toolWidgetOpaqueOrTransparent ();
0330 
0331         if (oot)
0332         {
0333             return oot->isOpaque ();
0334         }
0335     }
0336 
0337     return true;
0338 }
0339 
0340 // public
0341 kpTextStyle kpMainWindow::textStyle () const
0342 {
0343     return kpTextStyle (d->actionTextFontFamily->font (),
0344                         d->actionTextFontSize->fontSize (),
0345                         d->actionTextBold->isChecked (),
0346                         d->actionTextItalic->isChecked (),
0347                         d->actionTextUnderline->isChecked (),
0348                         d->actionTextStrikeThru->isChecked (),
0349                         d->colorToolBar ? d->colorToolBar->foregroundColor () : kpColor::Invalid,
0350                         d->colorToolBar ? d->colorToolBar->backgroundColor () : kpColor::Invalid,
0351                         isTextStyleBackgroundOpaque ());
0352 }
0353 
0354 // public
0355 void kpMainWindow::setTextStyle (const kpTextStyle &textStyle_)
0356 {
0357 #if DEBUG_KP_MAIN_WINDOW
0358     qCDebug(kpLogMainWindow) << "kpMainWindow::setTextStyle()";
0359 #endif
0360 
0361     d->settingTextStyle++;
0362 
0363 
0364     if (textStyle_.fontFamily () != d->actionTextFontFamily->font ())
0365     {
0366         d->actionTextFontFamily->setFont (textStyle_.fontFamily ());
0367         slotTextFontFamilyChanged ();
0368     }
0369 
0370     if (textStyle_.fontSize () != d->actionTextFontSize->fontSize ())
0371     {
0372         d->actionTextFontSize->setFontSize (textStyle_.fontSize ());
0373         slotTextFontSizeChanged ();
0374     }
0375 
0376     if (textStyle_.isBold () != d->actionTextBold->isChecked ())
0377     {
0378         d->actionTextBold->setChecked (textStyle_.isBold ());
0379         slotTextBoldChanged ();
0380     }
0381 
0382     if (textStyle_.isItalic () != d->actionTextItalic->isChecked ())
0383     {
0384         d->actionTextItalic->setChecked (textStyle_.isItalic ());
0385         slotTextItalicChanged ();
0386     }
0387 
0388     if (textStyle_.isUnderline () != d->actionTextUnderline->isChecked ())
0389     {
0390         d->actionTextUnderline->setChecked (textStyle_.isUnderline ());
0391         slotTextUnderlineChanged ();
0392     }
0393 
0394     if (textStyle_.isStrikeThru () != d->actionTextStrikeThru->isChecked ())
0395     {
0396         d->actionTextStrikeThru->setChecked (textStyle_.isStrikeThru ());
0397         slotTextStrikeThruChanged ();
0398     }
0399 
0400 
0401     if (textStyle_.foregroundColor () != d->colorToolBar->foregroundColor ())
0402     {
0403         d->colorToolBar->setForegroundColor (textStyle_.foregroundColor ());
0404     }
0405 
0406     if (textStyle_.backgroundColor () != d->colorToolBar->backgroundColor ())
0407     {
0408         d->colorToolBar->setBackgroundColor (textStyle_.backgroundColor ());
0409     }
0410 
0411 
0412     if (textStyle_.isBackgroundOpaque () != isTextStyleBackgroundOpaque ())
0413     {
0414         if (d->toolToolBar)
0415         {
0416             kpToolWidgetOpaqueOrTransparent *oot =
0417                 d->toolToolBar->toolWidgetOpaqueOrTransparent ();
0418 
0419             if (oot)
0420             {
0421                 oot->setOpaque (textStyle_.isBackgroundOpaque ());
0422             }
0423         }
0424     }
0425 
0426 
0427     d->settingTextStyle--;
0428 }
0429 
0430 // public
0431 int kpMainWindow::settingTextStyle () const
0432 {
0433     return d->settingTextStyle;
0434 }
0435