File indexing completed on 2024-05-12 04:21:15

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 
0029 #define DEBUG_KP_DOCUMENT_ENVIRONMENT 0
0030 
0031 
0032 #include "environments/document/kpDocumentEnvironment.h"
0033 
0034 #include "kpLogCategories.h"
0035 
0036 #include "mainWindow/kpMainWindow.h"
0037 #include "layers/selections/kpAbstractSelection.h"
0038 #include "document/kpDocument.h"
0039 #include "layers/selections/image/kpEllipticalImageSelection.h"
0040 #include "layers/selections/image/kpFreeFormImageSelection.h"
0041 #include "layers/selections/image/kpImageSelectionTransparency.h"
0042 #include "layers/selections/image/kpRectangularImageSelection.h"
0043 #include "layers/selections/text/kpTextSelection.h"
0044 #include "layers/selections/text/kpTextStyle.h"
0045 #if DEBUG_KP_DOCUMENT_ENVIRONMENT
0046     #include "tools/kpTool.h"
0047 #endif
0048 #include "views/manager/kpViewManager.h"
0049 
0050 
0051 struct kpDocumentEnvironmentPrivate
0052 {
0053 };
0054 
0055 kpDocumentEnvironment::kpDocumentEnvironment (kpMainWindow *mainWindow)
0056     : kpEnvironmentBase (mainWindow),
0057       d (new kpDocumentEnvironmentPrivate ())
0058 {
0059 }
0060 
0061 kpDocumentEnvironment::~kpDocumentEnvironment ()
0062 {
0063     delete d;
0064 }
0065 
0066 
0067 // public
0068 QWidget *kpDocumentEnvironment::dialogParent () const
0069 {
0070     return mainWindow ();
0071 }
0072 
0073 
0074 static kpViewManager *ViewManager (kpMainWindow *mw)
0075 {
0076     return mw->viewManager ();
0077 }
0078 
0079 // public
0080 void kpDocumentEnvironment::setQueueViewUpdates () const
0081 {
0082     ::ViewManager (mainWindow ())->setQueueUpdates ();
0083 }
0084 
0085 // public
0086 void kpDocumentEnvironment::restoreQueueViewUpdates () const
0087 {
0088     ::ViewManager (mainWindow ())->restoreQueueUpdates ();
0089 }
0090 
0091 //---------------------------------------------------------------------
0092 
0093 // public
0094 void kpDocumentEnvironment::switchToCompatibleTool (const kpAbstractSelection &selection,
0095         bool *isTextChanged) const
0096 {
0097 #if DEBUG_KP_DOCUMENT_ENVIRONMENT
0098     qCDebug(kpLogEnvironments) << "kpDocumentEnvironment::switchToCompatibleTool("
0099               << &selection << ")"
0100               << " mainwindow.tool="
0101               << (mainWindow ()->tool () ? mainWindow ()->tool ()->objectName () : nullptr)
0102               << " mainWindow.toolIsTextTool=" << mainWindow ()->toolIsTextTool ()
0103               << " current selection="
0104               << document ()->selection ()
0105               << " new selection is text="
0106               << dynamic_cast <const kpTextSelection *> (&selection);
0107 #endif
0108 
0109     *isTextChanged = (mainWindow ()->toolIsTextTool () !=
0110                      (dynamic_cast <const kpTextSelection *> (&selection) != nullptr));
0111 
0112     // We don't change the Selection Tool if the new selection's
0113     // shape is merely different to the current tool's (e.g. rectangular
0114     // vs elliptical) because:
0115     //
0116     // 1. All image selection tools support editing selections of all the
0117     //    different shapes anyway.
0118     // 2. Suppose the user is trying out different drags of selection borders
0119     //    and then decides to paste a differently shaped selection before continuing
0120     //    to try out different borders.  If the pasting were to switch to
0121     //    a differently shaped tool, the borders drawn after the paste would
0122     //    be using a new shape rather than the shape before the paste.  This
0123     //    could get irritating so we don't do the switch.
0124     if (!mainWindow ()->toolIsASelectionTool () || *isTextChanged)
0125     {
0126         // See kpDocument::setSelection() APIDoc for this assumption.
0127         Q_ASSERT (!document ()->selection ());
0128 
0129         // Switch to the appropriately shaped selection tool
0130         // _before_ we change the selection
0131         // (all selection tool's ::end() functions nuke the current selection)
0132         if (dynamic_cast <const kpRectangularImageSelection *> (&selection))
0133         {
0134         #if DEBUG_KP_DOCUMENT_ENVIRONMENT
0135             qCDebug(kpLogEnvironments) << "\tswitch to rect selection tool";
0136         #endif
0137             mainWindow ()->slotToolRectSelection ();
0138         }
0139         else if (dynamic_cast <const kpEllipticalImageSelection *> (&selection))
0140         {
0141         #if DEBUG_KP_DOCUMENT_ENVIRONMENT
0142             qCDebug(kpLogEnvironments) << "\tswitch to elliptical selection tool";
0143         #endif
0144             mainWindow ()->slotToolEllipticalSelection ();
0145         }
0146         else if (dynamic_cast <const kpFreeFormImageSelection *> (&selection))
0147         {
0148         #if DEBUG_KP_DOCUMENT_ENVIRONMENT
0149             qCDebug(kpLogEnvironments) << "\tswitch to free form selection tool";
0150         #endif
0151             mainWindow ()->slotToolFreeFormSelection ();
0152         }
0153         else if (dynamic_cast <const kpTextSelection *> (&selection))
0154         {
0155         #if DEBUG_KP_DOCUMENT_ENVIRONMENT
0156             qCDebug(kpLogEnvironments) << "\tswitch to text selection tool";
0157         #endif
0158             mainWindow ()->slotToolText ();
0159         }
0160         else {
0161             Q_ASSERT (!"Unknown selection type");
0162         }
0163     }
0164 
0165 #if DEBUG_KP_DOCUMENT_ENVIRONMENT
0166     qCDebug(kpLogEnvironments) << "kpDocumentEnvironment::switchToCompatibleTool(" << &selection << ") finished";
0167 #endif
0168 }
0169 
0170 //---------------------------------------------------------------------
0171 
0172 // public
0173 void kpDocumentEnvironment::assertMatchingUIState (const kpAbstractSelection &selection) const
0174 {
0175     // Trap and try to recover from bugs.
0176     // TODO: See kpDocument::setSelection() API comment and determine best fix.
0177     const auto *imageSelection = dynamic_cast <const kpAbstractImageSelection *> (&selection);
0178     const auto *textSelection = dynamic_cast <const kpTextSelection *> (&selection);
0179 
0180     if (imageSelection)
0181     {
0182         if (imageSelection->transparency () != mainWindow ()->imageSelectionTransparency ())
0183         {
0184             qCCritical(kpLogEnvironments) << "kpDocument::setSelection() sel's transparency differs "
0185                           "from mainWindow's transparency - setting mainWindow's transparency "
0186                           "to sel";
0187 
0188             qCCritical(kpLogEnvironments) << "\tisOpaque: sel=" << imageSelection->transparency ().isOpaque ()
0189                        << " mainWindow=" << mainWindow ()->imageSelectionTransparency ().isOpaque ();
0190 
0191             mainWindow ()->setImageSelectionTransparency (imageSelection->transparency ());
0192         }
0193     }
0194     else if (textSelection)
0195     {
0196         if (textSelection->textStyle () != mainWindow ()->textStyle ())
0197         {
0198             qCCritical(kpLogEnvironments) << "kpDocument::setSelection() sel's textStyle differs "
0199                           "from mainWindow's textStyle - setting mainWindow's textStyle "
0200                           "to sel";
0201 
0202             mainWindow ()->setTextStyle (textSelection->textStyle ());
0203         }
0204     }
0205     else
0206     {
0207         Q_ASSERT (!"Unknown selection type");
0208     }
0209 }
0210 
0211 #include "moc_kpDocumentEnvironment.cpp"