]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/util/fmtwidgetitem.h
bat: Implement a Date and FileType helper in ItemFormatter
[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-2010 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 three of the GNU Affero 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 Affero 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 Kern Sibbald.
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  *   TreeView formatting helpers - Riccardo Ghetta, May 2008 
32  */
33
34 class QWidget;
35 class QTreeWidgetItem;
36 class QTableWidget;
37 class QTableWidgetItem;
38 class QString;
39 class QBrush;
40
41
42 /*
43  * common conversion routines
44  *
45  */
46 QString convertJobStatus(const QString &sts);
47
48 /* bytes formatted as power-of-two with IEC suffixes (KiB, MiB, and so on) */
49 QString convertBytesIEC(qint64 fld);
50
51 /* bytes formatted as power-of-ten with SI suffixes (kB, MB, and so on) */
52 QString convertBytesSI(qint64 fld);
53
54 /*
55  * disable widget updating
56  */
57 class Freeze
58 {
59 private:
60    QWidget *qw;
61
62  public:
63    Freeze(QWidget &q);
64    ~Freeze();
65 };
66
67
68
69 /*
70  * base class for formatters
71  *
72  */
73 class ItemFormatterBase
74 {
75 public:
76    enum BYTES_CONVERSION {
77       BYTES_CONVERSION_NONE,
78       BYTES_CONVERSION_IEC,
79       BYTES_CONVERSION_SI,
80    };
81
82 public:
83    virtual ~ItemFormatterBase(); 
84
85    /* Prints Yes if fld is != 0, No otherwise. Centers field if center true*/
86    void setBoolFld(int index, const QString &fld, bool center = true);
87    void setBoolFld(int index, int fld, bool center = true);
88
89    /* Print nice icon to represent percent */
90    void setPercent(int index, float number);
91
92    /* Normal text field. Centers field if center true*/
93    void setTextFld(int index, const QString &fld, bool center = false);
94
95    /* Normal date field. Centers field if center true*/
96    void setDateFld(int index, utime_t fld, bool center = false);
97
98    /* Right-aligned text field. */
99    void setRightFld(int index, const QString &fld);
100
101    /* Numeric field - sorted as numeric type */
102    void setNumericFld(int index, const QString &fld);
103    void setNumericFld(int index, const QString &fld, const QVariant &sortVal);
104
105    /* fld value interpreted as bytes and formatted with size suffixes */
106    void setBytesFld(int index, const QString &fld);
107
108    /* fld value interpreted as seconds and formatted with y,m,w,h suffixes */
109    void setDurationFld(int index, const QString &fld);
110
111    /* fld value interpreted as volume status. Colored accordingly */
112    void setVolStatusFld(int index, const QString &fld, bool center = true);
113   
114    /* fld value interpreted as job status. Colored accordingly */
115    void setJobStatusFld(int index, const QString &status, bool center = true);
116   
117    /* fld value interpreted as job type. */
118    void setJobTypeFld(int index, const QString &fld, bool center = false);
119   
120    /* fld value interpreted as job level. */
121    void setJobLevelFld(int index, const QString &fld, bool center = false);
122
123    /* fld value interpreted as Online/Offline */
124    void setInChanger(int index, const QString &InChanger);
125   
126    /* fld value interpreted as file or folder */
127    void setFileType(int index, const QString &type);
128
129    static void setBytesConversion(BYTES_CONVERSION b) {
130       cnvFlag = b;
131    }
132    static BYTES_CONVERSION getBytesConversion() {
133       return cnvFlag;
134    }
135
136 protected:
137    /* only derived classes can create one of these */
138    ItemFormatterBase();
139
140    virtual void setText(int index, const QString &fld) = 0;
141    virtual void setTextAlignment(int index, int align) = 0;
142    virtual void setBackground(int index, const QBrush &) = 0;
143    virtual void setPixmap(int index, const QPixmap &pix) = 0;
144    virtual void setPixmap(int index, const QPixmap &pix, const QString &tip);
145
146    /* sets the *optional* value used for sorting */
147    virtual void setSortValue(int index, const QVariant &value) = 0;
148
149 private:
150    static BYTES_CONVERSION cnvFlag;
151 };
152
153 /*
154  * This class can be used instead of QTreeWidgetItem (it allocates one internally,
155  * to format data fields.
156  * All setXXXFld routines receive a column index and the unformatted string value.
157  */
158 class TreeItemFormatter : public ItemFormatterBase
159 {
160 public:
161
162    TreeItemFormatter(QTreeWidgetItem &parent, int indent_level);
163
164    /* access internal widget */
165    QTreeWidgetItem *widget() { return wdg; }
166    const QTreeWidgetItem *widget() const { return wdg; }
167
168 protected:
169    virtual void setText(int index, const QString &fld);
170    virtual void setTextAlignment(int index, int align);
171    virtual void setBackground(int index, const QBrush &);
172    virtual void setSortValue(int index, const QVariant &value);
173    virtual void setPixmap(int index, const QPixmap &pix);
174
175 private:
176    QTreeWidgetItem *wdg;
177    int level;
178 };
179
180 /*
181  * This class can be used instead of QTableWidgetItem (it allocates one internally,
182  * to format data fields.
183  * All setXXXFld routines receive the column and the unformatted string value.
184  */
185 class TableItemFormatter : public ItemFormatterBase
186 {
187 private:
188
189    /* specialized widget item - allows an optional data property for sorting */ 
190    class BatSortingTableItem : public QTableWidgetItem
191    {
192    private:
193       static const int SORTDATA_ROLE = Qt::UserRole + 100;
194    public:
195       BatSortingTableItem();
196       
197       /* uses the sort data if available, reverts to default behavior othervise */
198       virtual bool operator< ( const QTableWidgetItem & o ) const;
199
200       /* set the value used for sorting - MUST BE A NUMERIC TYPE */
201       void setSortData(const QVariant &d);
202    };
203
204 public:
205
206    TableItemFormatter(QTableWidget &parent, int row);
207
208    /* access internal widget at column col*/
209    QTableWidgetItem *widget(int col);
210    const QTableWidgetItem *widget(int col) const;
211
212 protected:
213    virtual void setText(int index, const QString &fld);
214    virtual void setTextAlignment(int index, int align);
215    virtual void setBackground(int index, const QBrush &);
216    virtual void setSortValue(int index, const QVariant &value);
217    virtual void setPixmap(int index, const QPixmap &pix);
218    virtual void setPixmap(int index, const QPixmap &pix, const QString &tip);
219
220 private:
221    QTableWidget *parent;
222    int row;
223    BatSortingTableItem  *last;
224 };
225
226 #endif /* _FMTWIDGETITEM_H_ */