A better fix for commit d34433a1.

Replace INCREMENTAL_TEXT_CTRL with wxSpinCtrlDouble because it looks
and works better.

Remove INCREMENTAL_TEXT_CTRL header and source files because the only
place they were used was in GAL_OPTIONS_PANEL.

(cherry picked from commit f08d040c02)
This commit is contained in:
Wayne Stambaugh 2019-05-15 18:17:37 -04:00
parent 5d5422b4c5
commit deb387f54c
5 changed files with 27 additions and 421 deletions

View File

@ -323,7 +323,6 @@ set( COMMON_SRCS
hotkey_store.cpp
hotkeys_basic.cpp
html_messagebox.cpp
incremental_text_ctrl.cpp
kiface_i.cpp
kiway.cpp
kiway_express.cpp

View File

@ -1,188 +0,0 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015 Jean-Pierre Charras, jean-pierre.charras at wanadoo.fr
* Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
*
* 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 <incremental_text_ctrl.h>
#include <utility>
/**
* Check that a string looks like a floating point number that can
* be dealt with.
*/
static bool validateFloatField( const wxString& aStr )
{
// Skip empty fields
if( aStr.size() == 0 )
return false;
// a single . or , doesn't count as number, although valid in a float
if( aStr.size() == 1 )
{
if( (aStr.compare( "." ) == 0) ||
(aStr.compare( "," ) == 0) )
return false;
}
return true;
}
INCREMENTAL_TEXT_CTRL::INCREMENTAL_TEXT_CTRL() :
m_minVal( 0.0 ),
m_maxVal( 1.0 ),
m_currentValue( 0.0 ),
m_precision( 4 )
{}
void INCREMENTAL_TEXT_CTRL::SetStep( double aMin, double aMax,
STEP_FUNCTION aStepFunc )
{
wxASSERT( aMin <= aMax );
m_minVal = std::min( aMin, aMax );
m_maxVal = std::max( aMin, aMax );
m_stepFunc = std::move( aStepFunc );
// finally, clamp the current value and re-display
updateTextValue();
}
void INCREMENTAL_TEXT_CTRL::updateTextValue()
{
if( m_currentValue > m_maxVal )
m_currentValue = m_maxVal;
if( m_currentValue < m_minVal )
m_currentValue = m_minVal;
wxString fmt = wxString::Format( "%%.%df", m_precision );
setTextCtrl( wxString::Format( fmt, m_currentValue ) );
}
void INCREMENTAL_TEXT_CTRL::incrementCtrlBy( double aInc )
{
const wxString txt = getCtrlText();
if( !validateFloatField( txt ) )
return;
txt.ToDouble( &m_currentValue );
m_currentValue += aInc;
updateTextValue();
}
void INCREMENTAL_TEXT_CTRL::incrementCtrl( bool aUp )
{
incrementCtrlBy( m_stepFunc( aUp, m_currentValue ) );
}
void INCREMENTAL_TEXT_CTRL::SetPrecision( int aPrecision )
{
m_precision = aPrecision;
}
void INCREMENTAL_TEXT_CTRL::SetValue( double aValue )
{
m_currentValue = aValue;
updateTextValue();
}
double INCREMENTAL_TEXT_CTRL::GetValue()
{
// sanitise before handing the value - if the user did something
// like close a window with outstanding text changes, we need
// to clamp the value and re-interpret the text
incrementCtrlBy( 0.0 );
return m_currentValue;
}
SPIN_INCREMENTAL_TEXT_CTRL::SPIN_INCREMENTAL_TEXT_CTRL( wxSpinButton& aSpinBtn,
wxTextCtrl& aTextCtrl ):
m_spinBtn( aSpinBtn ),
m_textCtrl( aTextCtrl )
{
(void) m_spinBtn;
// set always enabled, otherwise it's very hard to keep in sync
aSpinBtn.SetRange( -INT_MAX, INT_MAX );
auto spinUpHandler = [this] ( wxSpinEvent& event )
{
incrementCtrl( true );
};
// spin up/down if a single step of the field
auto spinDownHandler = [this] ( wxSpinEvent& event )
{
incrementCtrl( false );
};
auto mouseWheelHandler = [this] ( wxMouseEvent& aEvent )
{
incrementCtrl( aEvent.GetWheelRotation() >= 0 );
};
aSpinBtn.Bind( wxEVT_SPIN_UP, spinUpHandler );
aSpinBtn.Bind( wxEVT_SPIN_DOWN, spinDownHandler );
m_textCtrl.Bind( wxEVT_MOUSEWHEEL, mouseWheelHandler );
m_textCtrl.Bind( wxEVT_KILL_FOCUS, &SPIN_INCREMENTAL_TEXT_CTRL::onFocusLoss, this );
}
SPIN_INCREMENTAL_TEXT_CTRL::~SPIN_INCREMENTAL_TEXT_CTRL()
{
// this must be unbound, as kill focus can arrive after the
// text control is gone
m_textCtrl.Unbind( wxEVT_KILL_FOCUS, &SPIN_INCREMENTAL_TEXT_CTRL::onFocusLoss, this );
}
void SPIN_INCREMENTAL_TEXT_CTRL::onFocusLoss( wxFocusEvent& aEvent )
{
// re-read the input and sanitize any user changes
incrementCtrlBy( 0.0 );
}
void SPIN_INCREMENTAL_TEXT_CTRL::setTextCtrl( const wxString& val )
{
m_textCtrl.SetValue( val );
}
wxString SPIN_INCREMENTAL_TEXT_CTRL::getCtrlText() const
{
return m_textCtrl.GetValue();
}

View File

@ -22,11 +22,16 @@
*/
#include <wx/sizer.h>
#include <wx/checkbox.h>
#include <wx/radiobox.h>
#include <wx/spinctrl.h>
#include <wx/stattext.h>
#include <widgets/gal_options_panel.h>
#include <common.h>
#include <incremental_text_ctrl.h>
#include <config_map.h>
/*
@ -34,7 +39,7 @@
*/
static const double gridThicknessMin = 1.0;
static const double gridThicknessMax = 10.0;
static const double gridThicknessStep = 1.0;
static const double gridThicknessStep = 0.5;
static const double gridMinSpacingMin = 5;
static const double gridMinSpacingMax = 200;
@ -85,7 +90,7 @@ GAL_OPTIONS_PANEL::GAL_OPTIONS_PANEL( wxWindow* aParent, KIGFX::GAL_DISPLAY_OPTI
sGridSettings->Add( m_gridStyle, 0, wxALL|wxEXPAND, 5 );
wxFlexGridSizer* sGridSettingsGrid;
sGridSettingsGrid = new wxFlexGridSizer( 0, 4, 0, 0 );
sGridSettingsGrid = new wxFlexGridSizer( 0, 3, 0, 0 );
sGridSettingsGrid->AddGrowableCol( 1 );
sGridSettingsGrid->SetFlexibleDirection( wxBOTH );
sGridSettingsGrid->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
@ -95,14 +100,12 @@ GAL_OPTIONS_PANEL::GAL_OPTIONS_PANEL( wxWindow* aParent, KIGFX::GAL_DISPLAY_OPTI
l_gridLineWidth->Wrap( -1 );
sGridSettingsGrid->Add( l_gridLineWidth, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
m_gridLineWidth = new wxTextCtrl( sGridSettings->GetStaticBox(), wxID_ANY );
m_gridLineWidth = new wxSpinCtrlDouble( sGridSettings->GetStaticBox(), wxID_ANY );
m_gridLineWidth->SetRange( gridThicknessMin, gridThicknessMax );
m_gridLineWidth->SetIncrement( gridThicknessStep );
m_gridLineWidth->SetDigits( 1 );
sGridSettingsGrid->Add( m_gridLineWidth, 0, wxEXPAND | wxTOP | wxBOTTOM, 5 );
m_gridLineWidthSpinBtn = new wxSpinButton( sGridSettings->GetStaticBox(),
wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS );
sGridSettingsGrid->Add( m_gridLineWidthSpinBtn, 0,
wxEXPAND | wxTOP | wxBOTTOM | wxALIGN_CENTER_VERTICAL, 3 );
l_gridLineWidthUnits = new wxStaticText( sGridSettings->GetStaticBox(),
wxID_ANY, _( "px" ) );
l_gridLineWidthUnits->Wrap( -1 );
@ -113,14 +116,12 @@ GAL_OPTIONS_PANEL::GAL_OPTIONS_PANEL( wxWindow* aParent, KIGFX::GAL_DISPLAY_OPTI
l_gridMinSpacing->Wrap( -1 );
sGridSettingsGrid->Add( l_gridMinSpacing, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
m_gridMinSpacing = new wxTextCtrl( sGridSettings->GetStaticBox(), wxID_ANY);
m_gridMinSpacing = new wxSpinCtrlDouble( sGridSettings->GetStaticBox(), wxID_ANY);
m_gridMinSpacing->SetRange( gridMinSpacingMin, gridMinSpacingMax );
m_gridMinSpacing->SetIncrement( gridMinSpacingStep );
m_gridMinSpacing->SetDigits( 0 );
sGridSettingsGrid->Add( m_gridMinSpacing, 0, wxEXPAND | wxTOP | wxBOTTOM, 5 );
m_gridMinSpacingSpinBtn = new wxSpinButton( sGridSettings->GetStaticBox(),
wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS );
sGridSettingsGrid->Add( m_gridMinSpacingSpinBtn, 0,
wxEXPAND | wxTOP | wxBOTTOM | wxALIGN_CENTER_VERTICAL, 3 );
l_gridMinSpacingUnits = new wxStaticText( sGridSettings->GetStaticBox(),
wxID_ANY, _( "px" ) );
l_gridMinSpacingUnits->Wrap( -1 );
@ -129,21 +130,6 @@ GAL_OPTIONS_PANEL::GAL_OPTIONS_PANEL( wxWindow* aParent, KIGFX::GAL_DISPLAY_OPTI
sGridSettings->Add( sGridSettingsGrid, 1, wxALL | wxEXPAND, 5 );
sLeftSizer->Add( sGridSettings, 0, wxTOP | wxBOTTOM | wxRIGHT | wxEXPAND, 5 );
// bind the spin buttons and text boxes
m_gridSizeIncrementer = std::make_unique<SPIN_INCREMENTAL_TEXT_CTRL>(
*m_gridLineWidthSpinBtn, *m_gridLineWidth );
m_gridSizeIncrementer->SetStep( gridThicknessMin, gridThicknessMax,
gridThicknessStep );
m_gridSizeIncrementer->SetPrecision( 0 );
m_gridMinSpacingIncrementer = std::make_unique<SPIN_INCREMENTAL_TEXT_CTRL>(
*m_gridMinSpacingSpinBtn, *m_gridMinSpacing );
m_gridMinSpacingIncrementer->SetStep( gridMinSpacingMin, gridMinSpacingMax,
gridMinSpacingStep );
m_gridMinSpacingIncrementer->SetPrecision( 0 ); // restrict to ints
}
/*
@ -199,9 +185,9 @@ bool GAL_OPTIONS_PANEL::TransferDataToWindow()
m_gridStyle->SetSelection( UTIL::GetConfigForVal(
gridStyleSelectMap, m_galOptions.m_gridStyle ) );
m_gridSizeIncrementer->SetValue( m_galOptions.m_gridLineWidth );
m_gridLineWidth->SetValue( m_galOptions.m_gridLineWidth );
m_gridMinSpacingIncrementer->SetValue( m_galOptions.m_gridMinSpacing );
m_gridMinSpacing->SetValue( m_galOptions.m_gridMinSpacing );
m_cursorShape->SetSelection( m_galOptions.m_fullscreenCursor );
@ -216,9 +202,9 @@ bool GAL_OPTIONS_PANEL::TransferDataFromWindow()
m_galOptions.m_gridStyle = UTIL::GetValFromConfig(
gridStyleSelectMap, m_gridStyle->GetSelection() );
m_galOptions.m_gridLineWidth = std::floor( m_gridSizeIncrementer->GetValue() + 0.5 );
m_galOptions.m_gridLineWidth = m_gridLineWidth->GetValue();
m_galOptions.m_gridMinSpacing = m_gridMinSpacingIncrementer->GetValue();
m_galOptions.m_gridMinSpacing = m_gridMinSpacing->GetValue();
m_galOptions.m_fullscreenCursor = m_cursorShape->GetSelection();

View File

@ -1,188 +0,0 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2013 CERN
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* 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 INCREMENTAL_TEXT_CTRL__H_
#define INCREMENTAL_TEXT_CTRL__H_
#include <wx/wx.h>
#include <wx/spinbutt.h>
#include <functional>
/**
* Class that governs a textual control holding a number that can
* be incremented/decremented according to some scheme (often just
* a constant step).
*/
class INCREMENTAL_TEXT_CTRL
{
public:
/**
* A callable object type that can be used to provide a step
* value. Client can provide one of these to use for implementing
* non-linear stepping, or stepping based on external parameters,
* such as unit selection.
*
* @param aUp true if the next step is upwards
* @param aCurrVal the current value of the control
*/
using STEP_FUNCTION = std::function<double(bool aUp, double aCurrVal)>;
INCREMENTAL_TEXT_CTRL();
virtual ~INCREMENTAL_TEXT_CTRL() {}
/**
* Set the value of the text control, but obey the limits
* currently set.
*
* @param aValue the control value to set
*/
void SetValue( double aValue );
/**
* Get the current value of the control
*/
double GetValue();
/**
* Function SetStep()
*
* Set the stepping parameters of the control. The range is
* enforced by not allowing the scroll to exceed it, and on
* loss of focus, the control is also clamped to the range.
*
* @param aMin the minium value allowed
* @param aMax the maximum value allows
* @param aNewFunc the step function used to calculate the next step
*/
void SetStep( double aMin, double aMax, STEP_FUNCTION aNewFunc );
/**
* Function SetStep()
*
* Shortcut method to set step parameters when the step is constant
*
* @param aMin the minium value allowed
* @param aMax the maximum value allows
* @param aStep the constant step size
*/
void SetStep( double aMin, double aMax, double aStep )
{
SetStep( aMin, aMax,
[aStep] ( bool aUp, double aCurrent ) { return aUp ? aStep : -aStep; } );
}
/**
* Set the number of decimal places to display
*/
void SetPrecision( int aPrecision );
protected:
/**
* Increment the control by the given amount
*/
void incrementCtrlBy( double aInc);
/**
* Single increment up or down by one step
*/
void incrementCtrl( bool aUp );
/**
* Update the text control value with the current value,
* clamping to limits as needed
*/
void updateTextValue();
/*
* Implementation-specific interfaces
*/
/**
* Set the text control string value after an increment.
*/
virtual void setTextCtrl( const wxString& aVal ) = 0;
/**
* @return the current string value of the text control
*/
virtual wxString getCtrlText() const = 0;
private:
double m_minVal;
double m_maxVal;
///< Current value of the control
double m_currentValue;
///< Precision to display
int m_precision;
///< The function used to determine the step
STEP_FUNCTION m_stepFunc;
};
/**
* Class SPIN_INCREMENTING_TEXT_CTRL
*
* An incrementable text control, with WX spin buttons for clickable
* control.
*/
class SPIN_INCREMENTAL_TEXT_CTRL: public INCREMENTAL_TEXT_CTRL
{
public:
/**
* Constructor
*
* @param aSpinBtn the spin button to control the value
* @param aTextCtrl the text control that will display the value
*/
SPIN_INCREMENTAL_TEXT_CTRL( wxSpinButton& aSpinBtn,
wxTextCtrl& aTextCtrl );
~SPIN_INCREMENTAL_TEXT_CTRL();
protected:
///> @copydoc INCREMENTAL_TEXT_CTRL::setTextCtrl()
void setTextCtrl( const wxString& val ) override;
///> @copydoc INCREMENTAL_TEXT_CTRL::getCtrlText()
wxString getCtrlText() const override;
private:
void onFocusLoss( wxFocusEvent& aEvent );
wxSpinButton& m_spinBtn;
wxTextCtrl& m_textCtrl;
};
#endif /* INCREMENTAL_TEXT_CTRL__H_ */

View File

@ -24,12 +24,14 @@
#ifndef WIDGETS_GAL_OPTIONS_PANEL__H_
#define WIDGETS_GAL_OPTIONS_PANEL__H_
#include <wx/wx.h>
#include <wx/spinctrl.h>
#include <wx/panel.h>
#include <gal/gal_display_options.h>
class INCREMENTAL_TEXT_CTRL;
class wxBoxSizer;
class wxRadioBox;
class wxSpinCtrlDouble;
class wxStaticText;
class GAL_OPTIONS_PANEL: public wxPanel
{
@ -54,13 +56,11 @@ private:
wxRadioBox* m_gridStyle;
wxStaticText* l_gridLineWidth;
wxTextCtrl* m_gridLineWidth;
wxSpinButton* m_gridLineWidthSpinBtn;
wxSpinCtrlDouble* m_gridLineWidth;
wxStaticText* l_gridLineWidthUnits;
wxStaticText* l_gridMinSpacing;
wxTextCtrl* m_gridMinSpacing;
wxSpinButton* m_gridMinSpacingSpinBtn;
wxSpinCtrlDouble* m_gridMinSpacing;
wxStaticText* l_gridMinSpacingUnits;
wxRadioBox* m_cursorShape;
@ -68,9 +68,6 @@ private:
///> The GAL options to read/write
KIGFX::GAL_DISPLAY_OPTIONS& m_galOptions;
std::unique_ptr<INCREMENTAL_TEXT_CTRL> m_gridSizeIncrementer;
std::unique_ptr<INCREMENTAL_TEXT_CTRL> m_gridMinSpacingIncrementer;
};