WIDGET_NET_SELECTOR: speedup the net list creation.

Add comments and better names for variables.
This commit is contained in:
jean-pierre charras 2018-01-19 10:02:23 +01:00
parent 1b8ab1c23d
commit 2e83103c3d
2 changed files with 41 additions and 25 deletions

View File

@ -27,9 +27,12 @@
#include <class_board.h> #include <class_board.h>
#include <class_netinfo.h> #include <class_netinfo.h>
#include <wx/arrstr.h>
WIDGET_NET_SELECTOR::WIDGET_NET_SELECTOR (wxWindow *parent, wxWindowID id, const wxString &value, const wxPoint &pos, const wxSize &size, WIDGET_NET_SELECTOR::WIDGET_NET_SELECTOR( wxWindow *parent, wxWindowID id,
int n, const wxString choices[], long style, const wxValidator &validator, const wxString &name ) : const wxString &value, const wxPoint &pos, const wxSize &size,
int n, const wxString choices[], long style,
const wxValidator &validator, const wxString &name ) :
wxComboBox( parent, id, value, pos, size, n, choices, style, validator, name ), wxComboBox( parent, id, value, pos, size, n, choices, style, validator, name ),
m_multiple( false ) m_multiple( false )
{ {
@ -55,11 +58,11 @@ void WIDGET_NET_SELECTOR::SetMultiple( bool aMultiple )
void WIDGET_NET_SELECTOR::SetSelectedNet ( int aNetcode ) void WIDGET_NET_SELECTOR::SetSelectedNet ( int aNetcode )
{ {
for( const auto& n : m_nets ) for( const auto& net : m_nets )
{ {
if( n.code == aNetcode ) if( net.m_Code == aNetcode )
{ {
SetSelection( n.pos ); SetSelection( net.m_Pos );
return; return;
} }
} }
@ -70,10 +73,10 @@ int WIDGET_NET_SELECTOR::GetSelectedNet()
{ {
int pos = GetSelection(); int pos = GetSelection();
for( const auto& n : m_nets ) for( const auto& net : m_nets )
{ {
if( n.pos == pos ) if( net.m_Pos == pos )
return n.code; return net.m_Code;
} }
return 0; return 0;
@ -93,21 +96,27 @@ void WIDGET_NET_SELECTOR::SetBoard( BOARD* aBoard )
{ {
auto& netinfo = aBoard->GetNetInfo(); auto& netinfo = aBoard->GetNetInfo();
Append( wxT( "<no net>" ) );
for( unsigned i = 1; i < netinfo.GetNetCount(); i++ ) for( unsigned i = 1; i < netinfo.GetNetCount(); i++ )
{ {
NETINFO_ITEM* ni = netinfo.GetNetItem( i ); NETINFO_ITEM* ni = netinfo.GetNetItem( i );
NET n; NET net;
n.name = ni->GetNetname(); net.m_Name = ni->GetNetname();
n.code = i; net.m_Code = i;
m_nets.push_back( n ); m_nets.push_back( net );
} }
std::sort( m_nets.begin(), m_nets.end() ); std::sort( m_nets.begin(), m_nets.end() );
for( auto& n : m_nets ) // Add the list of selectable nets to the wxComboBox.
// Using a wxArrayString is much faster than adding each name separately
wxArrayString netnames;
netnames.Add( wxT( "<no net>" ) ); // Always on top of the list
for( auto& net : m_nets )
{ {
n.pos = Append( n.name ); net.m_Pos = netnames.Add( net.m_Name );
} }
Append( netnames );
} }

View File

@ -33,10 +33,17 @@ class BOARD;
class WIDGET_NET_SELECTOR : public wxComboBox class WIDGET_NET_SELECTOR : public wxComboBox
{ {
public: public:
WIDGET_NET_SELECTOR (wxWindow *parent, wxWindowID id, const wxString &value=wxEmptyString, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, // Note: this list of arguments is here because WIDGET_NET_SELECTOR must
int n=0, const wxString choices[]=NULL, long style=0, const wxValidator &validator=wxDefaultValidator, const wxString &name=wxComboBoxNameStr); // have the same arguments as wxComboBox to be used inside wxFormaBuilder
WIDGET_NET_SELECTOR( wxWindow *parent, wxWindowID id,
const wxString &value=wxEmptyString,
const wxPoint &pos=wxDefaultPosition,
const wxSize &size=wxDefaultSize,
int n=0, const wxString choices[]=NULL,
long style=0, const wxValidator &validator=wxDefaultValidator,
const wxString &name=wxComboBoxNameStr);
~WIDGET_NET_SELECTOR(); ~WIDGET_NET_SELECTOR();
void SetMultiple( bool aMultiple = true ); void SetMultiple( bool aMultiple = true );
void SetSelectedNet ( int aNetcode ); void SetSelectedNet ( int aNetcode );
@ -44,23 +51,23 @@ public:
bool IsUniqueNetSelected() const; bool IsUniqueNetSelected() const;
void SetBoard( BOARD* m_board ); // Build the list of netnames and populate the wxComboBox
void SetBoard( BOARD* aBoard );
private: private:
struct NET { struct NET {
int code; int m_Code;
int pos; int m_Pos;
wxString name; wxString m_Name;
bool operator <( const NET& aOther ) const bool operator <( const NET& aOther ) const
{ {
return name < aOther.name; return m_Name < aOther.m_Name;
} }
}; };
bool m_multiple; bool m_multiple;
std::vector<NET> m_nets; std::vector<NET> m_nets;
}; };
#endif #endif