00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "qwt_column_symbol.h"
00011 #include "qwt_math.h"
00012 #include "qwt_painter.h"
00013 #include <qpainter.h>
00014 #include <qpalette.h>
00015
00016 static void qwtDrawBox( QPainter *p, const QRectF &rect,
00017 const QPalette &pal, double lw )
00018 {
00019 if ( lw > 0.0 )
00020 {
00021 if ( rect.width() == 0.0 )
00022 {
00023 p->setPen( pal.dark().color() );
00024 p->drawLine( rect.topLeft(), rect.bottomLeft() );
00025 return;
00026 }
00027
00028 if ( rect.height() == 0.0 )
00029 {
00030 p->setPen( pal.dark().color() );
00031 p->drawLine( rect.topLeft(), rect.topRight() );
00032 return;
00033 }
00034
00035 lw = qMin( lw, rect.height() / 2.0 - 1.0 );
00036 lw = qMin( lw, rect.width() / 2.0 - 1.0 );
00037
00038 const QRectF outerRect = rect.adjusted( 0, 0, 1, 1 );
00039 QPolygonF polygon( outerRect );
00040
00041 if ( outerRect.width() > 2 * lw &&
00042 outerRect.height() > 2 * lw )
00043 {
00044 const QRectF innerRect = outerRect.adjusted( lw, lw, -lw, -lw );
00045 polygon = polygon.subtracted( innerRect );
00046 }
00047
00048 p->setPen( Qt::NoPen );
00049
00050 p->setBrush( pal.dark() );
00051 p->drawPolygon( polygon );
00052 }
00053
00054 const QRectF windowRect = rect.adjusted( lw, lw, -lw + 1, -lw + 1 );
00055 if ( windowRect.isValid() )
00056 p->fillRect( windowRect, pal.window() );
00057 }
00058
00059 static void qwtDrawPanel( QPainter *painter, const QRectF &rect,
00060 const QPalette &pal, double lw )
00061 {
00062 if ( lw > 0.0 )
00063 {
00064 if ( rect.width() == 0.0 )
00065 {
00066 painter->setPen( pal.window().color() );
00067 painter->drawLine( rect.topLeft(), rect.bottomLeft() );
00068 return;
00069 }
00070
00071 if ( rect.height() == 0.0 )
00072 {
00073 painter->setPen( pal.window().color() );
00074 painter->drawLine( rect.topLeft(), rect.topRight() );
00075 return;
00076 }
00077
00078 lw = qMin( lw, rect.height() / 2.0 - 1.0 );
00079 lw = qMin( lw, rect.width() / 2.0 - 1.0 );
00080
00081 const QRectF outerRect = rect.adjusted( 0, 0, 1, 1 );
00082 const QRectF innerRect = outerRect.adjusted( lw, lw, -lw, -lw );
00083
00084 QPolygonF lines[2];
00085
00086 lines[0] += outerRect.bottomLeft();
00087 lines[0] += outerRect.topLeft();
00088 lines[0] += outerRect.topRight();
00089 lines[0] += innerRect.topRight();
00090 lines[0] += innerRect.topLeft();
00091 lines[0] += innerRect.bottomLeft();
00092
00093 lines[1] += outerRect.topRight();
00094 lines[1] += outerRect.bottomRight();
00095 lines[1] += outerRect.bottomLeft();
00096 lines[1] += innerRect.bottomLeft();
00097 lines[1] += innerRect.bottomRight();
00098 lines[1] += innerRect.topRight();
00099
00100 painter->setPen( Qt::NoPen );
00101
00102 painter->setBrush( pal.light() );
00103 painter->drawPolygon( lines[0] );
00104 painter->setBrush( pal.dark() );
00105 painter->drawPolygon( lines[1] );
00106 }
00107
00108 painter->fillRect( rect.adjusted( lw, lw, -lw + 1, -lw + 1 ), pal.window() );
00109 }
00110
00111 class QwtColumnSymbol::PrivateData
00112 {
00113 public:
00114 PrivateData():
00115 style( QwtColumnSymbol::Box ),
00116 frameStyle( QwtColumnSymbol::Raised ),
00117 palette( Qt::gray ),
00118 lineWidth( 2 )
00119 {
00120 }
00121
00122 QwtColumnSymbol::Style style;
00123 QwtColumnSymbol::FrameStyle frameStyle;
00124
00125 QPalette palette;
00126 int lineWidth;
00127 };
00128
00135 QwtColumnSymbol::QwtColumnSymbol( Style style )
00136 {
00137 d_data = new PrivateData();
00138 d_data->style = style;
00139 }
00140
00142 QwtColumnSymbol::~QwtColumnSymbol()
00143 {
00144 delete d_data;
00145 }
00146
00153 void QwtColumnSymbol::setStyle( Style style )
00154 {
00155 d_data->style = style;
00156 }
00157
00162 QwtColumnSymbol::Style QwtColumnSymbol::style() const
00163 {
00164 return d_data->style;
00165 }
00166
00173 void QwtColumnSymbol::setPalette( const QPalette &palette )
00174 {
00175 d_data->palette = palette;
00176 }
00177
00182 const QPalette& QwtColumnSymbol::palette() const
00183 {
00184 return d_data->palette;
00185 }
00186
00193 void QwtColumnSymbol::setFrameStyle( FrameStyle frameStyle )
00194 {
00195 d_data->frameStyle = frameStyle;
00196 }
00197
00202 QwtColumnSymbol::FrameStyle QwtColumnSymbol::frameStyle() const
00203 {
00204 return d_data->frameStyle;
00205 }
00206
00213 void QwtColumnSymbol::setLineWidth( int width )
00214 {
00215 if ( width < 0 )
00216 width = 0;
00217
00218 d_data->lineWidth = width;
00219 }
00220
00225 int QwtColumnSymbol::lineWidth() const
00226 {
00227 return d_data->lineWidth;
00228 }
00229
00238 void QwtColumnSymbol::draw( QPainter *painter,
00239 const QwtColumnRect &rect ) const
00240 {
00241 painter->save();
00242
00243 switch ( d_data->style )
00244 {
00245 case QwtColumnSymbol::Box:
00246 {
00247 drawBox( painter, rect );
00248 break;
00249 }
00250 default:;
00251 }
00252
00253 painter->restore();
00254 }
00255
00264 void QwtColumnSymbol::drawBox( QPainter *painter,
00265 const QwtColumnRect &rect ) const
00266 {
00267 QRectF r = rect.toRect();
00268 if ( QwtPainter::roundingAlignment( painter ) )
00269 {
00270 r.setLeft( qRound( r.left() ) );
00271 r.setRight( qRound( r.right() ) );
00272 r.setTop( qRound( r.top() ) );
00273 r.setBottom( qRound( r.bottom() ) );
00274 }
00275
00276 switch ( d_data->frameStyle )
00277 {
00278 case QwtColumnSymbol::Raised:
00279 {
00280 qwtDrawPanel( painter, r, d_data->palette, d_data->lineWidth );
00281 break;
00282 }
00283 case QwtColumnSymbol::Plain:
00284 {
00285 qwtDrawBox( painter, r, d_data->palette, d_data->lineWidth );
00286 break;
00287 }
00288 default:
00289 {
00290 painter->fillRect( r, d_data->palette.window() );
00291 }
00292 }
00293 }