File indexing completed on 2024-12-22 04:07:24

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_TOOL_SELECTION 0
0030 
0031 
0032 #include "kpToolSelectionDestroyCommand.h"
0033 #include "kpLogCategories.h"
0034 #include "layers/selections/kpAbstractSelection.h"
0035 #include "layers/selections/image/kpAbstractImageSelection.h"
0036 #include "environments/commands/kpCommandEnvironment.h"
0037 #include "kpDefs.h"
0038 #include "document/kpDocument.h"
0039 #include "layers/selections/text/kpTextSelection.h"
0040 #include "views/manager/kpViewManager.h"
0041 
0042 //---------------------------------------------------------------------
0043 
0044 kpToolSelectionDestroyCommand::kpToolSelectionDestroyCommand (const QString &name,
0045         bool pushOntoDocument,
0046         kpCommandEnvironment *environ)
0047     : kpNamedCommand (name, environ),
0048       m_pushOntoDocument (pushOntoDocument),
0049       m_oldSelectionPtr (nullptr),
0050       m_textRow(0), m_textCol(0)
0051 {
0052 }
0053 
0054 //---------------------------------------------------------------------
0055 
0056 kpToolSelectionDestroyCommand::~kpToolSelectionDestroyCommand ()
0057 {
0058     delete m_oldSelectionPtr;
0059 }
0060 
0061 //---------------------------------------------------------------------
0062 
0063 // public virtual [base kpCommand]
0064 kpCommandSize::SizeType kpToolSelectionDestroyCommand::size () const
0065 {
0066     return ImageSize (m_oldDocImage) +
0067            SelectionSize (m_oldSelectionPtr);
0068 }
0069 
0070 //---------------------------------------------------------------------
0071 
0072 // public virtual [base kpCommand]
0073 void kpToolSelectionDestroyCommand::execute ()
0074 {
0075 #if DEBUG_KP_TOOL_SELECTION
0076     qCDebug(kpLogCommands) << "kpToolSelectionDestroyCommand::execute () CALLED";
0077 #endif
0078 
0079     kpDocument *doc = document ();
0080     Q_ASSERT (doc);
0081     Q_ASSERT (doc->selection ());
0082 
0083     m_textRow = viewManager ()->textCursorRow ();
0084     m_textCol = viewManager ()->textCursorCol ();
0085 
0086     Q_ASSERT (!m_oldSelectionPtr);
0087     m_oldSelectionPtr = doc->selection ()->clone ();
0088 
0089     if (m_pushOntoDocument)
0090     {
0091         m_oldDocImage = doc->getImageAt (doc->selection ()->boundingRect ());
0092         doc->selectionPushOntoDocument ();
0093     }
0094     else {
0095         doc->selectionDelete ();
0096     }
0097 
0098     environ ()->somethingBelowTheCursorChanged ();
0099 }
0100 
0101 //---------------------------------------------------------------------
0102 
0103 // public virtual [base kpCommand]
0104 void kpToolSelectionDestroyCommand::unexecute ()
0105 {
0106 #if DEBUG_KP_TOOL_SELECTION
0107     qCDebug(kpLogCommands) << "kpToolSelectionDestroyCommand::unexecute () CALLED";
0108 #endif
0109 
0110     kpDocument *doc = document ();
0111     Q_ASSERT (doc);
0112 
0113     if (doc->selection ())
0114     {
0115         // not error because it's possible that the user dragged out a new
0116         // region (without pulling image), and then CTRL+Z
0117     #if DEBUG_KP_TOOL_SELECTION
0118         qCDebug(kpLogCommands) << "kpToolSelectionDestroyCommand::unexecute() already has sel region";
0119     #endif
0120 
0121         if (doc->selection ()->hasContent ())
0122         {
0123             Q_ASSERT (!"kpToolSelectionDestroyCommand::unexecute() already has sel content");
0124             return;
0125         }
0126     }
0127 
0128     Q_ASSERT (m_oldSelectionPtr);
0129 
0130     if (m_pushOntoDocument)
0131     {
0132     #if DEBUG_KP_TOOL_SELECTION
0133         qCDebug(kpLogCommands) << "\tunpush oldDocImage onto doc first";
0134     #endif
0135         doc->setImageAt (m_oldDocImage, m_oldSelectionPtr->topLeft ());
0136     }
0137 
0138 #if DEBUG_KP_TOOL_SELECTION
0139     qCDebug(kpLogCommands) << "\tsetting selection to: rect=" << m_oldSelectionPtr->boundingRect ()
0140                << " hasContent=" << m_oldSelectionPtr->hasContent ();
0141 #endif
0142     kpAbstractImageSelection *imageSel =
0143         dynamic_cast <kpAbstractImageSelection *> (m_oldSelectionPtr);
0144     kpTextSelection *textSel =
0145         dynamic_cast <kpTextSelection *> (m_oldSelectionPtr);
0146     if (imageSel)
0147     {
0148         if (imageSel->transparency () != environ ()->imageSelectionTransparency ()) {
0149             environ ()->setImageSelectionTransparency (imageSel->transparency ());
0150         }
0151         if (dynamic_cast <kpTextSelection *> (doc->selection())) {
0152             doc->selectionPushOntoDocument();
0153         }
0154     }
0155     else if (textSel)
0156     {
0157         if (textSel->textStyle () != environ ()->textStyle ()) {
0158             environ ()->setTextStyle (textSel->textStyle ());
0159         }
0160         if (dynamic_cast <kpAbstractImageSelection *> (doc->selection())) {
0161             doc->selectionPushOntoDocument();
0162         }
0163     }
0164     else {
0165         Q_ASSERT (!"Unknown selection type");
0166     }
0167 
0168     viewManager ()->setTextCursorPosition (m_textRow, m_textCol);
0169     doc->setSelection (*m_oldSelectionPtr);
0170 
0171     environ ()->somethingBelowTheCursorChanged ();
0172 
0173     delete m_oldSelectionPtr;
0174     m_oldSelectionPtr = nullptr;
0175 }
0176