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::paintEvent( QPaintEvent* paintEvent )
64 QFrame::paintEvent( paintEvent );
66 QPainter painter( this );
68 unsigned nextDefaultColor = 0;
69 int totalWidth = contentsRect().width() - 2*YQBarGraphOuterMargin;
70 int segHeight = contentsRect().height() - 2*YQBarGraphOuterMargin;
71 int x_off = YQBarGraphOuterMargin;
72 int y_off = YQBarGraphOuterMargin;
75 for ( int i=0; i < segments(); i++ )
76 valueTotal += segment(i).value();
78 if ( valueTotal == 0 ) // Avoid division by zero
81 for ( int i=0; i < segments(); i++ )
83 const YBarGraphSegment & seg = segment(i);
84 int segWidth = ( (long) totalWidth * seg.value() ) / valueTotal;
86 if ( i == segments()-1 )
88 // Compensate for rounding errors:
89 // The last segment gets all leftover pixels from the previous ones.
91 segWidth = totalWidth - x_off + YQBarGraphOuterMargin;
99 YColor segmentColor = seg.segmentColor();
100 YColor textColor = seg.textColor();
102 if ( segmentColor.isUndefined() || textColor.isUndefined() )
104 // If any of the colors is undefined, use the next default color
105 // for both so some contrast is ensured.
107 segmentColor = defaultSegmentColor( nextDefaultColor );
108 textColor = defaultTextColor ( nextDefaultColor++ );
111 painter.setBrush( QColor( segmentColor.red(),
112 segmentColor.green(),
113 segmentColor.blue() ) );
114 painter.setPen( Qt::NoPen );
115 painter.drawRect( x_off, y_off, segWidth+2, segHeight+2 );
122 painter.setPen( Qt::SolidLine );
123 painter.setPen( QColor( textColor.red(),
125 textColor.blue() ) );
127 QString txt = QString::fromStdString( seg.label() );
129 if ( txt.contains( "%1" ) )
130 txt = txt.arg( seg.value() ); // substitute variable
132 painter.drawText( x_off + YQBarGraphLabelHorizontalMargin,
133 y_off + YQBarGraphLabelVerticalMargin,
134 segWidth - 2 * YQBarGraphLabelHorizontalMargin + 1,
135 segHeight - 2 * YQBarGraphLabelVerticalMargin + 1,
136 Qt::AlignCenter, txt );
138 // Prepare for the next segment
146 YQBarGraph::defaultSegmentColor( unsigned index )
150 case 0: return YColor( 0, 0, 128 ); // dark blue
151 case 1: return YColor( 64, 200, 255 ); // medium blue
152 case 2: return YColor( 255, 255, 255 ); // white
153 case 3: return YColor( 0, 153, 153 ); // cadet blue
154 case 4: return YColor( 150, 255, 255 ); // cyan
155 case 5: return YColor( 100, 100, 100 ); // medium grey
156 case 6: return YColor( 0, 200, 100 ); // medium green
157 case 7: return YColor( 0, 100, 76 ); // dark green
160 return YColor( 255, 255, 255 ); // just to make gcc happy
165 YQBarGraph::defaultTextColor( unsigned index )
167 YColor black = YColor( 0, 0, 0 );
168 YColor white = YColor( 255, 255, 255 );
172 case 0: return white;
173 case 1: return black;
174 case 2: return black;
175 case 3: return black;
176 case 4: return black;
177 case 5: return white;
178 case 6: return black;
179 case 7: return white;
182 return black; // just to make gcc happy
187 YQBarGraph::setEnabled( bool enabled )
189 QFrame::setEnabled( enabled );
190 YWidget::setEnabled( enabled );
195 YQBarGraph::preferredWidth()
198 QFontMetrics metrics = fontMetrics();
200 for ( int i=0; i < segments(); i++ )
202 QString txt = QString::fromStdString( segment(i).label() );
204 if ( txt.contains( "%1" ) )
205 txt = txt.arg( segment(i).value() );
207 QSize segSize = metrics.size( 0, txt );
208 width += segSize.width();
211 width += 2 * YQBarGraphLabelHorizontalMargin;
212 width += frameWidth();
213 width += 2 * YQBarGraphOuterMargin;
214 width = max( width, YQBarGraphMinWidth );
221 YQBarGraph::preferredHeight()
223 int height = YQBarGraphMinHeight;
224 QFontMetrics metrics = fontMetrics();
226 for ( int i=0; i < segments(); i++ )
228 QString txt = QString::fromStdString( segment(i).label() );
230 if ( txt.contains( "%1" ) )
231 txt = txt.arg( segment(i).value() );
233 QSize segSize = metrics.size( 0, txt );
234 height = max( height, segSize.height() );
237 height += 2 * YQBarGraphLabelVerticalMargin;
238 height += frameWidth();
239 height += 2 * YQBarGraphOuterMargin;
240 height = max( height, YQBarGraphMinHeight );
247 YQBarGraph::setSize( int newWidth, int newHeight )
249 resize( newWidth, newHeight );
254 #include "YQBarGraph.moc"