1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROGUI_PriorityTableModel.h"
28 @param theParent the parent object
30 HYDROGUI_PriorityTableModel::HYDROGUI_PriorityTableModel( QObject* theParent )
31 : QAbstractTableModel( theParent )
38 HYDROGUI_PriorityTableModel::~HYDROGUI_PriorityTableModel()
44 Qt::ItemFlags HYDROGUI_PriorityTableModel::flags( const QModelIndex & theIndex ) const
46 return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
51 QVariant HYDROGUI_PriorityTableModel::data( const QModelIndex &theIndex, int theRole ) const
55 int aRow = theIndex.row();
56 int aColumn = theIndex.column();
58 if ( !theIndex.isValid() || aRow > myRules.size() ) {
62 if ( theRole == Qt::DisplayRole ) {
63 HYDROData_CustomRule aRule = myRules.at(aRow);
66 aVariant = aRule.Object1->GetName();
67 } else if ( aColumn == 1 ) {
68 aVariant = priorityToString( aRule.Priority );
69 } else if ( aColumn == 2 ) {
70 aVariant = aRule.Object2->GetName();
71 } else if ( aColumn == 3 ) {
72 aVariant = mergeTypeToString( aRule.MergeType );
74 } else if ( theRole == Qt::EditRole ) {
75 HYDROData_CustomRule aRule = myRules.at(aRow);
78 aVariant = aRule.Object1->GetName();
79 } else if ( aColumn == 1 ) {
80 aVariant = aRule.Priority;
81 } else if ( aColumn == 2 ) {
82 aVariant = aRule.Object2->GetName();
83 } else if ( aColumn == 3 ) {
84 aVariant = aRule.MergeType;
86 } else if ( theRole == Qt::UserRole ) {
87 if ( aColumn == 0 || aColumn == 2 ) {
89 HYDROData_CustomRule aRule = myRules.at( aRow );
90 Handle(HYDROData_Object) aUsedObject = aColumn == 0 ? aRule.Object2 : aRule.Object1;
91 if ( !aUsedObject.IsNull() ) {
92 aNames = getAvailablePairs( aUsedObject );
95 } else if ( aColumn == 1 ) {
96 QMap<QString, QVariant> aMap;
97 aMap.insert( priorityToString( LESS ), LESS );
98 aMap.insert( priorityToString( GREATER ), GREATER );
99 aVariant = QVariant( aMap );
100 } else if ( aColumn == 3 ) {
101 QMap<QString, QVariant> aMap;
102 aMap.insert( mergeTypeToString( HYDROData_Zone::Merge_Object ), HYDROData_Zone::Merge_Object );
103 aMap.insert( mergeTypeToString( HYDROData_Zone::Merge_ZMIN ), HYDROData_Zone::Merge_ZMIN );
104 aMap.insert( mergeTypeToString( HYDROData_Zone::Merge_ZMAX ), HYDROData_Zone::Merge_ZMAX );
105 aVariant = QVariant( aMap );
107 } else if ( theRole == Qt::TextAlignmentRole ) {
108 aVariant = Qt::AlignCenter;
116 bool HYDROGUI_PriorityTableModel::setData( const QModelIndex & theIndex, const QVariant & theValue, int theRole )
118 int aRow = theIndex.row();
119 if ( !theIndex.isValid() || aRow > myRules.size() ) {
125 if ( theRole == Qt::EditRole ) {
126 int aColumn = theIndex.column();
128 if ( aColumn == 0 || aColumn == 2 ) {
129 Handle(HYDROData_Object) anObject;
130 QString anObjName = theValue.toString();
131 foreach ( const Handle(HYDROData_Object) anObj, myObjects ) {
132 if ( anObj->GetName() == anObjName ) {
137 if ( !anObject.IsNull() ) {
138 HYDROData_CustomRule anEditedRule = myRules[aRow];
140 if ( aColumn == 0 ) {
141 anEditedRule.Object1 = anObject;
143 anEditedRule.Object2 = anObject;
146 if ( !isUsed( anEditedRule.Object1, anEditedRule.Object2 ) ) {
147 myRules[aRow] = anEditedRule;
150 emit showError( tr("ALREADY_EXISTS") );
153 } else if ( aColumn == 1 ) {
154 myRules[aRow].Priority = (HYDROData_PriorityType)theValue.toInt();
155 } else if ( aColumn == 3 ) {
156 myRules[aRow].MergeType = (HYDROData_Zone::MergeAltitudesType)theValue.toInt();;
165 int HYDROGUI_PriorityTableModel::rowCount( const QModelIndex &theParent ) const
167 return myRules.count();
172 int HYDROGUI_PriorityTableModel::columnCount( const QModelIndex &theParent ) const
179 @param theRules the list of rules
181 void HYDROGUI_PriorityTableModel::setRules( const HYDROData_ListOfRules& theRules )
190 @return the list of rules
192 HYDROData_ListOfRules HYDROGUI_PriorityTableModel::getRules() const
199 QVariant HYDROGUI_PriorityTableModel::headerData( int theSection,
200 Qt::Orientation theOrientation,
205 if ( theRole != Qt::DisplayRole ) {
209 if ( theOrientation == Qt::Horizontal ) {
213 aData = tr( "OBJECT1" );
216 aData = tr( "PRIORITY" );
219 aData = tr( "OBJECT2" );
222 aData = tr( "BATHYMETRY" );
225 } else if ( theOrientation == Qt::Vertical ) {
226 aData = theSection + 1;
233 Set objects which could be used for rules.
234 @param theObjects the list of objects
236 void HYDROGUI_PriorityTableModel::setObjects( const QList<Handle(HYDROData_Object)>& theObjects )
238 myObjects = theObjects;
242 // Remove rules which use objects which are no longer available
243 QMutableListIterator<HYDROData_CustomRule> anIter( myRules );
244 while ( anIter.hasNext() ) {
245 HYDROData_CustomRule aRule = anIter.next();
246 if ( !myObjects.contains( aRule.Object1 ) ||
247 !myObjects.contains( aRule.Object2 ) ) {
257 @return true if a rule has been created successfully
259 bool HYDROGUI_PriorityTableModel::createNewRule()
261 if ( !canCreateNewRule() ) {
265 // Existing pairs of object indexes
266 QList< QPair<int, int> > aRules;
267 foreach( const HYDROData_CustomRule& aRule, getRules() ) {
268 int anIndex1 = myObjects.indexOf( aRule.Object1 );
269 int anIndex2 = myObjects.indexOf( aRule.Object2 );
270 if ( anIndex1 >= 0 && anIndex2 >= 0 ) {
271 aRules << QPair<int, int>( anIndex1, anIndex2 );
272 aRules << QPair<int, int>( anIndex2, anIndex1 );
276 // Try to find new pair of objects
277 Handle(HYDROData_Object) anObject1, anObject2;
279 int aNbObjects = myObjects.count();
280 for ( int anIndex1 = 0; anIndex1 < aNbObjects; anIndex1++ ) {
281 bool isFound = false;
283 for ( int anIndex2 = 0; anIndex2 < aNbObjects; anIndex2++ ) {
284 if ( anIndex1 == anIndex2 ) {
288 if ( !aRules.contains( QPair<int, int>( anIndex1, anIndex2 ) ) ) {
289 anObject1 = myObjects.at( anIndex1 );
290 anObject2 = myObjects.at( anIndex2 );
302 bool isCreated = false;
304 if ( !anObject1.IsNull() && !anObject2.IsNull() ) {
305 HYDROData_CustomRule aNewRule;
306 aNewRule.Object1 = anObject1;
307 aNewRule.Object2 = anObject2;
308 aNewRule.Priority = LESS;
309 aNewRule.MergeType = HYDROData_Zone::Merge_ZMIN;
322 Check if a new rule can be created.
323 @return true if a new rule could be created
325 bool HYDROGUI_PriorityTableModel::canCreateNewRule() const
327 int aNbObjects = myObjects.count();
329 return ( myRules.count() < (aNbObjects - 1)*aNbObjects/2 );
334 bool HYDROGUI_PriorityTableModel::removeRows ( int theRow, int theCount, const QModelIndex & theParent )
336 if ( myRules.isEmpty() || theRow < 0 || theRow >= myRules.count() ) {
340 int aLastRow = theRow + theCount - 1;
341 if ( aLastRow > myRules.count() ) {
342 aLastRow = myRules.count() - 1;
345 beginRemoveRows( theParent, theRow, aLastRow );
347 // Remove the corresponding rules
348 for ( int i = 1; i <= theCount; i++ ) {
349 myRules.removeAt( theRow );
358 Remove all data from the model.
359 @return true if at least one row is removed
361 bool HYDROGUI_PriorityTableModel::removeAll()
363 return removeRows( 0, rowCount() );
367 Get available pairs for the given object.
368 @param theObject the object
369 @return the list of object names
371 QStringList HYDROGUI_PriorityTableModel::getAvailablePairs( const Handle(HYDROData_Object)& theObject ) const
375 foreach ( const Handle(HYDROData_Object) anObj, myObjects ) {
376 if ( !anObj.IsNull() && anObj->GetName() != theObject->GetName() ) {
377 aNames << anObj->GetName();
386 @params theRows the list of rows to remove
387 @return true if at least one row is removed
389 bool HYDROGUI_PriorityTableModel::removeRows ( const QList<int> theRows )
391 QList<int> aSortedRows = theRows;
392 qSort( aSortedRows );
394 int aRowToRemove = -1;
396 foreach ( int aRow, aSortedRows ) {
397 aRowToRemove = aRow - aNbRemoved;
398 if ( removeRow( aRowToRemove ) ) {
403 return ( aNbRemoved > 0 );
407 Get priority string representation.
408 @param thePriority the priority
410 QString HYDROGUI_PriorityTableModel::priorityToString( const int thePriority ) const
412 switch( thePriority )
417 return tr( "GREATER" );
424 Get merge type string representation.
425 @param theMergeType the merge type
427 QString HYDROGUI_PriorityTableModel::mergeTypeToString( const int theMergeType ) const
429 switch( theMergeType )
431 case HYDROData_Zone::Merge_Object:
432 return tr( "PRIORITY" );
433 case HYDROData_Zone::Merge_ZMIN:
435 case HYDROData_Zone::Merge_ZMAX:
443 Check if the given pair of objects is already used.
444 @return true if the pair is used
446 bool HYDROGUI_PriorityTableModel::isUsed( const Handle(HYDROData_Object)& theObj1,
447 const Handle(HYDROData_Object)& theObj2 ) const
451 foreach ( const HYDROData_CustomRule& aRule, myRules ) {
452 if ( ( aRule.Object1->GetName() == theObj1->GetName() &&
453 aRule.Object2->GetName() == theObj2->GetName() ) ||
454 ( aRule.Object1->GetName() == theObj2->GetName() &&
455 aRule.Object2->GetName() == theObj1->GetName() ) ) {