File indexing completed on 2024-12-15 04:03:26

0001 
0002 /*
0003    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0004    All rights reserved.
0005 
0006    Redistribution and use in source and binary forms, with or without
0007    modification, are permitted provided that the following conditions
0008    are met:
0009 
0010    1. Redistributions of source code must retain the above copyright
0011       notice, this list of conditions and the following disclaimer.
0012    2. Redistributions in binary form must reproduce the above copyright
0013       notice, this list of conditions and the following disclaimer in the
0014       documentation and/or other materials provided with the distribution.
0015 
0016    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026 */
0027 
0028 #define DEBUG_KP_TOOL_TEXT 0
0029 
0030 
0031 #include "tools/selection/text/kpToolText.h"
0032 #include "kpToolTextPrivate.h"
0033 #include "kpLogCategories.h"
0034 #include "commands/kpCommandHistory.h"
0035 #include "document/kpDocument.h"
0036 #include "layers/selections/text/kpTextSelection.h"
0037 #include "commands/tools/selection/text/kpToolTextBackspaceCommand.h"
0038 #include "commands/tools/selection/text/kpToolTextChangeStyleCommand.h"
0039 #include "commands/tools/selection/text/kpToolTextGiveContentCommand.h"
0040 #include "commands/tools/selection/kpToolSelectionCreateCommand.h"
0041 #include "environments/tools/selection/kpToolSelectionEnvironment.h"
0042 #include "commands/tools/selection/text/kpToolTextDeleteCommand.h"
0043 #include "commands/tools/selection/text/kpToolTextEnterCommand.h"
0044 #include "commands/tools/selection/text/kpToolTextInsertCommand.h"
0045 #include "widgets/toolbars/options/kpToolWidgetOpaqueOrTransparent.h"
0046 #include "views/kpView.h"
0047 #include "views/manager/kpViewManager.h"
0048 
0049 #include <KLocalizedString>
0050 
0051 // protected
0052 bool kpToolText::shouldChangeTextStyle () const
0053 {
0054     if (environ ()->settingTextStyle ())
0055     {
0056     #if DEBUG_KP_TOOL_TEXT
0057         qCDebug(kpLogTools) << "\trecursion - abort setting text style: "
0058                    << environ ()->settingTextStyle ();
0059     #endif
0060         return false;
0061     }
0062 
0063     if (!document ()->textSelection ())
0064     {
0065     #if DEBUG_KP_TOOL_TEXT
0066         qCDebug(kpLogTools) << "\tno text selection - abort setting text style";
0067     #endif
0068         return false;
0069     }
0070 
0071     return true;
0072 }
0073 
0074 // protected
0075 void kpToolText::changeTextStyle (const QString &name,
0076                                   const kpTextStyle &newTextStyle,
0077                                   const kpTextStyle &oldTextStyle)
0078 {
0079 #if DEBUG_KP_TOOL_TEXT
0080     qCDebug(kpLogTools) << "kpToolText::changeTextStyle(" << name << ")";
0081 #endif
0082 
0083     if (hasBegunShape ()) {
0084         endShape (currentPoint (), normalizedRect ());
0085     }
0086 
0087     commandHistory ()->addCommand (
0088         new kpToolTextChangeStyleCommand (
0089             name,
0090             newTextStyle,
0091             oldTextStyle,
0092             environ ()->commandEnvironment ()));
0093 }
0094 
0095 
0096 // protected slot virtual [base kpAbstractSelectionTool]
0097 void kpToolText::slotIsOpaqueChanged (bool isOpaque)
0098 {
0099 #if DEBUG_KP_TOOL_TEXT
0100     qCDebug(kpLogTools) << "kpToolText::slotIsOpaqueChanged()";
0101 #endif
0102 
0103     if (!shouldChangeTextStyle ()) {
0104         return;
0105     }
0106 
0107     kpTextStyle newTextStyle = environ ()->textStyle ();
0108 
0109     kpTextStyle oldTextStyle = newTextStyle;
0110     oldTextStyle.setBackgroundOpaque (!isOpaque);
0111 
0112     changeTextStyle (newTextStyle.isBackgroundOpaque () ?
0113                          i18n ("Text: Opaque Background") :
0114                          i18n ("Text: Transparent Background"),
0115                      newTextStyle,
0116                      oldTextStyle);
0117 }
0118 
0119 // protected slot virtual [base kpTool]
0120 void kpToolText::slotColorsSwapped (const kpColor &newForegroundColor,
0121                                     const kpColor &newBackgroundColor)
0122 {
0123 #if DEBUG_KP_TOOL_TEXT
0124     qCDebug(kpLogTools) << "kpToolText::slotColorsSwapped()";
0125 #endif
0126 
0127     if (!shouldChangeTextStyle ()) {
0128         return;
0129     }
0130 
0131     kpTextStyle newTextStyle = environ ()->textStyle ();
0132 
0133     kpTextStyle oldTextStyle = newTextStyle;
0134     oldTextStyle.setForegroundColor (newBackgroundColor);
0135     oldTextStyle.setBackgroundColor (newForegroundColor);
0136 
0137     changeTextStyle (i18n ("Text: Swap Colors"),
0138                      newTextStyle,
0139                      oldTextStyle);
0140 }
0141 
0142 // protected slot virtual [base kpTool]
0143 void kpToolText::slotForegroundColorChanged (const kpColor & /*color*/)
0144 {
0145 #if DEBUG_KP_TOOL_TEXT
0146     qCDebug(kpLogTools) << "kpToolText::slotForegroundColorChanged()";
0147 #endif
0148 
0149     if (!shouldChangeTextStyle ()) {
0150         return;
0151     }
0152 
0153     kpTextStyle newTextStyle = environ ()->textStyle ();
0154 
0155     kpTextStyle oldTextStyle = newTextStyle;
0156     oldTextStyle.setForegroundColor (oldForegroundColor ());
0157 
0158     changeTextStyle (i18n ("Text: Foreground Color"),
0159                      newTextStyle,
0160                      oldTextStyle);
0161 }
0162 
0163 // protected slot virtual [base kpAbstractSelectionTool]
0164 void kpToolText::slotBackgroundColorChanged (const kpColor & /*color*/)
0165 {
0166 #if DEBUG_KP_TOOL_TEXT
0167     qCDebug(kpLogTools) << "kpToolText::slotBackgroundColorChanged()";
0168 #endif
0169 
0170     if (!shouldChangeTextStyle ()) {
0171         return;
0172     }
0173 
0174     kpTextStyle newTextStyle = environ ()->textStyle ();
0175 
0176     kpTextStyle oldTextStyle = newTextStyle;
0177     oldTextStyle.setBackgroundColor (oldBackgroundColor ());
0178 
0179     changeTextStyle (i18n ("Text: Background Color"),
0180                      newTextStyle,
0181                      oldTextStyle);
0182 }
0183 
0184 // protected slot virtual [base kpAbstractSelectionTool]
0185 void kpToolText::slotColorSimilarityChanged (double, int)
0186 {
0187     // --- don't pass on event to kpAbstractSelectionTool which would have set the
0188     //     SelectionTransparency - not relevant to the Text Tool ---
0189 }
0190 
0191 
0192 // public slot
0193 void kpToolText::slotFontFamilyChanged (const QString &fontFamily,
0194                                         const QString &oldFontFamily)
0195 {
0196 #if DEBUG_KP_TOOL_TEXT
0197     qCDebug(kpLogTools) << "kpToolText::slotFontFamilyChanged() new="
0198                << fontFamily
0199                << " old="
0200                << oldFontFamily;
0201 #else
0202     (void) fontFamily;
0203 #endif
0204 
0205     if (!shouldChangeTextStyle ()) {
0206         return;
0207     }
0208 
0209     kpTextStyle newTextStyle = environ ()->textStyle ();
0210 
0211     // Figure out old text style.
0212     kpTextStyle oldTextStyle = newTextStyle;
0213     oldTextStyle.setFontFamily (oldFontFamily);
0214 
0215     changeTextStyle (i18n ("Text: Font"),
0216                      newTextStyle,
0217                      oldTextStyle);
0218 }
0219 
0220 // public slot
0221 void kpToolText::slotFontSizeChanged (int fontSize, int oldFontSize)
0222 {
0223 #if DEBUG_KP_TOOL_TEXT
0224     qCDebug(kpLogTools) << "kpToolText::slotFontSizeChanged() new="
0225                << fontSize
0226                << " old="
0227                << oldFontSize;
0228 #else
0229     (void) fontSize;
0230 #endif
0231 
0232     if (!shouldChangeTextStyle ()) {
0233         return;
0234     }
0235 
0236     kpTextStyle newTextStyle = environ ()->textStyle ();
0237 
0238     // Figure out old text style.
0239     kpTextStyle oldTextStyle = newTextStyle;
0240     oldTextStyle.setFontSize (oldFontSize);
0241 
0242     changeTextStyle (i18n ("Text: Font Size"),
0243                      newTextStyle,
0244                      oldTextStyle);
0245 }
0246 
0247 
0248 // public slot
0249 void kpToolText::slotBoldChanged (bool isBold)
0250 {
0251 #if DEBUG_KP_TOOL_TEXT
0252     qCDebug(kpLogTools) << "kpToolText::slotBoldChanged(" << isBold << ")";
0253 #endif
0254 
0255     if (!shouldChangeTextStyle ()) {
0256         return;
0257     }
0258 
0259     kpTextStyle newTextStyle = environ ()->textStyle ();
0260 
0261     // Figure out old text style.
0262     kpTextStyle oldTextStyle = newTextStyle;
0263     oldTextStyle.setBold (!isBold);
0264 
0265     changeTextStyle (i18n ("Text: Bold"),
0266                      newTextStyle,
0267                      oldTextStyle);
0268 }
0269 
0270 // public slot
0271 void kpToolText::slotItalicChanged (bool isItalic)
0272 {
0273 #if DEBUG_KP_TOOL_TEXT
0274     qCDebug(kpLogTools) << "kpToolText::slotItalicChanged(" << isItalic << ")";
0275 #endif
0276 
0277     if (!shouldChangeTextStyle ()) {
0278         return;
0279     }
0280 
0281     kpTextStyle newTextStyle = environ ()->textStyle ();
0282 
0283     // Figure out old text style.
0284     kpTextStyle oldTextStyle = newTextStyle;
0285     oldTextStyle.setItalic (!isItalic);
0286 
0287     changeTextStyle (i18n ("Text: Italic"),
0288                      newTextStyle,
0289                      oldTextStyle);
0290 }
0291 
0292 // public slot
0293 void kpToolText::slotUnderlineChanged (bool isUnderline)
0294 {
0295 #if DEBUG_KP_TOOL_TEXT
0296     qCDebug(kpLogTools) << "kpToolText::slotUnderlineChanged(" << isUnderline << ")";
0297 #endif
0298 
0299     if (!shouldChangeTextStyle ()) {
0300         return;
0301     }
0302 
0303     kpTextStyle newTextStyle = environ ()->textStyle ();
0304 
0305     // Figure out old text style.
0306     kpTextStyle oldTextStyle = newTextStyle;
0307     oldTextStyle.setUnderline (!isUnderline);
0308 
0309     changeTextStyle (i18n ("Text: Underline"),
0310                      newTextStyle,
0311                      oldTextStyle);
0312 }
0313 
0314 // public slot
0315 void kpToolText::slotStrikeThruChanged (bool isStrikeThru)
0316 {
0317 #if DEBUG_KP_TOOL_TEXT
0318     qCDebug(kpLogTools) << "kpToolText::slotStrikeThruChanged(" << isStrikeThru << ")";
0319 #endif
0320 
0321     if (!shouldChangeTextStyle ()) {
0322         return;
0323     }
0324 
0325     kpTextStyle newTextStyle = environ ()->textStyle ();
0326 
0327     // Figure out old text style.
0328     kpTextStyle oldTextStyle = newTextStyle;
0329     oldTextStyle.setStrikeThru (!isStrikeThru);
0330 
0331     changeTextStyle (i18n ("Text: Strike Through"),
0332                      newTextStyle,
0333                      oldTextStyle);
0334 }