From 3eaef97a7d9f83ccdad6b4025b8bebefab8b3d75 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Tue, 13 May 2014 11:22:51 +0200 Subject: [PATCH] Added WX_UNIT_TEXT - wxWidget control for inputing sizes using different units (mm, inch, internal units). --- common/CMakeLists.txt | 1 + common/wxunittext.cpp | 146 ++++++++++++++++++++++++++++++++++++++++++ include/wxunittext.h | 139 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 286 insertions(+) create mode 100644 common/wxunittext.cpp create mode 100644 include/wxunittext.h diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 07da514a8e..b30ac34312 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -202,6 +202,7 @@ set( COMMON_SRCS wildcards_and_files_ext.cpp worksheet.cpp wxwineda.cpp + wxunittext.cpp xnode.cpp zoom.cpp ) diff --git a/common/wxunittext.cpp b/common/wxunittext.cpp new file mode 100644 index 0000000000..5af3c721ea --- /dev/null +++ b/common/wxunittext.cpp @@ -0,0 +1,146 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2014 CERN + * Author: Maciej Suminski + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include "wxunittext.h" +#include +#include +#include +#include +#include + +WX_UNIT_TEXT::WX_UNIT_TEXT( wxWindow* aParent, const wxString& aLabel, double aValue, double aStep ) : + wxPanel( aParent, wxID_ANY ), + m_step( aStep ) +{ + // Use the currently selected units + m_units = g_UserUnit; + + wxBoxSizer* sizer; + sizer = new wxBoxSizer( wxHORIZONTAL ); + + // Helper label + m_inputLabel = new wxStaticText( this, wxID_ANY, aLabel, + wxDefaultPosition, wxDefaultSize, 0 ); + wxSize size = m_inputLabel->GetMinSize(); + size.SetWidth( 150 ); + m_inputLabel->SetMinSize( size ); + sizer->Add( m_inputLabel, 1, wxALIGN_CENTER_VERTICAL | wxALL | wxEXPAND, 5 ); + + wxFloatingPointValidator validator( 4, NULL, wxNUM_VAL_NO_TRAILING_ZEROES ); + validator.SetRange( 0.0, std::numeric_limits::max() ); + + // Main input control + m_inputValue = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, + wxTE_PROCESS_ENTER ); + m_inputValue->SetValidator( validator ); + SetValue( aValue ); + sizer->Add( m_inputValue, 0, wxALIGN_CENTER_VERTICAL | wxALL ); + + // Spin buttons for modifying values using the mouse + m_spinButton = new wxSpinButton( this, wxID_ANY ); + m_spinButton->SetRange( std::numeric_limits::min(), std::numeric_limits::max() ); + m_spinButton->SetCanFocus( false ); + sizer->Add( m_spinButton, 0, wxALIGN_CENTER_VERTICAL | wxALL ); + + sizer->AddSpacer( 5 ); + + // Create units label + m_unitLabel = new wxStaticText( this, wxID_ANY, GetUnitsLabel( g_UserUnit ), + wxDefaultPosition, wxDefaultSize, 0 ); + sizer->Add( m_unitLabel, 0, wxALIGN_CENTER_VERTICAL | wxALL ); + + SetSizer( sizer ); + Layout(); + + Connect( wxEVT_SPIN_UP, wxSpinEventHandler( WX_UNIT_TEXT::onSpinUpEvent ), NULL, this ); + Connect( wxEVT_SPIN_DOWN, wxSpinEventHandler( WX_UNIT_TEXT::onSpinDownEvent ), NULL, this ); + Connect( wxEVT_TEXT_ENTER, wxCommandEventHandler( WX_UNIT_TEXT::onEnter ), NULL, this ); +} + + +WX_UNIT_TEXT::~WX_UNIT_TEXT() +{ +} + + +void WX_UNIT_TEXT::SetUnits( EDA_UNITS_T aUnits, bool aConvert ) +{ + assert( !aConvert ); // TODO conversion does not work yet + + m_unitLabel->SetLabel( GetUnitsLabel( g_UserUnit ) ); +} + + +void WX_UNIT_TEXT::SetValue( double aValue ) +{ + assert( aValue >= 0.0 ); + + if( aValue >= 0.0 ) + { + m_inputValue->SetValue( Double2Str( aValue ) ); + m_inputValue->MarkDirty(); + } +} + + +double WX_UNIT_TEXT::GetValue( EDA_UNITS_T aUnit ) const +{ + assert( false ); // TODO + + return 0.0; +} + + +double WX_UNIT_TEXT::GetValue() const +{ + wxString text = m_inputValue->GetValue(); + double value; + + if( !text.ToDouble( &value ) ) + value = 0.0; + + return value; +} + + +void WX_UNIT_TEXT::onSpinUpEvent( wxSpinEvent& aEvent ) +{ + SetValue( GetValue() + m_step ); +} + + +void WX_UNIT_TEXT::onSpinDownEvent( wxSpinEvent& aEvent ) +{ + double newValue = GetValue() - m_step; + + if( newValue >= 0.0 ) + SetValue( newValue ); +} + + +void WX_UNIT_TEXT::onEnter( wxCommandEvent& aEvent ) +{ + // Move focus to the next widget + Navigate(); +} diff --git a/include/wxunittext.h b/include/wxunittext.h new file mode 100644 index 0000000000..977002e59a --- /dev/null +++ b/include/wxunittext.h @@ -0,0 +1,139 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2014 CERN + * Author: Maciej Suminski + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef WXUNITTEXT_H_ +#define WXUNITTEXT_H_ + +#include +#include + +class wxTextCtrl; +class wxSpinButton; +class wxStaticText; + +class WX_UNIT_TEXT : public wxPanel +{ +public: + /** + * Constructor. + * @param aParent is the parent window. + * @param aLabel is the label displayed next to the text input control. + * @param aValue is the initial value for the control. + * @param aStep is the step size when using spin buttons. + */ + WX_UNIT_TEXT( wxWindow* aParent, const wxString& aLabel = wxString( "Size:" ), + double aValue = 0.0, double aStep = 0.1 ); + + virtual ~WX_UNIT_TEXT(); + + /** + * Function SetUnits + * Changes the units used by the control. + * @param aUnits is the new unit to be used. + * @param aConvert decides if the current value should be converted to the value in new units + * or should it stay the same. + */ + void SetUnits( EDA_UNITS_T aUnits, bool aConvert = false ); + + /** + * Function SetValue + * Sets new value for the control. + * @param aValue is the new value. + */ + virtual void SetValue( double aValue ); + + /** + * Function GetValue + * Returns the current value using specified units (if currently used units are different, then + * they are converted first). + * @param aUnits is the wanted unit. + */ + virtual double GetValue( EDA_UNITS_T aUnits ) const; + + /** + * Function GetValue + * Returns the current value in currently used units. + */ + virtual double GetValue() const; + + /** + * Function GetUnits + * Returns currently used units. + */ + EDA_UNITS_T GetUnits() const + { + return m_units; + } + + /** + * Function SetStep + * Sets the difference introduced by a single spin button click. + * @param aStep is new step size. + */ + void SetStep( double aStep ) + { + assert( aStep > 0.0 ); + + m_step = aStep; + } + + /** + * Function GetStep + * Returns the difference introduced by a single spin button click. + */ + double GetStep() const + { + return m_step; + } + +protected: + ///> Spin up button click event handler. + void onSpinUpEvent( wxSpinEvent& aEvent ); + + ///> Spin down button click event handler. + void onSpinDownEvent( wxSpinEvent& aEvent ); + + ///> On Enter press event handler. + void onEnter( wxCommandEvent& aEvent ); + + ///> Label for the input (e.g. "Size:") + wxStaticText* m_inputLabel; + + ///> Text input control. + wxTextCtrl* m_inputValue; + + ///> Spin buttons for changing the value using mouse. + wxSpinButton* m_spinButton; + + ///> Label showing currently used units. + wxStaticText* m_unitLabel; + + ///> Currently used units. + EDA_UNITS_T m_units; + + ///> Step size (added/subtracted difference if spin buttons are used). + double m_step; +}; + +#endif /* WXUNITTEXT_H_ */