KDChartRulerAttributes.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB.  All rights reserved.
00003 **
00004 ** This file is part of the KD Chart library.
00005 **
00006 ** Licensees holding valid commercial KD Chart licenses may use this file in
00007 ** accordance with the KD Chart Commercial License Agreement provided with
00008 ** the Software.
00009 **
00010 **
00011 ** This file may be distributed and/or modified under the terms of the
00012 ** GNU General Public License version 2 and version 3 as published by the
00013 ** Free Software Foundation and appearing in the file LICENSE.GPL included.
00014 **
00015 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 **
00018 ** Contact info@kdab.com if any conditions of this licensing are not
00019 ** clear to you.
00020 **
00021 **********************************************************************/
00022 
00023 #include "KDChartRulerAttributes.h"
00024 
00025 #include <limits>
00026 
00027 #include <QPen>
00028 #include <QDebug>
00029 
00030 #include <KDABLibFakes>
00031 
00032 #define d d_func()
00033 
00034 using namespace KDChart;
00035 
00036 class RulerAttributes::Private
00037 {
00038     friend class RulerAttributes;
00039 public:
00040     Private();
00041 private:
00042     QPen tickMarkPen;
00043     QPen majorTickMarkPen;
00044     QPen minorTickMarkPen;
00045     
00046     bool majorTickMarkPenIsSet;
00047     bool minorTickMarkPenIsSet;
00048 
00049     bool showMajorTickMarks;
00050     bool showMinorTickMarks;
00051     
00052     int labelMargin;
00053     
00054     RulerAttributes::TickMarkerPensMap customTickMarkPens;
00055 };
00056 
00057 RulerAttributes::Private::Private()
00058     : tickMarkPen( QColor( 0x00, 0x00, 0x00 ) )
00059     , majorTickMarkPen( QColor( 0x00, 0x00, 0x00 ) )
00060     , minorTickMarkPen( QColor( 0x00, 0x00, 0x00 ) )
00061 {
00062     tickMarkPen.setCapStyle( Qt::FlatCap );
00063     majorTickMarkPen.setCapStyle( Qt::FlatCap );
00064     minorTickMarkPen.setCapStyle( Qt::FlatCap );
00065 
00066     majorTickMarkPenIsSet = false;
00067     minorTickMarkPenIsSet = false;
00068 
00069     showMajorTickMarks = true;
00070     showMinorTickMarks = true;
00071     
00072     labelMargin = -1;
00073 }
00074 
00075 RulerAttributes::RulerAttributes()
00076     : _d( new Private() )
00077 {
00078     // this bloc left empty intentionally
00079 }
00080 
00081 RulerAttributes::RulerAttributes( const RulerAttributes& r )
00082     : _d( new Private( *r.d ) )
00083 {
00084 }
00085 
00086 void RulerAttributes::setTickMarkPen( const QPen& pen )
00087 {
00088         d->tickMarkPen = pen;
00089 }
00090 
00091 QPen RulerAttributes::tickMarkPen() const
00092 {
00093         return d->tickMarkPen;
00094 }
00095 
00096 void RulerAttributes::setMajorTickMarkPen( const QPen& pen )
00097 {
00098         d->majorTickMarkPen = pen;
00099         d->majorTickMarkPenIsSet = true;
00100 }
00101 
00102 bool RulerAttributes::majorTickMarkPenIsSet() const
00103 {
00104     return d->majorTickMarkPenIsSet;
00105 }
00106 
00107 QPen RulerAttributes::majorTickMarkPen() const
00108 {
00109         return d->majorTickMarkPenIsSet ? d->majorTickMarkPen : d->tickMarkPen;
00110 }
00111 
00112 void RulerAttributes::setMinorTickMarkPen( const QPen& pen )
00113 {
00114         d->minorTickMarkPen = pen;
00115         d->minorTickMarkPenIsSet = true;
00116 }
00117 
00118 bool RulerAttributes::minorTickMarkPenIsSet() const
00119 {
00120     return d->minorTickMarkPenIsSet;
00121 }
00122 
00123 QPen RulerAttributes::minorTickMarkPen() const
00124 {
00125         return d->minorTickMarkPenIsSet ? d->minorTickMarkPen : d->tickMarkPen;
00126 }
00127 
00128 void RulerAttributes::setTickMarkPen( qreal value, const QPen& pen )
00129 {
00130         if ( !d->customTickMarkPens.contains( value ) )
00131                 d->customTickMarkPens.insert( value, pen );
00132 }
00133 
00134 QPen RulerAttributes::tickMarkPen( qreal value ) const
00135 {
00136         QMapIterator<qreal, QPen> it( d->customTickMarkPens );
00137         while( it.hasNext() ) {
00138                 it.next();
00139                 if ( qAbs( value - it.key() ) < std::numeric_limits< float >::epsilon() )
00140                         return it.value();
00141         }
00142         return d->tickMarkPen;
00143 }
00144 
00145 RulerAttributes::TickMarkerPensMap RulerAttributes::tickMarkPens() const
00146 {
00147     return d->customTickMarkPens;
00148 }
00149 
00150 bool RulerAttributes::hasTickMarkPenAt( qreal value ) const
00151 {
00152         QMapIterator<qreal, QPen> it( d->customTickMarkPens );
00153         while( it.hasNext() ) {
00154                 it.next();
00155                 if ( qAbs( value - it.key() ) < std::numeric_limits< float >::epsilon() )
00156                         return true;
00157         }
00158         return false;
00159 }
00160 
00161 void RulerAttributes::setTickMarkColor( const QColor& color )
00162 {
00163         d->tickMarkPen.setColor( color );
00164 }
00165 
00166 QColor RulerAttributes::tickMarkColor() const
00167 {
00168         return d->tickMarkPen.color();
00169 }
00170 
00171 void RulerAttributes::setShowMajorTickMarks( bool show )
00172 {
00173     d->showMajorTickMarks = show;
00174 }
00175 
00176 bool RulerAttributes::showMajorTickMarks() const
00177 {
00178     return d->showMajorTickMarks;
00179 }
00180 
00181 void RulerAttributes::setShowMinorTickMarks( bool show )
00182 {
00183     d->showMinorTickMarks = show;
00184 }
00185 
00186 bool RulerAttributes::showMinorTickMarks() const
00187 {
00188     return d->showMinorTickMarks;
00189 }
00190 
00191 void RulerAttributes::setLabelMargin(int margin)
00192 {
00193     d->labelMargin = margin;
00194 }
00195 
00196 int RulerAttributes::labelMargin() const
00197 {
00198     return d->labelMargin;
00199 }
00200     
00201 RulerAttributes & RulerAttributes::operator=( const RulerAttributes& r )
00202 {
00203     if( this == &r )
00204         return *this;
00205 
00206     *d = *r.d;
00207 
00208     return *this;
00209 }
00210 
00211 RulerAttributes::~RulerAttributes()
00212 {
00213     delete _d; _d = 0;
00214 }
00215 
00216 
00217 bool RulerAttributes::operator == ( const RulerAttributes& r ) const
00218 {
00219     bool isEqual =
00220         tickMarkPen()      == r.tickMarkPen() &&
00221         majorTickMarkPen() == r.majorTickMarkPen() &&
00222         minorTickMarkPen() == r.minorTickMarkPen();
00223     if( isEqual ) {
00224         QMapIterator<qreal, QPen> it( d->customTickMarkPens );
00225         while( it.hasNext() ) {
00226             it.next();
00227             if ( it.value() != r.tickMarkPen(it.key()) )
00228                 return false;
00229         }
00230     }
00231     return isEqual;
00232 }
00233 
00234 #if !defined( QT_NO_DEBUG_STREAM )
00235 QDebug operator << ( QDebug dbg, const KDChart::RulerAttributes& a )
00236 {
00237     dbg << "KDChart::RulerAttributes("
00238             << "tickMarkPen=" << a.tickMarkPen()
00239             << "majorTickMarkPen=" << a.majorTickMarkPen()
00240             << "minorTickMarkPen=" << a.minorTickMarkPen();
00241     const RulerAttributes::TickMarkerPensMap pens( a.tickMarkPens() );
00242     QMapIterator<qreal, QPen> it( pens );
00243     while( it.hasNext() ) {
00244         it.next();
00245         dbg << "customTickMarkPen=(" << it.value() << " : " << it.key() << ")";
00246     }
00247     dbg << ")";
00248     return dbg;
00249 }
00250 #endif /* QT_NO_DEBUG_STREAM */
00251