kicad-source/kicad/pcm/dialogs/dialog_manage_repositories.cpp

295 lines
8.1 KiB
C++
Raw Normal View History

2020-10-26 03:54:36 -07:00
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2021 Andrew Lutsenko, anlutsenko at gmail dot com
* Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
2020-10-26 03:54:36 -07:00
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "dialog_manage_repositories.h"
#include <bitmaps/bitmap_types.h>
#include <bitmaps/bitmaps_list.h>
#include <grid_tricks.h>
#include <settings/kicad_settings.h>
#include <widgets/wx_grid.h>
#include <widgets/std_bitmap_button.h>
2020-10-26 03:54:36 -07:00
#define GRID_CELL_MARGIN 4
DIALOG_MANAGE_REPOSITORIES::DIALOG_MANAGE_REPOSITORIES(
wxWindow* parent, std::shared_ptr<PLUGIN_CONTENT_MANAGER> pcm ) :
DIALOG_MANAGE_REPOSITORIES_BASE( parent ),
m_pcm( pcm )
{
m_buttonAdd->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
m_buttonRemove->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
m_buttonMoveUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
m_buttonMoveDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
2020-10-26 03:54:36 -07:00
// 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 Repository" ) );
addMenu->Bind( wxEVT_COMMAND_MENU_SELECTED, &DIALOG_MANAGE_REPOSITORIES::OnAddDefault, this,
menuItem->GetId() );
m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
{
OnAdd( aEvent );
} ) );
2020-10-26 03:54:36 -07:00
for( int col = 0; col < m_grid->GetNumberCols(); col++ )
{
const wxString& heading = m_grid->GetColLabelValue( col );
int headingWidth = GetTextExtent( heading ).x + 2 * GRID_CELL_MARGIN;
// Set the minimal width to the column label size.
m_grid->SetColMinimalWidth( col, headingWidth );
}
// fix sizers now widgets are set.
finishDialogSettings();
2020-10-26 03:54:36 -07:00
}
DIALOG_MANAGE_REPOSITORIES::~DIALOG_MANAGE_REPOSITORIES()
{
// Delete the GRID_TRICKS.
m_grid->PopEventHandler( true );
}
2020-10-26 03:54:36 -07:00
void DIALOG_MANAGE_REPOSITORIES::setColumnWidths()
{
for( int col = 0; col < m_grid->GetNumberCols(); col++ )
{
// Set the width to see the full contents
2022-08-18 17:56:36 +01:00
m_grid->SetColSize( col, m_grid->GetVisibleWidth( col, true, true ) );
2020-10-26 03:54:36 -07:00
}
}
void DIALOG_MANAGE_REPOSITORIES::OnAdd( wxCommandEvent& event )
2020-10-26 03:54:36 -07:00
{
wxTextEntryDialog entry_dialog( this, _( "Please enter fully qualified repository url" ),
_( "Add repository" ) );
2020-10-26 03:54:36 -07:00
if( entry_dialog.ShowModal() == wxID_OK )
2020-10-26 03:54:36 -07:00
{
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;
}
2020-10-26 03:54:36 -07:00
return -1;
}
2020-10-26 03:54:36 -07:00
void DIALOG_MANAGE_REPOSITORIES::addRepository( const wxString& aUrl )
{
int matching_row;
2020-10-26 03:54:36 -07:00
if( ( matching_row = findRow( 1, aUrl ) ) >= 0 )
{
selectRow( matching_row );
return;
}
2021-11-24 21:34:41 -08:00
PCM_REPOSITORY repository;
WX_PROGRESS_REPORTER reporter( GetParent(), wxT( "" ), 1 );
2020-10-26 03:54:36 -07:00
if( m_pcm->FetchRepository( aUrl, repository, &reporter ) )
{
wxString name = repository.name;
int increment = 1;
2020-10-26 03:54:36 -07:00
while( findRow( 0, name ) >= 0 )
name = wxString::Format( "%s (%d)", repository.name, increment++ );
2020-10-26 03:54:36 -07:00
m_grid->Freeze();
2020-10-26 03:54:36 -07:00
m_grid->AppendRows();
int row = m_grid->GetNumberRows() - 1;
2020-10-26 03:54:36 -07:00
m_grid->SetCellValue( row, 0, name );
m_grid->SetCellValue( row, 1, aUrl );
2020-10-26 03:54:36 -07:00
setColumnWidths();
m_grid->Thaw();
selectRow( row );
2020-10-26 03:54:36 -07:00
}
}
void DIALOG_MANAGE_REPOSITORIES::OnAddDefault( wxCommandEvent& event )
{
addRepository( PCM_DEFAULT_REPOSITORY_URL );
}
2020-10-26 03:54:36 -07:00
void DIALOG_MANAGE_REPOSITORIES::OnRemoveButtonClicked( wxCommandEvent& event )
{
auto selectedRows = m_grid->GetSelectedRows();
// If nothing is selected or multiple rows are selected don't do anything.
if( selectedRows.size() != 1 )
{
wxBell();
return;
}
int selectedRow = selectedRows[0];
m_grid->DeleteRows( selectedRow );
setColumnWidths();
if( m_grid->GetNumberRows() > 0 )
m_grid->SelectRow( selectedRow == m_grid->GetNumberRows() ? selectedRow - 1 : selectedRow );
}
void DIALOG_MANAGE_REPOSITORIES::OnMoveUpButtonClicked( wxCommandEvent& event )
{
auto selectedRows = m_grid->GetSelectedRows();
// If nothing is selected or multiple rows are selected don't do anything.
if( selectedRows.size() != 1 )
return;
int selectedRow = selectedRows[0];
// If first row is selected, then it can't go any further up.
if( selectedRow == 0 )
{
wxBell();
return;
}
swapRows( selectedRow, selectedRow - 1 );
selectRow( selectedRow - 1 );
}
void DIALOG_MANAGE_REPOSITORIES::OnMoveDownButtonClicked( wxCommandEvent& event )
{
auto selectedRows = m_grid->GetSelectedRows();
// If nothing is selected or multiple rows are selected don't do anything.
if( selectedRows.size() != 1 )
return;
int selectedRow = selectedRows[0];
// If last row is selected, then it can't go any further down.
if( selectedRow + 1 == m_grid->GetNumberRows() )
{
wxBell();
return;
}
swapRows( selectedRow, selectedRow + 1 );
selectRow( selectedRow + 1 );
}
void DIALOG_MANAGE_REPOSITORIES::swapRows( int aRowA, int aRowB )
{
m_grid->Freeze();
wxString tempStr;
for( int column = 0; column < m_grid->GetNumberCols(); column++ )
{
tempStr = m_grid->GetCellValue( aRowA, column );
m_grid->SetCellValue( aRowA, column, m_grid->GetCellValue( aRowB, column ) );
m_grid->SetCellValue( aRowB, column, tempStr );
}
m_grid->Thaw();
}
void DIALOG_MANAGE_REPOSITORIES::OnGridCellClicked( wxGridEvent& event )
{
selectRow( event.GetRow() );
}
void DIALOG_MANAGE_REPOSITORIES::selectRow( int aRow )
{
m_grid->ClearSelection();
m_grid->SelectRow( aRow );
}
void DIALOG_MANAGE_REPOSITORIES::SetData( const std::vector<std::pair<wxString, wxString>>& aData )
2020-10-26 03:54:36 -07:00
{
m_grid->Freeze();
m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
m_grid->AppendRows( aData.size() );
for( size_t i = 0; i < aData.size(); i++ )
{
m_grid->SetCellValue( i, 0, aData[i].first );
m_grid->SetCellValue( i, 1, aData[i].second );
}
setColumnWidths();
m_grid->Thaw();
}
std::vector<std::pair<wxString, wxString>> DIALOG_MANAGE_REPOSITORIES::GetData()
2020-10-26 03:54:36 -07:00
{
std::vector<std::pair<wxString, wxString>> result;
2020-10-26 03:54:36 -07:00
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 ) ) );
2020-10-26 03:54:36 -07:00
}
return result;
}
void DIALOG_MANAGE_REPOSITORIES::OnSaveClicked( wxCommandEvent& event )
{
EndModal( wxID_SAVE );
}