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