Qwt User's Guide: qwt_plot_scaleitem.cpp Source File

Qwt User's Guide 6.2.0

qwt_plot_scaleitem.cpp
1 /******************************************************************************
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_plot_scaleitem.h"
11 #include "qwt_plot.h"
12 #include "qwt_scale_map.h"
13 #include "qwt_interval.h"
14 #include "qwt_text.h"
15
16 #include <qpalette.h>
17 #include <qpainter.h>
18
19 class QwtPlotScaleItem::PrivateData
20 {
21 public:
22 PrivateData()
23 : position( 0.0 )
24 , borderDistance( -1 )
25 , scaleDivFromAxis( true )
26 , scaleDraw( new QwtScaleDraw() )
27 {
28 }
29
30 ~PrivateData()
31 {
32 delete scaleDraw;
33 }
34
35 QwtInterval scaleInterval( const QRectF&,
36 const QwtScaleMap&, const QwtScaleMap& ) const;
37
38 QPalette palette;
39 QFont font;
40 double position;
41 int borderDistance;
42 bool scaleDivFromAxis;
43 QwtScaleDraw* scaleDraw;
44 };
45
46 QwtInterval QwtPlotScaleItem::PrivateData::scaleInterval( const QRectF& canvasRect,
47 const QwtScaleMap& xMap, const QwtScaleMap& yMap ) const
48 {
49 QwtInterval interval;
50 if ( scaleDraw->orientation() == Qt::Horizontal )
51 {
52 interval.setMinValue( xMap.invTransform( canvasRect.left() ) );
53 interval.setMaxValue( xMap.invTransform( canvasRect.right() - 1 ) );
54 }
55 else
56 {
57 interval.setMinValue( yMap.invTransform( canvasRect.bottom() - 1 ) );
58 interval.setMaxValue( yMap.invTransform( canvasRect.top() ) );
59 }
60
61 return interval;
62 }
63
75 QwtPlotScaleItem::QwtPlotScaleItem(
76 QwtScaleDraw::Alignment alignment, const double pos )
77 : QwtPlotItem( QwtText( "Scale" ) )
78 {
79 m_data = new PrivateData;
80 m_data->position = pos;
81 m_data->scaleDraw->setAlignment( alignment );
82
83 setItemInterest( QwtPlotItem::ScaleInterest, true );
84 setZ( 11.0 );
85 }
86
88 QwtPlotScaleItem::~QwtPlotScaleItem()
89 {
90 delete m_data;
91 }
92
94 int QwtPlotScaleItem::rtti() const
95 {
96 return QwtPlotItem::Rtti_PlotScale;
97 }
98
108 void QwtPlotScaleItem::setScaleDiv( const QwtScaleDiv& scaleDiv )
109 {
110 m_data->scaleDivFromAxis = false;
111 m_data->scaleDraw->setScaleDiv( scaleDiv );
112 }
113
115 const QwtScaleDiv& QwtPlotScaleItem::scaleDiv() const
116 {
117 return m_data->scaleDraw->scaleDiv();
118 }
119
127 void QwtPlotScaleItem::setScaleDivFromAxis( bool on )
128 {
129 if ( on != m_data->scaleDivFromAxis )
130 {
131 m_data->scaleDivFromAxis = on;
132 if ( on )
133 {
134 const QwtPlot* plt = plot();
135 if ( plt )
136 {
137 updateScaleDiv( plt->axisScaleDiv( xAxis() ),
138 plt->axisScaleDiv( yAxis() ) );
139 itemChanged();
140 }
141 }
142 }
143 }
144
150 bool QwtPlotScaleItem::isScaleDivFromAxis() const
151 {
152 return m_data->scaleDivFromAxis;
153 }
154
159 void QwtPlotScaleItem::setPalette( const QPalette& palette )
160 {
161 if ( palette != m_data->palette )
162 {
163 m_data->palette = palette;
164
165 legendChanged();
166 itemChanged();
167 }
168 }
169
174 QPalette QwtPlotScaleItem::palette() const
175 {
176 return m_data->palette;
177 }
178
183 void QwtPlotScaleItem::setFont( const QFont& font )
184 {
185 if ( font != m_data->font )
186 {
187 m_data->font = font;
188 itemChanged();
189 }
190 }
191
196 QFont QwtPlotScaleItem::font() const
197 {
198 return m_data->font;
199 }
200
212 void QwtPlotScaleItem::setScaleDraw( QwtScaleDraw* scaleDraw )
213 {
214 if ( scaleDraw == NULL )
215 return;
216
217 if ( scaleDraw != m_data->scaleDraw )
218 delete m_data->scaleDraw;
219
220 m_data->scaleDraw = scaleDraw;
221
222 const QwtPlot* plt = plot();
223 if ( plt )
224 {
225 updateScaleDiv( plt->axisScaleDiv( xAxis() ),
226 plt->axisScaleDiv( yAxis() ) );
227 }
228
229 itemChanged();
230 }
231
236 const QwtScaleDraw* QwtPlotScaleItem::scaleDraw() const
237 {
238 return m_data->scaleDraw;
239 }
240
245 QwtScaleDraw* QwtPlotScaleItem::scaleDraw()
246 {
247 return m_data->scaleDraw;
248 }
249
261 void QwtPlotScaleItem::setPosition( double pos )
262 {
263 if ( m_data->position != pos )
264 {
265 m_data->position = pos;
266 m_data->borderDistance = -1;
267 itemChanged();
268 }
269 }
270
275 double QwtPlotScaleItem::position() const
276 {
277 return m_data->position;
278 }
279
296 void QwtPlotScaleItem::setBorderDistance( int distance )
297 {
298 if ( distance < 0 )
299 distance = -1;
300
301 if ( distance != m_data->borderDistance )
302 {
303 m_data->borderDistance = distance;
304 itemChanged();
305 }
306 }
307
312 int QwtPlotScaleItem::borderDistance() const
313 {
314 return m_data->borderDistance;
315 }
316
333 void QwtPlotScaleItem::setAlignment( QwtScaleDraw::Alignment alignment )
334 {
335 QwtScaleDraw* sd = m_data->scaleDraw;
336 if ( sd->alignment() != alignment )
337 {
338 sd->setAlignment( alignment );
339 itemChanged();
340 }
341 }
342
346 void QwtPlotScaleItem::draw( QPainter* painter,
347 const QwtScaleMap& xMap, const QwtScaleMap& yMap,
348 const QRectF& canvasRect ) const
349 {
350 QwtScaleDraw* sd = m_data->scaleDraw;
351
352 if ( m_data->scaleDivFromAxis )
353 {
354 const QwtInterval interval =
355 m_data->scaleInterval( canvasRect, xMap, yMap );
356
357 if ( interval != sd->scaleDiv().interval() )
358 {
359 QwtScaleDiv scaleDiv = sd->scaleDiv();
360 scaleDiv.setInterval( interval );
361 sd->setScaleDiv( scaleDiv );
362 }
363 }
364
365 QPen pen = painter->pen();
366 pen.setStyle( Qt::SolidLine );
367 painter->setPen( pen );
368
369 if ( sd->orientation() == Qt::Horizontal )
370 {
371 double y;
372 if ( m_data->borderDistance >= 0 )
373 {
374 if ( sd->alignment() == QwtScaleDraw::BottomScale )
375 y = canvasRect.top() + m_data->borderDistance;
376 else
377 {
378 y = canvasRect.bottom() - m_data->borderDistance;
379 }
380
381 }
382 else
383 {
384 y = yMap.transform( m_data->position );
385 }
386
387 if ( y < canvasRect.top() || y > canvasRect.bottom() )
388 return;
389
390 sd->move( canvasRect.left(), y );
391 sd->setLength( canvasRect.width() - 1 );
392
393 QwtTransform* transform = NULL;
394 if ( xMap.transformation() )
395 transform = xMap.transformation()->copy();
396
397 sd->setTransformation( transform );
398 }
399 else // == Qt::Vertical
400 {
401 double x;
402 if ( m_data->borderDistance >= 0 )
403 {
404 if ( sd->alignment() == QwtScaleDraw::RightScale )
405 x = canvasRect.left() + m_data->borderDistance;
406 else
407 {
408 x = canvasRect.right() - m_data->borderDistance;
409 }
410 }
411 else
412 {
413 x = xMap.transform( m_data->position );
414 }
415 if ( x < canvasRect.left() || x > canvasRect.right() )
416 return;
417
418 sd->move( x, canvasRect.top() );
419 sd->setLength( canvasRect.height() - 1 );
420
421 QwtTransform* transform = NULL;
422 if ( yMap.transformation() )
423 transform = yMap.transformation()->copy();
424
425 sd->setTransformation( transform );
426 }
427
428 painter->setFont( m_data->font );
429
430 sd->draw( painter, m_data->palette );
431 }
432
445 void QwtPlotScaleItem::updateScaleDiv( const QwtScaleDiv& xScaleDiv,
446 const QwtScaleDiv& yScaleDiv )
447 {
448 QwtScaleDraw* scaleDraw = m_data->scaleDraw;
449
450 if ( m_data->scaleDivFromAxis && scaleDraw )
451 {
452 const QwtScaleDiv& scaleDiv =
453 scaleDraw->orientation() == Qt::Horizontal ? xScaleDiv : yScaleDiv;
454
455 const QwtPlot* plt = plot();
456 if ( plt != NULL )
457 {
458 const QRectF canvasRect = plt->canvas()->contentsRect();
459
460 const QwtInterval interval = m_data->scaleInterval(
461 canvasRect, plt->canvasMap( xAxis() ), plt->canvasMap( yAxis() ) );
462
463 QwtScaleDiv sd = scaleDiv;
464 sd.setInterval( interval );
465
466 if ( sd != scaleDraw->scaleDiv() )
467 {
468 // the internal label cache of QwtScaleDraw
469 // is cleared here, so better avoid pointless
470 // assignments.
471
472 scaleDraw->setScaleDiv( sd );
473 }
474 }
475 else
476 {
477 scaleDraw->setScaleDiv( scaleDiv );
478 }
479 }
480 }
QwtAbstractScaleDraw::setScaleDiv
void setScaleDiv(const QwtScaleDiv &)
Definition: qwt_abstract_scale_draw.cpp:104
QwtAbstractScaleDraw::setTransformation
void setTransformation(QwtTransform *)
Definition: qwt_abstract_scale_draw.cpp:115
QwtAbstractScaleDraw::draw
virtual void draw(QPainter *, const QPalette &) const
Draw the scale.
Definition: qwt_abstract_scale_draw.cpp:169
QwtAbstractScaleDraw::scaleDiv
const QwtScaleDiv & scaleDiv() const
Definition: qwt_abstract_scale_draw.cpp:133
QwtInterval
A class representing an interval.
Definition: qwt_interval.h:23
QwtInterval::setMaxValue
void setMaxValue(double)
Definition: qwt_interval.h:186
QwtInterval::setMinValue
void setMinValue(double)
Definition: qwt_interval.h:176
QwtPlot
A 2-D plotting widget.
Definition: qwt_plot.h:79
QwtPlot::canvas
QWidget * canvas()
Definition: qwt_plot.cpp:463
QwtPlot::canvasMap
virtual QwtScaleMap canvasMap(QwtAxisId) const
Definition: qwt_plot.cpp:800
QwtPlot::axisScaleDiv
const QwtScaleDiv & axisScaleDiv(QwtAxisId) const
Return the scale division of a specified axis.
Definition: qwt_plot_axis.cpp:283
QwtPlotItem
Base class for items on the plot canvas.
Definition: qwt_plot_item.h:67
QwtPlotItem::yAxis
QwtAxisId yAxis() const
Return yAxis.
Definition: qwt_plot_item.cpp:559
QwtPlotItem::legendChanged
virtual void legendChanged()
Definition: qwt_plot_item.cpp:491
QwtPlotItem::setZ
void setZ(double z)
Set the z value.
Definition: qwt_plot_item.cpp:165
QwtPlotItem::plot
QwtPlot * plot() const
Return attached plot.
Definition: qwt_plot_item.cpp:142
QwtPlotItem::xAxis
QwtAxisId xAxis() const
Return xAxis.
Definition: qwt_plot_item.cpp:553
QwtPlotItem::Rtti_PlotScale
@ Rtti_PlotScale
For QwtPlotScaleItem.
Definition: qwt_plot_item.h:84
QwtPlotItem::setItemInterest
void setItemInterest(ItemInterest, bool on=true)
Definition: qwt_plot_item.cpp:279
QwtPlotItem::itemChanged
virtual void itemChanged()
Definition: qwt_plot_item.cpp:481
QwtPlotItem::ScaleInterest
@ ScaleInterest
Definition: qwt_plot_item.h:185
QwtPlotScaleItem::setScaleDraw
void setScaleDraw(QwtScaleDraw *)
Set a scale draw.
Definition: qwt_plot_scaleitem.cpp:212
QwtPlotScaleItem::setScaleDivFromAxis
void setScaleDivFromAxis(bool on)
Definition: qwt_plot_scaleitem.cpp:127
QwtPlotScaleItem::borderDistance
int borderDistance() const
Definition: qwt_plot_scaleitem.cpp:312
QwtPlotScaleItem::scaleDiv
const QwtScaleDiv & scaleDiv() const
Definition: qwt_plot_scaleitem.cpp:115
QwtPlotScaleItem::updateScaleDiv
virtual void updateScaleDiv(const QwtScaleDiv &, const QwtScaleDiv &) override
Update the item to changes of the axes scale division.
Definition: qwt_plot_scaleitem.cpp:445
QwtPlotScaleItem::position
double position() const
Definition: qwt_plot_scaleitem.cpp:275
QwtPlotScaleItem::isScaleDivFromAxis
bool isScaleDivFromAxis() const
Definition: qwt_plot_scaleitem.cpp:150
QwtPlotScaleItem::scaleDraw
const QwtScaleDraw * scaleDraw() const
Definition: qwt_plot_scaleitem.cpp:236
QwtPlotScaleItem::draw
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect) const override
Draw the scale.
Definition: qwt_plot_scaleitem.cpp:346
QwtPlotScaleItem::rtti
virtual int rtti() const override
Definition: qwt_plot_scaleitem.cpp:94
QwtPlotScaleItem::~QwtPlotScaleItem
virtual ~QwtPlotScaleItem()
Destructor.
Definition: qwt_plot_scaleitem.cpp:88
QwtPlotScaleItem::setFont
void setFont(const QFont &)
Definition: qwt_plot_scaleitem.cpp:183
QwtPlotScaleItem::setPosition
void setPosition(double pos)
Definition: qwt_plot_scaleitem.cpp:261
QwtPlotScaleItem::setScaleDiv
void setScaleDiv(const QwtScaleDiv &)
Assign a scale division.
Definition: qwt_plot_scaleitem.cpp:108
QwtPlotScaleItem::QwtPlotScaleItem
QwtPlotScaleItem(QwtScaleDraw::Alignment=QwtScaleDraw::BottomScale, const double pos=0.0)
Constructor for scale item at the position pos.
Definition: qwt_plot_scaleitem.cpp:75
QwtPlotScaleItem::font
QFont font() const
Definition: qwt_plot_scaleitem.cpp:196
QwtPlotScaleItem::palette
QPalette palette() const
Definition: qwt_plot_scaleitem.cpp:174
QwtPlotScaleItem::setBorderDistance
void setBorderDistance(int)
Align the scale to the canvas.
Definition: qwt_plot_scaleitem.cpp:296
QwtPlotScaleItem::setAlignment
void setAlignment(QwtScaleDraw::Alignment)
Definition: qwt_plot_scaleitem.cpp:333
QwtPlotScaleItem::setPalette
void setPalette(const QPalette &)
Definition: qwt_plot_scaleitem.cpp:159
QwtScaleDiv
A class representing a scale division.
Definition: qwt_scale_div.h:34
QwtScaleDiv::interval
QwtInterval interval() const
Definition: qwt_scale_div.cpp:111
QwtScaleDiv::setInterval
void setInterval(double lowerBound, double upperBound)
Definition: qwt_scale_div.cpp:91
QwtScaleDraw
A class for drawing scales.
Definition: qwt_scale_draw.h:36
QwtScaleDraw::setLength
void setLength(double length)
Definition: qwt_scale_draw.cpp:732
QwtScaleDraw::move
void move(double x, double y)
Definition: qwt_scale_draw.h:118
QwtScaleDraw::setAlignment
void setAlignment(Alignment)
Definition: qwt_scale_draw.cpp:322
QwtScaleDraw::alignment
Alignment alignment() const
Definition: qwt_scale_draw.cpp:309
QwtScaleDraw::orientation
Qt::Orientation orientation() const
Definition: qwt_scale_draw.cpp:337
QwtScaleDraw::Alignment
Alignment
Definition: qwt_scale_draw.h:43
QwtScaleDraw::BottomScale
@ BottomScale
The scale is below.
Definition: qwt_scale_draw.h:45
QwtScaleDraw::RightScale
@ RightScale
The scale is right.
Definition: qwt_scale_draw.h:54
QwtScaleMap
A scale map.
Definition: qwt_scale_map.h:27
QwtScaleMap::transform
double transform(double s) const
Definition: qwt_scale_map.h:137
QwtScaleMap::transformation
const QwtTransform * transformation() const
Get the transformation.
Definition: qwt_scale_map.cpp:88
QwtScaleMap::invTransform
double invTransform(double p) const
Definition: qwt_scale_map.h:154
QwtText
A class representing a text.
Definition: qwt_text.h:52
QwtTransform
A transformation between coordinate systems.
Definition: qwt_transform.h:36
QwtTransform::copy
virtual QwtTransform * copy() const =0
Virtualized copy operation.