SoFCColorBar.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <Inventor/nodes/SoEventCallback.h>
00027 # include <Inventor/actions/SoGLRenderAction.h>
00028 # include <Inventor/nodes/SoSwitch.h>
00029 # include <Inventor/events/SoMouseButtonEvent.h>
00030 # include <QApplication>
00031 # include <QMenu>
00032 #endif
00033
00034 #include "SoFCColorBar.h"
00035 #include "SoFCColorGradient.h"
00036 #include "SoFCColorLegend.h"
00037
00038 using namespace Gui;
00039
00040 SO_NODE_ABSTRACT_SOURCE(SoFCColorBarBase);
00041
00045 SoFCColorBarBase::SoFCColorBarBase()
00046 {
00047 SO_NODE_CONSTRUCTOR(SoFCColorBarBase);
00048 }
00049
00053 SoFCColorBarBase::~SoFCColorBarBase()
00054 {
00055
00056 }
00057
00058
00059 void SoFCColorBarBase::initClass(void)
00060 {
00061 SO_NODE_INIT_ABSTRACT_CLASS(SoFCColorBarBase,SoSeparator,"Separator");
00062 }
00063
00064 void SoFCColorBarBase::finish()
00065 {
00066 atexit_cleanup();
00067 }
00068
00069 void SoFCColorBarBase::GLRenderBelowPath(SoGLRenderAction * action)
00070 {
00071 const SbViewportRegion& vp = action->getViewportRegion();
00072 const SbVec2s& size = vp.getWindowSize();
00073 if (_windowSize != size) {
00074 _windowSize = size;
00075 setViewportSize(size);
00076 }
00077 SoSeparator::GLRenderBelowPath(action);
00078 }
00079
00080
00081
00082 namespace Gui {
00083
00084 class SoFCColorBarProxyObject : public QObject
00085 {
00086 public:
00087 SoFCColorBarProxyObject(SoFCColorBar* b)
00088 : QObject(0), bar(b) {}
00089 ~SoFCColorBarProxyObject() {}
00090 void customEvent(QEvent *)
00091 {
00092 if (bar->customize())
00093 bar->Notify(0);
00094 this->deleteLater();
00095 }
00096
00097 private:
00098 SoFCColorBar* bar;
00099 };
00100 }
00101
00102 SO_NODE_SOURCE(SoFCColorBar);
00103
00107 SoFCColorBar::SoFCColorBar()
00108 {
00109 SO_NODE_CONSTRUCTOR(SoFCColorBar);
00110
00111
00112
00113
00114
00115 pColorMode = new SoSwitch;
00116 addChild(pColorMode);
00117
00118 _colorBars.push_back( new SoFCColorGradient );
00119 _colorBars.push_back( new SoFCColorLegend );
00120
00121 for (std::vector<SoFCColorBarBase*>::const_iterator it = _colorBars.begin(); it != _colorBars.end(); ++it)
00122 pColorMode->addChild( *it );
00123 pColorMode->whichChild = 0;
00124 }
00125
00129 SoFCColorBar::~SoFCColorBar()
00130 {
00131
00132 }
00133
00134
00135 void SoFCColorBar::initClass(void)
00136 {
00137 SO_NODE_INIT_CLASS(SoFCColorBar,SoFCColorBarBase,"Separator");
00138 }
00139
00140 void SoFCColorBar::finish()
00141 {
00142 atexit_cleanup();
00143 }
00144
00145 SoFCColorBarBase* SoFCColorBar::getActiveBar() const
00146 {
00147 int child = pColorMode->whichChild.getValue();
00148 return _colorBars[child];
00149 }
00150
00151 void SoFCColorBar::setViewportSize( const SbVec2s& size )
00152 {
00153
00154 float fRatio = ((float)size[0])/((float)size[1]);
00155 _fMinX= 4.0f, _fMaxX=4.5f;
00156 _fMinY= -4.0f, _fMaxY=4.0f;
00157 if (fRatio > 1.0f) {
00158 _fMinX = 4.0f * fRatio;
00159 _fMaxX = _fMinX+0.5f;
00160 }
00161 else if (fRatio < 1.0f) {
00162 _fMinY = -4.0f / fRatio;
00163 _fMaxY = 4.0f / fRatio;
00164 }
00165 }
00166
00167 void SoFCColorBar::setRange( float fMin, float fMax, int prec )
00168 {
00169 for (std::vector<SoFCColorBarBase*>::const_iterator it = _colorBars.begin(); it != _colorBars.end(); ++it)
00170 (*it)->setRange(fMin, fMax, prec);
00171 }
00172
00173 void SoFCColorBar::setOutsideGrayed (bool bVal)
00174 {
00175 for (std::vector<SoFCColorBarBase*>::const_iterator it = _colorBars.begin(); it != _colorBars.end(); ++it)
00176 (*it)->setOutsideGrayed(bVal);
00177 }
00178
00179 bool SoFCColorBar::isVisible (float fVal) const
00180 {
00181 return this->getActiveBar()->isVisible(fVal);
00182 }
00183
00184 float SoFCColorBar::getMinValue (void) const
00185 {
00186 return this->getActiveBar()->getMinValue();
00187 }
00188
00189 float SoFCColorBar::getMaxValue (void) const
00190 {
00191 return this->getActiveBar()->getMaxValue();
00192 }
00193
00194 bool SoFCColorBar::customize()
00195 {
00196 return this->getActiveBar()->customize();
00197 }
00198
00199 App::Color SoFCColorBar::getColor( float fVal ) const
00200 {
00201 return this->getActiveBar()->getColor( fVal );
00202 }
00203
00204 void SoFCColorBar::eventCallback(void * userdata, SoEventCallback * node)
00205 {
00206 const SoEvent * event = node->getEvent();
00207 if (event->getTypeId().isDerivedFrom(SoMouseButtonEvent::getClassTypeId())) {
00208 const SoMouseButtonEvent* e = static_cast<const SoMouseButtonEvent*>(event);
00209 if ((e->getButton() == SoMouseButtonEvent::BUTTON2)) {
00210 if (e->getState() == SoButtonEvent::UP) {
00211 }
00212 }
00213 }
00214 }
00215
00216 void SoFCColorBar::handleEvent (SoHandleEventAction *action)
00217 {
00218 const SoEvent * event = action->getEvent();
00219
00220
00221 if (event->getTypeId().isDerivedFrom(SoMouseButtonEvent::getClassTypeId())) {
00222 const SoMouseButtonEvent* e = static_cast<const SoMouseButtonEvent*>(event);
00223
00224
00225
00226 const SbViewportRegion& vp = action->getViewportRegion();
00227 float fRatio = vp.getViewportAspectRatio();
00228 SbVec2f pos = event->getNormalizedPosition(vp);
00229 float pX,pY; pos.getValue(pX,pY);
00230
00231 pX = pX*10.0f-5.0f;
00232 pY = pY*10.0f-5.0f;
00233
00234
00235
00236 if (fRatio > 1.0f) {
00237 pX = pX * fRatio;
00238 }
00239 else if (fRatio < 1.0f) {
00240 pY = pY / fRatio;
00241 }
00242
00243
00244 if (_fMinX > pX || pX > _fMaxX || _fMinY > pY || pY > _fMaxY)
00245 return;
00246
00247
00248 action->setHandled();
00249 if ((e->getButton() == SoMouseButtonEvent::BUTTON1)) {
00250 if (e->getState() == SoButtonEvent::DOWN) {
00251
00252 if (_timer.restart() < QApplication::doubleClickInterval()) {
00253 QApplication::postEvent(
00254 new SoFCColorBarProxyObject(this),
00255 new QEvent(QEvent::User));
00256 }
00257 }
00258 }
00259
00260 else if ((e->getButton() == SoMouseButtonEvent::BUTTON2)) {
00261 if (e->getState() == SoButtonEvent::UP) {
00262 SoFCColorBarBase* current = getActiveBar();
00263 QMenu menu;
00264 int i=0;
00265 for (std::vector<SoFCColorBarBase*>::const_iterator it = _colorBars.begin(); it != _colorBars.end(); ++it) {
00266 QAction* item = menu.addAction(QLatin1String((*it)->getColorBarName()));
00267 item->setCheckable(true);
00268 item->setChecked((*it) == current);
00269 item->setData(QVariant(i++));
00270 }
00271
00272 menu.addSeparator();
00273 QAction* option = menu.addAction(QObject::tr("Options..."));
00274 QAction* action = menu.exec(QCursor::pos());
00275
00276 if (action == option) {
00277 QApplication::postEvent(
00278 new SoFCColorBarProxyObject(this),
00279 new QEvent(QEvent::User));
00280 }
00281 else if (action) {
00282 int id = action->data().toInt();
00283 pColorMode->whichChild = id;
00284 }
00285 }
00286 }
00287 }
00288 }