]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/util/fmtwidgetitem.h
kes Change Bacula trademark owner from John Walker to Kern Sibbald
[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 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  *   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 /*
44  * common conversion routines
45  *
46  */
47 QString convertJobStatus(const QString &sts);
48
49
50 /*
51  * base class for formatters
52  *
53  */
54 class ItemFormatterBase
55 {
56 public:
57    enum BYTES_CONVERSION {
58       BYTES_CONVERSION_NONE,
59       BYTES_CONVERSION_IEC,
60       BYTES_CONVERSION_SI,
61    };
62
63 public:
64    virtual ~ItemFormatterBase(); 
65
66    /* Prints Yes if fld is != 0, No otherwise. Centers field if center true*/
67    void setBoolFld(int index, const QString &fld, bool center = true);
68    void setBoolFld(int index, int fld, bool center = true);
69
70    /* Normal text field. Centers field if center true*/
71    void setTextFld(int index, const QString &fld, bool center = false);
72
73    /* Right-aligned text field. */
74    void setRightFld(int index, const QString &fld);
75
76    /* Numeric field - sorted as numeric type */
77    void setNumericFld(int index, const QString &fld);
78    void setNumericFld(int index, const QString &fld, const QVariant &sortVal);
79
80    /* fld value interpreted as bytes and formatted with size suffixes */
81    void setBytesFld(int index, const QString &fld);
82
83    /* fld value interpreted as seconds and formatted with y,m,w,h suffixes */
84    void setDurationFld(int index, const QString &fld);
85
86    /* fld value interpreted as volume status. Colored accordingly */
87    void setVolStatusFld(int index, const QString &fld, bool center = true);
88   
89    /* fld value interpreted as job status. Colored accordingly */
90    void setJobStatusFld(int index, const QString &status, bool center = true);
91   
92    /* fld value interpreted as job type. */
93    void setJobTypeFld(int index, const QString &fld, bool center = false);
94   
95    /* fld value interpreted as job level. */
96    void setJobLevelFld(int index, const QString &fld, bool center = false);
97   
98    static void setBytesConversion(BYTES_CONVERSION b) {
99       cnvFlag = b;
100    }
101    static BYTES_CONVERSION getBytesConversion() {
102       return cnvFlag;
103    }
104
105 protected:
106    /* only derived classes can create one of these */
107    ItemFormatterBase();
108
109    virtual void setText(int index, const QString &fld) = 0;
110    virtual void setTextAlignment(int index, int align) = 0;
111    virtual void setBackground(int index, const QBrush &) = 0;
112
113    /* sets the *optional* value used for sorting */
114    virtual void setSortValue(int index, const QVariant &value) = 0;
115
116 private:
117
118    /* bytes formatted as power-of-two with IEC suffixes (KiB, MiB, and so on) */
119    static QString convertBytesIEC(qint64 fld);
120
121    /* bytes formatted as power-of-ten with SI suffixes (kB, MB, and so on) */
122    static QString convertBytesSI(qint64 fld);
123
124 private:
125    static BYTES_CONVERSION cnvFlag;
126 };
127
128 /*
129  * This class can be used instead of QTreeWidgetItem (it allocates one internally,
130  * to format data fields.
131  * All setXXXFld routines receive a column index and the unformatted string value.
132  */
133 class TreeItemFormatter : public ItemFormatterBase
134 {
135 public:
136
137    TreeItemFormatter(QTreeWidgetItem &parent, int indent_level);
138
139    /* access internal widget */
140    QTreeWidgetItem *widget() { return wdg; }
141    const QTreeWidgetItem *widget() const { return wdg; }
142
143 protected:
144    virtual void setText(int index, const QString &fld);
145    virtual void setTextAlignment(int index, int align);
146    virtual void setBackground(int index, const QBrush &);
147    virtual void setSortValue(int index, const QVariant &value);
148
149 private:
150    QTreeWidgetItem *wdg;
151    int level;
152 };
153
154 /*
155  * This class can be used instead of QTableWidgetItem (it allocates one internally,
156  * to format data fields.
157  * All setXXXFld routines receive the column and the unformatted string value.
158  */
159 class TableItemFormatter : public ItemFormatterBase
160 {
161 private:
162
163    /* specialized widget item - allows an optional data property for sorting */ 
164    class BatSortingTableItem : public QTableWidgetItem
165    {
166    private:
167       static const int SORTDATA_ROLE = Qt::UserRole + 100;
168    public:
169       BatSortingTableItem();
170       
171       /* uses the sort data if available, reverts to default behavior othervise */
172       virtual bool operator< ( const QTableWidgetItem & o ) const;
173
174       /* set the value used for sorting - MUST BE A NUMERIC TYPE */
175       void setSortData(const QVariant &d);
176    };
177
178 public:
179
180    TableItemFormatter(QTableWidget &parent, int row);
181
182    /* access internal widget at column col*/
183    QTableWidgetItem *widget(int col);
184    const QTableWidgetItem *widget(int col) const;
185
186 protected:
187    virtual void setText(int index, const QString &fld);
188    virtual void setTextAlignment(int index, int align);
189    virtual void setBackground(int index, const QBrush &);
190    virtual void setSortValue(int index, const QVariant &value);
191
192 private:
193    QTableWidget *parent;
194    int row;
195    BatSortingTableItem  *last;
196 };
197
198 #endif /* _FMTWIDGETITEM_H_ */