File indexing completed on 2024-04-28 04:20:11

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_COMMAND_HISTORY 0
0030 
0031 
0032 #include "kpCommandHistory.h"
0033 
0034 #include "layers/selections/kpAbstractSelection.h"
0035 #include "mainWindow/kpMainWindow.h"
0036 #include "tools/kpTool.h"
0037 #include "commands/tools/selection/kpToolSelectionCreateCommand.h"
0038 
0039 
0040 kpCommandHistory::kpCommandHistory (bool doReadConfig, kpMainWindow *mainWindow)
0041     : kpCommandHistoryBase (doReadConfig, mainWindow->actionCollection ()),
0042       m_mainWindow (mainWindow)
0043 {
0044 }
0045 
0046 kpCommandHistory::~kpCommandHistory () = default;
0047 
0048 
0049 static bool NextUndoCommandIsCreateBorder (kpCommandHistory *commandHistory)
0050 {
0051     Q_ASSERT (commandHistory);
0052 
0053     kpCommand *cmd = commandHistory->nextUndoCommand ();
0054     if (!cmd) {
0055         return false;
0056     }
0057 
0058     auto *c = dynamic_cast <kpToolSelectionCreateCommand *> (cmd);
0059     if (!c) {
0060         return false;
0061     }
0062 
0063     const kpAbstractSelection *sel = c->fromSelection ();
0064     Q_ASSERT (sel);
0065 
0066     return (!sel->hasContent ());
0067 }
0068 
0069 // public
0070 void kpCommandHistory::addCreateSelectionCommand (kpToolSelectionCreateCommand *cmd,
0071         bool execute)
0072 {
0073     if (cmd->fromSelection ()->hasContent ())
0074     {
0075         addCommand (cmd, execute);
0076         return;
0077     }
0078 
0079     if (::NextUndoCommandIsCreateBorder (this))
0080     {
0081         setNextUndoCommand (cmd);
0082         if (execute) {
0083             cmd->execute ();
0084         }
0085     }
0086     else {
0087         addCommand (cmd, execute);
0088     }
0089 }
0090 
0091 //---------------------------------------------------------------------
0092 
0093 // public slot virtual [base KCommandHistory]
0094 void kpCommandHistory::undo ()
0095 {
0096 #if DEBUG_KP_COMMAND_HISTORY
0097     qCDebug(kpLogCommands) << "kpCommandHistory::undo() CALLED!";
0098 #endif
0099     if (m_mainWindow && m_mainWindow->toolHasBegunShape ())
0100     {
0101     #if DEBUG_KP_COMMAND_HISTORY
0102         qCDebug(kpLogCommands) << "\thas begun shape - cancel draw";
0103     #endif
0104         m_mainWindow->tool ()->cancelShapeInternal ();
0105     }
0106     else {
0107         kpCommandHistoryBase::undo ();
0108     }
0109 }
0110 
0111 //---------------------------------------------------------------------
0112 
0113 // public slot virtual [base KCommandHistory]
0114 void kpCommandHistory::redo ()
0115 {
0116     if (m_mainWindow && m_mainWindow->toolHasBegunShape ())
0117     {
0118         // Not completely obvious but what else can we do?
0119         //
0120         // Ignoring the request would not be intuitive for tools like
0121         // Polygon & Polyline (where it's not always apparent to the user
0122         // that s/he's still drawing a shape even though the mouse isn't
0123         // down).
0124         m_mainWindow->tool ()->cancelShapeInternal ();
0125     }
0126     else {
0127         kpCommandHistoryBase::redo ();
0128     }
0129 }
0130 
0131 #include "moc_kpCommandHistory.cpp"