LibrePCB Developers Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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<const TObj>&,
59 Event>
62 const std::shared_ptr<const TObj>&, Event>
64
65 // Constructors / Destructor
66 UiObjectList() noexcept
67 : onEdited(*this),
68 mObjects(),
70 *this, &UiObjectList<TObj, TUiData>::elementUiDataChangedHandler) {}
71 UiObjectList(const UiObjectList& other) = delete;
72 ~UiObjectList() noexcept {}
73
74 // General Methods
75 int count() const noexcept { return mObjects.count(); }
76 bool isEmpty() const noexcept { return mObjects.isEmpty(); }
77 std::shared_ptr<TObj> at(int index) noexcept { return mObjects.at(index); }
78 std::shared_ptr<TObj> value(int index) noexcept {
79 return mObjects.value(index);
80 }
81 void append(const std::shared_ptr<TObj>& obj) noexcept {
82 insert(mObjects.count(), obj);
83 }
84 void insert(int index, const std::shared_ptr<TObj>& obj) noexcept {
85 Q_ASSERT(obj);
86 index = qBound(0, index, mObjects.count() + 1);
87 mObjects.insert(index, obj);
88 slint::Model<TUiData>::notify_row_added(index, 1);
89 obj->onUiDataChanged.attach(mOnUiDataChangedSlot);
90 onEdited.notify(index, obj, Event::ElementAdded);
91 }
92 bool remove(int index) noexcept { return takeAt(index) != nullptr; }
93 std::shared_ptr<TObj> take(const TObj* obj) noexcept {
94 if (auto index = indexOf(obj)) {
95 return takeAt(*index);
96 } else {
97 return nullptr;
98 }
99 }
100 std::shared_ptr<TObj> takeAt(int index) noexcept {
101 if (auto obj = mObjects.takeAt(index)) {
102 slint::Model<TUiData>::notify_row_removed(index, 1);
103 obj->onUiDataChanged.detach(mOnUiDataChangedSlot);
104 onEdited.notify(index, obj, Event::ElementRemoved);
105 return obj;
106 } else {
107 return nullptr;
108 }
109 }
110 std::optional<int> indexOf(const TObj* obj) const noexcept {
111 for (int i = 0; i < mObjects.count(); ++i) {
112 if (mObjects.at(i).get() == obj) {
113 return i;
114 }
115 }
116 return std::nullopt;
117 }
118 auto begin() noexcept { return mObjects.begin(); }
119 auto end() noexcept { return mObjects.end(); }
120
121 // Implementations
122 std::size_t row_count() const noexcept override {
123 return static_cast<std::size_t>(mObjects.count());
124 }
125 std::optional<TUiData> row_data(std::size_t i) const override {
126 if (auto obj = mObjects.value(i)) {
127 return obj->getUiData();
128 }
129 return std::nullopt;
130 }
131 void set_row_data(size_t i, const TUiData& data) noexcept override {
132 if (auto obj = mObjects.value(i)) {
133 obj->setUiData(data);
134 }
135 }
136
137 // Operator Overloadings
138 UiObjectList& operator=(const UiObjectList& rhs) = delete;
139
140private:
141 void elementUiDataChangedHandler(const TObj& obj) noexcept {
142 if (auto index = indexOf(&obj)) {
143 slint::Model<TUiData>::notify_row_changed(*index);
144 onEdited.notify(*index, at(*index), Event::ElementUiDataChanged);
145 }
146 }
147
148 QVector<std::shared_ptr<TObj>> mObjects;
150};
151
152/*******************************************************************************
153 * End of File
154 ******************************************************************************/
155
156} // namespace editor
157} // namespace librepcb
158
159#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
Slot< TObj > mOnUiDataChangedSlot
Definition uiobjectlist.h:149
Slot< UiObjectList< TObj, TUiData >, int, const std::shared_ptr< const TObj > &, Event > OnEditedSlot
Definition uiobjectlist.h:63
std::size_t row_count() const noexcept override
Definition uiobjectlist.h:122
int count() const noexcept
Definition uiobjectlist.h:75
QVector< std::shared_ptr< TObj > > mObjects
Definition uiobjectlist.h:148
std::optional< TUiData > row_data(std::size_t i) const override
Definition uiobjectlist.h:125
UiObjectList(const UiObjectList &other)=delete
TUiData UiData
Definition uiobjectlist.h:50
void append(const std::shared_ptr< TObj > &obj) noexcept
Definition uiobjectlist.h:81
Event
Definition uiobjectlist.h:53
std::shared_ptr< TObj > value(int index) noexcept
Definition uiobjectlist.h:78
std::shared_ptr< TObj > at(int index) noexcept
Definition uiobjectlist.h:77
auto begin() noexcept
Definition uiobjectlist.h:118
UiObjectList() noexcept
Definition uiobjectlist.h:66
std::shared_ptr< TObj > take(const TObj *obj) noexcept
Definition uiobjectlist.h:93
~UiObjectList() noexcept
Definition uiobjectlist.h:72
Signal< UiObjectList< TObj, TUiData >, int, const std::shared_ptr< const TObj > &, Event > onEdited
Definition uiobjectlist.h:60
void elementUiDataChangedHandler(const TObj &obj) noexcept
Definition uiobjectlist.h:141
UiObjectList & operator=(const UiObjectList &rhs)=delete
void insert(int index, const std::shared_ptr< TObj > &obj) noexcept
Definition uiobjectlist.h:84
bool remove(int index) noexcept
Definition uiobjectlist.h:92
auto end() noexcept
Definition uiobjectlist.h:119
void set_row_data(size_t i, const TUiData &data) noexcept override
Definition uiobjectlist.h:131
std::shared_ptr< TObj > takeAt(int index) noexcept
Definition uiobjectlist.h:100
bool isEmpty() const noexcept
Definition uiobjectlist.h:76
std::optional< int > indexOf(const TObj *obj) const noexcept
Definition uiobjectlist.h:110
TObj Element
Definition uiobjectlist.h:49
Definition occmodel.cpp:77