]> SALOME platform Git repositories - modules/hexablock.git/blob - src/HEXABLOCKGUI/HEXABLOCKGUI_DocumentDelegate.cxx
Salome HOME
Merge from V6_main 01/04/2013
[modules/hexablock.git] / src / HEXABLOCKGUI / HEXABLOCKGUI_DocumentDelegate.cxx
1
2 // Copyright (C) 2009-2013  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       _currentEditor(NULL)
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 QWidget *DocumentDelegate::createEditor( QWidget                    *parent,
52                                          const QStyleOptionViewItem &option,
53                                          const QModelIndex          &index ) const
54 {
55
56   //close current editor if there's one before creating a new one
57   if (_currentEditor != NULL)
58   {
59           delete _currentEditor;
60           _currentEditor = NULL;
61   }
62
63   HexaBaseDialog *editor = NULL;
64
65   if (_dw->widget()) _dw->widget()->close();
66   if ( !_dw->isVisible() ) _dw->setVisible(true);
67
68   switch ( index.data(HEXA_TREE_ROLE).toInt() ){
69 //  case VERTEX_TREE :   editor = new VertexDialog(_dw, HexaBaseDialog::UPDATE_MODE);   break; //Modification
70     case VERTEX_TREE :   editor = new VertexDialog(_dw, HexaBaseDialog::INFO_MODE);   break;
71     case EDGE_TREE :     editor = new EdgeDialog(_dw, HexaBaseDialog::INFO_MODE);     break;
72     case QUAD_TREE :     editor = new QuadDialog(_dw, HexaBaseDialog::INFO_MODE);     break;
73     case HEXA_TREE :     editor = new HexaDialog(_dw, HexaBaseDialog::INFO_MODE);     break;
74     case VECTOR_TREE :   editor = new VectorDialog(_dw, HexaBaseDialog::INFO_MODE);   break;
75     case CYLINDER_TREE : editor = new CylinderDialog(_dw, HexaBaseDialog::INFO_MODE);   break;
76     case PIPE_TREE :     editor = new PipeDialog(_dw, HexaBaseDialog::INFO_MODE);       break;
77   //   case ELEMENTS_TREE :  break;
78   //   case CROSSELEMENTS_TREE : break;
79     case GROUP_TREE       : editor = new GroupDialog(_dw, HexaBaseDialog::INFO_MODE/*UPDATE_MODE*/); break;
80     case LAW_TREE         : editor = new LawDialog(_dw, HexaBaseDialog::INFO_MODE); break;
81     case PROPAGATION_TREE : editor = new PropagationDialog(_dw, HexaBaseDialog::INFO_MODE); break;
82   }
83   if ( editor != NULL ){
84     HEXABLOCKGUI::assocInProgress = false;
85
86     //show the editor in the dockwidget
87     editor->resetSizeAndShow(_dw);
88   }
89   else
90           _dw->close();
91
92
93   _currentEditor = editor;
94   return editor;
95 }
96
97 //Close the current edition dialog
98 void DocumentDelegate::closeDialog()
99 {
100         if (_currentEditor!=NULL)
101         {
102                 _currentEditor->close();
103                 emit closeEditor(_currentEditor, NoHint);
104                 delete _currentEditor;
105                 _currentEditor = NULL;
106         }
107 }
108
109 void DocumentDelegate::setEditorData( QWidget *editor,
110                                       const QModelIndex &index) const
111 {
112   HexaBaseDialog* hexaEditor = dynamic_cast<HexaBaseDialog*>( editor );
113   if (hexaEditor == NULL) return;
114   DocumentModel* documentModel = hexaEditor->getDocumentModel();
115   if (documentModel == NULL) return;
116
117   switch ( index.data(HEXA_TREE_ROLE).toInt() ){
118     case VERTEX_TREE : {
119 //      HEXA_NS::Vertex *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Vertex* >();
120       HEXA_NS::Vertex *value = documentModel->getHexaPtr<HEXA_NS::Vertex *>(index);
121       VertexDialog *vertexEditor = static_cast<VertexDialog*>(editor);
122       vertexEditor->setValue(value);
123     }
124     break;
125     case EDGE_TREE : {
126       HEXA_NS::Edge *value = documentModel->getHexaPtr<HEXA_NS::Edge*>(index);
127       EdgeDialog *edgeEditor = static_cast<EdgeDialog*>(editor);
128       edgeEditor->setValue(value);
129     }
130     break;
131     case QUAD_TREE : {
132       HEXA_NS::Quad *value = documentModel->getHexaPtr<HEXA_NS::Quad*>(index);
133       QuadDialog *quadEditor = static_cast<QuadDialog*>(editor);
134       quadEditor->setValue(value);
135     }
136     break;
137     case HEXA_TREE : {
138       HEXA_NS::Hexa *value = documentModel->getHexaPtr<HEXA_NS::Hexa*>(index);
139       HexaDialog *hexaEditor = static_cast<HexaDialog*>(editor);
140       hexaEditor->setValue(value);
141     }
142     break;
143     case VECTOR_TREE : {
144       HEXA_NS::Vector *value = documentModel->getHexaPtr<HEXA_NS::Vector*>(index);
145       VectorDialog *vectorEditor = static_cast<VectorDialog*>(editor);
146       vectorEditor->setValue(value);
147     }
148     break;
149     case CYLINDER_TREE : {
150       HEXA_NS::Cylinder *value = documentModel->getHexaPtr<HEXA_NS::Cylinder*>(index);
151       CylinderDialog *cylinderEditor = static_cast<CylinderDialog*>(editor);
152       cylinderEditor->setValue(value);
153     }
154     break;
155     case PIPE_TREE : {
156       HEXA_NS::Pipe *value = documentModel->getHexaPtr<HEXA_NS::Pipe*>(index);
157       PipeDialog *pipeEditor= static_cast<PipeDialog*>(editor);
158       pipeEditor->setValue(value);
159     }
160     break;
161 //         case ELEMENTSITEM : editor = new ElementsDialog(parent);   break;
162 //         case CROSSELEMENTSITEM : editor = new CrossElementsDialog(parent);   break;
163     case GROUP_TREE :{
164       HEXA_NS::Group *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Group* >();
165       GroupDialog *groupEditor = static_cast<GroupDialog*>(editor);
166       groupEditor->setValue(value);
167     }
168     break;
169     case LAW_TREE : {
170       HEXA_NS::Law *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Law* >();
171       LawDialog *lawEditor = static_cast<LawDialog*>(editor);
172       lawEditor->setValue(value);
173     }
174     break;
175     case PROPAGATION_TREE : {
176       HEXA_NS::Propagation *value = index.data( HEXA_DATA_ROLE ).value< HEXA_NS::Propagation* >();
177       PropagationDialog *propagationEditor = static_cast<PropagationDialog*>(editor);
178       propagationEditor->setValue(value);
179     }
180     break;
181   }
182
183 }
184
185
186 bool DocumentDelegate::editorEvent ( QEvent                     *event,
187                                      QAbstractItemModel         *model,
188                                      const QStyleOptionViewItem &option,
189                                      const QModelIndex          &index )
190 {
191 /************************************************************
192         MESSAGE("DocumentDelegate::editorEvent(){");
193         MESSAGE("*  item   is: " << index.data().toString().toStdString());
194         MESSAGE("*  event  is: " << event->type() );
195
196         Qt::ItemFlags flags = model->flags(index);
197         if ( flags == Qt::ItemFlags( ~Qt::ItemIsEditable ) ){
198                 MESSAGE("*  you can select it ");
199         } else {
200                 MESSAGE("*  you cannot select it ");
201         }
202
203         //return QItemDelegate::editorEvent ( event, model, option, index );
204  *************************************************************/
205         return false;
206 }
207
208 bool DocumentDelegate::eventFilter ( QObject * editor, QEvent * event )
209 {
210         if ( event->type() == QEvent::FocusOut ){
211                 return true; //do nothing for this signal
212         }
213         else if (event->type() == QEvent::HideToParent && editor != NULL)
214         {
215             //close the current editor when the tree is reduced
216             ((QWidget*) editor->parent())->close();
217         }
218
219         return false;
220 }