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