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 : #include "dbe/CustomDelegate.hpp"
7 : #include "dbe/BuildingBlockEditors.hpp"
8 : #include "dbe/TableNode.hpp"
9 : #include "dbe/Validator.hpp"
10 : #include "dbe/confaccessor.hpp"
11 : #include "dbe/tableselection.hpp"
12 : // #include "dbe/MainWindow.hpp"
13 :
14 : #include <QCompleter>
15 : #include <QKeyEvent>
16 :
17 0 : dbe::CustomDelegate::CustomDelegate(QObject* parent)
18 0 : : QItemDelegate(parent)
19 : {
20 0 : setClipping(true);
21 0 : }
22 :
23 : QWidget*
24 0 : dbe::CustomDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
25 : {
26 0 : Q_UNUSED(option);
27 0 : const dbe::models::tableselection* Model = dynamic_cast<const dbe::models::tableselection*>(index.model());
28 :
29 0 : const dbe::models::table* SourceModel = dynamic_cast<const dbe::models::table*>(Model->sourceModel());
30 :
31 0 : TableNode* Item = Model->getnode(index);
32 :
33 0 : tref Object = SourceModel->GetTableObject(Model->mapToSource(index).row());
34 :
35 0 : if (Model == nullptr) {
36 : return nullptr;
37 : }
38 :
39 0 : if (index.column() == 0) {
40 0 : emit CreateObjectEditorSignal(Object);
41 0 : return nullptr;
42 : }
43 :
44 0 : if (dynamic_cast<TableRelationshipNode*>(Item)) {
45 0 : TableRelationshipNode* RelationshipItem = dynamic_cast<TableRelationshipNode*>(Item);
46 0 : dunedaq::conffwk::relationship_t RelationshipData = RelationshipItem->GetRelationship();
47 0 : widgets::editors::relation* Editor = new widgets::editors::relation(RelationshipData);
48 0 : connect(Editor, SIGNAL(signal_edit_end()), this, SLOT(CommitAndClose()));
49 0 : connect(Editor, SIGNAL(signal_force_close()), this, SLOT(Close()));
50 0 : return Editor;
51 0 : } else if (dynamic_cast<TableAttributeNode*>(Item)) {
52 0 : TableAttributeNode* AttributeItem = dynamic_cast<TableAttributeNode*>(Item);
53 0 : dunedaq::conffwk::attribute_t AttributeData = AttributeItem->GetAttribute();
54 :
55 0 : if (AttributeData.p_is_multi_value) {
56 0 : widgets::editors::multiattr* widget = new widgets::editors::multiattr(AttributeData);
57 0 : connect(widget, SIGNAL(signal_edit_end()), this, SLOT(CommitAndClose()));
58 0 : connect(widget, SIGNAL(signal_force_close()), this, SLOT(Close()));
59 0 : return widget;
60 : } else {
61 0 : switch (AttributeData.p_type) {
62 :
63 0 : case dunedaq::conffwk::date_type:
64 :
65 0 : case dunedaq::conffwk::time_type:
66 :
67 0 : case dunedaq::conffwk::string_type: {
68 0 : widgets::editors::stringattr* Editor = new widgets::editors::stringattr(AttributeData, parent);
69 0 : connect(Editor, SIGNAL(signal_edit_end()), this, SLOT(CommitAndClose()));
70 0 : connect(Editor, SIGNAL(signal_force_close()), this, SLOT(Close()));
71 0 : return Editor;
72 : }
73 :
74 0 : case dunedaq::conffwk::double_type:
75 :
76 0 : case dunedaq::conffwk::float_type:
77 :
78 0 : case dunedaq::conffwk::s8_type:
79 :
80 0 : case dunedaq::conffwk::s16_type:
81 :
82 0 : case dunedaq::conffwk::s32_type:
83 :
84 0 : case dunedaq::conffwk::s64_type:
85 :
86 0 : case dunedaq::conffwk::u8_type:
87 :
88 0 : case dunedaq::conffwk::u16_type:
89 :
90 0 : case dunedaq::conffwk::u32_type:
91 :
92 0 : case dunedaq::conffwk::u64_type: {
93 0 : widgets::editors::numericattr* Editor = new widgets::editors::numericattr(AttributeData, parent);
94 0 : connect(Editor, SIGNAL(signal_edit_end()), this, SLOT(CommitAndClose()));
95 0 : connect(Editor, SIGNAL(signal_force_close()), this, SLOT(Close()));
96 0 : return Editor;
97 : }
98 :
99 0 : case dunedaq::conffwk::bool_type:
100 :
101 0 : case dunedaq::conffwk::enum_type: {
102 0 : widgets::editors::combo* Editor = new widgets::editors::combo(AttributeData, parent);
103 0 : connect(Editor, SIGNAL(signal_value_change()), this, SLOT(CommitAndClose()));
104 0 : connect(Editor, SIGNAL(signal_force_close()), this, SLOT(Close()));
105 0 : return Editor;
106 : }
107 :
108 0 : case dunedaq::conffwk::class_type: {
109 0 : widgets::editors::combo* Editor = new widgets::editors::combo(AttributeData, parent);
110 0 : connect(Editor, SIGNAL(signal_edit_end()), this, SLOT(CommitAndClose()));
111 0 : connect(Editor, SIGNAL(signal_force_close()), this, SLOT(Close()));
112 0 : return Editor;
113 : }
114 :
115 0 : default:
116 0 : break;
117 : }
118 : }
119 0 : }
120 :
121 : return nullptr;
122 0 : }
123 :
124 : void
125 0 : dbe::CustomDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
126 : {
127 0 : const dbe::models::tableselection* mdl = dynamic_cast<const dbe::models::tableselection*>(index.model());
128 :
129 0 : if (mdl != nullptr || editor != nullptr) {
130 0 : if (widgets::editors::base* w = dynamic_cast<widgets::editors::base*>(editor)) {
131 0 : QStringList const& data = mdl->getnode(index)->GetData();
132 0 : w->setdata(data);
133 0 : w->SetEditor();
134 0 : }
135 : }
136 0 : }
137 :
138 : void
139 0 : dbe::CustomDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
140 : {
141 0 : const dbe::models::tableselection* Model = dynamic_cast<const dbe::models::tableselection*>(index.model());
142 :
143 0 : if (Model == nullptr || editor == nullptr) {
144 0 : return;
145 : }
146 :
147 0 : widgets::editors::base* WidgetInterface = dynamic_cast<widgets::editors::base*>(editor);
148 :
149 0 : QStringList ValueList = WidgetInterface->getdata();
150 :
151 0 : model->setData(index, ValueList, Qt::EditRole);
152 0 : }
153 :
154 : void
155 0 : dbe::CustomDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
156 : {
157 0 : QItemDelegate::paint(painter, option, index);
158 :
159 : // TODO: now commented-out because of small issues with text rendering
160 : // indeed the text is too close to the cell's borders and some times artifacts are present
161 : // the alternate colors have been anyway enabled in the table view
162 : // so basically we miss the back-ground for the first column
163 :
164 : // painter->save();
165 : //
166 : // QFont fontmodif = option.font;
167 : //
168 : // if ( option.state & QStyle::State_Selected )
169 : // {
170 : // painter->setBackground ( option.palette.mid() );
171 : // painter->setRenderHint ( QPainter::Antialiasing, true );
172 : // }
173 : // else if ( index.column() == 0 )
174 : // {
175 : // painter->setBackground ( QColor::fromRgb ( 75, 222, 148 ) );
176 : // fontmodif.setBold ( true );
177 : // }
178 : // else if ( index.row() % 2 == 1 )
179 : // {
180 : // painter->setBackground ( Qt::lightGray );
181 : // }
182 : // else
183 : // {
184 : // painter->setBackground ( Qt::white );
185 : // }
186 : //
187 : // painter->setFont ( fontmodif );
188 : // painter->fillRect ( option.rect, painter->background() );
189 : // painter->drawText ( option.rect, option.displayAlignment, index.data().toString() );
190 : //
191 : // painter->restore();
192 0 : }
193 :
194 : void
195 0 : dbe::CustomDelegate::updateEditorGeometry(QWidget* editor,
196 : const QStyleOptionViewItem& option,
197 : const QModelIndex& index) const
198 : {
199 0 : Q_UNUSED(index);
200 0 : editor->setGeometry(option.rect);
201 0 : }
202 :
203 : bool
204 0 : dbe::CustomDelegate::eventFilter(QObject* editor, QEvent* event)
205 : {
206 0 : if (event->type() == QEvent::KeyPress) {
207 0 : QKeyEvent* KeyEvent = static_cast<QKeyEvent*>(event);
208 :
209 0 : if (KeyEvent->key() == Qt::Key_Enter || KeyEvent->key() == Qt::Key_Return) {
210 0 : if (dynamic_cast<widgets::editors::combo*>(editor) != 0) {
211 0 : return QItemDelegate::eventFilter(editor, event);
212 : } else {
213 : return true;
214 : }
215 : }
216 0 : } else if (event->type() == QEvent::FocusOut) {
217 : return true;
218 : }
219 :
220 0 : return QItemDelegate::eventFilter(editor, event);
221 : }
222 :
223 : bool
224 0 : dbe::CustomDelegate::editorEvent(QEvent* event,
225 : QAbstractItemModel* model,
226 : const QStyleOptionViewItem& option,
227 : const QModelIndex& index)
228 : {
229 0 : if (!(event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease)) {
230 0 : if (dynamic_cast<QKeyEvent*>(event)) {
231 0 : QKeyEvent* KeyEvent = static_cast<QKeyEvent*>(event);
232 :
233 0 : if (KeyEvent->text() != "e") {
234 : return true;
235 : }
236 : }
237 : }
238 :
239 0 : return QItemDelegate::editorEvent(event, model, option, index);
240 : }
241 :
242 : void
243 0 : dbe::CustomDelegate::CommitAndClose()
244 : {
245 0 : widgets::editors::base* Editor = qobject_cast<widgets::editors::base*>(sender());
246 :
247 0 : if (Editor) {
248 0 : emit commitData(Editor); /// This trigger : this->setModelData
249 0 : emit closeEditor(Editor);
250 0 : return;
251 : }
252 : }
253 :
254 : void
255 0 : dbe::CustomDelegate::Close()
256 : {
257 0 : widgets::editors::base* Editor = qobject_cast<widgets::editors::base*>(sender());
258 :
259 0 : if (Editor) {
260 0 : emit closeEditor(Editor);
261 0 : return;
262 : }
263 : }
|