10 #ifndef HISTOGRAM_ITEM_H
11 #define HISTOGRAM_ITEM_H
16 #include "qwt_plot_item.h"
18 class QwtIntervalData;
24 explicit HistogramItem(
const QString &title = QString::null );
29 const QwtIntervalData &
data()
const;
36 virtual int rtti()
const;
38 virtual void draw( QPainter *,
const QwtScaleMap &xMap,
39 const QwtScaleMap &yMap,
const QRect & )
const;
56 virtual void drawBar( QPainter *,
57 Qt::Orientation o,
const QRect & )
const;
69 #include <qwt_interval_data.h>
70 #include <qwt_painter.h>
71 #include <qwt_scale_map.h>
72 #include <qwt_legend_item.h>
90 QwtPlotItem( QwtText( title ) )
100 void HistogramItem::init()
102 d_data =
new PrivateData();
106 setItemAttribute( QwtPlotItem::AutoScale,
true );
107 setItemAttribute( QwtPlotItem::Legend,
true );
139 if ( d_data->
color != color )
148 return d_data->
color;
153 QwtDoubleRect rect = d_data->
data.boundingRect();
154 if ( !rect.isValid() )
159 rect = QwtDoubleRect( rect.y(), rect.x(),
160 rect.height(), rect.width() );
164 else if ( rect.right() < d_data->
reference )
171 else if ( rect.top() > d_data->
reference )
181 return QwtPlotItem::Rtti_PlotHistogram;
186 if (
bool( d_data->
attributes & attribute ) == on )
203 const QwtScaleMap &yMap,
const QRect & )
const
205 const QwtIntervalData &iData = d_data->
data;
207 painter->setPen( QPen( d_data->
color ) );
209 const int x0 = xMap.transform(
baseline() );
210 const int y0 = yMap.transform(
baseline() );
212 for (
int i = 0; i < ( int )iData.size(); i++ )
216 const int x2 = xMap.transform( iData.value( i ) );
220 int y1 = yMap.transform( iData.interval( i ).minValue() );
221 int y2 = yMap.transform( iData.interval( i ).maxValue() );
225 if ( i < (
int )iData.size() - 2 )
227 const int yy1 = yMap.transform( iData.interval( i + 1 ).minValue() );
228 const int yy2 = yMap.transform( iData.interval( i + 1 ).maxValue() );
230 if ( y2 == qwtMin( yy1, yy2 ) )
232 const int xx2 = xMap.transform(
233 iData.interval( i + 1 ).minValue() );
234 if ( xx2 != x0 && (( xx2 < x0 && x2 < x0 ) ||
235 ( xx2 > x0 && x2 > x0 ) ) )
243 drawBar( painter, Qt::Horizontal,
244 QRect( x0, y1, x2 - x0, y2 - y1 ) );
248 const int y2 = yMap.transform( iData.value( i ) );
252 int x1 = xMap.transform( iData.interval( i ).minValue() );
253 int x2 = xMap.transform( iData.interval( i ).maxValue() );
257 if ( i < (
int )iData.size() - 2 )
259 const int xx1 = xMap.transform( iData.interval( i + 1 ).minValue() );
260 const int xx2 = xMap.transform( iData.interval( i + 1 ).maxValue() );
262 if ( x2 == qwtMin( xx1, xx2 ) )
264 const int yy2 = yMap.transform( iData.value( i + 1 ) );
265 if ( yy2 != y0 && (( yy2 < y0 && y2 < y0 ) ||
266 ( yy2 > y0 && y2 > y0 ) ) )
273 drawBar( painter, Qt::Vertical,
274 QRect( x1, y0, x2 - x1, y2 - y0 ) );
280 Qt::Orientation,
const QRect& rect )
const
284 const QColor
color( painter->pen().color() );
285 #if QT_VERSION >= 0x040000
286 const QRect r = rect.normalized();
288 const QRect r = rect.normalize();
291 const int factor = 125;
292 const QColor light(
color.light( factor ) );
293 const QColor dark(
color.dark( factor ) );
295 painter->setBrush(
color );
296 painter->setPen( Qt::NoPen );
297 QwtPainter::drawRect( painter, r.x() + 1, r.y() + 1,
298 r.width() - 2, r.height() - 2 );
299 painter->setBrush( Qt::NoBrush );
301 painter->setPen( QPen( light, 2 ) );
302 #if QT_VERSION >= 0x040000
303 QwtPainter::drawLine( painter,
304 r.left() + 1, r.top() + 2, r.right() + 1, r.top() + 2 );
306 QwtPainter::drawLine( painter,
307 r.left(), r.top() + 2, r.right() + 1, r.top() + 2 );
310 painter->setPen( QPen( dark, 2 ) );
311 #if QT_VERSION >= 0x040000
312 QwtPainter::drawLine( painter,
313 r.left() + 1, r.bottom(), r.right() + 1, r.bottom() );
315 QwtPainter::drawLine( painter,
316 r.left(), r.bottom(), r.right() + 1, r.bottom() );
319 painter->setPen( QPen( light, 1 ) );
321 #if QT_VERSION >= 0x040000
322 QwtPainter::drawLine( painter,
323 r.left(), r.top() + 1, r.left(), r.bottom() );
324 QwtPainter::drawLine( painter,
325 r.left() + 1, r.top() + 2, r.left() + 1, r.bottom() - 1 );
327 QwtPainter::drawLine( painter,
328 r.left(), r.top() + 1, r.left(), r.bottom() + 1 );
329 QwtPainter::drawLine( painter,
330 r.left() + 1, r.top() + 2, r.left() + 1, r.bottom() );
333 painter->setPen( QPen( dark, 1 ) );
335 #if QT_VERSION >= 0x040000
336 QwtPainter::drawLine( painter,
337 r.right() + 1, r.top() + 1, r.right() + 1, r.bottom() );
338 QwtPainter::drawLine( painter,
339 r.right(), r.top() + 2, r.right(), r.bottom() - 1 );
341 QwtPainter::drawLine( painter,
342 r.right() + 1, r.top() + 1, r.right() + 1, r.bottom() + 1 );
343 QwtPainter::drawLine( painter,
344 r.right(), r.top() + 2, r.right(), r.bottom() );
359 QWidget *widget = legend->find(
this );
360 if ( !widget || !widget->inherits(
"QwtLegendItem" ) )
363 QwtLegendItem *legendItem = ( QwtLegendItem * )widget;
365 #if QT_VERSION < 0x040000
366 const bool doUpdate = legendItem->isUpdatesEnabled();
368 const bool doUpdate = legendItem->updatesEnabled();
370 legendItem->setUpdatesEnabled(
false );
372 const int policy = legend->displayPolicy();
374 if ( policy == QwtLegend::FixedIdentifier )
376 int mode = legend->identifierMode();
378 legendItem->setCurvePen( QPen(
color() ) );
380 if ( mode & QwtLegendItem::ShowText )
381 legendItem->setText( title() );
383 legendItem->setText( QwtText() );
385 legendItem->setIdentifierMode( mode );
387 else if ( policy == QwtLegend::AutoIdentifier )
391 legendItem->setCurvePen( QPen(
color() ) );
392 mode |= QwtLegendItem::ShowLine;
393 if ( !title().isEmpty() )
395 legendItem->setText( title() );
396 mode |= QwtLegendItem::ShowText;
400 legendItem->setText( QwtText() );
402 legendItem->setIdentifierMode( mode );
405 legendItem->setUpdatesEnabled( doUpdate );
406 legendItem->update();