qwt_abstract_scale_draw.cpp
Go to the documentation of this file.
00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
00002  * Qwt Widget Library
00003  * Copyright (C) 1997   Josef Wilgen
00004  * Copyright (C) 2002   Uwe Rathmann
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the Qwt License, Version 1.0
00008  *****************************************************************************/
00009 
00010 #include "qwt_abstract_scale_draw.h"
00011 #include "qwt_math.h"
00012 #include "qwt_text.h"
00013 #include "qwt_painter.h"
00014 #include "qwt_scale_map.h"
00015 #include <qpainter.h>
00016 #include <qpalette.h>
00017 #include <qmap.h>
00018 #include <qlocale.h>
00019 
00020 class QwtAbstractScaleDraw::PrivateData
00021 {
00022 public:
00023     PrivateData():
00024         spacing( 4.0 ),
00025         penWidth( 0 ),
00026         minExtent( 0.0 )
00027     {
00028         components = QwtAbstractScaleDraw::Backbone 
00029             | QwtAbstractScaleDraw::Ticks 
00030             | QwtAbstractScaleDraw::Labels;
00031 
00032         tickLength[QwtScaleDiv::MinorTick] = 4.0;
00033         tickLength[QwtScaleDiv::MediumTick] = 6.0;
00034         tickLength[QwtScaleDiv::MajorTick] = 8.0;
00035     }
00036 
00037     ScaleComponents components;
00038 
00039     QwtScaleMap map;
00040     QwtScaleDiv scaleDiv;
00041 
00042     double spacing;
00043     double tickLength[QwtScaleDiv::NTickTypes];
00044     int penWidth;
00045 
00046     double minExtent;
00047 
00048     QMap<double, QwtText> labelCache;
00049 };
00050 
00058 QwtAbstractScaleDraw::QwtAbstractScaleDraw()
00059 {
00060     d_data = new QwtAbstractScaleDraw::PrivateData;
00061 }
00062 
00064 QwtAbstractScaleDraw::~QwtAbstractScaleDraw()
00065 {
00066     delete d_data;
00067 }
00068 
00077 void QwtAbstractScaleDraw::enableComponent(
00078     ScaleComponent component, bool enable )
00079 {
00080     if ( enable )
00081         d_data->components |= component;
00082     else
00083         d_data->components &= ~component;
00084 }
00085 
00093 bool QwtAbstractScaleDraw::hasComponent( ScaleComponent component ) const
00094 {
00095     return ( d_data->components & component );
00096 }
00097 
00102 void QwtAbstractScaleDraw::setScaleDiv( const QwtScaleDiv &scaleDiv )
00103 {
00104     d_data->scaleDiv = scaleDiv;
00105     d_data->map.setScaleInterval( scaleDiv.lowerBound(), scaleDiv.upperBound() );
00106     d_data->labelCache.clear();
00107 }
00108 
00113 void QwtAbstractScaleDraw::setTransformation(
00114     QwtTransform *transformation )
00115 {
00116     d_data->map.setTransformation( transformation );
00117 }
00118 
00120 const QwtScaleMap &QwtAbstractScaleDraw::scaleMap() const
00121 {
00122     return d_data->map;
00123 }
00124 
00126 QwtScaleMap &QwtAbstractScaleDraw::scaleMap()
00127 {
00128     return d_data->map;
00129 }
00130 
00132 const QwtScaleDiv& QwtAbstractScaleDraw::scaleDiv() const
00133 {
00134     return d_data->scaleDiv;
00135 }
00136 
00142 void QwtAbstractScaleDraw::setPenWidth( int width )
00143 {
00144     if ( width < 0 )
00145         width = 0;
00146 
00147     if ( width != d_data->penWidth )
00148         d_data->penWidth = width;
00149 }
00150 
00155 int QwtAbstractScaleDraw::penWidth() const
00156 {
00157     return d_data->penWidth;
00158 }
00159 
00168 void QwtAbstractScaleDraw::draw( QPainter *painter,
00169     const QPalette& palette ) const
00170 {
00171     painter->save();
00172 
00173     QPen pen = painter->pen();
00174     pen.setWidth( d_data->penWidth );
00175     pen.setCosmetic( false );
00176     painter->setPen( pen );
00177 
00178     if ( hasComponent( QwtAbstractScaleDraw::Labels ) )
00179     {
00180         painter->save();
00181         painter->setPen( palette.color( QPalette::Text ) ); // ignore pen style
00182 
00183         const QList<double> &majorTicks =
00184             d_data->scaleDiv.ticks( QwtScaleDiv::MajorTick );
00185 
00186         for ( int i = 0; i < majorTicks.count(); i++ )
00187         {
00188             const double v = majorTicks[i];
00189             if ( d_data->scaleDiv.contains( v ) )
00190                 drawLabel( painter, v );
00191         }
00192 
00193         painter->restore();
00194     }
00195 
00196     if ( hasComponent( QwtAbstractScaleDraw::Ticks ) )
00197     {
00198         painter->save();
00199 
00200         QPen pen = painter->pen();
00201         pen.setColor( palette.color( QPalette::WindowText ) );
00202         pen.setCapStyle( Qt::FlatCap );
00203 
00204         painter->setPen( pen );
00205 
00206         for ( int tickType = QwtScaleDiv::MinorTick;
00207             tickType < QwtScaleDiv::NTickTypes; tickType++ )
00208         {
00209             const double tickLen = d_data->tickLength[tickType];
00210             if ( tickLen <= 0.0 )
00211                 continue;
00212 
00213             const QList<double> &ticks = d_data->scaleDiv.ticks( tickType );
00214             for ( int i = 0; i < ticks.count(); i++ )
00215             {
00216                 const double v = ticks[i];
00217                 if ( d_data->scaleDiv.contains( v ) )
00218                     drawTick( painter, v, tickLen );
00219             }
00220         }
00221 
00222         painter->restore();
00223     }
00224 
00225     if ( hasComponent( QwtAbstractScaleDraw::Backbone ) )
00226     {
00227         painter->save();
00228 
00229         QPen pen = painter->pen();
00230         pen.setColor( palette.color( QPalette::WindowText ) );
00231         pen.setCapStyle( Qt::FlatCap );
00232 
00233         painter->setPen( pen );
00234 
00235         drawBackbone( painter );
00236 
00237         painter->restore();
00238     }
00239 
00240     painter->restore();
00241 }
00242 
00253 void QwtAbstractScaleDraw::setSpacing( double spacing )
00254 {
00255     if ( spacing < 0 )
00256         spacing = 0;
00257 
00258     d_data->spacing = spacing;
00259 }
00260 
00270 double QwtAbstractScaleDraw::spacing() const
00271 {
00272     return d_data->spacing;
00273 }
00274 
00288 void QwtAbstractScaleDraw::setMinimumExtent( double minExtent )
00289 {
00290     if ( minExtent < 0.0 )
00291         minExtent = 0.0;
00292 
00293     d_data->minExtent = minExtent;
00294 }
00295 
00301 double QwtAbstractScaleDraw::minimumExtent() const
00302 {
00303     return d_data->minExtent;
00304 }
00305 
00314 void QwtAbstractScaleDraw::setTickLength(
00315     QwtScaleDiv::TickType tickType, double length )
00316 {
00317     if ( tickType < QwtScaleDiv::MinorTick ||
00318         tickType > QwtScaleDiv::MajorTick )
00319     {
00320         return;
00321     }
00322 
00323     if ( length < 0.0 )
00324         length = 0.0;
00325 
00326     const double maxTickLen = 1000.0;
00327     if ( length > maxTickLen )
00328         length = maxTickLen;
00329 
00330     d_data->tickLength[tickType] = length;
00331 }
00332 
00337 double QwtAbstractScaleDraw::tickLength( QwtScaleDiv::TickType tickType ) const
00338 {
00339     if ( tickType < QwtScaleDiv::MinorTick ||
00340         tickType > QwtScaleDiv::MajorTick )
00341     {
00342         return 0;
00343     }
00344 
00345     return d_data->tickLength[tickType];
00346 }
00347 
00354 double QwtAbstractScaleDraw::maxTickLength() const
00355 {
00356     double length = 0.0;
00357     for ( int i = 0; i < QwtScaleDiv::NTickTypes; i++ )
00358         length = qMax( length, d_data->tickLength[i] );
00359 
00360     return length;
00361 }
00362 
00374 QwtText QwtAbstractScaleDraw::label( double value ) const
00375 {
00376     return QLocale().toString( value );
00377 }
00378 
00392 const QwtText &QwtAbstractScaleDraw::tickLabel(
00393     const QFont &font, double value ) const
00394 {
00395     QMap<double, QwtText>::const_iterator it = d_data->labelCache.find( value );
00396     if ( it == d_data->labelCache.end() )
00397     {
00398         QwtText lbl = label( value );
00399         lbl.setRenderFlags( 0 );
00400         lbl.setLayoutAttribute( QwtText::MinimumLayout );
00401 
00402         ( void )lbl.textSize( font ); // initialize the internal cache
00403 
00404         it = d_data->labelCache.insert( value, lbl );
00405     }
00406 
00407     return ( *it );
00408 }
00409 
00417 void QwtAbstractScaleDraw::invalidateCache()
00418 {
00419     d_data->labelCache.clear();
00420 }


plotjuggler
Author(s): Davide Faconti
autogenerated on Fri Sep 1 2017 02:41:56