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 QStringList colors = _backgroundColors.split(
",");
196 if ( colors.size() > 0 )
198 color = colors[index % colors.size()];
199 if ( color.isValid() )
200 return YColor( color.red(), color.green(), color.blue() );
206 case 0:
return YColor( 0, 0, 128 );
207 case 1:
return YColor( 64, 200, 255 );
208 case 2:
return YColor( 255, 255, 255 );
209 case 3:
return YColor( 0, 153, 153 );
210 case 4:
return YColor( 150, 255, 255 );
211 case 5:
return YColor( 100, 100, 100 );
212 case 6:
return YColor( 0, 200, 100 );
213 case 7:
return YColor( 0, 100, 76 );
216 return YColor( 255, 255, 255 );
226 QStringList colors = _foregroundColors.split(
",");
228 if ( colors.size() > 0 )
230 color = colors[index % colors.size()];
231 if (color.isValid() )
232 return YColor( color.red(), color.green(), color.blue() );
236 YColor black = YColor( 0, 0, 0 );
237 YColor white = YColor( 255, 255, 255 );
241 case 0:
return white;
242 case 1:
return black;
243 case 2:
return black;
244 case 3:
return black;
245 case 4:
return black;
246 case 5:
return white;
247 case 6:
return black;
248 case 7:
return white;
258 QFrame::setEnabled( enabled );
259 YWidget::setEnabled( enabled );
267 QFontMetrics metrics = fontMetrics();
269 for (
int i=0; i < segments(); i++ )
271 QString txt = fromUTF8( segment(i).label() );
273 if ( txt.contains(
"%1" ) )
274 txt = txt.arg( segment(i).value() );
276 QSize segSize = metrics.size( 0, txt );
277 width += segSize.width();
280 width += 2 * YQBarGraphLabelHorizontalMargin;
281 width += frameWidth();
282 width += 2 * YQBarGraphOuterMargin;
283 width = max( width, YQBarGraphMinWidth );
292 int height = YQBarGraphMinHeight;
293 QFontMetrics metrics = fontMetrics();
295 for (
int i=0; i < segments(); i++ )
297 QString txt = fromUTF8( segment(i).label() );
299 if ( txt.contains(
"%1" ) )
300 txt = txt.arg( segment(i).value() );
302 QSize segSize = metrics.size( 0, txt );
303 height = max( height, segSize.height() );
306 height += 2 * YQBarGraphLabelVerticalMargin;
307 height += frameWidth();
308 height += 2 * YQBarGraphOuterMargin;
309 height = max( height, YQBarGraphMinHeight );
318 resize( newWidth, newHeight );
321 QString YQBarGraph::getBackgroundColors()
323 return _backgroundColors;
326 void YQBarGraph::setBackgroundColors( QString colors )
328 _backgroundColors = colors;
331 QString YQBarGraph::getForegroundColors()
333 return _foregroundColors;
336 void YQBarGraph::setForegroundColors( QString colors )
338 _foregroundColors = colors;
343 #include "YQBarGraph.moc" YColor defaultSegmentColor(unsigned index)
Return one from a set of default segment background colors.
YQBarGraph(YWidget *parent)
Constructor.
virtual ~YQBarGraph()
Destructor.
virtual int preferredWidth()
Preferred width of the widget.
YColor defaultTextColor(unsigned index)
Return one from a set of default text colors.
virtual void doUpdate()
Perform a visual update on the screen.
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
virtual void paintEvent(QPaintEvent *painter)
Draw the contents.
virtual int preferredHeight()
Preferred height of the widget.
virtual void setEnabled(bool enabled)
Set enabled/disabled state.