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
|
<!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_counter.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_counter.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="comment">// vim: expandtab</span>
00011
00012 <span class="preprocessor">#include <qlayout.h></span>
00013 <span class="preprocessor">#include <qlineedit.h></span>
00014 <span class="preprocessor">#include <qevent.h></span>
00015 <span class="preprocessor">#include "qwt_counter.h"</span>
00016 <span class="preprocessor">#include "qwt_arrbtn.h"</span>
00017
<a name="l00027"></a><a class="code" href="class_qwt_counter.html#a0">00027</a> <a class="code" href="class_qwt_counter.html#a0">QwtCounter::QwtCounter</a>(QWidget *parent, <span class="keyword">const</span> <span class="keywordtype">char</span> *name ):
00028 QWidget(parent,name),
00029 d_blockKeys(FALSE),
00030 d_keyPressed(FALSE)
00031 {
00032 d_increment[Button1] = 1;
00033 d_increment[Button2] = 10;
00034 d_increment[Button3] = 100;
00035
00036 QHBoxLayout *layout = <span class="keyword">new</span> QHBoxLayout(<span class="keyword">this</span>);
00037 layout->setAutoAdd(TRUE);
00038
00039 <span class="keywordtype">int</span> i;
00040 <span class="keywordflow">for</span>(i = ButtonCnt - 1; i >= 0; i--)
00041 {
00042 <a class="code" href="class_qwt_arrow_button.html">QwtArrowButton</a> *btn =
00043 <span class="keyword">new</span> <a class="code" href="class_qwt_arrow_button.html">QwtArrowButton</a>(i+1, Qt::DownArrow,<span class="keyword">this</span>);
00044 btn->setFocusPolicy(QWidget::StrongFocus);
00045 btn->installEventFilter(<span class="keyword">this</span>);
00046
00047 connect(btn, SIGNAL(released()), SLOT(btnReleased()));
00048 connect(btn, SIGNAL(clicked()), SLOT(btnClicked()));
00049
00050 d_buttonDown[i] = btn;
00051 }
00052
00053 d_valueEdit = <span class="keyword">new</span> QLineEdit(<span class="keyword">this</span>);
00054 d_valueEdit->setReadOnly(TRUE);
00055 d_valueEdit->setFocusPolicy(QWidget::NoFocus);
00056
00057 layout->setStretchFactor(d_valueEdit, 10);
00058
00059 <span class="keywordflow">for</span>(i = 0; i < ButtonCnt; i++)
00060 {
00061 <a class="code" href="class_qwt_arrow_button.html">QwtArrowButton</a> *btn =
00062 <span class="keyword">new</span> <a class="code" href="class_qwt_arrow_button.html">QwtArrowButton</a>(i+1, Qt::UpArrow, <span class="keyword">this</span>);
00063 btn->setFocusPolicy(QWidget::StrongFocus);
00064 btn->installEventFilter(<span class="keyword">this</span>);
00065
00066 connect(btn, SIGNAL(released()), SLOT(btnReleased()));
00067 connect(btn, SIGNAL(clicked()), SLOT(btnClicked()));
00068
00069 d_buttonUp[i] = btn;
00070 }
00071
00072 <a class="code" href="class_qwt_counter.html#a14">setNumButtons</a>(2);
00073 <a class="code" href="class_qwt_dbl_range.html#a2">setRange</a>(0.0,1.0,0.001);
00074 <a class="code" href="class_qwt_counter.html#a18">setValue</a>(0.0);
00075 }
00076
<a name="l00078"></a><a class="code" href="class_qwt_counter.html#a21">00078</a> <span class="keywordtype">bool</span> <a class="code" href="class_qwt_counter.html#a21">QwtCounter::eventFilter</a>(QObject *object, QEvent *e)
00079 {
00080 <span class="keywordflow">if</span> ( object->inherits(<span class="stringliteral">"QPushButton"</span>) )
00081 {
00082 <span class="keywordflow">if</span> ( e->type() == QEvent::KeyPress )
00083 {
00084 <span class="keywordflow">if</span> ( !((QKeyEvent *)e)->isAutoRepeat() )
00085 d_keyPressed = TRUE;
00086 }
00087
00088 <span class="keywordflow">if</span> ( e->type() == QEvent::KeyRelease )
00089 {
00090 <span class="keywordflow">if</span> ( !((QKeyEvent *)e)->isAutoRepeat() )
00091 {
00092 d_keyPressed = FALSE;
00093
00094 <span class="comment">// Unblock key events. They might be blocked</span>
00095 <span class="comment">// to interrupt auto repeat, when we changed</span>
00096 <span class="comment">// the focus to a different button.</span>
00097
00098 d_blockKeys = FALSE;
00099 }
00100 }
00101 }
00102
00103 <span class="keywordflow">return</span> QWidget::eventFilter(object, e);
00104 }
00105
<a name="l00115"></a><a class="code" href="class_qwt_counter.html#a16">00115</a> <span class="keywordtype">void</span> <a class="code" href="class_qwt_counter.html#a16">QwtCounter::setIncSteps</a>(QwtCounter::Button btn, <span class="keywordtype">int</span> nSteps)
00116 {
00117 <span class="keywordflow">if</span> (( btn >= 0) && (btn < ButtonCnt))
00118 d_increment[btn] = nSteps;
00119 }
00120
<a name="l00127"></a><a class="code" href="class_qwt_counter.html#a17">00127</a> <span class="keywordtype">int</span> <a class="code" href="class_qwt_counter.html#a17">QwtCounter::incSteps</a>(QwtCounter::Button btn)<span class="keyword"> const</span>
00128 <span class="keyword"></span>{
00129 <span class="keywordflow">if</span> (( btn >= 0) && (btn < ButtonCnt))
00130 <span class="keywordflow">return</span> d_increment[btn];
00131
00132 <span class="keywordflow">return</span> 0;
00133 }
00134
<a name="l00142"></a><a class="code" href="class_qwt_counter.html#a18">00142</a> <span class="keywordtype">void</span> <a class="code" href="class_qwt_counter.html#a18">QwtCounter::setValue</a>(<span class="keywordtype">double</span> v)
00143 {
00144 <a class="code" href="class_qwt_dbl_range.html#a5">QwtDblRange::setValue</a>(v);
00145
00146 showNum(<a class="code" href="class_qwt_counter.html#a13">value</a>());
00147 updateButtons();
00148 }
00149
00153 <span class="keywordtype">void</span> <a class="code" href="class_qwt_dbl_range.html#b3">QwtCounter::valueChange</a>()
00154 {
00155 <span class="keywordflow">if</span> ( <a class="code" href="class_qwt_dbl_range.html#a4">isValid</a>() )
00156 showNum(<a class="code" href="class_qwt_counter.html#a13">value</a>());
00157 <span class="keywordflow">else</span>
00158 d_valueEdit->setText(QString::null);
00159
00160 updateButtons();
00161
00162 <span class="keywordflow">if</span> ( <a class="code" href="class_qwt_dbl_range.html#a4">isValid</a>() )
00163 emit <a class="code" href="class_qwt_counter.html#l1">valueChanged</a>(<a class="code" href="class_qwt_counter.html#a13">value</a>());
00164 }
00165
00174 <span class="keywordtype">void</span> QwtCounter::updateButtons()
00175 {
00176 <span class="keywordflow">if</span> ( <a class="code" href="class_qwt_dbl_range.html#a4">isValid</a>() )
00177 {
00178 <span class="comment">// 1. save enabled state of the smallest down- and up-button</span>
00179 <span class="comment">// 2. change enabled state on under- or over-flow</span>
00180 <span class="comment">// 3. switch focus if the enabled state has changed</span>
00181
00182 <span class="keywordtype">int</span> oldEnabledDown = d_buttonDown[0]->isEnabled();
00183 <span class="keywordtype">int</span> oldEnabledUp = d_buttonUp[0]->isEnabled();
00184
00185 <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 0; i < ButtonCnt; i++ )
00186 {
00187 d_buttonDown[i]->setEnabled(<a class="code" href="class_qwt_counter.html#a13">value</a>() > <a class="code" href="class_qwt_dbl_range.html#a12">minValue</a>());
00188 d_buttonUp[i]->setEnabled(<a class="code" href="class_qwt_counter.html#a13">value</a>() < <a class="code" href="class_qwt_dbl_range.html#a11">maxValue</a>());
00189 }
00190
00191 QPushButton *focusButton = NULL;
00192 <span class="keywordflow">if</span> (oldEnabledDown && !d_buttonDown[0]->isEnabled())
00193 focusButton = d_buttonUp[0];
00194
00195 <span class="keywordflow">if</span> (oldEnabledUp && !d_buttonUp[0]->isEnabled())
00196 focusButton = d_buttonDown[0];
00197
00198 <span class="keywordflow">if</span> ( focusButton )
00199 {
00200 focusButton->setFocus();
00201 <span class="keywordflow">if</span> ( d_keyPressed )
00202 {
00203 <span class="comment">// Stop auto repeat until the key has been released </span>
00204 d_blockKeys = TRUE;
00205 }
00206 }
00207 }
00208 <span class="keywordflow">else</span>
00209 {
00210 <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 0; i < ButtonCnt; i++ )
00211 {
00212 d_buttonDown[i]->setEnabled(FALSE);
00213 d_buttonUp[i]->setEnabled(FALSE);
00214 }
00215 }
00216 }
00217
<a name="l00222"></a><a class="code" href="class_qwt_counter.html#a14">00222</a> <span class="keywordtype">void</span> <a class="code" href="class_qwt_counter.html#a14">QwtCounter::setNumButtons</a>(<span class="keywordtype">int</span> n)
00223 {
00224 <span class="keywordflow">if</span> ( n<0 || n>ButtonCnt )
00225 <span class="keywordflow">return</span>;
00226
00227 <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 0; i < ButtonCnt; i++ )
00228 {
00229 <span class="keywordflow">if</span> ( i < n )
00230 {
00231 d_buttonDown[i]->show();
00232 d_buttonUp[i]->show();
00233 }
00234 <span class="keywordflow">else</span>
00235 {
00236 d_buttonDown[i]->hide();
00237 d_buttonUp[i]->hide();
00238 }
00239 }
00240
00241 d_nButtons = n;
00242 }
00243
<a name="l00247"></a><a class="code" href="class_qwt_counter.html#a15">00247</a> <span class="keywordtype">int</span> <a class="code" href="class_qwt_counter.html#a15">QwtCounter::numButtons</a>()<span class="keyword"> const </span>
00248 <span class="keyword"></span>{
00249 <span class="keywordflow">return</span> d_nButtons;
00250 }
00251
00253 <span class="keywordtype">void</span> QwtCounter::showNum(<span class="keywordtype">double</span> d)
00254 {
00255 QString v;
00256 v.setNum(d);
00257
00258 d_valueEdit->setText(v);
00259 d_valueEdit->setCursorPosition(0);
00260 }
00261
00263 <span class="keywordtype">void</span> QwtCounter::btnClicked()
00264 {
00265 <span class="keywordflow">if</span> (d_blockKeys)
00266 {
00267 <span class="comment">// When we set the focus to a different button,</span>
00268 <span class="comment">// auto repeat has been stopped until the key will</span>
00269 <span class="comment">// be released</span>
00270
00271 <span class="keywordflow">return</span>;
00272 }
00273
00274 <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 0; i < ButtonCnt; i++ )
00275 {
00276 <span class="keywordflow">if</span> ( d_buttonUp[i] == sender() )
00277 <a class="code" href="class_qwt_dbl_range.html#a14">incValue</a>(d_increment[i]);
00278
00279 <span class="keywordflow">if</span> ( d_buttonDown[i] == sender() )
00280 <a class="code" href="class_qwt_dbl_range.html#a14">incValue</a>(-d_increment[i]);
00281 }
00282 }
00283
00285 <span class="keywordtype">void</span> QwtCounter::btnReleased()
00286 {
00287 emit <a class="code" href="class_qwt_counter.html#l0">buttonReleased</a>(<a class="code" href="class_qwt_counter.html#a13">value</a>());
00288 }
00289
<a name="l00296"></a><a class="code" href="class_qwt_counter.html#b0">00296</a> <span class="keywordtype">void</span> <a class="code" href="class_qwt_counter.html#b0">QwtCounter::rangeChange</a>()
00297 {
00298 updateButtons();
00299 }
00300
<a name="l00308"></a><a class="code" href="class_qwt_counter.html#b1">00308</a> <span class="keywordtype">void</span> <a class="code" href="class_qwt_counter.html#b1">QwtCounter::fontChange</a>(<span class="keyword">const</span> QFont &f)
00309 {
00310 QWidget::fontChange( f );
00311 d_valueEdit->setFont(font());
00312 }
00313
<a name="l00315"></a><a class="code" href="class_qwt_counter.html#a20">00315</a> QSize <a class="code" href="class_qwt_counter.html#a20">QwtCounter::sizeHint</a>()<span class="keyword"> const</span>
00316 <span class="keyword"></span>{
00317 QString tmp;
00318 QFontMetrics fm(d_valueEdit->font());
00319
00320 <span class="keywordtype">int</span> w = fm.width(tmp.setNum(minValue()));
00321 <span class="keywordtype">int</span> w1 = fm.width(tmp.setNum(maxValue()));
00322 <span class="keywordflow">if</span> ( w1 > w )
00323 w = w1;
00324 w1 = fm.width(tmp.setNum(minValue() + <a class="code" href="class_qwt_counter.html#a1">step</a>()));
00325 <span class="keywordflow">if</span> ( w1 > w )
00326 w = w1;
00327 w1 = fm.width(tmp.setNum(maxValue() - <a class="code" href="class_qwt_counter.html#a1">step</a>()));
00328 <span class="keywordflow">if</span> ( w1 > w )
00329 w = w1;
00330
00331 <span class="comment">// QLineEdit::minimumSizeHint is for one char. Subtracting</span>
00332 <span class="comment">// the size for the char we get all the margins, frames ...</span>
00333
00334 <span class="preprocessor">#if QT_VERSION < 300</span>
00335 <span class="preprocessor"></span> w += d_valueEdit->minimumSizeHint().width() - fm.maxWidth();
00336 <span class="preprocessor">#else</span>
00337 <span class="preprocessor"></span> w += 2 * d_valueEdit->frameWidth() +
00338 d_valueEdit->fontMetrics().minRightBearing() + 3;
00339 <span class="preprocessor">#endif</span>
00340 <span class="preprocessor"></span>
00341 <span class="comment">// Now we replace default sizeHint contribution of d_valueEdit by</span>
00342 <span class="comment">// what we really need.</span>
00343
00344 w += QWidget::sizeHint().width() - d_valueEdit->sizeHint().width();
00345
00346 <span class="keywordflow">return</span> QSize(w, QWidget::sizeHint().height());
00347 }
00348
<a name="l00350"></a><a class="code" href="class_qwt_counter.html#a19">00350</a> QSizePolicy <a class="code" href="class_qwt_counter.html#a19">QwtCounter::sizePolicy</a>()<span class="keyword"> const</span>
00351 <span class="keyword"></span>{
00352 QSizePolicy sp;
00353 sp.setHorData( QSizePolicy::Preferred );
00354 sp.setVerData( QSizePolicy::Fixed );
00355 <span class="keywordflow">return</span> sp;
00356 }
</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>
|