LibrePCB Developers Documentation
Loading...
Searching...
No Matches
deriveduiobjectlistview.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_DERIVEDUIOBJECTLISTVIEW_H
21#define LIBREPCB_EDITOR_DERIVEDUIOBJECTLISTVIEW_H
22
23/*******************************************************************************
24 * Includes
25 ******************************************************************************/
26#include "uiobjectlist.h"
27
28/*******************************************************************************
29 * Namespace / Forward Declarations
30 ******************************************************************************/
31namespace librepcb {
32namespace editor {
33
34/*******************************************************************************
35 * Class DerivedUiObjectList
36 ******************************************************************************/
37
41template <typename TList, typename TDerived, typename TDerivedUiData>
42class DerivedUiObjectList : public slint::Model<TDerivedUiData> {
43public:
44 // Constructors / Destructor
46 explicit DerivedUiObjectList(const std::shared_ptr<TList>& list) noexcept
47 : mList(list),
49 *this,
50 &DerivedUiObjectList<TList, TDerived,
51 TDerivedUiData>::listEditedHandler),
53 *this,
56 Q_ASSERT(mList);
57 for (auto obj : *mList) {
58 if (auto o = std::dynamic_pointer_cast<TDerived>(obj)) {
59 o->onDerivedUiDataChanged.attach(mOnDerivedUiDataChangedSlot);
60 }
61 }
62 mList->onEdited.attach(mOnListEditedSlot);
63 }
65
66 // Implementations
67 std::size_t row_count() const noexcept override { return mList->row_count(); }
68 std::optional<TDerivedUiData> row_data(std::size_t i) const override {
69 if (auto obj = std::dynamic_pointer_cast<TDerived>(mList->value(i))) {
70 return obj->getDerivedUiData();
71 }
72 return std::nullopt;
73 }
74 void set_row_data(size_t i, const TDerivedUiData& data) noexcept override {
75 if (auto obj = std::dynamic_pointer_cast<TDerived>(mList->value(i))) {
76 obj->setDerivedUiData(data);
77 }
78 }
79
80 // Operator Overloadings
82
83private:
84 void listEditedHandler(const TList& list, int index,
85 const std::shared_ptr<typename TList::Element>& obj,
86 typename TList::Event event) noexcept {
87 Q_UNUSED(list);
88 switch (event) {
89 case TList::Event::ElementAdded: {
90 slint::Model<TDerivedUiData>::notify_row_added(index, 1);
91 if (auto derivedObj = std::dynamic_pointer_cast<TDerived>(obj)) {
92 derivedObj->onDerivedUiDataChanged.attach(
94 }
95 break;
96 }
97 case TList::Event::ElementRemoved: {
98 slint::Model<TDerivedUiData>::notify_row_removed(index, 1);
99 if (auto derivedObj = std::dynamic_pointer_cast<TDerived>(obj)) {
100 derivedObj->onDerivedUiDataChanged.detach(
102 }
103 break;
104 }
105 default:
106 break;
107 }
108 }
109
110 void elementDerivedUiDataChangedHandler(const TDerived& obj) noexcept {
111 if (auto index =
112 mList->indexOf(static_cast<const typename TList::Element*>(&obj))) {
113 slint::Model<TDerivedUiData>::notify_row_changed(*index);
114 }
115 }
116
117 std::shared_ptr<TList> mList;
118 typename TList::OnEditedSlot mOnListEditedSlot;
120};
121
122/*******************************************************************************
123 * End of File
124 ******************************************************************************/
125
126} // namespace editor
127} // namespace librepcb
128
129#endif
The Slot class is used to receive signals from non-QObject derived classes.
Definition signalslot.h:170
The DerivedUiObjectList class.
Definition deriveduiobjectlistview.h:42
std::size_t row_count() const noexcept override
Definition deriveduiobjectlistview.h:67
std::optional< TDerivedUiData > row_data(std::size_t i) const override
Definition deriveduiobjectlistview.h:68
void set_row_data(size_t i, const TDerivedUiData &data) noexcept override
Definition deriveduiobjectlistview.h:74
DerivedUiObjectList & operator=(const DerivedUiObjectList &rhs)=delete
DerivedUiObjectList(const DerivedUiObjectList &other)=delete
void listEditedHandler(const TList &list, int index, const std::shared_ptr< typename TList::Element > &obj, typename TList::Event event) noexcept
Definition deriveduiobjectlistview.h:84
void elementDerivedUiDataChangedHandler(const TDerived &obj) noexcept
Definition deriveduiobjectlistview.h:110
~DerivedUiObjectList() noexcept
Definition deriveduiobjectlistview.h:64
TList::OnEditedSlot mOnListEditedSlot
Definition deriveduiobjectlistview.h:118
std::shared_ptr< TList > mList
Definition deriveduiobjectlistview.h:117
Slot< TDerived > mOnDerivedUiDataChangedSlot
Definition deriveduiobjectlistview.h:119
DerivedUiObjectList(const std::shared_ptr< TList > &list) noexcept
Definition deriveduiobjectlistview.h:46
Definition occmodel.cpp:77