LibrePCB Developers Documentation
Loading...
Searching...
No Matches
uiobjectlist.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_UIOBJECTLIST_H
21#define LIBREPCB_EDITOR_UIOBJECTLIST_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
27
28#include <QtCore>
29
30#include <memory>
31#include <slint.h>
32
33/*******************************************************************************
34 * Namespace / Forward Declarations
35 ******************************************************************************/
36namespace librepcb {
37namespace editor {
38
39/*******************************************************************************
40 * Class UiObjectList
41 ******************************************************************************/
42
46template <typename TObj, typename TUiData>
47class UiObjectList : public slint::Model<TUiData> {
48public:
49 typedef TObj Element;
50 typedef TUiData UiData;
51
52 // Signals
53 enum class Event {
57 };
58 Signal<UiObjectList<TObj, TUiData>, int, const std::shared_ptr<TObj>&, Event>
60 typedef Slot<UiObjectList<TObj, TUiData>, int, const std::shared_ptr<TObj>&,
61 Event>
63
64 // Constructors / Destructor
65 UiObjectList() noexcept
66 : onEdited(*this),
67 mObjects(),
69 *this, &UiObjectList<TObj, TUiData>::elementUiDataChangedHandler) {}
70 UiObjectList(const UiObjectList& other) = delete;
71 ~UiObjectList() noexcept {}
72
73 // General Methods
74 int count() const noexcept { return mObjects.count(); }
75 bool isEmpty() const noexcept { return mObjects.isEmpty(); }
76 const std::shared_ptr<TObj>& at(int index) noexcept {
77 return mObjects.at(index);
78 }
79 std::shared_ptr<TObj> value(int index) noexcept {
80 return mObjects.value(index);
81 }
82 void append(const std::shared_ptr<TObj>& obj) noexcept {
83 insert(mObjects.count(), obj);
84 }
85 void insert(int index, const std::shared_ptr<TObj>& obj) noexcept {
86 Q_ASSERT(obj);
87 index = qBound(0, index, mObjects.count() + 1);
88 mObjects.insert(index, obj);
89 slint::Model<TUiData>::notify_row_added(index, 1);
90 obj->onUiDataChanged.attach(mOnUiDataChangedSlot);
91 onEdited.notify(index, obj, Event::ElementAdded);
92 }
93 bool remove(int index) noexcept { return takeAt(index) != nullptr; }
94 std::shared_ptr<TObj> take(const TObj* obj) noexcept {
95 if (auto index = indexOf(obj)) {
96 return takeAt(*index);
97 } else {
98 return nullptr;
99 }
100 }
101 std::shared_ptr<TObj> takeAt(int index) noexcept {
102 if (auto obj = mObjects.takeAt(index)) {
103 slint::Model<TUiData>::notify_row_removed(index, 1);
104 obj->onUiDataChanged.detach(mOnUiDataChangedSlot);
105 onEdited.notify(index, obj, Event::ElementRemoved);
106 return obj;
107 } else {
108 return nullptr;
109 }
110 }
111 void clear() noexcept {
112 for (int i = mObjects.count() - 1; i >= 0; --i) {
113 takeAt(i);
114 }
115 }
116 std::optional<int> indexOf(const TObj* obj) const noexcept {
117 for (int i = 0; i < mObjects.count(); ++i) {
118 if (mObjects.at(i).get() == obj) {
119 return i;
120 }
121 }
122 return std::nullopt;
123 }
124 const QVector<std::shared_ptr<TObj>>& values() { return mObjects; }
125 auto begin() noexcept { return mObjects.begin(); }
126 auto end() noexcept { return mObjects.end(); }
127
128 // Implementations
129 std::size_t row_count() const noexcept override {
130 return static_cast<std::size_t>(mObjects.count());
131 }
132 std::optional<TUiData> row_data(std::size_t i) const override {
133 if (auto obj = mObjects.value(i)) {
134 return obj->getUiData();
135 }
136 return std::nullopt;
137 }
138 void set_row_data(size_t i, const TUiData& data) noexcept override {
139 if (auto obj = mObjects.value(i)) {
140 obj->setUiData(data);
141 }
142 }
143
144 // Operator Overloadings
145 UiObjectList& operator=(const UiObjectList& rhs) = delete;
146
147private:
148 void elementUiDataChangedHandler(const TObj& obj) noexcept {
149 if (auto index = indexOf(&obj)) {
150 slint::Model<TUiData>::notify_row_changed(*index);
151 onEdited.notify(*index, at(*index), Event::ElementUiDataChanged);
152 }
153 }
154
155 QVector<std::shared_ptr<TObj>> mObjects;
157};
158
159/*******************************************************************************
160 * End of File
161 ******************************************************************************/
162
163} // namespace editor
164} // namespace librepcb
165
166#endif
The Signal class is used to emit signals on non-QObject derived classes.
Definition signalslot.h:65
The Slot class is used to receive signals from non-QObject derived classes.
Definition signalslot.h:170
The UiObjectList class.
Definition uiobjectlist.h:47
void clear() noexcept
Definition uiobjectlist.h:111
Slot< TObj > mOnUiDataChangedSlot
Definition uiobjectlist.h:156
std::size_t row_count() const noexcept override
Definition uiobjectlist.h:129
int count() const noexcept
Definition uiobjectlist.h:74
Slot< UiObjectList< TObj, TUiData >, int, const std::shared_ptr< TObj > &, Event > OnEditedSlot
Definition uiobjectlist.h:62
QVector< std::shared_ptr< TObj > > mObjects
Definition uiobjectlist.h:155
std::optional< TUiData > row_data(std::size_t i) const override
Definition uiobjectlist.h:132
UiObjectList(const UiObjectList &other)=delete
TUiData UiData
Definition uiobjectlist.h:50
void append(const std::shared_ptr< TObj > &obj) noexcept
Definition uiobjectlist.h:82
Event
Definition uiobjectlist.h:53
std::shared_ptr< TObj > value(int index) noexcept
Definition uiobjectlist.h:79
const std::shared_ptr< TObj > & at(int index) noexcept
Definition uiobjectlist.h:76
auto begin() noexcept
Definition uiobjectlist.h:125
UiObjectList() noexcept
Definition uiobjectlist.h:65
std::shared_ptr< TObj > take(const TObj *obj) noexcept
Definition uiobjectlist.h:94
Signal< UiObjectList< TObj, TUiData >, int, const std::shared_ptr< TObj > &, Event > onEdited
Definition uiobjectlist.h:59
~UiObjectList() noexcept
Definition uiobjectlist.h:71
void elementUiDataChangedHandler(const TObj &obj) noexcept
Definition uiobjectlist.h:148
UiObjectList & operator=(const UiObjectList &rhs)=delete
void insert(int index, const std::shared_ptr< TObj > &obj) noexcept
Definition uiobjectlist.h:85
bool remove(int index) noexcept
Definition uiobjectlist.h:93
auto end() noexcept
Definition uiobjectlist.h:126
const QVector< std::shared_ptr< TObj > > & values()
Definition uiobjectlist.h:124
void set_row_data(size_t i, const TUiData &data) noexcept override
Definition uiobjectlist.h:138
std::shared_ptr< TObj > takeAt(int index) noexcept
Definition uiobjectlist.h:101
bool isEmpty() const noexcept
Definition uiobjectlist.h:75
std::optional< int > indexOf(const TObj *obj) const noexcept
Definition uiobjectlist.h:116
TObj Element
Definition uiobjectlist.h:49
Definition occmodel.cpp:77