1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
15 Author: Stefan Hundhammer <sh@suse.de>
20 #define y2log_component "qt-ui"
21 #include <ycp/y2log.h>
25 #include <qnamespace.h>
29 #include "YQBarGraph.h"
32 #define YQBarGraphOuterMargin YQWidgetMargin
33 #define YQBarGraphLabelHorizontalMargin 1
34 #define YQBarGraphLabelVerticalMargin 2
35 #define YQBarGraphMinWidth 80
36 #define YQBarGraphMinHeight 30
40 YQBarGraph::YQBarGraph( YWidget * parent )
41 : QFrame( (QWidget *) parent->widgetRep() )
48 YQBarGraph::~YQBarGraph()
55 YQBarGraph::doUpdate()
57 QFrame::update(); // triggers drawContents()
62 YQBarGraph::drawContents( QPainter * painter )
64 unsigned nextDefaultColor = 0;
65 int totalWidth = contentsRect().width() - 2*YQBarGraphOuterMargin;
66 int segHeight = contentsRect().height() - 2*YQBarGraphOuterMargin;
67 int x_off = YQBarGraphOuterMargin;
68 int y_off = YQBarGraphOuterMargin;
71 for ( int i=0; i < segments(); i++ )
72 valueTotal += segment(i).value();
74 if ( valueTotal == 0 ) // Avoid division by zero
77 for ( int i=0; i < segments(); i++ )
79 const YBarGraphSegment & seg = segment(i);
80 int segWidth = ( (long) totalWidth * seg.value() ) / valueTotal;
82 if ( i == segments()-1 )
84 // Compensate for rounding errors:
85 // The last segment gets all leftover pixels from the previous ones.
87 segWidth = totalWidth - x_off + YQBarGraphOuterMargin;
95 YColor segmentColor = seg.segmentColor();
96 YColor textColor = seg.textColor();
98 if ( segmentColor.isUndefined() || textColor.isUndefined() )
100 // If any of the colors is undefined, use the next default color
101 // for both so some contrast is ensured.
103 segmentColor = defaultSegmentColor( nextDefaultColor );
104 textColor = defaultTextColor ( nextDefaultColor++ );
107 painter->setBrush( QColor( segmentColor.red(),
108 segmentColor.green(),
109 segmentColor.blue() ) );
110 painter->setPen( Qt::NoPen );
111 painter->drawRect( x_off, y_off, segWidth+2, segHeight+2 );
118 painter->setPen( Qt::SolidLine );
119 painter->setPen( QColor( textColor.red(),
121 textColor.blue() ) );
122 QString txt = seg.label();
124 if ( txt.contains( "%1" ) )
125 txt = txt.arg( seg.value() ); // substitute variable
127 painter->drawText( x_off + YQBarGraphLabelHorizontalMargin,
128 y_off + YQBarGraphLabelVerticalMargin,
129 segWidth - 2 * YQBarGraphLabelHorizontalMargin + 1,
130 segHeight - 2 * YQBarGraphLabelVerticalMargin + 1,
133 // Prepare for the next segment
141 YQBarGraph::defaultSegmentColor( unsigned index )
145 case 0: return YColor( 0, 0, 128 ); // dark blue
146 case 1: return YColor( 64, 200, 255 ); // medium blue
147 case 2: return YColor( 255, 255, 255 ); // white
148 case 3: return YColor( 0, 153, 153 ); // cadet blue
149 case 4: return YColor( 150, 255, 255 ); // cyan
150 case 5: return YColor( 100, 100, 100 ); // medium grey
151 case 6: return YColor( 0, 200, 100 ); // medium green
152 case 7: return YColor( 0, 100, 76 ); // dark green
155 return YColor( 255, 255, 255 ); // just to make gcc happy
160 YQBarGraph::defaultTextColor( unsigned index )
162 YColor black = YColor( 0, 0, 0 );
163 YColor white = YColor( 255, 255, 255 );
167 case 0: return white;
168 case 1: return black;
169 case 2: return black;
170 case 3: return black;
171 case 4: return black;
172 case 5: return white;
173 case 6: return black;
174 case 7: return white;
177 return black; // just to make gcc happy
182 YQBarGraph::setEnabled( bool enabled )
184 QFrame::setEnabled( enabled );
185 YWidget::setEnabled( enabled );
190 YQBarGraph::preferredWidth()
193 QFontMetrics metrics = fontMetrics();
195 for ( int i=0; i < segments(); i++ )
197 QString txt = segment(i).label();
199 if ( txt.contains( "%1" ) )
200 txt = txt.arg( segment(i).value() );
202 QSize segSize = metrics.size( 0, txt );
203 width += segSize.width();
206 width += 2 * YQBarGraphLabelHorizontalMargin;
207 width += frameWidth();
208 width += 2 * YQBarGraphOuterMargin;
209 width = max( width, YQBarGraphMinWidth );
216 YQBarGraph::preferredHeight()
218 int height = YQBarGraphMinHeight;
219 QFontMetrics metrics = fontMetrics();
221 for ( int i=0; i < segments(); i++ )
223 QString txt = segment(i).label();
225 if ( txt.contains( "%1" ) )
226 txt = txt.arg( segment(i).value() );
228 QSize segSize = metrics.size( 0, txt );
229 height = max( height, segSize.height() );
232 height += 2 * YQBarGraphLabelVerticalMargin;
233 height += frameWidth();
234 height += 2 * YQBarGraphOuterMargin;
235 height = max( height, YQBarGraphMinHeight );
242 YQBarGraph::setSize( int newWidth, int newHeight )
244 resize( newWidth, newHeight );
249 #include "YQBarGraph.moc"