KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_manage_repositories.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2021 Andrew Lutsenko, anlutsenko at gmail dot com
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
24#include <grid_tricks.h>
26#include <widgets/wx_grid.h>
28
29
30#define GRID_CELL_MARGIN 4
31
32
34 wxWindow* parent, std::shared_ptr<PLUGIN_CONTENT_MANAGER> pcm ) :
36 m_pcm( pcm )
37{
38 m_buttonAdd->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
39 m_buttonRemove->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
40 m_buttonMoveUp->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
41 m_buttonMoveDown->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
42
43 // For aesthetic reasons, we must set the size of m_buttonAdd to match the other bitmaps
44 // manually (for instance m_buttonRemove)
45 Layout(); // Needed at least on MSW to compute the actual buttons sizes, after initializing
46 // their bitmaps
49
50 m_buttonAdd->Bind( wxEVT_BUTTON, &DIALOG_MANAGE_REPOSITORIES::OnAdd, this );
51
52 wxMenu* addMenu = m_buttonAdd->GetSplitButtonMenu();
53 wxMenuItem* menuItem = addMenu->Append( wxID_ANY, _( "Add Default Repository" ) );
54
55 addMenu->Bind( wxEVT_COMMAND_MENU_SELECTED, &DIALOG_MANAGE_REPOSITORIES::OnAddDefault, this,
56 menuItem->GetId() );
57
58 m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
59 {
60 OnAdd( aEvent );
61 } ) );
62
63 for( int col = 0; col < m_grid->GetNumberCols(); col++ )
64 {
65 const wxString& heading = m_grid->GetColLabelValue( col );
66 int headingWidth = GetTextExtent( heading ).x + 2 * GRID_CELL_MARGIN;
67
68 // Set the minimal width to the column label size.
69 m_grid->SetColMinimalWidth( col, headingWidth );
70 }
71
72 // fix sizers now widgets are set.
74}
75
76
78{
79 // Delete the GRID_TRICKS.
80 m_grid->PopEventHandler( true );
81}
82
83
85{
86 for( int col = 0; col < m_grid->GetNumberCols(); col++ )
87 {
88 // Set the width to see the full contents
89 m_grid->SetColSize( col, m_grid->GetVisibleWidth( col, true, true ) );
90 }
91}
92
93
94void DIALOG_MANAGE_REPOSITORIES::OnAdd( wxCommandEvent& event )
95{
96 wxTextEntryDialog entry_dialog( this, _( "Fully qualified repository url:" ),
97 _( "Add Repository" ) );
98
99 if( entry_dialog.ShowModal() == wxID_OK )
100 {
101 wxString url = entry_dialog.GetValue();
102 addRepository( url );
103 }
104}
105
106
107int DIALOG_MANAGE_REPOSITORIES::findRow( int aCol, const wxString& aVal )
108{
109 for( int row = 0; row < m_grid->GetNumberRows(); row++ )
110 {
111 if( m_grid->GetCellValue( row, aCol ) == aVal )
112 return row;
113 }
114
115 return -1;
116}
117
118
120{
121 int matching_row;
122
123 if( ( matching_row = findRow( 1, aUrl ) ) >= 0 )
124 {
125 selectRow( matching_row );
126 return;
127 }
128
129 PCM_REPOSITORY repository;
130 WX_PROGRESS_REPORTER reporter( GetParent(), wxT( "" ), 1, PR_CAN_ABORT );
131
132 if( m_pcm->FetchRepository( aUrl, repository, &reporter ) )
133 {
134 wxString name = repository.name;
135 int increment = 1;
136
137 while( findRow( 0, name ) >= 0 )
138 name = wxString::Format( "%s (%d)", repository.name, increment++ );
139
140 m_grid->Freeze();
141
142 m_grid->AppendRows();
143 int row = m_grid->GetNumberRows() - 1;
144
145 m_grid->SetCellValue( row, 0, name );
146 m_grid->SetCellValue( row, 1, aUrl );
147
149 m_grid->Thaw();
150
151 selectRow( row );
152 }
153}
154
155
156void DIALOG_MANAGE_REPOSITORIES::OnAddDefault( wxCommandEvent& event )
157{
159}
160
161
163{
165 [&]( int row )
166 {
167 m_grid->DeleteRows( row );
169 } );
170}
171
172
174{
176 [&]( int row )
177 {
178 m_grid->SwapRows( row, row - 1 );
179 } );
180}
181
182
184{
186 [&]( int row )
187 {
188 m_grid->SwapRows( row, row + 1 );
189 } );
190}
191
192
194{
195 selectRow( event.GetRow() );
196}
197
198
200{
201 m_grid->ClearSelection();
202 m_grid->SelectRow( aRow );
203}
204
205
206void DIALOG_MANAGE_REPOSITORIES::SetData( const std::vector<std::pair<wxString, wxString>>& aData )
207{
208 m_grid->Freeze();
209
210 m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
211 m_grid->AppendRows( aData.size() );
212
213 for( size_t i = 0; i < aData.size(); i++ )
214 {
215 m_grid->SetCellValue( i, 0, aData[i].first );
216 m_grid->SetCellValue( i, 1, aData[i].second );
217 }
218
220
221 m_grid->Thaw();
222}
223
224
225std::vector<std::pair<wxString, wxString>> DIALOG_MANAGE_REPOSITORIES::GetData()
226{
227 std::vector<std::pair<wxString, wxString>> result;
228
229 for( int i = 0; i < m_grid->GetNumberRows(); i++ )
230 {
231 result.push_back(
232 std::make_pair( m_grid->GetCellValue( i, 0 ), m_grid->GetCellValue( i, 1 ) ) );
233 }
234
235 return result;
236}
237
238
239void DIALOG_MANAGE_REPOSITORIES::OnSaveClicked( wxCommandEvent& event )
240{
241 EndModal( wxID_SAVE );
242}
const char * name
Definition: DXF_plotter.cpp:62
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition: bitmap.cpp:110
Class DIALOG_MANAGE_REPOSITORIES_BASE.
void OnRemoveButtonClicked(wxCommandEvent &event) override
void OnMoveDownButtonClicked(wxCommandEvent &event) override
std::vector< std::pair< wxString, wxString > > GetData()
void addRepository(const wxString &aUrl)
void OnSaveClicked(wxCommandEvent &event) override
std::shared_ptr< PLUGIN_CONTENT_MANAGER > m_pcm
void OnAdd(wxCommandEvent &event)
DIALOG_MANAGE_REPOSITORIES(wxWindow *parent, std::shared_ptr< PLUGIN_CONTENT_MANAGER > aPcm)
Constructor.
int findRow(int aCol, const wxString &aVal)
void SetData(const std::vector< std::pair< wxString, wxString > > &aData)
void OnMoveUpButtonClicked(wxCommandEvent &event) override
void OnAddDefault(wxCommandEvent &event)
void OnGridCellClicked(wxGridEvent &event) override
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:61
void SetBitmap(const wxBitmapBundle &aBmp)
void SetWidthPadding(int aPadding)
wxMenu * GetSplitButtonMenu()
void SetMinSize(const wxSize &aSize) override
void SetBitmap(const wxBitmapBundle &aBmp)
int GetVisibleWidth(int aCol, bool aHeader=true, bool aContents=true, bool aKeep=false)
Calculate the specified column based on the actual size of the text on screen.
Definition: wx_grid.cpp:916
void OnMoveRowUp(const std::function< void(int row)> &aMover)
Definition: wx_grid.cpp:766
void SwapRows(int aRowA, int aRowB)
These aren't that tricky, but might as well share code.
Definition: wx_grid.cpp:755
void OnMoveRowDown(const std::function< void(int row)> &aMover)
Definition: wx_grid.cpp:799
void OnDeleteRows(const std::function< void(int row)> &aDeleter)
Handles a row deletion event.
Definition: wx_grid.cpp:704
Multi-thread safe progress reporter dialog, intended for use of tasks that parallel reporting back of...
#define GRID_CELL_MARGIN
#define _(s)
#define PCM_DEFAULT_REPOSITORY_URL
Package installation entry.
Definition: pcm_data.h:141
wxString name
Definition: pcm_data.h:142
#define PR_CAN_ABORT