libyui-ncurses  2.57.2
NCTableSort.h
1 /*
2  Copyright (C) 2020 SUSE LLC
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 #ifndef NCTableSort_h
18 #define NCTableSort_h
19 
20 #include <string>
21 #include <vector>
22 #include <yui/YItem.h>
23 
24 class NCTableLine;
25 
26 
27 /**
28  * Support classes for sorting by column in a table for use in an NCTablePad
29  **/
30 
31 
32 /**
33  * Abstract base class for sorting strategies.
34  **/
36 {
37 public:
38 
39  NCTableSortStrategyBase( int sortCol = 0, bool reverse = false )
40  : _sortCol( sortCol )
41  , _reverse( reverse )
42  {}
43 
44  virtual ~NCTableSortStrategyBase()
45  {}
46 
47  /**
48  * Sort items between 'begin' and 'end' in place.
49  *
50  * Derived classes are required to implement this.
51  **/
52  virtual void sort( YItemIterator begin, YItemIterator end ) = 0;
53 
54 
55  int sortCol() const { return _sortCol; }
56  void setSortCol( int col ) { _sortCol = col; }
57 
58  bool reverse() const { return _reverse; }
59  void setReverse( bool reverse ) { _reverse = reverse; }
60 
61 
62 private:
63 
64  int _sortCol;
65  bool _reverse;
66 };
67 
68 
69 /**
70  * Default sort strategy
71  **/
73 {
74 public:
75  virtual void sort( YItemIterator begin, YItemIterator end ) override;
76 
77 private:
78 
79  /**
80  * Comparison functor.
81  *
82  * This uses the sort key of the cell if it has one, the label if not.
83  *
84  * It also tries to convert strings to numbers to do a numeric comparison
85  * if possible.
86  **/
87  class Compare
88  {
89  public:
90  Compare( int sortCol, bool reverse )
91  : _sortCol( sortCol )
92  , _reverse( reverse )
93  {}
94 
95  /**
96  * The comparison itself: Return the result of item1 < item2
97  **/
98  bool operator() ( YItem * item1, YItem * item2 ) const;
99 
100  protected:
101 
102  /**
103  * Return the sort key of column no. _sortCol for an item or, if it
104  * doesn't have one, its label in that column.
105  **/
106  std::wstring smartSortKey( YItem * item ) const;
107 
108  /**
109  * Try to convert a string to a number. Return the number and set the
110  * 'ok' flag to 'true' on success, to 'false' on failure.
111  **/
112  long long toNumber( const std::wstring& str, bool * ok ) const;
113 
114 
115  // Data members
116 
117  const int _sortCol;
118  const bool _reverse;
119  };
120 
121 };
122 
123 
124 #endif // NCTableSort_h
NCTableSortStrategyBase
Support classes for sorting by column in a table for use in an NCTablePad.
Definition: NCTableSort.h:36
NCTableSortDefault
Default sort strategy.
Definition: NCTableSort.h:73
NCTableSortDefault::sort
virtual void sort(YItemIterator begin, YItemIterator end) override
Support classes for sorting by column in a table for use in an NCTablePad.
Definition: NCTableSort.cc:30
NCTableLine
One line in a NCTable with multiple cells and an optional tree hierarchy.
Definition: NCTableItem.h:68
NCTableSortStrategyBase::sort
virtual void sort(YItemIterator begin, YItemIterator end)=0
Sort items between 'begin' and 'end' in place.