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_PriorityTableModel.h"
21 #include <HYDROData_LandCover.h>
25 @param theParent the parent object
27 HYDROGUI_PriorityTableModel::HYDROGUI_PriorityTableModel( QObject* theParent )
28 : QAbstractTableModel( theParent ),
36 HYDROGUI_PriorityTableModel::~HYDROGUI_PriorityTableModel()
42 Qt::ItemFlags HYDROGUI_PriorityTableModel::flags( const QModelIndex & theIndex ) const
44 return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
49 QVariant HYDROGUI_PriorityTableModel::data( const QModelIndex &theIndex, int theRole ) const
53 int aRow = theIndex.row();
54 int aColumn = theIndex.column();
56 if ( !theIndex.isValid() || aRow > myRules.size() ) {
60 if ( theRole == Qt::DisplayRole ) {
61 HYDROData_CustomRule aRule = myRules.at(aRow);
64 aVariant = aRule.Object1->GetName();
65 } else if ( aColumn == 1 ) {
66 aVariant = priorityToString( aRule.Priority );
67 } else if ( aColumn == 2 ) {
68 aVariant = aRule.Object2->GetName();
69 } else if ( aColumn == 3 ) {
70 aVariant = mergeTypeToString( aRule.MergeType );
72 } else if ( theRole == Qt::EditRole ) {
73 HYDROData_CustomRule aRule = myRules.at(aRow);
76 aVariant = aRule.Object1->GetName();
77 } else if ( aColumn == 1 ) {
78 aVariant = aRule.Priority;
79 } else if ( aColumn == 2 ) {
80 aVariant = aRule.Object2->GetName();
81 } else if ( aColumn == 3 ) {
82 aVariant = aRule.MergeType;
84 } else if ( theRole == Qt::UserRole ) {
85 if ( aColumn == 0 || aColumn == 2 ) {
87 HYDROData_CustomRule aRule = myRules.at( aRow );
88 Handle(HYDROData_Entity) aUsedObject = aColumn == 0 ? aRule.Object2 : aRule.Object1;
89 if ( !aUsedObject.IsNull() ) {
90 aNames = getAvailablePairs( aUsedObject );
93 } else if ( aColumn == 1 ) {
94 QMap<QString, QVariant> aMap;
95 aMap.insert( priorityToString( LESS ), LESS );
96 aMap.insert( priorityToString( GREATER ), GREATER );
97 aVariant = QVariant( aMap );
98 } else if ( aColumn == 3 ) {
99 QMap<QString, QVariant> aMap;
100 aMap.insert( mergeTypeToString( HYDROData_Zone::Merge_Object ), HYDROData_Zone::Merge_Object );
101 aMap.insert( mergeTypeToString( HYDROData_Zone::Merge_ZMIN ), HYDROData_Zone::Merge_ZMIN );
102 aMap.insert( mergeTypeToString( HYDROData_Zone::Merge_ZMAX ), HYDROData_Zone::Merge_ZMAX );
103 aVariant = QVariant( aMap );
105 } else if ( theRole == Qt::TextAlignmentRole ) {
106 aVariant = Qt::AlignCenter;
114 bool HYDROGUI_PriorityTableModel::setData( const QModelIndex & theIndex, const QVariant & theValue, int theRole )
116 int aRow = theIndex.row();
117 if ( !theIndex.isValid() || aRow > myRules.size() ) {
123 if ( theRole == Qt::EditRole ) {
124 int aColumn = theIndex.column();
126 if ( aColumn == 0 || aColumn == 2 ) {
127 Handle(HYDROData_Entity) anObject;
128 QString anObjName = theValue.toString();
129 foreach ( const Handle(HYDROData_Entity) anObj, myObjects ) {
130 if ( anObj->GetName() == anObjName ) {
135 if ( !anObject.IsNull() ) {
136 HYDROData_CustomRule anEditedRule = myRules[aRow];
138 if ( aColumn == 0 ) {
139 anEditedRule.Object1 = anObject;
141 anEditedRule.Object2 = anObject;
144 if ( !isUsed( anEditedRule.Object1, anEditedRule.Object2 ) ) {
145 myRules[aRow] = anEditedRule;
148 emit showError( tr("ALREADY_EXISTS") );
151 } else if ( aColumn == 1 ) {
152 myRules[aRow].Priority = (HYDROData_PriorityType)theValue.toInt();
153 } else if ( aColumn == 3 ) {
154 myRules[aRow].MergeType = (HYDROData_Zone::MergeAltitudesType)theValue.toInt();;
163 int HYDROGUI_PriorityTableModel::rowCount( const QModelIndex &theParent ) const
165 return myRules.count();
170 int HYDROGUI_PriorityTableModel::columnCount( const QModelIndex &theParent ) const
172 return myColumnCount;
177 @param theRules the list of rules
179 void HYDROGUI_PriorityTableModel::setRules( const HYDROData_ListOfRules& theRules )
188 @return the list of rules
190 HYDROData_ListOfRules HYDROGUI_PriorityTableModel::getRules() const
197 QVariant HYDROGUI_PriorityTableModel::headerData( int theSection,
198 Qt::Orientation theOrientation,
203 if ( theRole != Qt::DisplayRole ) {
207 if ( theOrientation == Qt::Horizontal ) {
211 aData = tr( "OBJECT1" );
214 aData = tr( "PRIORITY" );
217 aData = tr( "OBJECT2" );
221 if ( getObjectsKind() != KIND_LAND_COVER )
222 aData = tr( "BATHYMETRY" );
226 } else if ( theOrientation == Qt::Vertical ) {
227 aData = theSection + 1;
234 Set objects which could be used for rules definition.
235 @param theObjects the ordered list of objects
237 void HYDROGUI_PriorityTableModel::setObjects( const QList<Handle(HYDROData_Entity)>& theObjects )
239 myObjects = theObjects;
243 // Remove rules which use objects which are no longer available
244 QStringList aNames = getAvailableObjectNames();
245 QMutableListIterator<HYDROData_CustomRule> anIter( myRules );
246 while ( anIter.hasNext() ) {
247 HYDROData_CustomRule aRule = anIter.next();
248 if ( !aNames.contains( aRule.Object1->GetName() ) ||
249 !aNames.contains( aRule.Object2->GetName() ) ) {
259 @return true if a rule has been created successfully
261 bool HYDROGUI_PriorityTableModel::createNewRule()
263 if ( !canCreateNewRule() ) {
267 // Existing pairs of object indexes
268 QStringList aNames = getAvailableObjectNames();
269 QList< QPair<int, int> > aRules;
270 foreach( const HYDROData_CustomRule& aRule, getRules() ) {
271 int anIndex1 = aNames.indexOf( aRule.Object1->GetName() );
272 int anIndex2 = aNames.indexOf( aRule.Object2->GetName() );
273 if ( anIndex1 >= 0 && anIndex2 >= 0 ) {
274 aRules << QPair<int, int>( anIndex1, anIndex2 );
275 aRules << QPair<int, int>( anIndex2, anIndex1 );
280 // 1. the new pair of objects
281 // 2. the priority type corresponding to the objects order
282 Handle(HYDROData_Entity) anObject1, anObject2;
283 HYDROData_PriorityType aPriorityType = GREATER;
285 int aNbObjects = myObjects.count();
286 for ( int anIndex1 = 0; anIndex1 < aNbObjects; anIndex1++ ) {
287 bool isFound = false;
289 for ( int anIndex2 = 0; anIndex2 < aNbObjects; anIndex2++ ) {
290 if ( anIndex1 == anIndex2 ) {
294 if ( !aRules.contains( QPair<int, int>( anIndex1, anIndex2 ) ) ) {
295 anObject1 = myObjects.at( anIndex1 );
296 anObject2 = myObjects.at( anIndex2 );
297 if ( anIndex1 > anIndex2 ) {
298 aPriorityType = LESS;
311 bool isCreated = false;
313 if ( !anObject1.IsNull() && !anObject2.IsNull() ) {
314 HYDROData_CustomRule aNewRule;
315 aNewRule.Object1 = anObject1;
316 aNewRule.Object2 = anObject2;
317 aNewRule.Priority = aPriorityType;
318 aNewRule.MergeType = HYDROData_Zone::Merge_ZMIN;
331 Check if a new rule can be created.
332 @return true if a new rule could be created
334 bool HYDROGUI_PriorityTableModel::canCreateNewRule() const
336 int aNbObjects = myObjects.count();
338 return ( myRules.count() < (aNbObjects - 1)*aNbObjects/2 );
343 bool HYDROGUI_PriorityTableModel::removeRows ( int theRow, int theCount, const QModelIndex & theParent )
345 if ( myRules.isEmpty() || theRow < 0 || theRow >= myRules.count() ) {
349 int aLastRow = theRow + theCount - 1;
350 if ( aLastRow > myRules.count() ) {
351 aLastRow = myRules.count() - 1;
354 beginRemoveRows( theParent, theRow, aLastRow );
356 // Remove the corresponding rules
357 for ( int i = 1; i <= theCount; i++ ) {
358 myRules.removeAt( theRow );
367 Remove all data from the model.
368 @return true if at least one row is removed
370 bool HYDROGUI_PriorityTableModel::removeAll()
372 return removeRows( 0, rowCount() );
376 Get available pairs for the given object.
377 @param theObject the object
378 @return the list of object names
380 QStringList HYDROGUI_PriorityTableModel::getAvailablePairs( const Handle(HYDROData_Entity)& theObject ) const
384 if ( !theObject.IsNull() ) {
385 aNames = getAvailableObjectNames();
386 aNames.removeAll( theObject->GetName() );
394 @params theRows the list of rows to remove
395 @return true if at least one row is removed
397 bool HYDROGUI_PriorityTableModel::removeRows ( const QList<int> theRows )
399 QList<int> aSortedRows = theRows;
400 qSort( aSortedRows );
402 int aRowToRemove = -1;
404 foreach ( int aRow, aSortedRows ) {
405 aRowToRemove = aRow - aNbRemoved;
406 if ( removeRow( aRowToRemove ) ) {
411 return ( aNbRemoved > 0 );
415 Get priority string representation.
416 @param thePriority the priority
418 QString HYDROGUI_PriorityTableModel::priorityToString( const int thePriority ) const
420 switch( thePriority )
425 return tr( "GREATER" );
432 Get merge type string representation.
433 @param theMergeType the merge type
435 QString HYDROGUI_PriorityTableModel::mergeTypeToString( const int theMergeType ) const
437 switch( theMergeType )
439 case HYDROData_Zone::Merge_Object:
440 return tr( "PRIORITY" );
441 case HYDROData_Zone::Merge_ZMIN:
443 case HYDROData_Zone::Merge_ZMAX:
451 Check if the given pair of objects is already used.
452 @return true if the pair is used
454 bool HYDROGUI_PriorityTableModel::isUsed( const Handle(HYDROData_Entity)& theObj1,
455 const Handle(HYDROData_Entity)& theObj2 ) const
459 foreach ( const HYDROData_CustomRule& aRule, myRules ) {
460 if ( ( aRule.Object1->GetName() == theObj1->GetName() &&
461 aRule.Object2->GetName() == theObj2->GetName() ) ||
462 ( aRule.Object1->GetName() == theObj2->GetName() &&
463 aRule.Object2->GetName() == theObj1->GetName() ) ) {
473 Get available object names.
474 @return the list of object names
476 QStringList HYDROGUI_PriorityTableModel::getAvailableObjectNames() const
480 foreach ( const Handle(HYDROData_Entity) anObj, myObjects ) {
481 if ( !anObj.IsNull() ) {
482 aNames << anObj->GetName();
490 Set number of columns in the table.
491 @param theColumnCount the number of columns
493 void HYDROGUI_PriorityTableModel::setColumnCount( int theColumnCount )
495 myColumnCount = theColumnCount;
499 Get type of objects for which rules are defined assuming,
500 that all objects in the list have the same type.
501 @return the type of objects
503 const ObjectKind HYDROGUI_PriorityTableModel::getObjectsKind() const
505 if ( myObjects.isEmpty() )
508 return myObjects.at( 0 )->GetKind();