]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQBarGraph.cc
restart qt4 porting
[duncan/yast2-qt4.git] / src / YQBarGraph.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQBarGraph.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #define y2log_component "qt-ui"
21 #include <ycp/y2log.h>
22
23 #include <algorithm>
24 #include <qpainter.h>
25 #include <qnamespace.h>
26
27 #include "utf8.h"
28 #include "YQUI.h"
29 #include "YQBarGraph.h"
30
31
32 #define YQBarGraphOuterMargin           YQWidgetMargin
33 #define YQBarGraphLabelHorizontalMargin 1
34 #define YQBarGraphLabelVerticalMargin   2
35 #define YQBarGraphMinWidth              80
36 #define YQBarGraphMinHeight             30
37
38
39
40 YQBarGraph::YQBarGraph( YWidget * parent )
41     : QFrame( (QWidget *) parent->widgetRep() )
42     , YBarGraph( parent )
43 {
44     setWidgetRep( this );
45 }
46
47
48 YQBarGraph::~YQBarGraph()
49 {
50     // NOP
51 }
52
53
54 void
55 YQBarGraph::doUpdate()
56 {
57     QFrame::update(); // triggers drawContents()
58 }
59
60
61 void
62 YQBarGraph::drawContents( QPainter * painter )
63 {
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;
69     int valueTotal      = 0;
70
71     for ( int i=0; i < segments(); i++ )
72         valueTotal += segment(i).value();
73
74     if ( valueTotal == 0 ) // Avoid division by zero
75         return;
76
77     for ( int i=0; i < segments(); i++ )
78     {
79         const YBarGraphSegment & seg = segment(i);
80         int segWidth = ( (long) totalWidth * seg.value() ) / valueTotal;
81
82         if ( i == segments()-1 )
83         {
84             // Compensate for rounding errors:
85             // The last segment gets all leftover pixels from the previous ones.
86
87             segWidth = totalWidth - x_off + YQBarGraphOuterMargin;
88         }
89
90         
91         //
92         // Fill the segment
93         //
94
95         YColor segmentColor = seg.segmentColor();
96         YColor textColor    = seg.textColor();
97
98         if ( segmentColor.isUndefined() || textColor.isUndefined() )
99         {
100             // If any of the colors is undefined, use the next default color
101             // for both so some contrast is ensured.
102             
103             segmentColor = defaultSegmentColor( nextDefaultColor   );
104             textColor    = defaultTextColor   ( nextDefaultColor++ );
105         }
106         
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 );
112
113         
114         //
115         // Draw the label
116         //
117
118         painter->setPen( Qt::SolidLine );
119         painter->setPen( QColor( textColor.red(),
120                                  textColor.green(),
121                                  textColor.blue() ) );
122         QString txt = seg.label();
123
124         if ( txt.contains( "%1" ) )
125             txt = txt.arg( seg.value() );               // substitute variable
126         
127         painter->drawText( x_off + YQBarGraphLabelHorizontalMargin,
128                            y_off + YQBarGraphLabelVerticalMargin,
129                            segWidth  - 2 * YQBarGraphLabelHorizontalMargin + 1,
130                            segHeight - 2 * YQBarGraphLabelVerticalMargin   + 1,
131                            AlignCenter, txt );
132
133         // Prepare for the next segment
134
135         x_off += segWidth;
136     }
137 }
138
139
140 YColor 
141 YQBarGraph::defaultSegmentColor( unsigned index )
142 {
143     switch( index % 8 )
144     {
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
153     }
154
155     return YColor( 255, 255, 255 ); // just to make gcc happy
156 }
157
158
159 YColor
160 YQBarGraph::defaultTextColor( unsigned index )
161 {
162     YColor black = YColor(   0,   0,   0 );
163     YColor white = YColor( 255, 255, 255 );
164     
165     switch( index % 8 )
166     {
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;
175     }
176
177     return black; // just to make gcc happy
178 }
179
180
181 void
182 YQBarGraph::setEnabled( bool enabled )
183 {
184     QFrame::setEnabled( enabled );
185     YWidget::setEnabled( enabled );
186 }
187
188
189 int
190 YQBarGraph::preferredWidth()
191 {
192     int width  = 0;
193     QFontMetrics metrics = fontMetrics();
194
195     for ( int i=0; i < segments(); i++ )
196     {
197         QString txt = segment(i).label();
198         
199         if ( txt.contains( "%1" ) )
200             txt = txt.arg( segment(i).value() );
201         
202         QSize segSize = metrics.size( 0, txt );
203         width += segSize.width();
204     }
205
206     width += 2 * YQBarGraphLabelHorizontalMargin;
207     width += frameWidth();
208     width += 2 * YQBarGraphOuterMargin;
209     width  = max( width, YQBarGraphMinWidth );
210
211     return width;
212 }
213
214
215 int
216 YQBarGraph::preferredHeight()
217 {
218     int height = YQBarGraphMinHeight;
219     QFontMetrics metrics = fontMetrics();
220
221     for ( int i=0; i < segments(); i++ )
222     {
223         QString txt = segment(i).label();
224         
225         if ( txt.contains( "%1" ) )
226             txt = txt.arg( segment(i).value() );
227         
228         QSize segSize = metrics.size( 0, txt );
229         height = max( height, segSize.height() );
230     }
231
232     height += 2 * YQBarGraphLabelVerticalMargin;
233     height += frameWidth();
234     height += 2 * YQBarGraphOuterMargin;
235     height  = max( height, YQBarGraphMinHeight );
236
237     return height;
238 }
239
240
241 void
242 YQBarGraph::setSize( int newWidth, int newHeight )
243 {
244     resize( newWidth, newHeight );
245 }
246
247
248
249 #include "YQBarGraph.moc"