LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cmdlistelementinsert.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_CMDLISTELEMENTINSERT
21 #define LIBREPCB_CMDLISTELEMENTINSERT
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include "../../undocommand.h"
27 #include "../serializableobjectlist.h"
28 
29 #include <QtCore>
30 
31 /*******************************************************************************
32  * Namespace / Forward Declarations
33  ******************************************************************************/
34 namespace librepcb {
35 
36 /*******************************************************************************
37  * Class CmdListElementInsert
38  ******************************************************************************/
39 
43 template <typename T, typename P, typename... OnEditedArgs>
44 class CmdListElementInsert final : public UndoCommand {
45 public:
46  // Constructors / Destructor
47  CmdListElementInsert() = delete;
48  CmdListElementInsert(const CmdListElementInsert& other) = delete;
50  const std::shared_ptr<T>& element,
51  int index = -1) noexcept
52  : UndoCommand(QString(tr("Add %1")).arg(P::tagname)),
53  mList(list),
54  mElement(element),
55  mIndex(index) {}
56  ~CmdListElementInsert() noexcept {}
57 
58  // Operator Overloadings
60 
61 private: // Methods
63  bool performExecute() override {
64  if (mIndex < 0) mIndex = mList.count();
65  performRedo(); // can throw
66  return true;
67  }
68 
70  void performUndo() override { mList.remove(mIndex); }
71 
73  void performRedo() override { mIndex = mList.insert(mIndex, mElement); }
74 
75 private: // Data
76  SerializableObjectList<T, P, OnEditedArgs...>& mList;
77  std::shared_ptr<T> mElement;
78  int mIndex;
79 };
80 
81 /*******************************************************************************
82  * End of File
83  ******************************************************************************/
84 
85 } // namespace librepcb
86 
87 #endif // LIBREPCB_CMDLISTELEMENTINSERT
void performRedo() override
Redo the command.
Definition: cmdlistelementinsert.h:73
bool performExecute() override
Execute the command the first time.
Definition: cmdlistelementinsert.h:63
CmdListElementInsert & operator=(const CmdListElementInsert &rhs)=delete
int count() const noexcept
Definition: serializableobjectlist.h:183
void performUndo() override
Undo the command.
Definition: cmdlistelementinsert.h:70
std::shared_ptr< T > mElement
Definition: cmdlistelementinsert.h:77
int mIndex
Definition: cmdlistelementinsert.h:78
CmdListElementInsert(SerializableObjectList< T, P, OnEditedArgs...> &list, const std::shared_ptr< T > &element, int index=-1) noexcept
Definition: cmdlistelementinsert.h:49
int insert(int index, const std::shared_ptr< T > &obj) noexcept
Definition: serializableobjectlist.h:315
void remove(int index) noexcept
Definition: serializableobjectlist.h:335
The UndoCommand class represents a command which you can undo/redo.
Definition: undocommand.h:47
~CmdListElementInsert() noexcept
Definition: cmdlistelementinsert.h:56
The CmdListElementInsert class.
Definition: cmdlistelementinsert.h:44
SerializableObjectList< T, P, OnEditedArgs...> & mList
Definition: cmdlistelementinsert.h:76