# qwt_plot_scaleitem.cpp

    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 <qpalette.h>
    00011 #include <qpainter.h>
    00012 #include "qwt_plot.h"
    00013 #include "qwt_plot_canvas.h"
    00014 #include "qwt_scale_map.h"
    00015 #include "qwt_plot_scaleitem.h"
    00016 #include "qwt_double_interval.h"
    00017
    00018 class QwtPlotScaleItem::PrivateData
    00019 {
    00020 public:
    00021     PrivateData():
    00022         position(0.0),
    00023         borderDistance(-1),
    00024         scaleDivFromAxis(true),
    00025         scaleDraw(new QwtScaleDraw())
    00026     {
    00027     }
    00028
    00029     ~PrivateData()
    00030     {
    00031         delete scaleDraw;
    00032     }
    00033
    00034 #if QT_VERSION < 0x040000
    00035     QColorGroup colorGroup;
    00036 #else
    00037     QPalette palette;
    00038 #endif
    00039     QFont font;
    00040     double position;
    00041     int borderDistance;
    00042     bool scaleDivFromAxis;
    00043     QwtScaleDraw *scaleDraw;
    00044     QRect canvasRectCache;
    00045 };
    00046
    00058 QwtPlotScaleItem::QwtPlotScaleItem(
    00059         QwtScaleDraw::Alignment alignment, const double pos):
    00060     QwtPlotItem(QwtText("Scale"))
    00061 {
    00062     d_data = new PrivateData;
    00063     d_data->position = pos;
    00064     d_data->scaleDraw->setAlignment(alignment);
    00065
    00066     setZ(11.0);
    00067 }
    00068
    00070 QwtPlotScaleItem::~QwtPlotScaleItem()
    00071 {
    00072     delete d_data;
    00073 }
    00074
    00076 int QwtPlotScaleItem::rtti() const
    00077 {
    00078     return QwtPlotItem::Rtti_PlotScale;
    00079 }
    00080
    00090 void QwtPlotScaleItem::setScaleDiv(const QwtScaleDiv& scaleDiv)
    00091 {
    00092     d_data->scaleDivFromAxis = false;
    00093     d_data->scaleDraw->setScaleDiv(scaleDiv);
    00094 }
    00095
    00097 const QwtScaleDiv& QwtPlotScaleItem::scaleDiv() const
    00098 {
    00099     return d_data->scaleDraw->scaleDiv();
    00100 }
    00101
    00108 void QwtPlotScaleItem::setScaleDivFromAxis(bool on)
    00109 {
    00110     if ( on != d_data->scaleDivFromAxis )
    00111     {
    00112         d_data->scaleDivFromAxis = on;
    00113         if ( on )
    00114         {
    00115             const QwtPlot *plt = plot();
    00116             if ( plt )
    00117             {
    00118                 updateScaleDiv( *plt->axisScaleDiv(xAxis()),
    00119                     *plt->axisScaleDiv(yAxis()) );
    00120                 itemChanged();
    00121             }
    00122         }
    00123     }
    00124 }
    00125
    00131 bool QwtPlotScaleItem::isScaleDivFromAxis() const
    00132 {
    00133     return d_data->scaleDivFromAxis;
    00134 }
    00135
    00136 #if QT_VERSION < 0x040000
    00137
    00142 void QwtPlotScaleItem::setColorGroup(const QColorGroup &colorGroup)
    00143 {
    00144     if ( colorGroup != d_data->colorGroup )
    00145     {
    00146         d_data->colorGroup = colorGroup;
    00147         itemChanged();
    00148     }
    00149 }
    00150
    00155 QColorGroup QwtPlotScaleItem::colorGroup() const
    00156 {
    00157     return d_data->colorGroup;
    00158 }
    00159
    00160 #else
    00161
    00166 void QwtPlotScaleItem::setPalette(const QPalette &palette)
    00167 {
    00168     if ( palette != d_data->palette )
    00169     {
    00170         d_data->palette = palette;
    00171         itemChanged();
    00172     }
    00173 }
    00174
    00179 QPalette QwtPlotScaleItem::palette() const
    00180 {
    00181     return d_data->palette;
    00182 }
    00183
    00184 #endif
    00185
    00190 void QwtPlotScaleItem::setFont(const QFont &font)
    00191 {
    00192     if ( font != d_data->font )
    00193     {
    00194         d_data->font = font;
    00195         itemChanged();
    00196     }
    00197 }
    00198
    00203 QFont QwtPlotScaleItem::font() const
    00204 {
    00205     return d_data->font;
    00206 }
    00207
    00220 void QwtPlotScaleItem::setScaleDraw(QwtScaleDraw *scaleDraw)
    00221 {
    00222     if ( scaleDraw == NULL )
    00223         return;
    00224
    00225     if ( scaleDraw != d_data->scaleDraw )
    00226         delete d_data->scaleDraw;
    00227
    00228     d_data->scaleDraw = scaleDraw;
    00229
    00230     const QwtPlot *plt = plot();
    00231     if ( plt )
    00232     {
    00233         updateScaleDiv( *plt->axisScaleDiv(xAxis()),
    00234             *plt->axisScaleDiv(yAxis()) );
    00235     }
    00236
    00237     itemChanged();
    00238 }
    00239
    00244 const QwtScaleDraw *QwtPlotScaleItem::scaleDraw() const
    00245 {
    00246     return d_data->scaleDraw;
    00247 }
    00248
    00253 QwtScaleDraw *QwtPlotScaleItem::scaleDraw()
    00254 {
    00255     return d_data->scaleDraw;
    00256 }
    00257
    00268 void QwtPlotScaleItem::setPosition(double pos)
    00269 {
    00270     if ( d_data->position != pos )
    00271     {
    00272         d_data->position = pos;
    00273         d_data->borderDistance = -1;
    00274         itemChanged();
    00275     }
    00276 }
    00277
    00282 double QwtPlotScaleItem::position() const
    00283 {
    00284     return d_data->position;
    00285 }
    00286
    00303 void QwtPlotScaleItem::setBorderDistance(int distance)
    00304 {
    00305     if ( distance < 0 )
    00306         distance = -1;
    00307
    00308     if ( distance != d_data->borderDistance )
    00309     {
    00310         d_data->borderDistance = distance;
    00311         itemChanged();
    00312     }
    00313 }
    00314
    00319 int QwtPlotScaleItem::borderDistance() const
    00320 {
    00321     return d_data->borderDistance;
    00322 }
    00323
    00340 void QwtPlotScaleItem::setAlignment(QwtScaleDraw::Alignment alignment)
    00341 {
    00342     QwtScaleDraw *sd = d_data->scaleDraw;
    00343     if ( sd->alignment() != alignment )
    00344     {
    00345         sd->setAlignment(alignment);
    00346         itemChanged();
    00347     }
    00348 }
    00349
    00353 void QwtPlotScaleItem::draw(QPainter *painter,
    00354     const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    00355     const QRect &canvasRect) const
    00356 {
    00357     if ( canvasRect != d_data->canvasRectCache )
    00358     {
    00359         QwtPlotScaleItem* that = (QwtPlotScaleItem*)this;
    00360         that->updateBorders();
    00361     }
    00362
    00363     QPen pen = painter->pen();
    00364     pen.setStyle(Qt::SolidLine);
    00365     painter->setPen(pen);
    00366
    00367     int pw = painter->pen().width();
    00368     if ( pw == 0 )
    00369         pw = 1;
    00370
    00371     QwtScaleDraw *sd = d_data->scaleDraw;
    00372     if ( sd->orientation() == Qt::Horizontal )
    00373     {
    00374         int y;
    00375         if ( d_data->borderDistance >= 0 )
    00376         {
    00377             if ( sd->alignment() == QwtScaleDraw::BottomScale )
    00378                 y = canvasRect.top() + d_data->borderDistance;
    00379             else
    00380             {
    00381                 y = canvasRect.bottom() - d_data->borderDistance - pw + 1;
    00382             }
    00383
    00384         }
    00385         else
    00386         {
    00387             y = yMap.transform(d_data->position);
    00388         }
    00389
    00390         if ( y < canvasRect.top() || y > canvasRect.bottom() )
    00391             return;
    00392
    00393         sd->move(canvasRect.left(), y);
    00394         sd->setLength(canvasRect.width());
    00395         sd->setTransformation(xMap.transformation()->copy());
    00396     }
    00397     else // == Qt::Vertical
    00398     {
    00399         int x;
    00400         if ( d_data->borderDistance >= 0 )
    00401         {
    00402             if ( sd->alignment() == QwtScaleDraw::RightScale )
    00403                 x = canvasRect.left() + d_data->borderDistance;
    00404             else
    00405             {
    00406                 x = canvasRect.right() - d_data->borderDistance - pw + 1;
    00407             }
    00408         }
    00409         else
    00410         {
    00411             x = xMap.transform(d_data->position);
    00412         }
    00413         if ( x < canvasRect.left() || x > canvasRect.right() )
    00414             return;
    00415
    00416         sd->move(x, canvasRect.top());
    00417         sd->setLength(canvasRect.height());
    00418         sd->setTransformation(yMap.transformation()->copy());
    00419     }
    00420
    00421     painter->setFont(d_data->font);
    00422
    00423 #if QT_VERSION < 0x040000
    00424     sd->draw(painter, d_data->colorGroup);
    00425 #else
    00426     sd->draw(painter, d_data->palette);
    00427 #endif
    00428
    00429 }
    00430
    00443 void QwtPlotScaleItem::updateScaleDiv(const QwtScaleDiv& xScaleDiv,
    00444     const QwtScaleDiv& yScaleDiv)
    00445 {
    00446     QwtScaleDraw *sd = d_data->scaleDraw;
    00447     if ( d_data->scaleDivFromAxis && sd )
    00448     {
    00449         sd->setScaleDiv(
    00450             sd->orientation() == Qt::Horizontal ? xScaleDiv : yScaleDiv);
    00451         updateBorders();
    00452     }
    00453 }
    00454
    00455 void QwtPlotScaleItem::updateBorders()
    00456 {
    00457     const QwtPlot *plt = plot();
    00458     if ( plt == NULL || !d_data->scaleDivFromAxis )
    00459         return;
    00460
    00461     const QRect r = plt->canvas()->contentsRect();
    00462     d_data->canvasRectCache = r;
    00463
    00464     QwtDoubleInterval interval;
    00465     if ( d_data->scaleDraw->orientation() == Qt::Horizontal )
    00466     {
    00467         const QwtScaleMap map = plt->canvasMap(xAxis());
    00468         interval.setMinValue(map.invTransform(r.left()));
    00469         interval.setMaxValue(map.invTransform(r.right()));
    00470     }
    00471     else
    00472     {
    00473         const QwtScaleMap map = plt->canvasMap(yAxis());
    00474         interval.setMinValue(map.invTransform(r.bottom()));
    00475         interval.setMaxValue(map.invTransform(r.top()));
    00476     }
    00477
    00478     QwtScaleDiv scaleDiv = d_data->scaleDraw->scaleDiv();
    00479     scaleDiv.setInterval(interval);
    00480     d_data->scaleDraw->setScaleDiv(scaleDiv);
    00481 }