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