26 #define YUILogComponent "qt-ui"
27 #include <yui/YUILog.h>
31 #include <qnamespace.h>
35 #include "YQBarGraph.h"
38 #define YQBarGraphOuterMargin YQWidgetMargin
39 #define YQBarGraphLabelHorizontalMargin 1
40 #define YQBarGraphLabelVerticalMargin 2
41 #define YQBarGraphMinWidth 80
42 #define YQBarGraphMinHeight 30
48 inline bool in_segment (pair <int, QString> seg,
int cmp)
50 return seg.first < cmp;
54 : QFrame( (QWidget *) parent->widgetRep() )
74 YQBarGraph::event ( QEvent *event)
76 if (event->type() == QEvent::ToolTip) {
77 QHelpEvent *helpEvent =
static_cast<QHelpEvent *
>(event);
82 map<int, QString>::reverse_iterator lbound =
83 find_if( toolTips.rbegin(), toolTips.rend(), bind2nd( ptr_fun(in_segment), helpEvent->x()));
85 if (lbound != toolTips.rend())
86 QToolTip::showText(helpEvent->globalPos(), lbound->second );
88 return QWidget::event(event);
95 QFrame::paintEvent( paintEvent );
97 QPainter painter(
this );
99 unsigned nextDefaultColor = 0;
100 int totalWidth = contentsRect().width() - 2*YQBarGraphOuterMargin;
101 int segHeight = contentsRect().height() - 2*YQBarGraphOuterMargin;
102 int x_off = YQBarGraphOuterMargin;
103 int y_off = YQBarGraphOuterMargin;
105 QFontMetrics fm = painter.fontMetrics();
109 for (
int i=0; i < segments(); i++ )
110 valueTotal += segment(i).value();
112 if ( valueTotal == 0 )
115 for (
int i=0; i < segments(); i++ )
117 const YBarGraphSegment & seg = segment(i);
118 int segWidth = ( (long) totalWidth * seg.value() ) / valueTotal;
121 if ( i == segments()-1 )
126 segWidth = totalWidth - x_off + YQBarGraphOuterMargin;
134 YColor segmentColor = seg.segmentColor();
135 YColor textColor = seg.textColor();
137 if ( segmentColor.isUndefined() || textColor.isUndefined() )
146 painter.setBrush( QColor( segmentColor.red(),
147 segmentColor.green(),
148 segmentColor.blue() ) );
149 painter.setPen( Qt::NoPen );
150 painter.drawRect( x_off, y_off, segWidth+2, segHeight+2 );
157 painter.setPen( Qt::SolidLine );
158 painter.setPen( QColor( textColor.red(),
160 textColor.blue() ) );
162 QString txt = fromUTF8( seg.label() );
164 if ( txt.contains(
"%1" ) )
165 txt = txt.arg( seg.value() );
167 stringWidth = fm.size(0,txt).width();
170 if (stringWidth < segWidth)
172 painter.drawText( x_off + YQBarGraphLabelHorizontalMargin,
173 y_off + YQBarGraphLabelVerticalMargin,
174 segWidth - 2 * YQBarGraphLabelHorizontalMargin + 1,
175 segHeight - 2 * YQBarGraphLabelVerticalMargin + 1,
176 Qt::AlignCenter, txt );
180 toolTips.insert(make_pair( x_off, txt));
194 case 0:
return YColor( 0, 0, 128 );
195 case 1:
return YColor( 64, 200, 255 );
196 case 2:
return YColor( 255, 255, 255 );
197 case 3:
return YColor( 0, 153, 153 );
198 case 4:
return YColor( 150, 255, 255 );
199 case 5:
return YColor( 100, 100, 100 );
200 case 6:
return YColor( 0, 200, 100 );
201 case 7:
return YColor( 0, 100, 76 );
204 return YColor( 255, 255, 255 );
211 YColor black = YColor( 0, 0, 0 );
212 YColor white = YColor( 255, 255, 255 );
216 case 0:
return white;
217 case 1:
return black;
218 case 2:
return black;
219 case 3:
return black;
220 case 4:
return black;
221 case 5:
return white;
222 case 6:
return black;
223 case 7:
return white;
233 QFrame::setEnabled( enabled );
234 YWidget::setEnabled( enabled );
242 QFontMetrics metrics = fontMetrics();
244 for (
int i=0; i < segments(); i++ )
246 QString txt = fromUTF8( segment(i).label() );
248 if ( txt.contains(
"%1" ) )
249 txt = txt.arg( segment(i).value() );
251 QSize segSize = metrics.size( 0, txt );
252 width += segSize.width();
255 width += 2 * YQBarGraphLabelHorizontalMargin;
256 width += frameWidth();
257 width += 2 * YQBarGraphOuterMargin;
258 width = max( width, YQBarGraphMinWidth );
267 int height = YQBarGraphMinHeight;
268 QFontMetrics metrics = fontMetrics();
270 for (
int i=0; i < segments(); i++ )
272 QString txt = fromUTF8( segment(i).label() );
274 if ( txt.contains(
"%1" ) )
275 txt = txt.arg( segment(i).value() );
277 QSize segSize = metrics.size( 0, txt );
278 height = max( height, segSize.height() );
281 height += 2 * YQBarGraphLabelVerticalMargin;
282 height += frameWidth();
283 height += 2 * YQBarGraphOuterMargin;
284 height = max( height, YQBarGraphMinHeight );
293 resize( newWidth, newHeight );
298 #include "YQBarGraph.moc"
YColor defaultSegmentColor(unsigned index)
YQBarGraph(YWidget *parent)
virtual int preferredWidth()
YColor defaultTextColor(unsigned index)
virtual void setSize(int newWidth, int newHeight)
virtual void paintEvent(QPaintEvent *painter)
virtual int preferredHeight()
virtual void setEnabled(bool enabled)