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