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 QT#include <QHeaderView>
7 : #include "dbe/tableselection.hpp"
8 :
9 : #include "dbe/confaccessor.hpp"
10 : #include <QAbstractItemView>
11 : #include <QScrollBar>
12 : #include <QMenu>
13 : #include <QLabel>
14 : #include <QHBoxLayout>
15 : #include <QContextMenuEvent>
16 : #include <QDialog>
17 : #include <QLineEdit>
18 : #include <QVariant>
19 : #include <QHeaderView>
20 :
21 : /// Including DBE
22 : #include "dbe/CustomTableView.hpp"
23 : #include "dbe/ObjectEditor.hpp"
24 : #include "dbe/ObjectCreator.hpp"
25 : #include "dbe/messenger.hpp"
26 :
27 : #include "dbe/MainWindow.hpp"
28 :
29 : //-----------------------------------------------------------------------------------------------------
30 0 : dbe::CustomTableView::CustomTableView ( QWidget * parent )
31 : : QTableView ( parent ),
32 0 : m_context_menu ( nullptr ),
33 0 : FindObject ( nullptr ),
34 0 : editObject ( nullptr ),
35 0 : deleteObjectAc ( nullptr ),
36 0 : refByAc ( nullptr ),
37 0 : refByAcOnlyComp ( nullptr ),
38 0 : copyObjectAc ( nullptr ),
39 0 : m_find_object_dialog ( nullptr ),
40 0 : LineEdit ( nullptr ),
41 0 : NextButton ( nullptr ),
42 0 : GoButton ( nullptr ),
43 0 : ListIndex ( 0 )
44 : {
45 0 : verticalHeader()->setVisible(true);
46 0 : verticalHeader()->setSectionResizeMode ( QHeaderView::Interactive );
47 :
48 0 : horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
49 0 : horizontalHeader()->setMaximumSectionSize(750);
50 0 : setSortingEnabled ( true );
51 0 : setAlternatingRowColors ( true );
52 0 : setSelectionMode ( SelectionMode::SingleSelection );
53 0 : setSelectionBehavior ( QAbstractItemView::SelectionBehavior::SelectRows );
54 0 : setHorizontalScrollMode(ScrollMode::ScrollPerPixel);
55 0 : setVerticalScrollMode(ScrollMode::ScrollPerPixel);
56 0 : setWordWrap(true);
57 0 : setTextElideMode(Qt::ElideRight);
58 :
59 0 : connect ( this, SIGNAL ( activated(const QModelIndex&) ), this, SLOT ( slot_edit_object() ) );
60 :
61 0 : }
62 : //-----------------------------------------------------------------------------------------------------
63 :
64 : //-----------------------------------------------------------------------------------------------------
65 0 : void dbe::CustomTableView::contextMenuEvent ( QContextMenuEvent * Event )
66 : {
67 0 : if ( m_context_menu == nullptr )
68 : {
69 0 : m_context_menu = new QMenu ( this );
70 0 : CreateActions();
71 : }
72 :
73 0 : QModelIndex Index = indexAt ( Event->pos() );
74 :
75 0 : if ( Index.isValid() ) {
76 0 : for (int item=0; item<m_last_object_item; ++item) {
77 0 : m_context_menu->actions().at ( item )->setVisible ( true );
78 : }
79 : }
80 : else {
81 0 : for (int item=0; item<m_last_object_item; ++item) {
82 0 : m_context_menu->actions().at ( item )->setVisible ( false );
83 : }
84 : }
85 0 : m_context_menu->exec ( Event->globalPos() );
86 0 : }
87 : //-----------------------------------------------------------------------------------------------------
88 :
89 : //-----------------------------------------------------------------------------------------------------
90 0 : void dbe::CustomTableView::FindObjectSlot()
91 : {
92 0 : if ( m_find_object_dialog != nullptr )
93 : {
94 0 : delete m_find_object_dialog;
95 0 : m_find_object_dialog = nullptr;
96 : }
97 :
98 0 : m_find_object_dialog = new QDialog ( this );
99 0 : m_find_object_dialog->setSizePolicy ( QSizePolicy::Preferred, QSizePolicy::Preferred );
100 0 : m_find_object_dialog->setToolTip ( "Type string to edit line and press Enter." );
101 0 : m_find_object_dialog->setWindowTitle ( "Search for an object in the table" );
102 :
103 0 : QHBoxLayout * Layout = new QHBoxLayout ( m_find_object_dialog );
104 0 : QLabel * Label = new QLabel ( QString ( "Find Object:" ), m_find_object_dialog );
105 :
106 0 : NextButton = new QPushButton ( "Next" );
107 0 : GoButton = new QPushButton ( "Go !" );
108 :
109 0 : LineEdit = new QLineEdit ( m_find_object_dialog );
110 0 : LineEdit->setToolTip ( "Type string and press Enter" );
111 :
112 0 : Layout->addWidget ( Label );
113 0 : Layout->addWidget ( LineEdit );
114 0 : Layout->addWidget ( GoButton );
115 0 : Layout->addWidget ( NextButton );
116 :
117 0 : m_find_object_dialog->setLayout ( Layout );
118 0 : m_find_object_dialog->show();
119 0 : NextButton->setDisabled ( true );
120 :
121 0 : connect ( LineEdit, SIGNAL ( textEdited ( QString ) ), this,
122 : SLOT ( EditedSearchString ( QString ) ) );
123 0 : connect ( LineEdit, SIGNAL ( returnPressed() ), this, SLOT ( slot_go_to_object() ) );
124 0 : connect ( GoButton, SIGNAL ( clicked() ), this, SLOT ( slot_go_to_object() ) );
125 0 : connect ( NextButton, SIGNAL ( clicked() ), this, SLOT ( GoToNext() ) );
126 0 : }
127 : //-----------------------------------------------------------------------------------------------------
128 :
129 : //-----------------------------------------------------------------------------------------------------
130 0 : void dbe::CustomTableView::slot_go_to_object()
131 : {
132 0 : ListIndex = 0;
133 0 : ListOfMatch.clear();
134 :
135 0 : QString UserType = LineEdit->text();
136 :
137 0 : if ( UserType.isEmpty() )
138 : {
139 0 : return;
140 : }
141 :
142 0 : QAbstractItemModel * Model = model();
143 :
144 0 : if ( Model != nullptr )
145 : {
146 0 : QVariant StringCriterium = QVariant ( UserType );
147 0 : QModelIndex WhereToStartSearch = Model->index ( 0, 0 );
148 0 : ListOfMatch = Model->match ( WhereToStartSearch, Qt::DisplayRole, StringCriterium, 1000,
149 0 : Qt::MatchContains | Qt::MatchWrap );
150 :
151 0 : if ( ListOfMatch.size() > 0 )
152 : {
153 0 : ListIndex = 0;
154 0 : auto val=ListOfMatch.value ( ListIndex );
155 0 : selectRow ( val.row() );
156 : // resizeColumnToContents ( val.row() );
157 0 : scrollTo (val , QAbstractItemView::PositionAtCenter );
158 :
159 0 : GoButton->setDisabled ( true );
160 0 : NextButton->setEnabled ( true );
161 :
162 0 : disconnect ( LineEdit, SIGNAL ( returnPressed() ), this, SLOT ( slot_go_to_object() ) );
163 0 : connect ( LineEdit, SIGNAL ( returnPressed() ), this, SLOT ( GoToNext() ) );
164 : }
165 0 : }
166 0 : }
167 : //-----------------------------------------------------------------------------------------------------
168 :
169 : //-----------------------------------------------------------------------------------------------------
170 0 : void dbe::CustomTableView::GoToNext()
171 : {
172 0 : if ( ( LineEdit->text() ).isEmpty() )
173 : {
174 0 : ListIndex = 0;
175 0 : ListOfMatch.clear();
176 0 : return;
177 : }
178 :
179 0 : if ( ListOfMatch.size() > 0 )
180 : {
181 0 : if ( ( ++ListIndex ) < ListOfMatch.size() )
182 : {
183 0 : scrollTo ( ListOfMatch.value ( ListIndex ), QAbstractItemView::EnsureVisible );
184 0 : selectRow ( ListOfMatch.value ( ListIndex ).row() );
185 0 : resizeColumnToContents ( ListIndex );
186 : }
187 : else
188 : {
189 0 : ListIndex = 0;
190 0 : scrollTo ( ListOfMatch.value ( ListIndex ), QAbstractItemView::EnsureVisible );
191 0 : selectRow ( ListOfMatch.value ( ListIndex ).row() );
192 0 : resizeColumnToContents ( ListIndex );
193 : }
194 : }
195 : }
196 : //-----------------------------------------------------------------------------------------------------
197 :
198 : //-----------------------------------------------------------------------------------------------------
199 0 : void dbe::CustomTableView::EditedSearchString ( QString Text )
200 : {
201 0 : Q_UNUSED ( Text )
202 :
203 0 : connect ( LineEdit, SIGNAL ( returnPressed() ), this, SLOT ( slot_go_to_object() ) );
204 0 : disconnect ( LineEdit, SIGNAL ( returnPressed() ), this, SLOT ( GoToNext() ) );
205 :
206 0 : GoButton->setEnabled ( true );
207 0 : NextButton->setDisabled ( true );
208 0 : }
209 : //-----------------------------------------------------------------------------------------------------
210 :
211 : //-----------------------------------------------------------------------------------------------------
212 0 : void dbe::CustomTableView::slot_edit_object()
213 : {
214 0 : QModelIndex const & index = this->currentIndex();
215 :
216 0 : if ( index.isValid() )
217 : {
218 0 : if ( models::tableselection const * selectionmodel =
219 0 : dynamic_cast<models::tableselection const *> ( index.model() ) )
220 : {
221 0 : if ( models::table const * srcmodel =
222 0 : dynamic_cast<const dbe::models::table *> ( selectionmodel->sourceModel() ) )
223 : {
224 0 : tref obj = srcmodel->GetTableObject ( selectionmodel->mapToSource ( index ).row() );
225 :
226 0 : if ( not obj.is_null() )
227 : {
228 0 : emit OpenEditor ( obj );
229 : }
230 0 : }
231 : }
232 : }
233 0 : }
234 : //-----------------------------------------------------------------------------------------------------
235 :
236 : //-----------------------------------------------------------------------------------------------------
237 0 : void dbe::CustomTableView::referencedBy_OnlyComposite()
238 : {
239 0 : QModelIndex const & Index = this->currentIndex();
240 :
241 0 : const dbe::models::tableselection * Model =
242 0 : dynamic_cast<const dbe::models::tableselection *> ( Index.model() );
243 :
244 0 : const dbe::models::table * SourceModel = dynamic_cast<const dbe::models::table *> ( Model
245 0 : ->sourceModel() );
246 :
247 0 : if ( !Index.isValid() || !Model )
248 : {
249 0 : return;
250 : }
251 :
252 0 : tref obj = SourceModel->GetTableObject ( Model->mapToSource ( Index ).row() );
253 0 : referencedBy ( obj, true );
254 0 : }
255 : //-----------------------------------------------------------------------------------------------------
256 :
257 : //-----------------------------------------------------------------------------------------------------
258 0 : void dbe::CustomTableView::referencedBy_All()
259 : {
260 0 : QModelIndex const & Index = this->currentIndex();
261 0 : const dbe::models::tableselection * Model =
262 0 : dynamic_cast<const dbe::models::tableselection *> ( Index.model() );
263 :
264 0 : dbe::models::table const * SourceModel = dynamic_cast<const dbe::models::table *> ( Model
265 0 : ->sourceModel() );
266 :
267 0 : if ( !Index.isValid() || !Model )
268 : {
269 0 : return;
270 : }
271 :
272 0 : tref obj = SourceModel->GetTableObject ( Model->mapToSource ( Index ).row() );
273 0 : referencedBy ( obj, false );
274 0 : }
275 : //-----------------------------------------------------------------------------------------------------
276 :
277 : //-----------------------------------------------------------------------------------------------------
278 0 : void dbe::CustomTableView::slot_copy_object()
279 : {
280 0 : QModelIndex const & Index = this->currentIndex();
281 :
282 0 : dbe::models::tableselection const * Model =
283 0 : dynamic_cast<const dbe::models::tableselection *> ( Index.model() );
284 :
285 0 : dbe::models::table const * SourceModel = dynamic_cast<const dbe::models::table *> ( Model
286 0 : ->sourceModel() );
287 :
288 0 : if ( Index.isValid() and Model )
289 : {
290 0 : tref obj = SourceModel->GetTableObject ( Model->mapToSource ( Index ).row() );
291 :
292 0 : if ( not obj.is_null() )
293 : {
294 0 : ( new ObjectCreator ( obj ) )->show();
295 : }
296 0 : }
297 0 : }
298 : //-----------------------------------------------------------------------------------------------------
299 :
300 : //-----------------------------------------------------------------------------------------------------
301 0 : void dbe::CustomTableView::slot_create_object()
302 : {
303 : // TODO implement dbe::CustomTableView::slot_create_object
304 0 : throw std::string ( " dbe::CustomTableView::slot_create_object is not yet implemented " );
305 : }
306 : //-----------------------------------------------------------------------------------------------------
307 :
308 : //-----------------------------------------------------------------------------------------------------
309 0 : void dbe::CustomTableView::slot_delete_objects()
310 : {
311 0 : if ( this->model() != nullptr )
312 : {
313 0 : QModelIndexList qindices = this->selectedIndexes();
314 0 : std::vector<QModelIndex> indices;
315 :
316 0 : for ( QModelIndex q : qindices )
317 : {
318 0 : if ( q.isValid() )
319 : {
320 0 : indices.push_back ( q );
321 : }
322 : }
323 :
324 0 : if ( dbe::models::tableselection * casted =
325 0 : dynamic_cast<dbe::models::tableselection *> ( this->model() ) )
326 : {
327 0 : casted->delete_objects ( indices.begin(), indices.end() );
328 : }
329 0 : else if ( dbe::models::table * casted = dynamic_cast<dbe::models::table *>
330 0 : ( this->model() ) )
331 : {
332 0 : casted->delete_objects ( indices.begin(), indices.end() );
333 : }
334 : else
335 : {
336 0 : WARN ( "Object Deleting",
337 : "Object deletion failed due to the internal model being in invalid state",
338 0 : "You are advised to restart the application before proceeding any further" );
339 : }
340 0 : }
341 :
342 0 : }
343 : //-----------------------------------------------------------------------------------------------------
344 :
345 : //-----------------------------------------------------------------------------------------------------
346 0 : void dbe::CustomTableView::CreateActions()
347 : {
348 0 : editObject = new QAction ( tr ( "&Edit Object" ), this );
349 0 : connect ( editObject, SIGNAL ( triggered() ), this, SLOT ( slot_edit_object() ) );
350 0 : m_context_menu->addAction ( editObject );
351 :
352 0 : deleteObjectAc = new QAction ( tr ( "&Delete Object" ), this );
353 0 : connect ( deleteObjectAc, SIGNAL ( triggered() ), this, SLOT ( slot_delete_objects() ) );
354 0 : m_context_menu->addAction ( deleteObjectAc );
355 :
356 0 : refByAc = new QAction ( tr ( "Referenced B&y (All objects)" ), this );
357 0 : refByAc->setToolTip ( "Find all objects which reference the selected object" );
358 0 : refByAc->setStatusTip ( refByAc->toolTip() );
359 0 : connect ( refByAc, SIGNAL ( triggered() ), this, SLOT ( referencedBy_All() ) );
360 0 : m_context_menu->addAction ( refByAc );
361 :
362 0 : refByAcOnlyComp = new QAction ( tr ( "Referenced B&y (Only Composite)" ), this );
363 0 : refByAcOnlyComp->setToolTip (
364 : "Find objects (ONLY Composite ones) which reference the selected object" );
365 0 : refByAcOnlyComp->setStatusTip ( refByAcOnlyComp->toolTip() );
366 0 : connect ( refByAcOnlyComp, SIGNAL ( triggered() ), this,
367 : SLOT ( referencedBy_OnlyComposite() ) );
368 0 : m_context_menu->addAction ( refByAcOnlyComp );
369 :
370 0 : copyObjectAc = new QAction ( tr ( "Copy This Object Into A &New One" ), this );
371 0 : connect ( copyObjectAc, SIGNAL ( triggered() ), this, SLOT ( slot_copy_object() ) );
372 0 : m_context_menu->addAction ( copyObjectAc );
373 :
374 0 : m_context_menu->addSeparator();
375 0 : m_last_object_item = m_context_menu->actions().size();
376 :
377 0 : FindObject = new QAction ( tr ( "&Find Object" ), this );
378 : // FindObject->setShortcut ( QKeySequence ( tr ( "Ctrl+F" ) ) );
379 : // FindObject->setShortcutContext ( Qt::WidgetShortcut );
380 0 : connect ( FindObject, SIGNAL ( triggered() ), this, SLOT ( FindObjectSlot() ) );
381 : // addAction (FindObject);
382 0 : m_context_menu->addAction ( FindObject );
383 :
384 0 : }
385 : //-----------------------------------------------------------------------------------------------------
386 :
387 : //-----------------------------------------------------------------------------------------------------
388 0 : void dbe::CustomTableView::referencedBy ( tref obj, bool onlyComposite )
389 : {
390 0 : if ( not obj.is_null() )
391 : {
392 0 : MainWindow::findthis()->get_view()->referencedBy ( onlyComposite, obj );
393 : }
394 0 : }
395 : //-----------------------------------------------------------------------------------------------------
396 :
|