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

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     d_data = new PrivateData;
00054 
00055     setScaleOptions(ScaleLabel); // Only labels, no backbone, no ticks
00056 
00057     setOrigin(270.0);
00058     setWrapping(true);
00059 
00060 
00061     d_data->labelMap.insert(0.0, QString::fromLatin1("N"));
00062     d_data->labelMap.insert(45.0, QString::fromLatin1("NE"));
00063     d_data->labelMap.insert(90.0, QString::fromLatin1("E"));
00064     d_data->labelMap.insert(135.0, QString::fromLatin1("SE"));
00065     d_data->labelMap.insert(180.0, QString::fromLatin1("S"));
00066     d_data->labelMap.insert(225.0, QString::fromLatin1("SW"));
00067     d_data->labelMap.insert(270.0, QString::fromLatin1("W"));
00068     d_data->labelMap.insert(315.0, QString::fromLatin1("NW"));
00069 
00070 #if 0
00071     d_data->labelMap.insert(22.5, QString::fromLatin1("NNE"));
00072     d_data->labelMap.insert(67.5, QString::fromLatin1("NEE"));
00073     d_data->labelMap.insert(112.5, QString::fromLatin1("SEE"));
00074     d_data->labelMap.insert(157.5, QString::fromLatin1("SSE"));
00075     d_data->labelMap.insert(202.5, QString::fromLatin1("SSW"));
00076     d_data->labelMap.insert(247.5, QString::fromLatin1("SWW"));
00077     d_data->labelMap.insert(292.5, QString::fromLatin1("NWW"));
00078     d_data->labelMap.insert(337.5, QString::fromLatin1("NNW"));
00079 #endif
00080 }
00081 
00083 QwtCompass::~QwtCompass() 
00084 {
00085     delete d_data;
00086 }
00087 
00089 void QwtCompass::drawScaleContents(QPainter *painter, 
00090         const QPoint &center, int radius) const
00091 {
00092     QPalette::ColorGroup cg;
00093     if ( isEnabled() )
00094         cg = hasFocus() ? QPalette::Active : QPalette::Inactive;
00095     else
00096         cg = QPalette::Disabled;
00097 
00098     double north = origin();
00099     if ( isValid() )
00100     {
00101         if ( mode() == RotateScale )
00102             north -= value(); 
00103     }
00104 
00105     const int margin = 4;
00106     drawRose(painter, center, radius - margin, 360.0 - north,  cg);
00107 }
00108 
00118 void QwtCompass::drawRose(QPainter *painter, const QPoint &center,
00119     int radius, double north, QPalette::ColorGroup cg) const
00120 {
00121     if ( d_data->rose )
00122         d_data->rose->draw(painter, center, radius, north,  cg);
00123 }
00124 
00132 void QwtCompass::setRose(QwtCompassRose *rose)
00133 {
00134     if ( rose != d_data->rose )
00135     {
00136         if ( d_data->rose )
00137             delete d_data->rose;
00138 
00139         d_data->rose = rose;
00140         update();
00141     }
00142 }
00143 
00148 const QwtCompassRose *QwtCompass::rose() const 
00149 { 
00150     return d_data->rose; 
00151 }
00152 
00157 QwtCompassRose *QwtCompass::rose() 
00158 { 
00159     return d_data->rose; 
00160 }
00161 
00171 void QwtCompass::keyPressEvent(QKeyEvent *kev) 
00172 {
00173     if (isReadOnly()) 
00174         return;
00175 
00176 #if 0
00177     if ( kev->key() == Key_5 )
00178     {
00179         invalidate(); // signal ???
00180         return;
00181     }
00182 #endif
00183 
00184     double newValue = value();
00185 
00186     if ( kev->key() >= Qt::Key_1 && kev->key() <= Qt::Key_9 )
00187     {
00188         if ( mode() != RotateNeedle || kev->key() == Qt::Key_5 )
00189             return;
00190 
00191         switch (kev->key()) 
00192         {
00193             case Qt::Key_6: 
00194                 newValue = 180.0 * 0.0;
00195                 break;
00196             case Qt::Key_3: 
00197                 newValue = 180.0 * 0.25;
00198                 break;
00199             case Qt::Key_2: 
00200                 newValue = 180.0 * 0.5;
00201                 break;
00202             case Qt::Key_1: 
00203                 newValue = 180.0 * 0.75;
00204                 break;
00205             case Qt::Key_4: 
00206                 newValue = 180.0 * 1.0;
00207                 break;
00208             case Qt::Key_7: 
00209                 newValue = 180.0 * 1.25;
00210                 break;
00211             case Qt::Key_8: 
00212                 newValue = 180.0 * 1.5;
00213                 break;
00214             case Qt::Key_9: 
00215                 newValue = 180.0 * 1.75;
00216                 break;
00217         }
00218         newValue -= origin();
00219         setValue(newValue);
00220     }
00221     else
00222     {
00223         QwtDial::keyPressEvent(kev);
00224     }
00225 }
00226 
00231 const QMap<double, QString> &QwtCompass::labelMap() const 
00232 { 
00233     return d_data->labelMap; 
00234 }
00235 
00240 QMap<double, QString> &QwtCompass::labelMap() 
00241 { 
00242     return d_data->labelMap; 
00243 }
00244 
00257 void QwtCompass::setLabelMap(const QMap<double, QString> &map) 
00258 { 
00259     d_data->labelMap = map; 
00260 }
00261 
00272 QwtText QwtCompass::scaleLabel(double value) const
00273 {
00274 #if 0
00275     // better solution ???
00276     if ( value == -0 )
00277         value = 0.0;
00278 #endif
00279 
00280     if ( value < 0.0 )
00281         value += 360.0;
00282 
00283     if ( d_data->labelMap.contains(value) )
00284         return d_data->labelMap[value];
00285 
00286     return QwtText();
00287 }

Generated on Mon Jan 30 22:16:25 2006 for Qwt User's Guide by  doxygen 1.4.4