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