Prevent hiding of reference column.

Using the quantity column for the collapse/expand controls feels too odd
when you expand and everything under the parent is a '1'.

Fixes: lp:1780847
* https://bugs.launchpad.net/kicad/+bug/1780847
This commit is contained in:
Jeff Young 2019-02-03 20:53:21 +00:00
parent 5edf3503ca
commit 8ca76177c0
1 changed files with 66 additions and 39 deletions

View File

@ -44,13 +44,6 @@
#include "dialog_fields_editor_global.h" #include "dialog_fields_editor_global.h"
#define QUANTITY_COLUMN ( 0 )
// The first column in m_grid is Quantity, so offset the rows
static constexpr int colNumber( const int aCol )
{
return aCol + 1;
}
enum enum
{ {
@ -72,12 +65,12 @@ public:
protected: protected:
void showPopupMenu( wxMenu& menu ) override void showPopupMenu( wxMenu& menu ) override
{ {
if( m_grid->GetGridCursorCol() == colNumber( FOOTPRINT ) ) if( m_grid->GetGridCursorCol() == FOOTPRINT )
{ {
menu.Append( MYID_SELECT_FOOTPRINT, _( "Select Footprint..." ), _( "Browse for footprint" ) ); menu.Append( MYID_SELECT_FOOTPRINT, _( "Select Footprint..." ), _( "Browse for footprint" ) );
menu.AppendSeparator(); menu.AppendSeparator();
} }
else if( m_grid->GetGridCursorCol() == colNumber( DATASHEET ) ) else if( m_grid->GetGridCursorCol() == DATASHEET )
{ {
menu.Append( MYID_SHOW_DATASHEET, _( "Show Datasheet" ), _( "Show datasheet in browser" ) ); menu.Append( MYID_SHOW_DATASHEET, _( "Show Datasheet" ), _( "Show datasheet in browser" ) );
menu.AppendSeparator(); menu.AppendSeparator();
@ -91,19 +84,17 @@ protected:
if( event.GetId() == MYID_SELECT_FOOTPRINT ) if( event.GetId() == MYID_SELECT_FOOTPRINT )
{ {
// pick a footprint using the footprint picker. // pick a footprint using the footprint picker.
wxString fpid = m_grid->GetCellValue( m_grid->GetGridCursorRow(), wxString fpid = m_grid->GetCellValue( m_grid->GetGridCursorRow(), FOOTPRINT );
colNumber( FOOTPRINT ) );
KIWAY_PLAYER* frame = m_dlg->Kiway().Player( FRAME_PCB_MODULE_VIEWER_MODAL, true, m_dlg ); KIWAY_PLAYER* frame = m_dlg->Kiway().Player( FRAME_PCB_MODULE_VIEWER_MODAL, true, m_dlg );
if( frame->ShowModal( &fpid, m_dlg ) ) if( frame->ShowModal( &fpid, m_dlg ) )
m_grid->SetCellValue( m_grid->GetGridCursorRow(), colNumber( FOOTPRINT ), fpid ); m_grid->SetCellValue( m_grid->GetGridCursorRow(), FOOTPRINT, fpid );
frame->Destroy(); frame->Destroy();
} }
else if (event.GetId() == MYID_SHOW_DATASHEET ) else if (event.GetId() == MYID_SHOW_DATASHEET )
{ {
wxString datasheet_uri = m_grid->GetCellValue( m_grid->GetGridCursorRow(), wxString datasheet_uri = m_grid->GetCellValue( m_grid->GetGridCursorRow(), DATASHEET );
colNumber( DATASHEET ) );
datasheet_uri = ResolveUriByEnvVars( datasheet_uri ); datasheet_uri = ResolveUriByEnvVars( datasheet_uri );
GetAssociatedDocument( m_dlg, datasheet_uri ); GetAssociatedDocument( m_dlg, datasheet_uri );
} }
@ -114,9 +105,16 @@ protected:
if( event.GetId() >= GRIDTRICKS_FIRST_SHOWHIDE && event.GetId() < GRIDTRICKS_LAST_ID ) if( event.GetId() >= GRIDTRICKS_FIRST_SHOWHIDE && event.GetId() < GRIDTRICKS_LAST_ID )
{ {
if( !m_grid->IsColShown( REFERENCE ) )
{
DisplayError( m_dlg, _( "The Reference column cannot be hidden." ) );
m_grid->ShowCol( REFERENCE );
}
// Refresh Show checkboxes from grid columns // Refresh Show checkboxes from grid columns
for( int i = 0; i < m_fieldsCtrl->GetItemCount(); ++i ) for( int i = 0; i < m_fieldsCtrl->GetItemCount(); ++i )
m_fieldsCtrl->SetToggleValue( colNumber( m_grid->IsColShown( i ) ), i, 1 ); m_fieldsCtrl->SetToggleValue( m_grid->IsColShown( i ), i, 1 );
} }
} }
@ -151,6 +149,8 @@ struct DATA_MODEL_ROW
#define SHOW_FIELD_COLUMN 1 #define SHOW_FIELD_COLUMN 1
#define GROUP_BY_COLUMN 2 #define GROUP_BY_COLUMN 2
#define QUANTITY_COLUMN ( GetNumberCols() - 1 )
#ifdef __WXMAC__ #ifdef __WXMAC__
#define COLUMN_MARGIN 5 #define COLUMN_MARGIN 5
#else #else
@ -213,12 +213,16 @@ public:
int GetNumberRows() override { return m_rows.size(); } int GetNumberRows() override { return m_rows.size(); }
int GetNumberCols() override { return m_fieldNames.size(); } // Columns are fieldNames + quantity column
int GetNumberCols() override { return m_fieldNames.size() + 1; }
wxString GetColLabelValue( int aCol ) override wxString GetColLabelValue( int aCol ) override
{ {
return m_fieldNames[ aCol ]; if( aCol == QUANTITY_COLUMN )
return _( "Qty" );
else
return m_fieldNames[ aCol ];
} }
@ -230,7 +234,7 @@ public:
wxString GetValue( int aRow, int aCol ) override wxString GetValue( int aRow, int aCol ) override
{ {
if( aCol == QUANTITY_COLUMN ) if( aCol == REFERENCE )
{ {
// Poor-man's tree controls // Poor-man's tree controls
if( m_rows[ aRow ].m_Flag == GROUP_COLLAPSED ) if( m_rows[ aRow ].m_Flag == GROUP_COLLAPSED )
@ -259,7 +263,7 @@ public:
for( const auto& ref : group.m_Refs ) for( const auto& ref : group.m_Refs )
{ {
if( aCol == colNumber( REFERENCE ) || aCol == QUANTITY_COLUMN ) if( aCol == REFERENCE || aCol == QUANTITY_COLUMN )
{ {
references.push_back( ref ); references.push_back( ref );
} }
@ -278,7 +282,7 @@ public:
} }
} }
if( aCol == colNumber( REFERENCE ) || aCol == QUANTITY_COLUMN ) if( aCol == REFERENCE || aCol == QUANTITY_COLUMN )
{ {
// Remove duplicates (other units of multi-unit parts) // Remove duplicates (other units of multi-unit parts)
std::sort( references.begin(), references.end(), std::sort( references.begin(), references.end(),
@ -304,7 +308,7 @@ public:
references.erase( logicalEnd, references.end() ); references.erase( logicalEnd, references.end() );
} }
if( aCol == colNumber( REFERENCE ) ) if( aCol == REFERENCE )
{ {
fieldValue = SCH_REFERENCE_LIST::Shorthand( references ); fieldValue = SCH_REFERENCE_LIST::Shorthand( references );
} }
@ -319,7 +323,7 @@ public:
void SetValue( int aRow, int aCol, const wxString &aValue ) override void SetValue( int aRow, int aCol, const wxString &aValue ) override
{ {
if( aCol == colNumber( REFERENCE ) || aCol == QUANTITY_COLUMN ) if( aCol == REFERENCE || aCol == QUANTITY_COLUMN )
return; // Can't modify references or quantity return; // Can't modify references or quantity
wxString value = EscapeString( aValue ); wxString value = EscapeString( aValue );
@ -345,12 +349,12 @@ public:
bool retVal; bool retVal;
// Primary sort key is sortCol; secondary is always REFERENCE (column 1) // Primary sort key is sortCol; secondary is always REFERENCE (column 0)
wxString lhs = dataModel->GetValue( (DATA_MODEL_ROW&) lhGroup, sortCol ); wxString lhs = dataModel->GetValue( (DATA_MODEL_ROW&) lhGroup, sortCol );
wxString rhs = dataModel->GetValue( (DATA_MODEL_ROW&) rhGroup, sortCol ); wxString rhs = dataModel->GetValue( (DATA_MODEL_ROW&) rhGroup, sortCol );
if( lhs == rhs || sortCol == colNumber( REFERENCE ) ) if( lhs == rhs || sortCol == REFERENCE )
{ {
wxString lhRef = lhGroup.m_Refs[ 0 ].GetRef() + lhGroup.m_Refs[ 0 ].GetRefNumber(); wxString lhRef = lhGroup.m_Refs[ 0 ].GetRef() + lhGroup.m_Refs[ 0 ].GetRefNumber();
wxString rhRef = rhGroup.m_Refs[ 0 ].GetRef() + rhGroup.m_Refs[ 0 ].GetRefNumber(); wxString rhRef = rhGroup.m_Refs[ 0 ].GetRef() + rhGroup.m_Refs[ 0 ].GetRefNumber();
@ -618,7 +622,7 @@ public:
{ {
int width = 0; int width = 0;
if( aCol == colNumber( REFERENCE ) ) if( aCol == REFERENCE )
{ {
for( int row = 0; row < GetNumberRows(); ++row ) for( int row = 0; row < GetNumberRows(); ++row )
{ {
@ -708,9 +712,9 @@ DIALOG_FIELDS_EDITOR_GLOBAL::DIALOG_FIELDS_EDITOR_GLOBAL( SCH_EDIT_FRAME* parent
for( int i = 0; i < m_fieldsCtrl->GetItemCount(); ++i ) for( int i = 0; i < m_fieldsCtrl->GetItemCount(); ++i )
{ {
if( m_fieldsCtrl->GetToggleValue( i, 1 ) ) if( m_fieldsCtrl->GetToggleValue( i, 1 ) )
m_grid->ShowCol( colNumber( i ) ); m_grid->ShowCol( i );
else else
m_grid->HideCol( colNumber( i ) ); m_grid->HideCol( i );
} }
// add Cut, Copy, and Paste to wxGrid // add Cut, Copy, and Paste to wxGrid
@ -722,23 +726,23 @@ DIALOG_FIELDS_EDITOR_GLOBAL::DIALOG_FIELDS_EDITOR_GLOBAL( SCH_EDIT_FRAME* parent
// set reference column attributes // set reference column attributes
wxGridCellAttr* attr = new wxGridCellAttr; wxGridCellAttr* attr = new wxGridCellAttr;
attr->SetReadOnly(); attr->SetReadOnly();
m_grid->SetColAttr( colNumber( REFERENCE ), attr ); m_grid->SetColAttr( REFERENCE, attr );
// set footprint column browse button // set footprint column browse button
attr = new wxGridCellAttr; attr = new wxGridCellAttr;
attr->SetEditor( new GRID_CELL_FOOTPRINT_ID_EDITOR( this ) ); attr->SetEditor( new GRID_CELL_FOOTPRINT_ID_EDITOR( this ) );
m_grid->SetColAttr( colNumber( FOOTPRINT ), attr ); m_grid->SetColAttr( FOOTPRINT, attr );
// set datasheet column viewer button // set datasheet column viewer button
attr = new wxGridCellAttr; attr = new wxGridCellAttr;
attr->SetEditor( new GRID_CELL_URL_EDITOR( this ) ); attr->SetEditor( new GRID_CELL_URL_EDITOR( this ) );
m_grid->SetColAttr( colNumber( DATASHEET ), attr ); m_grid->SetColAttr( DATASHEET, attr );
// set quantities column attributes // set quantities column attributes
attr = new wxGridCellAttr; attr = new wxGridCellAttr;
attr->SetReadOnly(); attr->SetReadOnly();
m_grid->SetColAttr( QUANTITY_COLUMN, attr ); m_grid->SetColAttr( m_dataModel->GetColsCount() - 1, attr );
m_grid->SetColFormatNumber( QUANTITY_COLUMN ); m_grid->SetColFormatNumber( m_dataModel->GetColsCount() - 1 );
m_grid->AutoSizeColumns( false ); m_grid->AutoSizeColumns( false );
for( int col = 0; col < m_grid->GetNumberCols(); ++ col ) for( int col = 0; col < m_grid->GetNumberCols(); ++ col )
@ -750,7 +754,7 @@ DIALOG_FIELDS_EDITOR_GLOBAL::DIALOG_FIELDS_EDITOR_GLOBAL( SCH_EDIT_FRAME* parent
int textWidth = m_dataModel->GetDataWidth( col ) + COLUMN_MARGIN; int textWidth = m_dataModel->GetDataWidth( col ) + COLUMN_MARGIN;
int maxWidth = defaultDlgSize.x / 3; int maxWidth = defaultDlgSize.x / 3;
if( col == QUANTITY_COLUMN ) if( col == m_grid->GetNumberCols() - 1 )
m_grid->SetColSize( col, std::min( std::max( 50, textWidth ), maxWidth ) ); m_grid->SetColSize( col, std::min( std::max( 50, textWidth ), maxWidth ) );
else else
m_grid->SetColSize( col, std::min( std::max( 100, textWidth ), maxWidth ) ); m_grid->SetColSize( col, std::min( std::max( 100, textWidth ), maxWidth ) );
@ -816,8 +820,8 @@ void DIALOG_FIELDS_EDITOR_GLOBAL::AddField( const wxString& aName,
wxVector<wxVariant> fieldsCtrlRow; wxVector<wxVariant> fieldsCtrlRow;
m_config->Read("SymbolFieldEditor/Show/" + aName, &defaultShow); m_config->Read( "SymbolFieldEditor/Show/" + aName, &defaultShow );
m_config->Read("SymbolFieldEditor/GroupBy/" + aName, &defaultSortBy); m_config->Read( "SymbolFieldEditor/GroupBy/" + aName, &defaultSortBy );
fieldsCtrlRow.push_back( wxVariant( aName ) ); fieldsCtrlRow.push_back( wxVariant( aName ) );
fieldsCtrlRow.push_back( wxVariant( defaultShow ) ); fieldsCtrlRow.push_back( wxVariant( defaultShow ) );
@ -843,7 +847,9 @@ void DIALOG_FIELDS_EDITOR_GLOBAL::LoadFieldNames()
userFieldNames.insert( comp->GetField( j )->GetName() ); userFieldNames.insert( comp->GetField( j )->GetName() );
} }
m_dataModel->AddColumn( _( "Qty" ) ); // Force References to always be shown
m_config->Write( "SymbolFieldEditor/Show/Reference", true );
AddField( _( "Reference" ), true, true ); AddField( _( "Reference" ), true, true );
AddField( _( "Value" ), true, true ); AddField( _( "Value" ), true, true );
AddField( _( "Footprint" ), true, true ); AddField( _( "Footprint" ), true, true );
@ -861,6 +867,11 @@ void DIALOG_FIELDS_EDITOR_GLOBAL::LoadFieldNames()
void DIALOG_FIELDS_EDITOR_GLOBAL::OnAddField( wxCommandEvent& event ) void DIALOG_FIELDS_EDITOR_GLOBAL::OnAddField( wxCommandEvent& event )
{ {
// quantities column will become new field column, so it needs to be reset
auto attr = new wxGridCellAttr;
m_grid->SetColAttr( m_dataModel->GetColsCount() - 1, attr );
m_grid->SetColFormatCustom( m_dataModel->GetColsCount() - 1, wxGRID_VALUE_STRING );
wxTextEntryDialog dlg( this, _( "New field name:" ), _( "Add Field" ) ); wxTextEntryDialog dlg( this, _( "New field name:" ), _( "Add Field" ) );
if( dlg.ShowModal() != wxID_OK ) if( dlg.ShowModal() != wxID_OK )
@ -889,6 +900,13 @@ void DIALOG_FIELDS_EDITOR_GLOBAL::OnAddField( wxCommandEvent& event )
wxGridTableMessage msg( m_dataModel, wxGRIDTABLE_NOTIFY_COLS_INSERTED, m_fieldsCtrl->GetItemCount(), 1 ); wxGridTableMessage msg( m_dataModel, wxGRIDTABLE_NOTIFY_COLS_INSERTED, m_fieldsCtrl->GetItemCount(), 1 );
m_grid->ProcessTableMessage( msg ); m_grid->ProcessTableMessage( msg );
// set up attributes on the new quantities column
attr = new wxGridCellAttr;
attr->SetReadOnly();
m_grid->SetColAttr( m_dataModel->GetColsCount() - 1, attr );
m_grid->SetColFormatNumber( m_dataModel->GetColsCount() - 1 );
m_grid->SetColSize( m_dataModel->GetColsCount() - 1, 50 );
} }
@ -907,13 +925,22 @@ void DIALOG_FIELDS_EDITOR_GLOBAL::OnColumnItemToggled( wxDataViewEvent& event )
case SHOW_FIELD_COLUMN: case SHOW_FIELD_COLUMN:
{ {
bool value = m_fieldsCtrl->GetToggleValue( row, col ); bool value = m_fieldsCtrl->GetToggleValue( row, col );
if( row == REFERENCE && !value )
{
DisplayError( this, _( "The Reference column cannot be hidden." ) );
value = true;
m_fieldsCtrl->SetToggleValue( value, row, col );
}
wxString fieldName = m_fieldsCtrl->GetTextValue( row, FIELD_NAME_COLUMN ); wxString fieldName = m_fieldsCtrl->GetTextValue( row, FIELD_NAME_COLUMN );
m_config->Write( "SymbolFieldEditor/Show/" + fieldName, value ); m_config->Write( "SymbolFieldEditor/Show/" + fieldName, value );
if( value ) if( value )
m_grid->ShowCol( colNumber( row ) ); m_grid->ShowCol( row );
else else
m_grid->HideCol( colNumber( row ) ); // grid's columns map to fieldsCtrl's rows m_grid->HideCol( row ); // grid's columns map to fieldsCtrl's rows
break; break;
} }
@ -973,7 +1000,7 @@ void DIALOG_FIELDS_EDITOR_GLOBAL::OnRegroupComponents( wxCommandEvent& event )
void DIALOG_FIELDS_EDITOR_GLOBAL::OnTableCellClick( wxGridEvent& event ) void DIALOG_FIELDS_EDITOR_GLOBAL::OnTableCellClick( wxGridEvent& event )
{ {
if( event.GetCol() == QUANTITY_COLUMN ) if( event.GetCol() == REFERENCE )
{ {
m_grid->ClearSelection(); m_grid->ClearSelection();
m_grid->SetGridCursor( event.GetRow(), event.GetCol() ); m_grid->SetGridCursor( event.GetRow(), event.GetCol() );