Line data Source code
1 : // DUNE DAQ modification notice:
2 : // This file has been modified from the original ATLAS dbe source for the DUNE DAQ project.
3 : // Fork baseline commit: dbe-02-12-17 (2022-05-12).
4 : // Renamed since fork: no.
5 :
6 : /// Including DBE
7 : #include "dbe/treeselection.hpp"
8 : #include "dbe/messenger.hpp"
9 : #include "dbe/tree.hpp"
10 :
11 0 : dbe::models::treeselection::treeselection(QObject* parent)
12 : : QSortFilterProxyModel(parent)
13 0 : , Type(RegExpFilterType)
14 0 : , LevelRestriction(1000)
15 0 : , Hide(false)
16 : {
17 0 : model_common_connections();
18 0 : }
19 :
20 0 : dbe::models::treeselection::~treeselection() {}
21 :
22 : bool
23 0 : dbe::models::treeselection::hasChildren(type_index const& index) const
24 : {
25 0 : if (index.isValid()) {
26 0 : QModelIndex sourceParent = mapToSource(index);
27 0 : return sourceModel()->hasChildren(sourceParent);
28 : } else {
29 : return true;
30 : }
31 : }
32 :
33 : bool
34 0 : dbe::models::treeselection::canFetchMore(type_index const& index) const
35 : {
36 0 : if (index.isValid()) {
37 0 : QModelIndex sourceParent = mapToSource(index);
38 0 : return sourceModel()->canFetchMore(sourceParent);
39 : } else {
40 : return false;
41 : }
42 : }
43 :
44 : void
45 0 : dbe::models::treeselection::fetchMore(type_index const& index)
46 : {
47 0 : QModelIndex sourceParent = mapToSource(index);
48 0 : sourceModel()->fetchMore(sourceParent);
49 0 : }
50 :
51 : void
52 0 : dbe::models::treeselection::SetFilterRestrictionLevel(int Levels)
53 : {
54 0 : LevelRestriction = Levels;
55 0 : }
56 :
57 : void
58 0 : dbe::models::treeselection::SetFilterType(dbe::models::treeselection::FilterType Filter)
59 : {
60 0 : Type = Filter;
61 0 : }
62 :
63 : void
64 0 : dbe::models::treeselection::SetQueryObjects(std::vector<tref> Objects)
65 : {
66 0 : QueryObjects = Objects;
67 0 : }
68 :
69 : std::vector<dbe::tref>
70 0 : dbe::models::treeselection::GetQueryObjects()
71 : {
72 0 : return QueryObjects;
73 : }
74 :
75 : dbe::treenode*
76 0 : dbe::models::treeselection::getnode(type_index const& index) const
77 : {
78 0 : if (index.isValid()) {
79 0 : dbe::models::tree* UnderlyingModel = dynamic_cast<dbe::models::tree*>(sourceModel());
80 :
81 0 : if (UnderlyingModel) {
82 0 : return UnderlyingModel->getnode(index);
83 : }
84 : }
85 :
86 : return nullptr;
87 : }
88 :
89 : dbe::tref
90 0 : dbe::models::treeselection::getobject(type_index const& index) const
91 : {
92 :
93 0 : if (index.isValid()) {
94 0 : QModelIndex sourceParent = mapToSource(index);
95 0 : dbe::models::tree* my = dynamic_cast<dbe::models::tree*>(sourceModel());
96 :
97 0 : if (my) {
98 0 : return my->getobject(sourceParent);
99 : }
100 : }
101 :
102 0 : throw daq::dbe::cannot_handle_invalid_qmodelindex(ERS_HERE);
103 : }
104 :
105 : dunedaq::conffwk::class_t
106 0 : dbe::models::treeselection::getclass(type_index const& index) const
107 : {
108 0 : QModelIndex sourceParent = mapToSource(index);
109 :
110 0 : if (dbe::models::tree* my = dynamic_cast<dbe::models::tree*>(sourceModel())) {
111 0 : return my->getclass(sourceParent);
112 : }
113 :
114 0 : return dunedaq::conffwk::class_t();
115 : }
116 :
117 : QAbstractItemModel*
118 0 : dbe::models::treeselection::ReturnSourceModel() const
119 : {
120 0 : return sourceModel();
121 : }
122 :
123 : void
124 0 : dbe::models::treeselection::ResetQueryObjects()
125 : {
126 0 : QueryObjects.clear();
127 0 : }
128 :
129 : void
130 0 : dbe::models::treeselection::ResetModel()
131 : {
132 0 : beginResetModel();
133 0 : endResetModel();
134 0 : }
135 :
136 : bool
137 0 : dbe::models::treeselection::filterAcceptsRow(int source_row, type_index const& source_parent) const
138 : {
139 0 : treenode* NodeObject = getnode(sourceModel()->index(source_row, 0, source_parent));
140 :
141 0 : if (dynamic_cast<AttributeNode*>(NodeObject)) {
142 : return false;
143 : }
144 :
145 0 : if (Hide) {
146 0 : if (!source_parent.isValid()) {
147 0 : QModelIndex Index_1 = sourceModel()->index(source_row, 1, source_parent);
148 :
149 0 : if (sourceModel()->data(Index_1).toUInt() == 0) {
150 0 : return false;
151 : }
152 : }
153 : }
154 :
155 0 : switch (Type) {
156 0 : case RegExpFilterType:
157 0 : return RegexpFilter(source_row, source_parent);
158 :
159 0 : case ObjectFilterType:
160 0 : return ObjectFilter(source_row, source_parent);
161 :
162 : default:
163 : return true;
164 : }
165 : }
166 :
167 : bool
168 0 : dbe::models::treeselection::lessThan(type_index const& left, type_index const& right) const
169 : {
170 0 : if (left.parent() == QModelIndex() || left.parent().parent() == QModelIndex()) {
171 0 : QVariant LeftData = sourceModel()->data(left);
172 0 : QVariant RightData = sourceModel()->data(right);
173 :
174 0 : switch (LeftData.type()) {
175 0 : case QVariant::Bool:
176 0 : case QVariant::UInt:
177 0 : return (LeftData.toUInt() < RightData.toUInt());
178 :
179 0 : case QVariant::Int:
180 0 : return (LeftData.toInt() < RightData.toInt());
181 :
182 0 : case QVariant::String:
183 0 : return ((LeftData.toString()).compare(RightData.toString()) > 0);
184 :
185 : default:
186 : return false;
187 : }
188 :
189 : return true;
190 0 : }
191 :
192 : return false;
193 : }
194 :
195 : bool
196 0 : dbe::models::treeselection::AcceptItem(type_index const& SourceIndex, int LevelRestriction) const
197 : {
198 0 : if (sourceModel()->canFetchMore(SourceIndex)) {
199 0 : sourceModel()->fetchMore(SourceIndex);
200 : }
201 :
202 0 : if (sourceModel()->data(SourceIndex).toString().contains(filterRegExp())) {
203 : return true;
204 : }
205 :
206 0 : if (LevelRestriction <= 1) {
207 : return false;
208 : }
209 :
210 0 : for (int i = 0; i < sourceModel()->rowCount(SourceIndex); ++i) {
211 0 : if (AcceptItem(sourceModel()->index(i, 0), --LevelRestriction)) {
212 : return true;
213 : }
214 : }
215 :
216 : return false;
217 : }
218 :
219 : bool
220 0 : dbe::models::treeselection::RegexpFilter(int sourceRow, type_index const& sourceParent) const
221 : {
222 0 : if (AtDepth(sourceParent) <= LevelRestriction) {
223 0 : QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
224 0 : return AcceptItem(index0, LevelRestriction);
225 : }
226 :
227 : return true;
228 : }
229 :
230 : bool
231 0 : dbe::models::treeselection::ObjectFilter(int sourceRow, type_index const& sourceParent) const
232 : {
233 0 : QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
234 0 : QString id = sourceModel()->data(index0).toString();
235 :
236 0 : if ((filterRegExp()).isEmpty()) {
237 : return true;
238 : }
239 :
240 0 : if (AtDepth(sourceParent) > 2) {
241 : return true;
242 : }
243 :
244 0 : if (!sourceParent.isValid()) {
245 0 : for (size_t i = 0; i < QueryObjects.size(); ++i)
246 0 : if (QString(QueryObjects.at(i).class_name().c_str()).compare(id) == 0) {
247 : return true;
248 : }
249 : } else {
250 0 : for (size_t i = 0; i < QueryObjects.size(); ++i)
251 0 : if (id.compare(QString(QueryObjects.at(i).UID().c_str())) == 0) {
252 : return true;
253 : }
254 : }
255 :
256 : return false;
257 0 : }
258 :
259 : int
260 0 : dbe::models::treeselection::AtDepth(type_index const& SourceParent) const
261 : {
262 0 : int Depth = 1;
263 0 : QModelIndex CurrentIndex = SourceParent;
264 :
265 0 : while (CurrentIndex.isValid()) {
266 0 : CurrentIndex = CurrentIndex.parent();
267 0 : Depth++;
268 : }
269 :
270 0 : return Depth;
271 : }
272 :
273 : void
274 0 : dbe::models::treeselection::ToggleEmptyClasses(bool HideLocal)
275 : {
276 0 : Hide = HideLocal;
277 0 : ResetModel();
278 0 : }
279 : //----------------------------------------------------------------------------------------------------
280 :
281 : //----------------------------------------------------------------------------------------------------
282 0 : MODEL_COMMON_INTERFACE_LOOKUP_IMPL(dbe::models::treeselection)
283 : {
284 0 : if (dbe::treenode* classnode = confaccessor::gethandler()->getnode(QString::fromStdString(obj.class_name()))) {
285 :
286 0 : auto found = [&obj, classnode](int i) {
287 0 : return obj.UID() == classnode->GetChild(i)->GetData(0).toString().toStdString();
288 0 : };
289 :
290 0 : int i = 0;
291 0 : int const childs = classnode->ChildCount();
292 :
293 0 : for (; i < childs and not found(i); ++i)
294 : ;
295 :
296 0 : return i == childs ? QModelIndex() : index(i, 0, QModelIndex());
297 : }
298 :
299 0 : return QModelIndex();
300 : }
301 : //----------------------------------------------------------------------------------------------------
302 :
303 : //----------------------------------------------------------------------------------------------------
304 0 : MODEL_COMMON_INTERFACE_CREATE_THAT_OBJ_IMPL(dbe::models::treeselection)
305 : {
306 : // This is a stub and nothing needs to be done since
307 : // the real job happens in the slot defined in dbe::models::tree object from signal for treeselectionmodel
308 0 : Q_UNUSED(index);
309 0 : Q_UNUSED(obj);
310 0 : }
311 :
312 0 : MODEL_COMMON_INTERFACE_DELETE_THAT_OBJ_IMPL(dbe::models::treeselection)
313 : {
314 : // This is a stub and nothing needs to be done since
315 : // the real job happens in the slot defined in dbe::models::tree
316 0 : Q_UNUSED(index);
317 0 : }
318 :
319 0 : MODEL_COMMON_INTERFACE_UPDATE_THAT_OBJ_IMPL(dbe::models::treeselection)
320 : {
321 0 : Q_UNUSED(obj);
322 0 : type_index const mapped_index = this->mapFromSource(index);
323 0 : emit dataChanged(mapped_index, mapped_index);
324 0 : }
325 :
326 0 : MODEL_COMMON_INTERFACE_RENAME_THAT_OBJ_IMPL(dbe::models::treeselection)
327 : {
328 0 : Q_UNUSED(obj);
329 0 : type_index const mapped_index = this->mapFromSource(index);
330 0 : emit dataChanged(mapped_index, mapped_index);
331 0 : }
332 : //----------------------------------------------------------------------------------------------------
333 :
334 : //----------------------------------------------------------------------------------------------------
335 0 : TREEMODEL_REMOVE_ROWS_DEF(dbe::models::treeselection)
336 0 : MODEL_COMMON_INTERFACE_SLOTS_DEF(dbe::models::treeselection)
337 : //----------------------------------------------------------------------------------------------------
|