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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROGUI_Zone.h"
21 #include <HYDROData_Zone.h>
22 #include <HYDROData_Object.h>
23 #include <HYDROData_IAltitudeObject.h>
24 #include <HYDROData_LandCover.h>
26 #include <SUIT_DataObject.h>
29 HYDROGUI_Zone::HYDROGUI_Zone( SUIT_DataObject* theParent,
30 Handle(HYDROData_Zone) theData,
31 const QString& theParentEntry,
32 const bool theIsInOperation )
33 : HYDROGUI_DataObject( theParent, theData, theParentEntry, theIsInOperation ), CAM_DataObject( theParent )
37 QString HYDROGUI_Zone::text( const int theColumnId ) const
40 if( !modelObject().IsNull() )
42 switch ( theColumnId )
45 // Get Ref.Object name
46 aRes = getRefObjectNames();
49 // Get altitude/land cover object name
50 aRes = getObjectName();
53 aRes = LightApp_DataObject::text( theColumnId );
59 QString HYDROGUI_Zone::getRefObjectNames() const
62 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
63 if ( !aZone.IsNull() )
65 HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
66 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
67 for ( ; anIter.More(); anIter.Next() )
69 Handle(HYDROData_Entity) aRefGeomObj =
70 Handle(HYDROData_Entity)::DownCast( anIter.Value() );
71 if ( !aRefGeomObj.IsNull() )
73 // Get Ref.Object name
74 aRes += aRefGeomObj->GetName() + ", ";
78 if ( aRes.length() > 1 )
80 aRes.remove( aRes.length() - 2, 2 );
85 QString HYDROGUI_Zone::getObjectName() const
88 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
89 if ( !aZone.IsNull() )
91 HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
92 bool isMergingNeed = aZone->IsMergingNeed();
93 if ( ( isMergingNeed && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
94 || ( aSeq.Length() == 1 ) || ( !isMergingNeed ) )
96 // Collect all used altitudes names when merging is necessary
97 // or just get the name of altitude of a single geometry object
98 // or just get the name of a single altitude
99 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
100 QSet<QString> aNamesSet;
102 for ( ; anIter.More(); anIter.Next() )
105 Handle(HYDROData_Object) aRefGeomObj =
106 Handle(HYDROData_Object)::DownCast( anIter.Value() );
107 if ( !aRefGeomObj.IsNull() )
109 // Get altitude object name
110 Handle(HYDROData_IAltitudeObject) anAltitudeObj = aRefGeomObj->GetAltitudeObject();
111 if ( !anAltitudeObj.IsNull() )
113 aName = anAltitudeObj->GetName();
114 if ( !isMergingNeed )
116 // Get the first geometry object's altitude name and go out
124 Handle(HYDROData_LandCover) aRefLandCoverObj =
125 Handle(HYDROData_LandCover)::DownCast( anIter.Value() );
126 if ( !aRefLandCoverObj.IsNull() )
128 // Get name of land cover object
129 aName = aRefLandCoverObj->GetName();
130 if ( !isMergingNeed )
132 // Get the first land cover object's altitude name and go out
139 if ( !aName.isEmpty() && !aNamesSet.contains( aName ) )
141 aRes += aName + ", ";
142 aNamesSet.insert( aName );
145 // Remove the last comma if necessary
146 if ( isMergingNeed && ( aRes.length() > 1 ) )
148 aRes.remove( aRes.length() - 2, 2 );
153 switch( aZone->GetMergeType() )
155 case HYDROData_Zone::Merge_ZMIN: // The minimum values
156 aRes = QObject::tr( "MERGE_ZMIN" );
158 case HYDROData_Zone::Merge_ZMAX: // The maximum values
159 aRes = QObject::tr( "MERGE_ZMAX" );
161 case HYDROData_Zone::Merge_Object: // Only one altitude/land cover will be taken into account
163 Handle(HYDROData_Entity) aMergeObj = aZone->GetMergeObject();
164 if ( !aMergeObj.IsNull() )
165 aRes = aMergeObj->GetName();
169 aRes = QObject::tr( "MERGE_UNKNOWN" );
176 bool HYDROGUI_Zone::isMergingNeed() const
179 if( !modelObject().IsNull() )
181 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
182 if ( !aZone.IsNull() )
184 aRes = aZone->IsMergingNeed();
190 QColor HYDROGUI_Zone::color( const ColorRole theColorRole, const int theColumnId ) const
192 // Implement red color for altitude conflicts in case creation dialog
194 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
195 if ( !aZone.IsNull() )
197 if ( ( aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN ) )
199 switch( theColorRole )
201 case Text: // editor foreground (text) color
202 case Foreground: // foreground (text) color
205 case HighlightedText: // highlighted foreground (text) color
208 case Base: // editor background color
209 case Background: // background color
210 case Highlight: // highlight background color
216 if ( !aRes.isValid() )
218 aRes = HYDROGUI_DataObject::color( theColorRole, theColumnId );
223 QStringList HYDROGUI_Zone::getObjects() const
226 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
227 if ( !aZone.IsNull() )
229 HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
230 // Collect all used altitudes/land cover names when merging is necessary
231 // or just get the name of altitude/land cover of a single object
232 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
233 for ( ; anIter.More(); anIter.Next() )
235 Handle(HYDROData_Object) aRefGeomObj =
236 Handle(HYDROData_Object)::DownCast( anIter.Value() );
237 if ( !aRefGeomObj.IsNull() )
239 Handle(HYDROData_IAltitudeObject) anAltitudeObj = aRefGeomObj->GetAltitudeObject();
240 if ( !anAltitudeObj.IsNull() && !aRes.contains( anAltitudeObj->GetName() ) )
241 aRes.append( anAltitudeObj->GetName() );
243 aRes.append( anIter.Value()->GetName() );
250 HYDROData_Zone::MergeType HYDROGUI_Zone::getMergeType() const
252 HYDROData_Zone::MergeType aRes = HYDROData_Zone::Merge_UNKNOWN;
253 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
254 if ( !aZone.IsNull() )
256 aRes = aZone->GetMergeType();
261 void HYDROGUI_Zone::setMergeType( int theMergeType, QString theMergeObjectName )
263 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
264 if ( !aZone.IsNull() )
266 HYDROData_Zone::MergeType aMergeType =
267 ( HYDROData_Zone::MergeType )theMergeType;
268 aZone->SetMergeType( aMergeType );
269 if ( aMergeType == HYDROData_Zone::Merge_Object )
271 // Find an altitude/land cover object by the given name and set it as the zone's merge altitude/land cover
272 HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
273 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
274 for ( ; anIter.More(); anIter.Next() )
276 Handle(HYDROData_Entity) aMergeObject;
278 Handle(HYDROData_Object) aRefGeomObj =
279 Handle(HYDROData_Object)::DownCast( anIter.Value() );
280 if ( !aRefGeomObj.IsNull() )
282 // Get altitude object
283 aMergeObject = aRefGeomObj->GetAltitudeObject();
285 aMergeObject = Handle(HYDROData_LandCover)::DownCast( anIter.Value() );
288 if ( !aMergeObject.IsNull() && theMergeObjectName == aMergeObject->GetName() )
290 aZone->SetMergeObject( aMergeObject );
299 \brief Check if this object is can't be renamed in place
302 \return \c true if the item can be renamed by the user in place (e.g. in the Object browser)
304 bool HYDROGUI_Zone::renameAllowed( const int id ) const
306 if ( id == NameId && isInOperation() )
310 return HYDROGUI_DataObject::renameAllowed( id );
314 // \brief Set name of this object.
316 // \return \c true if rename operation finished successfully, \c false otherwise.
318 //bool HYDROGUI_Zone::setName(const QString& theName)
320 // if ( isInOperation() )
322 // bool aRes = false;
323 // if ( !theName.isEmpty() )
325 // Handle(HYDROData_Entity) anEntity = modelObject();
326 // CAM_Module* aModule = module();
327 // if( anEntity->GetName() != theName && aModule )
329 // // check that there are no other objects with the same name in the document
330 // Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( aModule, theName );
331 // if ( anObject.IsNull() )
333 // anEntity->SetName( theName );
338 // // Inform the user that the name is already used
339 // QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
340 // QString aMessage = QObject::tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( theName );
341 // SUIT_MessageBox::critical( getApp()->desktop(), aTitle, aMessage );
348 // aRes = HYDROGUI_DataObject::setName( theName );