PCM: add "Add default" to manage repositories dialog

Allows to restore default repo url if it was removed.

Fixes #9588
This commit is contained in:
qu1ck 2022-05-23 20:40:12 +00:00 committed by Seth Hillbrand
parent 4afca58ce7
commit 0b7c79ea44
7 changed files with 115 additions and 86 deletions

View File

@ -26,6 +26,13 @@
///! Update the schema version whenever a migration is required
const int kicadSchemaVersion = 0;
const nlohmann::json PCM_DEFAULT_REPOSITORIES = nlohmann::json::array( {
nlohmann::json( {
{ "name", "KiCad official repository" },
{ "url", PCM_DEFAULT_REPOSITORY_URL },
} )
} );
KICAD_SETTINGS::KICAD_SETTINGS() :
APP_SETTINGS_BASE( "kicad", kicadSchemaVersion ), m_LeftWinWidth( 200 )
@ -66,12 +73,7 @@ KICAD_SETTINGS::KICAD_SETTINGS() :
wxString( entry["url"].get<std::string>() ) ) );
}
},
R"([
{
"name": "KiCad official repository",
"url": "https://repository.kicad.org/repository.json"
}
])"_json ) );
PCM_DEFAULT_REPOSITORIES ) );
m_params.emplace_back(
new PARAM<wxString>( "pcm.last_download_dir", &m_PcmLastDownloadDir, "" ) );

View File

@ -22,6 +22,7 @@
#define _KICAD_SETTINGS_H
#include <settings/app_settings.h>
#define PCM_DEFAULT_REPOSITORY_URL "https://repository.kicad.org/repository.json"
class KICAD_SETTINGS : public APP_SETTINGS_BASE

View File

@ -22,6 +22,7 @@
#include "bitmaps/bitmap_types.h"
#include "bitmaps/bitmaps_list.h"
#include "grid_tricks.h"
#include "kicad_settings.h"
#include "widgets/wx_grid.h"
@ -38,6 +39,21 @@ DIALOG_MANAGE_REPOSITORIES::DIALOG_MANAGE_REPOSITORIES(
m_buttonMoveUp->SetBitmap( KiBitmap( BITMAPS::small_up ) );
m_buttonMoveDown->SetBitmap( KiBitmap( BITMAPS::small_down ) );
// For aesthetic reasons, we must set the size of m_buttonAdd to match the other bitmaps
// manually (for instance m_buttonRemove)
Layout(); // Needed at least on MSW to compute the actual buttons sizes, after initializing
// their bitmaps
m_buttonAdd->SetWidthPadding( 4 );
m_buttonAdd->SetMinSize( m_buttonRemove->GetSize() );
m_buttonAdd->Bind( wxEVT_BUTTON, &DIALOG_MANAGE_REPOSITORIES::OnAdd, this );
wxMenu* addMenu = m_buttonAdd->GetSplitButtonMenu();
wxMenuItem* menuItem = addMenu->Append( wxID_ANY, _( "Add Default" ) );
addMenu->Bind( wxEVT_COMMAND_MENU_SELECTED, &DIALOG_MANAGE_REPOSITORIES::OnAddDefault, this,
menuItem->GetId() );
m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
for( int col = 0; col < m_grid->GetNumberCols(); col++ )
@ -71,64 +87,74 @@ void DIALOG_MANAGE_REPOSITORIES::setColumnWidths()
}
void DIALOG_MANAGE_REPOSITORIES::OnAddButtonClicked( wxCommandEvent& event )
void DIALOG_MANAGE_REPOSITORIES::OnAdd( wxCommandEvent& event )
{
wxTextEntryDialog entry_dialog( this,
_( "Please enter fully qualified repository url" ),
wxTextEntryDialog entry_dialog( this, _( "Please enter fully qualified repository url" ),
_( "Add repository" ) );
if( entry_dialog.ShowModal() == wxID_OK )
{
PCM_REPOSITORY repository;
wxString url = entry_dialog.GetValue();
const auto find_row = [&]( const int col, const wxString& val )
{
for( int row = 0; row < m_grid->GetNumberRows(); row++ )
{
if( m_grid->GetCellValue( row, col ) == val )
return row;
}
return -1;
};
int matching_row;
if( ( matching_row = find_row( 1, url ) ) >= 0 )
{
selectRow( matching_row );
}
else
{
WX_PROGRESS_REPORTER reporter( GetParent(), wxT( "" ), 1 );
if( m_pcm->FetchRepository( url, repository, &reporter ) )
{
wxString name = repository.name;
int increment = 1;
while( find_row( 0, name ) >= 0 )
name = wxString::Format( "%s (%d)", repository.name, increment++ );
m_grid->Freeze();
m_grid->AppendRows();
int row = m_grid->GetNumberRows() - 1;
m_grid->SetCellValue( row, 0, name );
m_grid->SetCellValue( row, 1, url );
setColumnWidths();
m_grid->Thaw();
selectRow( row );
}
}
wxString url = entry_dialog.GetValue();
addRepository( url );
}
}
int DIALOG_MANAGE_REPOSITORIES::findRow( int aCol, const wxString& aVal )
{
for( int row = 0; row < m_grid->GetNumberRows(); row++ )
{
if( m_grid->GetCellValue( row, aCol ) == aVal )
return row;
}
return -1;
}
void DIALOG_MANAGE_REPOSITORIES::addRepository( const wxString& aUrl )
{
int matching_row;
if( ( matching_row = findRow( 1, aUrl ) ) >= 0 )
{
selectRow( matching_row );
return;
}
PCM_REPOSITORY repository;
WX_PROGRESS_REPORTER reporter( GetParent(), wxT( "" ), 1 );
if( m_pcm->FetchRepository( aUrl, repository, &reporter ) )
{
wxString name = repository.name;
int increment = 1;
while( findRow( 0, name ) >= 0 )
name = wxString::Format( "%s (%d)", repository.name, increment++ );
m_grid->Freeze();
m_grid->AppendRows();
int row = m_grid->GetNumberRows() - 1;
m_grid->SetCellValue( row, 0, name );
m_grid->SetCellValue( row, 1, aUrl );
setColumnWidths();
m_grid->Thaw();
selectRow( row );
}
}
void DIALOG_MANAGE_REPOSITORIES::OnAddDefault( wxCommandEvent& event )
{
addRepository( PCM_DEFAULT_REPOSITORY_URL );
}
void DIALOG_MANAGE_REPOSITORIES::OnRemoveButtonClicked( wxCommandEvent& event )
{
auto selectedRows = m_grid->GetSelectedRows();
@ -250,8 +276,8 @@ std::vector<std::pair<wxString, wxString>> DIALOG_MANAGE_REPOSITORIES::GetData()
for( int i = 0; i < m_grid->GetNumberRows(); i++ )
{
result.push_back( std::make_pair( m_grid->GetCellValue( i, 0 ),
m_grid->GetCellValue( i, 1 ) ) );
result.push_back(
std::make_pair( m_grid->GetCellValue( i, 0 ), m_grid->GetCellValue( i, 1 ) ) );
}
return result;

View File

@ -32,7 +32,6 @@ class DIALOG_MANAGE_REPOSITORIES : public DIALOG_MANAGE_REPOSITORIES_BASE
{
protected:
// Handlers for DIALOG_MANAGE_REPOSITORIES_BASE events.
void OnAddButtonClicked( wxCommandEvent& event ) override;
void OnRemoveButtonClicked( wxCommandEvent& event ) override;
void OnMoveUpButtonClicked( wxCommandEvent& event ) override;
void OnMoveDownButtonClicked( wxCommandEvent& event ) override;
@ -47,12 +46,18 @@ public:
void SetData( const std::vector<std::pair<wxString, wxString>>& aData );
void OnAdd( wxCommandEvent& event );
void OnAddDefault( wxCommandEvent& event );
std::vector<std::pair<wxString, wxString>> GetData();
private:
void swapRows( int aRowA, int aRowB );
void selectRow( int aRow );
void setColumnWidths();
void addRepository( const wxString& aUrl );
int findRow( int aCol, const wxString& aVal );
std::shared_ptr<PLUGIN_CONTENT_MANAGER> m_pcm;
};

View File

@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version Oct 26 2018)
// C++ code generated with wxFormBuilder (version 3.10.1-0-g8feb16b3)
// http://www.wxformbuilder.org/
//
// PLEASE DO *NOT* EDIT THIS FILE!
@ -35,9 +35,9 @@ DIALOG_MANAGE_REPOSITORIES_BASE::DIALOG_MANAGE_REPOSITORIES_BASE( wxWindow* pare
m_grid->SetColSize( 1, 400 );
m_grid->EnableDragColMove( false );
m_grid->EnableDragColSize( true );
m_grid->SetColLabelSize( 22 );
m_grid->SetColLabelValue( 0, _("Name") );
m_grid->SetColLabelValue( 1, _("URL") );
m_grid->SetColLabelSize( 22 );
m_grid->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
// Rows
@ -54,7 +54,7 @@ DIALOG_MANAGE_REPOSITORIES_BASE::DIALOG_MANAGE_REPOSITORIES_BASE( wxWindow* pare
wxBoxSizer* bButtonsSizer;
bButtonsSizer = new wxBoxSizer( wxHORIZONTAL );
m_buttonAdd = new wxBitmapButton( this, wxID_ANY, wxNullBitmap, wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW|0 );
m_buttonAdd = new SPLIT_BUTTON( this, wxID_ANY, _( "Add Existing" ) );
bButtonsSizer->Add( m_buttonAdd, 0, wxRIGHT, 5 );
m_buttonMoveUp = new wxBitmapButton( this, wxID_ANY, wxNullBitmap, wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW|0 );
@ -93,7 +93,6 @@ DIALOG_MANAGE_REPOSITORIES_BASE::DIALOG_MANAGE_REPOSITORIES_BASE( wxWindow* pare
// Connect Events
m_grid->Connect( wxEVT_GRID_CELL_LEFT_CLICK, wxGridEventHandler( DIALOG_MANAGE_REPOSITORIES_BASE::OnGridCellClicked ), NULL, this );
m_buttonAdd->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_MANAGE_REPOSITORIES_BASE::OnAddButtonClicked ), NULL, this );
m_buttonMoveUp->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_MANAGE_REPOSITORIES_BASE::OnMoveUpButtonClicked ), NULL, this );
m_buttonMoveDown->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_MANAGE_REPOSITORIES_BASE::OnMoveDownButtonClicked ), NULL, this );
m_buttonRemove->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_MANAGE_REPOSITORIES_BASE::OnRemoveButtonClicked ), NULL, this );
@ -104,7 +103,6 @@ DIALOG_MANAGE_REPOSITORIES_BASE::~DIALOG_MANAGE_REPOSITORIES_BASE()
{
// Disconnect Events
m_grid->Disconnect( wxEVT_GRID_CELL_LEFT_CLICK, wxGridEventHandler( DIALOG_MANAGE_REPOSITORIES_BASE::OnGridCellClicked ), NULL, this );
m_buttonAdd->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_MANAGE_REPOSITORIES_BASE::OnAddButtonClicked ), NULL, this );
m_buttonMoveUp->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_MANAGE_REPOSITORIES_BASE::OnMoveUpButtonClicked ), NULL, this );
m_buttonMoveDown->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_MANAGE_REPOSITORIES_BASE::OnMoveDownButtonClicked ), NULL, this );
m_buttonRemove->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( DIALOG_MANAGE_REPOSITORIES_BASE::OnRemoveButtonClicked ), NULL, this );

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<wxFormBuilder_Project>
<FileVersion major="1" minor="15" />
<FileVersion major="1" minor="16" />
<object class="Project" expanded="1">
<property name="class_decoration">; </property>
<property name="code_generation">C++</property>
@ -14,6 +14,7 @@
<property name="file">dialog_manage_repositories_base</property>
<property name="first_id">1000</property>
<property name="help_provider">none</property>
<property name="image_path_wrapper_function_name"></property>
<property name="indent_with_spaces"></property>
<property name="internationalize">1</property>
<property name="name">dialog_manage_repositories_base</property>
@ -25,6 +26,7 @@
<property name="skip_php_events">1</property>
<property name="skip_python_events">1</property>
<property name="ui_table">UI</property>
<property name="use_array_enum">0</property>
<property name="use_enum">0</property>
<property name="use_microsoft_bom">0</property>
<object class="Dialog" expanded="1">
@ -50,6 +52,7 @@
<property name="subclass">DIALOG_SHIM; dialog_shim.h; forward_declare</property>
<property name="title">Manage Repositories</property>
<property name="tooltip"></property>
<property name="two_step_creation">0</property>
<property name="window_extra_style"></property>
<property name="window_name"></property>
<property name="window_style"></property>
@ -169,7 +172,7 @@
<property name="border">5</property>
<property name="flag">wxRIGHT</property>
<property name="proportion">0</property>
<object class="wxBitmapButton" expanded="1">
<object class="CustomControl" expanded="1">
<property name="BottomDockable">1</property>
<property name="LeftDockable">1</property>
<property name="RightDockable">1</property>
@ -180,31 +183,27 @@
<property name="aui_row"></property>
<property name="best_size"></property>
<property name="bg"></property>
<property name="bitmap"></property>
<property name="caption"></property>
<property name="caption_visible">1</property>
<property name="center_pane">0</property>
<property name="class">SPLIT_BUTTON</property>
<property name="close_button">1</property>
<property name="construction">m_buttonAdd = new SPLIT_BUTTON( this, wxID_ANY, _( &quot;Add Existing&quot; ) );</property>
<property name="context_help"></property>
<property name="context_menu">1</property>
<property name="current"></property>
<property name="default">0</property>
<property name="declaration"></property>
<property name="default_pane">0</property>
<property name="disabled"></property>
<property name="dock">Dock</property>
<property name="dock_fixed">0</property>
<property name="docking">Left</property>
<property name="enabled">1</property>
<property name="fg"></property>
<property name="floatable">1</property>
<property name="focus"></property>
<property name="font"></property>
<property name="gripper">0</property>
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label">Add repository</property>
<property name="margins"></property>
<property name="markup">0</property>
<property name="include">#include &lt;widgets/split_button.h&gt;</property>
<property name="max_size"></property>
<property name="maximize_button">0</property>
<property name="maximum_size"></property>
@ -219,23 +218,16 @@
<property name="permission">protected</property>
<property name="pin_button">1</property>
<property name="pos"></property>
<property name="position"></property>
<property name="pressed"></property>
<property name="resize">Resizable</property>
<property name="settings"></property>
<property name="show">1</property>
<property name="size"></property>
<property name="style"></property>
<property name="subclass">; ; forward_declare</property>
<property name="subclass">forward_declare</property>
<property name="toolbar_pane">0</property>
<property name="tooltip"></property>
<property name="validator_data_type"></property>
<property name="validator_style">wxFILTER_NONE</property>
<property name="validator_type">wxDefaultValidator</property>
<property name="validator_variable"></property>
<property name="window_extra_style"></property>
<property name="window_name"></property>
<property name="window_style"></property>
<event name="OnButtonClick">OnAddButtonClicked</event>
</object>
</object>
<object class="sizeritem" expanded="1">
@ -251,6 +243,7 @@
<property name="aui_name"></property>
<property name="aui_position"></property>
<property name="aui_row"></property>
<property name="auth_needed">0</property>
<property name="best_size"></property>
<property name="bg"></property>
<property name="bitmap"></property>
@ -324,6 +317,7 @@
<property name="aui_name"></property>
<property name="aui_position"></property>
<property name="aui_row"></property>
<property name="auth_needed">0</property>
<property name="best_size"></property>
<property name="bg"></property>
<property name="bitmap"></property>
@ -407,6 +401,7 @@
<property name="aui_name"></property>
<property name="aui_position"></property>
<property name="aui_row"></property>
<property name="auth_needed">0</property>
<property name="best_size"></property>
<property name="bg"></property>
<property name="bitmap"></property>

View File

@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version Oct 26 2018)
// C++ code generated with wxFormBuilder (version 3.10.1-0-g8feb16b3)
// http://www.wxformbuilder.org/
//
// PLEASE DO *NOT* EDIT THIS FILE!
@ -11,6 +11,7 @@
#include <wx/xrc/xmlres.h>
#include <wx/intl.h>
class WX_GRID;
class forward_declare;
#include "dialog_shim.h"
#include <wx/colour.h>
@ -19,6 +20,7 @@ class WX_GRID;
#include <wx/font.h>
#include <wx/grid.h>
#include <wx/gdicmn.h>
#include <widgets/split_button.h>
#include <wx/bmpbuttn.h>
#include <wx/bitmap.h>
#include <wx/image.h>
@ -39,7 +41,7 @@ class DIALOG_MANAGE_REPOSITORIES_BASE : public DIALOG_SHIM
protected:
WX_GRID* m_grid;
wxBitmapButton* m_buttonAdd;
SPLIT_BUTTON* m_buttonAdd;
wxBitmapButton* m_buttonMoveUp;
wxBitmapButton* m_buttonMoveDown;
wxBitmapButton* m_buttonRemove;
@ -47,9 +49,8 @@ class DIALOG_MANAGE_REPOSITORIES_BASE : public DIALOG_SHIM
wxButton* m_sdbSizer1Save;
wxButton* m_sdbSizer1Cancel;
// Virtual event handlers, overide them in your derived class
// Virtual event handlers, override them in your derived class
virtual void OnGridCellClicked( wxGridEvent& event ) { event.Skip(); }
virtual void OnAddButtonClicked( wxCommandEvent& event ) { event.Skip(); }
virtual void OnMoveUpButtonClicked( wxCommandEvent& event ) { event.Skip(); }
virtual void OnMoveDownButtonClicked( wxCommandEvent& event ) { event.Skip(); }
virtual void OnRemoveButtonClicked( wxCommandEvent& event ) { event.Skip(); }
@ -59,6 +60,7 @@ class DIALOG_MANAGE_REPOSITORIES_BASE : public DIALOG_SHIM
public:
DIALOG_MANAGE_REPOSITORIES_BASE( wxWindow* parent, wxWindowID id = wxID_ANY, const wxString& title = _("Manage Repositories"), const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( -1,-1 ), long style = wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER );
~DIALOG_MANAGE_REPOSITORIES_BASE();
};