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