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.
24 #include "QY2DiskUsageList.h"
29 QY2DiskUsageList::QY2DiskUsageList( QWidget * parent, bool addStdColumns )
30 : QY2ListView( parent )
33 _percentageBarCol = -42;
40 QStringList columnLabels;
44 columnLabels << _( "Name" ); _nameCol = numCol++;
45 // Translators: Please keep this short!
46 columnLabels << _("Disk Usage"); _percentageBarCol = numCol++;
47 columnLabels << ""; _percentageCol = numCol++;
48 columnLabels << _("Used"); _usedSizeCol = numCol++;
49 columnLabels << _( "Free"); _freeSizeCol = numCol++;
50 columnLabels << _("Total"); _totalSizeCol = numCol++;
52 addColumn( _( "Device" ) ); _deviceNameCol = numCol++;
55 setColumnCount(numCol);
56 setHeaderLabels(columnLabels);
59 // setTextAlignment( percentageCol(), Qt::AlignRight );
60 // setTextAlignment( usedSizeCol(), Qt::AlignRight );
61 // setTextAlignment( freeSizeCol(), Qt::AlignRight );
62 // setTextAlignment( totalSizeCol(), Qt::AlignRight );
63 sortItems( percentageBarCol(), Qt::AscendingOrder );
67 setSelectionMode(QAbstractItemView::NoSelection);
71 QY2DiskUsageList::~QY2DiskUsageList()
80 QY2DiskUsageListItem::QY2DiskUsageListItem( QY2DiskUsageList * parent )
81 : QY2ListViewItem( parent )
82 , _diskUsageList( parent )
89 QY2DiskUsageListItem::~QY2DiskUsageListItem()
98 QY2DiskUsageListItem::init( bool allFields )
100 if ( percentageCol() >= 0 )
102 QString percentageText;
103 percentageText.sprintf( "%d%%", usedPercent() );
104 setText( percentageCol(), percentageText );
107 if ( usedSizeCol() >= 0 ) setText( usedSizeCol(), usedSize() );
108 if ( freeSizeCol() >= 0 ) setText( freeSizeCol(), freeSize() );
112 if ( totalSizeCol() >= 0 ) setText( totalSizeCol(), totalSize() );
113 if ( nameCol() >= 0 ) setText( nameCol(), " " + name() );
114 if ( deviceNameCol() >= 0 ) setText( deviceNameCol(), deviceName() );
120 QY2DiskUsageListItem::setText( int column, const FSize & size )
122 QString sizeText = size.form( 0, 1, true ).c_str();
124 setText( column, sizeText );
129 QY2DiskUsageListItem::freeSize() const
131 return totalSize() - usedSize();
136 QY2DiskUsageListItem::usedPercent() const
140 if ( totalSize() != 0 )
141 percent = ( 100 * usedSize() ) / totalSize();
148 QY2DiskUsageListItem::updateStatus()
155 QY2DiskUsageListItem::updateData()
165 * Comparison function used for sorting the list.
172 QY2DiskUsageListItem::compare( QTreeWidgetItem * otherListViewItem,
174 bool ascending ) const
176 QY2DiskUsageListItem * other = dynamic_cast<QY2DiskUsageListItem *> (otherListViewItem);
180 if ( col == percentageCol() ||
181 col == percentageBarCol() )
183 // Intentionally reverting sort order: Fullest first
185 if ( this->usedPercent() < other->usedPercent() ) return 1;
186 if ( this->usedPercent() > other->usedPercent() ) return -1;
189 else if ( col == usedSizeCol() )
191 if ( this->usedSize() < other->usedSize() ) return -1;
192 if ( this->usedSize() > other->usedSize() ) return 1;
195 else if ( col == freeSizeCol() )
197 if ( this->freeSize() < other->freeSize() ) return -1;
198 if ( this->freeSize() > other->freeSize() ) return 1;
201 else if ( col == totalSizeCol() )
203 if ( this->totalSize() < other->totalSize() ) return -1;
204 if ( this->totalSize() > other->totalSize() ) return 1;
209 return QY2ListViewItem::compare( otherListViewItem, col, ascending );
214 QY2DiskUsageListItem::paintCell( QPainter * painter,
215 const QColorGroup & colorGroup,
220 if ( column == percentageBarCol() )
222 QColor background = colorGroup.base();
223 painter->setBackgroundColor( background );
225 paintPercentageBar( usedPercent(),
227 _diskUsageList->treeStepSize() * depth(),
229 interpolateColor( usedPercent(),
231 QColor( 0, 0x80, 0 ), // Medium dark green
232 QColor( 0xFF, 0, 0 ) ), // Bright red
233 background.dark( 115 ) );
238 QColorGroup cg( colorGroup );
240 if ( usedSize() > totalSize() )
241 cg.setColor( QColorGroup::Text, Qt::red ); // Set red text foreground
243 // Intentionally bypassing the direct parent class method, use the grandparent's:
244 // Don't let QY2ListViewItem::_textColor / _backgroundColor interfere with our colors.
246 Q3ListViewItem::paintCell( painter, cg, column, width, alignment );
252 * Stolen from KDirStat::KDirTreeView with the author's permission.
255 QY2DiskUsageListItem::paintPercentageBar( float percent,
259 const QColor & fillColor,
260 const QColor & barBackground )
262 // if ( percent > 100.0 ) percent = 100.0;
263 // if ( percent < 0.0 ) percent = 0.0;
265 // int extraMargin = 3;
266 // int x = 0; /*FIXME _diskUsageList->itemMargin(); */
267 // int y = extraMargin;
268 // int w = width - 2; /*FIXME * _diskUsageList->horizontalOffset(); */
269 // int h = height() - 2; /*FIXME * extraMargin; */
272 // painter->eraseRect( 0, 0, width, height() );
278 // QPen pen( painter->pen() );
280 // painter->setPen( pen );
281 // painter->setBrush( Qt::NoBrush );
282 // fillWidth = (int) ( ( w - 2 * penWidth ) * percent / 100.0 );
285 // // Fill bar background.
287 // painter->fillRect( x + penWidth, y + penWidth,
288 // w - 2 * penWidth + 1, h - 2 * penWidth + 1,
291 // * Notice: The Xlib XDrawRectangle() function always fills one
292 // * pixel less than specified. Altough this is very likely just a
293 // * plain old bug, it is documented that way. Obviously, Qt just
294 // * maps the fillRect() call directly to XDrawRectangle() so they
295 // * inherited that bug ( although the Qt doc stays silent about
296 // * it ). So it is really necessary to compensate for that missing
297 // * pixel in each dimension.
299 // * If you don't believe it, see for yourself.
300 // * Hint: Try the xmag program to zoom into the drawn pixels.
303 // // Fill the desired percentage.
305 // painter->fillRect( x + penWidth, y + penWidth,
306 // fillWidth+1, h - 2 * penWidth+1,
310 // // Draw 3D shadows.
312 // pen.setColor( contrastingColor ( Qt::black,
313 // painter->backgroundColor() ) );
314 // painter->setPen( pen );
315 // painter->drawLine( x, y, x+w, y );
316 // painter->drawLine( x, y, x, y+h );
318 // pen.setColor( contrastingColor( barBackground.dark(),
319 // painter->backgroundColor() ) );
320 // painter->setPen( pen );
321 // painter->drawLine( x+1, y+1, x+w-1, y+1 );
322 // painter->drawLine( x+1, y+1, x+1, y+h-1 );
324 // pen.setColor( contrastingColor( barBackground.light(),
325 // painter->backgroundColor() ) );
326 // painter->setPen( pen );
327 // painter->drawLine( x+1, y+h, x+w, y+h );
328 // painter->drawLine( x+w, y, x+w, y+h );
330 // pen.setColor( contrastingColor( Qt::white,
331 // painter->backgroundColor() ) );
332 // painter->setPen( pen );
333 // painter->drawLine( x+2, y+h-1, x+w-1, y+h-1 );
334 // painter->drawLine( x+w-1, y+1, x+w-1, y+h-1 );
340 * Stolen from KDirStat::KDirTreeView with the author's permission.
343 QY2DiskUsageListItem::contrastingColor( const QColor & desiredColor,
344 const QColor & contrastColor )
346 if ( desiredColor != contrastColor )
351 if ( contrastColor != contrastColor.light() )
353 // try a little lighter
354 return contrastColor.light();
358 // try a little darker
359 return contrastColor.dark();
365 QY2DiskUsageListItem::interpolateColor( int val,
368 const QColor & minColor,
369 const QColor & maxColor )
375 minColor.getHsv( &minH, &minS, &minV );
376 maxColor.getHsv( &maxH, &maxS, &maxV );
378 return QColor::fromHsv( interpolate( val, minVal, maxVal, minH, maxH ),
379 interpolate( val, minVal, maxVal, minS, maxS ),
380 interpolate( val, minVal, maxVal, minV, maxV ) );
385 QY2DiskUsageListItem::interpolate( int from,
386 int minFrom, int maxFrom,
387 int minTo, int maxTo )
389 if ( minFrom > maxFrom )
391 // Swap min/max values
398 long x = from - minFrom;
400 x /= maxFrom - minFrom;
405 if ( x < minTo ) x = minTo;
406 if ( x > maxTo ) x = maxTo;
410 if ( x < maxTo ) x = maxTo;
411 if ( x > minTo ) x = minTo;
419 #include "QY2DiskUsageList.moc"