WFloatLineEdit.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This program is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU General Public License as published by the Free  *
00007 * Software Foundation; either version 2 of the License, or (at your option)   *
00008 * any later version.                                                          *
00009 *                                                                             *
00010 * This program is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for    *
00013 * more details.                                                               *
00014 *                                                                             *
00015 * You should have received a copy of the GNU General Public License along     *
00016 * with this program; if not, write to the Free Software Foundation, Inc., 51  *
00017 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.                   *
00018 *******************************************************************************
00019 *                            SOFA :: Applications                             *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
00026 ******************************************************************************/
00027 /* -------------------------------------------------------- */
00028 #include <math.h>
00029 #include "WFloatLineEdit.h"
00030 #include <iostream>
00031 using std::cerr; using std::endl;
00032 /* -------------------------------------------------------- */
00033 
00034 WFloatLineEdit::WFloatLineEdit(QWidget *parent,const char *name) : QLineEdit(parent,name)
00035 {
00036   m_iPercent=-1;
00037   m_fMinValue=0.0;
00038   m_fMaxValue=1.0;
00039   m_fValue=0.0;
00040   m_bFirst=true;
00041   m_DblValid=new QDoubleValidator(m_fMinValue,m_fMaxValue,6,this);
00042   setValidator(m_DblValid);
00043 
00044   connect(this,SIGNAL(returnPressed()),
00045       this,SLOT  (slotReturnPressed()));
00046 
00047   m_bInternal=false;
00048   validateAndSet(QString("%1").arg(m_fValue),0,0,0);
00049 }
00050 /* -------------------------------------------------------- */
00051 void WFloatLineEdit::slotReturnPressed()
00052 {
00053   //cerr<<"WFloatLineEdit::slotReturnPressed"<<endl;
00054   m_bInternal=true;
00055 
00056   slotCalcFloatValue(text().toFloat());
00057 
00058 }
00059 /* -------------------------------------------------------- */
00060 void WFloatLineEdit::slotCalcFloatValue(float f)
00061 {
00062   int    p;
00063   
00064   //cerr << "WFloatLineEdit::slotCalcFloatValue" << endl;
00065   if (f < m_fMinValue)
00066     f=m_fMinValue;
00067   else if (f > m_fMaxValue)
00068     f=m_fMaxValue;
00069   if (f != m_fValue || m_bFirst)
00070   {
00071     m_bFirst=false;
00072     m_fValue=f;
00073     //cerr << "WFloatLineEdit::slotCalcFloatValue m_fValue = " << m_fValue << endl;
00074     emit (floatValueChanged(f));
00075     p=(int)(100.0*(f - m_fMinValue)/(m_fMaxValue - m_fMinValue));
00076     if (p != m_iPercent)
00077     {
00078 //      cerr << "m_iPercent = " << m_iPercent << endl;
00079       emit (valuePercentChanged(p));
00080       m_iPercent=p;
00081     }
00082     update();
00083   }
00084   validateAndSet(QString("%1").arg(m_fValue),0,0,0);
00085 }
00086 /* -------------------------------------------------------- */
00087 void WFloatLineEdit::slotCalcFloatValue(const QString& s)
00088 {
00089   slotCalcFloatValue(s.toFloat());
00090 }
00091 /* -------------------------------------------------------- */
00092 void WFloatLineEdit::setFloatValue(float f) 
00093 {
00094   m_bInternal=true;
00095   slotCalcFloatValue(f);
00096 }
00097 
00098 void WFloatLineEdit::setIntValue(int f)
00099 {
00100  setFloatValue(static_cast<float>(f));
00101 }
00102 /* -------------------------------------------------------- */
00103 void WFloatLineEdit::setValuePercent(int p)
00104 {
00105   if (!m_bInternal)
00106     setFloatValue(m_fMinValue + (m_fMaxValue -
00107                  m_fMinValue)*((double)p)/99.0);
00108   else
00109     m_bInternal=false;
00110 }
00111 /* -------------------------------------------------------- */
00112 int WFloatLineEdit::valuePercent()
00113 {
00114   return ((int)(99.0*(m_fValue - m_fMinValue)/(m_fMaxValue - m_fMinValue)));
00115 }
00116 /* -------------------------------------------------------- */
00117 void WFloatLineEdit::keyPressEvent(QKeyEvent *e)
00118 {
00119   if (e->key() == Qt::Key_Escape)
00120     validateAndSet(QString("%1").arg(m_fValue),0,0,0);
00121   else
00122     QLineEdit::keyPressEvent(e);
00123 }
00124 /* -------------------------------------------------------- */
00125 

Generated on Sun Nov 23 02:28:39 2008 for Sofa by  doxygen 1.5.7.1