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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_abstract_scale.h"
#include "qwt_scale_engine.h"
#include "qwt_scale_draw.h"
#include "qwt_scale_div.h"
#include "qwt_scale_map.h"
#include "qwt_interval.h"
class QwtAbstractScale::PrivateData
{
public:
PrivateData():
maxMajor( 5 ),
maxMinor( 3 ),
stepSize( 0.0 )
{
scaleEngine = new QwtLinearScaleEngine();
scaleDraw = new QwtScaleDraw();
}
~PrivateData()
{
delete scaleEngine;
delete scaleDraw;
}
QwtScaleEngine *scaleEngine;
QwtAbstractScaleDraw *scaleDraw;
int maxMajor;
int maxMinor;
double stepSize;
};
/*!
Constructor
\param parent Parent widget
Creates a default QwtScaleDraw and a QwtLinearScaleEngine.
The initial scale boundaries are set to [ 0.0, 100.0 ]
The scaleStepSize() is initialized to 0.0, scaleMaxMajor() to 5
and scaleMaxMajor to 3.
*/
QwtAbstractScale::QwtAbstractScale( QWidget *parent ):
QWidget( parent )
{
d_data = new PrivateData;
rescale( 0.0, 100.0, d_data->stepSize );
}
//! Destructor
QwtAbstractScale::~QwtAbstractScale()
{
delete d_data;
}
/*!
Set the lower bound of the scale
\param value Lower bound
\sa lowerBound(), setScale(), setUpperBound()
\note For inverted scales the lower bound
is greater than the upper bound
*/
void QwtAbstractScale::setLowerBound( double value )
{
setScale( value, upperBound() );
}
/*!
\return Lower bound of the scale
\sa setLowerBound(), setScale(), upperBound()
*/
double QwtAbstractScale::lowerBound() const
{
return d_data->scaleDraw->scaleDiv().lowerBound();
}
/*!
Set the upper bound of the scale
\param value Upper bound
\sa upperBound(), setScale(), setLowerBound()
\note For inverted scales the lower bound
is greater than the upper bound
*/
void QwtAbstractScale::setUpperBound( double value )
{
setScale( lowerBound(), value );
}
/*!
\return Upper bound of the scale
\sa setUpperBound(), setScale(), lowerBound()
*/
double QwtAbstractScale::upperBound() const
{
return d_data->scaleDraw->scaleDiv().upperBound();
}
/*!
\brief Specify a scale.
Define a scale by an interval
The ticks are calculated using scaleMaxMinor(),
scaleMaxMajor() and scaleStepSize().
\param lowerBound lower limit of the scale interval
\param upperBound upper limit of the scale interval
\note For inverted scales the lower bound
is greater than the upper bound
*/
void QwtAbstractScale::setScale( double lowerBound, double upperBound )
{
rescale( lowerBound, upperBound, d_data->stepSize );
}
/*!
\brief Specify a scale.
Define a scale by an interval
The ticks are calculated using scaleMaxMinor(),
scaleMaxMajor() and scaleStepSize().
\param interval Interval
*/
void QwtAbstractScale::setScale( const QwtInterval &interval )
{
setScale( interval.minValue(), interval.maxValue() );
}
/*!
\brief Specify a scale.
scaleMaxMinor(), scaleMaxMajor() and scaleStepSize() and have no effect.
\param scaleDiv Scale division
\sa setAutoScale()
*/
void QwtAbstractScale::setScale( const QwtScaleDiv &scaleDiv )
{
if ( scaleDiv != d_data->scaleDraw->scaleDiv() )
{
#if 1
if ( d_data->scaleEngine )
{
d_data->scaleDraw->setTransformation(
d_data->scaleEngine->transformation() );
}
#endif
d_data->scaleDraw->setScaleDiv( scaleDiv );
scaleChange();
}
}
/*!
\brief Set the maximum number of major tick intervals.
The scale's major ticks are calculated automatically such that
the number of major intervals does not exceed ticks.
The default value is 5.
\param ticks Maximal number of major ticks.
\sa scaleMaxMajor(), setScaleMaxMinor(),
setScaleStepSize(), QwtScaleEngine::divideInterval()
*/
void QwtAbstractScale::setScaleMaxMajor( int ticks )
{
if ( ticks != d_data->maxMajor )
{
d_data->maxMajor = ticks;
updateScaleDraw();
}
}
/*!
\return Maximal number of major tick intervals
\sa setScaleMaxMajor(), scaleMaxMinor()
*/
int QwtAbstractScale::scaleMaxMajor() const
{
return d_data->maxMajor;
}
/*!
\brief Set the maximum number of minor tick intervals
The scale's minor ticks are calculated automatically such that
the number of minor intervals does not exceed ticks.
The default value is 3.
\param ticks Maximal number of minor ticks.
\sa scaleMaxMajor(), setScaleMaxMinor(),
setScaleStepSize(), QwtScaleEngine::divideInterval()
*/
void QwtAbstractScale::setScaleMaxMinor( int ticks )
{
if ( ticks != d_data->maxMinor )
{
d_data->maxMinor = ticks;
updateScaleDraw();
}
}
/*!
\return Maximal number of minor tick intervals
\sa setScaleMaxMinor(), scaleMaxMajor()
*/
int QwtAbstractScale::scaleMaxMinor() const
{
return d_data->maxMinor;
}
/*!
\brief Set the step size used for calculating a scale division
The step size is hint for calculating the intervals for
the major ticks of the scale. A value of 0.0 is interpreted
as no hint.
\param stepSize Hint for the step size of the scale
\sa scaleStepSize(), QwtScaleEngine::divideScale()
\note Position and distance between the major ticks also
depends on scaleMaxMajor().
*/
void QwtAbstractScale::setScaleStepSize( double stepSize )
{
if ( stepSize != d_data->stepSize )
{
d_data->stepSize = stepSize;
updateScaleDraw();
}
}
/*!
\return Hint for the step size of the scale
\sa setScaleStepSize(), QwtScaleEngine::divideScale()
*/
double QwtAbstractScale::scaleStepSize() const
{
return d_data->stepSize;
}
/*!
\brief Set a scale draw
scaleDraw has to be created with new and will be deleted in
the destructor or the next call of setAbstractScaleDraw().
\sa abstractScaleDraw()
*/
void QwtAbstractScale::setAbstractScaleDraw( QwtAbstractScaleDraw *scaleDraw )
{
if ( scaleDraw == NULL || scaleDraw == d_data->scaleDraw )
return;
if ( d_data->scaleDraw != NULL )
scaleDraw->setScaleDiv( d_data->scaleDraw->scaleDiv() );
delete d_data->scaleDraw;
d_data->scaleDraw = scaleDraw;
}
/*!
\return Scale draw
\sa setAbstractScaleDraw()
*/
QwtAbstractScaleDraw *QwtAbstractScale::abstractScaleDraw()
{
return d_data->scaleDraw;
}
/*!
\return Scale draw
\sa setAbstractScaleDraw()
*/
const QwtAbstractScaleDraw *QwtAbstractScale::abstractScaleDraw() const
{
return d_data->scaleDraw;
}
/*!
\brief Set a scale engine
The scale engine is responsible for calculating the scale division
and provides a transformation between scale and widget coordinates.
scaleEngine has to be created with new and will be deleted in
the destructor or the next call of setScaleEngine.
*/
void QwtAbstractScale::setScaleEngine( QwtScaleEngine *scaleEngine )
{
if ( scaleEngine != NULL && scaleEngine != d_data->scaleEngine )
{
delete d_data->scaleEngine;
d_data->scaleEngine = scaleEngine;
}
}
/*!
\return Scale engine
\sa setScaleEngine()
*/
const QwtScaleEngine *QwtAbstractScale::scaleEngine() const
{
return d_data->scaleEngine;
}
/*!
\return Scale engine
\sa setScaleEngine()
*/
QwtScaleEngine *QwtAbstractScale::scaleEngine()
{
return d_data->scaleEngine;
}
/*!
\return Scale boundaries and positions of the ticks
The scale division might have been assigned explicitly
or calculated implicitly by rescale().
*/
const QwtScaleDiv &QwtAbstractScale::scaleDiv() const
{
return d_data->scaleDraw->scaleDiv();
}
/*!
\return Map to translate between scale and widget coordinates
*/
const QwtScaleMap &QwtAbstractScale::scaleMap() const
{
return d_data->scaleDraw->scaleMap();
}
/*!
Translate a scale value into a widget coordinate
\param value Scale value
\return Corresponding widget coordinate for value
\sa scaleMap(), invTransform()
*/
int QwtAbstractScale::transform( double value ) const
{
return qRound( d_data->scaleDraw->scaleMap().transform( value ) );
}
/*!
Translate a widget coordinate into a scale value
\param value Widget coordinate
\return Corresponding scale coordinate for value
\sa scaleMap(), transform()
*/
double QwtAbstractScale::invTransform( int value ) const
{
return d_data->scaleDraw->scaleMap().invTransform( value );
}
/*!
\return True, when the scale is increasing in opposite direction
to the widget coordinates
*/
bool QwtAbstractScale::isInverted() const
{
return d_data->scaleDraw->scaleMap().isInverting();
}
/*!
\return The boundary with the smaller value
\sa maximum(), lowerBound(), upperBound()
*/
double QwtAbstractScale::minimum() const
{
return qMin( d_data->scaleDraw->scaleDiv().lowerBound(),
d_data->scaleDraw->scaleDiv().upperBound() );
}
/*!
\return The boundary with the larger value
\sa minimum(), lowerBound(), upperBound()
*/
double QwtAbstractScale::maximum() const
{
return qMax( d_data->scaleDraw->scaleDiv().lowerBound(),
d_data->scaleDraw->scaleDiv().upperBound() );
}
//! Notify changed scale
void QwtAbstractScale::scaleChange()
{
}
/*!
Recalculate the scale division and update the scale.
\param lowerBound Lower limit of the scale interval
\param upperBound Upper limit of the scale interval
\param stepSize Major step size
\sa scaleChange()
*/
void QwtAbstractScale::rescale(
double lowerBound, double upperBound, double stepSize )
{
const QwtScaleDiv scaleDiv = d_data->scaleEngine->divideScale(
lowerBound, upperBound, d_data->maxMajor, d_data->maxMinor, stepSize );
if ( scaleDiv != d_data->scaleDraw->scaleDiv() )
{
#if 1
d_data->scaleDraw->setTransformation(
d_data->scaleEngine->transformation() );
#endif
d_data->scaleDraw->setScaleDiv( scaleDiv );
scaleChange();
}
}
void QwtAbstractScale::updateScaleDraw()
{
rescale( d_data->scaleDraw->scaleDiv().lowerBound(),
d_data->scaleDraw->scaleDiv().upperBound(), d_data->stepSize );
}
|