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 ) {
88 aVariant = getAvailableObjects();
89 } else if ( aColumn == 1 ) {
90 QMap<QString, QVariant> aMap;
91 aMap.insert( priorityToString( LESS ), LESS );
92 aMap.insert( priorityToString( GREATER ), GREATER );
93 aVariant = QVariant( aMap );
94 } else if ( aColumn == 3 ) {
95 QMap<QString, QVariant> aMap;
96 aMap.insert( mergeTypeToString( HYDROData_Zone::Merge_Object ), HYDROData_Zone::Merge_Object );
97 aMap.insert( mergeTypeToString( HYDROData_Zone::Merge_ZMIN ), HYDROData_Zone::Merge_ZMIN );
98 aMap.insert( mergeTypeToString( HYDROData_Zone::Merge_ZMAX ), HYDROData_Zone::Merge_ZMAX );
99 aVariant = QVariant( aMap );
101 } else if ( theRole == Qt::TextAlignmentRole ) {
102 aVariant = Qt::AlignCenter;
110 bool HYDROGUI_PriorityTableModel::setData( const QModelIndex & theIndex, const QVariant & theValue, int theRole )
112 int aRow = theIndex.row();
113 if ( !theIndex.isValid() || aRow > myRules.size() ) {
119 if ( theRole == Qt::EditRole ) {
120 int aColumn = theIndex.column();
122 if ( aColumn == 0 || aColumn == 2 ) {
123 Handle(HYDROData_Object) anObject;
124 QString anObjName = theValue.toString();
125 foreach ( const Handle(HYDROData_Object) anObj, myObjects ) {
126 if ( anObj->GetName() == anObjName ) {
131 if ( !anObject.IsNull() ) {
132 if ( aColumn == 0 ) {
133 myRules[aRow].Object1 = anObject;
135 myRules[aRow].Object2 = anObject;
140 } else if ( aColumn == 1 ) {
141 myRules[aRow].Priority = (HYDROData_PriorityType)theValue.toInt();
142 } else if ( aColumn == 3 ) {
143 myRules[aRow].MergeType = (HYDROData_Zone::MergeAltitudesType)theValue.toInt();;
152 int HYDROGUI_PriorityTableModel::rowCount( const QModelIndex &theParent ) const
154 return myRules.count();
159 int HYDROGUI_PriorityTableModel::columnCount( const QModelIndex &theParent ) const
166 @param theRules the list of rules
168 void HYDROGUI_PriorityTableModel::setRules( const HYDROData_ListOfRules& theRules )
177 @return the list of rules
179 HYDROData_ListOfRules HYDROGUI_PriorityTableModel::getRules() const
186 QVariant HYDROGUI_PriorityTableModel::headerData( int theSection,
187 Qt::Orientation theOrientation,
192 if ( theRole != Qt::DisplayRole ) {
196 if ( theOrientation == Qt::Horizontal ) {
200 aData = tr( "OBJECT1" );
203 aData = tr( "PRIORITY" );
206 aData = tr( "OBJECT2" );
209 aData = tr( "BATHYMETRY" );
212 } else if ( theOrientation == Qt::Vertical ) {
213 aData = theSection + 1;
220 Set objects which could be used for rules.
221 @param theObjects the list of objects
223 void HYDROGUI_PriorityTableModel::setObjects( const QList<Handle(HYDROData_Object)>& theObjects )
225 myObjects = theObjects;
227 QMutableListIterator<HYDROData_CustomRule> anIter( myRules );
228 while ( anIter.hasNext() ) {
229 HYDROData_CustomRule aRule = anIter.next();
230 if ( !myObjects.contains( aRule.Object1 ) ||
231 !myObjects.contains( aRule.Object2 ) ) {
242 void HYDROGUI_PriorityTableModel::createNewRule()
244 if ( myObjects.count() < 2 ) {
248 HYDROData_CustomRule aNewRule;
249 aNewRule.Object1 = myObjects.at(0);
250 aNewRule.Object2 = myObjects.at(1);
251 aNewRule.Priority = LESS;
252 aNewRule.MergeType = HYDROData_Zone::Merge_ZMIN;
261 bool HYDROGUI_PriorityTableModel::removeRows ( int theRow, int theCount, const QModelIndex & theParent )
265 if ( theRow + theCount <= myRules.count() ) {
266 for ( int i = 1; i <= theCount; i++ ) {
267 myRules.removeAt( theRow );
277 Get available objects.
278 @return the list of object names
280 QStringList HYDROGUI_PriorityTableModel::getAvailableObjects() const
284 foreach ( const Handle(HYDROData_Object) anObj, myObjects ) {
285 if ( !anObj.IsNull() ) {
286 aNames << anObj->GetName();
295 @params theRows the list of rows to remove
297 void HYDROGUI_PriorityTableModel::removeRows ( const QList<int> theRows )
299 QList<int> aSortedRows = theRows;
300 qSort( aSortedRows );
302 int aRowToRemove = -1;
304 foreach ( int aRow, aSortedRows ) {
305 aRowToRemove = aRow - aNbRemoved;
306 if ( removeRow( aRowToRemove ) ) {
313 Get priority string representation.
314 @param thePriority the priority
316 QString HYDROGUI_PriorityTableModel::priorityToString( const int thePriority ) const
318 switch( thePriority )
323 return tr( "GREATER" );
330 Get merge type string representation.
331 @param theMergeType the merge type
333 QString HYDROGUI_PriorityTableModel::mergeTypeToString( const int theMergeType ) const
335 switch( theMergeType )
337 case HYDROData_Zone::Merge_Object:
338 return tr( "PRIORITY" );
339 case HYDROData_Zone::Merge_ZMIN:
341 case HYDROData_Zone::Merge_ZMAX: