qwt_scale_widget.cpp
Go to the documentation of this file.
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_scale_widget.h"
11 #include "qwt_painter.h"
12 #include "qwt_color_map.h"
13 #include "qwt_scale_map.h"
14 #include "qwt_math.h"
15 #include "qwt_scale_div.h"
16 #include "qwt_text.h"
17 #include "qwt_scale_engine.h"
18 #include <qpainter.h>
19 #include <qevent.h>
20 #include <qmath.h>
21 #include <qstyle.h>
22 #include <qstyleoption.h>
23 
25 {
26 public:
28  scaleDraw( NULL )
29  {
30  colorBar.colorMap = NULL;
31  }
32 
34  {
35  delete scaleDraw;
36  delete colorBar.colorMap;
37  }
38 
40 
41  int borderDist[2];
42  int minBorderDist[2];
44  int margin;
45 
47  int spacing;
49 
51 
52  struct t_colorBar
53  {
54  bool isEnabled;
55  int width;
58  } colorBar;
59 };
60 
65 QwtScaleWidget::QwtScaleWidget( QWidget *parent ):
66  QWidget( parent )
67 {
69 }
70 
77  QwtScaleDraw::Alignment align, QWidget *parent ):
78  QWidget( parent )
79 {
80  initScale( align );
81 }
82 
85 {
86  delete d_data;
87 }
88 
91 {
92  d_data = new PrivateData;
93 
94  d_data->layoutFlags = 0;
95  if ( align == QwtScaleDraw::RightScale )
97 
98  d_data->borderDist[0] = 0;
99  d_data->borderDist[1] = 0;
100  d_data->minBorderDist[0] = 0;
101  d_data->minBorderDist[1] = 0;
102  d_data->margin = 4;
103  d_data->titleOffset = 0;
104  d_data->spacing = 2;
105 
107  d_data->scaleDraw->setAlignment( align );
108  d_data->scaleDraw->setLength( 10 );
109 
111  QwtLinearScaleEngine().divideScale( 0.0, 100.0, 10, 5 ) );
112 
114  d_data->colorBar.isEnabled = false;
115  d_data->colorBar.width = 10;
116 
117  const int flags = Qt::AlignHCenter
118  | Qt::TextExpandTabs | Qt::TextWordWrap;
119  d_data->title.setRenderFlags( flags );
120  d_data->title.setFont( font() );
121 
122  QSizePolicy policy( QSizePolicy::MinimumExpanding,
123  QSizePolicy::Fixed );
124  if ( d_data->scaleDraw->orientation() == Qt::Vertical )
125  policy.transpose();
126 
127  setSizePolicy( policy );
128 
129  setAttribute( Qt::WA_WState_OwnSizePolicy, false );
130 }
131 
141 {
142  if ( ( ( d_data->layoutFlags & flag ) != 0 ) != on )
143  {
144  if ( on )
145  d_data->layoutFlags |= flag;
146  else
147  d_data->layoutFlags &= ~flag;
148  }
149 }
150 
159 {
160  return ( d_data->layoutFlags & flag );
161 }
162 
169 void QwtScaleWidget::setTitle( const QString &title )
170 {
171  if ( d_data->title.text() != title )
172  {
173  d_data->title.setText( title );
174  layoutScale();
175  }
176 }
177 
188 {
189  QwtText t = title;
190  const int flags = title.renderFlags() & ~( Qt::AlignTop | Qt::AlignBottom );
191  t.setRenderFlags( flags );
192 
193  if ( t != d_data->title )
194  {
195  d_data->title = t;
196  layoutScale();
197  }
198 }
199 
207 {
208  if ( d_data->scaleDraw )
209  d_data->scaleDraw->setAlignment( alignment );
210 
211  if ( !testAttribute( Qt::WA_WState_OwnSizePolicy ) )
212  {
213  QSizePolicy policy( QSizePolicy::MinimumExpanding,
214  QSizePolicy::Fixed );
215  if ( d_data->scaleDraw->orientation() == Qt::Vertical )
216  policy.transpose();
217 
218  setSizePolicy( policy );
219 
220  setAttribute( Qt::WA_WState_OwnSizePolicy, false );
221  }
222 
223  layoutScale();
224 }
225 
226 
232 {
233  if ( !scaleDraw() )
235 
236  return scaleDraw()->alignment();
237 }
238 
247 void QwtScaleWidget::setBorderDist( int dist1, int dist2 )
248 {
249  if ( dist1 != d_data->borderDist[0] || dist2 != d_data->borderDist[1] )
250  {
251  d_data->borderDist[0] = dist1;
252  d_data->borderDist[1] = dist2;
253  layoutScale();
254  }
255 }
256 
263 {
264  margin = qMax( 0, margin );
265  if ( margin != d_data->margin )
266  {
267  d_data->margin = margin;
268  layoutScale();
269  }
270 }
271 
278 {
279  spacing = qMax( 0, spacing );
280  if ( spacing != d_data->spacing )
281  {
283  layoutScale();
284  }
285 }
286 
293 {
294  d_data->scaleDraw->setLabelAlignment( alignment );
295  layoutScale();
296 }
297 
305 void QwtScaleWidget::setLabelRotation( double rotation )
306 {
307  d_data->scaleDraw->setLabelRotation( rotation );
308  layoutScale();
309 }
310 
323 {
324  if ( ( scaleDraw == NULL ) || ( scaleDraw == d_data->scaleDraw ) )
325  return;
326 
327  const QwtScaleDraw* sd = d_data->scaleDraw;
328  if ( sd )
329  {
330  scaleDraw->setAlignment( sd->alignment() );
331  scaleDraw->setScaleDiv( sd->scaleDiv() );
332 
333  QwtTransform *transform = NULL;
334  if ( sd->scaleMap().transformation() )
335  transform = sd->scaleMap().transformation()->copy();
336 
337  scaleDraw->setTransformation( transform );
338  }
339 
340  delete d_data->scaleDraw;
342 
343  layoutScale();
344 }
345 
351 {
352  return d_data->scaleDraw;
353 }
354 
360 {
361  return d_data->scaleDraw;
362 }
363 
369 {
370  return d_data->title;
371 }
372 
378 {
379  return d_data->borderDist[0];
380 }
381 
387 {
388  return d_data->borderDist[1];
389 }
390 
396 {
397  return d_data->margin;
398 }
399 
405 {
406  return d_data->spacing;
407 }
408 
412 void QwtScaleWidget::paintEvent( QPaintEvent *event )
413 {
414  QPainter painter( this );
415  painter.setClipRegion( event->region() );
416 
417  QStyleOption opt;
418  opt.init(this);
419  style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
420 
421  draw( &painter );
422 }
423 
427 void QwtScaleWidget::draw( QPainter *painter ) const
428 {
429  d_data->scaleDraw->draw( painter, palette() );
430 
431  if ( d_data->colorBar.isEnabled && d_data->colorBar.width > 0 &&
433  {
434  drawColorBar( painter, colorBarRect( contentsRect() ) );
435  }
436 
437  QRect r = contentsRect();
438  if ( d_data->scaleDraw->orientation() == Qt::Horizontal )
439  {
440  r.setLeft( r.left() + d_data->borderDist[0] );
441  r.setWidth( r.width() - d_data->borderDist[1] );
442  }
443  else
444  {
445  r.setTop( r.top() + d_data->borderDist[0] );
446  r.setHeight( r.height() - d_data->borderDist[1] );
447  }
448 
449  if ( !d_data->title.isEmpty() )
450  drawTitle( painter, d_data->scaleDraw->alignment(), r );
451 }
452 
459 QRectF QwtScaleWidget::colorBarRect( const QRectF& rect ) const
460 {
461  QRectF cr = rect;
462 
463  if ( d_data->scaleDraw->orientation() == Qt::Horizontal )
464  {
465  cr.setLeft( cr.left() + d_data->borderDist[0] );
466  cr.setWidth( cr.width() - d_data->borderDist[1] + 1 );
467  }
468  else
469  {
470  cr.setTop( cr.top() + d_data->borderDist[0] );
471  cr.setHeight( cr.height() - d_data->borderDist[1] + 1 );
472  }
473 
474  switch ( d_data->scaleDraw->alignment() )
475  {
477  {
478  cr.setLeft( cr.right() - d_data->margin
479  - d_data->colorBar.width );
480  cr.setWidth( d_data->colorBar.width );
481  break;
482  }
483 
485  {
486  cr.setLeft( cr.left() + d_data->margin );
487  cr.setWidth( d_data->colorBar.width );
488  break;
489  }
490 
492  {
493  cr.setTop( cr.top() + d_data->margin );
494  cr.setHeight( d_data->colorBar.width );
495  break;
496  }
497 
499  {
500  cr.setTop( cr.bottom() - d_data->margin
501  - d_data->colorBar.width );
502  cr.setHeight( d_data->colorBar.width );
503  break;
504  }
505  }
506 
507  return cr;
508 }
509 
516 void QwtScaleWidget::changeEvent( QEvent *event )
517 {
518  if ( event->type() == QEvent::LocaleChange )
519  {
521  }
522 
523  QWidget::changeEvent( event );
524 }
525 
530 void QwtScaleWidget::resizeEvent( QResizeEvent *event )
531 {
532  Q_UNUSED( event );
533  layoutScale( false );
534 }
535 
544 void QwtScaleWidget::layoutScale( bool update_geometry )
545 {
546  int bd0, bd1;
547  getBorderDistHint( bd0, bd1 );
548  if ( d_data->borderDist[0] > bd0 )
549  bd0 = d_data->borderDist[0];
550  if ( d_data->borderDist[1] > bd1 )
551  bd1 = d_data->borderDist[1];
552 
553  int colorBarWidth = 0;
555  colorBarWidth = d_data->colorBar.width + d_data->spacing;
556 
557  const QRectF r = contentsRect();
558  double x, y, length;
559 
560  if ( d_data->scaleDraw->orientation() == Qt::Vertical )
561  {
562  y = r.top() + bd0;
563  length = r.height() - ( bd0 + bd1 );
564 
566  x = r.right() - 1.0 - d_data->margin - colorBarWidth;
567  else
568  x = r.left() + d_data->margin + colorBarWidth;
569  }
570  else
571  {
572  x = r.left() + bd0;
573  length = r.width() - ( bd0 + bd1 );
574 
576  y = r.top() + d_data->margin + colorBarWidth;
577  else
578  y = r.bottom() - 1.0 - d_data->margin - colorBarWidth;
579  }
580 
581  d_data->scaleDraw->move( x, y );
582  d_data->scaleDraw->setLength( length );
583 
584  const int extent = qCeil( d_data->scaleDraw->extent( font() ) );
585 
587  d_data->margin + d_data->spacing + colorBarWidth + extent;
588 
589  if ( update_geometry )
590  {
591  updateGeometry();
592  update();
593  }
594 }
595 
604 void QwtScaleWidget::drawColorBar( QPainter *painter, const QRectF& rect ) const
605 {
606  if ( !d_data->colorBar.interval.isValid() )
607  return;
608 
609  const QwtScaleDraw* sd = d_data->scaleDraw;
610 
613  sd->orientation(), rect );
614 }
615 
624 void QwtScaleWidget::drawTitle( QPainter *painter,
625  QwtScaleDraw::Alignment align, const QRectF &rect ) const
626 {
627  QRectF r = rect;
628  double angle;
629  int flags = d_data->title.renderFlags() &
630  ~( Qt::AlignTop | Qt::AlignBottom | Qt::AlignVCenter );
631 
632  switch ( align )
633  {
635  angle = -90.0;
636  flags |= Qt::AlignTop;
637  r.setRect( r.left(), r.bottom(),
638  r.height(), r.width() - d_data->titleOffset );
639  break;
640 
642  angle = -90.0;
643  flags |= Qt::AlignTop;
644  r.setRect( r.left() + d_data->titleOffset, r.bottom(),
645  r.height(), r.width() - d_data->titleOffset );
646  break;
647 
649  angle = 0.0;
650  flags |= Qt::AlignBottom;
651  r.setTop( r.top() + d_data->titleOffset );
652  break;
653 
655  default:
656  angle = 0.0;
657  flags |= Qt::AlignTop;
658  r.setBottom( r.bottom() - d_data->titleOffset );
659  break;
660  }
661 
663  {
664  if ( align == QwtScaleDraw::LeftScale
665  || align == QwtScaleDraw::RightScale )
666  {
667  angle = -angle;
668  r.setRect( r.x() + r.height(), r.y() - r.width(),
669  r.width(), r.height() );
670  }
671  }
672 
673  painter->save();
674  painter->setFont( font() );
675  painter->setPen( palette().color( QPalette::Text ) );
676 
677  painter->translate( r.x(), r.y() );
678  if ( angle != 0.0 )
679  painter->rotate( angle );
680 
682  title.setRenderFlags( flags );
683  title.draw( painter, QRectF( 0.0, 0.0, r.width(), r.height() ) );
684 
685  painter->restore();
686 }
687 
697 {
698  layoutScale();
699 }
700 
705 {
706  return minimumSizeHint();
707 }
708 
713 {
714  const Qt::Orientation o = d_data->scaleDraw->orientation();
715 
716  // Border Distance cannot be less than the scale borderDistHint
717  // Note, the borderDistHint is already included in minHeight/minWidth
718  int length = 0;
719  int mbd1, mbd2;
720  getBorderDistHint( mbd1, mbd2 );
721  length += qMax( 0, d_data->borderDist[0] - mbd1 );
722  length += qMax( 0, d_data->borderDist[1] - mbd2 );
723  length += d_data->scaleDraw->minLength( font() );
724 
725  int dim = dimForLength( length, font() );
726  if ( length < dim )
727  {
728  // compensate for long titles
729  length = dim;
730  dim = dimForLength( length, font() );
731  }
732 
733  QSize size( length + 2, dim );
734  if ( o == Qt::Vertical )
735  size.transpose();
736 
737  int left, right, top, bottom;
738  getContentsMargins( &left, &top, &right, &bottom );
739  return size + QSize( left + right, top + bottom );
740 }
741 
749 {
750  return qCeil( d_data->title.heightForWidth( width, font() ) );
751 }
752 
762 int QwtScaleWidget::dimForLength( int length, const QFont &scaleFont ) const
763 {
764  const int extent = qCeil( d_data->scaleDraw->extent( scaleFont ) );
765 
766  int dim = d_data->margin + extent + 1;
767 
768  if ( !d_data->title.isEmpty() )
769  dim += titleHeightForWidth( length ) + d_data->spacing;
770 
772  dim += d_data->colorBar.width + d_data->spacing;
773 
774  return dim;
775 }
776 
795 void QwtScaleWidget::getBorderDistHint( int &start, int &end ) const
796 {
797  d_data->scaleDraw->getBorderDistHint( font(), start, end );
798 
799  if ( start < d_data->minBorderDist[0] )
800  start = d_data->minBorderDist[0];
801 
802  if ( end < d_data->minBorderDist[1] )
803  end = d_data->minBorderDist[1];
804 }
805 
816 void QwtScaleWidget::setMinBorderDist( int start, int end )
817 {
818  d_data->minBorderDist[0] = start;
819  d_data->minBorderDist[1] = end;
820 }
821 
833 void QwtScaleWidget::getMinBorderDist( int &start, int &end ) const
834 {
835  start = d_data->minBorderDist[0];
836  end = d_data->minBorderDist[1];
837 }
838 
848 {
850  if ( sd->scaleDiv() != scaleDiv )
851  {
852  sd->setScaleDiv( scaleDiv );
853  layoutScale();
854 
855  Q_EMIT scaleDivChanged();
856  }
857 }
858 
866 {
867  d_data->scaleDraw->setTransformation( transformation );
868  layoutScale();
869 }
870 
876 {
877  if ( on != d_data->colorBar.isEnabled )
878  {
879  d_data->colorBar.isEnabled = on;
880  layoutScale();
881  }
882 }
883 
889 {
890  return d_data->colorBar.isEnabled;
891 }
892 
900 {
901  if ( width != d_data->colorBar.width )
902  {
903  d_data->colorBar.width = width;
904  if ( isColorBarEnabled() )
905  layoutScale();
906  }
907 }
908 
914 {
915  return d_data->colorBar.width;
916 }
917 
923 {
924  return d_data->colorBar.interval;
925 }
926 
937  const QwtInterval &interval, QwtColorMap *colorMap )
938 {
939  d_data->colorBar.interval = interval;
940 
941  if ( colorMap != d_data->colorBar.colorMap )
942  {
943  delete d_data->colorBar.colorMap;
945  }
946 
947  if ( isColorBarEnabled() )
948  layoutScale();
949 }
950 
956 {
957  return d_data->colorBar.colorMap;
958 }
void drawColorBar(QPainter *painter, const QRectF &) const
virtual void resizeEvent(QResizeEvent *)
void scaleDivChanged()
Signal emitted, whenever the scale division changes.
QwtInterval normalized() const
Normalize the limits of the interval.
void setAlignment(Alignment)
void setLength(double length)
void setColorBarEnabled(bool)
virtual ~QwtScaleWidget()
Destructor.
void setFont(const QFont &)
Definition: qwt_text.cpp:296
void setColorMap(const QwtInterval &, QwtColorMap *)
void initScale(QwtScaleDraw::Alignment)
Initialize the scale.
void setSpacing(int td)
Specify the distance between color bar, scale and title.
virtual double extent(const QFont &) const
int renderFlags() const
Definition: qwt_text.cpp:284
A class representing an interval.
Definition: qwt_interval.h:26
void setText(const QString &, QwtText::TextFormat textFormat=AutoText)
Definition: qwt_text.cpp:244
QwtScaleWidget::LayoutFlags layoutFlags
int margin() const
The scale is above.
int titleHeightForWidth(int width) const
Find the height of the title for a given width.
virtual QwtTransform * copy() const =0
Virtualized copy operation.
void setLabelAlignment(Qt::Alignment)
Change the alignment for the labels.
void draw(QPainter *painter, const QRectF &rect) const
Definition: qwt_text.cpp:560
A class representing a scale division.
Definition: qwt_scale_div.h:36
const QwtScaleMap & scaleMap() const
void setMargin(int)
Specify the margin to the colorBar/base line.
const QwtScaleDraw * scaleDraw() const
void getBorderDistHint(const QFont &, int &start, int &end) const
Determine the minimum border distance.
TFSIMD_FORCE_INLINE const tfScalar & y() const
void setLabelRotation(double rotation)
Change the rotation for the labels. See QwtScaleDraw::setLabelRotation().
LayoutFlag
Layout flags of the title.
QwtScaleDraw::Alignment alignment() const
int minLength(const QFont &) const
void setAlignment(QwtScaleDraw::Alignment)
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
PrivateData * d_data
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
int startBorderDist() const
void getMinBorderDist(int &start, int &end) const
Alignment alignment() const
void setBorderDist(int start, int end)
The scale is left.
void setRenderFlags(int flags)
Change the render flags.
Definition: qwt_text.cpp:271
QwtInterval colorBarInterval() const
void setTitle(const QString &title)
QRectF colorBarRect(const QRectF &) const
struct QwtScaleWidget::PrivateData::t_colorBar colorBar
const QwtScaleDiv & scaleDiv() const
void setScaleDiv(const QwtScaleDiv &sd)
Assign a scale division.
bool isEmpty() const
Definition: qwt_text.h:213
bool isValid() const
Definition: qwt_interval.h:211
A transformation between coordinate systems.
Definition: qwt_transform.h:35
virtual QSize minimumSizeHint() const
A class representing a text.
Definition: qwt_text.h:51
void setMinBorderDist(int start, int end)
QString text() const
Definition: qwt_text.cpp:256
QwtColorMap is used to map values into colors.
Definition: qwt_color_map.h:33
void drawTitle(QPainter *painter, QwtScaleDraw::Alignment, const QRectF &rect) const
int dimForLength(int length, const QFont &scaleFont) const
Find the minimum dimension for a given length. dim is the height, length the width seen in direction ...
bool testLayoutFlag(LayoutFlag) const
virtual void changeEvent(QEvent *)
TFSIMD_FORCE_INLINE const tfScalar & x() const
void setColorBarWidth(int)
void setLabelRotation(double rotation)
virtual QSize sizeHint() const
const QwtTransform * transformation() const
Get the transformation.
QwtLinearColorMap builds a color map from color stops.
Definition: qwt_color_map.h:95
Qt::Orientation orientation() const
int endBorderDist() const
uintptr_t size
UnboundConversion o
int spacing() const
int colorBarWidth() const
void draw(QPainter *p) const
draw the scale
virtual void draw(QPainter *, const QPalette &) const
Draw the scale.
QwtScaleWidget(QWidget *parent=NULL)
Create a scale with the position QwtScaleWidget::Left.
void setLayoutFlag(LayoutFlag, bool on)
bool isColorBarEnabled() const
virtual void paintEvent(QPaintEvent *)
paintEvent
The scale is right.
void layoutScale(bool update=true)
static void drawColorBar(QPainter *painter, const QwtColorMap &, const QwtInterval &, const QwtScaleMap &, Qt::Orientation, const QRectF &)
QFlags< LayoutFlag > LayoutFlags
Layout flags of the title.
void getBorderDistHint(int &start, int &end) const
Calculate a hint for the border distances.
void setScaleDiv(const QwtScaleDiv &s)
void setTransformation(QwtTransform *)
A class for drawing scales.
A scale engine for linear scales.
void setTransformation(QwtTransform *)
QwtText title() const
void setScaleDraw(QwtScaleDraw *)
void scaleChange()
Notify a change of the scale.
int flags
const QwtColorMap * colorMap() const
std::size_t length
void setLabelAlignment(Qt::Alignment)
Change the label flags.
double heightForWidth(double width, const QFont &=QFont()) const
Definition: qwt_text.cpp:490
The scale is below.
void move(double x, double y)


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17