Qwt User's Guide 6.3.0
Loading...
Searching...
No Matches
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
19class 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
46QwtInterval 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
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
84 setZ( 11.0 );
85}
86
89{
90 delete m_data;
91}
92
98
109{
110 m_data->scaleDivFromAxis = false;
111 m_data->scaleDraw->setScaleDiv( scaleDiv );
112}
113
116{
117 return m_data->scaleDraw->scaleDiv();
118}
119
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 {
138 plt->axisScaleDiv( yAxis() ) );
139 itemChanged();
140 }
141 }
142 }
143}
144
151{
152 return m_data->scaleDivFromAxis;
153}
154
159void QwtPlotScaleItem::setPalette( const QPalette& palette )
160{
161 if ( palette != m_data->palette )
162 {
163 m_data->palette = palette;
164
166 itemChanged();
167 }
168}
169
175{
176 return m_data->palette;
177}
178
183void QwtPlotScaleItem::setFont( const QFont& font )
184{
185 if ( font != m_data->font )
186 {
187 m_data->font = font;
188 itemChanged();
189 }
190}
191
197{
198 return m_data->font;
199}
200
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 {
226 plt->axisScaleDiv( yAxis() ) );
227 }
228
229 itemChanged();
230}
231
237{
238 return m_data->scaleDraw;
239}
240
246{
247 return m_data->scaleDraw;
248}
249
262{
263 if ( m_data->position != pos )
264 {
265 m_data->position = pos;
266 m_data->borderDistance = -1;
267 itemChanged();
268 }
269}
270
276{
277 return m_data->position;
278}
279
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
313{
314 return m_data->borderDistance;
315}
316
334{
335 QwtScaleDraw* sd = m_data->scaleDraw;
336 if ( sd->alignment() != alignment )
337 {
338 sd->setAlignment( alignment );
339 itemChanged();
340 }
341}
342
346void 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 {
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 {
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
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
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 {
478 }
479 }
480}
void setScaleDiv(const QwtScaleDiv &)
void setTransformation(QwtTransform *)
virtual void draw(QPainter *, const QPalette &) const
Draw the scale.
const QwtScaleDiv & scaleDiv() const
A class representing an interval.
void setMaxValue(double)
void setMinValue(double)
A 2-D plotting widget.
Definition qwt_plot.h:79
QWidget * canvas()
Definition qwt_plot.cpp:463
virtual QwtScaleMap canvasMap(QwtAxisId) const
Definition qwt_plot.cpp:800
const QwtScaleDiv & axisScaleDiv(QwtAxisId) const
Return the scale division of a specified axis.
Base class for items on the plot canvas.
QwtAxisId yAxis() const
Return yAxis.
virtual void legendChanged()
void setZ(double z)
Set the z value.
QwtPlot * plot() const
Return attached plot.
QwtAxisId xAxis() const
Return xAxis.
@ Rtti_PlotScale
For QwtPlotScaleItem.
void setItemInterest(ItemInterest, bool on=true)
virtual void itemChanged()
void setScaleDraw(QwtScaleDraw *)
Set a scale draw.
void setScaleDivFromAxis(bool on)
const QwtScaleDiv & scaleDiv() const
virtual void updateScaleDiv(const QwtScaleDiv &, const QwtScaleDiv &) override
Update the item to changes of the axes scale division.
bool isScaleDivFromAxis() const
const QwtScaleDraw * scaleDraw() const
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect) const override
Draw the scale.
virtual int rtti() const override
virtual ~QwtPlotScaleItem()
Destructor.
void setFont(const QFont &)
void setPosition(double pos)
void setScaleDiv(const QwtScaleDiv &)
Assign a scale division.
QwtPlotScaleItem(QwtScaleDraw::Alignment=QwtScaleDraw::BottomScale, const double pos=0.0)
Constructor for scale item at the position pos.
QPalette palette() const
void setBorderDistance(int)
Align the scale to the canvas.
void setAlignment(QwtScaleDraw::Alignment)
void setPalette(const QPalette &)
A class representing a scale division.
QwtInterval interval() const
void setInterval(double lowerBound, double upperBound)
A class for drawing scales.
void setLength(double length)
void move(double x, double y)
void setAlignment(Alignment)
Alignment alignment() const
Qt::Orientation orientation() const
@ BottomScale
The scale is below.
@ RightScale
The scale is right.
A scale map.
double transform(double s) const
const QwtTransform * transformation() const
Get the transformation.
double invTransform(double p) const
A class representing a text.
Definition qwt_text.h:52
A transformation between coordinate systems.
virtual QwtTransform * copy() const =0
Virtualized copy operation.