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 "commands/tools/selection/kpToolSelectionCreateCommand.h"
0033 
0034 #include "layers/selections/kpAbstractSelection.h"
0035 #include "layers/selections/image/kpAbstractImageSelection.h"
0036 #include "environments/commands/kpCommandEnvironment.h"
0037 #include "commands/kpCommandHistory.h"
0038 #include "kpDefs.h"
0039 #include "document/kpDocument.h"
0040 #include "layers/selections/text/kpTextSelection.h"
0041 #include "widgets/toolbars/options/kpToolWidgetOpaqueOrTransparent.h"
0042 #include "views/kpView.h"
0043 #include "views/manager/kpViewManager.h"
0044 
0045 #include "kpLogCategories.h"
0046 
0047 
0048 
0049 kpToolSelectionCreateCommand::kpToolSelectionCreateCommand (const QString &name,
0050         const kpAbstractSelection &fromSelection,
0051         kpCommandEnvironment *environ)
0052     : kpNamedCommand (name, environ),
0053       m_fromSelection (nullptr),
0054       m_textRow (0), m_textCol (0)
0055 {
0056     setFromSelection (fromSelection);
0057 }
0058 
0059 kpToolSelectionCreateCommand::~kpToolSelectionCreateCommand ()
0060 {
0061     delete m_fromSelection;
0062 }
0063 
0064 
0065 // public virtual [base kpCommand]
0066 kpCommandSize::SizeType kpToolSelectionCreateCommand::size () const
0067 {
0068     return SelectionSize (m_fromSelection);
0069 }
0070 
0071 
0072 // public
0073 const kpAbstractSelection *kpToolSelectionCreateCommand::fromSelection () const
0074 {
0075     return m_fromSelection;
0076 }
0077 
0078 // public
0079 void kpToolSelectionCreateCommand::setFromSelection (const kpAbstractSelection &fromSelection)
0080 {
0081     delete m_fromSelection;
0082     m_fromSelection = fromSelection.clone ();
0083 }
0084 
0085 // public virtual [base kpCommand]
0086 void kpToolSelectionCreateCommand::execute ()
0087 {
0088 #if DEBUG_KP_TOOL_SELECTION
0089     qCDebug(kpLogCommands) << "kpToolSelectionCreateCommand::execute()";
0090 #endif
0091 
0092     kpDocument *doc = document ();
0093     Q_ASSERT (doc);
0094 
0095     if (m_fromSelection)
0096     {
0097     #if DEBUG_KP_TOOL_SELECTION
0098         qCDebug(kpLogCommands) << "\tusing fromSelection";
0099         qCDebug(kpLogCommands) << "\t\thave sel=" << doc->selection () << endl;
0100     #endif
0101         kpAbstractImageSelection *imageSel =
0102             dynamic_cast <kpAbstractImageSelection *> (m_fromSelection);
0103         kpTextSelection *textSel =
0104             dynamic_cast <kpTextSelection *> (m_fromSelection);
0105         if (imageSel)
0106         {
0107             if (imageSel->transparency () != environ ()->imageSelectionTransparency ()) {
0108                 environ ()->setImageSelectionTransparency (imageSel->transparency ());
0109             }
0110         }
0111         else if (textSel)
0112         {
0113             if (textSel->textStyle () != environ ()->textStyle ()) {
0114                 environ ()->setTextStyle (textSel->textStyle ());
0115             }
0116         }
0117         else {
0118             Q_ASSERT (!"Unknown selection type");
0119         }
0120 
0121         viewManager ()->setTextCursorPosition (m_textRow, m_textCol);
0122         doc->setSelection (*m_fromSelection);
0123 
0124         environ ()->somethingBelowTheCursorChanged ();
0125     }
0126 }
0127 
0128 // public virtual [base kpCommand]
0129 void kpToolSelectionCreateCommand::unexecute ()
0130 {
0131     kpDocument *doc = document ();
0132     Q_ASSERT (doc);
0133 
0134     if (!doc->selection ())
0135     {
0136         // Was just a border that got deselected?
0137         if (m_fromSelection && !m_fromSelection->hasContent ()) {
0138             return;
0139         }
0140 
0141         Q_ASSERT (!"kpToolSelectionCreateCommand::unexecute() without sel region");
0142         return;
0143     }
0144 
0145     m_textRow = viewManager ()->textCursorRow ();
0146     m_textCol = viewManager ()->textCursorCol ();
0147 
0148     doc->selectionDelete ();
0149 
0150     environ ()->somethingBelowTheCursorChanged ();
0151 }
0152