KiCad PCB EDA Suite
fp_text_grid_table.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) 2018 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include <kiway.h>
25 #include <kiway_player.h>
26 #include <fp_text_grid_table.h>
28 #include <widgets/grid_combobox.h>
29 #include <trigo.h>
30 #include <pcb_base_frame.h>
31 #include "grid_layer_box_helpers.h"
32 
33 enum
34 {
35  MYID_SELECT_FOOTPRINT = 991, // must be within GRID_TRICKS' enum range
37 };
38 
39 
40 wxArrayString g_menuOrientations;
41 
42 
44  m_frame( aFrame )
45 {
46  // Build the column attributes.
47 
48  m_readOnlyAttr = new wxGridCellAttr;
49  m_readOnlyAttr->SetReadOnly( true );
50 
51  m_boolColAttr = new wxGridCellAttr;
52  m_boolColAttr->SetRenderer( new wxGridCellBoolRenderer() );
53  m_boolColAttr->SetEditor( new wxGridCellBoolEditor() );
54  m_boolColAttr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
55 
56  if( g_menuOrientations.IsEmpty() )
57  {
58  g_menuOrientations.push_back(
60  g_menuOrientations.push_back(
61  wxT( "90 " ) + GetAbbreviatedUnitsLabel( EDA_UNITS::DEGREES ) );
62  g_menuOrientations.push_back(
63  wxT( "-90 " ) + GetAbbreviatedUnitsLabel( EDA_UNITS::DEGREES ) );
64  g_menuOrientations.push_back(
65  wxT( "180 " ) + GetAbbreviatedUnitsLabel( EDA_UNITS::DEGREES ) );
66  }
67 
68  m_orientationColAttr = new wxGridCellAttr;
70 
71  m_layerColAttr = new wxGridCellAttr;
72  m_layerColAttr->SetRenderer( new GRID_CELL_LAYER_RENDERER( m_frame ) );
73  m_layerColAttr->SetEditor( new GRID_CELL_LAYER_SELECTOR( m_frame, {} ) );
74 
75  m_frame->Bind( UNITS_CHANGED, &FP_TEXT_GRID_TABLE::onUnitsChanged, this );
76 }
77 
78 
80 {
81  m_readOnlyAttr->DecRef();
82  m_boolColAttr->DecRef();
83  m_orientationColAttr->DecRef();
84  m_layerColAttr->DecRef();
85 
86  m_frame->Unbind( UNITS_CHANGED, &FP_TEXT_GRID_TABLE::onUnitsChanged, this );
87 }
88 
89 
90 void FP_TEXT_GRID_TABLE::onUnitsChanged( wxCommandEvent& aEvent )
91 {
92  if( GetView() )
93  GetView()->ForceRefresh();
94 
95  aEvent.Skip();
96 }
97 
98 
100 {
101  switch( aCol )
102  {
103  case FPT_TEXT: return _( "Text Items" );
104  case FPT_SHOWN: return _( "Show" );
105  case FPT_WIDTH: return _( "Width" );
106  case FPT_HEIGHT: return _( "Height" );
107  case FPT_THICKNESS: return _( "Thickness" );
108  case FPT_ITALIC: return _( "Italic" );
109  case FPT_LAYER: return _( "Layer" );
110  case FPT_ORIENTATION: return _( "Orientation" );
111  case FPT_UPRIGHT: return _( "Keep Upright" );
112  case FPT_XOFFSET: return _( "X Offset" );
113  case FPT_YOFFSET: return _( "Y Offset" );
114  default: wxFAIL; return wxEmptyString;
115  }
116 }
117 
118 
120 {
121  switch( aRow )
122  {
123  case 0: return _( "Reference designator" );
124  case 1: return _( "Value" );
125  default: return wxEmptyString;
126  }
127 }
128 
129 
130 bool FP_TEXT_GRID_TABLE::CanGetValueAs( int aRow, int aCol, const wxString& aTypeName )
131 {
132  switch( aCol )
133  {
134  case FPT_TEXT:
135  case FPT_WIDTH:
136  case FPT_HEIGHT:
137  case FPT_THICKNESS:
138  case FPT_ORIENTATION:
139  case FPT_XOFFSET:
140  case FPT_YOFFSET:
141  return aTypeName == wxGRID_VALUE_STRING;
142 
143  case FPT_SHOWN:
144  case FPT_ITALIC:
145  case FPT_UPRIGHT:
146  return aTypeName == wxGRID_VALUE_BOOL;
147 
148  case FPT_LAYER:
149  return aTypeName == wxGRID_VALUE_NUMBER;
150 
151  default:
152  wxFAIL;
153  return false;
154  }
155 }
156 
157 
158 bool FP_TEXT_GRID_TABLE::CanSetValueAs( int aRow, int aCol, const wxString& aTypeName )
159 {
160  return CanGetValueAs( aRow, aCol, aTypeName );
161 }
162 
163 
164 wxGridCellAttr* FP_TEXT_GRID_TABLE::GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind )
165 {
166  switch( aCol )
167  {
168  case FPT_TEXT:
169  case FPT_WIDTH:
170  case FPT_HEIGHT:
171  case FPT_THICKNESS:
172  case FPT_XOFFSET:
173  case FPT_YOFFSET:
174  return nullptr;
175 
176  case FPT_SHOWN:
177  case FPT_ITALIC:
178  case FPT_UPRIGHT:
179  m_boolColAttr->IncRef();
180  return m_boolColAttr;
181 
182  case FPT_LAYER:
183  m_layerColAttr->IncRef();
184  return m_layerColAttr;
185 
186  case FPT_ORIENTATION:
187  m_orientationColAttr->IncRef();
188  return m_orientationColAttr;
189 
190  default:
191  wxFAIL;
192  return nullptr;
193  }
194 }
195 
196 
197 wxString FP_TEXT_GRID_TABLE::GetValue( int aRow, int aCol )
198 {
199  const FP_TEXT& text = this->at((size_t) aRow );
200 
201  switch( aCol )
202  {
203  case FPT_TEXT:
204  return text.GetText();
205 
206  case FPT_WIDTH:
207  return StringFromValue( m_frame->GetUserUnits(), text.GetTextWidth(), true );
208 
209  case FPT_HEIGHT:
210  return StringFromValue( m_frame->GetUserUnits(), text.GetTextHeight(), true );
211 
212  case FPT_THICKNESS:
213  return StringFromValue( m_frame->GetUserUnits(), text.GetTextThickness(), true );
214 
215  case FPT_LAYER:
216  return text.GetLayerName();
217 
218  case FPT_ORIENTATION:
219  return StringFromValue( EDA_UNITS::DEGREES, (int) NormalizeAnglePos( text.GetTextAngle() ),
220  true );
221 
222  case FPT_XOFFSET:
223  return StringFromValue( m_frame->GetUserUnits(), text.GetPos0().x, true );
224 
225  case FPT_YOFFSET:
226  return StringFromValue( m_frame->GetUserUnits(), text.GetPos0().y, true );
227 
228  default:
229  // we can't assert here because wxWidgets sometimes calls this without checking
230  // the column type when trying to see if there's an overflow
231  return wxT( "bad wxWidgets!" );
232  }
233 }
234 
235 
236 bool FP_TEXT_GRID_TABLE::GetValueAsBool( int aRow, int aCol )
237 {
238  FP_TEXT& text = this->at((size_t) aRow );
239 
240  switch( aCol )
241  {
242  case FPT_SHOWN: return text.IsVisible();
243  case FPT_ITALIC: return text.IsItalic();
244  case FPT_UPRIGHT: return text.IsKeepUpright();
245  default:
246  wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a bool value" ), aCol ) );
247  return false;
248  }
249 }
250 
251 
252 long FP_TEXT_GRID_TABLE::GetValueAsLong( int aRow, int aCol )
253 {
254  FP_TEXT& text = this->at((size_t) aRow );
255 
256  switch( aCol )
257  {
258  case FPT_LAYER: return text.GetLayer();
259  default:
260  wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a long value" ), aCol ) );
261  return 0;
262  }
263 }
264 
265 
266 void FP_TEXT_GRID_TABLE::SetValue( int aRow, int aCol, const wxString &aValue )
267 {
268  FP_TEXT& text = this->at((size_t) aRow );
269  wxPoint pos;
270 
271  switch( aCol )
272  {
273  case FPT_TEXT:
274  text.SetText( aValue );
275  break;
276 
277  case FPT_WIDTH:
278  text.SetTextWidth( ValueFromString( m_frame->GetUserUnits(), aValue ) );
279  break;
280 
281  case FPT_HEIGHT:
282  text.SetTextHeight( ValueFromString( m_frame->GetUserUnits(), aValue ) );
283  break;
284 
285  case FPT_THICKNESS:text.SetTextThickness( ValueFromString( m_frame->GetUserUnits(), aValue ) );
286  break;
287 
288  case FPT_ORIENTATION:
289  text.SetTextAngle( DoubleValueFromString( EDA_UNITS::DEGREES, aValue ) );
290  text.SetDrawCoord();
291  break;
292 
293  case FPT_XOFFSET:
294  case FPT_YOFFSET:
295  pos = text.GetPos0();
296 
297  if( aCol == FPT_XOFFSET )
298  pos.x = ValueFromString( m_frame->GetUserUnits(), aValue );
299  else
300  pos.y = ValueFromString( m_frame->GetUserUnits(), aValue );
301 
302  text.SetPos0( pos );
303  text.SetDrawCoord();
304  break;
305 
306  default:
307  wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a string value" ), aCol ) );
308  break;
309  }
310 
311  GetView()->Refresh();
312 }
313 
314 
315 void FP_TEXT_GRID_TABLE::SetValueAsBool( int aRow, int aCol, bool aValue )
316 {
317  FP_TEXT& text = this->at((size_t) aRow );
318 
319  switch( aCol )
320  {
321  case FPT_SHOWN:
322  text.SetVisible( aValue );
323  break;
324 
325  case FPT_ITALIC:
326  text.SetItalic( aValue );
327  break;
328 
329  case FPT_UPRIGHT:text.SetKeepUpright( aValue );
330  break;
331 
332  default:
333  wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a bool value" ), aCol ) );
334  break;
335  }
336 }
337 
338 
339 void FP_TEXT_GRID_TABLE::SetValueAsLong( int aRow, int aCol, long aValue )
340 {
341  FP_TEXT& text = this->at((size_t) aRow );
342 
343  switch( aCol )
344  {
345  case FPT_LAYER:
346  text.SetLayer( ToLAYER_ID( (int) aValue ) );
347  text.SetMirrored( IsBackLayer( text.GetLayer() ) );
348  break;
349 
350  default:
351  wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a long value" ), aCol ) );
352  break;
353  }
354 }
355 
wxGridCellAttr * m_orientationColAttr
void SetValue(int aRow, int aCol, const wxString &aValue) override
void SetValueAsLong(int aRow, int aCol, long aValue) override
wxString GetValue(int aRow, int aCol) override
wxGridCellAttr * m_layerColAttr
wxString GetAbbreviatedUnitsLabel(EDA_UNITS aUnit, EDA_DATA_TYPE aType)
Get the units string for a given units type.
Definition: base_units.cpp:424
wxGridCellAttr * m_readOnlyAttr
Classes used in Pcbnew, CvPcb and GerbView.
bool GetValueAsBool(int aRow, int aCol) override
bool IsBackLayer(PCB_LAYER_ID aLayerId)
Layer classification: check if it's a back layer.
Definition: layer_ids.h:907
long long int ValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType)
Function ValueFromString converts aTextValue in aUnits to internal units used by the application.
Definition: base_units.cpp:416
wxString GetRowLabelValue(int aRow) override
void onUnitsChanged(wxCommandEvent &aEvent)
#define _(s)
wxGridCellAttr * m_boolColAttr
void SetValueAsBool(int aRow, int aCol, bool aValue) override
PCB_BASE_FRAME * m_frame
wxGridCellAttr * GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) override
wxString GetColLabelValue(int aCol) override
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
long GetValueAsLong(int aRow, int aCol) override
T NormalizeAnglePos(T Angle)
Normalize angle to be in the 0.0 .. 360.0 range: angle is in 1/10 degrees.
Definition: trigo.h:281
FP_TEXT_GRID_TABLE(PCB_BASE_FRAME *aFrame)
wxString StringFromValue(EDA_UNITS aUnits, double aValue, bool aAddUnitSymbol, EDA_DATA_TYPE aType)
Convert a value to a string using double notation.
Definition: base_units.cpp:204
bool CanSetValueAs(int aRow, int aCol, const wxString &aTypeName) override
double DoubleValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:307
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:914
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
wxArrayString g_menuOrientations
bool CanGetValueAs(int aRow, int aCol, const wxString &aTypeName) override
EDA_UNITS GetUserUnits() const
Return the user units currently in use.