KD Chart 2: KDChartAbstractAreaBase.cpp Source File

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

# KDChartAbstractAreaBase.cpp

Go to the documentation of this file.

    00001 /****************************************************************************
    00002  ** Copyright (C) 2006 Klarälvdalens Datakonsult AB.  All rights reserved.
    00003  **
    00004  ** This file is part of the KD Chart library.
    00005  **
    00006  ** This file may be distributed and/or modified under the terms of the
    00007  ** GNU General Public License version 2 as published by the Free Software
    00008  ** Foundation and appearing in the file LICENSE.GPL included in the
    00009  ** packaging of this file.
    00010  **
    00011  ** Licensees holding valid commercial KD Chart licenses may use this file in
    00012  ** accordance with the KD Chart Commercial License Agreement provided with
    00013  ** the Software.
    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  ** See http://www.kdab.net/kdchart for
    00019  **   information about KDChart Commercial License Agreements.
    00020  **
    00021  ** Contact info@kdab.net if any conditions of this
    00022  ** licensing are not clear to you.
    00023  **
    00024  **********************************************************************/
    00025
    00026 #include "KDChartAbstractAreaBase.h"
    00027 #include "KDChartAbstractAreaBase_p.h"
    00028 #include <KDChartBackgroundAttributes.h>
    00029 #include <KDChartFrameAttributes.h>
    00030 #include <KDChartTextAttributes.h>
    00031 #include "KDChartPainterSaver_p.h"
    00032 #include <QPainter>
    00033
    00034 #include <KDABLibFakes>
    00035
    00036
    00037 using namespace KDChart;
    00038
    00039 AbstractAreaBase::Private::Private() :
    00040     visible( true )
    00041     // PENDING(khz) dockingPointToPadding?, alignToDockingPoint?
    00042 {
    00043     init();
    00044 }
    00045
    00046
    00047 AbstractAreaBase::Private::~Private() {}
    00048
    00049
    00050 void AbstractAreaBase::Private::init()
    00051 {
    00052 }
    00053
    00054
    00055
    00056
    00057 AbstractAreaBase::AbstractAreaBase() :
    00058     _d( new Private() )
    00059 {
    00060 }
    00061
    00062 AbstractAreaBase::~AbstractAreaBase()
    00063 {
    00064     delete _d; _d = 0;
    00065 }
    00066
    00067
    00068 void AbstractAreaBase::init()
    00069 {
    00070 }
    00071
    00072
    00073 #define d d_func()
    00074
    00075 bool AbstractAreaBase::compare( const AbstractAreaBase* other )const
    00076 {
    00077     if( other == this ) return true;
    00078     if( ! other ){
    00079         //qDebug() << "CartesianAxis::compare() cannot compare to Null pointer";
    00080         return false;
    00081     }
    00082     /*
    00083     qDebug() << "AbstractAreaBase:" << (frameAttributes() == other->frameAttributes())
    00084         << (backgroundAttributes() == other->backgroundAttributes()) << "\n";
    00085     */
    00086     return  (frameAttributes()      == other->frameAttributes()) &&
    00087             (backgroundAttributes() == other->backgroundAttributes());
    00088 }
    00089
    00090 void AbstractAreaBase::alignToReferencePoint( const RelativePosition& position )
    00091 {
    00092     Q_UNUSED( position );
    00093     // PENDING(kalle) FIXME
    00094     qWarning( "Sorry, not implemented: void AbstractAreaBase::alignToReferencePoint( const RelativePosition& position )" );
    00095 }
    00096
    00097 void AbstractAreaBase::setFrameAttributes( const FrameAttributes &a )
    00098 {
    00099     d->frameAttributes = a;
    00100 }
    00101
    00102 FrameAttributes AbstractAreaBase::frameAttributes() const
    00103 {
    00104     return d->frameAttributes;
    00105 }
    00106
    00107 void AbstractAreaBase::setBackgroundAttributes( const BackgroundAttributes &a )
    00108 {
    00109     d->backgroundAttributes = a;
    00110 }
    00111
    00112 BackgroundAttributes AbstractAreaBase::backgroundAttributes() const
    00113 {
    00114     return d->backgroundAttributes;
    00115 }
    00116
    00117
    00118 /* static */
    00119 void AbstractAreaBase::paintBackgroundAttributes( QPainter& painter, const QRect& rect,
    00120     const KDChart::BackgroundAttributes& attributes )
    00121 {
    00122     if( !attributes.isVisible() ) return;
    00123
    00124     /* first draw the brush (may contain a pixmap)*/
    00125     if( Qt::NoBrush != attributes.brush().style() ) {
    00126         KDChart::PainterSaver painterSaver( &painter );
    00127         painter.setPen( Qt::NoPen );
    00128         const QPointF newTopLeft( painter.deviceMatrix().map( rect.topLeft() ) );
    00129         painter.setBrushOrigin( newTopLeft );
    00130         painter.setBrush( attributes.brush() );
    00131         painter.drawRect( rect.adjusted( 0, 0, -1, -1 ) );
    00132     }
    00133     /* next draw the backPixmap over the brush */
    00134     if( !attributes.pixmap().isNull() &&
    00135         attributes.pixmapMode() != BackgroundAttributes::BackgroundPixmapModeNone ) {
    00136         QPointF ol = rect.topLeft();
    00137         if( BackgroundAttributes::BackgroundPixmapModeCentered == attributes.pixmapMode() )
    00138         {
    00139             ol.setX( rect.center().x() - attributes.pixmap().width() / 2 );
    00140             ol.setY( rect.center().y() - attributes.pixmap().height()/ 2 );
    00141             painter.drawPixmap( ol, attributes.pixmap() );
    00142         } else {
    00143             QMatrix m;
    00144             double zW = (double)rect.width()  / (double)attributes.pixmap().width();
    00145             double zH = (double)rect.height() / (double)attributes.pixmap().height();
    00146             switch( attributes.pixmapMode() ) {
    00147             case BackgroundAttributes::BackgroundPixmapModeScaled:
    00148             {
    00149                 double z;
    00150                 z = qMin( zW, zH );
    00151                 m.scale( z, z );
    00152             }
    00153             break;
    00154             case BackgroundAttributes::BackgroundPixmapModeStretched:
    00155                 m.scale( zW, zH );
    00156                 break;
    00157             default:
    00158                 ; // Cannot happen, previously checked
    00159             }
    00160             QPixmap pm = attributes.pixmap().transformed( m );
    00161             ol.setX( rect.center().x() - pm.width() / 2 );
    00162             ol.setY( rect.center().y() - pm.height()/ 2 );
    00163             painter.drawPixmap( ol, pm );
    00164         }
    00165     }
    00166 }
    00167
    00168 /* static */
    00169 void AbstractAreaBase::paintFrameAttributes( QPainter& painter, const QRect& rect,
    00170     const KDChart::FrameAttributes& attributes )
    00171 {
    00172
    00173     if( !attributes.isVisible() ) return;
    00174
    00175     // Note: We set the brush to NoBrush explicitely here.
    00176     //       Otherwise we might get a filled rectangle, so any
    00177     //       previously drawn background would be overwritten by that area.
    00178
    00179     const QPen   oldPen(   painter.pen() );
    00180     const QBrush oldBrush( painter.brush() );
    00181     painter.setPen(   attributes.pen() );
    00182     painter.setBrush( Qt::NoBrush );
    00183     painter.drawRect( rect.adjusted( 0, 0, -1, -1 ) );
    00184     painter.setBrush( oldBrush );
    00185     painter.setPen(   oldPen );
    00186 }
    00187
    00188 void AbstractAreaBase::paintBackground( QPainter& painter, const QRect& rect )
    00189 {
    00190     Q_ASSERT_X ( d != 0, "AbstractAreaBase::paintBackground()",
    00191                 "Private class was not initialized!" );
    00192     paintBackgroundAttributes( painter, rect, d->backgroundAttributes );
    00193 }
    00194
    00195
    00196 void AbstractAreaBase::paintFrame( QPainter& painter, const QRect& rect )
    00197 {
    00198     Q_ASSERT_X ( d != 0, "AbstractAreaBase::paintFrame()",
    00199                 "Private class was not initialized!" );
    00200     paintFrameAttributes( painter, rect, d->frameAttributes );
    00201 }
    00202
    00203
    00204 void AbstractAreaBase::getFrameLeadings(int& left, int& top, int& right, int& bottom ) const
    00205 {
    00206     if( d && d->frameAttributes.isVisible() ){
    00207         const int padding = qMax( d->frameAttributes.padding(), 0 );
    00208         left   = padding;
    00209         top    = padding;
    00210         right  = padding;
    00211         bottom = padding;
    00212     }else{
    00213         left   = 0;
    00214         top    = 0;
    00215         right  = 0;
    00216         bottom = 0;
    00217     }
    00218 }
    00219
    00220 QRect AbstractAreaBase::innerRect() const
    00221 {
    00222     int left;
    00223     int top;
    00224     int right;
    00225     int bottom;
    00226     getFrameLeadings( left, top, right, bottom );
    00227     return
    00228         QRect( QPoint(0,0), areaGeometry().size() )
    00229             .adjusted( left, top, -right, -bottom );
    00230 }
    00231
    00232 void AbstractAreaBase::positionHasChanged()
    00233 {
    00234     // this bloc left empty intentionally
    00235 }
    00236