Main Page | Class Hierarchy | Alphabetical List | Class List | 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_scldraw.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 00034 QwtCompass::QwtCompass(QWidget* parent, const char* name): 00035 QwtDial(parent, name), 00036 d_rose(0) 00037 { 00038 setScaleOptions(ScaleLabel); // Only labels, no backbone, no ticks 00039 00040 setOrigin(270.0); 00041 setWrapping(TRUE); 00042 00043 00044 d_labelMap.insert(0.0, QString::fromLatin1("N")); 00045 d_labelMap.insert(45.0, QString::fromLatin1("NE")); 00046 d_labelMap.insert(90.0, QString::fromLatin1("E")); 00047 d_labelMap.insert(135.0, QString::fromLatin1("SE")); 00048 d_labelMap.insert(180.0, QString::fromLatin1("S")); 00049 d_labelMap.insert(225.0, QString::fromLatin1("SW")); 00050 d_labelMap.insert(270.0, QString::fromLatin1("W")); 00051 d_labelMap.insert(315.0, QString::fromLatin1("NW")); 00052 00053 #if 0 00054 d_labelMap.insert(22.5, QString::fromLatin1("NNE")); 00055 d_labelMap.insert(67.5, QString::fromLatin1("NEE")); 00056 d_labelMap.insert(112.5, QString::fromLatin1("SEE")); 00057 d_labelMap.insert(157.5, QString::fromLatin1("SSE")); 00058 d_labelMap.insert(202.5, QString::fromLatin1("SSW")); 00059 d_labelMap.insert(247.5, QString::fromLatin1("SWW")); 00060 d_labelMap.insert(292.5, QString::fromLatin1("NWW")); 00061 d_labelMap.insert(337.5, QString::fromLatin1("NNW")); 00062 #endif 00063 } 00064 00066 QwtCompass::~QwtCompass() 00067 { 00068 delete d_rose; 00069 } 00070 00072 void QwtCompass::drawScaleContents(QPainter *painter, 00073 const QPoint &center, int radius) const 00074 { 00075 QPalette::ColorGroup cg; 00076 if ( isEnabled() ) 00077 cg = hasFocus() ? QPalette::Active : QPalette::Inactive; 00078 else 00079 cg = QPalette::Disabled; 00080 00081 double north = origin(); 00082 if ( isValid() ) 00083 { 00084 if ( mode() == RotateScale ) 00085 north -= value(); 00086 } 00087 00088 const int margin = 4; 00089 drawRose(painter, center, radius - margin, 360.0 - north, cg); 00090 } 00091 00101 void QwtCompass::drawRose(QPainter *painter, const QPoint &center, 00102 int radius, double north, QPalette::ColorGroup cg) const 00103 { 00104 if ( d_rose ) 00105 d_rose->draw(painter, center, radius, north, cg); 00106 } 00107 00115 void QwtCompass::setRose(QwtCompassRose *rose) 00116 { 00117 if ( rose != d_rose ) 00118 { 00119 if ( d_rose ) 00120 delete d_rose; 00121 00122 d_rose = rose; 00123 update(); 00124 } 00125 } 00126 00131 const QwtCompassRose *QwtCompass::rose() const 00132 { 00133 return d_rose; 00134 } 00135 00140 QwtCompassRose *QwtCompass::rose() 00141 { 00142 return d_rose; 00143 } 00144 00154 void QwtCompass::keyPressEvent(QKeyEvent *kev) 00155 { 00156 if (isReadOnly()) 00157 return; 00158 00159 #if 0 00160 if ( kev->key() == Key_5 ) 00161 { 00162 invalidate(); // signal ??? 00163 return; 00164 } 00165 #endif 00166 00167 double newValue = value(); 00168 00169 if ( kev->key() >= Qt::Key_1 && kev->key() <= Qt::Key_9 ) 00170 { 00171 if ( mode() != RotateNeedle || kev->key() == Qt::Key_5 ) 00172 return; 00173 00174 switch (kev->key()) 00175 { 00176 case Qt::Key_6: 00177 newValue = 180.0 * 0.0; 00178 break; 00179 case Qt::Key_3: 00180 newValue = 180.0 * 0.25; 00181 break; 00182 case Qt::Key_2: 00183 newValue = 180.0 * 0.5; 00184 break; 00185 case Qt::Key_1: 00186 newValue = 180.0 * 0.75; 00187 break; 00188 case Qt::Key_4: 00189 newValue = 180.0 * 1.0; 00190 break; 00191 case Qt::Key_7: 00192 newValue = 180.0 * 1.25; 00193 break; 00194 case Qt::Key_8: 00195 newValue = 180.0 * 1.5; 00196 break; 00197 case Qt::Key_9: 00198 newValue = 180.0 * 1.75; 00199 break; 00200 } 00201 newValue -= origin(); 00202 setValue(newValue); 00203 } 00204 else 00205 { 00206 QwtDial::keyPressEvent(kev); 00207 } 00208 } 00209 00214 const QMap<double, QString> &QwtCompass::labelMap() const 00215 { 00216 return d_labelMap; 00217 } 00218 00223 QMap<double, QString> &QwtCompass::labelMap() 00224 { 00225 return d_labelMap; 00226 } 00227 00240 void QwtCompass::setLabelMap(const QMap<double, QString> &map) 00241 { 00242 d_labelMap = map; 00243 } 00244 00255 QString QwtCompass::scaleLabel(double value) const 00256 { 00257 #if 0 00258 // better solution ??? 00259 if ( value == -0 ) 00260 value = 0.0; 00261 #endif 00262 00263 if ( value < 0.0 ) 00264 value += 360.0; 00265 00266 if ( d_labelMap.contains(value) ) 00267 return d_labelMap[value]; 00268 00269 return QString::null; 00270 } 00271 00272 // Local Variables: 00273 // mode: C++ 00274 // c-file-style: "stroustrup" 00275 // indent-tabs-mode: nil 00276 // End:

Generated on Tue Nov 16 21:12:20 2004 for Qwt User's Guide by doxygen 1.3.8