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>
25 #include <SUIT_DataObject.h>
28 HYDROGUI_Zone::HYDROGUI_Zone( SUIT_DataObject* theParent,
29 Handle(HYDROData_Zone) theData,
30 const QString& theParentEntry,
31 const bool theIsInOperation )
32 : HYDROGUI_DataObject( theParent, theData, theParentEntry, theIsInOperation ), CAM_DataObject( theParent )
36 QString HYDROGUI_Zone::text( const int theColumnId ) const
39 if( !modelObject().IsNull() )
41 switch ( theColumnId )
44 // Get Ref.Object name
45 aRes = getRefObjectNames();
48 // Get altitude/land cover object name
49 aRes = getObjectName();
52 aRes = LightApp_DataObject::text( theColumnId );
58 QString HYDROGUI_Zone::getRefObjectNames() const
61 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
62 if ( !aZone.IsNull() )
64 HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
65 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
66 for ( ; anIter.More(); anIter.Next() )
68 Handle(HYDROData_Entity) aRefGeomObj =
69 Handle(HYDROData_Entity)::DownCast( anIter.Value() );
70 if ( !aRefGeomObj.IsNull() )
72 // Get Ref.Object name
73 aRes += aRefGeomObj->GetName() + ", ";
77 if ( aRes.length() > 1 )
79 aRes.remove( aRes.length() - 2, 2 );
84 QString HYDROGUI_Zone::getObjectName() const
87 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
88 if ( !aZone.IsNull() )
90 HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
91 bool isMergingNeed = aZone->IsMergingNeed();
92 if ( ( isMergingNeed && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
93 || ( aSeq.Length() == 1 ) || ( !isMergingNeed ) )
95 // Collect all used altitudes names when merging is necessary
96 // or just get the name of altitude of a single geometry object
97 // or just get the name of a single altitude
98 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
99 QSet<QString> aNamesSet;
101 for ( ; anIter.More(); anIter.Next() )
104 Handle(HYDROData_Object) aRefGeomObj =
105 Handle(HYDROData_Object)::DownCast( anIter.Value() );
106 if ( !aRefGeomObj.IsNull() )
108 // Get altitude object name
109 Handle(HYDROData_IAltitudeObject) anAltitudeObj = aRefGeomObj->GetAltitudeObject();
110 if ( !anAltitudeObj.IsNull() )
112 aName = anAltitudeObj->GetName();
113 if ( !isMergingNeed )
115 // Get the first geometry object's altitude name and go out
122 if ( !aName.isEmpty() && !aNamesSet.contains( aName ) )
124 aRes += aName + ", ";
125 aNamesSet.insert( aName );
128 // Remove the last comma if necessary
129 if ( isMergingNeed && ( aRes.length() > 1 ) )
131 aRes.remove( aRes.length() - 2, 2 );
136 switch( aZone->GetMergeType() )
138 case HYDROData_Zone::Merge_ZMIN: // The minimum values
139 aRes = QObject::tr( "MERGE_ZMIN" );
141 case HYDROData_Zone::Merge_ZMAX: // The maximum values
142 aRes = QObject::tr( "MERGE_ZMAX" );
144 case HYDROData_Zone::Merge_Object: // Only one altitude/land cover will be taken into account
146 Handle(HYDROData_Entity) aMergeObj = aZone->GetMergeObject();
147 if ( !aMergeObj.IsNull() )
148 aRes = aMergeObj->GetName();
152 aRes = QObject::tr( "MERGE_UNKNOWN" );
159 bool HYDROGUI_Zone::isMergingNeed() const
162 if( !modelObject().IsNull() )
164 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
165 if ( !aZone.IsNull() )
167 aRes = aZone->IsMergingNeed();
173 QColor HYDROGUI_Zone::color( const ColorRole theColorRole, const int theColumnId ) const
175 // Implement red color for altitude conflicts in case creation dialog
177 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
178 if ( !aZone.IsNull() )
180 if ( ( aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN ) )
182 switch( theColorRole )
184 case Text: // editor foreground (text) color
185 case Foreground: // foreground (text) color
188 case HighlightedText: // highlighted foreground (text) color
191 case Base: // editor background color
192 case Background: // background color
193 case Highlight: // highlight background color
199 if ( !aRes.isValid() )
201 aRes = HYDROGUI_DataObject::color( theColorRole, theColumnId );
206 QStringList HYDROGUI_Zone::getObjects() const
209 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
210 if ( !aZone.IsNull() )
212 HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
213 // Collect all used altitudes/land cover names when merging is necessary
214 // or just get the name of altitude/land cover of a single object
215 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
216 for ( ; anIter.More(); anIter.Next() )
218 Handle(HYDROData_Object) aRefGeomObj =
219 Handle(HYDROData_Object)::DownCast( anIter.Value() );
220 if ( !aRefGeomObj.IsNull() )
222 Handle(HYDROData_IAltitudeObject) anAltitudeObj = aRefGeomObj->GetAltitudeObject();
223 if ( !anAltitudeObj.IsNull() && !aRes.contains( anAltitudeObj->GetName() ) )
224 aRes.append( anAltitudeObj->GetName() );
226 aRes.append( anIter.Value()->GetName() );
233 HYDROData_Zone::MergeType HYDROGUI_Zone::getMergeType() const
235 HYDROData_Zone::MergeType aRes = HYDROData_Zone::Merge_UNKNOWN;
236 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
237 if ( !aZone.IsNull() )
239 aRes = aZone->GetMergeType();
244 void HYDROGUI_Zone::setMergeType( int theMergeType, QString theMergeObjectName )
246 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
247 if ( !aZone.IsNull() )
249 HYDROData_Zone::MergeType aMergeType =
250 ( HYDROData_Zone::MergeType )theMergeType;
251 aZone->SetMergeType( aMergeType );
252 if ( aMergeType == HYDROData_Zone::Merge_Object )
254 // Find an altitude/land cover object by the given name and set it as the zone's merge altitude/land cover
255 HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
256 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
257 for ( ; anIter.More(); anIter.Next() )
259 Handle(HYDROData_Entity) aMergeObject;
261 Handle(HYDROData_Object) aRefGeomObj =
262 Handle(HYDROData_Object)::DownCast( anIter.Value() );
263 if ( !aRefGeomObj.IsNull() )
265 // Get altitude object
266 aMergeObject = aRefGeomObj->GetAltitudeObject();
269 if ( !aMergeObject.IsNull() && theMergeObjectName == aMergeObject->GetName() )
271 aZone->SetMergeObject( aMergeObject );
280 \brief Check if this object is can't be renamed in place
283 \return \c true if the item can be renamed by the user in place (e.g. in the Object browser)
285 bool HYDROGUI_Zone::renameAllowed( const int id ) const
287 if ( id == NameId && isInOperation() )
291 return HYDROGUI_DataObject::renameAllowed( id );
295 // \brief Set name of this object.
297 // \return \c true if rename operation finished successfully, \c false otherwise.
299 //bool HYDROGUI_Zone::setName(const QString& theName)
301 // if ( isInOperation() )
303 // bool aRes = false;
304 // if ( !theName.isEmpty() )
306 // Handle(HYDROData_Entity) anEntity = modelObject();
307 // CAM_Module* aModule = module();
308 // if( anEntity->GetName() != theName && aModule )
310 // // check that there are no other objects with the same name in the document
311 // Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( aModule, theName );
312 // if ( anObject.IsNull() )
314 // anEntity->SetName( theName );
319 // // Inform the user that the name is already used
320 // QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
321 // QString aMessage = QObject::tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( theName );
322 // SUIT_MessageBox::critical( getApp()->desktop(), aTitle, aMessage );
329 // aRes = HYDROGUI_DataObject::setName( theName );