KiCad PCB EDA Suite
text_mod_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 <text_mod_grid_table.h>
28 #include <widgets/grid_combobox.h>
29 #include <trigo.h>
30 #include "grid_layer_box_helpers.h"
31 
32 enum
33 {
34  MYID_SELECT_FOOTPRINT = 991, // must be within GRID_TRICKS' enum range
36 };
37 
38 
39 wxArrayString g_menuOrientations;
40 
41 
43  : m_userUnits( aUserUnits ), m_frame( aFrame )
44 {
45  // Build the column attributes.
46 
47  m_readOnlyAttr = new wxGridCellAttr;
48  m_readOnlyAttr->SetReadOnly( true );
49 
50  m_boolColAttr = new wxGridCellAttr;
51  m_boolColAttr->SetRenderer( new wxGridCellBoolRenderer() );
52  m_boolColAttr->SetEditor( new wxGridCellBoolEditor() );
53  m_boolColAttr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
54 
55  if( g_menuOrientations.IsEmpty() )
56  {
57  g_menuOrientations.push_back(
59  g_menuOrientations.push_back(
60  wxT( "90 " ) + GetAbbreviatedUnitsLabel( EDA_UNITS::DEGREES ) );
61  g_menuOrientations.push_back(
62  wxT( "-90 " ) + GetAbbreviatedUnitsLabel( EDA_UNITS::DEGREES ) );
63  g_menuOrientations.push_back(
64  wxT( "180 " ) + GetAbbreviatedUnitsLabel( EDA_UNITS::DEGREES ) );
65  }
66 
67  m_orientationColAttr = new wxGridCellAttr;
69 
70  m_layerColAttr = new wxGridCellAttr;
71  m_layerColAttr->SetRenderer( new GRID_CELL_LAYER_RENDERER( m_frame ) );
72  m_layerColAttr->SetEditor( new GRID_CELL_LAYER_SELECTOR( m_frame, {} ) );
73 }
74 
75 
77 {
78  m_readOnlyAttr->DecRef();
79  m_boolColAttr->DecRef();
80  m_orientationColAttr->DecRef();
81  m_layerColAttr->DecRef();
82 }
83 
84 
86 {
87  switch( aCol )
88  {
89  case TMC_TEXT: return _( "Text Items" );
90  case TMC_SHOWN: return _( "Show" );
91  case TMC_WIDTH: return _( "Width" );
92  case TMC_HEIGHT: return _( "Height" );
93  case TMC_THICKNESS: return _( "Thickness" );
94  case TMC_ITALIC: return _( "Italic" );
95  case TMC_LAYER: return _( "Layer" );
96  case TMC_ORIENTATION: return _( "Orientation" );
97  case TMC_UPRIGHT: return _( "Keep Upright" );
98  case TMC_XOFFSET: return _( "X Offset" );
99  case TMC_YOFFSET: return _( "Y Offset" );
100  default: wxFAIL; return wxEmptyString;
101  }
102 }
103 
104 
106 {
107  switch( aRow )
108  {
109  case 0: return _( "Reference designator" );
110  case 1: return _( "Value" );
111  default: return wxEmptyString;
112  }
113 }
114 
115 
116 bool TEXT_MOD_GRID_TABLE::CanGetValueAs( int aRow, int aCol, const wxString& aTypeName )
117 {
118  switch( aCol )
119  {
120  case TMC_TEXT:
121  case TMC_WIDTH:
122  case TMC_HEIGHT:
123  case TMC_THICKNESS:
124  case TMC_ORIENTATION:
125  case TMC_XOFFSET:
126  case TMC_YOFFSET:
127  return aTypeName == wxGRID_VALUE_STRING;
128 
129  case TMC_SHOWN:
130  case TMC_ITALIC:
131  case TMC_UPRIGHT:
132  return aTypeName == wxGRID_VALUE_BOOL;
133 
134  case TMC_LAYER:
135  return aTypeName == wxGRID_VALUE_NUMBER;
136 
137  default:
138  wxFAIL;
139  return false;
140  }
141 }
142 
143 
144 bool TEXT_MOD_GRID_TABLE::CanSetValueAs( int aRow, int aCol, const wxString& aTypeName )
145 {
146  return CanGetValueAs( aRow, aCol, aTypeName );
147 }
148 
149 
150 wxGridCellAttr* TEXT_MOD_GRID_TABLE::GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind )
151 {
152  switch( aCol )
153  {
154  case TMC_TEXT:
155  case TMC_WIDTH:
156  case TMC_HEIGHT:
157  case TMC_THICKNESS:
158  case TMC_XOFFSET:
159  case TMC_YOFFSET:
160  return nullptr;
161 
162  case TMC_SHOWN:
163  case TMC_ITALIC:
164  case TMC_UPRIGHT:
165  m_boolColAttr->IncRef();
166  return m_boolColAttr;
167 
168  case TMC_LAYER:
169  m_layerColAttr->IncRef();
170  return m_layerColAttr;
171 
172  case TMC_ORIENTATION:
173  m_orientationColAttr->IncRef();
174  return m_orientationColAttr;
175 
176  default:
177  wxFAIL;
178  return nullptr;
179  }
180 }
181 
182 
183 wxString TEXT_MOD_GRID_TABLE::GetValue( int aRow, int aCol )
184 {
185  const FP_TEXT& text = this->at((size_t) aRow );
186 
187  switch( aCol )
188  {
189  case TMC_TEXT:
190  return text.GetText();
191 
192  case TMC_WIDTH:
193  return StringFromValue( m_userUnits, text.GetTextWidth() );
194 
195  case TMC_HEIGHT:
196  return StringFromValue( m_userUnits, text.GetTextHeight() );
197 
198  case TMC_THICKNESS:
199  return StringFromValue( m_userUnits, text.GetTextThickness() );
200 
201  case TMC_LAYER:
202  return text.GetLayerName();
203 
204  case TMC_ORIENTATION:
206  true );
207 
208  case TMC_XOFFSET:
209  return StringFromValue( m_userUnits, text.GetPos0().x );
210 
211  case TMC_YOFFSET:
212  return StringFromValue( m_userUnits, text.GetPos0().y );
213 
214  default:
215  // we can't assert here because wxWidgets sometimes calls this without checking
216  // the column type when trying to see if there's an overflow
217  return wxT( "bad wxWidgets!" );
218  }
219 }
220 
221 
222 bool TEXT_MOD_GRID_TABLE::GetValueAsBool( int aRow, int aCol )
223 {
224  FP_TEXT& text = this->at((size_t) aRow );
225 
226  switch( aCol )
227  {
228  case TMC_SHOWN: return text.IsVisible();
229  case TMC_ITALIC: return text.IsItalic();
230  case TMC_UPRIGHT: return text.IsKeepUpright();
231  default:
232  wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a bool value" ), aCol ) );
233  return false;
234  }
235 }
236 
237 
238 long TEXT_MOD_GRID_TABLE::GetValueAsLong( int aRow, int aCol )
239 {
240  FP_TEXT& text = this->at((size_t) aRow );
241 
242  switch( aCol )
243  {
244  case TMC_LAYER: return text.GetLayer();
245  default:
246  wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a long value" ), aCol ) );
247  return 0;
248  }
249 }
250 
251 
252 void TEXT_MOD_GRID_TABLE::SetValue( int aRow, int aCol, const wxString &aValue )
253 {
254  FP_TEXT& text = this->at((size_t) aRow );
255  wxPoint pos;
256 
257  switch( aCol )
258  {
259  case TMC_TEXT:
260  text.SetText( aValue );
261  break;
262 
263  case TMC_WIDTH:
264  text.SetTextWidth( ValueFromString( m_userUnits, aValue ) );
265  break;
266 
267  case TMC_HEIGHT:
268  text.SetTextHeight( ValueFromString( m_userUnits, aValue ) );
269  break;
270 
272  break;
273 
274  case TMC_ORIENTATION:
276  text.SetDrawCoord();
277  break;
278 
279  case TMC_XOFFSET:
280  case TMC_YOFFSET:
281  pos = text.GetPos0();
282 
283  if( aCol == TMC_XOFFSET )
284  pos.x = ValueFromString( m_userUnits, aValue );
285  else
286  pos.y = ValueFromString( m_userUnits, aValue );
287 
288  text.SetPos0( pos );
289  text.SetDrawCoord();
290  break;
291 
292  default:
293  wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a string value" ), aCol ) );
294  break;
295  }
296 
297  GetView()->Refresh();
298 }
299 
300 
301 void TEXT_MOD_GRID_TABLE::SetValueAsBool( int aRow, int aCol, bool aValue )
302 {
303  FP_TEXT& text = this->at((size_t) aRow );
304 
305  switch( aCol )
306  {
307  case TMC_SHOWN:
308  text.SetVisible( aValue );
309  break;
310 
311  case TMC_ITALIC:
312  text.SetItalic( aValue );
313  break;
314 
315  case TMC_UPRIGHT:text.SetKeepUpright( aValue );
316  break;
317 
318  default:
319  wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a bool value" ), aCol ) );
320  break;
321  }
322 }
323 
324 
325 void TEXT_MOD_GRID_TABLE::SetValueAsLong( int aRow, int aCol, long aValue )
326 {
327  FP_TEXT& text = this->at((size_t) aRow );
328 
329  switch( aCol )
330  {
331  case TMC_LAYER:
332  text.SetLayer( ToLAYER_ID( (int) aValue ) );
333  text.SetMirrored( IsBackLayer( text.GetLayer() ) );
334  break;
335 
336  default:
337  wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a long value" ), aCol ) );
338  break;
339  }
340 }
341 
void SetMirrored(bool isMirrored)
Definition: eda_text.h:194
long GetValueAsLong(int aRow, int aCol) override
wxGridCellAttr * GetAttr(int row, int col, wxGridCellAttr::wxAttrKind kind) override
wxGridCellAttr * m_boolColAttr
virtual void SetLayer(PCB_LAYER_ID aLayer)
Function SetLayer sets the layer this item is on.
wxString GetRowLabelValue(int aRow) override
bool IsBackLayer(PCB_LAYER_ID aLayerId)
Layer classification: check if it's a back layer.
bool IsVisible() const
Definition: eda_text.h:192
void SetTextAngle(double aAngle) override
Definition: fp_text.cpp:68
void SetItalic(bool isItalic)
Definition: eda_text.h:185
void SetVisible(bool aVisible)
Definition: eda_text.h:191
bool GetValueAsBool(int aRow, int aCol) override
double GetTextAngle() const
Definition: eda_text.h:180
bool CanGetValueAs(int aRow, int aCol, const wxString &aTypeName) override
void SetDrawCoord()
Set absolute coordinates.
Definition: fp_text.cpp:198
wxString GetAbbreviatedUnitsLabel(EDA_UNITS aUnit, EDA_DATA_TYPE aType)
Get the units string for a given units type.
Definition: base_units.cpp:467
int GetTextThickness() const
Definition: eda_text.h:165
int GetTextHeight() const
Definition: eda_text.h:251
bool IsItalic() const
Definition: eda_text.h:186
bool CanSetValueAs(int aRow, int aCol, const wxString &aTypeName) override
wxGridCellAttr * m_orientationColAttr
wxString GetValue(int aRow, int aCol) override
virtual void SetText(const wxString &aText)
Definition: eda_text.cpp:120
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:444
wxGridCellAttr * m_readOnlyAttr
PCB_BASE_FRAME * m_frame
wxArrayString g_menuOrientations
wxGridCellAttr * m_layerColAttr
void SetPos0(const wxPoint &aPos)
Definition: fp_text.h:169
const wxPoint & GetPos0() const
Definition: fp_text.h:170
void SetTextWidth(int aWidth)
Definition: eda_text.h:247
EDA_UNITS
Definition: eda_units.h:38
void SetKeepUpright(bool aKeepUpright)
Definition: fp_text.h:117
void SetValueAsLong(int aRow, int aCol, long aValue) override
int GetTextWidth() const
Definition: eda_text.h:248
TEXT_MOD_GRID_TABLE(EDA_UNITS userUnits, PCB_BASE_FRAME *aFrame)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
void SetValue(int aRow, int aCol, const wxString &aValue) override
#define _(s)
Definition: 3d_actions.cpp:33
void SetValueAsBool(int aRow, int aCol, bool aValue) override
T NormalizeAnglePos(T Angle)
Normalize angle to be in the 0.0 .
Definition: trigo.h:268
void SetTextHeight(int aHeight)
Definition: eda_text.h:250
wxString GetColLabelValue(int aCol) override
wxString StringFromValue(EDA_UNITS aUnits, double aValue, bool aAddUnitSymbol, EDA_DATA_TYPE aType)
Function StringFromValue returns the string from aValue according to units (inch, mm ....
Definition: base_units.cpp:220
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.h:164
double DoubleValueFromString(EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType)
Function DoubleValueFromString converts aTextValue to a double.
Definition: base_units.cpp:333
bool IsKeepUpright()
Definition: fp_text.h:112
wxString GetLayerName() const
Function GetLayerName returns the name of the PCB layer on which the item resides.
virtual PCB_LAYER_ID GetLayer() const
Function GetLayer returns the primary layer this item is on.
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition: eda_text.h:133
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition: lset.cpp:897
PCB_BASE_FRAME basic PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.