SoFCColorBar.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
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     //delete THIS;
00056 }
00057 
00058 // doc from parent
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 // Proxy class that receives an asynchronous custom event
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 //  SoEventCallback * cb = new SoEventCallback;
00112 //  cb->addEventCallback(SoMouseButtonEvent::getClassTypeId(), eventCallback, this);
00113 //  insertChild(cb, 0);
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     //delete THIS;
00132 }
00133 
00134 // doc from parent
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     // don't know why the parameter range isn't between [-1,+1]
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     // check for mouse button events
00221     if (event->getTypeId().isDerivedFrom(SoMouseButtonEvent::getClassTypeId())) {
00222         const SoMouseButtonEvent*  e = static_cast<const SoMouseButtonEvent*>(event);
00223 
00224         // calculate the mouse position relative to the colorbar
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         // now calculate the real points respecting aspect ratio information
00235         //
00236         if (fRatio > 1.0f) {
00237             pX = pX * fRatio;
00238         }
00239         else if (fRatio < 1.0f) {
00240             pY = pY / fRatio;
00241         }
00242 
00243         // check if the cursor is near to the color bar
00244         if (_fMinX > pX || pX > _fMaxX || _fMinY > pY || pY > _fMaxY)
00245             return; // not inside the rectangle
00246 
00247         // left mouse pressed
00248         action->setHandled();
00249         if ((e->getButton() == SoMouseButtonEvent::BUTTON1)) {
00250             if (e->getState() == SoButtonEvent::DOWN) {
00251                 // double click event
00252                 if (_timer.restart() < QApplication::doubleClickInterval()) {
00253                     QApplication::postEvent(
00254                         new SoFCColorBarProxyObject(this),
00255                         new QEvent(QEvent::User));
00256                 }
00257             }
00258         }
00259         // right mouse pressed
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 }

Generated on Wed Nov 23 19:00:39 2011 for FreeCAD by  doxygen 1.6.1