KiCad PCB EDA Suite
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-2021 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"
27
28
29#define GRID_CELL_MARGIN 4
30
31
33 wxWindow* parent, std::shared_ptr<PLUGIN_CONTENT_MANAGER> pcm ) :
35 m_pcm( pcm )
36{
41
42 // For aesthetic reasons, we must set the size of m_buttonAdd to match the other bitmaps
43 // manually (for instance m_buttonRemove)
44 Layout(); // Needed at least on MSW to compute the actual buttons sizes, after initializing
45 // their bitmaps
48
49 m_buttonAdd->Bind( wxEVT_BUTTON, &DIALOG_MANAGE_REPOSITORIES::OnAdd, this );
50
51 wxMenu* addMenu = m_buttonAdd->GetSplitButtonMenu();
52 wxMenuItem* menuItem = addMenu->Append( wxID_ANY, _( "Add Default Repository" ) );
53
54 addMenu->Bind( wxEVT_COMMAND_MENU_SELECTED, &DIALOG_MANAGE_REPOSITORIES::OnAddDefault, this,
55 menuItem->GetId() );
56
57 m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
58 {
59 OnAdd( aEvent );
60 } ) );
61
62 for( int col = 0; col < m_grid->GetNumberCols(); col++ )
63 {
64 const wxString& heading = m_grid->GetColLabelValue( col );
65 int headingWidth = GetTextExtent( heading ).x + 2 * GRID_CELL_MARGIN;
66
67 // Set the minimal width to the column label size.
68 m_grid->SetColMinimalWidth( col, headingWidth );
69 }
70
71 // fix sizers now widgets are set.
73}
74
75
77{
78 // Delete the GRID_TRICKS.
79 m_grid->PopEventHandler( true );
80}
81
82
84{
85 for( int col = 0; col < m_grid->GetNumberCols(); col++ )
86 {
87 // Set the width to see the full contents
88 m_grid->SetColSize( col, m_grid->GetVisibleWidth( col, true, true ) );
89 }
90}
91
92
93void DIALOG_MANAGE_REPOSITORIES::OnAdd( wxCommandEvent& event )
94{
95 wxTextEntryDialog entry_dialog( this, _( "Please enter fully qualified repository url" ),
96 _( "Add repository" ) );
97
98 if( entry_dialog.ShowModal() == wxID_OK )
99 {
100 wxString url = entry_dialog.GetValue();
101 addRepository( url );
102 }
103}
104
105
106int DIALOG_MANAGE_REPOSITORIES::findRow( int aCol, const wxString& aVal )
107{
108 for( int row = 0; row < m_grid->GetNumberRows(); row++ )
109 {
110 if( m_grid->GetCellValue( row, aCol ) == aVal )
111 return row;
112 }
113
114 return -1;
115}
116
117
119{
120 int matching_row;
121
122 if( ( matching_row = findRow( 1, aUrl ) ) >= 0 )
123 {
124 selectRow( matching_row );
125 return;
126 }
127
128 PCM_REPOSITORY repository;
129 WX_PROGRESS_REPORTER reporter( GetParent(), wxT( "" ), 1 );
130
131 if( m_pcm->FetchRepository( aUrl, repository, &reporter ) )
132 {
133 wxString name = repository.name;
134 int increment = 1;
135
136 while( findRow( 0, name ) >= 0 )
137 name = wxString::Format( "%s (%d)", repository.name, increment++ );
138
139 m_grid->Freeze();
140
141 m_grid->AppendRows();
142 int row = m_grid->GetNumberRows() - 1;
143
144 m_grid->SetCellValue( row, 0, name );
145 m_grid->SetCellValue( row, 1, aUrl );
146
148 m_grid->Thaw();
149
150 selectRow( row );
151 }
152}
153
154
155void DIALOG_MANAGE_REPOSITORIES::OnAddDefault( wxCommandEvent& event )
156{
158}
159
160
162{
163 auto selectedRows = m_grid->GetSelectedRows();
164
165 // If nothing is selected or multiple rows are selected don't do anything.
166 if( selectedRows.size() != 1 )
167 {
168 wxBell();
169 return;
170 }
171
172 int selectedRow = selectedRows[0];
173 m_grid->DeleteRows( selectedRow );
175
176 if( m_grid->GetNumberRows() > 0 )
177 m_grid->SelectRow( selectedRow == m_grid->GetNumberRows() ? selectedRow - 1 : selectedRow );
178}
179
180
182{
183 auto selectedRows = m_grid->GetSelectedRows();
184
185 // If nothing is selected or multiple rows are selected don't do anything.
186 if( selectedRows.size() != 1 )
187 return;
188
189 int selectedRow = selectedRows[0];
190
191 // If first row is selected, then it can't go any further up.
192 if( selectedRow == 0 )
193 {
194 wxBell();
195 return;
196 }
197
198 swapRows( selectedRow, selectedRow - 1 );
199
200 selectRow( selectedRow - 1 );
201}
202
203
205{
206 auto selectedRows = m_grid->GetSelectedRows();
207
208 // If nothing is selected or multiple rows are selected don't do anything.
209 if( selectedRows.size() != 1 )
210 return;
211
212 int selectedRow = selectedRows[0];
213
214 // If last row is selected, then it can't go any further down.
215 if( selectedRow + 1 == m_grid->GetNumberRows() )
216 {
217 wxBell();
218 return;
219 }
220
221 swapRows( selectedRow, selectedRow + 1 );
222
223 selectRow( selectedRow + 1 );
224}
225
226
227void DIALOG_MANAGE_REPOSITORIES::swapRows( int aRowA, int aRowB )
228{
229 m_grid->Freeze();
230
231 wxString tempStr;
232
233 for( int column = 0; column < m_grid->GetNumberCols(); column++ )
234 {
235 tempStr = m_grid->GetCellValue( aRowA, column );
236 m_grid->SetCellValue( aRowA, column, m_grid->GetCellValue( aRowB, column ) );
237 m_grid->SetCellValue( aRowB, column, tempStr );
238 }
239
240 m_grid->Thaw();
241}
242
243
245{
246 selectRow( event.GetRow() );
247}
248
249
251{
252 m_grid->ClearSelection();
253 m_grid->SelectRow( aRow );
254}
255
256
257void DIALOG_MANAGE_REPOSITORIES::SetData( const std::vector<std::pair<wxString, wxString>>& aData )
258{
259 m_grid->Freeze();
260
261 m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
262 m_grid->AppendRows( aData.size() );
263
264 for( size_t i = 0; i < aData.size(); i++ )
265 {
266 m_grid->SetCellValue( i, 0, aData[i].first );
267 m_grid->SetCellValue( i, 1, aData[i].second );
268 }
269
271
272 m_grid->Thaw();
273}
274
275
276std::vector<std::pair<wxString, wxString>> DIALOG_MANAGE_REPOSITORIES::GetData()
277{
278 std::vector<std::pair<wxString, wxString>> result;
279
280 for( int i = 0; i < m_grid->GetNumberRows(); i++ )
281 {
282 result.push_back(
283 std::make_pair( m_grid->GetCellValue( i, 0 ), m_grid->GetCellValue( i, 1 ) ) );
284 }
285
286 return result;
287}
288
289
290void DIALOG_MANAGE_REPOSITORIES::OnSaveClicked( wxCommandEvent& event )
291{
292 EndModal( wxID_SAVE );
293}
const char * name
Definition: DXF_plotter.cpp:56
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:105
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 SetWidthPadding(int aPadding)
wxMenu * GetSplitButtonMenu()
void SetMinSize(const wxSize &aSize) override
void SetBitmap(const wxBitmap &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:282
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
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
Package installation entry.
Definition: pcm_data.h:122
wxString name
Definition: pcm_data.h:123