Q3RangeControl Class Reference

#include <q3rangecontrol.h>

List of all members.


Detailed Description

The Q3RangeControl class provides an integer value within a range.

Although originally designed for the QScrollBar widget, the Q3RangeControl can also be used in conjunction with other widgets such as QSlider and QSpinBox. Here are the five main concepts in the class:

1

{Current value} The bounded integer that Q3RangeControl maintains. value() returns it, and several functions, including setValue(), set it.

{Minimum} The lowest value that value() can ever return. Returned by minValue() and set by setRange() or one of the constructors.

{Maximum} The highest value that value() can ever return. Returned by maxValue() and set by setRange() or one of the constructors.

{Line step} The smaller of two natural steps that Q3RangeControl provides and typically corresponds to the user pressing an arrow key. The line step is returned by lineStep() and set using setSteps(). The functions addLine() and subtractLine() respectively increment and decrement the current value by lineStep().

{Page step} The larger of two natural steps that Q3RangeControl provides and typically corresponds to the user pressing PageUp or PageDown. The page step is returned by pageStep() and set using setSteps(). The functions addPage() and substractPage() respectively increment and decrement the current value by pageStep().

Unity (1) may be viewed as a third step size. setValue() lets you set the current value to any integer in the allowed range, not just minValue() + n * lineStep() for integer values of n. Some widgets may allow the user to set any value at all; others may just provide multiples of lineStep() or pageStep().

Q3RangeControl provides three virtual functions that are well suited for updating the on-screen representation of range controls and emitting signals: valueChange(), rangeChange() and stepChange().

Q3RangeControl also provides a function called bound() which lets you force arbitrary integers to be within the allowed range of the range control.

We recommend that all widgets that inherit Q3RangeControl provide at least a signal called valueChanged(); many widgets will want to provide addStep(), addPage(), substractStep() and substractPage() as slots.

Note that you must use multiple inheritance if you plan to implement a widget using Q3RangeControl because Q3RangeControl is not derived from QWidget.

Definition at line 38 of file q3rangecontrol.h.

Public Member Functions

 Q3RangeControl ()
 Q3RangeControl (int minValue, int maxValue, int lineStep, int pageStep, int value)
virtual ~Q3RangeControl ()
int value () const
void setValue (int)
void addPage ()
void subtractPage ()
void addLine ()
void subtractLine ()
int minValue () const
int maxValue () const
void setRange (int minValue, int maxValue)
void setMinValue (int minVal)
void setMaxValue (int minVal)
int lineStep () const
int pageStep () const
void setSteps (int line, int page)
int bound (int) const

Protected Member Functions

int positionFromValue (int val, int space) const
int valueFromPosition (int pos, int space) const
void directSetValue (int val)
int prevValue () const
virtual void valueChange ()
virtual void rangeChange ()
virtual void stepChange ()

Private Attributes

int minVal
int maxVal
int line
int page
int val
int prevVal
Q3RangeControlPrivate * d


Constructor & Destructor Documentation

Q3RangeControl::Q3RangeControl (  ) 

Constructs a range control with a minimum value of 0, maximum value of 99, line step of 1, page step of 10 and initial value 0.

Definition at line 101 of file q3rangecontrol.cpp.

References d, line, maxVal, minVal, page, prevVal, and val.

00102 {
00103     minVal  = 0;
00104     maxVal  = 99;
00105     line    = 1;
00106     page    = 10;
00107     val            = 0;
00108     prevVal = -1;
00109     d            = 0;
00110 }

Q3RangeControl::Q3RangeControl ( int  minValue,
int  maxValue,
int  lineStep,
int  pageStep,
int  value 
)

Constructs a range control whose value can never be smaller than minValue or greater than maxValue, whose line step size is lineStep and page step size is pageStep and whose value is initially value (which is guaranteed to be in range using bound()).

Definition at line 120 of file q3rangecontrol.cpp.

References bound(), d, line, maxVal, minVal, page, prevVal, and val.

00123 {
00124     minVal  = minValue;
00125     maxVal  = maxValue;
00126     line    = QABS(lineStep);
00127     page    = QABS(pageStep);
00128     prevVal = minVal - 1;
00129     val            = bound(value);
00130     d            = 0;
00131 }

Here is the call graph for this function:

Q3RangeControl::~Q3RangeControl (  )  [virtual]

Destroys the range control

Definition at line 137 of file q3rangecontrol.cpp.

00138 {
00139 }


Member Function Documentation

int Q3RangeControl::value (  )  const [inline]

Returns the current range control value. This is guaranteed to be within the range [minValue(), maxValue()].

See also:
setValue() prevValue()

Definition at line 87 of file q3rangecontrol.h.

References val.

Referenced by addLine(), addPage(), subtractLine(), and subtractPage().

00088 { return val; }

void Q3RangeControl::setValue ( int  value  ) 

Sets the range control's value to value and forces it to be within the legal range.

Calls the virtual valueChange() function if the new value is different from the previous value. The old value can still be retrieved using prevValue().

See also:
value()

Definition at line 179 of file q3rangecontrol.cpp.

References directSetValue(), prevVal, val, and valueChange().

Referenced by addLine(), addPage(), subtractLine(), and subtractPage().

00180 {
00181     directSetValue(value);
00182     if (prevVal != val)
00183         valueChange();
00184 }

Here is the call graph for this function:

void Q3RangeControl::addPage (  ) 

Equivalent to {setValue(value() + pageStep())}.

If the value is changed, then valueChange() is called.

See also:
subtractPage() addLine() setValue()

Definition at line 215 of file q3rangecontrol.cpp.

References pageStep(), setValue(), and value().

00216 {
00217     setValue(value() + pageStep());
00218 }

Here is the call graph for this function:

void Q3RangeControl::subtractPage (  ) 

Equivalent to {setValue(value() - pageStep())}.

If the value is changed, then valueChange() is called.

See also:
addPage() subtractLine() setValue()

Definition at line 228 of file q3rangecontrol.cpp.

References pageStep(), setValue(), and value().

00229 {
00230     setValue(value() - pageStep());
00231 }

Here is the call graph for this function:

void Q3RangeControl::addLine (  ) 

Equivalent to {setValue(value() + lineStep())}.

If the value is changed, then valueChange() is called.

See also:
subtractLine() addPage() setValue()

Definition at line 241 of file q3rangecontrol.cpp.

References lineStep(), setValue(), and value().

00242 {
00243     setValue(value() + lineStep());
00244 }

Here is the call graph for this function:

void Q3RangeControl::subtractLine (  ) 

Equivalent to {setValue(value() - lineStep())}.

If the value is changed, then valueChange() is called.

See also:
addLine() subtractPage() setValue()

Definition at line 254 of file q3rangecontrol.cpp.

References lineStep(), setValue(), and value().

00255 {
00256     setValue(value() - lineStep());
00257 }

Here is the call graph for this function:

int Q3RangeControl::minValue (  )  const [inline]

Returns the minimum value of the range.

See also:
setMinValue() setRange() maxValue()

Definition at line 93 of file q3rangecontrol.h.

References minVal.

Referenced by positionFromValue(), setMaxValue(), and valueFromPosition().

00094 { return minVal; }

int Q3RangeControl::maxValue (  )  const [inline]

Returns the maximum value of the range.

See also:
setMaxValue() setRange() minValue()

Definition at line 96 of file q3rangecontrol.h.

References maxVal.

Referenced by positionFromValue(), setMinValue(), and valueFromPosition().

00097 { return maxVal; }

void Q3RangeControl::setRange ( int  minValue,
int  maxValue 
)

Sets the range control's minimum value to minValue and its maximum value to maxValue.

Calls the virtual rangeChange() function if one or both of the new minimum and maximum values are different from the previous setting. Calls the virtual valueChange() function if the current value is adjusted because it was outside the new range.

If maxValue is smaller than minValue, minValue becomes the only legal value.

See also:
minValue() maxValue()

Definition at line 323 of file q3rangecontrol.cpp.

References bound(), maxVal, minVal, prevVal, qWarning(), rangeChange(), val, and valueChange().

Referenced by setMaxValue(), and setMinValue().

00324 {
00325     if (minValue > maxValue) {
00326         qWarning("Q3RangeControl::setRange: minValue %d > maxValue %d",
00327                   minValue, maxValue);
00328         maxValue = minValue;
00329     }
00330     if (minValue == minVal && maxValue == maxVal)
00331         return;
00332     minVal = minValue;
00333     maxVal = maxValue;
00334     int tmp = bound(val);
00335     rangeChange();
00336     if (tmp != val) {
00337         prevVal = val;
00338         val = tmp;
00339         valueChange();
00340     }
00341 }

Here is the call graph for this function:

void Q3RangeControl::setMinValue ( int  minVal  ) 

Sets the minimum value of the range to minVal.

If necessary, the maxValue() is adjusted so that the range remains valid.

See also:
minValue() setMaxValue()

Definition at line 284 of file q3rangecontrol.cpp.

References maxVal, maxValue(), and setRange().

00285 {
00286     int maxVal = maxValue();
00287     if (maxVal < minVal)
00288         maxVal = minVal;
00289     setRange(minVal, maxVal);
00290 }

Here is the call graph for this function:

void Q3RangeControl::setMaxValue ( int  maxVal  ) 

Sets the minimum value of the range to maxVal.

If necessary, the minValue() is adjusted so that the range remains valid.

See also:
maxValue() setMinValue()

Definition at line 300 of file q3rangecontrol.cpp.

References minVal, minValue(), and setRange().

00301 {
00302     int minVal = minValue();
00303     if (minVal > maxVal)
00304         minVal = maxVal;
00305     setRange(minVal, maxVal);
00306 }

Here is the call graph for this function:

int Q3RangeControl::lineStep (  )  const [inline]

Returns the line step.

See also:
setSteps() pageStep()

Definition at line 99 of file q3rangecontrol.h.

References line.

Referenced by addLine(), and subtractLine().

00100 { return line; }

int Q3RangeControl::pageStep (  )  const [inline]

Returns the page step.

See also:
setSteps() lineStep()

Definition at line 102 of file q3rangecontrol.h.

References page.

Referenced by addPage(), and subtractPage().

00103 { return page; }

void Q3RangeControl::setSteps ( int  lineStep,
int  pageStep 
)

Sets the range's line step to lineStep and page step to pageStep.

Calls the virtual stepChange() function if the new line step or page step are different from the previous settings.

See also:
lineStep() pageStep() setRange()

Definition at line 370 of file q3rangecontrol.cpp.

References line, page, and stepChange().

00371 {
00372     if (lineStep != line || pageStep != page) {
00373         line = QABS(lineStep);
00374         page = QABS(pageStep);
00375         stepChange();
00376     }
00377 }

Here is the call graph for this function:

int Q3RangeControl::bound ( int  v  )  const

Forces the value v to be within the range from minValue() to maxValue() inclusive, and returns the result.

This function is provided so that you can easily force other numbers than value() into the allowed range. You do not need to call it in order to use Q3RangeControl itself.

See also:
setValue() value() minValue() maxValue()

Definition at line 439 of file q3rangecontrol.cpp.

References maxVal, and minVal.

Referenced by directSetValue(), Q3RangeControl(), and setRange().

00440 {
00441     if (v < minVal)
00442         return minVal;
00443     if (v > maxVal)
00444         return maxVal;
00445     return v;
00446 }

int Q3RangeControl::positionFromValue ( int  logical_val,
int  span 
) const [protected]

Converts logical_val to a pixel position. minValue() maps to 0, maxValue() maps to span and other values are distributed evenly in-between.

This function can handle the entire integer range without overflow, providing span is <= 4096.

Calling this method is useful when actually drawing a range control such as a QScrollBar on-screen.

See also:
valueFromPosition()

Definition at line 463 of file q3rangecontrol.cpp.

References maxValue(), minValue(), p, and scale().

00464 {
00465     if (span <= 0 || logical_val < minValue() || maxValue() <= minValue())
00466         return 0;
00467     if (logical_val > maxValue())
00468         return span;
00469 
00470     uint range = maxValue() - minValue();
00471     uint p = logical_val - minValue();
00472 
00473     if (range > (uint)INT_MAX/4096) {
00474         const int scale = 4096*2;
00475         return ((p/scale) * span) / (range/scale);
00476         // ### the above line is probably not 100% correct
00477         // ### but fixing it isn't worth the extreme pain...
00478     } else if (range > (uint)span) {
00479         return (2*p*span + range) / (2*range);
00480     } else {
00481         uint div = span / range;
00482         uint mod = span % range;
00483         return p*div + (2*p*mod + range) / (2*range);
00484     }
00485     //equiv. to (p*span)/range + 0.5
00486     // no overflow because of this implicit assumption:
00487     // span <= 4096
00488 }

Here is the call graph for this function:

int Q3RangeControl::valueFromPosition ( int  pos,
int  span 
) const [protected]

Converts the pixel position pos to a value. 0 maps to minValue(), span maps to maxValue() and other values are distributed evenly in-between.

This function can handle the entire integer range without overflow.

Calling this method is useful if you actually implemented a range control widget such as QScrollBar and want to handle mouse press events. This function then maps screen coordinates to the logical values.

See also:
positionFromValue()

Definition at line 507 of file q3rangecontrol.cpp.

References maxValue(), and minValue().

00508 {
00509     if (span <= 0 || pos <= 0)
00510         return minValue();
00511     if (pos >= span)
00512         return maxValue();
00513 
00514     uint range = maxValue() - minValue();
00515 
00516     if ((uint)span > range)
00517         return  minValue() + (2*pos*range + span) / (2*span);
00518     else {
00519         uint div = range / span;
00520         uint mod = range % span;
00521         return  minValue() + pos*div + (2*pos*mod + span) / (2*span);
00522     }
00523     // equiv. to minValue() + (pos*range)/span + 0.5
00524     // no overflow because of this implicit assumption:
00525     // pos <= span < sqrt(INT_MAX+0.0625)+0.25 ~ sqrt(INT_MAX)
00526 }

Here is the call graph for this function:

void Q3RangeControl::directSetValue ( int  value  )  [protected]

Sets the range control value directly without calling valueChange().

Forces the new value to be within the legal range.

You will rarely have to call this function. However, if you want to change the range control's value inside the overloaded method valueChange(), setValue() would call the function valueChange() again. To avoid this recursion you must use directSetValue() instead.

See also:
setValue()

Definition at line 201 of file q3rangecontrol.cpp.

References bound(), prevVal, and val.

Referenced by setValue().

00202 {
00203     prevVal = val;
00204     val = bound(value);
00205 }

Here is the call graph for this function:

int Q3RangeControl::prevValue (  )  const [inline, protected]

Returns the previous value of the range control. "Previous value" means the value before the last change occurred. Setting a new range may affect the value, too, because the value is forced to be inside the specified range. When the range control is initially created, this is the same as value().

prevValue() can be outside the current legal range if a call to setRange() causes the current value to change. For example, if the range was [0, 1000] and the current value is 500, setRange(0, 400) makes value() return 400 and prevValue() return 500.

See also:
value() setRange()

Definition at line 90 of file q3rangecontrol.h.

References prevVal.

00091 { return prevVal; }

void Q3RangeControl::valueChange (  )  [protected, virtual]

This virtual function is called whenever the range control value changes. You can reimplement it if you want to be notified when the value changes. The default implementation does nothing.

Note that this method is called after the value has changed. The previous value can be retrieved using prevValue().

See also:
setValue(), addPage(), subtractPage(), addLine(), subtractLine() rangeChange(), stepChange()

Definition at line 392 of file q3rangecontrol.cpp.

Referenced by setRange(), and setValue().

00393 {
00394 }

void Q3RangeControl::rangeChange (  )  [protected, virtual]

This virtual function is called whenever the range control's range changes. You can reimplement it if you want to be notified when the range changes. The default implementation does nothing.

Note that this method is called after the range has changed.

See also:
setRange(), valueChange(), stepChange()

Definition at line 407 of file q3rangecontrol.cpp.

Referenced by setRange().

00408 {
00409 }

void Q3RangeControl::stepChange (  )  [protected, virtual]

This virtual function is called whenever the range control's line or page step settings change. You can reimplement it if you want to be notified when the step changes. The default implementation does nothing.

Note that this method is called after a step setting has changed.

See also:
setSteps(), rangeChange(), valueChange()

Definition at line 423 of file q3rangecontrol.cpp.

Referenced by setSteps().

00424 {
00425 }


Member Data Documentation

int Q3RangeControl::minVal [private]

Definition at line 76 of file q3rangecontrol.h.

Referenced by bound(), minValue(), Q3RangeControl(), setMaxValue(), and setRange().

int Q3RangeControl::maxVal [private]

Definition at line 76 of file q3rangecontrol.h.

Referenced by bound(), maxValue(), Q3RangeControl(), setMinValue(), and setRange().

int Q3RangeControl::line [private]

Definition at line 77 of file q3rangecontrol.h.

Referenced by lineStep(), Q3RangeControl(), and setSteps().

int Q3RangeControl::page [private]

Definition at line 77 of file q3rangecontrol.h.

Referenced by pageStep(), Q3RangeControl(), and setSteps().

int Q3RangeControl::val [private]

Definition at line 78 of file q3rangecontrol.h.

Referenced by directSetValue(), Q3RangeControl(), setRange(), setValue(), and value().

int Q3RangeControl::prevVal [private]

Definition at line 78 of file q3rangecontrol.h.

Referenced by directSetValue(), prevValue(), Q3RangeControl(), setRange(), and setValue().

Q3RangeControlPrivate* Q3RangeControl::d [private]

Definition at line 80 of file q3rangecontrol.h.

Referenced by Q3RangeControl().


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 16:15:21 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1