1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Qwt User's Guide: qwt_knob.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3.8 -->
<div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Class List</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Class Members</a> | <a class="qindex" href="globals.html">File Members</a> | <a class="qindex" href="pages.html">Related Pages</a></div>
<h1>qwt_knob.cpp</h1><pre class="fragment"><div>00001 <span class="comment">/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************</span>
00002 <span class="comment"> * Qwt Widget Library</span>
00003 <span class="comment"> * Copyright (C) 1997 Josef Wilgen</span>
00004 <span class="comment"> * Copyright (C) 2002 Uwe Rathmann</span>
00005 <span class="comment"> *</span>
00006 <span class="comment"> * This library is free software; you can redistribute it and/or</span>
00007 <span class="comment"> * modify it under the terms of the Qwt License, Version 1.0</span>
00008 <span class="comment"> *****************************************************************************/</span>
00009
00010 <span class="preprocessor">#include <qpainter.h></span>
00011 <span class="preprocessor">#include <qpalette.h></span>
00012 <span class="preprocessor">#include <qstyle.h></span>
00013 <span class="preprocessor">#include <qevent.h></span>
00014 <span class="preprocessor">#include "qwt_knob.h"</span>
00015 <span class="preprocessor">#include "<a class="code" href="qwt__math_8h.html">qwt_math.h</a>"</span>
00016 <span class="preprocessor">#include "qwt_paint_buffer.h"</span>
00017
<a name="l00023"></a><a class="code" href="class_qwt_knob.html#a0">00023</a> <a class="code" href="class_qwt_knob.html#a0">QwtKnob::QwtKnob</a>(QWidget* parent, <span class="keyword">const</span> <span class="keywordtype">char</span> *name):
00024 <a class="code" href="class_qwt_slider_base.html">QwtSliderBase</a>(Qt::Horizontal, parent, name,
00025 Qt::WRepaintNoErase|Qt::WResizeNoErase)
00026 {
00027 d_angle = 0.0;
00028 d_oldAngle = 0.0;
00029 d_nTurns = 0.0;
00030 d_borderWidth = 2;
00031 d_borderDist = 4;
00032 d_totalAngle = 270.0;
00033 d_scaleDist = 4;
00034 d_hasScale = 0;
00035 d_symbol = Line;
00036 d_maxScaleTicks = 11;
00037 d_knobWidth = 50;
00038 d_dotWidth = 8;
00039
00040 <a class="code" href="class_qwt_scale_if.html#a11">scaleDraw</a>()-><a class="code" href="class_qwt_scale_draw.html#a6">setGeometry</a>(
00041 0, 0, d_knobWidth + 2 * d_scaleDist, QwtScaleDraw::Round );
00042 <a class="code" href="class_qwt_slider_base.html#a2">setUpdateTime</a>(50);
00043 <a class="code" href="class_qwt_knob.html#a3">setTotalAngle</a>( 270.0 );
00044 recalcAngle();
00045 setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
00046 }
00047
<a name="l00049"></a><a class="code" href="class_qwt_knob.html#a1">00049</a> <a class="code" href="class_qwt_knob.html#a1">QwtKnob::~QwtKnob</a>()
00050 {
00051 }
00052
<a name="l00057"></a><a class="code" href="class_qwt_knob.html#a8">00057</a> <span class="keywordtype">void</span> <a class="code" href="class_qwt_knob.html#a8">QwtKnob::setSymbol</a>(QwtKnob::Symbol s)
00058 {
00059 <span class="keywordflow">if</span> ( d_symbol != s )
00060 {
00061 d_symbol = s;
00062 update();
00063 }
00064 }
00065
<a name="l00070"></a><a class="code" href="class_qwt_knob.html#a9">00070</a> <a class="code" href="class_qwt_knob.html#w2">QwtKnob::Symbol</a> <a class="code" href="class_qwt_knob.html#a9">QwtKnob::symbol</a>()<span class="keyword"> const</span>
00071 <span class="keyword"></span>{
00072 <span class="keywordflow">return</span> d_symbol;
00073 }
00074
<a name="l00083"></a><a class="code" href="class_qwt_knob.html#a3">00083</a> <span class="keywordtype">void</span> <a class="code" href="class_qwt_knob.html#a3">QwtKnob::setTotalAngle</a> (<span class="keywordtype">double</span> angle)
00084 {
00085 <span class="keywordflow">if</span> (angle < 10.0)
00086 d_totalAngle = 10.0;
00087 <span class="keywordflow">else</span>
00088 d_totalAngle = angle;
00089 <a class="code" href="class_qwt_scale_if.html#a11">scaleDraw</a>()-><a class="code" href="class_qwt_scale_draw.html#a11">setAngleRange</a>( -0.5 * d_totalAngle, 0.5 * d_totalAngle);
00090 layoutKnob();
00091 }
00092
00093
<a name="l00099"></a><a class="code" href="class_qwt_knob.html#b3">00099</a> <span class="keywordtype">void</span> <a class="code" href="class_qwt_knob.html#b3">QwtKnob::drawKnob</a>(QPainter *p, <span class="keyword">const</span> QRect &r)
00100 {
00101 <span class="keyword">const</span> <span class="keywordtype">int</span> bw2 = d_borderWidth / 2;
00102
00103 QRect aRect(r.x() + bw2, r.y() + bw2,
00104 r.width() - 2 * bw2, r.height() - 2 * bw2);
00105
00106 <span class="comment">//</span>
00107 <span class="comment">// draw button face</span>
00108 <span class="comment">//</span>
00109 p->setBrush(colorGroup().brush(QColorGroup::Button));
00110 p->drawEllipse(aRect);
00111
00112 <span class="comment">//</span>
00113 <span class="comment">// draw button shades</span>
00114 <span class="comment">//</span>
00115 QPen pn;
00116 pn.setWidth(d_borderWidth);
00117
00118 pn.setColor(colorGroup().light());
00119 p->setPen(pn);
00120 p->drawArc(aRect, 45*16,180*16);
00121
00122 pn.setColor(colorGroup().dark());
00123 p->setPen(pn);
00124 p->drawArc(aRect, 225*16,180*16);
00125
00126 <span class="comment">//</span>
00127 <span class="comment">// draw marker</span>
00128 <span class="comment">//</span>
00129 <span class="keywordflow">if</span> ( <a class="code" href="class_qwt_slider_base.html#a10">isValid</a>() )
00130 <a class="code" href="class_qwt_knob.html#b4">drawMarker</a>(p, d_angle, colorGroup().buttonText());
00131 }
00132
00139 <span class="keywordtype">void</span> <a class="code" href="class_qwt_slider_base.html#b1">QwtKnob::valueChange</a>()
00140 {
00141 recalcAngle();
00142 update();
00143 <a class="code" href="class_qwt_slider_base.html#b1">QwtSliderBase::valueChange</a>();
00144 }
00145
00152 <span class="keywordtype">double</span> <a class="code" href="class_qwt_slider_base.html#b8">QwtKnob::getValue</a>(<span class="keyword">const</span> QPoint &p)
00153 {
00154 <span class="keyword">const</span> <span class="keywordtype">double</span> dx = double((rect().x() + rect().width() / 2) - p.x() );
00155 <span class="keyword">const</span> <span class="keywordtype">double</span> dy = double((rect().y() + rect().height() / 2) - p.y() );
00156
00157 <span class="keyword">const</span> <span class="keywordtype">double</span> arc = atan2(-dx,dy) * 180.0 / M_PI;
00158
00159 <span class="keywordtype">double</span> newValue = 0.5 * (<a class="code" href="class_qwt_dbl_range.html#a12">minValue</a>() + <a class="code" href="class_qwt_dbl_range.html#a11">maxValue</a>())
00160 + (arc + d_nTurns * 360.0) * (<a class="code" href="class_qwt_dbl_range.html#a11">maxValue</a>() - <a class="code" href="class_qwt_dbl_range.html#a12">minValue</a>())
00161 / d_totalAngle;
00162
00163 <span class="keyword">const</span> <span class="keywordtype">double</span> oneTurn = fabs(<a class="code" href="class_qwt_dbl_range.html#a11">maxValue</a>() - <a class="code" href="class_qwt_dbl_range.html#a12">minValue</a>()) * 360.0 / d_totalAngle;
00164 <span class="keyword">const</span> <span class="keywordtype">double</span> eqValue = <a class="code" href="class_qwt_dbl_range.html#a6">value</a>() + d_mouseOffset;
00165
00166 <span class="keywordflow">if</span> (fabs(newValue - eqValue) > 0.5 * oneTurn)
00167 {
00168 <span class="keywordflow">if</span> (newValue < eqValue)
00169 newValue += oneTurn;
00170 <span class="keywordflow">else</span>
00171 newValue -= oneTurn;
00172 }
00173
00174 <span class="keywordflow">return</span> newValue;
00175 }
00176
00183 <span class="keywordtype">void</span> <a class="code" href="class_qwt_slider_base.html#b9">QwtKnob::getScrollMode</a>(<span class="keyword">const</span> QPoint &p, <span class="keywordtype">int</span> &scrollMode, <span class="keywordtype">int</span> &direction)
00184 {
00185 <span class="keyword">const</span> <span class="keywordtype">int</span> r = d_kRect.width() / 2;
00186
00187 <span class="keyword">const</span> <span class="keywordtype">int</span> dx = d_kRect.x() + r - p.x();
00188 <span class="keyword">const</span> <span class="keywordtype">int</span> dy = d_kRect.y() + r - p.y();
00189
00190 <span class="keywordflow">if</span> ( (dx * dx) + (dy * dy) <= (r * r)) <span class="comment">// point is inside the knob</span>
00191 {
00192 scrollMode = ScrMouse;
00193 direction = 0;
00194 }
00195 <span class="keywordflow">else</span> <span class="comment">// point lies outside</span>
00196 {
00197 scrollMode = ScrTimer;
00198 <span class="keywordtype">double</span> arc = atan2(<span class="keywordtype">double</span>(-dx),<span class="keywordtype">double</span>(dy)) * 180.0 / M_PI;
00199 <span class="keywordflow">if</span> ( arc < d_angle)
00200 direction = -1;
00201 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (arc > d_angle)
00202 direction = 1;
00203 <span class="keywordflow">else</span>
00204 direction = 0;
00205 }
00206 }
00207
00208
00214 <span class="keywordtype">void</span> <a class="code" href="class_qwt_dbl_range.html#b5">QwtKnob::rangeChange</a>()
00215 {
00216 <span class="keywordflow">if</span> (!<a class="code" href="class_qwt_scale_if.html#b0">hasUserScale</a>())
00217 {
00218 <a class="code" href="class_qwt_scale_if.html#a11">scaleDraw</a>()-><a class="code" href="class_qwt_scale_draw.html#a2">setScale</a>(<a class="code" href="class_qwt_dbl_range.html#a12">minValue</a>(), <a class="code" href="class_qwt_dbl_range.html#a11">maxValue</a>(),
00219 <a class="code" href="class_qwt_scale_if.html#a9">scaleMaxMajor</a>(), <a class="code" href="class_qwt_scale_if.html#a8">scaleMaxMinor</a>());
00220 }
00221 layoutKnob();
00222 recalcAngle();
00223 }
00224
<a name="l00228"></a><a class="code" href="class_qwt_knob.html#b1">00228</a> <span class="keywordtype">void</span> <a class="code" href="class_qwt_knob.html#b1">QwtKnob::resizeEvent</a>(QResizeEvent *)
00229 {
00230 layoutKnob( FALSE );
00231 }
00232
00234 <span class="comment">// the current rect and fonts.</span>
00235 <span class="comment">// \param update_geometry notify the layout system and call update</span>
00236 <span class="comment">// to redraw the scale</span>
00237 <span class="keywordtype">void</span> QwtKnob::layoutKnob( <span class="keywordtype">bool</span> update_geometry )
00238 {
00239 <span class="keyword">const</span> QRect &r = rect();
00240
00241 <span class="keyword">const</span> <span class="keywordtype">int</span> width = qwtMin(qwtMin(r.height(), r.width()), d_knobWidth);
00242 <span class="keyword">const</span> <span class="keywordtype">int</span> width_2 = width / 2;
00243
00244 d_kRect.setRect(r.x() + r.width() / 2 - width_2,
00245 r.y() + r.height() / 2 - width_2,
00246 width, width);
00247
00248 <a class="code" href="class_qwt_scale_if.html#a11">scaleDraw</a>()-><a class="code" href="class_qwt_scale_draw.html#a6">setGeometry</a>(d_kRect.x() - d_scaleDist,
00249 d_kRect.y() - d_scaleDist,
00250 width + 2 * d_scaleDist, QwtScaleDraw::Round );
00251
00252 <span class="keywordflow">if</span> ( update_geometry )
00253 {
00254 updateGeometry();
00255 update();
00256 }
00257 }
00258
<a name="l00262"></a><a class="code" href="class_qwt_knob.html#b0">00262</a> <span class="keywordtype">void</span> <a class="code" href="class_qwt_knob.html#b0">QwtKnob::paintEvent</a>(QPaintEvent *e)
00263 {
00264 <span class="keyword">const</span> QRect &ur = e->rect();
00265 <span class="keywordflow">if</span> ( ur.isValid() )
00266 {
00267 <a class="code" href="class_qwt_paint_buffer.html">QwtPaintBuffer</a> paintBuffer(<span class="keyword">this</span>, ur);
00268 <a class="code" href="class_qwt_knob.html#b2">draw</a>(paintBuffer.<a class="code" href="class_qwt_paint_buffer.html#a5">painter</a>(), ur);
00269 }
00270 }
00271
<a name="l00275"></a><a class="code" href="class_qwt_knob.html#b2">00275</a> <span class="keywordtype">void</span> <a class="code" href="class_qwt_knob.html#b2">QwtKnob::draw</a>(QPainter *painter, <span class="keyword">const</span> QRect& ur)
00276 {
00277 <span class="keywordflow">if</span> ( !d_kRect.contains( ur ) ) <span class="comment">// event from valueChange()</span>
00278 <a class="code" href="class_qwt_scale_if.html#a11">scaleDraw</a>()-><a class="code" href="class_qwt_scale_draw.html#a29">draw</a>( painter );
00279 <a class="code" href="class_qwt_knob.html#b3">drawKnob</a>( painter, d_kRect );
00280
00281 <span class="keywordflow">if</span> ( hasFocus() )
00282 {
00283 QRect r = rect();
00284
00285 <span class="preprocessor">#if QT_VERSION < 300</span>
00286 <span class="preprocessor"></span> style().drawFocusRect(painter, r, colorGroup());
00287 <span class="preprocessor">#else</span>
00288 <span class="preprocessor"></span> style().drawPrimitive(QStyle::PE_FocusRect, painter,
00289 r, colorGroup());
00290 <span class="preprocessor">#endif</span>
00291 <span class="preprocessor"></span> }
00292
00293 }
00294
<a name="l00301"></a><a class="code" href="class_qwt_knob.html#b4">00301</a> <span class="keywordtype">void</span> <a class="code" href="class_qwt_knob.html#b4">QwtKnob::drawMarker</a>(QPainter *p, <span class="keywordtype">double</span> arc, <span class="keyword">const</span> QColor &c)
00302 {
00303 <span class="keyword">const</span> <span class="keywordtype">double</span> rarc = arc * M_PI / 180.0;
00304 <span class="keyword">const</span> <span class="keywordtype">double</span> ca = cos(rarc);
00305 <span class="keyword">const</span> <span class="keywordtype">double</span> sa = - sin(rarc);
00306
00307 <span class="keywordtype">int</span> radius = d_kRect.width() / 2 - d_borderWidth;
00308 <span class="keywordflow">if</span> (radius < 3)
00309 radius = 3;
00310
00311 <span class="keyword">const</span> <span class="keywordtype">int</span> ym = d_kRect.y() + radius + d_borderWidth;
00312 <span class="keyword">const</span> <span class="keywordtype">int</span> xm = d_kRect.x() + radius + d_borderWidth;
00313
00314 <span class="keywordflow">switch</span> (d_symbol)
00315 {
00316 <span class="keywordflow">case</span> Dot:
00317 {
00318 p->setBrush(c);
00319 p->setPen(Qt::NoPen);
00320
00321 <span class="keyword">const</span> <span class="keywordtype">double</span> rb = double(qwtMax(radius - 4 - d_dotWidth / 2, 0));
00322 p->drawEllipse(xm - <span class="keywordtype">int</span>(floor (sa * rb + 0.5)) - d_dotWidth / 2,
00323 ym - <span class="keywordtype">int</span>(floor (ca * rb + 0.5)) - d_dotWidth / 2,
00324 d_dotWidth, d_dotWidth);
00325 <span class="keywordflow">break</span>;
00326 }
00327 <span class="keywordflow">case</span> Line:
00328 {
00329 p->setPen(QPen(c, 2));
00330
00331 <span class="keyword">const</span> <span class="keywordtype">double</span> rb = qwtMax(<span class="keywordtype">double</span>((radius - 4) / 3.0), 0.0);
00332 <span class="keyword">const</span> <span class="keywordtype">double</span> re = qwtMax(<span class="keywordtype">double</span>(radius - 4), 0.0);
00333
00334 p->drawLine (xm - <span class="keywordtype">int</span> (floor (sa * rb + 0.5)),
00335 ym - <span class="keywordtype">int</span> (floor (ca * rb + 0.5)),
00336 xm - <span class="keywordtype">int</span> (floor (sa * re + 0.5)),
00337 ym - <span class="keywordtype">int</span> (floor (ca * re + 0.5)));
00338
00339 <span class="keywordflow">break</span>;
00340 }
00341 }
00342 }
00343
<a name="l00350"></a><a class="code" href="class_qwt_knob.html#a2">00350</a> <span class="keywordtype">void</span> <a class="code" href="class_qwt_knob.html#a2">QwtKnob::setKnobWidth</a>(<span class="keywordtype">int</span> w)
00351 {
00352 d_knobWidth = qwtMax(w,5);
00353 layoutKnob();
00354 }
00355
<a name="l00360"></a><a class="code" href="class_qwt_knob.html#a4">00360</a> <span class="keywordtype">void</span> <a class="code" href="class_qwt_knob.html#a4">QwtKnob::setBorderWidth</a>(<span class="keywordtype">int</span> bw)
00361 {
00362 d_borderWidth = qwtMax(bw, 0);
00363 layoutKnob();
00364 }
00365
00370 <span class="keywordtype">void</span> QwtKnob::recalcAngle()
00371 {
00372 d_oldAngle = d_angle;
00373
00374 <span class="comment">//</span>
00375 <span class="comment">// calculate the angle corresponding to the value</span>
00376 <span class="comment">//</span>
00377 <span class="keywordflow">if</span> (<a class="code" href="class_qwt_dbl_range.html#a11">maxValue</a>() == <a class="code" href="class_qwt_dbl_range.html#a12">minValue</a>())
00378 {
00379 d_angle = 0;
00380 d_nTurns = 0;
00381 }
00382 <span class="keywordflow">else</span>
00383 {
00384 d_angle = (<a class="code" href="class_qwt_dbl_range.html#a6">value</a>() - 0.5 * (<a class="code" href="class_qwt_dbl_range.html#a12">minValue</a>() + <a class="code" href="class_qwt_dbl_range.html#a11">maxValue</a>()))
00385 / (<a class="code" href="class_qwt_dbl_range.html#a11">maxValue</a>() - <a class="code" href="class_qwt_dbl_range.html#a12">minValue</a>()) * d_totalAngle;
00386 d_nTurns = floor((d_angle + 180.0) / 360.0);
00387 d_angle = d_angle - d_nTurns * 360.0;
00388 }
00389 }
00390
00391
00396 <span class="keywordtype">void</span> <a class="code" href="class_qwt_scale_if.html#b1">QwtKnob::scaleChange</a>()
00397 {
00398 layoutKnob();
00399 }
00400
00405 <span class="keywordtype">void</span> QwtKnob::fontChange(<span class="keyword">const</span> QFont &f)
00406 {
00407 QwtSliderBase::fontChange( f );
00408 layoutKnob();
00409 }
00410
<a name="l00414"></a><a class="code" href="class_qwt_knob.html#a10">00414</a> QSize <a class="code" href="class_qwt_knob.html#a10">QwtKnob::sizeHint</a>()<span class="keyword"> const</span>
00415 <span class="keyword"></span>{
00416 <span class="keywordflow">return</span> <a class="code" href="class_qwt_knob.html#a11">minimumSizeHint</a>();
00417 }
00418
<a name="l00424"></a><a class="code" href="class_qwt_knob.html#a11">00424</a> QSize <a class="code" href="class_qwt_knob.html#a11">QwtKnob::minimumSizeHint</a>()<span class="keyword"> const</span>
00425 <span class="keyword"></span>{
00426 <span class="comment">// Add the scale radial thickness to the knobWidth</span>
00427 <span class="keyword">const</span> <span class="keywordtype">int</span> sh = <a class="code" href="class_qwt_scale_if.html#a11">scaleDraw</a>()-><a class="code" href="class_qwt_scale_draw.html#a22">minHeight</a>( QPen(), fontMetrics() );
00428 <span class="keyword">const</span> <span class="keywordtype">int</span> d = 2 * sh + 2 * d_scaleDist + d_knobWidth;
00429
00430 <span class="keywordflow">return</span> QSize( d, d );
00431 }
00432
00433 <span class="comment">// Local Variables:</span>
00434 <span class="comment">// mode: C++</span>
00435 <span class="comment">// c-file-style: "stroustrup"</span>
00436 <span class="comment">// indent-tabs-mode: nil</span>
00437 <span class="comment">// End:</span>
</div></pre><hr size="1"><address style="align: right;"><small>Generated on Tue Nov 16 21:12:20 2004 for Qwt User's Guide by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 ></a> 1.3.8 </small></address>
</body>
</html>
|