Salome HOME
Fix for the feature #6: Update of objects (T 1.2): For objects which need updating...
[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 <SUIT_DataObject.h>
26 #include <TDF_Tool.hxx>
27 #include <SUIT_ResourceMgr.h>
28 #include <SUIT_Session.h>
29 #include <QPixmap>
30
31 HYDROGUI_DataObject::HYDROGUI_DataObject( SUIT_DataObject* theParent, 
32                                           Handle(HYDROData_Entity) theData,
33                                           const QString& theParentEntry )
34 : CAM_DataObject( theParent ),
35   LightApp_DataObject( theParent ),
36   myData( theData ),
37   myParentEntry( theParentEntry ),
38   myIsValid( true )
39 {
40 }
41
42 QString HYDROGUI_DataObject::entry() const
43 {
44   QString anEntry = HYDROGUI_DataObject::dataObjectEntry( modelObject() );
45   if( isReference() )
46     anEntry.prepend( myParentEntry + "_" );
47   return anEntry;
48 }
49
50 QString HYDROGUI_DataObject::refEntry() const
51 {
52   if( !myParentEntry.isEmpty() )
53     return HYDROGUI_DataObject::dataObjectEntry( modelObject() );
54   return QString();
55 }
56
57 QString HYDROGUI_DataObject::name() const
58 {
59   if( !myData.IsNull() )
60     return myData->GetName();
61   return QString();
62 }
63
64 QFont HYDROGUI_DataObject::font( const int theId ) const
65 {
66   QFont aFont = LightApp_DataObject::font( theId );
67   if( theId == NameId )
68   {
69     Handle(HYDROData_Entity) aDataObject = modelObject();
70     if( !aDataObject.IsNull() && aDataObject->IsMustBeUpdated() )
71     {
72       aFont.setItalic( true );
73       aFont.setBold( true );
74     }
75   }
76   return aFont;
77 }
78
79 QColor HYDROGUI_DataObject::color( const ColorRole theRole, const int theId ) const
80 {
81   QColor aColor;
82
83   if ( !isValid() ) {
84     switch ( theRole )
85     {
86       case Text:
87       case Foreground:
88       case Highlight:
89         aColor = Qt::red; // red
90       break;
91       case HighlightedText:
92         // text color for the highlighted item
93         aColor = Qt::white;   // white
94       break;
95
96       default:
97         break;
98     }
99   }
100
101   if ( !aColor.isValid() )
102   {
103     Handle(HYDROData_Entity) aDataObject = modelObject();
104     if( !aDataObject.IsNull() && aDataObject->IsMustBeUpdated() )
105     {
106       switch ( theRole )
107       {
108         case Text:
109         case Foreground:
110         case Highlight:
111           aColor = Qt::blue;    // color for objects which need updating
112         break;
113         case HighlightedText:
114           // text color for the highlighted item
115           aColor = Qt::white;   // white
116         break;
117
118         default:
119           break;
120       }
121     }
122   }
123
124   if ( !aColor.isValid() ) {
125     aColor = LightApp_DataObject::color( theRole, theId );
126   }
127
128   return aColor;
129 }
130
131 QPixmap HYDROGUI_DataObject::icon( const int theId ) const
132 {
133   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
134   if ( theId == NameId )
135   {
136     QString anIcon;
137     Handle(HYDROData_Entity) aDataObject = modelObject();
138     if( aDataObject.IsNull() )
139     {
140       anIcon = QObject::tr( "HYDRO_TYPE0_ICO" ); // KIND_UNKNOWN
141     }
142     else
143     {
144       QString aNeedUpdate = "";
145       if ( aDataObject->IsMustBeUpdated() )
146       {
147         aNeedUpdate = "M_";
148       }
149       anIcon = QObject::tr( QString("HYDRO_%1TYPE%2_ICO").arg( aNeedUpdate ).arg( (int)aDataObject->GetKind() ).toAscii() );
150     }
151
152     return aResMgr->loadPixmap( "HYDRO", anIcon );
153   }
154   return LightApp_DataObject::icon( theId );
155 }
156
157 QString HYDROGUI_DataObject::dataObjectEntry( const Handle(HYDROData_Entity)& theObject,
158                                               const bool theWithPrefix )
159 {
160   QString aEntryStr = QString::null;
161   if( !theObject.IsNull() )
162   {
163     TCollection_AsciiString aLabEntr;
164     TDF_Tool::Entry( theObject->Label(), aLabEntr );
165     aEntryStr = aLabEntr.ToCString();
166     if( theWithPrefix )
167       aEntryStr.prepend( HYDROGUI_DataObject::entryPrefix() );
168   }
169   return aEntryStr;
170 }
171
172 void HYDROGUI_DataObject::setIsValid( const bool theIsValid )
173 {
174   myIsValid = theIsValid;
175 }
176
177 bool HYDROGUI_DataObject::isValid() const
178 {
179   return myIsValid;
180 }
181
182 HYDROGUI_NamedObject::HYDROGUI_NamedObject( SUIT_DataObject* theParent,
183                                             const QString&   theName,
184                                             const QString&   theParentEntry  )
185 : CAM_DataObject( theParent ),
186   LightApp_DataObject( theParent ),
187   myName( theName ),
188   myParentEntry( theParentEntry )
189 {
190 }
191
192 QString HYDROGUI_NamedObject::entry() const
193 {
194   QString anEntry = HYDROGUI_DataObject::entryPrefix() + name();
195   if( !myParentEntry.isEmpty() )
196     anEntry.prepend( myParentEntry + "_" );
197   return anEntry;
198 }
199
200 QString HYDROGUI_NamedObject::name() const
201 {
202   return myName;
203 }
204
205 QPixmap HYDROGUI_NamedObject::icon( const int theId ) const
206 {
207   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
208   if ( theId == NameId )
209   {
210     return aResMgr->loadPixmap( "HYDRO", QObject::tr( "PARTITION_ICO" ) );
211   }
212   return LightApp_DataObject::icon( theId );
213 }
214
215 HYDROGUI_DropTargetObject::HYDROGUI_DropTargetObject( SUIT_DataObject* theParent,
216                                             const QString&   theName,
217                                             const QString&   theParentEntry  )
218 : HYDROGUI_NamedObject( theParent, theName, theParentEntry ), CAM_DataObject( theParent )
219 {
220 }