From bb596ebdafa30eef2881fb10c29e4c3a23fc7f5f Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Thu, 19 Sep 2019 01:00:48 +0200 Subject: [PATCH] Fix width of part selector columns * Move column width manipulation into the lib tree adapter * Fix issue with GTK where if someone types too fast, the part column gets 0 width. Fixes: lp:1841584 * https://bugs.launchpad.net/kicad/+bug/1841584 --- common/lib_tree_model_adapter.cpp | 55 ++++++++++++++++++++++++++----- common/lib_tree_model_adapter.h | 25 ++++++++++++-- common/widgets/lib_tree.cpp | 17 ++-------- common/widgets/lib_tree.h | 3 -- 4 files changed, 73 insertions(+), 27 deletions(-) diff --git a/common/lib_tree_model_adapter.cpp b/common/lib_tree_model_adapter.cpp index f2bede8b3b..f0dca46c09 100644 --- a/common/lib_tree_model_adapter.cpp +++ b/common/lib_tree_model_adapter.cpp @@ -19,12 +19,15 @@ * with this program. If not, see . */ -#include #include +#include +#include #include #include +#define LIST_COLUMN_WIDTH_KEY wxT( "SelectorColumnWidth" ) + static const int kDataViewIndent = 20; @@ -75,13 +78,40 @@ LIB_TREE_MODEL_ADAPTER::LIB_TREE_MODEL_ADAPTER() m_col_part( nullptr ), m_col_desc( nullptr ), m_widget( nullptr ) -{} +{ + // Default column widths + m_colWidths[PART_COL] = 360; + m_colWidths[DESC_COL] = 2000; + + m_config = Kiface().KifaceSettings(); + m_configPrefix = typeid( this ).name(); + + // Read the column width from the config + int colWidth = 0; + + if( m_config->Read( m_configPrefix + LIST_COLUMN_WIDTH_KEY, &colWidth ) ) + m_colWidths[PART_COL] = colWidth; +} LIB_TREE_MODEL_ADAPTER::~LIB_TREE_MODEL_ADAPTER() {} +void LIB_TREE_MODEL_ADAPTER::SaveColWidths() +{ + if( m_widget ) + { + int colWidth = m_widget->GetColumn( PART_COL )->GetWidth(); + m_config->Write( m_configPrefix + LIST_COLUMN_WIDTH_KEY, colWidth ); + } + else + { + wxLogDebug( "Error saving column size, tree view doesn't exist" ); + } +} + + void LIB_TREE_MODEL_ADAPTER::SetFilter( CMP_FILTER_TYPE aFilter ) { m_filter = aFilter; @@ -184,14 +214,21 @@ void LIB_TREE_MODEL_ADAPTER::UpdateSearchString( wxString const& aSearch ) void LIB_TREE_MODEL_ADAPTER::AttachTo( wxDataViewCtrl* aDataViewCtrl ) { wxString partHead = _( "Item" ); - int partWidth = 360; wxString descHead = _( "Description" ); - int descWidth = 2000; if( aDataViewCtrl->GetColumnCount() > 0 ) { - partWidth = aDataViewCtrl->GetColumn( 0 )->GetWidth(); - descWidth = aDataViewCtrl->GetColumn( 1 )->GetWidth(); + int partWidth = aDataViewCtrl->GetColumn( PART_COL )->GetWidth(); + int descWidth = aDataViewCtrl->GetColumn( DESC_COL )->GetWidth(); + + // Only use the widths read back if they are non-zero. + // GTK returns the displayed width of the column, which is not calculated immediately + // this leads to cases of 0 column width if the user types too fast in the filter + if( descWidth > 0 ) + { + m_colWidths[PART_COL] = partWidth; + m_colWidths[DESC_COL] = descWidth; + } } m_widget = aDataViewCtrl; @@ -199,8 +236,10 @@ void LIB_TREE_MODEL_ADAPTER::AttachTo( wxDataViewCtrl* aDataViewCtrl ) aDataViewCtrl->AssociateModel( this ); aDataViewCtrl->ClearColumns(); - m_col_part = aDataViewCtrl->AppendTextColumn( partHead, 0, wxDATAVIEW_CELL_INERT, partWidth ); - m_col_desc = aDataViewCtrl->AppendTextColumn( descHead, 1, wxDATAVIEW_CELL_INERT, descWidth ); + m_col_part = aDataViewCtrl->AppendTextColumn( partHead, PART_COL, wxDATAVIEW_CELL_INERT, + m_colWidths[PART_COL] ); + m_col_desc = aDataViewCtrl->AppendTextColumn( descHead, DESC_COL, wxDATAVIEW_CELL_INERT, + m_colWidths[DESC_COL] ); } diff --git a/common/lib_tree_model_adapter.h b/common/lib_tree_model_adapter.h index 4863d3e5af..f9677f783d 100644 --- a/common/lib_tree_model_adapter.h +++ b/common/lib_tree_model_adapter.h @@ -87,7 +87,7 @@ * - `Compare()` - compare two rows, for sorting * - `HasDefaultCompare()` - whether sorted by default */ - + class TOOL_INTERACTIVE; class LIB_TREE_MODEL_ADAPTER: public wxDataViewModel @@ -114,6 +114,22 @@ public: CMP_FILTER_POWER, ///< list components flagged PWR }; + /** + * This enum defines the order of the columns in the tree view + */ + enum TREE_COLS + { + PART_COL = 0, ///< Part name column + DESC_COL, ///< Part description column + NUM_COLS ///< The number of tree columns + }; + + /** + * Save the column widths to the config file. This requires the tree view to still be + * valid. + */ + void SaveColWidths(); + /** * Set the component filter type. Must be set before adding libraries * @@ -273,7 +289,7 @@ protected: */ wxDataViewItem GetParent( wxDataViewItem const& aItem ) const override; - unsigned int GetColumnCount() const override { return 2; } + unsigned int GetColumnCount() const override { return NUM_COLS; } /** * Return the type of data stored in the column as indicated by wxVariant::GetType() @@ -322,6 +338,11 @@ private: wxDataViewColumn* m_col_desc; wxDataViewCtrl* m_widget; + int m_colWidths[NUM_COLS]; + + wxConfigBase* m_config; + wxString m_configPrefix; + /** * Find any results worth highlighting and expand them, according to given * criteria (f(CMP_TREE_NODE const*) -> bool) diff --git a/common/widgets/lib_tree.cpp b/common/widgets/lib_tree.cpp index 8ebfa456a2..f300c22e6d 100644 --- a/common/widgets/lib_tree.cpp +++ b/common/widgets/lib_tree.cpp @@ -29,9 +29,6 @@ #include #include #include -#include - -#define LIST_COLUMN_WIDTH_KEY wxT( "SelectorColumnWidth" ) LIB_TREE::LIB_TREE( wxWindow* aParent, LIB_TABLE* aLibTable, LIB_TREE_MODEL_ADAPTER::PTR& aAdapter, @@ -43,9 +40,6 @@ LIB_TREE::LIB_TREE( wxWindow* aParent, LIB_TABLE* aLibTable, LIB_TREE_MODEL_ADAP m_query_ctrl( nullptr ), m_details_ctrl( nullptr ) { - m_config = Kiface().KifaceSettings(); - m_configPrefix = typeid( m_adapter ).name(); - auto sizer = new wxBoxSizer( wxVERTICAL ); // Search text control @@ -116,11 +110,6 @@ LIB_TREE::LIB_TREE( wxWindow* aParent, LIB_TABLE* aLibTable, LIB_TREE_MODEL_ADAP Bind( COMPONENT_PRESELECTED, &LIB_TREE::onPreselect, this ); - int colWidth = 0; - - if( m_config->Read( m_configPrefix + LIST_COLUMN_WIDTH_KEY, &colWidth ) ) - m_tree_ctrl->GetColumn( 0 )->SetWidth( colWidth ); - // If wxTextCtrl::SetHint() is called before binding wxEVT_TEXT, the event // handler will intermittently fire. if( m_query_ctrl ) @@ -148,8 +137,8 @@ LIB_TREE::LIB_TREE( wxWindow* aParent, LIB_TABLE* aLibTable, LIB_TREE_MODEL_ADAP LIB_TREE::~LIB_TREE() { - int colWidth = m_tree_ctrl->GetColumn( 0 )->GetWidth(); - m_config->Write( m_configPrefix + LIST_COLUMN_WIDTH_KEY, colWidth ); + // Save the column widths to the config file + m_adapter->SaveColWidths(); } @@ -412,7 +401,7 @@ void LIB_TREE::onPreselect( wxCommandEvent& aEvent ) void LIB_TREE::onContextMenu( wxDataViewEvent& aEvent ) { TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool(); - + if( tool ) { tool->Activate(); diff --git a/common/widgets/lib_tree.h b/common/widgets/lib_tree.h index cdee3afc39..47631e528a 100644 --- a/common/widgets/lib_tree.h +++ b/common/widgets/lib_tree.h @@ -158,9 +158,6 @@ protected: void onContextMenu( wxDataViewEvent& aEvent ); protected: - wxConfigBase* m_config; - wxString m_configPrefix; - LIB_TABLE* m_lib_table; LIB_TREE_MODEL_ADAPTER::PTR m_adapter;