00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <qpainter.h>
00019 #include <qwt_plot.h>
00020 #include <qwt_painter.h>
00021 #include <qwt_scale_map.h>
00022
00023 #include "QmitkHistogram.h"
00024
00025 class QmitkHistogram::HistogramData
00026 {
00027 public:
00028 QwtIntervalData data;
00029 QColor color;
00030 double reference;
00031 };
00032
00033 QmitkHistogram::QmitkHistogram(const QwtText &title):
00034 QwtPlotItem(title)
00035 {
00036 init();
00037 }
00038
00039 QmitkHistogram::QmitkHistogram(const QString &title):
00040 QwtPlotItem(QwtText(title))
00041 {
00042 init();
00043 }
00044
00045 QmitkHistogram::~QmitkHistogram()
00046 {
00047 delete m_Data;
00048 }
00049
00050 void QmitkHistogram::init()
00051 {
00052 m_Data = new HistogramData();
00053 m_Data->reference = 0.0;
00054
00055 setItemAttribute(QwtPlotItem::AutoScale, true);
00056 setItemAttribute(QwtPlotItem::Legend, true);
00057
00058 setZ(20.0);
00059 }
00060
00061 void QmitkHistogram::setBaseline(double reference)
00062 {
00063 if ( m_Data->reference != reference )
00064 {
00065 m_Data->reference = reference;
00066 itemChanged();
00067 }
00068 }
00069
00070 double QmitkHistogram::baseline() const
00071 {
00072 return m_Data->reference;
00073 }
00074
00075 void QmitkHistogram::setData(const QwtIntervalData &data)
00076 {
00077 m_Data->data = data;
00078 itemChanged();
00079 }
00080
00081 const QwtIntervalData &QmitkHistogram::data() const
00082 {
00083 return m_Data->data;
00084 }
00085
00086 void QmitkHistogram::setColor(const QColor &color)
00087 {
00088 if ( m_Data->color != color )
00089 {
00090 m_Data->color = color;
00091 itemChanged();
00092 }
00093 }
00094
00095 QColor QmitkHistogram::color() const
00096 {
00097 return m_Data->color;
00098 }
00099
00100 QwtDoubleRect QmitkHistogram::boundingRect() const
00101 {
00102 QwtDoubleRect rect = m_Data->data.boundingRect();
00103 if ( !rect.isValid() )
00104 return rect;
00105
00106 if ( rect.bottom() < m_Data->reference )
00107 rect.setBottom( m_Data->reference );
00108 else if ( rect.top() > m_Data->reference )
00109 rect.setTop( m_Data->reference );
00110
00111 return rect;
00112 }
00113
00114 void QmitkHistogram::draw(QPainter *painter, const QwtScaleMap &xMap,
00115 const QwtScaleMap &yMap, const QRect &) const
00116 {
00117 const QwtIntervalData &iData = m_Data->data;
00118
00119 painter->setPen(QPen(m_Data->color));
00120
00121
00122 const int y0 = yMap.transform(baseline());
00123
00124 for ( int i = 0; i < (int)iData.size(); i++ )
00125 {
00126 const int y2 = yMap.transform(iData.value(i));
00127 if ( y2 == y0 )
00128 continue;
00129
00130 int x1 = xMap.transform(iData.interval(i).minValue());
00131 int x2 = xMap.transform(iData.interval(i).maxValue());
00132 if ( x1 > x2 )
00133 qSwap(x1, x2);
00134
00135 if ( i < (int)iData.size() - 2 )
00136 {
00137 const int xx1 = xMap.transform(iData.interval(i+1).minValue());
00138 const int xx2 = xMap.transform(iData.interval(i+1).maxValue());
00139
00140 if ( x2 == qwtMin(xx1, xx2) )
00141 {
00142 const int yy2 = yMap.transform(iData.value(i+1));
00143 if ( yy2 != y0 && ( (yy2 < y0 && y2 < y0) ||
00144 (yy2 > y0 && y2 > y0) ) )
00145 {
00146
00147 x2--;
00148 }
00149 }
00150 }
00151 drawBar(painter, Qt::Vertical,
00152 QRect(x1, y0, x2 - x1, y2 - y0) );
00153 }
00154 }
00155
00156 void QmitkHistogram::drawBar(QPainter *painter,
00157 Qt::Orientation, const QRect& rect) const
00158 {
00159 painter->save();
00160
00161 const QColor color(painter->pen().color());
00162 #if QT_VERSION >= 0x040000
00163 const QRect r = rect.normalized();
00164 #else
00165 const QRect r = rect.normalize();
00166 #endif
00167
00168 const int factor = 125;
00169 const QColor light(color.light(factor));
00170 const QColor dark(color.dark(factor));
00171
00172 painter->setBrush(color);
00173 painter->setPen(Qt::NoPen);
00174 QwtPainter::drawRect(painter, r.x() + 1, r.y() + 1,
00175 r.width() - 2, r.height() - 2);
00176 painter->setBrush(Qt::NoBrush);
00177
00178 painter->setPen(QPen(light, 2));
00179 #if QT_VERSION >= 0x040000
00180 QwtPainter::drawLine(painter,
00181 r.left() + 1, r.top() + 2, r.right() + 1, r.top() + 2);
00182 #else
00183 QwtPainter::drawLine(painter,
00184 r.left(), r.top() + 2, r.right() + 1, r.top() + 2);
00185 #endif
00186
00187 painter->setPen(QPen(dark, 2));
00188 #if QT_VERSION >= 0x040000
00189 QwtPainter::drawLine(painter,
00190 r.left() + 1, r.bottom(), r.right() + 1, r.bottom());
00191 #else
00192 QwtPainter::drawLine(painter,
00193 r.left(), r.bottom(), r.right() + 1, r.bottom());
00194 #endif
00195
00196 painter->setPen(QPen(light, 1));
00197
00198 #if QT_VERSION >= 0x040000
00199 QwtPainter::drawLine(painter,
00200 r.left(), r.top() + 1, r.left(), r.bottom());
00201 QwtPainter::drawLine(painter,
00202 r.left() + 1, r.top() + 2, r.left() + 1, r.bottom() - 1);
00203 #else
00204 QwtPainter::drawLine(painter,
00205 r.left(), r.top() + 1, r.left(), r.bottom() + 1);
00206 QwtPainter::drawLine(painter,
00207 r.left() + 1, r.top() + 2, r.left() + 1, r.bottom());
00208 #endif
00209
00210 painter->setPen(QPen(dark, 1));
00211
00212 #if QT_VERSION >= 0x040000
00213 QwtPainter::drawLine(painter,
00214 r.right() + 1, r.top() + 1, r.right() + 1, r.bottom());
00215 QwtPainter::drawLine(painter,
00216 r.right(), r.top() + 2, r.right(), r.bottom() - 1);
00217 #else
00218 QwtPainter::drawLine(painter,
00219 r.right() + 1, r.top() + 1, r.right() + 1, r.bottom() + 1);
00220 QwtPainter::drawLine(painter,
00221 r.right(), r.top() + 2, r.right(), r.bottom());
00222 #endif
00223
00224 painter->restore();
00225 }