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