Back to index

nux  3.0.0
NumericValuator.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 "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("%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   void NumericValuator::Draw(GraphicsEngine &graphics_engine, bool force_draw)
00074   {
00075     Geometry base = GetGeometry();
00076 
00077     GeometryPositioning gp(eHALeft, eVACenter);
00078     Geometry GeoPo = ComputeGeometryPositioning(m_SpinnerUpBtn->GetGeometry(), GetTheme().GetImageGeometry(eTRIANGLE_RIGHT), gp);
00079     GetPainter().PaintShape(graphics_engine, GeoPo, Color(0xFFFFFFFF), eTRIANGLE_RIGHT);
00080 
00081     GeoPo = ComputeGeometryPositioning(m_SpinnerDownBtn->GetGeometry(), GetTheme().GetImageGeometry(eTRIANGLE_LEFT), gp);
00082     GetPainter().PaintShape(graphics_engine, GeoPo, Color(0xFFFFFFFF), eTRIANGLE_LEFT);
00083 
00084     m_EditLine->QueueDraw();
00085   }
00086 
00087   void NumericValuator::DrawContent(GraphicsEngine &graphics_engine, bool force_draw)
00088   {
00089     m_EditLine->ProcessDraw(graphics_engine, force_draw);
00090   }
00091 
00092   void NumericValuator::PostDraw(GraphicsEngine &graphics_engine, bool force_draw)
00093   {
00094 
00095   }
00096 
00097   void NumericValuator::SetValue(float value)
00098   {
00099     m_fValue = value;
00100 
00101     if (m_fValue < m_DoubleValidator.GetMinimum())
00102       m_fValue = m_DoubleValidator.GetMinimum();
00103 
00104     if (m_fValue > m_DoubleValidator.GetMaximum())
00105       m_fValue = m_DoubleValidator.GetMaximum();
00106 
00107     m_EditLine->SetText(NString::Printf("%f", m_fValue));
00108   }
00109 
00110   float NumericValuator::GetValue() const
00111   {
00112     return m_fValue;
00113   }
00114 
00115   void NumericValuator::SetStep(float f)
00116   {
00117     m_Step = f;
00118   }
00119 
00120   float NumericValuator::GetStep()
00121   {
00122     return m_Step;
00123   }
00124 
00125 
00126   void NumericValuator::ImplementIncrementBtn()
00127   {
00128     SetValue(m_fValue + m_Step);
00129     sigIncrement.emit();
00130     sigValueChanged.emit(m_fValue);
00131 
00132     if (m_fValue < m_DoubleValidator.GetMaximum())
00133     {
00134       m_UpTimerHandler = GetTimer().AddTimerHandler(100, m_UpTimerCallback, 0);
00135       QueueDraw();
00136     }
00137   }
00138 
00139   void NumericValuator::ImplementDecrementBtn()
00140   {
00141     SetValue(m_fValue - m_Step);
00142     sigDecrement.emit();
00143     sigValueChanged.emit(m_fValue);
00144 
00145     if (m_fValue > m_DoubleValidator.GetMinimum())
00146     {
00147       m_DownTimerHandler = GetTimer().AddTimerHandler(100, m_DownTimerCallback, 0);
00148       QueueDraw();
00149     }
00150   }
00151 
00152   void NumericValuator::ImplementValidateEntry()
00153   {
00154     double ret = 0;
00155     ret = CharToDouble(m_EditLine->GetCleanText().GetTCharPtr());
00156     {
00157       m_fValue = ret;
00158 
00159       if (m_fValue < m_DoubleValidator.GetMinimum())
00160       {
00161         m_fValue = m_DoubleValidator.GetMinimum();
00162         m_EditLine->SetText(NString::Printf("%f", m_fValue));
00163       }
00164 
00165       if (m_fValue > m_DoubleValidator.GetMaximum())
00166       {
00167         m_fValue = m_DoubleValidator.GetMaximum();
00168         m_EditLine->SetText(NString::Printf("%f", m_fValue));
00169       }
00170     }
00171 //     else
00172 //     {
00173 //         m_EditLine->SetText(NString::Printf("%f", m_fValue));
00174 //     }
00175   }
00176 
00177 }