KiCad PCB EDA Suite
dialog_dimension_properties.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) 2020 Jon Evans <jon@craftyjon.com>
5  * Copyright (C) 2020 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 
21 #include <board.h>
22 #include <dimension.h>
23 #include <pcb_base_edit_frame.h>
24 #include <pcb_layer_box_selector.h>
25 #include <widgets/unit_binder.h>
26 
28 
29 
30 
32  BOARD_ITEM* aItem ) :
34  m_frame( aParent ),
35  m_cbLayerActual( m_cbLayer ),
36  m_txtValueActual( m_txtValue ),
37  m_textWidth( aParent, m_lblTextWidth, m_txtTextWidth, m_lblTextWidthUnits ),
38  m_textHeight( aParent, m_lblTextHeight, m_txtTextHeight, m_lblTextHeightUnits ),
39  m_textThickness( aParent, m_lblTextThickness, m_txtTextThickness, m_lblTextThicknessUnits ),
40  m_textPosX( aParent, m_lblTextPosX, m_txtTextPosX, m_lblTextPosXUnits ),
41  m_textPosY( aParent, m_lblTextPosY, m_txtTextPosY, m_lblTextPosYUnits ),
42  m_orientValidator( 1, &m_orientValue ),
43  m_lineThickness( aParent, m_lblLineThickness, m_txtLineThickness, m_lblLineThicknessUnits ),
44  m_arrowLength( aParent, m_lblArrowLength, m_txtArrowLength, m_lblArrowLengthUnits ),
45  m_extensionOffset( aParent, m_lblExtensionOffset, m_txtExtensionOffset, m_lblExtensionOffsetUnits )
46 {
47  wxASSERT( BaseType( aItem->Type() ) == PCB_DIMENSION_T );
48  m_dimension = static_cast<DIMENSION_BASE*>( aItem );
49  m_previewDimension = static_cast<DIMENSION_BASE*>( m_dimension->Clone() );
51 
52  switch( m_dimension->Type() )
53  {
54  case PCB_DIM_LEADER_T:
55  // Hide the main format controls and keep the leader controls shown
56  m_sizerFormat->GetStaticBox()->Hide();
57  m_sizerCenter->GetStaticBox()->Hide();
58 
61 
62  // Remove a fewings from text format
63  m_lblTextPositionMode->Hide();
64  m_cbTextPositionMode->Hide();
65  break;
66 
67  case PCB_DIM_CENTER_T:
68  m_sizerLeader->GetStaticBox()->Hide();
69  m_sizerFormat->GetStaticBox()->Hide();
70  m_sizerText->GetStaticBox()->Hide();
71 
72  m_lblArrowLength->Hide();
73  m_txtArrowLength->Hide();
74  m_lblArrowLengthUnits->Hide();
75 
76  m_lblExtensionOffset->Hide();
77  m_txtExtensionOffset->Hide();
79 
81  break;
82 
83  default:
84  m_sizerLeader->GetStaticBox()->Hide();
85  m_sizerCenter->GetStaticBox()->Hide();
86  break;
87  }
88 
89  // Fix the size after hiding/showing some of the properties
90  Layout();
91 
92  // Configure display origin transforms
95 
96  // Configure the layers list selector. Note that footprints are built outside the current
97  // board and so we may need to show all layers if the text is on an unactivated layer.
100 
102  m_cbLayerActual->SetBoardFrame( aParent );
104 
105  m_orientValue = 0.0;
106  m_orientValidator.SetRange( -360.0, 360.0 );
107  m_cbTextOrientation->SetValidator( m_orientValidator );
109 
110  // Handle decimal separators in combo dropdown
111  for( size_t i = 0; i < m_cbTextOrientation->GetCount(); ++i )
112  {
113  wxString item = m_cbTextOrientation->GetString( i );
114  item.Replace( '.', localeconv()->decimal_point[0] );
115  m_cbTextOrientation->SetString( i, item );
116  }
117 
118  m_sdbSizerOK->SetDefault();
119 
120  m_cbOverrideValue->Bind( wxEVT_CHECKBOX,
121  [&]( wxCommandEvent& evt )
122  {
123  m_txtValue->Enable( m_cbOverrideValue->GetValue() );
124 
125  if( !m_cbOverrideValue->GetValue() )
126  m_txtValue->SetValue( m_dimension->GetValueText() );
127  } );
128 
129  auto updateEventHandler =
130  [&]( wxCommandEvent& evt )
131  {
132  if( !m_cbOverrideValue->GetValue() )
133  m_txtValue->ChangeValue( m_dimension->GetValueText() );
134 
136  };
137 
138  // No need to use m_txtValueActual here since we don't have previewing for leaders
139  m_txtValue->Bind( wxEVT_TEXT, updateEventHandler );
140  m_txtPrefix->Bind( wxEVT_TEXT, updateEventHandler );
141  m_txtSuffix->Bind( wxEVT_TEXT, updateEventHandler );
142 
143  m_cbUnits->Bind( wxEVT_CHOICE, updateEventHandler );
144  m_cbUnitsFormat->Bind( wxEVT_CHOICE, updateEventHandler );
145  m_cbPrecision->Bind( wxEVT_CHOICE, updateEventHandler );
146  m_cbSuppressZeroes->Bind( wxEVT_CHECKBOX, updateEventHandler );
147 
148  m_cbTextPositionMode->Bind( wxEVT_CHOICE,
149  [&]( wxCommandEvent& aEvt )
150  {
151  // manual mode
152  bool allowPositioning = ( m_cbTextPositionMode->GetSelection() == 2 );
153 
154  m_txtTextPosX->Enable( allowPositioning );
155  m_txtTextPosY->Enable( allowPositioning );
156  } );
157 
158  m_cbKeepAligned->Bind( wxEVT_CHECKBOX,
159  [&]( wxCommandEvent& aEvt )
160  {
161  m_cbTextOrientation->Enable( !m_cbKeepAligned->GetValue() );
162  } );
163 
165 }
166 
167 
169 {
170  delete m_previewDimension;
171 }
172 
173 
175 {
176  BOARD* board = m_frame->GetBoard();
177 
180 
181  switch( m_dimension->GetUnitsMode() )
182  {
183  case DIM_UNITS_MODE::INCHES: m_cbUnits->SetSelection( 0 ); break;
184  case DIM_UNITS_MODE::MILS: m_cbUnits->SetSelection( 1 ); break;
185  case DIM_UNITS_MODE::MILLIMETRES: m_cbUnits->SetSelection( 2 ); break;
186  case DIM_UNITS_MODE::AUTOMATIC: m_cbUnits->SetSelection( 3 ); break;
187  }
188 
189  m_cbUnitsFormat->SetSelection( static_cast<int>( m_dimension->GetUnitsFormat() ) );
190  m_cbPrecision->SetSelection( static_cast<int>( m_dimension->GetPrecision() ) );
191 
194 
196  {
197  wxMessageBox( _( "This item was on a non-existing or forbidden layer.\n"
198  "It has been moved to the first allowed layer." ) );
199  m_cbLayerActual->SetSelection( 0 );
200  }
201 
203 
205 
206  m_textWidth.SetValue( text.GetTextSize().x );
207  m_textHeight.SetValue( text.GetTextSize().y );
208  m_textThickness.SetValue( text.GetTextThickness() );
209 
210  m_textPosX.SetValue( text.GetTextPos().x );
211  m_textPosY.SetValue( text.GetTextPos().y );
212  m_cbTextPositionMode->SetSelection( static_cast<int>( m_dimension->GetTextPositionMode() ) );
213 
215  {
216  m_txtTextPosX->Disable();
217  m_txtTextPosY->Disable();
218  }
219 
220  m_orientValue = text.GetTextAngleDegrees();
223 
224  m_cbItalic->SetValue( text.IsItalic() );
225  m_cbMirrored->SetValue( text.IsMirrored() );
226  EDA_TEXT_HJUSTIFY_T hJustify = text.GetHorizJustify();
227  m_cbJustification->SetSelection( (int) hJustify + 1 );
228 
232 
233  // Do this last; it depends on the other settings
235  {
236  wxString txt = board->ConvertKIIDsToCrossReferences( m_dimension->GetOverrideText() );
237  m_txtValueActual->SetValue( txt );
238  }
239  else
240  {
241  m_txtValueActual->SetValue( m_dimension->GetValueText() );
242  }
243 
244  m_orientValidator.TransferToWindow();
245 
246  if( m_dimension->Type() == PCB_DIM_LEADER_T )
247  {
248  LEADER* leader = static_cast<LEADER*>( m_dimension );
249  m_cbTextFrame->SetSelection( static_cast<int>( leader->GetTextFrame() ) );
250  }
251 
252  return DIALOG_DIMENSION_PROPERTIES_BASE::TransferDataToWindow();
253 }
254 
255 
257 {
258  if( !DIALOG_DIMENSION_PROPERTIES_BASE::TransferDataFromWindow() )
259  return false;
260 
261  BOARD_COMMIT commit( m_frame );
262  commit.Modify( m_dimension );
263 
264  // If no other command in progress, prepare undo command
265  // (for a command in progress, will be made later, at the completion of command)
266  bool pushCommit = ( m_dimension->GetEditFlags() == 0 );
267 
268  /* set flag in edit to force undo/redo/abort proper operation,
269  * and avoid new calls to SaveCopyInUndoList for the same dimension
270  * this can occurs when a dimension is moved, and then rotated, edited ..
271  */
272  if( !pushCommit )
274 
276 
277  if( pushCommit )
278  commit.Push( _( "Change dimension properties" ) );
279 
280  return true;
281 }
282 
283 
285 {
286  BOARD* board = m_frame->GetBoard();
287 
288  m_orientValidator.TransferFromWindow();
289 
290  aTarget->SetOverrideTextEnabled( m_cbOverrideValue->GetValue() );
291 
292  if( m_cbOverrideValue->GetValue() )
293  {
294  wxString txt = board->ConvertCrossReferencesToKIIDs( m_txtValueActual->GetValue() );
295  aTarget->SetOverrideText( txt );
296  }
297 
298  aTarget->SetPrefix( board->ConvertCrossReferencesToKIIDs( m_txtPrefix->GetValue() ) );
299  aTarget->SetSuffix( board->ConvertCrossReferencesToKIIDs( m_txtSuffix->GetValue() ) );
300  aTarget->SetLayer( static_cast<PCB_LAYER_ID>( m_cbLayerActual->GetLayerSelection() ) );
301 
302  switch( m_cbUnits->GetSelection() )
303  {
304  case 0:
306  break;
307 
308  case 1:
310  break;
311 
312  case 2:
314  break;
315 
316  case 3:
318  aTarget->SetUnits( m_frame->GetUserUnits() );
319  break;
320  }
321 
322  aTarget->SetUnitsFormat( static_cast<DIM_UNITS_FORMAT>( m_cbUnitsFormat->GetSelection() ) );
323  aTarget->SetPrecision( m_cbPrecision->GetSelection() );
324  aTarget->SetSuppressZeroes( m_cbSuppressZeroes->GetValue() );
325 
326  PCB_TEXT& text = aTarget->Text();
327 
328  DIM_TEXT_POSITION tpm = static_cast<DIM_TEXT_POSITION>( m_cbTextPositionMode->GetSelection() );
329  aTarget->SetTextPositionMode( tpm );
330 
331  if( tpm == DIM_TEXT_POSITION::MANUAL )
332  {
333  wxPoint pos( m_textPosX.GetValue(), m_textPosY.GetValue() );
334  text.SetPosition( pos );
335  }
336 
337  aTarget->SetKeepTextAligned( m_cbKeepAligned->GetValue() );
338 
339  text.SetTextAngle( KiROUND( m_orientValue * 10.0 ) );
340  text.SetTextWidth( m_textWidth.GetValue() );
341  text.SetTextHeight( m_textHeight.GetValue() );
342  text.SetTextThickness( m_textThickness.GetValue() );
343  text.SetItalic( m_cbItalic->GetValue() );
344  text.SetMirrored( m_cbMirrored->GetValue() );
345  int justification = m_cbJustification->GetSelection() - 1;
346  text.SetHorizJustify( static_cast<EDA_TEXT_HJUSTIFY_T>( justification ) );
347 
349  aTarget->SetArrowLength( m_arrowLength.GetValue() );
351 
352  if( aTarget->Type() == PCB_DIM_LEADER_T )
353  {
354  LEADER* leader = static_cast<LEADER*>( aTarget );
355  leader->SetTextFrame( static_cast<DIM_TEXT_FRAME>( m_cbTextFrame->GetSelection() ) );
356  }
357 
358  aTarget->Update();
359 }
360 
361 
363 {
366 }
bool GetSuppressZeroes() const
Definition: dimension.h:172
COMMIT & Modify(EDA_ITEM *aItem)
Create an undo entry for an item that has been already modified.
Definition: commit.h:103
bool GetOverrideTextEnabled() const
Definition: dimension.h:130
EDA_TEXT_HJUSTIFY_T
Definition: eda_text.h:61
Class DIALOG_DIMENSION_PROPERTIES_BASE.
class LEADER, a leader dimension (graphic item)
Definition: typeinfo.h:101
DIM_UNITS_FORMAT GetUnitsFormat() const
Definition: dimension.h:166
void SetParent(EDA_ITEM *aParent) override
Definition: dimension.cpp:61
void SetTextFrame(DIM_TEXT_FRAME aFrame)
Definition: dimension.h:501
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:82
DIM_UNITS_MODE GetUnitsMode() const
Definition: dimension.cpp:150
DIM_TEXT_FRAME GetTextFrame() const
Definition: dimension.h:502
constexpr KICAD_T BaseType(const KICAD_T aType)
Returns the underlying type of the given type.
Definition: typeinfo.h:235
class CENTER_DIMENSION, a center point marking (graphic item)
Definition: typeinfo.h:102
void SetLayer(PCB_LAYER_ID aLayer) override
Set the layer this item is on.
Definition: dimension.cpp:212
void SetUnitsFormat(const DIM_UNITS_FORMAT aFormat)
Definition: dimension.h:167
void SetPrefix(const wxString &aPrefix)
Definition: dimension.cpp:132
DIM_TEXT_POSITION GetTextPositionMode() const
Definition: dimension.h:179
void SetUnits(EDA_UNITS aUnits)
Definition: dimension.cpp:144
wxString GetOverrideText() const
Definition: dimension.h:133
void SetBoardFrame(PCB_BASE_FRAME *aFrame)
wxFloatingPointValidator< double > m_orientValidator
void Update()
Updates the dimension's cached text and geometry.
Definition: dimension.h:146
PCB_LAYER_BOX_SELECTOR * m_cbLayerActual
bool IsLayerEnabled(PCB_LAYER_ID aLayer) const
A proxy function that calls the correspondent function in m_BoardSettings tests whether a given layer...
Definition: board.h:515
int GetPrecision() const
Definition: dimension.h:169
void SetOverrideText(const wxString &aValue)
Definition: dimension.h:134
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition: eda_item.cpp:81
int GetLineThickness() const
Definition: dimension.h:187
void SetSuppressZeroes(bool aSuppress)
Definition: dimension.h:173
LAYER_NUM GetLayerSelection() const
wxString GetShownText(int aDepth=0) const override
Return the string actually shown after processing of the base text.
Definition: pcb_text.cpp:53
wxString GetSuffix() const
Definition: dimension.h:155
void SetFlags(STATUS_FLAGS aMask)
Definition: eda_item.h:203
wxString GetPrefix() const
Definition: dimension.h:152
A leader is a dimension-like object pointing to a specific point.
Definition: dimension.h:478
STATUS_FLAGS GetEditFlags() const
Definition: eda_item.h:208
void ShowNonActivatedLayers(bool aShow)
#define IN_EDIT
Item currently edited.
Definition: eda_item.h:105
PCB_TEXT & Text()
Definition: dimension.h:209
Text placement is manually set by the user.
int SetLayerSelection(LAYER_NUM layer)
void SetExtensionOffset(int aOffset)
Definition: dimension.h:184
void SetSuffix(const wxString &aSuffix)
Definition: dimension.cpp:138
bool SetLayersHotkeys(bool value)
class DIMENSION_BASE: abstract dimension meta-type
Definition: typeinfo.h:99
void updateDimensionFromDialog(DIMENSION_BASE *aTarget)
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
Common, abstract interface for edit frames.
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
#define _(s)
Definition: 3d_actions.cpp:33
void SetPrecision(int aPrecision)
Definition: dimension.h:170
virtual void SetValue(int aValue)
Set new value (in Internal Units) for the text field, taking care of units conversion.
wxString ConvertCrossReferencesToKIIDs(const wxString &aSource) const
Convert cross-references back and forth between ${refDes:field} and ${kiid:field}.
Definition: board.cpp:930
void SetArrowLength(int aLength)
Definition: dimension.h:182
DIALOG_DIMENSION_PROPERTIES(PCB_BASE_EDIT_FRAME *aParent, BOARD_ITEM *aItem)
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:68
virtual void Push(const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true) override
Revert the commit by restoring the modified items state.
virtual long long int GetValue()
Return the current value in Internal Units.
wxString ConvertKIIDsToCrossReferences(const wxString &aSource) const
Definition: board.cpp:984
DIM_TEXT_POSITION
Where to place the text on a dimension.
Definition: dimension.h:47
void SetOverrideTextEnabled(bool aOverride)
Definition: dimension.h:131
void SetCoordType(ORIGIN_TRANSFORMS::COORD_TYPES_T aCoordType)
Set the current origin transform mode.
Definition: unit_binder.h:173
BOARD * GetBoard() const
void SetLineThickness(int aWidth)
Definition: dimension.h:188
void SetTextPositionMode(DIM_TEXT_POSITION aMode)
Definition: dimension.h:178
Abstract dimension API.
Definition: dimension.h:95
bool GetKeepTextAligned() const
Definition: dimension.h:175
virtual PCB_LAYER_ID GetLayer() const
Return the primary layer this item is on.
Definition: board_item.h:173
int GetArrowLength() const
Definition: dimension.h:181
int GetExtensionOffset() const
Definition: dimension.h:185
void SetUnitsMode(DIM_UNITS_MODE aMode)
Definition: dimension.cpp:174
EDA_UNITS GetUserUnits() const
Return the user units currently in use.
void SetKeepTextAligned(bool aKeepAligned)
Definition: dimension.h:176
wxString GetValueText() const
Definition: dimension.cpp:103
KICAD_T Type() const
Returns the type of object.
Definition: eda_item.h:163