Qwt User's Guide: qwt_compass.cpp Source File

- Main Page
- Classes
- Files
- Related Pages

- File List
- File Members

# qwt_compass.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 // vim: expandtab
    00011
    00012 #include <math.h>
    00013 #include <qpainter.h>
    00014 #include <qpixmap.h>
    00015 #include <qevent.h>
    00016 #include "qwt_math.h"
    00017 #include "qwt_scale_draw.h"
    00018 #include "qwt_paint_buffer.h"
    00019 #include "qwt_painter.h"
    00020 #include "qwt_dial_needle.h"
    00021 #include "qwt_compass_rose.h"
    00022 #include "qwt_compass.h"
    00023
    00024 class QwtCompass::PrivateData
    00025 {
    00026 public:
    00027     PrivateData():
    00028         rose(NULL)
    00029     {
    00030     }
    00031
    00032     ~PrivateData()
    00033     {
    00034         delete rose;
    00035     }
    00036
    00037     QwtCompassRose *rose;
    00038     QMap<double, QString> labelMap;
    00039 };
    00040
    00050 QwtCompass::QwtCompass(QWidget* parent):
    00051     QwtDial(parent)
    00052 {
    00053     initCompass();
    00054 }
    00055
    00056 #if QT_VERSION < 0x040000
    00057
    00068 QwtCompass::QwtCompass(QWidget* parent, const char *name):
    00069     QwtDial(parent, name)
    00070 {
    00071     initCompass();
    00072 }
    00073
    00074 #endif
    00075
    00077 QwtCompass::~QwtCompass()
    00078 {
    00079     delete d_data;
    00080 }
    00081
    00082 void QwtCompass::initCompass()
    00083 {
    00084     d_data = new PrivateData;
    00085
    00086     setScaleOptions(ScaleLabel); // Only labels, no backbone, no ticks
    00087
    00088     setOrigin(270.0);
    00089     setWrapping(true);
    00090
    00091
    00092     d_data->labelMap.insert(0.0, QString::fromLatin1("N"));
    00093     d_data->labelMap.insert(45.0, QString::fromLatin1("NE"));
    00094     d_data->labelMap.insert(90.0, QString::fromLatin1("E"));
    00095     d_data->labelMap.insert(135.0, QString::fromLatin1("SE"));
    00096     d_data->labelMap.insert(180.0, QString::fromLatin1("S"));
    00097     d_data->labelMap.insert(225.0, QString::fromLatin1("SW"));
    00098     d_data->labelMap.insert(270.0, QString::fromLatin1("W"));
    00099     d_data->labelMap.insert(315.0, QString::fromLatin1("NW"));
    00100
    00101 #if 0
    00102     d_data->labelMap.insert(22.5, QString::fromLatin1("NNE"));
    00103     d_data->labelMap.insert(67.5, QString::fromLatin1("NEE"));
    00104     d_data->labelMap.insert(112.5, QString::fromLatin1("SEE"));
    00105     d_data->labelMap.insert(157.5, QString::fromLatin1("SSE"));
    00106     d_data->labelMap.insert(202.5, QString::fromLatin1("SSW"));
    00107     d_data->labelMap.insert(247.5, QString::fromLatin1("SWW"));
    00108     d_data->labelMap.insert(292.5, QString::fromLatin1("NWW"));
    00109     d_data->labelMap.insert(337.5, QString::fromLatin1("NNW"));
    00110 #endif
    00111 }
    00112
    00114 void QwtCompass::drawScaleContents(QPainter *painter,
    00115         const QPoint &center, int radius) const
    00116 {
    00117     QPalette::ColorGroup cg;
    00118     if ( isEnabled() )
    00119         cg = hasFocus() ? QPalette::Active : QPalette::Inactive;
    00120     else
    00121         cg = QPalette::Disabled;
    00122
    00123     double north = origin();
    00124     if ( isValid() )
    00125     {
    00126         if ( mode() == RotateScale )
    00127             north -= value();
    00128     }
    00129
    00130     const int margin = 4;
    00131     drawRose(painter, center, radius - margin, 360.0 - north,  cg);
    00132 }
    00133
    00143 void QwtCompass::drawRose(QPainter *painter, const QPoint &center,
    00144     int radius, double north, QPalette::ColorGroup cg) const
    00145 {
    00146     if ( d_data->rose )
    00147         d_data->rose->draw(painter, center, radius, north,  cg);
    00148 }
    00149
    00157 void QwtCompass::setRose(QwtCompassRose *rose)
    00158 {
    00159     if ( rose != d_data->rose )
    00160     {
    00161         if ( d_data->rose )
    00162             delete d_data->rose;
    00163
    00164         d_data->rose = rose;
    00165         update();
    00166     }
    00167 }
    00168
    00173 const QwtCompassRose *QwtCompass::rose() const
    00174 {
    00175     return d_data->rose;
    00176 }
    00177
    00182 QwtCompassRose *QwtCompass::rose()
    00183 {
    00184     return d_data->rose;
    00185 }
    00186
    00196 void QwtCompass::keyPressEvent(QKeyEvent *kev)
    00197 {
    00198     if (isReadOnly())
    00199         return;
    00200
    00201 #if 0
    00202     if ( kev->key() == Key_5 )
    00203     {
    00204         invalidate(); // signal ???
    00205         return;
    00206     }
    00207 #endif
    00208
    00209     double newValue = value();
    00210
    00211     if ( kev->key() >= Qt::Key_1 && kev->key() <= Qt::Key_9 )
    00212     {
    00213         if ( mode() != RotateNeedle || kev->key() == Qt::Key_5 )
    00214             return;
    00215
    00216         switch (kev->key())
    00217         {
    00218             case Qt::Key_6:
    00219                 newValue = 180.0 * 0.0;
    00220                 break;
    00221             case Qt::Key_3:
    00222                 newValue = 180.0 * 0.25;
    00223                 break;
    00224             case Qt::Key_2:
    00225                 newValue = 180.0 * 0.5;
    00226                 break;
    00227             case Qt::Key_1:
    00228                 newValue = 180.0 * 0.75;
    00229                 break;
    00230             case Qt::Key_4:
    00231                 newValue = 180.0 * 1.0;
    00232                 break;
    00233             case Qt::Key_7:
    00234                 newValue = 180.0 * 1.25;
    00235                 break;
    00236             case Qt::Key_8:
    00237                 newValue = 180.0 * 1.5;
    00238                 break;
    00239             case Qt::Key_9:
    00240                 newValue = 180.0 * 1.75;
    00241                 break;
    00242         }
    00243         newValue -= origin();
    00244         setValue(newValue);
    00245     }
    00246     else
    00247     {
    00248         QwtDial::keyPressEvent(kev);
    00249     }
    00250 }
    00251
    00256 const QMap<double, QString> &QwtCompass::labelMap() const
    00257 {
    00258     return d_data->labelMap;
    00259 }
    00260
    00265 QMap<double, QString> &QwtCompass::labelMap()
    00266 {
    00267     return d_data->labelMap;
    00268 }
    00269
    00282 void QwtCompass::setLabelMap(const QMap<double, QString> &map)
    00283 {
    00284     d_data->labelMap = map;
    00285 }
    00286
    00297 QwtText QwtCompass::scaleLabel(double value) const
    00298 {
    00299 #if 0
    00300     // better solution ???
    00301     if ( value == -0 )
    00302         value = 0.0;
    00303 #endif
    00304
    00305     if ( value < 0.0 )
    00306         value += 360.0;
    00307
    00308     if ( d_data->labelMap.contains(value) )
    00309         return d_data->labelMap[value];
    00310
    00311     return QwtText();
    00312 }