Salome HOME
Merge from V6_main (04/10/2012)
[modules/hexablock.git] / src / HEXABLOCKGUI / HEXABLOCKGUI_DocumentItem.cxx
1 // Copyright (C) 2009-2012  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "HEXABLOCKGUI_DocumentItem.hxx"
21 #include "HexShape.hxx"
22
23 #include <inttypes.h>
24
25 using namespace std;
26 using namespace HEXABLOCK::GUI;
27
28 // ElementItem --------------------------------------------------
29 ElementItem::ElementItem( HEXA_NS::EltBase* docElement, QString entry, HexaType ttype, HexaTreeRole treeRole):
30 QStandardItem()
31 {
32         m_DocElt = docElement;
33         m_type   = ttype;
34
35         if (m_DocElt != NULL)
36                 setText(m_DocElt->getName());
37         setData( treeRole,    HEXA_TREE_ROLE );
38         setData( entry,       HEXA_DOC_ENTRY_ROLE);
39         setData( IDptr(),     HEXA_ENTRY_ROLE );
40
41         //Init assocs entry
42         if (m_DocElt!=NULL && m_DocElt->isAssociated())
43         {
44                 QString entry;
45                 if (m_type == VERTEXITEM) //Vertex
46                 {
47                         HEXA_NS::Shape* assoc = m_DocElt->getAssociation();
48                         if ( assoc ){
49                                 entry = QString(assoc->ident.c_str());
50                                 setData( entry + ";" , HEXA_ASSOC_ENTRY_ROLE );
51                         }
52                 }
53                 else if (m_type == EDGEITEM)
54                 {
55                         QString entries;
56                         const HEXA_NS::Shapes& assocs = ((HEXA_NS::Edge*)m_DocElt)->getAssociations();
57                         for( HEXA_NS::Shapes::const_iterator anAssoc = assocs.begin(); anAssoc != assocs.end(); ++anAssoc ){
58                                 entry = (*anAssoc)->ident.c_str();
59                                 entries += entry + ";";
60                         }
61                         if ( !entries.isEmpty() )
62                                 setData( entries, HEXA_ASSOC_ENTRY_ROLE );
63                 }
64                 else if (m_type == QUADITEM)
65                 {
66                         QString entries;
67                         const HEXA_NS::Shapes& assocs = ((HEXA_NS::Quad*)m_DocElt)->getAssociations();
68                         for( HEXA_NS::Shapes::const_iterator anAssoc = assocs.begin(); anAssoc != assocs.end(); ++anAssoc ){
69                                 entry = (*anAssoc)->ident.c_str();
70                                 entries += entry + ";";
71                         }
72                         if ( !entries.isEmpty() )
73                                 setData( entries, HEXA_ASSOC_ENTRY_ROLE );
74                 }
75         }
76 }
77
78
79 //ElementItem::ElementItem( HEXA_NS::EltBase* docElement, HexaType ttype, HexaTreeRole treeRole):
80 //QStandardItem()
81 //{
82 //      m_DocElt = docElement;
83 //      m_type   = ttype;
84 //
85 //      if (m_DocElt != NULL)
86 //              setText(m_DocElt->getName());
87 //      setData( treeRole,    HEXA_TREE_ROLE );
88 //      setData( IDptr(),     HEXA_ENTRY_ROLE );
89 //
90 ////    if (m_DocElt->isAssociated())
91 ////            setData(  "Y" ,     HEXA_ASSOC_ENTRY_ROLE );
92 //}
93
94 //---------------------------------------------------------------
95 QVariant ElementItem::data( int role ) const
96 {
97         if ( role == HEXA_DATA_ROLE ){
98                 switch(m_type)
99                 {
100                 case VERTEXITEM: return QVariant::fromValue( (HEXA_NS::Vertex*)m_DocElt );
101                 case EDGEITEM: return QVariant::fromValue( (HEXA_NS::Edge*)m_DocElt );
102                 case QUADITEM: return QVariant::fromValue( (HEXA_NS::Quad*)m_DocElt );
103                 case HEXAITEM: return QVariant::fromValue( (HEXA_NS::Hexa*)m_DocElt );
104                 case VECTORITEM: return QVariant::fromValue( (HEXA_NS::Vector*)m_DocElt );
105                 case CYLINDERITEM: return QVariant::fromValue( (HEXA_NS::Cylinder*)m_DocElt );
106                 case PIPEITEM: return QVariant::fromValue( (HEXA_NS::Pipe*)m_DocElt );
107                 case ELEMENTSITEM: return QVariant::fromValue( (HEXA_NS::Elements*)m_DocElt );
108                 case CROSSELEMENTSITEM: return QVariant::fromValue( (HEXA_NS::CrossElements*)m_DocElt );
109                 default: return QVariant::fromValue( m_DocElt );
110                 }
111         }
112
113         if (role == Qt::ForegroundRole ) {
114                 if ( m_DocElt->isAssociated() )
115                         return QColor(Qt::darkGreen);
116                 else
117                         return QColor(Qt::black);
118         }
119         return QStandardItem::data( role );
120 }
121
122 //---------------------------------------------------------------
123 void ElementItem::setData ( const QVariant& valcont, int role )
124 {
125         if ( role == HEXA_DATA_ROLE ){
126                 m_DocElt = valcont.value<HEXA_NS::EltBase*>();
127                 emitDataChanged ();
128
129         } else {
130                 QStandardItem::setData ( valcont, role );
131         }
132 }
133 //---------------------------------------------------------------
134 int ElementItem::type () const {return m_type;}
135 //---------------------------------------------------------------
136 bool ElementItem::isAssoc () const {return m_DocElt->isAssociated();}
137 //---------------------------------------------------------------
138 QString ElementItem::IDptr() const {  return QString::number( reinterpret_cast<intptr_t>(m_DocElt) ); }
139
140
141 // ----------------------- VERTEX (DATA)
142 VertexItem::VertexItem( HEXA_NS::Vertex* hexaVertex, QString entry):
143                 GraphicElementItem(hexaVertex, entry, VERTEXITEM, VERTEX_TREE)
144 {
145 }
146
147 // ----------------------- EDGE   (DATA)
148 EdgeItem::EdgeItem( HEXA_NS::Edge* hexaEdge, QString entry ):
149                 GraphicElementItem(hexaEdge, entry, EDGEITEM, EDGE_TREE)
150 {
151 }
152
153 // ----------------------- QUAD   (DATA)
154 QuadItem::QuadItem( HEXA_NS::Quad* hexaQuad, QString entry):
155                 GraphicElementItem(hexaQuad, entry, QUADITEM, QUAD_TREE)
156 {
157 }
158
159 // ----------------------- HEXA   (DATA)
160 HexaItem::HexaItem( HEXA_NS::Hexa* hexaHexa , QString entry):
161                 GraphicElementItem(hexaHexa,entry, HEXAITEM, HEXA_TREE)
162 {
163 }
164
165 // ----------------------- VECTOR   (BUILDER)
166 VectorItem::VectorItem( HEXA_NS::Vector* hexaVector, QString entry ):
167                 StandardElementItem(hexaVector, entry, VECTORITEM, VECTOR_TREE)
168 {
169 }
170
171 // ----------------------- CYLINDER (BUILDER)
172 CylinderItem::CylinderItem( HEXA_NS::Cylinder* hexaCylinder, QString entry):
173                 StandardElementItem(hexaCylinder, entry, CYLINDERITEM, CYLINDER_TREE)
174 {
175 }
176
177 // ----------------------- PIPE     (BUILDER)
178 PipeItem::PipeItem( HEXA_NS::Pipe* hexaPipe, QString entry ):
179                 StandardElementItem(hexaPipe, entry, PIPEITEM, PIPE_TREE)
180 {
181 }
182
183 // ----------------------- ELEMENTS      (NOT USED)
184 ElementsItem::ElementsItem( HEXA_NS::Elements* hexaElements, QString entry ):
185                 StandardElementItem(hexaElements, entry, ELEMENTSITEM, ELEMENTS_TREE)
186 {
187 }
188
189 // ----------------------- CROSSELEMENTS (NOT USED)
190 CrossElementsItem::CrossElementsItem( HEXA_NS::CrossElements* hexaCrossElts, QString entry ):
191                 StandardElementItem(hexaCrossElts, entry, CROSSELEMENTSITEM, CROSSELEMENTS_TREE)
192 {
193 }
194
195 // ----------------------- GROUP
196 GroupItem::GroupItem( HEXA_NS::Group* hexaGroup ):
197   QStandardItem(),
198   _hexaGroup( hexaGroup )
199 {
200 //   char pName[12];
201   QString name = _hexaGroup->getName();//pName);
202   setText(name);
203   setData( GROUP_TREE, HEXA_TREE_ROLE );
204   setData( QString::number(reinterpret_cast<intptr_t>(_hexaGroup)), HEXA_ENTRY_ROLE );
205 }
206
207 int GroupItem::type() const
208 {
209   return GROUPITEM;
210 }
211
212 QVariant GroupItem::data( int role ) const
213 {
214     if ( role == HEXA_DATA_ROLE ){
215       return QVariant::fromValue( _hexaGroup );
216     } else {
217       return QStandardItem::data( role );
218     }
219 }
220
221 void GroupItem::setData ( const QVariant& value, int role )
222 {
223     if ( role == HEXA_DATA_ROLE ){
224       _hexaGroup = value.value<HEXA_NS::Group*>();
225       emitDataChanged ();
226     } else {
227       QStandardItem::setData ( value, role );
228     }
229 }
230
231
232
233 // ----------------------- LAW
234 LawItem::LawItem( HEXA_NS::Law* hexaLaw ):
235   QStandardItem(),
236   _hexaLaw( hexaLaw )
237 {
238 //   char pName[12];
239   QString name = _hexaLaw->getName();//pName);
240   setText(name);
241   setData( LAW_TREE, HEXA_TREE_ROLE );
242   setData( QString::number(reinterpret_cast<intptr_t>(_hexaLaw)), HEXA_ENTRY_ROLE );
243 }
244
245 int LawItem::type() const
246 {
247   return LAWITEM;
248 }
249
250 QVariant LawItem::data( int role ) const
251 {
252     if ( role == HEXA_DATA_ROLE ){
253       return QVariant::fromValue( _hexaLaw);
254     } else {
255       return QStandardItem::data( role );
256     }
257 }
258
259 void LawItem::setData ( const QVariant & value, int role )
260 {
261     if ( role == HEXA_DATA_ROLE ){
262       _hexaLaw = value.value<HEXA_NS::Law*>();
263       emitDataChanged ();
264     } else {
265       QStandardItem::setData ( value, role );
266     }
267 }
268
269
270
271 // ----------------------- PROPAGATIONITEM
272 PropagationItem::PropagationItem( HEXA_NS::Propagation* hexaPropagation ):
273   QStandardItem(),
274   _hexaPropagation( hexaPropagation )
275 {
276 //   char pName[12];
277 //   QString name = _hexaPropagation->getName(pName);
278   QString name = "Propagation";
279   setText(name);
280   setData( PROPAGATION_TREE, HEXA_TREE_ROLE );
281   setData( QString::number(reinterpret_cast<intptr_t>(_hexaPropagation)), HEXA_ENTRY_ROLE );
282 }
283
284 int PropagationItem::type() const
285 {
286   return PROPAGATIONITEM;
287 }
288
289 QVariant PropagationItem::data( int role ) const
290 {
291     if ( role == HEXA_DATA_ROLE ){
292       return QVariant::fromValue( _hexaPropagation );
293     } else {
294       return QStandardItem::data( role );
295     }
296 }
297
298 void PropagationItem::setData ( const QVariant & value, int role )
299 {
300     if ( role == HEXA_DATA_ROLE ){
301       _hexaPropagation = value.value<HEXA_NS::Propagation*>();
302       emitDataChanged ();
303     } else {
304       QStandardItem::setData ( value, role );
305     }
306 }
307 //-------------------------------------------------
308
309