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