KD Chart 2  [rev.2.5]
KDChartAbstractAxis.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (C) 2001-2012 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.txt 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 "KDChartAbstractAxis.h"
00024 #include "KDChartAbstractAxis_p.h"
00025 #include "KDChartAbstractDiagram.h"
00026 #include "KDChartAbstractCartesianDiagram.h"
00027 #include "KDChartEnums.h"
00028 #include "KDChartMeasure.h"
00029 
00030 #include <KDABLibFakes>
00031 
00032 using namespace KDChart;
00033 
00034 #define d d_func()
00035 
00036 AbstractAxis::Private::Private( AbstractDiagram* diagram, AbstractAxis* axis )
00037     : observer( 0 )
00038     , mDiagram( diagram )
00039     , mAxis( axis )
00040 {
00041     // Note: We do NOT call setDiagram( diagram, axis );
00042     //       but it is called in AbstractAxis::delayedInit() instead!
00043 }
00044 
00045 AbstractAxis::Private::~Private()
00046 {
00047     delete observer;
00048     observer = 0;
00049 }
00050 
00051 bool AbstractAxis::Private::setDiagram(
00052     AbstractDiagram* diagram_,
00053     bool delayedInit )
00054 {
00055     AbstractDiagram* diagram = delayedInit ? mDiagram : diagram_;
00056     if( delayedInit ){
00057         mDiagram = 0;
00058     }
00059 
00060     // do not set a diagram again that was already set
00061     if ( diagram &&
00062         ((diagram == mDiagram) || secondaryDiagrams.contains( diagram )) )
00063         return false;
00064 
00065     bool bNewDiagramStored = false;
00066     if ( ! mDiagram ) {
00067         mDiagram = diagram;
00068         delete observer;
00069         if ( mDiagram ) {
00070             observer = new DiagramObserver( mDiagram, mAxis );
00071             const bool con = connect( observer, SIGNAL( diagramDataChanged( AbstractDiagram *) ),
00072                     mAxis, SIGNAL( coordinateSystemChanged() ) );
00073             Q_UNUSED( con )
00074             Q_ASSERT( con );
00075             bNewDiagramStored = true;
00076         }else{
00077             observer = 0;
00078         }
00079     } else {
00080         if ( diagram )
00081             secondaryDiagrams.enqueue( diagram );
00082     }
00083     return bNewDiagramStored;
00084 }
00085 
00086 void AbstractAxis::Private::unsetDiagram( AbstractDiagram* diagram )
00087 {
00088     if ( diagram == mDiagram ) {
00089         mDiagram = 0;
00090         delete observer;
00091         observer = 0;
00092     } else {
00093         secondaryDiagrams.removeAll( diagram );
00094     }
00095     if( !secondaryDiagrams.isEmpty() ) {
00096         AbstractDiagram *nextDiagram = secondaryDiagrams.dequeue();
00097         setDiagram( nextDiagram );
00098     }
00099 }
00100 
00101 bool AbstractAxis::Private::hasDiagram( AbstractDiagram* diagram ) const
00102 {
00103     return diagram == mDiagram || secondaryDiagrams.contains( diagram );
00104 }
00105 
00106 AbstractAxis::AbstractAxis ( AbstractDiagram* diagram )
00107     : AbstractArea( new Private( diagram, this ) )
00108 {
00109     init();
00110     QTimer::singleShot(0, this, SLOT(delayedInit()));
00111 }
00112 
00113 AbstractAxis::~AbstractAxis()
00114 {
00115     d->mDiagram = 0;
00116     d->secondaryDiagrams.clear();
00117 }
00118 
00119 
00120 void AbstractAxis::init()
00121 {
00122     Measure m(
00123         12.5,
00124         KDChartEnums::MeasureCalculationModeAuto,
00125         KDChartEnums::MeasureOrientationAuto );
00126     d->textAttributes.setFontSize( m );
00127     m.setValue( 5 );
00128     m.setCalculationMode( KDChartEnums::MeasureCalculationModeAbsolute );
00129     d->textAttributes.setMinimalFontSize( m );
00130     if ( d->diagram() )
00131         createObserver( d->diagram() );
00132 }
00133 
00134 void AbstractAxis::delayedInit()
00135 {
00136     // We call setDiagram() here, because the c'tor of Private
00137     // only has stored the pointers, but it did not call setDiagram().
00138     if( d )
00139         d->setDiagram( 0, true /* delayedInit */ );
00140 }
00141 
00142 bool AbstractAxis::compare( const AbstractAxis* other ) const
00143 {
00144     if ( other == this ) {
00145         return true;
00146     }
00147     if ( !other ) {
00148         return false;
00149     }
00150 
00151     return  ( static_cast<const AbstractAreaBase*>(this)->compare( other ) ) &&
00152             (textAttributes() == other->textAttributes()) &&
00153             (labels()         == other->labels()) &&
00154             (shortLabels()    == other->shortLabels());
00155 }
00156 
00157 
00158 const QString AbstractAxis::customizedLabel( const QString& label ) const
00159 {
00160     return label;
00161 }
00162 
00163 
00164 void AbstractAxis::createObserver( AbstractDiagram* diagram )
00165 {
00166     d->setDiagram( diagram );
00167 }
00168 
00169 void AbstractAxis::deleteObserver( AbstractDiagram* diagram )
00170 {
00171     d->unsetDiagram( diagram );
00172 }
00173 
00174 void AbstractAxis::connectSignals()
00175 {
00176     if( d->observer ){
00177         const bool con = connect( d->observer, SIGNAL( diagramDataChanged( AbstractDiagram *) ),
00178                 this, SIGNAL( coordinateSystemChanged() ) );
00179         Q_UNUSED( con );
00180         Q_ASSERT( con );
00181     }
00182 }
00183 
00184 void AbstractAxis::setTextAttributes( const TextAttributes &a )
00185 {
00186     if( d->textAttributes == a )
00187         return;
00188 
00189     d->textAttributes = a;
00190     update();
00191 }
00192 
00193 TextAttributes AbstractAxis::textAttributes() const
00194 {
00195     return d->textAttributes;
00196 }
00197 
00198 
00199 void AbstractAxis::setRulerAttributes( const RulerAttributes &a )
00200 {
00201         d->rulerAttributes = a;
00202         update();
00203 }
00204 
00205 RulerAttributes AbstractAxis::rulerAttributes() const
00206 {
00207         return d->rulerAttributes;
00208 }
00209 
00210 void AbstractAxis::setLabels( const QStringList& list )
00211 {
00212     if( d->hardLabels == list )
00213         return;
00214 
00215     d->hardLabels = list;
00216     update();
00217 }
00218 
00219 QStringList AbstractAxis::labels() const
00220 {
00221     return d->hardLabels;
00222 }
00223 
00224 void AbstractAxis::setShortLabels( const QStringList& list )
00225 {
00226     if( d->hardShortLabels == list )
00227         return;
00228 
00229     d->hardShortLabels = list;
00230     update();
00231 }
00232 
00233 QStringList AbstractAxis::shortLabels() const
00234 {
00235     return d->hardShortLabels;
00236 }
00237 
00238 const AbstractCoordinatePlane* AbstractAxis::coordinatePlane() const
00239 {
00240     if( d->diagram() )
00241         return d->diagram()->coordinatePlane();
00242     return 0;
00243 }
00244 
00245 const AbstractDiagram * KDChart::AbstractAxis::diagram() const
00246 {
00247     return d->diagram();
00248 }
00249 
00250 bool KDChart::AbstractAxis::observedBy( AbstractDiagram * diagram ) const
00251 {
00252     return d->hasDiagram( diagram );
00253 }
00254 
00255 void KDChart::AbstractAxis::update()
00256 {
00257     if( d->diagram() )
00258         d->diagram()->update();
00259 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Defines

Klarälvdalens Datakonsult AB (KDAB)
Qt-related services and products
http://www.kdab.com/
http://www.kdab.com/products/kd-chart/