Back to index

nux  3.0.0
SpinBoxDouble.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 
00025 #include "TimerProc.h"
00026 #include "HLayout.h"
00027 #include "VLayout.h"
00028 #include "DoubleValidator.h"
00029 #include "SpinBox_Logic.h"
00030 
00031 #include "SpinBoxDouble.h"
00032 namespace nux
00033 {
00034   const Color SPINBOX_DOUBLE_BUTTON_COLOR = Color(0xFF4D4D4D);
00035   const Color SPINBOX_DOUBLE_BUTTON_MOUSEOVER_COLOR = Color(0xFF222222);
00036 
00037   SpinBoxDouble::SpinBoxDouble(double Value, double Step, double MinValue, double MaxValue, NUX_FILE_LINE_DECL)
00038     : SpinBox_Logic(NUX_FILE_LINE_PARAM)
00039     , m_DoubleValidator(MinValue, MaxValue)
00040     , m_Step(Step)
00041   {
00042     InitializeLayout();
00043     InitializeWidgets();
00044     SetValue(Value);
00045   }
00046 
00047   SpinBoxDouble::~SpinBoxDouble()
00048   {
00049   }
00050 
00051   void SpinBoxDouble::InitializeWidgets()
00052   {
00053     m_EditLine->SetValidator(&m_DoubleValidator);
00054     m_EditLine->SetSuffix("");
00055     m_EditLine->SetPrefix("");
00056     m_EditLine->SetText(NString::Printf("%.3f", m_DoubleValidator.GetMinimum()));
00057 
00058     m_EditLine->SetMinimumSize(1.5 * DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT);
00059     m_EditLine->SetGeometry(Geometry(0, 0, DEFAULT_WIDGET_WIDTH, DEFAULT_WIDGET_HEIGHT));
00060 
00061     m_SpinnerUpBtn->SetMinimumSize(15, 10);
00062     m_SpinnerUpBtn->SetGeometry(Geometry(0, 0, 15, 10));
00063     m_SpinnerDownBtn->SetMinimumSize(15, 10);
00064     m_SpinnerDownBtn->SetGeometry(Geometry(0, 0, 15, 10));
00065 
00066 
00067     // Set the minimum size of this widget.
00068     // This is use by TextLineEditPropertyItem::GetItemBestHeight
00069     SetMinimumSize(DEFAULT_WIDGET_WIDTH, PRACTICAL_WIDGET_HEIGHT);
00070 
00071     m_hlayout->AddView(m_EditLine, 1);
00072 
00073     m_vlayout->AddView(m_SpinnerUpBtn, 1);
00074     m_vlayout->AddView(m_SpinnerDownBtn, 1);
00075     m_hlayout->AddLayout(m_vlayout, 0);
00076 
00077     SetCompositionLayout(m_hlayout);
00078   }
00079 
00080   void SpinBoxDouble::InitializeLayout()
00081   {
00082     m_hlayout = new HLayout(NUX_TRACKER_LOCATION);
00083     m_vlayout = new VLayout(NUX_TRACKER_LOCATION);
00084   }
00085 
00086   void SpinBoxDouble::Draw(GraphicsEngine &graphics_engine, bool force_draw)
00087   {
00088     Geometry base = GetGeometry();
00089     GetPainter().PaintBackground(graphics_engine, base);
00090 
00091     if (m_EditLine->IsMouseInside() || m_SpinnerUpBtn->IsMouseInside() || m_SpinnerDownBtn->IsMouseInside())
00092     {
00093 
00094       GetPainter().PaintShapeCorner(graphics_engine, m_SpinnerUpBtn->GetGeometry(), SPINBOX_DOUBLE_BUTTON_MOUSEOVER_COLOR, eSHAPE_CORNER_ROUND4,
00095                                  eCornerTopRight, false);
00096       GetPainter().PaintShapeCorner(graphics_engine, m_SpinnerDownBtn->GetGeometry(), SPINBOX_DOUBLE_BUTTON_MOUSEOVER_COLOR, eSHAPE_CORNER_ROUND4,
00097                                  eCornerBottomRight, false);
00098     }
00099     else
00100     {
00101       GetPainter().PaintShapeCorner(graphics_engine, m_SpinnerUpBtn->GetGeometry(), SPINBOX_DOUBLE_BUTTON_COLOR, eSHAPE_CORNER_ROUND4,
00102                                  eCornerTopRight, false);
00103       GetPainter().PaintShapeCorner(graphics_engine, m_SpinnerDownBtn->GetGeometry(), SPINBOX_DOUBLE_BUTTON_COLOR, eSHAPE_CORNER_ROUND4,
00104                                  eCornerBottomRight, false);
00105     }
00106 
00107     GeometryPositioning gp(eHACenter, eVACenter);
00108     Geometry GeoPo = ComputeGeometryPositioning(m_SpinnerUpBtn->GetGeometry(), GetTheme().GetImageGeometry(eSPINER_UP), gp);
00109 
00110     if (m_SpinnerUpBtn->IsMouseInside())
00111       GetPainter().PaintShape(graphics_engine, GeoPo, Color(0xFFFFFFFF), eSPINER_UP);
00112     else
00113       GetPainter().PaintShape(graphics_engine, GeoPo, Color(0xFFFFFFFF), eSPINER_UP);
00114 
00115 
00116     gp.SetAlignment(eHACenter, eVACenter);
00117     GeoPo = ComputeGeometryPositioning(m_SpinnerDownBtn->GetGeometry(), GetTheme().GetImageGeometry(eSPINER_DOWN), gp);
00118 
00119     if (m_SpinnerDownBtn->IsMouseInside())
00120       GetPainter().PaintShape(graphics_engine, GeoPo, Color(0xFFFFFFFF), eSPINER_DOWN);
00121     else
00122       GetPainter().PaintShape(graphics_engine, GeoPo, Color(0xFFFFFFFF), eSPINER_DOWN);
00123 
00124     m_EditLine->QueueDraw();
00125   }
00126 
00127   void SpinBoxDouble::DrawContent(GraphicsEngine &graphics_engine, bool force_draw)
00128   {
00129     m_EditLine->ProcessDraw(graphics_engine, force_draw);
00130   }
00131 
00132   void SpinBoxDouble::PostDraw(GraphicsEngine &graphics_engine, bool force_draw)
00133   {
00134 
00135   }
00136 
00137   void SpinBoxDouble::SetValue(double value)
00138   {
00139     m_Value = m_DoubleValidator.GetClampedValue(value);
00140 //    if (m_Value < m_DoubleValidator.GetMinimum())
00141 //        m_Value = m_DoubleValidator.GetMinimum();
00142 //    if (m_Value > m_DoubleValidator.GetMaximum())
00143 //        m_Value = m_DoubleValidator.GetMaximum();
00144     m_EditLine->SetText(NString::Printf("%.3f", m_Value));
00145     sigValueChanged.emit(this);
00146     sigValue.emit(m_Value);
00147     QueueDraw();
00148   }
00149 
00150   double SpinBoxDouble::GetValue() const
00151   {
00152     return m_Value;
00153   }
00154 
00155   void SpinBoxDouble::SetStep(double d)
00156   {
00157     m_Step = d;
00158 
00159     if (m_Step <= 0)
00160       m_Step = 1.0;
00161 
00162     QueueDraw();
00163   }
00164 
00165   double SpinBoxDouble::GetStep() const
00166   {
00167     return m_Step;
00168   }
00169 
00170   double SpinBoxDouble::GetMinValue() const
00171   {
00172     return m_DoubleValidator.GetMinimum();
00173   }
00174 
00175   double SpinBoxDouble::GetMaxValue() const
00176   {
00177     return m_DoubleValidator.GetMaximum();
00178   }
00179 
00180   void SpinBoxDouble::SetRange(double MinValue, double Maxvalue)
00181   {
00182     m_DoubleValidator.SetMinimum(MinValue);
00183     m_DoubleValidator.SetMaximum(Maxvalue);
00184     m_Value = m_DoubleValidator.GetClampedValue(m_Value);
00185     sigValueChanged.emit(this);
00186     sigValue.emit(m_Value);
00187     QueueDraw();
00188   }
00189 
00190   void SpinBoxDouble::ImplementIncrementBtn()
00191   {
00192     SetValue(m_Value + m_Step);
00193 
00194     if (m_Value < m_DoubleValidator.GetMaximum())
00195     {
00196       if (m_UpTimerHandler.IsValid())
00197         m_UpTimerHandler = GetTimer().AddTimerHandler(100, m_UpTimerCallback, 0);
00198       else
00199         m_UpTimerHandler = GetTimer().AddTimerHandler(800, m_UpTimerCallback, 0);
00200 
00201       QueueDraw();
00202     }
00203 
00204     sigValueChanged.emit(this);
00205     sigIncrement.emit(this);
00206     sigValue.emit(m_Value);
00207   }
00208 
00209   void SpinBoxDouble::ImplementDecrementBtn()
00210   {
00211     SetValue(m_Value - m_Step);
00212 
00213     if (m_Value > m_DoubleValidator.GetMinimum())
00214     {
00215       if (m_DownTimerHandler.IsValid())
00216         m_DownTimerHandler = GetTimer().AddTimerHandler(100, m_DownTimerCallback, 0);
00217       else
00218         m_DownTimerHandler = GetTimer().AddTimerHandler(800, m_DownTimerCallback, 0);
00219 
00220       QueueDraw();
00221     }
00222 
00223     sigValueChanged.emit(this);
00224     sigDecrement.emit(this);
00225     sigValue.emit(m_Value);
00226   }
00227 
00228   void SpinBoxDouble::ImplementValidateEntry()
00229   {
00230     double ret = 0;
00231     ret = CharToDouble(m_EditLine->GetCleanText().GetTCharPtr());
00232     {
00233       m_Value = m_DoubleValidator.GetClampedValue(ret);
00234       m_EditLine->SetText(NString::Printf("%.3f", m_Value));
00235       sigValueChanged.emit(this);
00236       sigValue.emit(m_Value);
00237 //
00238 //        if (m_Value < m_DoubleValidator.GetMinimum())
00239 //        {
00240 //            m_Value = m_DoubleValidator.GetMinimum();
00241 //            m_EditLine->SetText(NString::Printf("%.3f", m_Value));
00242 //        }
00243 //        if (m_Value > m_DoubleValidator.GetMaximum())
00244 //        {
00245 //            m_Value = m_DoubleValidator.GetMaximum();
00246 //            m_EditLine->SetText(NString::Printf("%.3f", m_Value));
00247 //        }
00248     }
00249 //     else
00250 //     {
00251 //         m_EditLine->SetText(NString::Printf("%.3f", m_Value));
00252 //         sigValueChanged.emit(this);
00253 //         sigValue.emit(m_Value);
00254 //     }
00255   }
00256 
00257 }