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