1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
13 File: QY2DiskUsageList.cc
15 Author: Stefan Hundhammer <sh@suse.de>
17 Textdomain "packages-qt"
19 This is a pure Qt widget - it can be used independently of YaST2.
25 #include "QY2DiskUsageList.h"
30 QY2DiskUsageList::QY2DiskUsageList( QWidget * parent, bool addStdColumns )
31 : QY2ListView( parent )
34 _percentageBarCol = -42;
44 addColumn( _( "Name" ) ); _nameCol = numCol++;
46 // Translators: Please keep this short!
47 addColumn( _( "Disk Usage" ) ); _percentageBarCol = numCol++;
48 addColumn( "" ); _percentageCol = numCol++;
49 addColumn( _( "Used" ) ); _usedSizeCol = numCol++;
50 addColumn( _( "Free" ) ); _freeSizeCol = numCol++;
51 addColumn( _( "Total" ) ); _totalSizeCol = numCol++;
53 addColumn( _( "Device" ) ); _deviceNameCol = numCol++;
56 setColumnAlignment( percentageCol(), Qt::AlignRight );
57 setColumnAlignment( usedSizeCol(), Qt::AlignRight );
58 setColumnAlignment( freeSizeCol(), Qt::AlignRight );
59 setColumnAlignment( totalSizeCol(), Qt::AlignRight );
61 setSorting( percentageBarCol() );
65 setSelectionMode( Q3ListView::NoSelection );
69 QY2DiskUsageList::~QY2DiskUsageList()
78 QY2DiskUsageListItem::QY2DiskUsageListItem( QY2DiskUsageList * parent )
79 : QY2ListViewItem( parent )
80 , _diskUsageList( parent )
87 QY2DiskUsageListItem::~QY2DiskUsageListItem()
96 QY2DiskUsageListItem::init( bool allFields )
98 if ( percentageCol() >= 0 )
100 QString percentageText;
101 percentageText.sprintf( "%d%%", usedPercent() );
102 setText( percentageCol(), percentageText );
105 if ( usedSizeCol() >= 0 ) setText( usedSizeCol(), usedSize() );
106 if ( freeSizeCol() >= 0 ) setText( freeSizeCol(), freeSize() );
110 if ( totalSizeCol() >= 0 ) setText( totalSizeCol(), totalSize() );
111 if ( nameCol() >= 0 ) setText( nameCol(), " " + name() );
112 if ( deviceNameCol() >= 0 ) setText( deviceNameCol(), deviceName() );
118 QY2DiskUsageListItem::setText( int column, const FSize & size )
120 QString sizeText = size.form( 0, 1, true ).c_str();
122 setText( column, sizeText );
127 QY2DiskUsageListItem::freeSize() const
129 return totalSize() - usedSize();
134 QY2DiskUsageListItem::usedPercent() const
138 if ( totalSize() != 0 )
139 percent = ( 100 * usedSize() ) / totalSize();
146 QY2DiskUsageListItem::updateStatus()
153 QY2DiskUsageListItem::updateData()
163 * Comparison function used for sorting the list.
170 QY2DiskUsageListItem::compare( Q3ListViewItem * otherListViewItem,
172 bool ascending ) const
174 QY2DiskUsageListItem * other = dynamic_cast<QY2DiskUsageListItem *> (otherListViewItem);
178 if ( col == percentageCol() ||
179 col == percentageBarCol() )
181 // Intentionally reverting sort order: Fullest first
183 if ( this->usedPercent() < other->usedPercent() ) return 1;
184 if ( this->usedPercent() > other->usedPercent() ) return -1;
187 else if ( col == usedSizeCol() )
189 if ( this->usedSize() < other->usedSize() ) return -1;
190 if ( this->usedSize() > other->usedSize() ) return 1;
193 else if ( col == freeSizeCol() )
195 if ( this->freeSize() < other->freeSize() ) return -1;
196 if ( this->freeSize() > other->freeSize() ) return 1;
199 else if ( col == totalSizeCol() )
201 if ( this->totalSize() < other->totalSize() ) return -1;
202 if ( this->totalSize() > other->totalSize() ) return 1;
207 return QY2ListViewItem::compare( otherListViewItem, col, ascending );
212 QY2DiskUsageListItem::paintCell( QPainter * painter,
213 const QColorGroup & colorGroup,
218 if ( column == percentageBarCol() )
220 QColor background = colorGroup.base();
221 painter->setBackgroundColor( background );
223 paintPercentageBar( usedPercent(),
225 _diskUsageList->treeStepSize() * depth(),
227 interpolateColor( usedPercent(),
229 QColor( 0, 0x80, 0 ), // Medium dark green
230 QColor( 0xFF, 0, 0 ) ), // Bright red
231 background.dark( 115 ) );
236 QColorGroup cg( colorGroup );
238 if ( usedSize() > totalSize() )
239 cg.setColor( QColorGroup::Text, Qt::red ); // Set red text foreground
241 // Intentionally bypassing the direct parent class method, use the grandparent's:
242 // Don't let QY2ListViewItem::_textColor / _backgroundColor interfere with our colors.
244 Q3ListViewItem::paintCell( painter, cg, column, width, alignment );
250 * Stolen from KDirStat::KDirTreeView with the author's permission.
253 QY2DiskUsageListItem::paintPercentageBar( float percent,
257 const QColor & fillColor,
258 const QColor & barBackground )
260 if ( percent > 100.0 ) percent = 100.0;
261 if ( percent < 0.0 ) percent = 0.0;
264 int x = _diskUsageList->itemMargin();
266 int w = width - 2 * _diskUsageList->itemMargin();
267 int h = height() - 2 * extraMargin;
270 painter->eraseRect( 0, 0, width, height() );
276 QPen pen( painter->pen() );
278 painter->setPen( pen );
279 painter->setBrush( Qt::NoBrush );
280 fillWidth = (int) ( ( w - 2 * penWidth ) * percent / 100.0 );
283 // Fill bar background.
285 painter->fillRect( x + penWidth, y + penWidth,
286 w - 2 * penWidth + 1, h - 2 * penWidth + 1,
289 * Notice: The Xlib XDrawRectangle() function always fills one
290 * pixel less than specified. Altough this is very likely just a
291 * plain old bug, it is documented that way. Obviously, Qt just
292 * maps the fillRect() call directly to XDrawRectangle() so they
293 * inherited that bug ( although the Qt doc stays silent about
294 * it ). So it is really necessary to compensate for that missing
295 * pixel in each dimension.
297 * If you don't believe it, see for yourself.
298 * Hint: Try the xmag program to zoom into the drawn pixels.
301 // Fill the desired percentage.
303 painter->fillRect( x + penWidth, y + penWidth,
304 fillWidth+1, h - 2 * penWidth+1,
310 pen.setColor( contrastingColor ( Qt::black,
311 painter->backgroundColor() ) );
312 painter->setPen( pen );
313 painter->drawLine( x, y, x+w, y );
314 painter->drawLine( x, y, x, y+h );
316 pen.setColor( contrastingColor( barBackground.dark(),
317 painter->backgroundColor() ) );
318 painter->setPen( pen );
319 painter->drawLine( x+1, y+1, x+w-1, y+1 );
320 painter->drawLine( x+1, y+1, x+1, y+h-1 );
322 pen.setColor( contrastingColor( barBackground.light(),
323 painter->backgroundColor() ) );
324 painter->setPen( pen );
325 painter->drawLine( x+1, y+h, x+w, y+h );
326 painter->drawLine( x+w, y, x+w, y+h );
328 pen.setColor( contrastingColor( Qt::white,
329 painter->backgroundColor() ) );
330 painter->setPen( pen );
331 painter->drawLine( x+2, y+h-1, x+w-1, y+h-1 );
332 painter->drawLine( x+w-1, y+1, x+w-1, y+h-1 );
338 * Stolen from KDirStat::KDirTreeView with the author's permission.
341 QY2DiskUsageListItem::contrastingColor( const QColor & desiredColor,
342 const QColor & contrastColor )
344 if ( desiredColor != contrastColor )
349 if ( contrastColor != contrastColor.light() )
351 // try a little lighter
352 return contrastColor.light();
356 // try a little darker
357 return contrastColor.dark();
363 QY2DiskUsageListItem::interpolateColor( int val,
366 const QColor & minColor,
367 const QColor & maxColor )
373 minColor.hsv( &minH, &minS, &minV );
374 maxColor.hsv( &maxH, &maxS, &maxV );
376 return QColor( interpolate( val, minVal, maxVal, minH, maxH ),
377 interpolate( val, minVal, maxVal, minS, maxS ),
378 interpolate( val, minVal, maxVal, minV, maxV ),
384 QY2DiskUsageListItem::interpolate( int from,
385 int minFrom, int maxFrom,
386 int minTo, int maxTo )
388 if ( minFrom > maxFrom )
390 // Swap min/max values
397 long x = from - minFrom;
399 x /= maxFrom - minFrom;
404 if ( x < minTo ) x = minTo;
405 if ( x > maxTo ) x = maxTo;
409 if ( x < maxTo ) x = maxTo;
410 if ( x > minTo ) x = minTo;
418 #include "QY2DiskUsageList.moc"