Salome HOME
LOT 15
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_DataObject.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_DataObject.h"
20
21 #include <HYDROData_DummyObject3D.h>
22 #include <HYDROData_ImmersibleZone.h>
23 #include <HYDROData_Object.h>
24 #include <HYDROData_ArtificialObject.h>
25 #include <HYDROData_NaturalObject.h>
26 #include <HYDROData_BCPolygon.h>
27
28 #include <TDF_Tool.hxx>
29
30 #include <SUIT_DataObject.h>
31 #include <SUIT_ResourceMgr.h>
32 #include <SUIT_Session.h>
33 #include <SUIT_Operation.h>
34 #include <CAM_Module.h>
35 #include <CAM_Application.h>
36 #include <SUIT_Study.h>
37
38 #include <QPixmap>
39 #include <QPainter>
40
41 HYDROGUI_DataObject::HYDROGUI_DataObject( SUIT_DataObject* theParent, 
42                                           Handle(HYDROData_Entity) theData,
43                                           const QString& theParentEntry,
44                                           const bool theIsInOperation )
45 : CAM_DataObject( theParent ),
46 #ifndef TEST_MODE
47   LightApp_DataObject( theParent ),
48 #endif
49   myData( theData ),
50   myParentEntry( theParentEntry ),
51   myIsValid( true ),
52   myIsInOperation( theIsInOperation )
53 {
54 }
55
56 QString HYDROGUI_DataObject::entry() const
57 {
58   QString anEntry = HYDROGUI_DataObject::dataObjectEntry( modelObject() );
59   if( !refEntry().isEmpty() )
60     anEntry.prepend( myParentEntry + "_" );
61   return anEntry;
62 }
63
64 QString HYDROGUI_DataObject::refEntry() const
65 {
66   if( !myParentEntry.isEmpty() )
67     return HYDROGUI_DataObject::dataObjectEntry( modelObject() );
68   return QString();
69 }
70
71 QString HYDROGUI_DataObject::name() const
72 {
73   if( !myData.IsNull() )
74     return myData->GetName();
75   return QString();
76 }
77
78 QFont HYDROGUI_DataObject::font( const int theId ) const
79 {
80   QFont aFont = PARENT::font( theId );
81   if( theId == NameId )
82   {
83     Handle(HYDROData_Entity) aDataObject = modelObject();
84     if( !aDataObject.IsNull() && aDataObject->IsMustBeUpdated( HYDROData_Entity::Geom_All ) )
85     {
86       aFont.setItalic( true );
87       aFont.setBold( true );
88     }
89   }
90   return aFont;
91 }
92
93 QColor HYDROGUI_DataObject::color( const ColorRole theRole, const int theId ) const
94 {
95   QColor aColor;
96
97   if ( !isValid() ) {
98     switch ( theRole )
99     {
100       case Text:
101       case Foreground:
102       case Highlight:
103         aColor = Qt::red; // red
104       break;
105       case HighlightedText:
106         // text color for the highlighted item
107         aColor = Qt::white;   // white
108       break;
109
110       default:
111         break;
112     }
113   }
114
115   if ( !aColor.isValid() )
116   {
117     Handle(HYDROData_Entity) aDataObject = modelObject();
118     if( !aDataObject.IsNull() && aDataObject->IsMustBeUpdated( HYDROData_Entity::Geom_All ) )
119     {
120       switch ( theRole )
121       {
122         case Text:
123         case Foreground:
124         case Highlight:
125           aColor = Qt::blue;    // color for objects which need updating
126         break;
127         case HighlightedText:
128           // text color for the highlighted item
129           aColor = Qt::white;   // white
130         break;
131
132         default:
133           break;
134       }
135     }
136   }
137
138   if ( !aColor.isValid() ) {
139     aColor = PARENT::color( theRole, theId );
140   }
141
142   return aColor;
143 }
144
145 QPixmap HYDROGUI_DataObject::icon( const int theId ) const
146 {
147   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
148   if ( theId == NameId )
149   {
150     QString anIcon;
151     Handle(HYDROData_Entity) aDataObject = modelObject();
152     Handle(HYDROData_Object) anObject;
153     Handle(HYDROData_ArtificialObject) anAObject = Handle( HYDROData_ArtificialObject )::DownCast(aDataObject);
154     Handle(HYDROData_NaturalObject) aNObject = Handle( HYDROData_NaturalObject )::DownCast(aDataObject);
155     
156     if (!anAObject.IsNull())
157       anObject = anAObject;
158     if (!aNObject.IsNull())
159       anObject = aNObject;
160
161     if( aDataObject.IsNull() )
162     {
163       anIcon = QObject::tr( "HYDRO_TYPE0_ICO" ); // KIND_UNKNOWN
164     }
165     else
166     {
167       QString aNeedUpdate( aDataObject->IsMustBeUpdated( HYDROData_Entity::Geom_All ) ? "M_" : "" );
168
169       int anObjectKind = (int)aDataObject->GetKind();
170       bool IsUnsImmZone = false;
171       if ( anObjectKind == KIND_DUMMY_3D )
172       {
173         Handle(HYDROData_DummyObject3D) anObject3D = 
174           Handle(HYDROData_DummyObject3D)::DownCast( aDataObject );
175         
176         Handle(HYDROData_Object) aFatherObj = anObject3D->GetObject();
177         if ( !aFatherObj.IsNull() )
178           anObjectKind = aFatherObj->GetKind();
179         anIcon = QObject::tr( QString("HYDRO_%1TYPE%2_ICO").arg( aNeedUpdate ).arg( anObjectKind ).toLatin1() );
180       }
181       else if ( !anObject.IsNull() )
182       {
183         bool IsSubm = anObject->IsSubmersible();
184         QString anIcon1 = QObject::tr( QString("HYDRO_%1TYPE%2_ICO").arg( aNeedUpdate ).arg( anObjectKind ).toLatin1() );
185         QString anIcon2;
186         if (IsSubm) 
187           anIcon2 = QObject::tr( QString("HYDRO_SUBMERSIBLE16_ICO").toLatin1());
188         else
189           anIcon2 = QObject::tr( QString("HYDRO_UNSUBMERSIBLE16_ICO").toLatin1());
190
191         QPixmap qpm1 = aResMgr->loadPixmap( "HYDRO", anIcon1 );
192         QPixmap qpm2 = aResMgr->loadPixmap( "HYDRO", anIcon2 );
193         QPixmap qpmD(32,16);
194         qpmD.fill(QColor(0,0,0));
195         QPainter painter;
196         painter.begin(&qpmD);
197         painter.drawPixmap(0, 0, qpm1);
198         painter.drawPixmap(16, 0, qpm2);
199         painter.end();
200         return qpmD;
201       }
202       else if (anObjectKind == KIND_BC_POLYGON)
203       {
204         Handle(HYDROData_BCPolygon) aBCObj = Handle(HYDROData_BCPolygon)::DownCast( aDataObject );
205         int aBT = aBCObj->GetBoundaryType();
206         if (aBT == 1)
207           anIcon = QObject::tr( QString("HYDRO_BC_POLYGON_TYPE1_ICO").toLatin1());
208         else if (aBT == 2)
209           anIcon = QObject::tr( QString("HYDRO_BC_POLYGON_TYPE2_ICO").toLatin1());
210         else if (aBT == 3)
211           anIcon = QObject::tr( QString("HYDRO_BC_POLYGON_TYPE3_ICO").toLatin1());
212         else
213           anIcon = QObject::tr( QString("HYDRO_BC_POLYGON_TYPE_UNDEF_ICO").toLatin1());
214       }
215       else
216         anIcon = QObject::tr( QString("HYDRO_%1TYPE%2_ICO").arg( aNeedUpdate ).arg( anObjectKind ).toLatin1() );
217     }
218
219     return aResMgr->loadPixmap( "HYDRO", anIcon );
220   }
221   return PARENT::icon( theId );
222 }
223
224 QString HYDROGUI_DataObject::dataObjectEntry( const Handle(HYDROData_Entity)& theObject,
225                                               const bool theWithPrefix )
226 {
227   QString aEntryStr = QString::null;
228   if( !theObject.IsNull() )
229   {
230     TCollection_AsciiString aLabEntr;
231     TDF_Tool::Entry( theObject->Label(), aLabEntr );
232     aEntryStr = aLabEntr.ToCString();
233     if( theWithPrefix )
234       aEntryStr.prepend( HYDROGUI_DataObject::entryPrefix() );
235   }
236   return aEntryStr;
237 }
238
239 void HYDROGUI_DataObject::setIsValid( const bool theIsValid )
240 {
241   myIsValid = theIsValid;
242 }
243
244 bool HYDROGUI_DataObject::isValid() const
245 {
246   return myIsValid;
247 }
248
249 bool HYDROGUI_DataObject::renameAllowed( const int theColumnId ) const
250 {
251   bool aRes = false;
252   if ( theColumnId == NameId && module())
253   {
254     SUIT_Operation* anOp = module()->application()->activeStudy()->activeOperation();
255     if ( anOp && anOp->inherits( "HYDROGUI_CalculationOp" ) )
256     {
257       aRes = isInOperation();
258     }
259     else
260     {
261       aRes = !anOp;
262     }
263   }
264   else
265   {
266     aRes = PARENT::renameAllowed( theColumnId );
267   }
268   return aRes;
269 }
270
271 void HYDROGUI_DataObject::updateBy( SUIT_DataObject* theObj )
272 {
273   HYDROGUI_DataObject* aDataObj = dynamic_cast<HYDROGUI_DataObject*>( theObj );
274   if( !aDataObj )
275     return;
276
277   myData = aDataObj->myData;
278   myParentEntry = aDataObj->myParentEntry;
279   myIsValid = aDataObj->myIsValid;
280   myIsInOperation = aDataObj->myIsInOperation;
281   myIcon = aDataObj->myIcon;
282   setModified( true );
283 }
284
285 HYDROGUI_NamedObject::HYDROGUI_NamedObject( SUIT_DataObject* theParent,
286                                             const QString&   theName,
287                                             const QString&   theParentEntry,
288                                             const bool       theIsInOperation  )
289 : CAM_DataObject( theParent ),
290 #ifndef TEST_MODE
291   LightApp_DataObject( theParent ),
292 #endif
293   myName( theName ),
294   myParentEntry( theParentEntry ),
295   myIsInOperation( theIsInOperation )
296 {
297 }
298
299 QString HYDROGUI_NamedObject::entry() const
300 {
301   QString anEntry = HYDROGUI_DataObject::entryPrefix() + name();
302   if( !myParentEntry.isEmpty() )
303     anEntry.prepend( myParentEntry + "_" );
304   return anEntry;
305 }
306
307 QString HYDROGUI_NamedObject::name() const
308 {
309   return myName.toUpper();
310 }
311
312 QPixmap HYDROGUI_NamedObject::icon( const int theId ) const
313 {
314   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
315   if ( theId == NameId )
316   {
317     return aResMgr->loadPixmap( "HYDRO", QObject::tr( "PARTITION_ICO" ) );
318   }
319   return PARENT::icon( theId );
320 }
321
322 QFont HYDROGUI_NamedObject::font( const int theId ) const
323 {
324   QFont aFont = PARENT::font( theId );
325   if( theId == NameId )
326   {
327   if ( dynamic_cast<CAM_ModuleObject*>( parent() ) )
328     {
329       aFont.setBold( true );
330     }
331   }
332   return aFont;
333 }
334
335 void HYDROGUI_NamedObject::updateBy( SUIT_DataObject* theObj )
336 {
337   HYDROGUI_NamedObject* aNamedObj = dynamic_cast<HYDROGUI_NamedObject*>( theObj );
338   if( !aNamedObj )
339     return;
340
341   myName = aNamedObj->myName;
342   myParentEntry = aNamedObj->myParentEntry;
343   myIcon = aNamedObj->myIcon;
344   myIsInOperation = aNamedObj->myIsInOperation;
345   setModified( true );
346 }
347
348
349
350
351
352
353 HYDROGUI_DropTargetObject::HYDROGUI_DropTargetObject( SUIT_DataObject* theParent,
354                                             const QString&   theName,
355                                             const QString&   theParentEntry,
356                                             const bool       theIsInOperation  )
357 : HYDROGUI_NamedObject( theParent, theName, theParentEntry, theIsInOperation ), 
358   CAM_DataObject( theParent )
359 {
360 }