]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROGUI/HYDROGUI_DataObject.cxx
Salome HOME
portage V8_5_0
[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         anIcon = QObject::tr( QString("HYDRO_%1TYPE%2_ICO").arg( aNeedUpdate ).arg( anObjectKind ).toLatin1() );
179       }
180       else if ( !anObject.IsNull() )
181       {
182         bool IsSubm = anObject->IsSubmersible();
183         QString anIcon1 = QObject::tr( QString("HYDRO_%1TYPE%2_ICO").arg( aNeedUpdate ).arg( anObjectKind ).toLatin1() );
184         QString anIcon2;
185         if (IsSubm) 
186           anIcon2 = QObject::tr( QString("HYDRO_SUBMERSIBLE16_ICO").toLatin1());
187         else
188           anIcon2 = QObject::tr( QString("HYDRO_UNSUBMERSIBLE16_ICO").toLatin1());
189
190         QPixmap qpm1 = aResMgr->loadPixmap( "HYDRO", anIcon1 );
191         QPixmap qpm2 = aResMgr->loadPixmap( "HYDRO", anIcon2 );
192         QPixmap qpmD(32,16);
193         qpmD.fill(QColor(0,0,0));
194         QPainter painter;
195         painter.begin(&qpmD);
196         painter.drawPixmap(0, 0, qpm1);
197         painter.drawPixmap(16, 0, qpm2);
198         painter.end();
199         return qpmD;
200       }
201       else
202         anIcon = QObject::tr( QString("HYDRO_%1TYPE%2_ICO").arg( aNeedUpdate ).arg( anObjectKind ).toLatin1() );
203     }
204
205     return aResMgr->loadPixmap( "HYDRO", anIcon );
206   }
207   return PARENT::icon( theId );
208 }
209
210 QString HYDROGUI_DataObject::dataObjectEntry( const Handle(HYDROData_Entity)& theObject,
211                                               const bool theWithPrefix )
212 {
213   QString aEntryStr = QString::null;
214   if( !theObject.IsNull() )
215   {
216     TCollection_AsciiString aLabEntr;
217     TDF_Tool::Entry( theObject->Label(), aLabEntr );
218     aEntryStr = aLabEntr.ToCString();
219     if( theWithPrefix )
220       aEntryStr.prepend( HYDROGUI_DataObject::entryPrefix() );
221   }
222   return aEntryStr;
223 }
224
225 void HYDROGUI_DataObject::setIsValid( const bool theIsValid )
226 {
227   myIsValid = theIsValid;
228 }
229
230 bool HYDROGUI_DataObject::isValid() const
231 {
232   return myIsValid;
233 }
234
235 bool HYDROGUI_DataObject::renameAllowed( const int theColumnId ) const
236 {
237   bool aRes = false;
238   if ( theColumnId == NameId && module())
239   {
240     SUIT_Operation* anOp = module()->application()->activeStudy()->activeOperation();
241     if ( anOp && anOp->inherits( "HYDROGUI_CalculationOp" ) )
242     {
243       aRes = isInOperation();
244     }
245     else
246     {
247       aRes = !anOp;
248     }
249   }
250   else
251   {
252     aRes = PARENT::renameAllowed( theColumnId );
253   }
254   return aRes;
255 }
256
257 void HYDROGUI_DataObject::updateBy( SUIT_DataObject* theObj )
258 {
259   HYDROGUI_DataObject* aDataObj = dynamic_cast<HYDROGUI_DataObject*>( theObj );
260   if( !aDataObj )
261     return;
262
263   myData = aDataObj->myData;
264   myParentEntry = aDataObj->myParentEntry;
265   myIsValid = aDataObj->myIsValid;
266   myIsInOperation = aDataObj->myIsInOperation;
267   myIcon = aDataObj->myIcon;
268   setModified( true );
269 }
270
271 HYDROGUI_NamedObject::HYDROGUI_NamedObject( SUIT_DataObject* theParent,
272                                             const QString&   theName,
273                                             const QString&   theParentEntry,
274                                             const bool       theIsInOperation  )
275 : CAM_DataObject( theParent ),
276 #ifndef TEST_MODE
277   LightApp_DataObject( theParent ),
278 #endif
279   myName( theName ),
280   myParentEntry( theParentEntry ),
281   myIsInOperation( theIsInOperation )
282 {
283 }
284
285 QString HYDROGUI_NamedObject::entry() const
286 {
287   QString anEntry = HYDROGUI_DataObject::entryPrefix() + name();
288   if( !myParentEntry.isEmpty() )
289     anEntry.prepend( myParentEntry + "_" );
290   return anEntry;
291 }
292
293 QString HYDROGUI_NamedObject::name() const
294 {
295   return myName.toUpper();
296 }
297
298 QPixmap HYDROGUI_NamedObject::icon( const int theId ) const
299 {
300   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
301   if ( theId == NameId )
302   {
303     return aResMgr->loadPixmap( "HYDRO", QObject::tr( "PARTITION_ICO" ) );
304   }
305   return PARENT::icon( theId );
306 }
307
308 QFont HYDROGUI_NamedObject::font( const int theId ) const
309 {
310   QFont aFont = PARENT::font( theId );
311   if( theId == NameId )
312   {
313   if ( dynamic_cast<CAM_ModuleObject*>( parent() ) )
314     {
315       aFont.setBold( true );
316     }
317   }
318   return aFont;
319 }
320
321 void HYDROGUI_NamedObject::updateBy( SUIT_DataObject* theObj )
322 {
323   HYDROGUI_NamedObject* aNamedObj = dynamic_cast<HYDROGUI_NamedObject*>( theObj );
324   if( !aNamedObj )
325     return;
326
327   myName = aNamedObj->myName;
328   myParentEntry = aNamedObj->myParentEntry;
329   myIcon = aNamedObj->myIcon;
330   myIsInOperation = aNamedObj->myIsInOperation;
331   setModified( true );
332 }
333
334
335
336
337
338
339 HYDROGUI_DropTargetObject::HYDROGUI_DropTargetObject( SUIT_DataObject* theParent,
340                                             const QString&   theName,
341                                             const QString&   theParentEntry,
342                                             const bool       theIsInOperation  )
343 : HYDROGUI_NamedObject( theParent, theName, theParentEntry, theIsInOperation ), 
344   CAM_DataObject( theParent )
345 {
346 }