Salome HOME
refs #426: grammatic error
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_DataObject.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_DataObject.h"
24
25 #include <HYDROData_DummyObject3D.h>
26 #include <HYDROData_Object.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
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() )
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() )
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     if( aDataObject.IsNull() )
150     {
151       anIcon = QObject::tr( "HYDRO_TYPE0_ICO" ); // KIND_UNKNOWN
152     }
153     else
154     {
155       QString aNeedUpdate( aDataObject->IsMustBeUpdated() ? "M_" : "" );
156
157       int anObjectKind = (int)aDataObject->GetKind();
158       if ( anObjectKind == KIND_DUMMY_3D )
159       {
160         Handle(HYDROData_DummyObject3D) anObject3D = 
161           Handle(HYDROData_DummyObject3D)::DownCast( aDataObject );
162         
163         Handle(HYDROData_Object) aFatherObj = anObject3D->GetObject();
164         if ( !aFatherObj.IsNull() )
165           anObjectKind = aFatherObj->GetKind();
166       }
167
168       anIcon = QObject::tr( QString("HYDRO_%1TYPE%2_ICO").arg( aNeedUpdate ).arg( anObjectKind ).toAscii() );
169     }
170
171     return aResMgr->loadPixmap( "HYDRO", anIcon );
172   }
173   return LightApp_DataObject::icon( theId );
174 }
175
176 QString HYDROGUI_DataObject::dataObjectEntry( const Handle(HYDROData_Entity)& theObject,
177                                               const bool theWithPrefix )
178 {
179   QString aEntryStr = QString::null;
180   if( !theObject.IsNull() )
181   {
182     TCollection_AsciiString aLabEntr;
183     TDF_Tool::Entry( theObject->Label(), aLabEntr );
184     aEntryStr = aLabEntr.ToCString();
185     if( theWithPrefix )
186       aEntryStr.prepend( HYDROGUI_DataObject::entryPrefix() );
187   }
188   return aEntryStr;
189 }
190
191 void HYDROGUI_DataObject::setIsValid( const bool theIsValid )
192 {
193   myIsValid = theIsValid;
194 }
195
196 bool HYDROGUI_DataObject::isValid() const
197 {
198   return myIsValid;
199 }
200
201 bool HYDROGUI_DataObject::renameAllowed( const int theColumnId ) const
202 {
203   bool aRes = false;
204   if ( theColumnId == NameId && module())
205   {
206     SUIT_Operation* anOp = module()->application()->activeStudy()->activeOperation();
207     if ( anOp && anOp->inherits( "HYDROGUI_CalculationOp" ) )
208     {
209       aRes = isInOperation();
210     }
211     else
212     {
213       aRes = !anOp;
214     }
215   }
216   else
217   {
218     aRes = LightApp_DataObject::renameAllowed( theColumnId );
219   }
220   return aRes;
221 }
222
223 void HYDROGUI_DataObject::updateBy( SUIT_DataObject* theObj )
224 {
225   HYDROGUI_DataObject* aDataObj = dynamic_cast<HYDROGUI_DataObject*>( theObj );
226   if( !aDataObj )
227     return;
228
229   myData = aDataObj->myData;
230   myParentEntry = aDataObj->myParentEntry;
231   myIsValid = aDataObj->myIsValid;
232   myIsInOperation = aDataObj->myIsInOperation;
233   myIcon = aDataObj->myIcon;
234   setModified( true );
235 }
236
237
238
239
240
241
242 HYDROGUI_NamedObject::HYDROGUI_NamedObject( SUIT_DataObject* theParent,
243                                             const QString&   theName,
244                                             const QString&   theParentEntry,
245                                             const bool       theIsInOperation  )
246 : CAM_DataObject( theParent ),
247   LightApp_DataObject( theParent ),
248   myName( theName ),
249   myParentEntry( theParentEntry ),
250   myIsInOperation( theIsInOperation )
251 {
252 }
253
254 QString HYDROGUI_NamedObject::entry() const
255 {
256   QString anEntry = HYDROGUI_DataObject::entryPrefix() + name();
257   if( !myParentEntry.isEmpty() )
258     anEntry.prepend( myParentEntry + "_" );
259   return anEntry;
260 }
261
262 QString HYDROGUI_NamedObject::name() const
263 {
264   return myName.toUpper();
265 }
266
267 QPixmap HYDROGUI_NamedObject::icon( const int theId ) const
268 {
269   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
270   if ( theId == NameId )
271   {
272     return aResMgr->loadPixmap( "HYDRO", QObject::tr( "PARTITION_ICO" ) );
273   }
274   return LightApp_DataObject::icon( theId );
275 }
276
277 QFont HYDROGUI_NamedObject::font( const int theId ) const
278 {
279   QFont aFont = LightApp_DataObject::font( theId );
280   if( theId == NameId )
281   {
282   if ( dynamic_cast<CAM_ModuleObject*>( parent() ) )
283     {
284       aFont.setBold( true );
285     }
286   }
287   return aFont;
288 }
289
290 void HYDROGUI_NamedObject::updateBy( SUIT_DataObject* theObj )
291 {
292   HYDROGUI_NamedObject* aNamedObj = dynamic_cast<HYDROGUI_NamedObject*>( theObj );
293   if( !aNamedObj )
294     return;
295
296   myName = aNamedObj->myName;
297   myParentEntry = aNamedObj->myParentEntry;
298   myIcon = aNamedObj->myIcon;
299   myIsInOperation = aNamedObj->myIsInOperation;
300   setModified( true );
301 }
302
303
304
305
306
307
308 HYDROGUI_DropTargetObject::HYDROGUI_DropTargetObject( SUIT_DataObject* theParent,
309                                             const QString&   theName,
310                                             const QString&   theParentEntry,
311                                             const bool       theIsInOperation  )
312 : HYDROGUI_NamedObject( theParent, theName, theParentEntry, theIsInOperation ), 
313   CAM_DataObject( theParent )
314 {
315 }