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 (C) 1992-2022 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, _( "Please enter 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 );
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{
164 auto selectedRows = m_grid->GetSelectedRows();
165
166 // If nothing is selected or multiple rows are selected don't do anything.
167 if( selectedRows.size() != 1 )
168 {
169 wxBell();
170 return;
171 }
172
173 int selectedRow = selectedRows[0];
174 m_grid->DeleteRows( selectedRow );
176
177 if( m_grid->GetNumberRows() > 0 )
178 m_grid->SelectRow( selectedRow == m_grid->GetNumberRows() ? selectedRow - 1 : selectedRow );
179}
180
181
183{
184 auto selectedRows = m_grid->GetSelectedRows();
185
186 // If nothing is selected or multiple rows are selected don't do anything.
187 if( selectedRows.size() != 1 )
188 return;
189
190 int selectedRow = selectedRows[0];
191
192 // If first row is selected, then it can't go any further up.
193 if( selectedRow == 0 )
194 {
195 wxBell();
196 return;
197 }
198
199 swapRows( selectedRow, selectedRow - 1 );
200
201 selectRow( selectedRow - 1 );
202}
203
204
206{
207 auto selectedRows = m_grid->GetSelectedRows();
208
209 // If nothing is selected or multiple rows are selected don't do anything.
210 if( selectedRows.size() != 1 )
211 return;
212
213 int selectedRow = selectedRows[0];
214
215 // If last row is selected, then it can't go any further down.
216 if( selectedRow + 1 == m_grid->GetNumberRows() )
217 {
218 wxBell();
219 return;
220 }
221
222 swapRows( selectedRow, selectedRow + 1 );
223
224 selectRow( selectedRow + 1 );
225}
226
227
228void DIALOG_MANAGE_REPOSITORIES::swapRows( int aRowA, int aRowB )
229{
230 m_grid->Freeze();
231
232 wxString tempStr;
233
234 for( int column = 0; column < m_grid->GetNumberCols(); column++ )
235 {
236 tempStr = m_grid->GetCellValue( aRowA, column );
237 m_grid->SetCellValue( aRowA, column, m_grid->GetCellValue( aRowB, column ) );
238 m_grid->SetCellValue( aRowB, column, tempStr );
239 }
240
241 m_grid->Thaw();
242}
243
244
246{
247 selectRow( event.GetRow() );
248}
249
250
252{
253 m_grid->ClearSelection();
254 m_grid->SelectRow( aRow );
255}
256
257
258void DIALOG_MANAGE_REPOSITORIES::SetData( const std::vector<std::pair<wxString, wxString>>& aData )
259{
260 m_grid->Freeze();
261
262 m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
263 m_grid->AppendRows( aData.size() );
264
265 for( size_t i = 0; i < aData.size(); i++ )
266 {
267 m_grid->SetCellValue( i, 0, aData[i].first );
268 m_grid->SetCellValue( i, 1, aData[i].second );
269 }
270
272
273 m_grid->Thaw();
274}
275
276
277std::vector<std::pair<wxString, wxString>> DIALOG_MANAGE_REPOSITORIES::GetData()
278{
279 std::vector<std::pair<wxString, wxString>> result;
280
281 for( int i = 0; i < m_grid->GetNumberRows(); i++ )
282 {
283 result.push_back(
284 std::make_pair( m_grid->GetCellValue( i, 0 ), m_grid->GetCellValue( i, 1 ) ) );
285 }
286
287 return result;
288}
289
290
291void DIALOG_MANAGE_REPOSITORIES::OnSaveClicked( wxCommandEvent& event )
292{
293 EndModal( wxID_SAVE );
294}
const char * name
Definition: DXF_plotter.cpp:57
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
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 swapRows(int aRowA, int aRowB)
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)
Calculates the specified column based on the actual size of the text on screen.
Definition: wx_grid.cpp:769
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:133
wxString name
Definition: pcm_data.h:134