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