qwt_round_scale_draw.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #include "qwt_round_scale_draw.h"
  10. #include "qwt_painter.h"
  11. #include "qwt_scale_div.h"
  12. #include "qwt_scale_map.h"
  13. #include <qpen.h>
  14. #include <qpainter.h>
  15. #include <qfontmetrics.h>
  16. #include <qmath.h>
  17. class QwtRoundScaleDraw::PrivateData
  18. {
  19. public:
  20. PrivateData():
  21. center( 50, 50 ),
  22. radius( 50 ),
  23. startAngle( -135 * 16 ),
  24. endAngle( 135 * 16 )
  25. {
  26. }
  27. QPointF center;
  28. double radius;
  29. double startAngle;
  30. double endAngle;
  31. };
  32. /*!
  33. \brief Constructor
  34. The range of the scale is initialized to [0, 100],
  35. The center is set to (50, 50) with a radius of 50.
  36. The angle range is set to [-135, 135].
  37. */
  38. QwtRoundScaleDraw::QwtRoundScaleDraw()
  39. {
  40. d_data = new QwtRoundScaleDraw::PrivateData;
  41. setRadius( 50 );
  42. scaleMap().setPaintInterval( d_data->startAngle, d_data->endAngle );
  43. }
  44. //! Destructor
  45. QwtRoundScaleDraw::~QwtRoundScaleDraw()
  46. {
  47. delete d_data;
  48. }
  49. /*!
  50. Change of radius the scale
  51. Radius is the radius of the backbone without ticks and labels.
  52. \param radius New Radius
  53. \sa moveCenter()
  54. */
  55. void QwtRoundScaleDraw::setRadius( int radius )
  56. {
  57. d_data->radius = radius;
  58. }
  59. /*!
  60. Get the radius
  61. Radius is the radius of the backbone without ticks and labels.
  62. \sa setRadius(), extent()
  63. */
  64. int QwtRoundScaleDraw::radius() const
  65. {
  66. return d_data->radius;
  67. }
  68. /*!
  69. Move the center of the scale draw, leaving the radius unchanged
  70. \param center New center
  71. \sa setRadius()
  72. */
  73. void QwtRoundScaleDraw::moveCenter( const QPointF &center )
  74. {
  75. d_data->center = center;
  76. }
  77. //! Get the center of the scale
  78. QPointF QwtRoundScaleDraw::center() const
  79. {
  80. return d_data->center;
  81. }
  82. /*!
  83. \brief Adjust the baseline circle segment for round scales.
  84. The baseline will be drawn from min(angle1,angle2) to max(angle1, angle2).
  85. The default setting is [ -135, 135 ].
  86. An angle of 0 degrees corresponds to the 12 o'clock position,
  87. and positive angles count in a clockwise direction.
  88. \param angle1
  89. \param angle2 boundaries of the angle interval in degrees.
  90. \warning <ul>
  91. <li>The angle range is limited to [-360, 360] degrees. Angles exceeding
  92. this range will be clipped.
  93. <li>For angles more than 359 degrees above or below min(angle1, angle2),
  94. scale marks will not be drawn.
  95. <li>If you need a counterclockwise scale, use QwtScaleDiv::setRange
  96. </ul>
  97. */
  98. void QwtRoundScaleDraw::setAngleRange( double angle1, double angle2 )
  99. {
  100. angle1 = qwtLim( angle1, -360.0, 360.0 );
  101. angle2 = qwtLim( angle2, -360.0, 360.0 );
  102. d_data->startAngle = angle1 * 16.0;
  103. d_data->endAngle = angle2 * 16.0;
  104. if ( d_data->startAngle == d_data->endAngle )
  105. {
  106. d_data->startAngle -= 1;
  107. d_data->endAngle += 1;
  108. }
  109. scaleMap().setPaintInterval( d_data->startAngle, d_data->endAngle );
  110. }
  111. /*!
  112. Draws the label for a major scale tick
  113. \param painter Painter
  114. \param value Value
  115. \sa drawTick(), drawBackbone()
  116. */
  117. void QwtRoundScaleDraw::drawLabel( QPainter *painter, double value ) const
  118. {
  119. const QwtText label = tickLabel( painter->font(), value );
  120. if ( label.isEmpty() )
  121. return;
  122. const double tval = map().transform( value );
  123. if ( ( tval > d_data->startAngle + 359 * 16 )
  124. || ( tval < d_data->startAngle - 359 * 16 ) )
  125. {
  126. return;
  127. }
  128. double radius = d_data->radius;
  129. if ( hasComponent( QwtAbstractScaleDraw::Ticks ) ||
  130. hasComponent( QwtAbstractScaleDraw::Backbone ) )
  131. {
  132. radius += spacing();
  133. }
  134. if ( hasComponent( QwtAbstractScaleDraw::Ticks ) )
  135. radius += majTickLength();
  136. const QSizeF sz = label.textSize( painter->font() );
  137. const double arc = tval / 16.0 / 360.0 * 2 * M_PI;
  138. const double x = d_data->center.x() +
  139. ( radius + sz.width() / 2.0 ) * qSin( arc );
  140. const double y = d_data->center.y() -
  141. ( radius + sz.height() / 2.0 ) * cos( arc );
  142. const QRectF r( x - sz.width() / 2, y - sz.height() / 2,
  143. sz.width(), sz.height() );
  144. label.draw( painter, r );
  145. }
  146. /*!
  147. Draw a tick
  148. \param painter Painter
  149. \param value Value of the tick
  150. \param len Lenght of the tick
  151. \sa drawBackbone(), drawLabel()
  152. */
  153. void QwtRoundScaleDraw::drawTick( QPainter *painter, double value, double len ) const
  154. {
  155. if ( len <= 0 )
  156. return;
  157. const double tval = map().transform( value );
  158. const double cx = d_data->center.x();
  159. const double cy = d_data->center.y();
  160. const double radius = d_data->radius;
  161. if ( ( tval <= d_data->startAngle + 359 * 16 )
  162. || ( tval >= d_data->startAngle - 359 * 16 ) )
  163. {
  164. const double arc = double( tval ) / 16.0 * M_PI / 180.0;
  165. const double sinArc = qSin( arc );
  166. const double cosArc = qCos( arc );
  167. const double x1 = cx + radius * sinArc;
  168. const double x2 = cx + ( radius + len ) * sinArc;
  169. const double y1 = cy - radius * cosArc;
  170. const double y2 = cy - ( radius + len ) * cosArc;
  171. QwtPainter::drawLine( painter, x1, y1, x2, y2 );
  172. }
  173. }
  174. /*!
  175. Draws the baseline of the scale
  176. \param painter Painter
  177. \sa drawTick(), drawLabel()
  178. */
  179. void QwtRoundScaleDraw::drawBackbone( QPainter *painter ) const
  180. {
  181. const double a1 = qMin( map().p1(), map().p2() ) - 90 * 16;
  182. const double a2 = qMax( map().p1(), map().p2() ) - 90 * 16;
  183. const double radius = d_data->radius;
  184. const double x = d_data->center.x() - radius;
  185. const double y = d_data->center.y() - radius;
  186. painter->drawArc( x, y, 2 * radius, 2 * radius,
  187. -a2, a2 - a1 + 1 ); // counterclockwise
  188. }
  189. /*!
  190. Calculate the extent of the scale
  191. The extent is the distcance between the baseline to the outermost
  192. pixel of the scale draw. radius() + extent() is an upper limit
  193. for the radius of the bounding circle.
  194. \param font Font used for painting the labels
  195. \sa setMinimumExtent(), minimumExtent()
  196. \warning The implemented algo is not too smart and
  197. calculates only an upper limit, that might be a
  198. few pixels too large
  199. */
  200. double QwtRoundScaleDraw::extent( const QFont &font ) const
  201. {
  202. double d = 0.0;
  203. if ( hasComponent( QwtAbstractScaleDraw::Labels ) )
  204. {
  205. const QwtScaleDiv &sd = scaleDiv();
  206. const QList<double> &ticks = sd.ticks( QwtScaleDiv::MajorTick );
  207. for ( uint i = 0; i < ( uint )ticks.count(); i++ )
  208. {
  209. const double value = ticks[i];
  210. if ( !sd.contains( value ) )
  211. continue;
  212. const QwtText label = tickLabel( font, value );
  213. if ( label.isEmpty() )
  214. continue;
  215. const double tval = map().transform( value );
  216. if ( ( tval < d_data->startAngle + 360 * 16 )
  217. && ( tval > d_data->startAngle - 360 * 16 ) )
  218. {
  219. const double arc = tval / 16.0 / 360.0 * 2 * M_PI;
  220. const QSizeF sz = label.textSize( font );
  221. const double off = qMax( sz.width(), sz.height() );
  222. double x = off * qSin( arc );
  223. double y = off * qCos( arc );
  224. const double dist = qSqrt( x * x + y * y );
  225. if ( dist > d )
  226. d = dist;
  227. }
  228. }
  229. }
  230. if ( hasComponent( QwtAbstractScaleDraw::Ticks ) )
  231. {
  232. d += majTickLength();
  233. }
  234. if ( hasComponent( QwtAbstractScaleDraw::Backbone ) )
  235. {
  236. const double pw = qMax( 1, penWidth() ); // penwidth can be zero
  237. d += pw;
  238. }
  239. if ( hasComponent( QwtAbstractScaleDraw::Labels ) &&
  240. ( hasComponent( QwtAbstractScaleDraw::Ticks ) ||
  241. hasComponent( QwtAbstractScaleDraw::Backbone ) ) )
  242. {
  243. d += spacing();
  244. }
  245. d = qMax( d, minimumExtent() );
  246. return d;
  247. }