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