Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
GNETemplateElementList.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
18// Template used for Element lists
19/****************************************************************************/
20#pragma once
21
24
25#include "GNEElementTable.h"
26#include "GNEElementList.h"
27
28// ===========================================================================
29// class definitions
30// ===========================================================================
31
32template <typename elementDialogType, typename elementType, typename GNEChange_Type>
34
35public:
38 FXVerticalFrame* contentFrame, SumoXMLTag tag, GNEElementList::Options options) :
39 GNEElementList(contentFrame, elementDialogParent->getApplicationWindow()->getTagPropertiesDatabase()->getTagProperty(tag, true), options),
40 myElementDialogParent(elementDialogParent) {
41 // update table
42 updateList();
43 }
44
46 const std::vector<elementType*>& getEditedElements() const {
47 return myEditedElements;
48 }
49
51 long insertElement(elementType* element) {
52 // add change command
53 element->getNet()->getViewNet()->getUndoList()->add(new GNEChange_Type(element, true), true);
54 // update table
55 return updateList();
56 }
57
59 long updateList() {
60 // reset edited element
61 myEditedElements.clear();
62 for (const auto& child : myElementDialogParent->getElement()->getChildren().template get<elementType*>()) {
63 if (child->getTagProperty()->getTag() == myTagProperty->getTag()) {
64 myEditedElements.push_back(child);
65 }
66 }
67 // first resize table (used if we removed some elements)
69 // now update all rows
70 for (size_t i = 0; i < myEditedElements.size(); i++) {
72 }
73 return 1;
74 }
75
77 bool checkSort() const {
78 // get sort tuples
79 const auto tuples = getSortTuples(false);
80 // check if the elements are sorted
81 for (int i = 0; i < ((int)tuples.size() - 1); i++) {
82 if (tuples.at(i) > tuples.at(i)) {
83 return false;
84 }
85 }
86 return true;
87 }
88
90 long sortRows() {
91 // get sort tuples sorted
92 const auto sortedTuples = getSortTuples(true);
93 // now update edited elements list using sortTuples
94 myEditedElements.clear();
95 for (const auto& element : sortedTuples) {
96 myEditedElements.push_back(std::get<6>(element));
97 }
98 // update table
99 return updateList();
100 }
101
103 long removeElement(const size_t rowIndex) {
104 // delete element recursively
106 // update table
107 return updateList();
108 }
109
111 long removeElement(const elementType* element) {
112 // search index
113 for (size_t rowIndex = 0; rowIndex < myEditedElements.size(); rowIndex++) {
114 if (myEditedElements.at(rowIndex) == element) {
115 return removeElement(rowIndex);
116 }
117 }
118 // if we reach this point, the element was not found
119 throw ProcessError("Element not found in removeElement");
120 }
121
123 virtual long addNewElement() = 0;
124
126 virtual long openElementDialog(const size_t rowIndex) = 0;
127
128protected:
131
133 std::vector<elementType*> myEditedElements;
134
135private:
137 typedef std::tuple<double, double, double, double, double, double, elementType*> SortTuple;
138
140 std::vector<SortTuple> getSortTuples(const bool sort) const {
141 // declare sorted element set
142 std::vector<SortTuple> elementSortKeyVector;
143 // add all elements
144 for (size_t i = 0; i < myEditedElements.size(); i++) {
145 // create tuple with max 6 sortable attributes
146 auto tuple = std::make_tuple(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, myEditedElements.at(i));
147 // update tuple with sortable attributes
148 const auto& sortableAttributes = myElementTable->getColumnHeader()->getSortableAttributes();
149 // fill tuple
150 if (sortableAttributes.size() > 0) {
151 std::get<0>(tuple) = GNEAttributeCarrier::parse<double>(myEditedElements.at(i)->getAttribute(sortableAttributes.at(0)));
152 }
153 if (sortableAttributes.size() > 1) {
154 std::get<1>(tuple) = GNEAttributeCarrier::parse<double>(myEditedElements.at(i)->getAttribute(sortableAttributes.at(1)));
155 }
156 if (sortableAttributes.size() > 2) {
157 std::get<2>(tuple) = GNEAttributeCarrier::parse<double>(myEditedElements.at(i)->getAttribute(sortableAttributes.at(2)));
158 }
159 if (sortableAttributes.size() > 3) {
160 std::get<3>(tuple) = GNEAttributeCarrier::parse<double>(myEditedElements.at(i)->getAttribute(sortableAttributes.at(3)));
161 }
162 if (sortableAttributes.size() > 4) {
163 std::get<4>(tuple) = GNEAttributeCarrier::parse<double>(myEditedElements.at(i)->getAttribute(sortableAttributes.at(4)));
164 }
165 if (sortableAttributes.size() > 5) {
166 std::get<5>(tuple) = GNEAttributeCarrier::parse<double>(myEditedElements.at(i)->getAttribute(sortableAttributes.at(5)));
167 }
168 elementSortKeyVector.push_back(tuple);
169 }
170 // check if sort tuples
171 if (sort) {
172 std::set<SortTuple> elementSortKeySet;
173 for (const auto& element : elementSortKeyVector) {
174 // insert tuple into set
175 elementSortKeySet.insert(element);
176 }
177 // clear vector and copy set into vector
178 elementSortKeyVector.clear();
179 for (const auto& element : elementSortKeySet) {
180 elementSortKeyVector.push_back(element);
181 }
182 }
183 return elementSortKeyVector;
184 }
185
188
191};
SumoXMLTag
Numbers representing SUMO-XML - element names.
Options
FOX-declaration.
const GNETagProperties * myTagProperty
FOX needs this.
GNEElementTable * myElementTable
element table
void removeElementRecursively(GNEAdditional *additionalElement) const
delete additional element recursively
const std::vector< SumoXMLAttr > & getSortableAttributes()
get sortable attributes
void updateRow(const size_t index, GNEAttributeCarrier *AC)
update row
void resizeTable(const size_t numRows)
resize table
ColumnHeader * getColumnHeader() const
get column header
SumoXMLTag getTag() const
get Tag vinculated with this attribute Property
T * getElement() const
get edited element
GNETemplateElementList & operator=(const GNETemplateElementList &)=delete
Invalidated assignment operator.
std::tuple< double, double, double, double, double, double, elementType * > SortTuple
typedef used for sorting elements by attributes
std::vector< SortTuple > getSortTuples(const bool sort) const
get element sorted
const std::vector< elementType * > & getEditedElements() const
get edited elements
long removeElement(const size_t rowIndex)
remove element (using index)
std::vector< elementType * > myEditedElements
edited elements
bool checkSort() const
check if the elements are sorted
long updateList()
update element list
long removeElement(const elementType *element)
remove element
long insertElement(elementType *element)
insert element
GNETemplateElementDialog< elementDialogType > * myElementDialogParent
element dialog parent
virtual long openElementDialog(const size_t rowIndex)=0
open element dialog (optional
GNETemplateElementList(GNETemplateElementDialog< elementDialogType > *elementDialogParent, FXVerticalFrame *contentFrame, SumoXMLTag tag, GNEElementList::Options options)
constructor
virtual long addNewElement()=0
add new element (must be implemented in children)
GNETemplateElementList(const GNETemplateElementList &)=delete
Invalidated copy constructor.