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"
24 @param theParent the parent object
26 HYDROGUI_PriorityTableModel::HYDROGUI_PriorityTableModel( QObject* theParent )
27 : QAbstractTableModel( theParent )
34 HYDROGUI_PriorityTableModel::~HYDROGUI_PriorityTableModel()
40 Qt::ItemFlags HYDROGUI_PriorityTableModel::flags( const QModelIndex & theIndex ) const
42 return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
47 QVariant HYDROGUI_PriorityTableModel::data( const QModelIndex &theIndex, int theRole ) const
51 int aRow = theIndex.row();
52 int aColumn = theIndex.column();
54 if ( !theIndex.isValid() || aRow > myRules.size() ) {
58 if ( theRole == Qt::DisplayRole ) {
59 HYDROData_CustomRule aRule = myRules.at(aRow);
62 aVariant = aRule.Object1->GetName();
63 } else if ( aColumn == 1 ) {
64 aVariant = priorityToString( aRule.Priority );
65 } else if ( aColumn == 2 ) {
66 aVariant = aRule.Object2->GetName();
67 } else if ( aColumn == 3 ) {
68 aVariant = mergeTypeToString( aRule.MergeType );
70 } else if ( theRole == Qt::EditRole ) {
71 HYDROData_CustomRule aRule = myRules.at(aRow);
74 aVariant = aRule.Object1->GetName();
75 } else if ( aColumn == 1 ) {
76 aVariant = aRule.Priority;
77 } else if ( aColumn == 2 ) {
78 aVariant = aRule.Object2->GetName();
79 } else if ( aColumn == 3 ) {
80 aVariant = aRule.MergeType;
82 } else if ( theRole == Qt::UserRole ) {
83 if ( aColumn == 0 || aColumn == 2 ) {
85 HYDROData_CustomRule aRule = myRules.at( aRow );
86 Handle(HYDROData_Object) aUsedObject = aColumn == 0 ? aRule.Object2 : aRule.Object1;
87 if ( !aUsedObject.IsNull() ) {
88 aNames = getAvailablePairs( aUsedObject );
91 } else if ( aColumn == 1 ) {
92 QMap<QString, QVariant> aMap;
93 aMap.insert( priorityToString( LESS ), LESS );
94 aMap.insert( priorityToString( GREATER ), GREATER );
95 aVariant = QVariant( aMap );
96 } else if ( aColumn == 3 ) {
97 QMap<QString, QVariant> aMap;
98 aMap.insert( mergeTypeToString( HYDROData_Zone::Merge_Object ), HYDROData_Zone::Merge_Object );
99 aMap.insert( mergeTypeToString( HYDROData_Zone::Merge_ZMIN ), HYDROData_Zone::Merge_ZMIN );
100 aMap.insert( mergeTypeToString( HYDROData_Zone::Merge_ZMAX ), HYDROData_Zone::Merge_ZMAX );
101 aVariant = QVariant( aMap );
103 } else if ( theRole == Qt::TextAlignmentRole ) {
104 aVariant = Qt::AlignCenter;
112 bool HYDROGUI_PriorityTableModel::setData( const QModelIndex & theIndex, const QVariant & theValue, int theRole )
114 int aRow = theIndex.row();
115 if ( !theIndex.isValid() || aRow > myRules.size() ) {
121 if ( theRole == Qt::EditRole ) {
122 int aColumn = theIndex.column();
124 if ( aColumn == 0 || aColumn == 2 ) {
125 Handle(HYDROData_Object) anObject;
126 QString anObjName = theValue.toString();
127 foreach ( const Handle(HYDROData_Object) anObj, myObjects ) {
128 if ( anObj->GetName() == anObjName ) {
133 if ( !anObject.IsNull() ) {
134 HYDROData_CustomRule anEditedRule = myRules[aRow];
136 if ( aColumn == 0 ) {
137 anEditedRule.Object1 = anObject;
139 anEditedRule.Object2 = anObject;
142 if ( !isUsed( anEditedRule.Object1, anEditedRule.Object2 ) ) {
143 myRules[aRow] = anEditedRule;
146 emit showError( tr("ALREADY_EXISTS") );
149 } else if ( aColumn == 1 ) {
150 myRules[aRow].Priority = (HYDROData_PriorityType)theValue.toInt();
151 } else if ( aColumn == 3 ) {
152 myRules[aRow].MergeType = (HYDROData_Zone::MergeAltitudesType)theValue.toInt();;
161 int HYDROGUI_PriorityTableModel::rowCount( const QModelIndex &theParent ) const
163 return myRules.count();
168 int HYDROGUI_PriorityTableModel::columnCount( const QModelIndex &theParent ) const
175 @param theRules the list of rules
177 void HYDROGUI_PriorityTableModel::setRules( const HYDROData_ListOfRules& theRules )
186 @return the list of rules
188 HYDROData_ListOfRules HYDROGUI_PriorityTableModel::getRules() const
195 QVariant HYDROGUI_PriorityTableModel::headerData( int theSection,
196 Qt::Orientation theOrientation,
201 if ( theRole != Qt::DisplayRole ) {
205 if ( theOrientation == Qt::Horizontal ) {
209 aData = tr( "OBJECT1" );
212 aData = tr( "PRIORITY" );
215 aData = tr( "OBJECT2" );
218 aData = tr( "BATHYMETRY" );
221 } else if ( theOrientation == Qt::Vertical ) {
222 aData = theSection + 1;
229 Set objects which could be used for rules definition.
230 @param theObjects the ordered list of objects
232 void HYDROGUI_PriorityTableModel::setObjects( const QList<Handle(HYDROData_Object)>& theObjects )
234 myObjects = theObjects;
238 // Remove rules which use objects which are no longer available
239 QStringList aNames = getAvailableObjectNames();
240 QMutableListIterator<HYDROData_CustomRule> anIter( myRules );
241 while ( anIter.hasNext() ) {
242 HYDROData_CustomRule aRule = anIter.next();
243 if ( !aNames.contains( aRule.Object1->GetName() ) ||
244 !aNames.contains( aRule.Object2->GetName() ) ) {
254 @return true if a rule has been created successfully
256 bool HYDROGUI_PriorityTableModel::createNewRule()
258 if ( !canCreateNewRule() ) {
262 // Existing pairs of object indexes
263 QStringList aNames = getAvailableObjectNames();
264 QList< QPair<int, int> > aRules;
265 foreach( const HYDROData_CustomRule& aRule, getRules() ) {
266 int anIndex1 = aNames.indexOf( aRule.Object1->GetName() );
267 int anIndex2 = aNames.indexOf( aRule.Object2->GetName() );
268 if ( anIndex1 >= 0 && anIndex2 >= 0 ) {
269 aRules << QPair<int, int>( anIndex1, anIndex2 );
270 aRules << QPair<int, int>( anIndex2, anIndex1 );
275 // 1. the new pair of objects
276 // 2. the priority type corresponding to the objects order
277 Handle(HYDROData_Object) anObject1, anObject2;
278 HYDROData_PriorityType aPriorityType = GREATER;
280 int aNbObjects = myObjects.count();
281 for ( int anIndex1 = 0; anIndex1 < aNbObjects; anIndex1++ ) {
282 bool isFound = false;
284 for ( int anIndex2 = 0; anIndex2 < aNbObjects; anIndex2++ ) {
285 if ( anIndex1 == anIndex2 ) {
289 if ( !aRules.contains( QPair<int, int>( anIndex1, anIndex2 ) ) ) {
290 anObject1 = myObjects.at( anIndex1 );
291 anObject2 = myObjects.at( anIndex2 );
292 if ( anIndex1 > anIndex2 ) {
293 aPriorityType = LESS;
306 bool isCreated = false;
308 if ( !anObject1.IsNull() && !anObject2.IsNull() ) {
309 HYDROData_CustomRule aNewRule;
310 aNewRule.Object1 = anObject1;
311 aNewRule.Object2 = anObject2;
312 aNewRule.Priority = aPriorityType;
313 aNewRule.MergeType = HYDROData_Zone::Merge_ZMIN;
326 Check if a new rule can be created.
327 @return true if a new rule could be created
329 bool HYDROGUI_PriorityTableModel::canCreateNewRule() const
331 int aNbObjects = myObjects.count();
333 return ( myRules.count() < (aNbObjects - 1)*aNbObjects/2 );
338 bool HYDROGUI_PriorityTableModel::removeRows ( int theRow, int theCount, const QModelIndex & theParent )
340 if ( myRules.isEmpty() || theRow < 0 || theRow >= myRules.count() ) {
344 int aLastRow = theRow + theCount - 1;
345 if ( aLastRow > myRules.count() ) {
346 aLastRow = myRules.count() - 1;
349 beginRemoveRows( theParent, theRow, aLastRow );
351 // Remove the corresponding rules
352 for ( int i = 1; i <= theCount; i++ ) {
353 myRules.removeAt( theRow );
362 Remove all data from the model.
363 @return true if at least one row is removed
365 bool HYDROGUI_PriorityTableModel::removeAll()
367 return removeRows( 0, rowCount() );
371 Get available pairs for the given object.
372 @param theObject the object
373 @return the list of object names
375 QStringList HYDROGUI_PriorityTableModel::getAvailablePairs( const Handle(HYDROData_Object)& theObject ) const
379 if ( !theObject.IsNull() ) {
380 aNames = getAvailableObjectNames();
381 aNames.removeAll( theObject->GetName() );
389 @params theRows the list of rows to remove
390 @return true if at least one row is removed
392 bool HYDROGUI_PriorityTableModel::removeRows ( const QList<int> theRows )
394 QList<int> aSortedRows = theRows;
395 qSort( aSortedRows );
397 int aRowToRemove = -1;
399 foreach ( int aRow, aSortedRows ) {
400 aRowToRemove = aRow - aNbRemoved;
401 if ( removeRow( aRowToRemove ) ) {
406 return ( aNbRemoved > 0 );
410 Get priority string representation.
411 @param thePriority the priority
413 QString HYDROGUI_PriorityTableModel::priorityToString( const int thePriority ) const
415 switch( thePriority )
420 return tr( "GREATER" );
427 Get merge type string representation.
428 @param theMergeType the merge type
430 QString HYDROGUI_PriorityTableModel::mergeTypeToString( const int theMergeType ) const
432 switch( theMergeType )
434 case HYDROData_Zone::Merge_Object:
435 return tr( "PRIORITY" );
436 case HYDROData_Zone::Merge_ZMIN:
438 case HYDROData_Zone::Merge_ZMAX:
446 Check if the given pair of objects is already used.
447 @return true if the pair is used
449 bool HYDROGUI_PriorityTableModel::isUsed( const Handle(HYDROData_Object)& theObj1,
450 const Handle(HYDROData_Object)& theObj2 ) const
454 foreach ( const HYDROData_CustomRule& aRule, myRules ) {
455 if ( ( aRule.Object1->GetName() == theObj1->GetName() &&
456 aRule.Object2->GetName() == theObj2->GetName() ) ||
457 ( aRule.Object1->GetName() == theObj2->GetName() &&
458 aRule.Object2->GetName() == theObj1->GetName() ) ) {
468 Get available object names.
469 @return the list of object names
471 QStringList HYDROGUI_PriorityTableModel::getAvailableObjectNames() const
475 foreach ( const Handle(HYDROData_Object) anObj, myObjects ) {
476 if ( !anObj.IsNull() ) {
477 aNames << anObj->GetName();