Salome HOME
c0e1dacbfd2ea5b4edd6256e413c944c9371b5b5
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_PriorityTableModel.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_PriorityTableModel.h"
24
25
26 /**
27   Constructor.
28   @param theParent the parent object
29 */
30 HYDROGUI_PriorityTableModel::HYDROGUI_PriorityTableModel( QObject* theParent )
31  : QAbstractTableModel( theParent )
32 {
33 }
34
35 /**
36   Destructor.
37 */
38 HYDROGUI_PriorityTableModel::~HYDROGUI_PriorityTableModel()
39 {
40 }
41
42 /**
43  */
44 Qt::ItemFlags   HYDROGUI_PriorityTableModel::flags( const QModelIndex & theIndex ) const
45 {
46   return Qt::ItemIsSelectable   | Qt::ItemIsEditable | Qt::ItemIsEnabled;
47 }
48
49 /**
50  */
51 QVariant HYDROGUI_PriorityTableModel::data( const QModelIndex &theIndex, int theRole ) const
52 {
53   QVariant aVariant;
54
55   int aRow = theIndex.row();
56   int aColumn = theIndex.column();
57
58   if ( !theIndex.isValid() || aRow > myRules.size() ) {
59     return aVariant;
60   }
61   
62   if ( theRole == Qt::DisplayRole ) {
63     HYDROData_CustomRule aRule = myRules.at(aRow);
64
65     if ( aColumn == 0 ) {
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 );
73     }
74   } else if ( theRole == Qt::EditRole ) {
75     HYDROData_CustomRule aRule = myRules.at(aRow);
76
77     if ( aColumn == 0 ) {
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;
85     }
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 );
100     } 
101   } else if ( theRole == Qt::TextAlignmentRole ) {
102     aVariant = Qt::AlignCenter;
103   }
104
105   return aVariant;
106 }
107
108 /**
109  */
110 bool HYDROGUI_PriorityTableModel::setData( const QModelIndex & theIndex, const QVariant & theValue, int theRole )
111 {
112   int aRow = theIndex.row();
113   if ( !theIndex.isValid() || aRow > myRules.size() ) {
114     return false;
115   }
116
117   bool aRes = false;
118
119   if ( theRole ==  Qt::EditRole ) {
120     int aColumn = theIndex.column();
121
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 ) {
127           anObject = anObj;
128           break;
129         }
130       }
131       if ( !anObject.IsNull() ) {
132         if ( aColumn == 0 ) {
133           myRules[aRow].Object1 = anObject;
134         } else {
135           myRules[aRow].Object2 = anObject;
136         }
137
138         aRes = true;
139       }
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();;
144     }
145   }
146
147   return aRes;
148 }
149
150 /**
151  */
152 int HYDROGUI_PriorityTableModel::rowCount( const QModelIndex &theParent ) const
153 {
154   return myRules.count();
155 }
156
157 /**
158  */
159 int HYDROGUI_PriorityTableModel::columnCount( const QModelIndex &theParent ) const
160 {
161   return 4;
162 }
163
164 /**
165   Set rules.
166   @param theRules the list of rules
167 */
168 void HYDROGUI_PriorityTableModel::setRules( const HYDROData_ListOfRules& theRules )
169 {
170   myRules = theRules;
171
172   reset();
173 }
174
175 /**
176   Get rules.
177   @return the list of rules
178 */
179 HYDROData_ListOfRules HYDROGUI_PriorityTableModel::getRules() const
180 {
181   return myRules;
182 }
183
184 /**
185 */
186 QVariant HYDROGUI_PriorityTableModel::headerData( int theSection,
187                                                   Qt::Orientation theOrientation,
188                                                   int theRole ) const
189 {
190   QVariant aData;
191
192   if ( theRole != Qt::DisplayRole ) {
193     return aData;
194   }
195
196   if ( theOrientation == Qt::Horizontal ) {
197     switch( theSection )
198     {
199     case 0:
200       aData = tr( "OBJECT1" );
201       break;
202     case 1:
203       aData = tr( "PRIORITY" );
204       break;
205     case 2:
206       aData = tr( "OBJECT2" );
207       break;
208     case 3:
209       aData = tr( "BATHYMETRY" );
210       break;
211     };
212   } else if ( theOrientation == Qt::Vertical ) {
213     aData = theSection + 1;
214   }
215
216   return aData;
217 }
218
219 /**
220   Set objects which could be used for rules.
221   @param theObjects the list of objects
222  */
223 void HYDROGUI_PriorityTableModel::setObjects( const QList<Handle(HYDROData_Object)>& theObjects )
224 {
225   myObjects = theObjects;
226
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 ) ) {
232       anIter.remove();
233     }
234   }
235
236   reset();
237 }
238
239 /**
240  Create new rule. 
241  */
242 void HYDROGUI_PriorityTableModel::createNewRule() 
243 {
244   if ( myObjects.count() < 2 ) {
245     return;
246   }
247
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;
253
254   myRules << aNewRule;
255
256   reset();
257 }
258
259 /**
260  */
261 bool HYDROGUI_PriorityTableModel::removeRows ( int theRow, int theCount, const QModelIndex & theParent )
262 {
263   bool aRes = false;
264
265   if ( theRow + theCount <= myRules.count() ) {
266     for ( int i = 1; i <= theCount; i++ ) {
267       myRules.removeAt( theRow );
268     }
269     aRes = true;
270     reset();
271   }
272
273   return true;
274 }
275
276 /**
277  Get available objects.
278  @return the list of object names
279  */
280 QStringList HYDROGUI_PriorityTableModel::getAvailableObjects() const
281 {
282   QStringList aNames;
283
284   foreach ( const Handle(HYDROData_Object) anObj, myObjects ) {
285     if ( !anObj.IsNull() ) {
286       aNames << anObj->GetName();
287     }
288   }
289
290   return aNames;
291 }
292
293 /**
294  Remove the rows.
295  @params theRows the list of rows to remove
296  */
297 void HYDROGUI_PriorityTableModel::removeRows ( const QList<int> theRows )
298 {
299   QList<int> aSortedRows = theRows;
300   qSort( aSortedRows );
301
302   int aRowToRemove = -1;
303   int aNbRemoved = 0;
304   foreach ( int aRow, aSortedRows ) {
305     aRowToRemove = aRow - aNbRemoved;
306     if ( removeRow( aRowToRemove ) ) {
307       aNbRemoved++;
308     }
309   }
310 }
311
312 /**
313  Get priority string representation.
314  @param thePriority the priority
315  */
316 QString HYDROGUI_PriorityTableModel::priorityToString( const int thePriority ) const
317 {
318   switch( thePriority )
319   {
320   case LESS:
321     return tr( "LESS" );
322   case GREATER:
323     return tr( "GREATER" );
324   default:
325     return QString();
326   };
327 }
328
329 /**
330  Get merge type string representation.
331  @param theMergeType the merge type
332  */
333 QString HYDROGUI_PriorityTableModel::mergeTypeToString( const int theMergeType ) const
334 {
335   switch( theMergeType )
336   {
337   case HYDROData_Zone::Merge_Object:
338     return tr( "PRIORITY" );
339   case HYDROData_Zone::Merge_ZMIN:
340     return tr( "ZMIN" );
341   case HYDROData_Zone::Merge_ZMAX:
342     return tr( "ZMAX" );
343   default:
344     return QString();
345   };
346 }