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