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