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 
22 #include "bitmaps/bitmap_types.h"
23 #include "bitmaps/bitmaps_list.h"
24 #include "grid_tricks.h"
25 #include "widgets/wx_grid.h"
26 
27 
28 #define GRID_CELL_MARGIN 4
29 
30 
32  wxWindow* parent, std::shared_ptr<PLUGIN_CONTENT_MANAGER> pcm ) :
34  m_pcm( pcm )
35 {
36  m_buttonAdd->SetBitmap( KiBitmap( BITMAPS::small_plus ) );
38  m_buttonMoveUp->SetBitmap( KiBitmap( BITMAPS::small_up ) );
40 
41  m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
42 
43  for( int col = 0; col < m_grid->GetNumberCols(); col++ )
44  {
45  const wxString& heading = m_grid->GetColLabelValue( col );
46  int headingWidth = GetTextExtent( heading ).x + 2 * GRID_CELL_MARGIN;
47 
48  // Set the minimal width to the column label size.
49  m_grid->SetColMinimalWidth( col, headingWidth );
50  }
51 }
52 
53 
55 {
56  for( int col = 0; col < m_grid->GetNumberCols(); col++ )
57  {
58  // Set the width to see the full contents
59  m_grid->SetColSize( col, m_grid->GetVisibleWidth( col, true, true, false ) );
60  }
61 }
62 
63 
65 {
66  wxTextEntryDialog* entry_dialog = new wxTextEntryDialog(
67  this, _( "Please enter fully qualified repository url" ), _( "Add repository" ) );
68 
69  if( entry_dialog->ShowModal() == wxID_OK )
70  {
71  PCM_REPOSITORY repository;
72  wxString url = entry_dialog->GetValue();
73 
74  const auto find_row = [&]( const int col, const wxString& val )
75  {
76  for( int row = 0; row < m_grid->GetNumberRows(); row++ )
77  {
78  if( m_grid->GetCellValue( row, col ) == val )
79  return row;
80  }
81 
82  return -1;
83  };
84 
85  int matching_row;
86 
87  if( ( matching_row = find_row( 1, url ) ) >= 0 )
88  {
89  selectRow( matching_row );
90  }
91  else
92  {
93  if( m_pcm->FetchRepository( url, repository ) )
94  {
95  wxString name = repository.name;
96  int increment = 1;
97 
98  while( find_row( 0, name ) >= 0 )
99  name = wxString::Format( "%s (%d)", repository.name, increment++ );
100 
101  m_grid->Freeze();
102 
103  m_grid->AppendRows();
104  int row = m_grid->GetNumberRows() - 1;
105 
106  m_grid->SetCellValue( row, 0, name );
107  m_grid->SetCellValue( row, 1, url );
108 
109  setColumnWidths();
110  m_grid->Thaw();
111 
112  selectRow( row );
113  }
114  }
115  }
116 
117  entry_dialog->Destroy();
118 }
119 
120 
122 {
123  auto selectedRows = m_grid->GetSelectedRows();
124 
125  // If nothing is selected or multiple rows are selected don't do anything.
126  if( selectedRows.size() != 1 )
127  {
128  wxBell();
129  return;
130  }
131 
132  int selectedRow = selectedRows[0];
133  m_grid->DeleteRows( selectedRow );
134  setColumnWidths();
135 
136  if( m_grid->GetNumberRows() > 0 )
137  m_grid->SelectRow( selectedRow == m_grid->GetNumberRows() ? selectedRow - 1 : selectedRow );
138 }
139 
140 
142 {
143  auto selectedRows = m_grid->GetSelectedRows();
144 
145  // If nothing is selected or multiple rows are selected don't do anything.
146  if( selectedRows.size() != 1 )
147  return;
148 
149  int selectedRow = selectedRows[0];
150 
151  // If first row is selected, then it can't go any further up.
152  if( selectedRow == 0 )
153  {
154  wxBell();
155  return;
156  }
157 
158  swapRows( selectedRow, selectedRow - 1 );
159 
160  selectRow( selectedRow - 1 );
161 }
162 
163 
165 {
166  auto selectedRows = m_grid->GetSelectedRows();
167 
168  // If nothing is selected or multiple rows are selected don't do anything.
169  if( selectedRows.size() != 1 )
170  return;
171 
172  int selectedRow = selectedRows[0];
173 
174  // If last row is selected, then it can't go any further down.
175  if( selectedRow + 1 == m_grid->GetNumberRows() )
176  {
177  wxBell();
178  return;
179  }
180 
181  swapRows( selectedRow, selectedRow + 1 );
182 
183  selectRow( selectedRow + 1 );
184 }
185 
186 
187 void DIALOG_MANAGE_REPOSITORIES::swapRows( int aRowA, int aRowB )
188 {
189  m_grid->Freeze();
190 
191  wxString tempStr;
192 
193  for( int column = 0; column < m_grid->GetNumberCols(); column++ )
194  {
195  tempStr = m_grid->GetCellValue( aRowA, column );
196  m_grid->SetCellValue( aRowA, column, m_grid->GetCellValue( aRowB, column ) );
197  m_grid->SetCellValue( aRowB, column, tempStr );
198  }
199 
200  m_grid->Thaw();
201 }
202 
203 
205 {
206  selectRow( event.GetRow() );
207 }
208 
209 
211 {
212  m_grid->ClearSelection();
213  m_grid->SelectRow( aRow );
214 }
215 
216 
218 {
219  m_grid->Freeze();
220 
221  m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
222  m_grid->AppendRows( aData.size() );
223 
224  for( size_t i = 0; i < aData.size(); i++ )
225  {
226  m_grid->SetCellValue( i, 0, aData[i].first );
227  m_grid->SetCellValue( i, 1, aData[i].second );
228  }
229 
230  setColumnWidths();
231 
232  m_grid->Thaw();
233 }
234 
235 
237 {
238  STRING_PAIR_LIST result;
239 
240  for( int i = 0; i < m_grid->GetNumberRows(); i++ )
241  {
242  result.push_back(
243  std::make_pair( m_grid->GetCellValue( i, 0 ), m_grid->GetCellValue( i, 1 ) ) );
244  }
245 
246  return result;
247 }
248 
249 
250 void DIALOG_MANAGE_REPOSITORIES::OnSaveClicked( wxCommandEvent& event )
251 {
252  EndModal( wxID_SAVE );
253 }
void OnMoveUpButtonClicked(wxCommandEvent &event) override
#define GRID_CELL_MARGIN
void SetData(const STRING_PAIR_LIST &aData)
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:55
void swapRows(int aRowA, int aRowB)
std::shared_ptr< PLUGIN_CONTENT_MANAGER > m_pcm
void OnMoveDownButtonClicked(wxCommandEvent &event) override
int GetVisibleWidth(int aCol, bool aHeader=true, bool aContents=false, bool aKeep=true)
Calculates the specified column based on the actual size of the text on screen.
Definition: wx_grid.cpp:246
wxString name
Definition: pcm_data.h:121
void OnSaveClicked(wxCommandEvent &event) override
Class DIALOG_MANAGE_REPOSITORIES_BASE.
void OnGridCellClicked(wxGridEvent &event) override
DIALOG_MANAGE_REPOSITORIES(wxWindow *parent, std::shared_ptr< PLUGIN_CONTENT_MANAGER > aPcm)
Constructor.
#define _(s)
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
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
void OnAddButtonClicked(wxCommandEvent &event) override
std::vector< std::pair< wxString, wxString > > STRING_PAIR_LIST
Definition: pcm.h:69
const char * name
Definition: DXF_plotter.cpp:56
void OnRemoveButtonClicked(wxCommandEvent &event) override
Package installation entry.
Definition: pcm_data.h:119