nux-1.14.0
NumericValuator.cpp
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 "HLayout.h"
00025 #include "EditTextBox.h"
00026 #include "DoubleValidator.h"
00027 #include "NumericValuator.h"
00028 
00029 namespace nux
00030 {
00031 
00032   const int BTN_WIDTH = 14;
00033   const int BTN_HEIGHT = 14;
00034 
00035   NumericValuator::NumericValuator()
00036     :   m_DoubleValidator (0.0, 100.0)
00037     ,   m_Step (0.1f)
00038   {
00039     InitializeLayout();
00040     InitializeWidgets();
00041   }
00042 
00043   NumericValuator::~NumericValuator()
00044   {
00045   }
00046 
00047   void NumericValuator::InitializeWidgets()
00048   {
00049     m_EditLine->SetValidator (&m_DoubleValidator);
00050     m_EditLine->SetText (NString::Printf (TEXT ("%d"), m_DoubleValidator.GetMinimum ()));
00051 
00052     m_EditLine->SetMinimumSize (2 * DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT);
00053     m_EditLine->SetGeometry (Geometry (0, 0, 2 * DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT) );
00054 
00055     m_SpinnerDownBtn->SetMinimumSize (BTN_WIDTH, BTN_HEIGHT);
00056     m_SpinnerDownBtn->SetGeometry (Geometry (0, 0, BTN_WIDTH, BTN_HEIGHT) );
00057     m_SpinnerUpBtn->SetMinimumSize (BTN_WIDTH, BTN_HEIGHT);
00058     m_SpinnerUpBtn->SetGeometry (Geometry (0, 0, BTN_WIDTH, BTN_HEIGHT) );
00059 
00060     hlayout->AddView (m_SpinnerDownBtn, 0);
00061     hlayout->AddView (m_EditLine, 1);
00062     hlayout->AddView (m_SpinnerUpBtn, 0);
00063     hlayout->SetContentDistribution (eStackLeft);
00064 
00065     SetCompositionLayout (hlayout);
00066   }
00067 
00068   void NumericValuator::InitializeLayout()
00069   {
00070     hlayout = new HLayout (NUX_TRACKER_LOCATION);
00071   }
00072 
00073   long NumericValuator::ProcessEvent (IEvent &ievent, long TraverseInfo, long ProcessEventInfo)
00074   {
00075     long ret = TraverseInfo;
00076     ret = m_SpinnerDownBtn->OnEvent (ievent, ret, ProcessEventInfo);
00077     ret = m_SpinnerUpBtn->OnEvent (ievent, ret, ProcessEventInfo);
00078     ret = m_EditLine->ProcessEvent (ievent, ret, ProcessEventInfo);
00079     ret = PostProcessEvent2 (ievent, ret, ProcessEventInfo);
00080     return ret;
00081   };
00082 
00083   void NumericValuator::Draw (GraphicsEngine &GfxContext, bool force_draw)
00084   {
00085     Geometry base = GetGeometry();
00086 
00087     GeometryPositioning gp (eHALeft, eVACenter);
00088     Geometry GeoPo = ComputeGeometryPositioning (m_SpinnerUpBtn->GetGeometry(), GetTheme().GetImageGeometry (eTRIANGLE_RIGHT), gp);
00089     GetPainter().PaintShape (GfxContext, GeoPo, Color (0xFFFFFFFF), eTRIANGLE_RIGHT);
00090 
00091     GeoPo = ComputeGeometryPositioning (m_SpinnerDownBtn->GetGeometry(), GetTheme().GetImageGeometry (eTRIANGLE_LEFT), gp);
00092     GetPainter().PaintShape (GfxContext, GeoPo, Color (0xFFFFFFFF), eTRIANGLE_LEFT);
00093 
00094     m_EditLine->QueueDraw();
00095   }
00096 
00097   void NumericValuator::DrawContent (GraphicsEngine &GfxContext, bool force_draw)
00098   {
00099     m_EditLine->ProcessDraw (GfxContext, force_draw);
00100   }
00101 
00102   void NumericValuator::PostDraw (GraphicsEngine &GfxContext, bool force_draw)
00103   {
00104 
00105   }
00106 
00107   void NumericValuator::SetValue (float value)
00108   {
00109     m_fValue = value;
00110 
00111     if (m_fValue < m_DoubleValidator.GetMinimum() )
00112       m_fValue = m_DoubleValidator.GetMinimum();
00113 
00114     if (m_fValue > m_DoubleValidator.GetMaximum() )
00115       m_fValue = m_DoubleValidator.GetMaximum();
00116 
00117     m_EditLine->SetText (NString::Printf ("%f", m_fValue) );
00118   }
00119 
00120   float NumericValuator::GetValue() const
00121   {
00122     return m_fValue;
00123   }
00124 
00125   void NumericValuator::SetStep (float f)
00126   {
00127     m_Step = f;
00128   }
00129 
00130   float NumericValuator::GetStep()
00131   {
00132     return m_Step;
00133   }
00134 
00135 
00136   void NumericValuator::ImplementIncrementBtn()
00137   {
00138     SetValue (m_fValue + m_Step);
00139     sigIncrement.emit();
00140     sigValueChanged.emit (m_fValue);
00141 
00142     if (m_fValue < m_DoubleValidator.GetMaximum() )
00143     {
00144       m_UpTimerHandler = GetTimer().AddTimerHandler (100, m_UpTimerCallback, 0);
00145       QueueDraw();
00146     }
00147   }
00148 
00149   void NumericValuator::ImplementDecrementBtn()
00150   {
00151     SetValue (m_fValue - m_Step);
00152     sigDecrement.emit();
00153     sigValueChanged.emit (m_fValue);
00154 
00155     if (m_fValue > m_DoubleValidator.GetMinimum() )
00156     {
00157       m_DownTimerHandler = GetTimer().AddTimerHandler (100, m_DownTimerCallback, 0);
00158       QueueDraw();
00159     }
00160   }
00161 
00162   void NumericValuator::ImplementValidateEntry()
00163   {
00164     double ret = 0;
00165     ret = CharToDouble (m_EditLine->GetCleanText().GetTCharPtr() );
00166     {
00167       m_fValue = ret;
00168 
00169       if (m_fValue < m_DoubleValidator.GetMinimum() )
00170       {
00171         m_fValue = m_DoubleValidator.GetMinimum();
00172         m_EditLine->SetText (NString::Printf ("%f", m_fValue) );
00173       }
00174 
00175       if (m_fValue > m_DoubleValidator.GetMaximum() )
00176       {
00177         m_fValue = m_DoubleValidator.GetMaximum();
00178         m_EditLine->SetText (NString::Printf ("%f", m_fValue) );
00179       }
00180     }
00181 //     else
00182 //     {
00183 //         m_EditLine->SetText(NString::Printf("%f", m_fValue));
00184 //     }
00185   }
00186 
00187 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends