LibrePCB Developers Documentation
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_EDITOR_CMDLISTELEMENTINSERT_H
21#define LIBREPCB_EDITOR_CMDLISTELEMENTINSERT_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include "../undocommand.h"
27
29
30#include <QtCore>
31
32/*******************************************************************************
33 * Namespace / Forward Declarations
34 ******************************************************************************/
35namespace librepcb {
36namespace editor {
37
38/*******************************************************************************
39 * Class CmdListElementInsert
40 ******************************************************************************/
41
45template <typename T, typename P, typename... OnEditedArgs>
46class CmdListElementInsert final : public UndoCommand {
47public:
48 // Constructors / Destructor
52 const std::shared_ptr<T>& element,
53 int index = -1) noexcept
54 : UndoCommand(tr("Add %1").arg(P::tagname)),
55 mList(list),
56 mElement(element),
57 mIndex(index) {}
59
60 // Operator Overloadings
62
63private: // Methods
65 bool performExecute() override {
66 if (mIndex < 0) mIndex = mList.count();
67 performRedo(); // can throw
68 return true;
69 }
70
72 void performUndo() override { mList.remove(mIndex); }
73
75 void performRedo() override { mIndex = mList.insert(mIndex, mElement); }
76
77private: // Data
78 SerializableObjectList<T, P, OnEditedArgs...>& mList;
79 std::shared_ptr<T> mElement;
80 int mIndex;
81};
82
83/*******************************************************************************
84 * End of File
85 ******************************************************************************/
86
87} // namespace editor
88} // namespace librepcb
89
90#endif
int count() const noexcept
Definition: serializableobjectlist.h:199
void remove(int index) noexcept
Definition: serializableobjectlist.h:362
int insert(int index, const std::shared_ptr< T > &obj) noexcept
Definition: serializableobjectlist.h:336
The CmdListElementInsert class.
Definition: cmdlistelementinsert.h:46
void performRedo() override
Redo the command.
Definition: cmdlistelementinsert.h:75
CmdListElementInsert(const CmdListElementInsert &other)=delete
int mIndex
Definition: cmdlistelementinsert.h:80
~CmdListElementInsert() noexcept
Definition: cmdlistelementinsert.h:58
CmdListElementInsert & operator=(const CmdListElementInsert &rhs)=delete
void performUndo() override
Undo the command.
Definition: cmdlistelementinsert.h:72
CmdListElementInsert(SerializableObjectList< T, P, OnEditedArgs... > &list, const std::shared_ptr< T > &element, int index=-1) noexcept
Definition: cmdlistelementinsert.h:51
std::shared_ptr< T > mElement
Definition: cmdlistelementinsert.h:79
SerializableObjectList< T, P, OnEditedArgs... > & mList
Definition: cmdlistelementinsert.h:78
bool performExecute() override
Execute the command the first time.
Definition: cmdlistelementinsert.h:65
The UndoCommand class represents a command which you can undo/redo.
Definition: undocommand.h:46
Definition: occmodel.cpp:77