Back to index

nux  3.0.0
DoubleValidator.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "Nux.h"
00024 #include "DoubleValidator.h"
00025 
00026 namespace nux
00027 {
00028 
00029   DoubleValidator::DoubleValidator(double Minimum, double Maximum)
00030     :   m_Minimum(Minimum)
00031     ,   m_Maximum(Maximum)
00032     ,   m_Decimals(3)
00033   {
00034     _regexp_str = "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$";
00035 
00036     InitRegExp();
00037 
00038     if (m_Minimum > m_Maximum)
00039     {
00040       double temp = m_Minimum;
00041       m_Minimum = m_Maximum;
00042       m_Maximum = temp;
00043     }
00044   }
00045 
00046   DoubleValidator::DoubleValidator(const DoubleValidator &copy)
00047   {
00048     m_Minimum   = copy.m_Minimum;
00049     m_Minimum   = copy.m_Maximum;
00050     _regexp_str = copy._regexp_str;
00051     InitRegExp();
00052   }
00053 
00054   DoubleValidator &DoubleValidator::operator= (const DoubleValidator &rhs)
00055   {
00056     if (&rhs != this)
00057     {
00058       m_Minimum   = rhs.m_Minimum;
00059       m_Minimum   = rhs.m_Maximum;
00060       _regexp_str = rhs._regexp_str;
00061       InitRegExp();
00062     }
00063 
00064     return *this;
00065   }
00066 
00067   DoubleValidator::~DoubleValidator()
00068   {
00069   }
00070 
00071   Validator *DoubleValidator::Clone()  const
00072   {
00073     return new DoubleValidator(*this);
00074   }
00075 
00076   void DoubleValidator::SetMinimum(double value)
00077   {
00078     m_Minimum = value;
00079 
00080     if (m_Minimum > m_Maximum)
00081     {
00082       double temp = m_Minimum;
00083       m_Minimum = m_Maximum;
00084       m_Maximum = temp;
00085     }
00086   }
00087 
00088   double DoubleValidator::GetMinimum() const
00089   {
00090     return m_Minimum;
00091   }
00092 
00093   void DoubleValidator::SetMaximum(double value)
00094   {
00095     m_Maximum = value;
00096 
00097     if (m_Minimum > m_Maximum)
00098     {
00099       double temp = m_Minimum;
00100       m_Minimum = m_Maximum;
00101       m_Maximum = temp;
00102     }
00103   }
00104 
00105   double DoubleValidator::GetMaximum() const
00106   {
00107     return m_Maximum;
00108   }
00109 
00110   double DoubleValidator::GetClampedValue(double d) const
00111   {
00112     if (d < m_Minimum)
00113       return m_Minimum;
00114 
00115     if (d > m_Maximum)
00116       return m_Maximum;
00117 
00118     return d;
00119   }
00120 
00121   void DoubleValidator::Alternative(const char *str)
00122   {
00123     str = "0.0";
00124   }
00125 
00126   void DoubleValidator::SetDecimals(int dec)
00127   {
00128     m_Decimals = Clamp<int> (dec, 0, 13);
00129   }
00130 
00131   NString DoubleValidator::ToString(double d)
00132   {
00133     NString Prec = NString("%.") + NString::Printf("%d", m_Decimals) + NString("f");
00134     return NString::Printf(Prec.GetTCharPtr(), d);
00135   }
00136 
00137   double DoubleValidator::ToDouble(const char *str)
00138   {
00139     if (Validate(str) == Acceptable)
00140       return CharToDouble(str);
00141     else
00142       return 0.0;
00143   }
00144 
00145 }