]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/util/fmtwidgetitem.h
Riccardo's patch to correct sorting on numeric fields.
[bacula/bacula] / bacula / src / qt-console / util / fmtwidgetitem.h
1 #ifndef _FMTWIDGETITEM_H_
2 #define _FMTWIDGETITEM_H_
3 /*
4    Bacula® - The Network Backup Solution
5
6    Copyright (C) 2007-2007 Free Software Foundation Europe e.V.
7
8    The main author of Bacula is Kern Sibbald, with contributions from
9    many others, a complete list can be found in the file AUTHORS.
10    This program is Free Software; you can redistribute it and/or
11    modify it under the terms of version two of the GNU General Public
12    License as published by the Free Software Foundation and included
13    in the file LICENSE.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Bacula® is a registered trademark of John Walker.
26    The licensor of Bacula is the Free Software Foundation Europe
27    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
28    Switzerland, email:ftf@fsfeurope.org.
29 */
30 /*
31  *   Version $Id$
32  *
33  *   TreeView formatting helpers - Riccardo Ghetta, May 2008 
34  */
35
36 class QTreeWidgetItem;
37 class QTableWidget;
38 class QTableWidgetItem;
39 class QString;
40 class QBrush;
41
42 /*
43  * base class for formatters
44  *
45  */
46 class ItemFormatterBase
47 {
48 public:
49    enum BYTES_CONVERSION {
50       BYTES_CONVERSION_NONE,
51       BYTES_CONVERSION_IEC,
52       BYTES_CONVERSION_SI,
53    };
54
55 public:
56    virtual ~ItemFormatterBase(); 
57
58    /* Prints Yes if fld is != 0, No otherwise. Centers field if center true*/
59    void setBoolFld(int index, const QString &fld, bool center = true);
60    void setBoolFld(int index, int fld, bool center = true);
61
62    /* Normal text field. Centers field if center true*/
63    void setTextFld(int index, const QString &fld, bool center = false);
64
65    /* Right-aligned text field. */
66    void setRightFld(int index, const QString &fld);
67
68    /* Numeric field - sorted as numeric type */
69    void setNumericFld(int index, const QString &fld);
70    void setNumericFld(int index, const QString &fld, const QVariant &sortVal);
71
72    /* fld value interpreted as bytes and formatted with size suffixes */
73    void setBytesFld(int index, const QString &fld);
74
75    /* fld value interpreted as seconds and formatted with y,m,w,h suffixes */
76    void setDurationFld(int index, const QString &fld);
77
78    /* fld value interpreted as volume status. Colored accordingly */
79    void setVolStatusFld(int index, const QString &fld, bool center = true);
80   
81    /* fld value interpreted as job status. Colored accordingly */
82    void setJobStatusFld(int index, const QString &shortStatus, const QString &longstatus, 
83                         bool center = true);
84   
85    /* fld value interpreted as job type. */
86    void setJobTypeFld(int index, const QString &fld, bool center = false);
87   
88    /* fld value interpreted as job level. */
89    void setJobLevelFld(int index, const QString &fld, bool center = false);
90   
91    static void setBytesConversion(BYTES_CONVERSION b) {
92       cnvFlag = b;
93    }
94    static BYTES_CONVERSION getBytesConversion() {
95       return cnvFlag;
96    }
97
98 protected:
99    /* only derived classes can create one of these */
100    ItemFormatterBase();
101
102    virtual void setText(int index, const QString &fld) = 0;
103    virtual void setTextAlignment(int index, int align) = 0;
104    virtual void setBackground(int index, const QBrush &) = 0;
105
106    /* sets the *optional* value used for sorting */
107    virtual void setSortValue(int index, const QVariant &value) = 0;
108
109 private:
110
111    /* bytes formatted as power-of-two with IEC suffixes (KiB, MiB, and so on) */
112    static QString convertBytesIEC(qint64 fld);
113
114    /* bytes formatted as power-of-ten with SI suffixes (kB, MB, and so on) */
115    static QString convertBytesSI(qint64 fld);
116
117 private:
118    static BYTES_CONVERSION cnvFlag;
119 };
120
121 /*
122  * This class can be used instead of QTreeWidgetItem (it allocates one internally,
123  * to format data fields.
124  * All setXXXFld routines receive a column index and the unformatted string value.
125  */
126 class TreeItemFormatter : public ItemFormatterBase
127 {
128 public:
129
130    TreeItemFormatter(QTreeWidgetItem &parent, int indent_level);
131
132    /* access internal widget */
133    QTreeWidgetItem *widget() { return wdg; }
134    const QTreeWidgetItem *widget() const { return wdg; }
135
136 protected:
137    virtual void setText(int index, const QString &fld);
138    virtual void setTextAlignment(int index, int align);
139    virtual void setBackground(int index, const QBrush &);
140    virtual void setSortValue(int index, const QVariant &value);
141
142 private:
143    QTreeWidgetItem *wdg;
144    int level;
145 };
146
147 /*
148  * This class can be used instead of QTableWidgetItem (it allocates one internally,
149  * to format data fields.
150  * All setXXXFld routines receive the column and the unformatted string value.
151  */
152 class TableItemFormatter : public ItemFormatterBase
153 {
154 private:
155
156    /* specialized widget item - allows an optional data property for sorting */ 
157    class BatSortingTableItem : public QTableWidgetItem
158    {
159    private:
160       static const int SORTDATA_ROLE = Qt::UserRole + 100;
161    public:
162       BatSortingTableItem();
163       
164       /* uses the sort data if available, reverts to default behavior othervise */
165       virtual bool operator< ( const QTableWidgetItem & o ) const;
166
167       /* set the value used for sorting - MUST BE A NUMERIC TYPE */
168       void setSortData(const QVariant &d);
169    };
170
171 public:
172
173    TableItemFormatter(QTableWidget &parent, int row);
174
175    /* access internal widget at column col*/
176    QTableWidgetItem *widget(int col);
177    const QTableWidgetItem *widget(int col) const;
178
179 protected:
180    virtual void setText(int index, const QString &fld);
181    virtual void setTextAlignment(int index, int align);
182    virtual void setBackground(int index, const QBrush &);
183    virtual void setSortValue(int index, const QVariant &value);
184
185 private:
186    QTableWidget *parent;
187    int row;
188    BatSortingTableItem  *last;
189 };
190
191 #endif /* _FMTWIDGETITEM_H_ */