KiCad PCB EDA Suite
wxunittext.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2014 CERN
5  * Author: Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include "wxunittext.h"
26 #include <wx/stattext.h>
27 #include <wx/sizer.h>
28 #include <wx/textctrl.h>
29 #include <limits>
30 #include <base_units.h>
31 #include <wx/valnum.h>
32 #include <boost/optional.hpp>
33 
34 WX_UNIT_TEXT::WX_UNIT_TEXT( wxWindow* aParent, const wxString& aLabel, double aValue, double aStep ) :
35  wxPanel( aParent, wxID_ANY ),
36  m_step( aStep )
37 {
38  // Use the currently selected units
40 
41  wxBoxSizer* sizer;
42  sizer = new wxBoxSizer( wxHORIZONTAL );
43 
44  // Helper label
45  m_inputLabel = new wxStaticText( this, wxID_ANY, aLabel,
46  wxDefaultPosition, wxDefaultSize, 0 );
47  wxSize size = m_inputLabel->GetMinSize();
48  size.SetWidth( 150 );
49  m_inputLabel->SetMinSize( size );
50  sizer->Add( m_inputLabel, 1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 );
51 
52  // Main input control
53  m_inputValue = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
54  wxTE_PROCESS_ENTER );
55 
56  SetValue( aValue );
57  sizer->Add( m_inputValue, 0, wxALIGN_CENTER_VERTICAL | wxALL );
58 
59  wxFloatingPointValidator<double> validator( 4, NULL, wxNUM_VAL_NO_TRAILING_ZEROES );
60  validator.SetRange( 0.0, std::numeric_limits<double>::max() );
61  m_inputValue->SetValidator( validator );
62 
63  // Spin buttons for modifying values using the mouse
64  m_spinButton = new wxSpinButton( this, wxID_ANY );
66 
67  m_spinButton->SetCanFocus( false );
68  sizer->Add( m_spinButton, 0, wxALIGN_CENTER_VERTICAL | wxALL );
69 
70  Connect( wxEVT_SPIN_UP, wxSpinEventHandler( WX_UNIT_TEXT::onSpinUpEvent ), NULL, this );
71  Connect( wxEVT_SPIN_DOWN, wxSpinEventHandler( WX_UNIT_TEXT::onSpinDownEvent ), NULL, this );
72 
73  sizer->AddSpacer( 5 );
74 
75  // Create units label
76  m_unitLabel = new wxStaticText( this, wxID_ANY, GetUnitsLabel( g_UserUnit ),
77  wxDefaultPosition, wxDefaultSize, 0 );
78  sizer->Add( m_unitLabel, 0, wxALIGN_CENTER_VERTICAL | wxALL );
79 
80  SetSizer( sizer );
81  Layout();
82 }
83 
84 
86 {
87 }
88 
89 
90 void WX_UNIT_TEXT::SetUnits( EDA_UNITS_T aUnits, bool aConvert )
91 {
92  assert( !aConvert ); // TODO conversion does not work yet
93 
94  m_unitLabel->SetLabel( GetUnitsLabel( g_UserUnit ) );
95 }
96 
97 
98 void WX_UNIT_TEXT::SetValue( double aValue )
99 {
100  if( aValue >= 0.0 )
101  {
102  m_inputValue->SetValue( wxString( Double2Str( aValue ).c_str(), wxConvUTF8 ) );
103  m_inputValue->MarkDirty();
104  }
105  else
106  {
107  m_inputValue->SetValue( DEFAULT_VALUE );
108  }
109 }
110 
111 
113 {
114  wxString text = m_inputValue->GetValue();
115  double value;
116 
117  if( text == DEFAULT_VALUE )
118  return boost::optional<double>( -1.0 );
119 
120  if( !text.ToDouble( &value ) )
121  return boost::optional<double>();
122 
123  return boost::optional<double>( value );
124 }
125 
126 
127 void WX_UNIT_TEXT::onSpinUpEvent( wxSpinEvent& aEvent )
128 {
129  SetValue( *GetValue() + m_step );
130 }
131 
132 
133 void WX_UNIT_TEXT::onSpinDownEvent( wxSpinEvent& aEvent )
134 {
135  double newValue = *GetValue() - m_step;
136 
137  if( newValue >= 0.0 )
138  SetValue( newValue );
139 }
140 
141 
142 const wxString WX_UNIT_TEXT::DEFAULT_VALUE = _( "default ");
Implementation of conversion functions that require both schematic and board internal units...
static const wxString DEFAULT_VALUE
Default value (or non-specified)
Definition: wxunittext.h:141
wxString GetUnitsLabel(EDA_UNITS_T aUnit)
Get a human readable units string.
Definition: base_units.cpp:452
virtual void SetValue(double aValue)
Function SetValue Sets new value for the control.
Definition: wxunittext.cpp:98
std::string Double2Str(double aValue)
Helper function Double2Str to print a float number without using scientific notation and no trailing ...
Definition: base_units.cpp:60
void onSpinUpEvent(wxSpinEvent &aEvent)
Spin up button click event handler.
Definition: wxunittext.cpp:127
wxTextCtrl * m_inputValue
Text input control.
Definition: wxunittext.h:126
wxStaticText * m_unitLabel
Label showing currently used units.
Definition: wxunittext.h:132
void onSpinDownEvent(wxSpinEvent &aEvent)
Spin down button click event handler.
Definition: wxunittext.cpp:133
WX_UNIT_TEXT(wxWindow *aParent, const wxString &aLabel=_("Size:"), double aValue=0.0, double aStep=0.1)
Constructor.
Definition: wxunittext.cpp:34
EDA_UNITS_T g_UserUnit
Global variables definitions.
Definition: common.cpp:56
wxSpinButton * m_spinButton
Spin buttons for changing the value using mouse.
Definition: wxunittext.h:129
void SetUnits(EDA_UNITS_T aUnits, bool aConvert=false)
Function SetUnits Changes the units used by the control.
Definition: wxunittext.cpp:90
#define max(a, b)
Definition: auxiliary.h:86
virtual ~WX_UNIT_TEXT()
Definition: wxunittext.cpp:85
double m_step
Step size (added/subtracted difference if spin buttons are used).
Definition: wxunittext.h:138
wxStaticText * m_inputLabel
Label for the input (e.g. "Size:")
Definition: wxunittext.h:123
virtual boost::optional< double > GetValue() const
Function GetValue Returns the current value in currently used units.
Definition: wxunittext.cpp:112
EDA_UNITS_T m_units
Currently used units.
Definition: wxunittext.h:135
EDA_UNITS_T
Definition: common.h:144
#define min(a, b)
Definition: auxiliary.h:85