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 "kpToolSelectionMoveCommand.h"
0033 
0034 #include "layers/selections/kpAbstractSelection.h"
0035 #include "environments/commands/kpCommandEnvironment.h"
0036 #include "kpDefs.h"
0037 #include "document/kpDocument.h"
0038 #include "tools/kpTool.h"
0039 #include "widgets/toolbars/options/kpToolWidgetOpaqueOrTransparent.h"
0040 #include "views/manager/kpViewManager.h"
0041 
0042 #include "kpLogCategories.h"
0043 
0044 //--------------------------------------------------------------------------------
0045 
0046 kpToolSelectionMoveCommand::kpToolSelectionMoveCommand (const QString &name,
0047         kpCommandEnvironment *environ)
0048     : kpNamedCommand (name, environ)
0049 {
0050     kpDocument *doc = document ();
0051     Q_ASSERT (doc);
0052     Q_ASSERT (doc->selection ());
0053 
0054     m_startPoint = m_endPoint = doc->selection ()->topLeft ();
0055 }
0056 
0057 kpToolSelectionMoveCommand::~kpToolSelectionMoveCommand () = default;
0058 
0059 
0060 // public
0061 kpAbstractSelection *kpToolSelectionMoveCommand::originalSelectionClone () const
0062 {
0063     kpDocument *doc = document ();
0064     Q_ASSERT (doc);
0065     Q_ASSERT (doc->selection ());
0066 
0067     kpAbstractSelection *selection = doc->selection ()->clone ();
0068     selection->moveTo (m_startPoint);
0069 
0070     return selection;
0071 }
0072 
0073 
0074 // public virtual [base kpComand]
0075 kpCommandSize::SizeType kpToolSelectionMoveCommand::size () const
0076 {
0077     return ImageSize (m_oldDocumentImage) +
0078            PolygonSize (m_copyOntoDocumentPoints);
0079 }
0080 
0081 
0082 // public virtual [base kpCommand]
0083 void kpToolSelectionMoveCommand::execute ()
0084 {
0085 #if DEBUG_KP_TOOL_SELECTION && 1
0086     qCDebug(kpLogCommands) << "kpToolSelectionMoveCommand::execute()";
0087 #endif
0088 
0089     kpDocument *doc = document ();
0090     Q_ASSERT (doc);
0091 
0092     kpAbstractSelection *sel = doc->selection ();
0093     // Must have content before it can be moved.
0094     Q_ASSERT (sel && sel->hasContent ());
0095 
0096     kpViewManager *vm = viewManager ();
0097     Q_ASSERT (vm);
0098 
0099     vm->setQueueUpdates ();
0100     {
0101         for (const auto &p : m_copyOntoDocumentPoints)
0102         {
0103             sel->moveTo (p);
0104             doc->selectionCopyOntoDocument ();
0105         }
0106 
0107         sel->moveTo (m_endPoint);
0108 
0109         environ ()->somethingBelowTheCursorChanged ();
0110     }
0111     vm->restoreQueueUpdates ();
0112 }
0113 
0114 // public virtual [base kpCommand]
0115 void kpToolSelectionMoveCommand::unexecute ()
0116 {
0117 #if DEBUG_KP_TOOL_SELECTION && 1
0118     qCDebug(kpLogCommands) << "kpToolSelectionMoveCommand::unexecute()";
0119 #endif
0120 
0121     kpDocument *doc = document ();
0122     Q_ASSERT (doc);
0123 
0124     kpAbstractSelection *sel = doc->selection ();
0125     // Must have content before it can be un-moved.
0126     Q_ASSERT (sel && sel->hasContent ());
0127 
0128     kpViewManager *vm = viewManager ();
0129     Q_ASSERT (vm);
0130 
0131     vm->setQueueUpdates ();
0132 
0133     if (!m_oldDocumentImage.isNull ()) {
0134         doc->setImageAt (m_oldDocumentImage, m_documentBoundingRect.topLeft ());
0135     }
0136 
0137 #if DEBUG_KP_TOOL_SELECTION && 1
0138     qCDebug(kpLogCommands) << "\tmove to startPoint=" << m_startPoint;
0139 #endif
0140     sel->moveTo (m_startPoint);
0141 
0142     environ ()->somethingBelowTheCursorChanged ();
0143 
0144     vm->restoreQueueUpdates ();
0145 }
0146 
0147 // public
0148 void kpToolSelectionMoveCommand::moveTo (const QPoint &point, bool moveLater)
0149 {
0150 #if DEBUG_KP_TOOL_SELECTION && 0
0151     qCDebug(kpLogCommands) << "kpToolSelectionMoveCommand::moveTo" << point
0152                << " moveLater=" << moveLater;
0153 #endif
0154 
0155     if (!moveLater)
0156     {
0157         kpDocument *doc = document ();
0158         Q_ASSERT (doc);
0159 
0160         kpAbstractSelection *sel = doc->selection ();
0161         // Must have content before it can be moved.
0162         Q_ASSERT (sel && sel->hasContent ());
0163 
0164         if (point == sel->topLeft ()) {
0165             return;
0166         }
0167 
0168         sel->moveTo (point);
0169     }
0170 
0171     m_endPoint = point;
0172 }
0173 
0174 // public
0175 void kpToolSelectionMoveCommand::moveTo (int x, int y, bool moveLater)
0176 {
0177     moveTo (QPoint (x, y), moveLater);
0178 }
0179 
0180 // public
0181 void kpToolSelectionMoveCommand::copyOntoDocument ()
0182 {
0183 #if DEBUG_KP_TOOL_SELECTION
0184     qCDebug(kpLogCommands) << "kpToolSelectionMoveCommand::copyOntoDocument()";
0185 #endif
0186 
0187     kpDocument *doc = document ();
0188     Q_ASSERT (doc);
0189 
0190     kpAbstractSelection *sel = doc->selection ();
0191     // Must have content before we allow it be stamped onto the document,
0192     // to be consistent with the requirement on other selection operations.
0193     Q_ASSERT (sel && sel->hasContent ());
0194 
0195     if (m_oldDocumentImage.isNull ()) {
0196         m_oldDocumentImage = doc->image ();
0197     }
0198 
0199     QRect selBoundingRect = sel->boundingRect ();
0200     m_documentBoundingRect = m_documentBoundingRect.united (selBoundingRect);
0201 
0202     doc->selectionCopyOntoDocument ();
0203 
0204     m_copyOntoDocumentPoints.putPoints (m_copyOntoDocumentPoints.count (),
0205                                         1,
0206                                         selBoundingRect.x (),
0207                                         selBoundingRect.y ());
0208 }
0209 
0210 // public
0211 void kpToolSelectionMoveCommand::finalize ()
0212 {
0213     if (!m_oldDocumentImage.isNull () && !m_documentBoundingRect.isNull ())
0214     {
0215         m_oldDocumentImage = kpTool::neededPixmap (m_oldDocumentImage,
0216                                                     m_documentBoundingRect);
0217     }
0218 }
0219