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() ) );
123 QString txt = QString::fromStdString( seg.label() );
125 if ( txt.contains( "%1" ) )
126 txt = txt.arg( seg.value() ); // substitute variable
128 painter->drawText( x_off + YQBarGraphLabelHorizontalMargin,
129 y_off + YQBarGraphLabelVerticalMargin,
130 segWidth - 2 * YQBarGraphLabelHorizontalMargin + 1,
131 segHeight - 2 * YQBarGraphLabelVerticalMargin + 1,
132 Qt::AlignCenter, txt );
134 // Prepare for the next segment
142 YQBarGraph::defaultSegmentColor( unsigned index )
146 case 0: return YColor( 0, 0, 128 ); // dark blue
147 case 1: return YColor( 64, 200, 255 ); // medium blue
148 case 2: return YColor( 255, 255, 255 ); // white
149 case 3: return YColor( 0, 153, 153 ); // cadet blue
150 case 4: return YColor( 150, 255, 255 ); // cyan
151 case 5: return YColor( 100, 100, 100 ); // medium grey
152 case 6: return YColor( 0, 200, 100 ); // medium green
153 case 7: return YColor( 0, 100, 76 ); // dark green
156 return YColor( 255, 255, 255 ); // just to make gcc happy
161 YQBarGraph::defaultTextColor( unsigned index )
163 YColor black = YColor( 0, 0, 0 );
164 YColor white = YColor( 255, 255, 255 );
168 case 0: return white;
169 case 1: return black;
170 case 2: return black;
171 case 3: return black;
172 case 4: return black;
173 case 5: return white;
174 case 6: return black;
175 case 7: return white;
178 return black; // just to make gcc happy
183 YQBarGraph::setEnabled( bool enabled )
185 QFrame::setEnabled( enabled );
186 YWidget::setEnabled( enabled );
191 YQBarGraph::preferredWidth()
194 QFontMetrics metrics = fontMetrics();
196 for ( int i=0; i < segments(); i++ )
198 QString txt = QString::fromStdString( segment(i).label() );
200 if ( txt.contains( "%1" ) )
201 txt = txt.arg( segment(i).value() );
203 QSize segSize = metrics.size( 0, txt );
204 width += segSize.width();
207 width += 2 * YQBarGraphLabelHorizontalMargin;
208 width += frameWidth();
209 width += 2 * YQBarGraphOuterMargin;
210 width = max( width, YQBarGraphMinWidth );
217 YQBarGraph::preferredHeight()
219 int height = YQBarGraphMinHeight;
220 QFontMetrics metrics = fontMetrics();
222 for ( int i=0; i < segments(); i++ )
224 QString txt = QString::fromStdString( segment(i).label() );
226 if ( txt.contains( "%1" ) )
227 txt = txt.arg( segment(i).value() );
229 QSize segSize = metrics.size( 0, txt );
230 height = max( height, segSize.height() );
233 height += 2 * YQBarGraphLabelVerticalMargin;
234 height += frameWidth();
235 height += 2 * YQBarGraphOuterMargin;
236 height = max( height, YQBarGraphMinHeight );
243 YQBarGraph::setSize( int newWidth, int newHeight )
245 resize( newWidth, newHeight );
250 #include "YQBarGraph.moc"