Salome HOME
Merge from V6_main (04/10/2012)
[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 #include <qpixmap.h>
23 #include <qrect.h>
24 #include <qstyle.h>
25
26
27 #include "utilities.h"
28 #include "HEXABLOCKGUI.hxx"
29
30
31 #include "HEXABLOCKGUI_DocumentDelegate.hxx"
32
33 using namespace std;
34 using namespace HEXABLOCK::GUI;
35
36
37 //QWidget* currentEditor = NULL;
38
39 DocumentDelegate::DocumentDelegate(QDockWidget *dw, QObject *parent)
40     : QItemDelegate(parent),
41       _dw(dw),
42       _documentModel(0),
43       _patternDataSelectionModel(0),
44       _patternBuilderSelectionModel(0),
45       _groupsSelectionModel(0),
46       _meshSelectionModel(0),
47       _currentEditor(NULL)
48 {
49 //   connect( this, SIGNAL( closeEditor(QWidget *, QAbstractItemDelegate::EndEditHint) ),
50 //            this, SLOT( onCloseEditor(QWidget *, QAbstractItemDelegate::EndEditHint) ) );
51 //   connect( this, SIGNAL( commitData ( QWidget * ) ),
52 //            this, SLOT( onCommitData ( QWidget * ) ) );
53 }
54
55
56 QWidget *DocumentDelegate::createEditor( QWidget                    *parent,
57                                          const QStyleOptionViewItem &option,
58                                          const QModelIndex          &index ) const
59 {
60   MESSAGE("DocumentDelegate::createEditor(){");
61   MESSAGE("*  parent is: " << parent);
62   MESSAGE("*  _dw    is: " << _dw);
63   MESSAGE("*  item   is: " << index.data(Qt::DisplayRole).toString().toStdString());
64
65   //close current editor if there's one before creating a new one
66   if (_currentEditor != NULL)
67   {
68           delete _currentEditor;
69           _currentEditor = NULL;
70   }
71
72   HexaBaseDialog *editor = NULL;
73
74   if (_dw->widget()) _dw->widget()->close();
75   if ( !_dw->isVisible() ) _dw->setVisible(true);
76
77   switch ( index.data(HEXA_TREE_ROLE).toInt() ){
78     case VERTEX_TREE :   editor = new VertexDialog(_dw, HexaBaseDialog::UPDATE_MODE);   break;
79     case EDGE_TREE :     editor = new EdgeDialog(_dw, /*HexaBaseDialog::NEW_MODE*/HexaBaseDialog::INFO_MODE);     break;
80     case QUAD_TREE :     editor = new QuadDialog(_dw, HexaBaseDialog::INFO_MODE);     break;
81     case HEXA_TREE :     editor = new HexaDialog(_dw, HexaBaseDialog::INFO_MODE);     break;
82     case VECTOR_TREE :   editor = new VectorDialog(_dw, HexaBaseDialog::INFO_MODE);   break;
83     case CYLINDER_TREE : editor = new CylinderDialog(_dw, HexaBaseDialog::INFO_MODE);   break;
84     case PIPE_TREE :     editor = new PipeDialog(_dw, HexaBaseDialog::INFO_MODE);       break;
85   //   case ELEMENTS_TREE :  break;
86   //   case CROSSELEMENTS_TREE : break;
87     case GROUP_TREE       : editor = new GroupDialog(_dw, HexaBaseDialog::INFO_MODE/*UPDATE_MODE*/); break;
88     case LAW_TREE         : editor = new LawDialog(_dw, HexaBaseDialog::UPDATE_MODE); break;
89     case PROPAGATION_TREE : editor = new PropagationDialog(_dw, HexaBaseDialog::INFO_MODE); break;
90   }
91   if ( editor != NULL ){
92     if ( _documentModel )                editor->setDocumentModel( _documentModel );
93     if ( _patternDataSelectionModel )
94     {
95         editor->setPatternDataSelectionModel( _patternDataSelectionModel );
96 //      _patternDataSelectionModel->currentDialog = editor;
97     }
98     if ( _patternBuilderSelectionModel ) editor->setPatternBuilderSelectionModel( _patternBuilderSelectionModel);
99     if ( _groupsSelectionModel )         editor->setGroupsSelectionModel( _groupsSelectionModel );
100     if ( _meshSelectionModel )           editor->setMeshSelectionModel( _meshSelectionModel);
101
102     HEXABLOCKGUI::assocInProgress = false;
103     //show the editor in the dockwidget
104     editor->resetSizeAndShow(_dw);
105   }
106   else
107           _dw->close();
108
109   MESSAGE("}");
110
111   _currentEditor = editor;
112   return editor;
113 }
114
115 //Close the current edition dialog
116 void DocumentDelegate::closeDialog()
117 {
118         if (_currentEditor!=NULL)
119         {
120                 _currentEditor->close();
121                 emit closeEditor(_currentEditor, NoHint);
122                 delete _currentEditor;
123                 _currentEditor = NULL;
124         }
125 }
126
127 void DocumentDelegate::setEditorData( QWidget *editor,
128                                       const QModelIndex &index) const
129 {
130   MESSAGE("DocumentDelegate::setEditorData(){");
131   MESSAGE("*  item   is: " << index.data(Qt::DisplayRole).toString().toStdString());
132
133 //   HexaBaseDialog* editor = dynamic_cast<HexaBaseDialog*>( editor );
134
135   switch ( index.data(HEXA_TREE_ROLE).toInt() ){
136     case VERTEX_TREE : {
137 //      HEXA_NS::Vertex *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Vertex* >();
138       HEXA_NS::Vertex *value = _documentModel->getHexaPtr<HEXA_NS::Vertex *>(index);
139       VertexDialog *vertexEditor = static_cast<VertexDialog*>(editor);
140       vertexEditor->setValue(value);
141     }
142     break;
143     case EDGE_TREE : {
144       HEXA_NS::Edge *value = _documentModel->getHexaPtr<HEXA_NS::Edge*>(index);
145       EdgeDialog *edgeEditor = static_cast<EdgeDialog*>(editor);
146       edgeEditor->setValue(value);
147     }
148     break;
149     case QUAD_TREE : {
150       HEXA_NS::Quad *value = _documentModel->getHexaPtr<HEXA_NS::Quad*>(index);
151       QuadDialog *quadEditor = static_cast<QuadDialog*>(editor);
152       quadEditor->setValue(value);
153     }
154     break;
155     case HEXA_TREE : {
156       HEXA_NS::Hexa *value = _documentModel->getHexaPtr<HEXA_NS::Hexa*>(index);
157       HexaDialog *hexaEditor = static_cast<HexaDialog*>(editor);
158       hexaEditor->setValue(value);
159     }
160     break;
161     case VECTOR_TREE : {
162       HEXA_NS::Vector *value = _documentModel->getHexaPtr<HEXA_NS::Vector*>(index);
163       VectorDialog *vectorEditor = static_cast<VectorDialog*>(editor);
164       vectorEditor->setValue(value);
165     }
166     break;
167     case CYLINDER_TREE : {
168       HEXA_NS::Cylinder *value = _documentModel->getHexaPtr<HEXA_NS::Cylinder*>(index);
169       CylinderDialog *cylinderEditor = static_cast<CylinderDialog*>(editor);
170       cylinderEditor->setValue(value);
171     }
172     break;
173     case PIPE_TREE : {
174       HEXA_NS::Pipe *value = _documentModel->getHexaPtr<HEXA_NS::Pipe*>(index);
175       PipeDialog *pipeEditor= static_cast<PipeDialog*>(editor);
176       pipeEditor->setValue(value);
177     }
178     break;
179 //         case ELEMENTSITEM : editor = new ElementsDialog(parent);   break;
180 //         case CROSSELEMENTSITEM : editor = new CrossElementsDialog(parent);   break;
181     case GROUP_TREE :{
182       HEXA_NS::Group *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Group* >();
183       GroupDialog *groupEditor = static_cast<GroupDialog*>(editor);
184       groupEditor->setValue(value);
185     }
186     break;
187     case LAW_TREE : {
188       HEXA_NS::Law *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Law* >();
189       LawDialog *lawEditor = static_cast<LawDialog*>(editor);
190       lawEditor->setValue(value);
191     }
192     break;
193     case PROPAGATION_TREE : {
194       HEXA_NS::Propagation *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Propagation* >();
195       PropagationDialog *propagationEditor = static_cast<PropagationDialog*>(editor);
196       propagationEditor->setValue(value);
197     }
198     break;
199   }
200
201   MESSAGE("}");
202 }
203
204
205 bool DocumentDelegate::editorEvent ( QEvent                     *event,
206                                      QAbstractItemModel         *model,
207                                      const QStyleOptionViewItem &option,
208                                      const QModelIndex          &index )
209 {
210         MESSAGE("DocumentDelegate::editorEvent(){");
211         MESSAGE("*  item   is: " << index.data().toString().toStdString());
212         MESSAGE("*  event  is: " << event->type() );
213
214         Qt::ItemFlags flags = model->flags(index);
215         if ( flags == Qt::ItemFlags( ~Qt::ItemIsEditable ) ){
216                 MESSAGE("*  you can select it ");
217         } else {
218                 MESSAGE("*  you cannot select it ");
219         }
220
221         //return QItemDelegate::editorEvent ( event, model, option, index );
222         return false;
223 }
224
225 bool DocumentDelegate::eventFilter ( QObject * editor, QEvent * event )
226 {
227         if ( event->type() == QEvent::FocusOut ){
228                 //((QWidget*) editor->parent())->close();
229                 return true;
230         }
231         return false;
232 }