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