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