]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/QY2DiskUsageList.h
picking up branches/tmp/sh/qt4-port/, merging it with trunk
[duncan/yast2-qt4.git] / src / QY2DiskUsageList.h
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                          contributed Qt widgets                      |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       QY2DiskUsageList.h
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17   This is a pure Qt widget - it can be used independently of YaST2.
18
19 /-*/
20
21
22 #ifndef QY2DiskUsageList_h
23 #define QY2DiskUsageList_h
24
25 #include <QY2ListView.h>
26 #include <FSize.h>
27 #include <qcolor.h>
28
29
30 class QY2DiskUsageListItem;
31
32
33 /**
34  * Generic scrollable list of disk usage for any number of partitions.
35  **/
36 class QY2DiskUsageList : public QY2ListView
37 {
38     Q_OBJECT
39
40 public:
41
42     /**
43      * Constructor.
44      *
45      * Adds a standard set of list columns if 'addStdColumns' is
46      *'true'. Otherwise the caller is responsible for adding any columns.
47      **/
48     QY2DiskUsageList( QWidget * parent, bool addStdColumns = true );
49
50     /**
51      * Destructor
52      **/
53     virtual ~QY2DiskUsageList();
54
55
56     // Column numbers
57
58     int nameCol()               const   { return _nameCol;              }
59     int percentageBarCol()      const   { return _percentageBarCol;     }
60     int percentageCol()         const   { return _percentageCol;        }
61     int usedSizeCol()           const   { return _usedSizeCol;          }
62     int freeSizeCol()           const   { return _freeSizeCol;          }
63     int totalSizeCol()          const   { return _totalSizeCol;         }
64     int deviceNameCol()         const   { return _deviceNameCol;        }
65
66
67 protected:
68
69     int _nameCol;
70     int _percentageBarCol;
71     int _percentageCol;
72     int _usedSizeCol;
73     int _freeSizeCol;
74     int _totalSizeCol;
75     int _deviceNameCol;
76 };
77
78
79
80 /**
81  * Abstract base class for one partition ( mount point ) to display in a
82  * QY2DiskUsageList.
83  *
84  * This class contains pure virtuals, so it cannot be used directly.
85  **/
86 class QY2DiskUsageListItem: public QY2ListViewItem
87 {
88 protected:
89     /**
90      * Constructor.
91      *
92      * Call updateData() after the constructor for the initial display
93      * update. Unfortunately, this cannot be done automatically in the
94      * constructor since it uses virtual methods that are not available yet at
95      * this point.
96      **/
97     QY2DiskUsageListItem( QY2DiskUsageList * parent );
98
99
100     /**
101      * Destructor.
102      **/
103     virtual ~QY2DiskUsageListItem();
104
105
106 public:
107
108     /**
109      * The currently used size of this partition.
110      *
111      * Derived classes need to implement this method.
112      **/
113
114     virtual FSize usedSize() const = 0;
115
116     /**
117      * The total size of this partition.
118      *
119      * Derived classes need to implement this method.
120      **/
121     virtual FSize totalSize() const = 0;
122
123     /**
124      * The current free size of this partition.
125      *
126      * Derived classes can choose reimpmenent this if it is less expensive than
127      * calculating this value each time from usedSize() and totalSize() which
128      * is the default implementation.
129      **/
130     virtual FSize freeSize() const;
131
132     /**
133      * The currently used percentage ( 0..100 ) of this partition.
134      *
135      * Derived classes can choose reimpmenent this if it is less expensive than
136      * calculating this value each time from usedSize() and totalSize() which
137      * is the default implementation.
138      **/
139     virtual int usedPercent() const;
140
141     /**
142      * The name to display for this partition.
143      * It makes most sense to use the mount point here ( but this is not a
144      * requirement ). This is what will be displayed in the "Name" column.
145      *
146      * Derived classes need to implement this method.
147      **/
148     virtual QString name() const = 0;
149
150     /**
151      * The device name of this partition.
152      *
153      * Derived classes may choose to reimplement this method.
154      * This default implementation returns an empty string.
155      **/
156     virtual QString deviceName() const { return ""; }
157
158
159     /**
160      * Update this item's status ( here: the numeric fields ).
161      * Triggered by QY2ListView::updateAllItemStates().
162      *
163      * Reimplemented from QY2ListViewItem.
164      **/
165     virtual void updateStatus();
166
167     /**
168      * Update this item's data completely.
169      * Triggered by QY2ListView::updateAllItemData().
170      *
171      * Reimplemented from QY2ListViewItem.
172      **/
173     virtual void updateData();
174
175     /**
176      * Re-declare ordinary setText() method so the compiler doesn't get
177      * confused which one to use.
178      **/
179     void setText( int column, const QString & text )
180         { Q3ListViewItem::setText( column, text ); }
181
182     /**
183      * Set a column text via FSize.
184      **/
185     void setText( int column, const FSize & size );
186
187     /**
188      * Comparison function used for sorting the list.
189      * Returns:
190      * -1 if this <  other
191      *  0 if this == other
192      * +1 if this >  other
193      *
194      * Reimplemented from QY2ListViewItem.
195      **/
196     virtual int compare( Q3ListViewItem *       other,
197                          int                    col,
198                          bool                   ascending ) const;
199
200     // Columns
201
202     int nameCol()               const   { return _diskUsageList->nameCol();             }
203     int percentageBarCol()      const   { return _diskUsageList->percentageBarCol();    }
204     int percentageCol()         const   { return _diskUsageList->percentageCol();       }
205     int usedSizeCol()           const   { return _diskUsageList->usedSizeCol();         }
206     int freeSizeCol()           const   { return _diskUsageList->freeSizeCol();         }
207     int totalSizeCol()          const   { return _diskUsageList->totalSizeCol();        }
208     int deviceNameCol()         const   { return _diskUsageList->deviceNameCol();       }
209
210
211 protected:
212
213     /**
214      * ( Re- ) initialize fields - all displayed fields ( if 'allFields' is
215      * 'true' ) or only the varying fields ( used, free, percentage ).
216      **/
217     void init( bool allFields );
218
219     /**
220      * Paint method.
221      *
222      * Reimplemented from QY2ListViewItem.
223      **/
224     virtual void paintCell( QPainter *          painter,
225                             const QColorGroup & colorGroup,
226                             int                 column,
227                             int                 width,
228                             int                 alignment );
229
230     /**
231      * Paint a percentage bar into a @ref QListViewItem cell.
232      * 'width' is the width of the entire cell.
233      * 'indent' is the number of pixels to indent the bar.
234      *
235      * Stolen from KDirStat::KDirTreeView with the author's permission.
236      **/
237     void paintPercentageBar( float              percent,
238                              QPainter *         painter,
239                              int                indent,
240                              int                width,
241                              const QColor &     fillColor,
242                              const QColor &     barBackground   );
243
244     /**
245      * Return a color that contrasts to 'contrastColor'.
246      *
247      * Stolen from KDirStat::KDirTreeView with the author's permission.
248      **/
249     QColor contrastingColor ( const QColor &    desiredColor,
250                               const QColor &    contrastColor );
251
252     /**
253      * Interpolate ( in the HSV color space ) a color between 'minColor' and
254      * 'maxColor' for a current value 'val' so that 'minVal' corresponds to
255      * 'minColor' and 'maxVal' to 'maxColor'.
256      *
257      * Returns the interpolated color.
258      **/
259     virtual QColor interpolateColor( int                val,
260                                      int                minVal,
261                                      int                maxVal,
262                                      const QColor &     minColor,
263                                      const QColor &     maxColor );
264
265     /**
266      * Interpolate ( translate ) a value 'from' in the range between 'minFrom'
267      * and 'maxFrom'  to a range between 'minTo' and 'maxTo'.
268      **/
269     int interpolate( int from,
270                      int minFrom,       int maxFrom,
271                      int minTo,         int maxTo       );
272
273
274     //
275     // Data members
276     //
277
278     QY2DiskUsageList * _diskUsageList;
279 };
280
281
282
283
284 #endif // ifndef QY2DiskUsageList_h