source: ntrip/trunk/BNC/qwt/qwt_column_symbol.cpp@ 6914

Last change on this file since 6914 was 4271, checked in by mervart, 12 years ago
File size: 6.7 KB
Line 
1/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
2 * Qwt Widget Library
3 * Copyright (C) 1997 Josef Wilgen
4 * Copyright (C) 2002 Uwe Rathmann
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the Qwt License, Version 1.0
8 *****************************************************************************/
9
10#include "qwt_column_symbol.h"
11#include "qwt_math.h"
12#include "qwt_text.h"
13#include "qwt_painter.h"
14#include <qpainter.h>
15#include <qpalette.h>
16
17static void drawBox( QPainter *p, const QRectF &rect,
18 const QPalette &pal, double lw )
19{
20 if ( lw > 0.0 )
21 {
22 if ( rect.width() == 0.0 )
23 {
24 p->setPen( pal.dark().color() );
25 p->drawLine( rect.topLeft(), rect.bottomLeft() );
26 return;
27 }
28
29 if ( rect.height() == 0.0 )
30 {
31 p->setPen( pal.dark().color() );
32 p->drawLine( rect.topLeft(), rect.topRight() );
33 return;
34 }
35
36 lw = qMin( lw, rect.height() / 2.0 - 1.0 );
37 lw = qMin( lw, rect.width() / 2.0 - 1.0 );
38
39 const QRectF outerRect = rect.adjusted( 0, 0, 1, 1 );
40 QPolygonF polygon( outerRect );
41
42 if ( outerRect.width() > 2 * lw &&
43 outerRect.height() > 2 * lw )
44 {
45 const QRectF innerRect = outerRect.adjusted( lw, lw, -lw, -lw );
46 polygon = polygon.subtracted( innerRect );
47 }
48
49 p->setPen( Qt::NoPen );
50
51 p->setBrush( pal.dark() );
52 p->drawPolygon( polygon );
53 }
54
55 const QRectF windowRect = rect.adjusted( lw, lw, -lw + 1, -lw + 1 );
56 if ( windowRect.isValid() )
57 p->fillRect( windowRect, pal.window() );
58}
59
60static void drawPanel( QPainter *painter, const QRectF &rect,
61 const QPalette &pal, double lw )
62{
63 if ( lw > 0.0 )
64 {
65 if ( rect.width() == 0.0 )
66 {
67 painter->setPen( pal.window().color() );
68 painter->drawLine( rect.topLeft(), rect.bottomLeft() );
69 return;
70 }
71
72 if ( rect.height() == 0.0 )
73 {
74 painter->setPen( pal.window().color() );
75 painter->drawLine( rect.topLeft(), rect.topRight() );
76 return;
77 }
78
79 lw = qMin( lw, rect.height() / 2.0 - 1.0 );
80 lw = qMin( lw, rect.width() / 2.0 - 1.0 );
81
82 const QRectF outerRect = rect.adjusted( 0, 0, 1, 1 );
83 const QRectF innerRect = outerRect.adjusted( lw, lw, -lw, -lw );
84
85 QPolygonF lines[2];
86
87 lines[0] += outerRect.bottomLeft();
88 lines[0] += outerRect.topLeft();
89 lines[0] += outerRect.topRight();
90 lines[0] += innerRect.topRight();
91 lines[0] += innerRect.topLeft();
92 lines[0] += innerRect.bottomLeft();
93
94 lines[1] += outerRect.topRight();
95 lines[1] += outerRect.bottomRight();
96 lines[1] += outerRect.bottomLeft();
97 lines[1] += innerRect.bottomLeft();
98 lines[1] += innerRect.bottomRight();
99 lines[1] += innerRect.topRight();
100
101 painter->setPen( Qt::NoPen );
102
103 painter->setBrush( pal.light() );
104 painter->drawPolygon( lines[0] );
105 painter->setBrush( pal.dark() );
106 painter->drawPolygon( lines[1] );
107 }
108
109 painter->fillRect( rect.adjusted( lw, lw, -lw + 1, -lw + 1 ), pal.window() );
110}
111
112class QwtColumnSymbol::PrivateData
113{
114public:
115 PrivateData():
116 style( QwtColumnSymbol::Box ),
117 frameStyle( QwtColumnSymbol::Raised ),
118 lineWidth( 2 )
119 {
120 palette = QPalette( Qt::gray );
121 }
122
123 QwtColumnSymbol::Style style;
124 QwtColumnSymbol::FrameStyle frameStyle;
125
126 QPalette palette;
127 QwtText label;
128
129 int lineWidth;
130};
131
132/*!
133 Constructor
134
135 \param style Style of the symbol
136 \sa setStyle(), style(), Style
137*/
138QwtColumnSymbol::QwtColumnSymbol( Style style )
139{
140 d_data = new PrivateData();
141 d_data->style = style;
142}
143
144//! Destructor
145QwtColumnSymbol::~QwtColumnSymbol()
146{
147 delete d_data;
148}
149
150/*!
151 Specify the symbol style
152
153 \param style Style
154 \sa style(), setPalette()
155*/
156void QwtColumnSymbol::setStyle( Style style )
157{
158 d_data->style = style;
159}
160
161/*!
162 \return Current symbol style
163 \sa setStyle()
164*/
165QwtColumnSymbol::Style QwtColumnSymbol::style() const
166{
167 return d_data->style;
168}
169
170/*!
171 Assign a palette for the symbol
172
173 \param palette Palette
174 \sa palette(), setStyle()
175*/
176void QwtColumnSymbol::setPalette( const QPalette &palette )
177{
178 d_data->palette = palette;
179}
180
181/*!
182 \return Current palette
183 \sa setPalette()
184*/
185const QPalette& QwtColumnSymbol::palette() const
186{
187 return d_data->palette;
188}
189
190/*!
191 Set the frame, that is used for the Box style.
192
193 \param frameStyle Frame style
194 \sa frameStyle(), setLineWidth(), setStyle()
195*/
196void QwtColumnSymbol::setFrameStyle( FrameStyle frameStyle )
197{
198 d_data->frameStyle = frameStyle;
199}
200
201/*!
202 \return Current frame style, that is used for the Box style.
203 \sa setFrameStyle(), lineWidth(), setStyle()
204*/
205QwtColumnSymbol::FrameStyle QwtColumnSymbol::frameStyle() const
206{
207 return d_data->frameStyle;
208}
209
210/*!
211 Set the line width of the frame, that is used for the Box style.
212
213 \param width Width
214 \sa lineWidth(), setFrameStyle()
215*/
216void QwtColumnSymbol::setLineWidth( int width )
217{
218 if ( width < 0 )
219 width = 0;
220
221 d_data->lineWidth = width;
222}
223
224/*!
225 \return Line width of the frame, that is used for the Box style.
226 \sa setLineWidth(), frameStyle(), setStyle()
227*/
228int QwtColumnSymbol::lineWidth() const
229{
230 return d_data->lineWidth;
231}
232
233/*!
234 Draw the symbol depending on its style.
235
236 \param painter Painter
237 \param rect Directed rectangle
238
239 \sa drawBox()
240*/
241void QwtColumnSymbol::draw( QPainter *painter,
242 const QwtColumnRect &rect ) const
243{
244 painter->save();
245
246 switch ( d_data->style )
247 {
248 case QwtColumnSymbol::Box:
249 {
250 drawBox( painter, rect );
251 break;
252 }
253 default:;
254 }
255
256 painter->restore();
257}
258
259/*!
260 Draw the symbol when it is in Box style.
261
262 \param painter Painter
263 \param rect Directed rectangle
264
265 \sa draw()
266*/
267void QwtColumnSymbol::drawBox( QPainter *painter,
268 const QwtColumnRect &rect ) const
269{
270 QRectF r = rect.toRect();
271 if ( QwtPainter::roundingAlignment( painter ) )
272 {
273 r.setLeft( qRound( r.left() ) );
274 r.setRight( qRound( r.right() ) );
275 r.setTop( qRound( r.top() ) );
276 r.setBottom( qRound( r.bottom() ) );
277 }
278
279 switch ( d_data->frameStyle )
280 {
281 case QwtColumnSymbol::Raised:
282 {
283 ::drawPanel( painter, r, d_data->palette, d_data->lineWidth );
284 break;
285 }
286 case QwtColumnSymbol::Plain:
287 {
288 ::drawBox( painter, r, d_data->palette, d_data->lineWidth );
289 break;
290 }
291 default:
292 {
293 painter->fillRect( r, d_data->palette.window() );
294 }
295 }
296}
Note: See TracBrowser for help on using the repository browser.