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