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 "commands/kpMacroCommand.h"
0033 #include "views/manager/kpViewManager.h"
0034 
0035 #include <climits>
0036 
0037 #include <QtAlgorithms>
0038 
0039 //---------------------------------------------------------------------
0040 
0041 kpMacroCommand::kpMacroCommand (const QString &name, kpCommandEnvironment *environ)
0042     : kpNamedCommand (name, environ)
0043 {
0044 }
0045 
0046 //---------------------------------------------------------------------
0047 
0048 kpMacroCommand::~kpMacroCommand ()
0049 {
0050     qDeleteAll(m_commandList);
0051 }
0052 
0053 //---------------------------------------------------------------------
0054 
0055 // public virtual [base kpCommand]
0056 kpCommandSize::SizeType kpMacroCommand::size () const
0057 {
0058 #if DEBUG_KP_COMMAND_HISTORY && 0
0059     qCDebug(kpLogCommands) << "kpMacroCommand::size()";
0060 #endif
0061     SizeType s = 0;
0062 
0063 #if DEBUG_KP_COMMAND_HISTORY && 0
0064     qCDebug(kpLogCommands) << "\tcalculating:";
0065 #endif
0066     for (kpCommand *cmd : m_commandList)
0067     {
0068     #if DEBUG_KP_COMMAND_HISTORY && 0
0069         qCDebug(kpLogCommands) << "\t\tcurrentSize=" << s << " + "
0070                    << cmd->name () << ".size=" << cmd->size ();
0071     #endif
0072         s += cmd->size ();
0073     }
0074 
0075 #if DEBUG_KP_COMMAND_HISTORY && 0
0076     qCDebug(kpLogCommands) << "\treturning " << s;
0077 #endif
0078     return s;
0079 }
0080 
0081 //---------------------------------------------------------------------
0082 
0083 // public virtual [base kpCommand]
0084 void kpMacroCommand::execute ()
0085 {
0086 #if DEBUG_KP_COMMAND_HISTORY
0087     qCDebug(kpLogCommands) << "kpMacroCommand::execute()";
0088 #endif
0089 
0090     viewManager()->setQueueUpdates();
0091 
0092     for (kpCommand *command : std::as_const(m_commandList))
0093     {
0094     #if DEBUG_KP_COMMAND_HISTORY
0095         qCDebug(kpLogCommands) << "\texecuting " << command->name();
0096     #endif
0097         command->execute();
0098     }
0099 
0100     viewManager()->restoreQueueUpdates();
0101 }
0102 
0103 //---------------------------------------------------------------------
0104 
0105 // public virtual [base kpCommand]
0106 void kpMacroCommand::unexecute ()
0107 {
0108 #if DEBUG_KP_COMMAND_HISTORY
0109     qCDebug(kpLogCommands) << "kpMacroCommand::unexecute()";
0110 #endif
0111 
0112     viewManager()->setQueueUpdates();
0113 
0114     for (int i = m_commandList.count() - 1; i >= 0; i--)
0115     {
0116     #if DEBUG_KP_COMMAND_HISTORY
0117         qCDebug(kpLogCommands) << "\tunexecuting " << m_commandList[i]->name();
0118     #endif
0119         m_commandList[i]->unexecute();
0120     }
0121 
0122     viewManager()->restoreQueueUpdates();
0123 }
0124 
0125 //---------------------------------------------------------------------
0126 
0127 // public
0128 void kpMacroCommand::addCommand(kpCommand *command)
0129 {
0130     m_commandList.append(command);
0131 }
0132 
0133 //---------------------------------------------------------------------