1 /* This file is part of the KDE project
2    Copyright 2005,2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public License
15    along with this library; see the file COPYING.LIB.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17    Boston, MA 02110-1301, USA.
18 */
19 
20 #ifndef CALLIGRA_SHEETS_ABSTRACT_REGION_COMMAND
21 #define CALLIGRA_SHEETS_ABSTRACT_REGION_COMMAND
22 
23 #include <kundo2command.h>
24 
25 #include "Region.h"
26 
27 #include "sheets_common_export.h"
28 
29 class KoCanvasBase;
30 
31 namespace Calligra
32 {
33 namespace Sheets
34 {
35 class Sheet;
36 
37 /**
38  * \class AbstractRegionCommand
39  * \ingroup Commands
40  * \brief Abstract base class for all region related operations.
41  */
42 class CALLIGRA_SHEETS_COMMON_EXPORT AbstractRegionCommand : public Region, public KUndo2Command
43 {
44 public:
45     /**
46      * Constructor.
47      */
48     explicit AbstractRegionCommand(KUndo2Command *parent = 0);
49 
50     /**
51      * Destructor.
52      */
53     ~AbstractRegionCommand() override;
54 
55     /**
56      * \return the Sheet this AbstractRegionCommand works on
57      */
sheet()58     Sheet* sheet() const {
59         return m_sheet;
60     }
61 
62     /**
63      * Sets \p sheet to be the Sheet to work on.
64      */
setSheet(Sheet * sheet)65     void setSheet(Sheet* sheet) {
66         m_sheet = sheet;
67     }
68 
69     /**
70      * Executes the actual operation and adds the manipulator to the undo history, if desired.
71      * \return \c true if the command was executed successfully
72      * \return \c false if the command fails, was already executed once or is not approved
73      * \see setRegisterUndo, isApproved
74      */
75     virtual bool execute(KoCanvasBase* canvas = 0);
76 
77     /**
78      * Executes the actual operation.
79      */
80     void redo() override;
81 
82     /**
83      * Executes the actual operation in reverse order.
84      */
85     void undo() override;
86 
87     /**
88      * Sets reverse mode to \b reverse .
89      * \see redo
90      * \see undo
91      */
setReverse(bool reverse)92     virtual void setReverse(bool reverse) {
93         m_reverse = reverse;
94     }
95 
96     /**
97      * If \p registerUndo is \c true , this manipulator registers an
98      * undo operation for the document.
99      */
setRegisterUndo(bool registerUndo)100     void setRegisterUndo(bool registerUndo) {
101         m_register = registerUndo;
102     }
103 
104 protected:
105     /**
106      * Processes \p element , a Region::Point or a Region::Range .
107      * Invoked by mainProcessing() .
108      */
process(Element *)109     virtual bool process(Element*) {
110         return true;
111     }
112 
113     /**
114      * Preprocessing of the region.
115      */
preProcessing()116     virtual bool preProcessing() {
117         return true;
118     }
119 
120     /**
121      * Processes the region. Calls process(Element*).
122      */
123     virtual bool mainProcessing();
124 
125     /**
126      * Postprocessing of the region.
127      */
postProcessing()128     virtual bool postProcessing() {
129         return true;
130     }
131 
132     /**
133      * Checks all cells, that should be processed, for protection and matrix locks.
134      * \return \c true if execution is approved
135      * \return \c false otherwise
136      */
137     bool isApproved() const;
138 
139 protected:
140     Sheet*  m_sheet;
141     bool    m_reverse   : 1;
142     bool    m_firstrun  : 1;
143     bool    m_register  : 1;
144     bool    m_success   : 1;
145     bool    m_checkLock : 1;
146 };
147 
148 } // namespace Sheets
149 } // namespace Calligra
150 
151 #endif // CALLIGRA_SHEETS_ABSTRACT_REGION_COMMAND
152