]> SALOME platform Git repositories - modules/hexablock.git/blob - src/HEXABLOCKGUI/HEXABLOCKGUI_DocumentDelegate.cxx
Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/hexablock.git] / src / HEXABLOCKGUI / HEXABLOCKGUI_DocumentDelegate.cxx
1
2 // Copyright (C) 2009-2012  CEA/DEN, EDF R&D
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #include <iostream>
21 #include <QtGui>
22
23
24 #include "utilities.h"
25
26
27 #include "HEXABLOCKGUI_DocumentDelegate.hxx"
28 #include "HEXABLOCKGUI_DocumentItem.hxx"
29 #include "HEXABLOCKGUI_DocumentPanel.hxx"
30
31 using namespace std;
32 using namespace HEXABLOCK::GUI;
33
34
35 DocumentDelegate::DocumentDelegate(QDockWidget *dw, QObject *parent)
36     : QItemDelegate(parent),
37       _dw(dw),
38       _documentModel(0),
39       _patternDataSelectionModel(0),
40       _patternBuilderSelectionModel(0),
41       _groupsSelectionModel(0),
42       _meshSelectionModel(0)
43 {
44 //   connect( this, SIGNAL( closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint) ),
45 //            this, SLOT( onCloseEditor(QWidget *, QAbstractItemDelegate::EndEditHint) ) );
46 //   connect( this, SIGNAL( commitData ( QWidget * ) ),
47 //            this, SLOT( onCommitData ( QWidget * ) ) );
48 }
49
50
51 // void DocumentDelegate::commitEditor()
52 // {
53 //   MESSAGE("DocumentDelegate::commitEditor(){")
54 //   MESSAGE("*  sender is: " << sender() );
55 //   QWidget* editor = qobject_cast<QWidget*>(sender());
56 //   emit commitData(editor);
57 // //   emit closeEditor(editor);
58 //   MESSAGE("}");
59 // }
60
61 QWidget *DocumentDelegate::createEditor( QWidget                    *parent,
62                                          const QStyleOptionViewItem &option,
63                                          const QModelIndex          &index ) const
64 {
65   MESSAGE("DocumentDelegate::createEditor(){");
66   MESSAGE("*  parent is: " << parent);
67   MESSAGE("*  _dw    is: " << _dw);
68   MESSAGE("*  item   is: " << index.data(Qt::DisplayRole).toString().toStdString());
69
70   HexaBaseDialog *editor = 0;
71
72   switch ( index.data(HEXA_TREE_ROLE).toInt() ){
73     case VERTEX_TREE :   editor = new VertexDialog(parent, HexaBaseDialog::UPDATE_MODE);   break;
74     case EDGE_TREE :     editor = new EdgeDialog(parent, /*HexaBaseDialog::NEW_MODE*/HexaBaseDialog::INFO_MODE);     break;
75     case QUAD_TREE :     editor = new QuadDialog(parent, HexaBaseDialog::INFO_MODE);     break;
76     case HEXA_TREE :     editor = new HexaDialog(parent, HexaBaseDialog::INFO_MODE);     break;
77     case VECTOR_TREE :   editor = new VectorDialog(parent, HexaBaseDialog::INFO_MODE);   break;
78     case CYLINDER_TREE : editor = new CylinderDialog(parent, HexaBaseDialog::INFO_MODE);   break;
79     case PIPE_TREE :     editor = new PipeDialog(parent, HexaBaseDialog::INFO_MODE);       break;
80   //   case ELEMENTS_TREE :  break;
81   //   case CROSSELEMENTS_TREE : break;
82     case GROUP_TREE       : editor = new GroupDialog(parent, HexaBaseDialog::INFO_MODE/*UPDATE_MODE*/); break;
83     case LAW_TREE         : editor = new LawDialog(parent, HexaBaseDialog::UPDATE_MODE); break;
84     case PROPAGATION_TREE : editor = new PropagationDialog(parent, HexaBaseDialog::INFO_MODE); break;
85   }
86   if ( editor ){
87     if ( _documentModel )                editor->setDocumentModel( _documentModel );
88     if ( _patternDataSelectionModel )    editor->setPatternDataSelectionModel( _patternDataSelectionModel );
89     if ( _patternBuilderSelectionModel ) editor->setPatternBuilderSelectionModel( _patternBuilderSelectionModel);
90     if ( _groupsSelectionModel )         editor->setGroupsSelectionModel( _groupsSelectionModel );
91     if ( _meshSelectionModel )           editor->setMeshSelectionModel( _meshSelectionModel);
92
93 //     QWidget* w = _dw->widget();
94 //     if (w) w->close();
95     if ( !_dw->isVisible() ) _dw->setVisible(true);
96     _dw->setWidget( editor );
97     _dw->setWindowTitle( editor->windowTitle() );
98 //         connect ( editor, SIGNAL( editingFinished() ), this, SLOT ( commitEditor() ) );
99 //         editor->exec();
100 //     editor->show();
101 //     editor->raise();
102 //     editor->setFocus();
103   }
104
105   MESSAGE("}");
106   return editor;
107 //   return new QLineEdit(parent);
108 }
109
110
111 void DocumentDelegate::setEditorData( QWidget *editor,
112                                       const QModelIndex &index) const
113 {
114   MESSAGE("DocumentDelegate::setEditorData(){");
115   MESSAGE("*  item   is: " << index.data(Qt::DisplayRole).toString().toStdString());
116
117 //   HexaBaseDialog* editor = dynamic_cast<HexaBaseDialog*>( editor );
118
119   switch ( index.data(HEXA_TREE_ROLE).toInt() ){
120     case VERTEX_TREE : {
121       HEXA_NS::Vertex *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Vertex* >();
122       VertexDialog *vertexEditor = static_cast<VertexDialog*>(editor);
123       vertexEditor->setValue(value);
124     }
125     break;
126     case EDGE_TREE : {
127       HEXA_NS::Edge *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Edge* >();
128       EdgeDialog *edgeEditor = static_cast<EdgeDialog*>(editor);
129       edgeEditor->setValue(value);
130     }
131     break;
132     case QUAD_TREE : {
133       HEXA_NS::Quad *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Quad* >();
134       QuadDialog *quadEditor = static_cast<QuadDialog*>(editor);
135       quadEditor->setValue(value);
136     }
137     break;
138     case HEXA_TREE : {
139       HEXA_NS::Hexa *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Hexa* >();
140       HexaDialog *hexaEditor = static_cast<HexaDialog*>(editor);
141       hexaEditor->setValue(value);
142     }
143     break;
144     case VECTOR_TREE : {
145       HEXA_NS::Vector *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Vector* >();
146       VectorDialog *vectorEditor = static_cast<VectorDialog*>(editor);
147       vectorEditor->setValue(value);
148     }
149     break;
150     case CYLINDER_TREE : {
151       HEXA_NS::Cylinder *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Cylinder* >();
152       CylinderDialog *cylinderEditor = static_cast<CylinderDialog*>(editor);
153       cylinderEditor->setValue(value);
154     }
155     break;
156     case PIPE_TREE : {
157       HEXA_NS::Pipe *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Pipe* >();
158       PipeDialog *pipeEditor= static_cast<PipeDialog*>(editor);
159       pipeEditor->setValue(value);
160     }
161     break;
162 //         case ELEMENTSITEM : editor = new ElementsDialog(parent);   break;
163 //         case CROSSELEMENTSITEM : editor = new CrossElementsDialog(parent);   break;
164     case GROUP_TREE :{
165       HEXA_NS::Group *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Group* >();
166       GroupDialog *groupEditor = static_cast<GroupDialog*>(editor);
167       groupEditor->setValue(value);
168     }
169     break;
170     case LAW_TREE : {
171       HEXA_NS::Law *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Law* >();
172       LawDialog *lawEditor = static_cast<LawDialog*>(editor);
173       lawEditor->setValue(value);
174     }
175     break;
176     case PROPAGATION_TREE : {
177       HEXA_NS::Propagation *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Propagation* >();
178       PropagationDialog *propagationEditor = static_cast<PropagationDialog*>(editor);
179       propagationEditor->setValue(value);
180     }
181     break;
182   }
183
184 //   editor->setFocus();
185 //  editor->exec();
186   MESSAGE("}");
187 }
188
189 // void DocumentDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
190 //                                      const QModelIndex &index) const
191 // {
192 //     cout << "//////////////////// CS_BP DocumentDelegate::setModelData BEGIN "<< editor << endl;
193 // // //     QStandardItem  *item = 0;
194 // // //     QItemDelegate::setModelData( editor, model,index);
195 // // 
196 // // //     // CS_TODO : simplification?
197 // //     QSortFilterProxyModel *pmodel = dynamic_cast<QSortFilterProxyModel *>( model );
198 // //     if ( pmodel ){
199 // //       QStandardItemModel *smodel = dynamic_cast<QStandardItemModel *>( pmodel->sourceModel() );
200 // //       if ( smodel ){
201 // // //         item = smodel->itemFromIndex(index);
202 // //         QItemDelegate::setModelData( editor, smodel, pmodel->mapToSource(index));
203 // //       }
204 // //     } else {
205 // //       QStandardItemModel *smodel = dynamic_cast<QStandardItemModel *>( model );
206 // //       if ( smodel ){
207 // // //         item = smodel->itemFromIndex(index);
208 // //         QItemDelegate::setModelData( editor, model,index);
209 // //       }
210 // //     }
211 // //   QItemDelegate::setModelData( editor, model, index);
212 //   cout << "//////////////////// CS_BP DocumentDelegate::setModelData END"<< editor << endl;    
213 // 
214 // }
215
216
217 void DocumentDelegate::setModelData( QWidget *editor, 
218                                      QAbstractItemModel *model,
219                                      const QModelIndex &index) const
220 {
221 //   cout <<" XXXXXXXXXXXXXX setModelData editor => " << editor << endl;
222   MESSAGE("DocumentDelegate::setModelData(){");
223   MESSAGE("*  item   is: " << index.data(Qt::DisplayRole).toString().toStdString());
224 //   HexaDialog *hexaEditor = qobject_cast<HexaDialog*>(editor);
225 // //   PipeDialog *qtcast = qobject_cast<PipeDialog*>(editor);
226 // //   PipeDialog *cppcast = dynamic_cast<PipeDialog*>(editor);
227 // 
228 //   if  ( hexaEditor ){
229 //     HEXA_NS::Hexa *value  = hexaEditor->getValue();
230 //     cout << "XXXXXXXXXXX DocumentDelegate::setModelData value  "<< value  << endl;
231 //     model->setData( index, QVariant::fromValue( value ), HEXA_DATA_ROLE );
232 //     model->setData( index, value->getName(), Qt::DisplayRole/*Qt::EditRole*/ );
233 //     model->setData( index, QString::number( reinterpret_cast<intptr_t>(value) ), HEXA_ENTRY_ROLE ); 
234 //   }
235 //   editor->show();
236 //   editor->raise();
237   
238 //   cout << "AAAAAAA _dw"<< _dw<< endl;
239 //   cout << "AAAAAAA _dw->widget()"<< _dw->widget() << endl;
240 //   cout << "AAAAAAA isVisible"<< _dw->isVisible() << endl;
241 //   cout << "AAAAAAA editor isVisible"<< editor->isVisible() << endl;
242 // //   editor = new HexaDialog();
243 //   _dw->setWindowTitle( "HOOOOOOOOOO" );
244 //   _dw->setWidget(editor);
245 //   _dw->setVisible(false);
246 // //   editor->raise();
247 //   _dw->raise();
248 //   _dw->setVisible(true);
249 //   cout << "BBBBBBB _dw"<< _dw<< endl;
250 //   cout << "BBBBBBB _dw->widget()"<< _dw->widget() << endl;
251 //   cout << "BBBBBBB isVisible"<< _dw->isVisible() << endl;
252 //   cout << "BBBBBBB editor isVisible"<< editor->isVisible() << endl;
253   
254 //               HEXA_NS::Quad *value   = quadEditor->getValue();
255 //               model->setData(index, QVariant::fromValue( value ), HEXA_DATA_ROLE);
256   MESSAGE("}");
257 }
258
259
260
261
262
263 void DocumentDelegate::updateEditorGeometry(  QWidget *editor,
264                                               const QStyleOptionViewItem &option,
265                                               const QModelIndex &index ) const
266 {
267
268   MESSAGE("DocumentDelegate::updateEditorGeometry(){");
269   MESSAGE("*  item   is: " << index.data(Qt::DisplayRole).toString().toStdString());
270 //   cout << "CS_BP option.rect.size()"<<option.rect.size()<<endl;
271   MESSAGE("*  option.rect.width() is: " << option.rect.width() << option.rect.width() );
272   MESSAGE("*  option.rect.x()     is: " << option.rect.x() << option.rect.x() );
273   MESSAGE("*  option.rect.y()     is: " << option.rect.y() << option.rect.y() );
274 //   editor->setGeometry(option.rect);
275   editor->show();
276 //   editor->raise();
277   MESSAGE("}");
278 }
279
280
281
282
283
284 void DocumentDelegate::setDocumentModel( DocumentModel* m )
285 {
286   _documentModel = m;
287 }
288
289 void DocumentDelegate::setPatternDataSelectionModel( PatternDataSelectionModel* s )
290 {
291   _patternDataSelectionModel = s;
292 }
293
294 void DocumentDelegate::setPatternBuilderSelectionModel( PatternBuilderSelectionModel* s )
295 {
296   _patternBuilderSelectionModel = s;
297 }
298
299 void DocumentDelegate::setGroupsSelectionModel( GroupsSelectionModel* s )
300 {
301   _groupsSelectionModel = s ;
302 }
303
304 void DocumentDelegate::setMeshSelectionModel( MeshSelectionModel* s )
305 {
306   _meshSelectionModel = s;
307 }
308
309
310
311 bool DocumentDelegate::editorEvent ( QEvent                     *event,
312                                      QAbstractItemModel         *model,
313                                      const QStyleOptionViewItem &option,
314                                      const QModelIndex          &index )
315 {
316   MESSAGE("DocumentDelegate::editorEvent(){");
317   MESSAGE("*  item   is: " << index.data().toString().toStdString());
318   MESSAGE("*  event  is: " << event->type() );
319
320   Qt::ItemFlags flags = model->flags(index);
321   if ( flags == Qt::ItemFlags( ~Qt::ItemIsEditable ) ){
322     MESSAGE("*  you can select it ");
323   } else {
324     MESSAGE("*  you cannot select it ");
325   }
326
327
328   
329 //   bool r;
330 //   if ( event->type() == 3 ){
331 //     r = true;
332 //   } else {
333 //     r = QItemDelegate::editorEvent ( event, model, option, index );
334 //   }
335 //   MESSAGE("*  result  is: " << r );
336 //   MESSAGE("}");
337 //   return r;
338
339   return QItemDelegate::editorEvent ( event, model, option, index );
340 }
341
342 // void DocumentDelegate::onCloseEditor( QWidget *w, QAbstractItemDelegate::EndEditHint h)
343 // {
344 //   MESSAGE("DocumentDelegate::onCloseEditor(){");
345 //   MESSAGE("}");
346 // }
347 // 
348 // 
349 // void DocumentDelegate::onCommitData ( QWidget * editor )
350 // {
351 //   MESSAGE("DocumentDelegate::onCommitData(){");
352 //   MESSAGE("}");
353 // }
354
355
356 // connect( this, SIGNAL( commitData ( QWidget * ) ),
357 //          this, SLOT( onCommitData ( QWidget * ) ) );
358
359
360 // // virtual bool      eventFilter ( QObject * editor, QEvent * event )
361 // // {
362 // // //   std::cout << "eventFilter  eventFilter " << event->type() << std::endl;
363 // // //   std::cout << std::endl;
364 // // 
365 // //  if (event->type() == QEvent::FocusOut) {
366 // // //              QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
367 // // //              qDebug() << "Ate key press" << keyEvent->key();
368 // // //              return true;
369 // // return false;
370 // //          } else {
371 // //               return QItemDelegate::eventFilter ( editor, event );
372 // // //              return false;
373 // //          }
374
375
376
377 // void DocumentDelegate::setModelData( QWidget *editor, QAbstractItemModel *model,
378 //                                      const QModelIndex &index) const
379 // {
380 //     cout << "*********************** CS_BP DocumentDelegate::setModelData BEGIN "<< editor << endl;
381 //     QStandardItem  *item = 0;
382 // 
383 //     // CS_TODO : simplification?
384 //     QSortFilterProxyModel *pmodel = dynamic_cast<QSortFilterProxyModel *>( model );
385 //     if ( pmodel ){
386 //       QStandardItemModel *smodel = dynamic_cast<QStandardItemModel *>( pmodel->sourceModel() );
387 //       if ( smodel ){
388 //         item = smodel->itemFromIndex(pmodel->mapToSource(index));
389 //       }
390 //     } else {
391 //       QStandardItemModel *smodel = dynamic_cast<QStandardItemModel *>( model );
392 //       if ( smodel ){
393 //         item = smodel->itemFromIndex(index);
394 //       }
395 //     }
396 // 
397 // 
398 //     if ( item ){
399 // //       cout << "item " << item->type() << endl;
400 //       if ( item->data().isValid() )
401 //         cout << "***************   item " << item->data(Qt::DisplayRole).toString().toStdString() << endl;
402 //       switch ( item->type() ){
403 //         case VERTEXITEM : {
404 //               VertexDialog *vertexEditor = static_cast<VertexDialog*>(editor);
405 //               HEXA_NS::Vertex *value     = vertexEditor->getValue();
406 //               model->setData(index, QVariant::fromValue( value ), HEXA_DATA_ROLE);
407 //             }
408 //             break;
409 // 
410 //         case EDGEITEM : {
411 //               EdgeDialog *edgeEditor = static_cast<EdgeDialog*>(editor);
412 //               HEXA_NS::Edge *value   = edgeEditor->getValue();
413 //               model->setData(index, QVariant::fromValue( value ), HEXA_DATA_ROLE);
414 //             }
415 //             break;
416 // 
417 //         case QUADITEM : {
418 //               QuadDialog *quadEditor = static_cast<QuadDialog*>(editor);
419 //               HEXA_NS::Quad *value   = quadEditor->getValue();
420 //               model->setData(index, QVariant::fromValue( value ), HEXA_DATA_ROLE);
421 //             }
422 //             break;
423 // 
424 //         case HEXAITEM : {
425 //               HexaDialog *hexaEditor = static_cast<HexaDialog*>(editor);
426 //               HEXA_NS::Hexa *value   = hexaEditor->getValue();
427 //               model->setData(index, QVariant::fromValue( value ), HEXA_DATA_ROLE);
428 //             }
429 //             break;
430 // 
431 //         case VECTORITEM : {
432 //               VectorDialog *vectorEditor = static_cast<VectorDialog*>(editor);
433 //               HEXA_NS::Vector *value   = vectorEditor->getValue();
434 //               model->setData(index, QVariant::fromValue( value ), HEXA_DATA_ROLE);
435 //             }
436 //             break;
437 // 
438 //         case CYLINDERITEM : {
439 //               CylinderDialog *cylinderEditor = static_cast<CylinderDialog*>(editor);
440 //               HEXA_NS::Cylinder *value   = cylinderEditor->getValue();
441 //               model->setData(index, QVariant::fromValue( value ), HEXA_DATA_ROLE);
442 //             }
443 //         break;
444 //         case PIPEITEM : {
445 //               PipeDialog *pipeEditor = static_cast<PipeDialog*>(editor);
446 //               HEXA_NS::Pipe *value   = pipeEditor->getValue();
447 //               model->setData(index, QVariant::fromValue( value ), HEXA_DATA_ROLE);
448 //             }
449 //         break;
450 // //         case ELEMENTSITEM : editor = new ElementsDialog(parent);   break;
451 // //         case CROSSELEMENTSITEM : editor = new CrossElementsDialog(parent);   break;
452 //         case GROUPITEM :{
453 //               GroupDialog *groupEditor = static_cast<GroupDialog*>(editor);
454 //               HEXA_NS::Group *value = groupEditor->getValue();
455 //               model->setData(index, QVariant::fromValue( value ), HEXA_DATA_ROLE);
456 //         }
457 //         break;
458 //         case LAWITEM : {
459 //               LawDialog *lawEditor = static_cast<LawDialog*>(editor);
460 //               HEXA_NS::Law *value = lawEditor->getValue();
461 //               model->setData(index, QVariant::fromValue( value ), HEXA_DATA_ROLE);
462 //         }
463 //         break;
464 //         case PROPAGATIONITEM : {
465 //               PropagationDialog *propagationEditor = static_cast<PropagationDialog*>(editor);
466 //               HEXA_NS::Propagation *value = propagationEditor->getValue();
467 //               model->setData(index, QVariant::fromValue( value ), HEXA_DATA_ROLE);
468 //         }
469 //         break;
470 // 
471 //       }
472 //     }
473 //   editor->show();
474 //   cout << "*********************** CS_BP DocumentDelegate::setModelData END "<< editor << endl;
475 // }