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>
29 #include "HYDRO_trace.hxx"
31 HYDROGUI_Zone::HYDROGUI_Zone( SUIT_DataObject* theParent,
32 Handle(HYDROData_Zone) theData,
33 const QString& theParentEntry,
34 const bool theIsInOperation )
35 : HYDROGUI_DataObject( theParent, theData, theParentEntry, theIsInOperation ), CAM_DataObject( theParent )
39 QString HYDROGUI_Zone::text( const int theColumnId ) const
42 if( !modelObject().IsNull() )
44 switch ( theColumnId )
47 // Get Ref.Object name
48 aRes = getRefObjectNames();
51 // Get altitude/land cover object name
52 aRes = getObjectName();
55 aRes = LightApp_DataObject::text( theColumnId );
61 QString HYDROGUI_Zone::getRefObjectNames() const
64 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
65 if ( !aZone.IsNull() )
67 HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
68 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
69 for ( ; anIter.More(); anIter.Next() )
71 Handle(HYDROData_Entity) aRefGeomObj = anIter.Value();
72 if ( !aRefGeomObj.IsNull() )
74 // Get Ref.Object name
75 aRes += aRefGeomObj->GetName() + ", ";
79 if ( aRes.length() > 1 )
81 aRes.remove( aRes.length() - 2, 2 );
86 QString HYDROGUI_Zone::getObjectName() const
89 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
90 if ( !aZone.IsNull() )
92 HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
93 bool isMergingNeed = aZone->IsMergingNeed();
94 if ( ( isMergingNeed && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
95 || ( aSeq.Length() == 1 ) || ( !isMergingNeed ) )
97 // Collect all used altitudes names when merging is necessary
98 // or just get the name of altitude of a single geometry object
99 // or just get the name of a single altitude
100 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
101 QSet<QString> aNamesSet;
103 for ( ; anIter.More(); anIter.Next() )
106 Handle(HYDROData_Object) aRefGeomObj =
107 Handle(HYDROData_Object)::DownCast( anIter.Value() );
108 if ( !aRefGeomObj.IsNull() )
110 // Get altitude object name
111 Handle(HYDROData_IAltitudeObject) anAltitudeObj = aRefGeomObj->GetAltitudeObject();
112 if ( !anAltitudeObj.IsNull() )
114 aName = anAltitudeObj->GetName();
115 if ( !isMergingNeed )
117 // Get the first geometry object's altitude name and go out
124 if ( !aName.isEmpty() && !aNamesSet.contains( aName ) )
126 aRes += aName + ", ";
127 aNamesSet.insert( aName );
130 // Remove the last comma if necessary
131 if ( isMergingNeed && ( aRes.length() > 1 ) )
133 aRes.remove( aRes.length() - 2, 2 );
138 switch( aZone->GetMergeType() )
140 case HYDROData_Zone::Merge_ZMIN: // The minimum values
141 aRes = QObject::tr( "MERGE_ZMIN" );
143 case HYDROData_Zone::Merge_ZMAX: // The maximum values
144 aRes = QObject::tr( "MERGE_ZMAX" );
146 case HYDROData_Zone::Merge_Object: // Only one altitude/land cover will be taken into account
148 Handle(HYDROData_Entity) aMergeObj = aZone->GetMergeObject();
149 if ( !aMergeObj.IsNull() )
150 aRes = aMergeObj->GetName();
154 aRes = QObject::tr( "MERGE_UNKNOWN" );
161 bool HYDROGUI_Zone::isMergingNeed() const
164 if( !modelObject().IsNull() )
166 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
167 if ( !aZone.IsNull() )
169 aRes = aZone->IsMergingNeed();
175 QColor HYDROGUI_Zone::color( const ColorRole theColorRole, const int theColumnId ) const
177 // Implement red color for altitude conflicts in case creation dialog
179 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
180 if ( !aZone.IsNull() )
182 if ( ( aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN ) )
184 switch( theColorRole )
186 case Text: // editor foreground (text) color
187 case Foreground: // foreground (text) color
190 case HighlightedText: // highlighted foreground (text) color
193 case Base: // editor background color
194 case Background: // background color
195 case Highlight: // highlight background color
201 if ( !aRes.isValid() )
203 aRes = HYDROGUI_DataObject::color( theColorRole, theColumnId );
208 QStringList HYDROGUI_Zone::getObjects() const
211 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
212 if ( !aZone.IsNull() )
214 HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
215 // Collect all used altitudes/land cover names when merging is necessary
216 // or just get the name of altitude/land cover of a single object
217 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
218 for ( ; anIter.More(); anIter.Next() )
220 Handle(HYDROData_Object) aRefGeomObj =
221 Handle(HYDROData_Object)::DownCast( anIter.Value() );
222 if ( !aRefGeomObj.IsNull() )
224 Handle(HYDROData_IAltitudeObject) anAltitudeObj = aRefGeomObj->GetAltitudeObject();
225 if ( !anAltitudeObj.IsNull() && !aRes.contains( anAltitudeObj->GetName() ) )
226 aRes.append( anAltitudeObj->GetName() );
228 aRes.append( anIter.Value()->GetName() );
235 HYDROData_Zone::MergeType HYDROGUI_Zone::getMergeType() const
237 HYDROData_Zone::MergeType aRes = HYDROData_Zone::Merge_UNKNOWN;
238 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
239 if ( !aZone.IsNull() )
241 aRes = aZone->GetMergeType();
246 void HYDROGUI_Zone::setMergeType( int theMergeType, QString theMergeObjectName )
248 DEBTRACE("HYDROGUI_Zone::setMergeType " << theMergeType << " " << theMergeObjectName.toStdString());
249 Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( modelObject() );
250 if ( !aZone.IsNull() )
252 HYDROData_Zone::MergeType aMergeType =
253 ( HYDROData_Zone::MergeType )theMergeType;
254 aZone->SetMergeType( aMergeType );
255 if ( aMergeType == HYDROData_Zone::Merge_Object )
257 // Find an altitude/land cover object by the given name and set it as the zone's merge altitude/land cover
258 HYDROData_SequenceOfObjects aSeq = aZone->GetObjects();
259 HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
260 for ( ; anIter.More(); anIter.Next() )
262 Handle(HYDROData_Entity) aMergeObject;
264 Handle(HYDROData_Object) aRefGeomObj =
265 Handle(HYDROData_Object)::DownCast( anIter.Value() );
266 if ( !aRefGeomObj.IsNull() )
268 // Get altitude object
269 DEBTRACE("aRefGeomObj " << aRefGeomObj->GetName().toStdString());
270 aMergeObject = aRefGeomObj->GetAltitudeObject();
273 if ( !aMergeObject.IsNull() && theMergeObjectName == aMergeObject->GetName() )
275 aZone->SetMergeObject( aMergeObject );
284 \brief Check if this object is can't be renamed in place
287 \return \c true if the item can be renamed by the user in place (e.g. in the Object browser)
289 bool HYDROGUI_Zone::renameAllowed( const int id ) const
291 if ( id == NameId && isInOperation() )
295 return HYDROGUI_DataObject::renameAllowed( id );
299 // \brief Set name of this object.
301 // \return \c true if rename operation finished successfully, \c false otherwise.
303 //bool HYDROGUI_Zone::setName(const QString& theName)
305 // if ( isInOperation() )
307 // bool aRes = false;
308 // if ( !theName.isEmpty() )
310 // Handle(HYDROData_Entity) anEntity = modelObject();
311 // CAM_Module* aModule = module();
312 // if( anEntity->GetName() != theName && aModule )
314 // // check that there are no other objects with the same name in the document
315 // Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( aModule, theName );
316 // if ( anObject.IsNull() )
318 // anEntity->SetName( theName );
323 // // Inform the user that the name is already used
324 // QString aTitle = QObject::tr( "INSUFFICIENT_INPUT_DATA" );
325 // QString aMessage = QObject::tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( theName );
326 // SUIT_MessageBox::critical( getApp()->desktop(), aTitle, aMessage );
333 // aRes = HYDROGUI_DataObject::setName( theName );