]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_DataObject.cxx
Salome HOME
Merge remote-tracking branch 'origin/BR_1323_0' into BR_2017
[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
27 #include <TDF_Tool.hxx>
28
29 #include <SUIT_DataObject.h>
30 #include <SUIT_ResourceMgr.h>
31 #include <SUIT_Session.h>
32 #include <SUIT_Operation.h>
33 #include <CAM_Module.h>
34 #include <CAM_Application.h>
35 #include <SUIT_Study.h>
36
37 #include <QPixmap>
38 #include <QPainter>
39
40 HYDROGUI_DataObject::HYDROGUI_DataObject( SUIT_DataObject* theParent, 
41                                           Handle(HYDROData_Entity) theData,
42                                           const QString& theParentEntry,
43                                           const bool theIsInOperation )
44 : CAM_DataObject( theParent ),
45 #ifndef TEST_MODE
46   LightApp_DataObject( theParent ),
47 #endif
48   myData( theData ),
49   myParentEntry( theParentEntry ),
50   myIsValid( true ),
51   myIsInOperation( theIsInOperation )
52 {
53 }
54
55 QString HYDROGUI_DataObject::entry() const
56 {
57   QString anEntry = HYDROGUI_DataObject::dataObjectEntry( modelObject() );
58   if( !refEntry().isEmpty() )
59     anEntry.prepend( myParentEntry + "_" );
60   return anEntry;
61 }
62
63 QString HYDROGUI_DataObject::refEntry() const
64 {
65   if( !myParentEntry.isEmpty() )
66     return HYDROGUI_DataObject::dataObjectEntry( modelObject() );
67   return QString();
68 }
69
70 QString HYDROGUI_DataObject::name() const
71 {
72   if( !myData.IsNull() )
73     return myData->GetName();
74   return QString();
75 }
76
77 QFont HYDROGUI_DataObject::font( const int theId ) const
78 {
79   QFont aFont = PARENT::font( theId );
80   if( theId == NameId )
81   {
82     Handle(HYDROData_Entity) aDataObject = modelObject();
83     if( !aDataObject.IsNull() && aDataObject->IsMustBeUpdated( HYDROData_Entity::Geom_All ) )
84     {
85       aFont.setItalic( true );
86       aFont.setBold( true );
87     }
88   }
89   return aFont;
90 }
91
92 QColor HYDROGUI_DataObject::color( const ColorRole theRole, const int theId ) const
93 {
94   QColor aColor;
95
96   if ( !isValid() ) {
97     switch ( theRole )
98     {
99       case Text:
100       case Foreground:
101       case Highlight:
102         aColor = Qt::red; // red
103       break;
104       case HighlightedText:
105         // text color for the highlighted item
106         aColor = Qt::white;   // white
107       break;
108
109       default:
110         break;
111     }
112   }
113
114   if ( !aColor.isValid() )
115   {
116     Handle(HYDROData_Entity) aDataObject = modelObject();
117     if( !aDataObject.IsNull() && aDataObject->IsMustBeUpdated( HYDROData_Entity::Geom_All ) )
118     {
119       switch ( theRole )
120       {
121         case Text:
122         case Foreground:
123         case Highlight:
124           aColor = Qt::blue;    // color for objects which need updating
125         break;
126         case HighlightedText:
127           // text color for the highlighted item
128           aColor = Qt::white;   // white
129         break;
130
131         default:
132           break;
133       }
134     }
135   }
136
137   if ( !aColor.isValid() ) {
138     aColor = PARENT::color( theRole, theId );
139   }
140
141   return aColor;
142 }
143
144 QPixmap HYDROGUI_DataObject::icon( const int theId ) const
145 {
146   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
147   if ( theId == NameId )
148   {
149     QString anIcon;
150     Handle(HYDROData_Entity) aDataObject = modelObject();
151     Handle(HYDROData_Object) anObject;
152     Handle(HYDROData_ArtificialObject) anAObject = Handle( HYDROData_ArtificialObject )::DownCast(aDataObject);
153     Handle(HYDROData_NaturalObject) aNObject = Handle( HYDROData_NaturalObject )::DownCast(aDataObject);
154     
155     if (!anAObject.IsNull())
156       anObject = anAObject;
157     if (!aNObject.IsNull())
158       anObject = aNObject;
159
160     if( aDataObject.IsNull() )
161     {
162       anIcon = QObject::tr( "HYDRO_TYPE0_ICO" ); // KIND_UNKNOWN
163     }
164     else
165     {
166       QString aNeedUpdate( aDataObject->IsMustBeUpdated( HYDROData_Entity::Geom_All ) ? "M_" : "" );
167
168       int anObjectKind = (int)aDataObject->GetKind();
169       bool IsUnsImmZone = false;
170       if ( anObjectKind == KIND_DUMMY_3D )
171       {
172         Handle(HYDROData_DummyObject3D) anObject3D = 
173           Handle(HYDROData_DummyObject3D)::DownCast( aDataObject );
174         
175         Handle(HYDROData_Object) aFatherObj = anObject3D->GetObject();
176         if ( !aFatherObj.IsNull() )
177           anObjectKind = aFatherObj->GetKind();
178       }
179       else if ( !anObject.IsNull() )
180       {
181         bool IsSubm = anObject->IsSubmersible();
182         QString anIcon1 = QObject::tr( QString("HYDRO_%1TYPE%2_ICO").arg( aNeedUpdate ).arg( anObjectKind ).toLatin1() );
183         QString anIcon2;
184         if (IsSubm) 
185           anIcon2 = QObject::tr( QString("HYDRO_SUBMERSIBLE16_ICO").toLatin1());
186         else
187           anIcon2 = QObject::tr( QString("HYDRO_UNSUBMERSIBLE16_ICO").toLatin1());
188
189         QPixmap qpm1 = aResMgr->loadPixmap( "HYDRO", anIcon1 );
190         QPixmap qpm2 = aResMgr->loadPixmap( "HYDRO", anIcon2 );
191         QPixmap qpmD(32,16);
192         qpmD.fill(QColor(0,0,0));
193         QPainter painter;
194         painter.begin(&qpmD);
195         painter.drawPixmap(0, 0, qpm1);
196         painter.drawPixmap(16, 0, qpm2);
197         painter.end();
198         return qpmD;
199       }
200       else
201         anIcon = QObject::tr( QString("HYDRO_%1TYPE%2_ICO").arg( aNeedUpdate ).arg( anObjectKind ).toLatin1() );
202     }
203
204     return aResMgr->loadPixmap( "HYDRO", anIcon );
205   }
206   return PARENT::icon( theId );
207 }
208
209 QString HYDROGUI_DataObject::dataObjectEntry( const Handle(HYDROData_Entity)& theObject,
210                                               const bool theWithPrefix )
211 {
212   QString aEntryStr = QString::null;
213   if( !theObject.IsNull() )
214   {
215     TCollection_AsciiString aLabEntr;
216     TDF_Tool::Entry( theObject->Label(), aLabEntr );
217     aEntryStr = aLabEntr.ToCString();
218     if( theWithPrefix )
219       aEntryStr.prepend( HYDROGUI_DataObject::entryPrefix() );
220   }
221   return aEntryStr;
222 }
223
224 void HYDROGUI_DataObject::setIsValid( const bool theIsValid )
225 {
226   myIsValid = theIsValid;
227 }
228
229 bool HYDROGUI_DataObject::isValid() const
230 {
231   return myIsValid;
232 }
233
234 bool HYDROGUI_DataObject::renameAllowed( const int theColumnId ) const
235 {
236   bool aRes = false;
237   if ( theColumnId == NameId && module())
238   {
239     SUIT_Operation* anOp = module()->application()->activeStudy()->activeOperation();
240     if ( anOp && anOp->inherits( "HYDROGUI_CalculationOp" ) )
241     {
242       aRes = isInOperation();
243     }
244     else
245     {
246       aRes = !anOp;
247     }
248   }
249   else
250   {
251     aRes = PARENT::renameAllowed( theColumnId );
252   }
253   return aRes;
254 }
255
256 void HYDROGUI_DataObject::updateBy( SUIT_DataObject* theObj )
257 {
258   HYDROGUI_DataObject* aDataObj = dynamic_cast<HYDROGUI_DataObject*>( theObj );
259   if( !aDataObj )
260     return;
261
262   myData = aDataObj->myData;
263   myParentEntry = aDataObj->myParentEntry;
264   myIsValid = aDataObj->myIsValid;
265   myIsInOperation = aDataObj->myIsInOperation;
266   myIcon = aDataObj->myIcon;
267   setModified( true );
268 }
269
270 HYDROGUI_NamedObject::HYDROGUI_NamedObject( SUIT_DataObject* theParent,
271                                             const QString&   theName,
272                                             const QString&   theParentEntry,
273                                             const bool       theIsInOperation  )
274 : CAM_DataObject( theParent ),
275 #ifndef TEST_MODE
276   LightApp_DataObject( theParent ),
277 #endif
278   myName( theName ),
279   myParentEntry( theParentEntry ),
280   myIsInOperation( theIsInOperation )
281 {
282 }
283
284 QString HYDROGUI_NamedObject::entry() const
285 {
286   QString anEntry = HYDROGUI_DataObject::entryPrefix() + name();
287   if( !myParentEntry.isEmpty() )
288     anEntry.prepend( myParentEntry + "_" );
289   return anEntry;
290 }
291
292 QString HYDROGUI_NamedObject::name() const
293 {
294   return myName.toUpper();
295 }
296
297 QPixmap HYDROGUI_NamedObject::icon( const int theId ) const
298 {
299   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
300   if ( theId == NameId )
301   {
302     return aResMgr->loadPixmap( "HYDRO", QObject::tr( "PARTITION_ICO" ) );
303   }
304   return PARENT::icon( theId );
305 }
306
307 QFont HYDROGUI_NamedObject::font( const int theId ) const
308 {
309   QFont aFont = PARENT::font( theId );
310   if( theId == NameId )
311   {
312   if ( dynamic_cast<CAM_ModuleObject*>( parent() ) )
313     {
314       aFont.setBold( true );
315     }
316   }
317   return aFont;
318 }
319
320 void HYDROGUI_NamedObject::updateBy( SUIT_DataObject* theObj )
321 {
322   HYDROGUI_NamedObject* aNamedObj = dynamic_cast<HYDROGUI_NamedObject*>( theObj );
323   if( !aNamedObj )
324     return;
325
326   myName = aNamedObj->myName;
327   myParentEntry = aNamedObj->myParentEntry;
328   myIcon = aNamedObj->myIcon;
329   myIsInOperation = aNamedObj->myIsInOperation;
330   setModified( true );
331 }
332
333
334
335
336
337
338 HYDROGUI_DropTargetObject::HYDROGUI_DropTargetObject( SUIT_DataObject* theParent,
339                                             const QString&   theName,
340                                             const QString&   theParentEntry,
341                                             const bool       theIsInOperation  )
342 : HYDROGUI_NamedObject( theParent, theName, theParentEntry, theIsInOperation ), 
343   CAM_DataObject( theParent )
344 {
345 }