]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMEDSImpl/SALOMEDSImpl_AttributeTable.hxx
Salome HOME
0020465: [CEA 335] sort tables in visualisation mode
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeTable.hxx
1 //  Copyright (C) 2007-2008  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 //  File   : SALOMEDSImpl_AttributeTable.hxx
23 //  Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
24 //
25
26 #ifndef _SALOMEDSImpl_AttributeTable_HeaderFile
27 #define _SALOMEDSImpl_AttributeTable_HeaderFile
28
29 #include "SALOMEDSImpl_Defines.hxx"
30
31 class SALOMEDSIMPL_EXPORT SALOMEDSImpl_AttributeTable
32 {
33 public:
34   //! Sort order
35   typedef enum { 
36     AscendingOrder,  //!< The items are sorted ascending
37     DescendingOrder, //!< The items are sorted descending
38   } SortOrder;
39
40   //! Sort policy (specifies how empty cells are taken into account when sorting)
41   typedef enum {
42     EmptyLowest,     //!< Empty cells are considered as lowest values
43     EmptyHighest,    //!< Empty cells are considered as highest values
44     EmptyFirst,      //!< Empty cells are always first
45     EmptyLast,       //!< Empty cells are always last
46     EmptyIgnore,     //!< Empty cells are ignored (stay at initial positions)
47   } SortPolicy;
48 };
49
50 template <class TTable> class TableSorter
51 {
52   TTable*                                 myTable;
53   SALOMEDSImpl_AttributeTable::SortOrder  mySortOrder;
54   SALOMEDSImpl_AttributeTable::SortPolicy mySortPolicy;
55   int                                     myIndex;
56   bool                                    myIsRow;
57
58 public:
59   TableSorter( TTable*                                 table,
60                SALOMEDSImpl_AttributeTable::SortOrder  so,
61                SALOMEDSImpl_AttributeTable::SortPolicy sp,
62                int                                     index,
63                bool                                    sortRow)
64     : myTable( table ), mySortOrder( so ), mySortPolicy( sp ), 
65       myIndex( index ), myIsRow( sortRow ) {}
66
67   bool operator() ( int idx1, int idx2 ) 
68   {
69     bool hasValue1 = myIsRow ? myTable->HasValue( myIndex, idx1 ) : myTable->HasValue( idx1, myIndex );
70     bool hasValue2 = myIsRow ? myTable->HasValue( myIndex, idx2 ) : myTable->HasValue( idx2, myIndex );
71     if ( !hasValue1 && !hasValue2 ) {
72       return false;
73     }
74     else if ( !hasValue1 || !hasValue2 ) {
75       switch ( mySortPolicy ) {
76       case SALOMEDSImpl_AttributeTable::EmptyLowest:
77         return (!hasValue1) ? 
78           (mySortOrder == SALOMEDSImpl_AttributeTable::AscendingOrder) : 
79           (mySortOrder == SALOMEDSImpl_AttributeTable::DescendingOrder);
80       case SALOMEDSImpl_AttributeTable::EmptyHighest:
81         return (!hasValue1) ? 
82           (mySortOrder != SALOMEDSImpl_AttributeTable::AscendingOrder) : 
83           (mySortOrder != SALOMEDSImpl_AttributeTable::DescendingOrder);
84       case SALOMEDSImpl_AttributeTable::EmptyFirst:
85         return (!hasValue1);
86       case SALOMEDSImpl_AttributeTable::EmptyLast:
87         return hasValue1;
88       case SALOMEDSImpl_AttributeTable::EmptyIgnore:
89       default:
90         // should not go here
91         return false;
92       }
93     }
94     else {
95       if ( myIsRow ) {
96         return mySortOrder == SALOMEDSImpl_AttributeTable::AscendingOrder ? 
97           myTable->GetValue( myIndex, idx1 ) < myTable->GetValue( myIndex, idx2 ) :
98           myTable->GetValue( myIndex, idx2 ) < myTable->GetValue( myIndex, idx1 );
99       }
100       else {
101         return mySortOrder == SALOMEDSImpl_AttributeTable::AscendingOrder ? 
102           myTable->GetValue( idx1, myIndex ) < myTable->GetValue( idx2, myIndex ) :
103           myTable->GetValue( idx2, myIndex ) < myTable->GetValue( idx1, myIndex );
104       }
105     }
106   }
107 };
108
109 #endif // _SALOMEDSImpl_AttributeTable_HeaderFile