LibrePCB Developers Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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:
85 const TList& list, int index,
86 const std::shared_ptr<const typename TList::Element>& obj,
87 typename TList::Event event) noexcept {
88 Q_UNUSED(list);
89 Q_UNUSED(obj);
90 switch (event) {
91 case TList::Event::ElementAdded: {
92 slint::Model<TDerivedUiData>::notify_row_added(index, 1);
93 if (auto o = std::dynamic_pointer_cast<TDerived>(mList->value(index))) {
94 Q_ASSERT(o == obj);
95 o->onDerivedUiDataChanged.attach(mOnDerivedUiDataChangedSlot);
96 }
97 break;
98 }
99 case TList::Event::ElementRemoved: {
100 slint::Model<TDerivedUiData>::notify_row_removed(index, 1);
101 if (auto o = std::dynamic_pointer_cast<TDerived>(mList->value(index))) {
102 Q_ASSERT(o == obj);
103 o->onDerivedUiDataChanged.detach(mOnDerivedUiDataChangedSlot);
104 }
105 break;
106 }
107 default:
108 break;
109 }
110 }
111
112 void elementDerivedUiDataChangedHandler(const TDerived& obj) noexcept {
113 if (auto index =
114 mList->indexOf(static_cast<const typename TList::Element*>(&obj))) {
115 slint::Model<TDerivedUiData>::notify_row_changed(*index);
116 }
117 }
118
119 std::shared_ptr<TList> mList;
120 typename TList::OnEditedSlot mOnListEditedSlot;
122};
123
124/*******************************************************************************
125 * End of File
126 ******************************************************************************/
127
128} // namespace editor
129} // namespace librepcb
130
131#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< const typename TList::Element > &obj, typename TList::Event event) noexcept
Definition deriveduiobjectlistview.h:84
void elementDerivedUiDataChangedHandler(const TDerived &obj) noexcept
Definition deriveduiobjectlistview.h:112
~DerivedUiObjectList() noexcept
Definition deriveduiobjectlistview.h:64
TList::OnEditedSlot mOnListEditedSlot
Definition deriveduiobjectlistview.h:120
std::shared_ptr< TList > mList
Definition deriveduiobjectlistview.h:119
Slot< TDerived > mOnDerivedUiDataChangedSlot
Definition deriveduiobjectlistview.h:121
DerivedUiObjectList(const std::shared_ptr< TList > &list) noexcept
Definition deriveduiobjectlistview.h:46
Definition occmodel.cpp:77