1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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 definition.
234 @param theObjects the ordered 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 QStringList aNames = getAvailableObjectNames();
244 QMutableListIterator<HYDROData_CustomRule> anIter( myRules );
245 while ( anIter.hasNext() ) {
246 HYDROData_CustomRule aRule = anIter.next();
247 if ( !aNames.contains( aRule.Object1->GetName() ) ||
248 !aNames.contains( aRule.Object2->GetName() ) ) {
258 @return true if a rule has been created successfully
260 bool HYDROGUI_PriorityTableModel::createNewRule()
262 if ( !canCreateNewRule() ) {
266 // Existing pairs of object indexes
267 QStringList aNames = getAvailableObjectNames();
268 QList< QPair<int, int> > aRules;
269 foreach( const HYDROData_CustomRule& aRule, getRules() ) {
270 int anIndex1 = aNames.indexOf( aRule.Object1->GetName() );
271 int anIndex2 = aNames.indexOf( aRule.Object2->GetName() );
272 if ( anIndex1 >= 0 && anIndex2 >= 0 ) {
273 aRules << QPair<int, int>( anIndex1, anIndex2 );
274 aRules << QPair<int, int>( anIndex2, anIndex1 );
279 // 1. the new pair of objects
280 // 2. the priority type corresponding to the objects order
281 Handle(HYDROData_Object) anObject1, anObject2;
282 HYDROData_PriorityType aPriorityType = GREATER;
284 int aNbObjects = myObjects.count();
285 for ( int anIndex1 = 0; anIndex1 < aNbObjects; anIndex1++ ) {
286 bool isFound = false;
288 for ( int anIndex2 = 0; anIndex2 < aNbObjects; anIndex2++ ) {
289 if ( anIndex1 == anIndex2 ) {
293 if ( !aRules.contains( QPair<int, int>( anIndex1, anIndex2 ) ) ) {
294 anObject1 = myObjects.at( anIndex1 );
295 anObject2 = myObjects.at( anIndex2 );
296 if ( anIndex1 > anIndex2 ) {
297 aPriorityType = LESS;
310 bool isCreated = false;
312 if ( !anObject1.IsNull() && !anObject2.IsNull() ) {
313 HYDROData_CustomRule aNewRule;
314 aNewRule.Object1 = anObject1;
315 aNewRule.Object2 = anObject2;
316 aNewRule.Priority = aPriorityType;
317 aNewRule.MergeType = HYDROData_Zone::Merge_ZMIN;
330 Check if a new rule can be created.
331 @return true if a new rule could be created
333 bool HYDROGUI_PriorityTableModel::canCreateNewRule() const
335 int aNbObjects = myObjects.count();
337 return ( myRules.count() < (aNbObjects - 1)*aNbObjects/2 );
342 bool HYDROGUI_PriorityTableModel::removeRows ( int theRow, int theCount, const QModelIndex & theParent )
344 if ( myRules.isEmpty() || theRow < 0 || theRow >= myRules.count() ) {
348 int aLastRow = theRow + theCount - 1;
349 if ( aLastRow > myRules.count() ) {
350 aLastRow = myRules.count() - 1;
353 beginRemoveRows( theParent, theRow, aLastRow );
355 // Remove the corresponding rules
356 for ( int i = 1; i <= theCount; i++ ) {
357 myRules.removeAt( theRow );
366 Remove all data from the model.
367 @return true if at least one row is removed
369 bool HYDROGUI_PriorityTableModel::removeAll()
371 return removeRows( 0, rowCount() );
375 Get available pairs for the given object.
376 @param theObject the object
377 @return the list of object names
379 QStringList HYDROGUI_PriorityTableModel::getAvailablePairs( const Handle(HYDROData_Object)& theObject ) const
383 if ( !theObject.IsNull() ) {
384 aNames = getAvailableObjectNames();
385 aNames.removeAll( theObject->GetName() );
393 @params theRows the list of rows to remove
394 @return true if at least one row is removed
396 bool HYDROGUI_PriorityTableModel::removeRows ( const QList<int> theRows )
398 QList<int> aSortedRows = theRows;
399 qSort( aSortedRows );
401 int aRowToRemove = -1;
403 foreach ( int aRow, aSortedRows ) {
404 aRowToRemove = aRow - aNbRemoved;
405 if ( removeRow( aRowToRemove ) ) {
410 return ( aNbRemoved > 0 );
414 Get priority string representation.
415 @param thePriority the priority
417 QString HYDROGUI_PriorityTableModel::priorityToString( const int thePriority ) const
419 switch( thePriority )
424 return tr( "GREATER" );
431 Get merge type string representation.
432 @param theMergeType the merge type
434 QString HYDROGUI_PriorityTableModel::mergeTypeToString( const int theMergeType ) const
436 switch( theMergeType )
438 case HYDROData_Zone::Merge_Object:
439 return tr( "PRIORITY" );
440 case HYDROData_Zone::Merge_ZMIN:
442 case HYDROData_Zone::Merge_ZMAX:
450 Check if the given pair of objects is already used.
451 @return true if the pair is used
453 bool HYDROGUI_PriorityTableModel::isUsed( const Handle(HYDROData_Object)& theObj1,
454 const Handle(HYDROData_Object)& theObj2 ) const
458 foreach ( const HYDROData_CustomRule& aRule, myRules ) {
459 if ( ( aRule.Object1->GetName() == theObj1->GetName() &&
460 aRule.Object2->GetName() == theObj2->GetName() ) ||
461 ( aRule.Object1->GetName() == theObj2->GetName() &&
462 aRule.Object2->GetName() == theObj1->GetName() ) ) {
472 Get available object names.
473 @return the list of object names
475 QStringList HYDROGUI_PriorityTableModel::getAvailableObjectNames() const
479 foreach ( const Handle(HYDROData_Object) anObj, myObjects ) {
480 if ( !anObj.IsNull() ) {
481 aNames << anObj->GetName();