]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/util/fmtwidgetitem.h
Tweak copyrights
[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 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  *   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    /* Right-aligned text field. */
96    void setRightFld(int index, const QString &fld);
97
98    /* Numeric field - sorted as numeric type */
99    void setNumericFld(int index, const QString &fld);
100    void setNumericFld(int index, const QString &fld, const QVariant &sortVal);
101
102    /* fld value interpreted as bytes and formatted with size suffixes */
103    void setBytesFld(int index, const QString &fld);
104
105    /* fld value interpreted as seconds and formatted with y,m,w,h suffixes */
106    void setDurationFld(int index, const QString &fld);
107
108    /* fld value interpreted as volume status. Colored accordingly */
109    void setVolStatusFld(int index, const QString &fld, bool center = true);
110   
111    /* fld value interpreted as job status. Colored accordingly */
112    void setJobStatusFld(int index, const QString &status, bool center = true);
113   
114    /* fld value interpreted as job type. */
115    void setJobTypeFld(int index, const QString &fld, bool center = false);
116   
117    /* fld value interpreted as job level. */
118    void setJobLevelFld(int index, const QString &fld, bool center = false);
119
120    /* fld value interpreted as Online/Offline */
121    void setInChanger(int index, const QString &InChanger);
122   
123    static void setBytesConversion(BYTES_CONVERSION b) {
124       cnvFlag = b;
125    }
126    static BYTES_CONVERSION getBytesConversion() {
127       return cnvFlag;
128    }
129
130 protected:
131    /* only derived classes can create one of these */
132    ItemFormatterBase();
133
134    virtual void setText(int index, const QString &fld) = 0;
135    virtual void setTextAlignment(int index, int align) = 0;
136    virtual void setBackground(int index, const QBrush &) = 0;
137    virtual void setPixmap(int index, const QPixmap &pix) = 0;
138    virtual void setPixmap(int index, const QPixmap &pix, const QString &tip);
139
140    /* sets the *optional* value used for sorting */
141    virtual void setSortValue(int index, const QVariant &value) = 0;
142
143 private:
144    static BYTES_CONVERSION cnvFlag;
145 };
146
147 /*
148  * This class can be used instead of QTreeWidgetItem (it allocates one internally,
149  * to format data fields.
150  * All setXXXFld routines receive a column index and the unformatted string value.
151  */
152 class TreeItemFormatter : public ItemFormatterBase
153 {
154 public:
155
156    TreeItemFormatter(QTreeWidgetItem &parent, int indent_level);
157
158    /* access internal widget */
159    QTreeWidgetItem *widget() { return wdg; }
160    const QTreeWidgetItem *widget() const { return wdg; }
161
162 protected:
163    virtual void setText(int index, const QString &fld);
164    virtual void setTextAlignment(int index, int align);
165    virtual void setBackground(int index, const QBrush &);
166    virtual void setSortValue(int index, const QVariant &value);
167    virtual void setPixmap(int index, const QPixmap &pix);
168
169 private:
170    QTreeWidgetItem *wdg;
171    int level;
172 };
173
174 /*
175  * This class can be used instead of QTableWidgetItem (it allocates one internally,
176  * to format data fields.
177  * All setXXXFld routines receive the column and the unformatted string value.
178  */
179 class TableItemFormatter : public ItemFormatterBase
180 {
181 private:
182
183    /* specialized widget item - allows an optional data property for sorting */ 
184    class BatSortingTableItem : public QTableWidgetItem
185    {
186    private:
187       static const int SORTDATA_ROLE = Qt::UserRole + 100;
188    public:
189       BatSortingTableItem();
190       
191       /* uses the sort data if available, reverts to default behavior othervise */
192       virtual bool operator< ( const QTableWidgetItem & o ) const;
193
194       /* set the value used for sorting - MUST BE A NUMERIC TYPE */
195       void setSortData(const QVariant &d);
196    };
197
198 public:
199
200    TableItemFormatter(QTableWidget &parent, int row);
201
202    /* access internal widget at column col*/
203    QTableWidgetItem *widget(int col);
204    const QTableWidgetItem *widget(int col) const;
205
206 protected:
207    virtual void setText(int index, const QString &fld);
208    virtual void setTextAlignment(int index, int align);
209    virtual void setBackground(int index, const QBrush &);
210    virtual void setSortValue(int index, const QVariant &value);
211    virtual void setPixmap(int index, const QPixmap &pix);
212    virtual void setPixmap(int index, const QPixmap &pix, const QString &tip);
213
214 private:
215    QTableWidget *parent;
216    int row;
217    BatSortingTableItem  *last;
218 };
219
220 #endif /* _FMTWIDGETITEM_H_ */