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, true ),
38  m_textHeight( aParent, m_lblTextHeight, m_txtTextHeight, m_lblTextHeightUnits, true ),
39  m_textThickness( aParent, m_lblTextThickness, m_txtTextThickness,
40  m_lblTextThicknessUnits, true ),
41  m_textPosX( aParent, m_lblTextPosX, m_txtTextPosX, m_lblTextPosXUnits ),
42  m_textPosY( aParent, m_lblTextPosY, m_txtTextPosY, m_lblTextPosYUnits ),
43  m_orientValidator( 1, &m_orientValue ),
44  m_lineThickness( aParent, m_lblLineThickness, m_txtLineThickness,
45  m_lblLineThicknessUnits, true ),
46  m_arrowLength( aParent, m_lblArrowLength, m_txtArrowLength, m_lblArrowLengthUnits, true ),
47  m_extensionOffset( aParent, m_lblExtensionOffset, m_txtExtensionOffset,
48  m_lblExtensionOffsetUnits )
49 {
50  wxASSERT( BaseType( aItem->Type() ) == PCB_DIMENSION_T );
51  m_dimension = static_cast<DIMENSION_BASE*>( aItem );
52  m_previewDimension = static_cast<DIMENSION_BASE*>( m_dimension->Clone() );
54 
55  switch( m_dimension->Type() )
56  {
57  case PCB_DIM_LEADER_T:
58  // Hide the main format controls and keep the leader controls shown
59  m_sizerFormat->GetStaticBox()->Hide();
60  m_sizerCenter->GetStaticBox()->Hide();
61 
64 
65  // Remove a fewings from text format
66  m_lblTextPositionMode->Hide();
67  m_cbTextPositionMode->Hide();
68  break;
69 
70  case PCB_DIM_CENTER_T:
71  m_sizerLeader->GetStaticBox()->Hide();
72  m_sizerFormat->GetStaticBox()->Hide();
73  m_sizerText->GetStaticBox()->Hide();
74 
75  m_lblArrowLength->Hide();
76  m_txtArrowLength->Hide();
77  m_lblArrowLengthUnits->Hide();
78 
79  m_lblExtensionOffset->Hide();
80  m_txtExtensionOffset->Hide();
82 
84  break;
85 
86  default:
87  m_sizerLeader->GetStaticBox()->Hide();
88  m_sizerCenter->GetStaticBox()->Hide();
89  break;
90  }
91 
92  // Fix the size after hiding/showing some of the properties
93  Layout();
94 
95  // Configure display origin transforms
98 
99  // Configure the layers list selector. Note that footprints are built outside the current
100  // board and so we may need to show all layers if the text is on an unactivated layer.
103 
105  m_cbLayerActual->SetBoardFrame( aParent );
107 
108  m_orientValue = 0.0;
109  m_orientValidator.SetRange( -360.0, 360.0 );
110  m_cbTextOrientation->SetValidator( m_orientValidator );
112 
113  // Handle decimal separators in combo dropdown
114  for( size_t i = 0; i < m_cbTextOrientation->GetCount(); ++i )
115  {
116  wxString item = m_cbTextOrientation->GetString( i );
117  item.Replace( '.', localeconv()->decimal_point[0] );
118  m_cbTextOrientation->SetString( i, item );
119  }
120 
121  m_sdbSizerOK->SetDefault();
122 
123  m_cbOverrideValue->Bind( wxEVT_CHECKBOX,
124  [&]( wxCommandEvent& evt )
125  {
126  m_txtValue->Enable( m_cbOverrideValue->GetValue() );
127 
128  if( !m_cbOverrideValue->GetValue() )
129  m_txtValue->SetValue( m_dimension->GetValueText() );
130  } );
131 
132  auto updateEventHandler =
133  [&]( wxCommandEvent& evt )
134  {
135  if( !m_cbOverrideValue->GetValue() )
136  m_txtValue->ChangeValue( m_dimension->GetValueText() );
137 
139  };
140 
141  // No need to use m_txtValueActual here since we don't have previewing for leaders
142  m_txtValue->Bind( wxEVT_TEXT, updateEventHandler );
143  m_txtPrefix->Bind( wxEVT_TEXT, updateEventHandler );
144  m_txtSuffix->Bind( wxEVT_TEXT, updateEventHandler );
145 
146  m_cbUnits->Bind( wxEVT_CHOICE, updateEventHandler );
147  m_cbUnitsFormat->Bind( wxEVT_CHOICE, updateEventHandler );
148  m_cbPrecision->Bind( wxEVT_CHOICE, updateEventHandler );
149  m_cbSuppressZeroes->Bind( wxEVT_CHECKBOX, updateEventHandler );
150 
151  m_cbTextPositionMode->Bind( wxEVT_CHOICE,
152  [&]( wxCommandEvent& aEvt )
153  {
154  // manual mode
155  bool allowPositioning = ( m_cbTextPositionMode->GetSelection() == 2 );
156 
157  m_txtTextPosX->Enable( allowPositioning );
158  m_txtTextPosY->Enable( allowPositioning );
159  } );
160 
161  m_cbKeepAligned->Bind( wxEVT_CHECKBOX,
162  [&]( wxCommandEvent& aEvt )
163  {
164  m_cbTextOrientation->Enable( !m_cbKeepAligned->GetValue() );
165  } );
166 
168 }
169 
170 
172 {
173  delete m_previewDimension;
174 }
175 
176 
178 {
179  BOARD* board = m_frame->GetBoard();
180 
183 
184  switch( m_dimension->GetUnitsMode() )
185  {
186  case DIM_UNITS_MODE::INCHES: m_cbUnits->SetSelection( 0 ); break;
187  case DIM_UNITS_MODE::MILS: m_cbUnits->SetSelection( 1 ); break;
188  case DIM_UNITS_MODE::MILLIMETRES: m_cbUnits->SetSelection( 2 ); break;
189  case DIM_UNITS_MODE::AUTOMATIC: m_cbUnits->SetSelection( 3 ); break;
190  }
191 
192  m_cbUnitsFormat->SetSelection( static_cast<int>( m_dimension->GetUnitsFormat() ) );
193  m_cbPrecision->SetSelection( static_cast<int>( m_dimension->GetPrecision() ) );
194 
197 
199  {
200  wxMessageBox( _( "This item was on a non-existing or forbidden layer.\n"
201  "It has been moved to the first allowed layer." ) );
202  m_cbLayerActual->SetSelection( 0 );
203  }
204 
206 
207  PCB_TEXT& text = m_dimension->Text();
208 
209  m_textWidth.SetValue( text.GetTextSize().x );
210  m_textHeight.SetValue( text.GetTextSize().y );
212 
213  m_textPosX.SetValue( text.GetTextPos().x );
214  m_textPosY.SetValue( text.GetTextPos().y );
215  m_cbTextPositionMode->SetSelection( static_cast<int>( m_dimension->GetTextPositionMode() ) );
216 
218  {
219  m_txtTextPosX->Disable();
220  m_txtTextPosY->Disable();
221  }
222 
226 
227  m_cbItalic->SetValue( text.IsItalic() );
228  m_cbMirrored->SetValue( text.IsMirrored() );
229  EDA_TEXT_HJUSTIFY_T hJustify = text.GetHorizJustify();
230  m_cbJustification->SetSelection( (int) hJustify + 1 );
231 
235 
236  // Do this last; it depends on the other settings
238  {
239  wxString txt = board->ConvertKIIDsToCrossReferences( m_dimension->GetOverrideText() );
240  m_txtValueActual->SetValue( txt );
241  }
242  else
243  {
244  m_txtValueActual->SetValue( m_dimension->GetValueText() );
245  }
246 
247  m_orientValidator.TransferToWindow();
248 
249  if( m_dimension->Type() == PCB_DIM_LEADER_T )
250  {
251  LEADER* leader = static_cast<LEADER*>( m_dimension );
252  m_cbTextFrame->SetSelection( static_cast<int>( leader->GetTextFrame() ) );
253  }
254 
255  return DIALOG_DIMENSION_PROPERTIES_BASE::TransferDataToWindow();
256 }
257 
258 
260 {
261  if( !DIALOG_DIMENSION_PROPERTIES_BASE::TransferDataFromWindow() )
262  return false;
263 
264  BOARD_COMMIT commit( m_frame );
265  commit.Modify( m_dimension );
266 
267  // If no other command in progress, prepare undo command
268  // (for a command in progress, will be made later, at the completion of command)
269  bool pushCommit = ( m_dimension->GetEditFlags() == 0 );
270 
271  /* set flag in edit to force undo/redo/abort proper operation,
272  * and avoid new calls to SaveCopyInUndoList for the same dimension
273  * this can occurs when a dimension is moved, and then rotated, edited ..
274  */
275  if( !pushCommit )
277 
279 
280  if( pushCommit )
281  commit.Push( _( "Change dimension properties" ) );
282 
283  return true;
284 }
285 
286 
288 {
289  BOARD* board = m_frame->GetBoard();
290 
291  m_orientValidator.TransferFromWindow();
292 
293  aTarget->SetOverrideTextEnabled( m_cbOverrideValue->GetValue() );
294 
295  if( m_cbOverrideValue->GetValue() )
296  {
297  wxString txt = board->ConvertCrossReferencesToKIIDs( m_txtValueActual->GetValue() );
298  aTarget->SetOverrideText( txt );
299  }
300 
301  aTarget->SetPrefix( board->ConvertCrossReferencesToKIIDs( m_txtPrefix->GetValue() ) );
302  aTarget->SetSuffix( board->ConvertCrossReferencesToKIIDs( m_txtSuffix->GetValue() ) );
303  aTarget->SetLayer( static_cast<PCB_LAYER_ID>( m_cbLayerActual->GetLayerSelection() ) );
304 
305  switch( m_cbUnits->GetSelection() )
306  {
307  case 0:
309  break;
310 
311  case 1:
313  break;
314 
315  case 2:
317  break;
318 
319  case 3:
321  aTarget->SetUnits( m_frame->GetUserUnits() );
322  break;
323  }
324 
325  aTarget->SetUnitsFormat( static_cast<DIM_UNITS_FORMAT>( m_cbUnitsFormat->GetSelection() ) );
326  aTarget->SetPrecision( m_cbPrecision->GetSelection() );
327  aTarget->SetSuppressZeroes( m_cbSuppressZeroes->GetValue() );
328 
329  PCB_TEXT& text = aTarget->Text();
330 
331  DIM_TEXT_POSITION tpm = static_cast<DIM_TEXT_POSITION>( m_cbTextPositionMode->GetSelection() );
332  aTarget->SetTextPositionMode( tpm );
333 
334  if( tpm == DIM_TEXT_POSITION::MANUAL )
335  {
336  wxPoint pos( m_textPosX.GetValue(), m_textPosY.GetValue() );
337  text.SetPosition( pos );
338  }
339 
340  aTarget->SetKeepTextAligned( m_cbKeepAligned->GetValue() );
341 
342  text.SetTextAngle( KiROUND( m_orientValue * 10.0 ) );
346  text.SetItalic( m_cbItalic->GetValue() );
347  text.SetMirrored( m_cbMirrored->GetValue() );
348  int justification = m_cbJustification->GetSelection() - 1;
349  text.SetHorizJustify( static_cast<EDA_TEXT_HJUSTIFY_T>( justification ) );
350 
352  aTarget->SetArrowLength( m_arrowLength.GetValue() );
354 
355  if( aTarget->Type() == PCB_DIM_LEADER_T )
356  {
357  LEADER* leader = static_cast<LEADER*>( aTarget );
358  leader->SetTextFrame( static_cast<DIM_TEXT_FRAME>( m_cbTextFrame->GetSelection() ) );
359  }
360 
361  aTarget->Update();
362 }
363 
364 
366 {
369 }
void SetMirrored(bool isMirrored)
Definition: eda_text.h:195
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
bool IsMirrored() const
Definition: eda_text.h:196
DIM_UNITS_FORMAT GetUnitsFormat() const
Definition: dimension.h:166
void SetParent(EDA_ITEM *aParent) override
Definition: dimension.cpp:61
virtual void SetPosition(const wxPoint &aPos) override
Definition: pcb_text.h:77
void SetTextFrame(DIM_TEXT_FRAME aFrame)
Definition: dimension.h:499
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:500
void SetTextAngle(double aAngle) override
Definition: pcb_text.cpp:102
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 SetItalic(bool isItalic)
Definition: eda_text.h:186
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
int GetTextThickness() const
Definition: eda_text.h:167
wxString ConvertKIIDsToCrossReferences(const wxString &aSource)
Definition: board.cpp:961
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:513
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:97
int GetLineThickness() const
Definition: dimension.h:187
bool IsItalic() const
Definition: eda_text.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:202
EDA_TEXT_HJUSTIFY_T GetHorizJustify() const
Definition: eda_text.h:205
wxString GetPrefix() const
Definition: dimension.h:152
A leader is a dimension-like object pointing to a specific point.
Definition: dimension.h:476
STATUS_FLAGS GetEditFlags() const
Definition: eda_item.h:207
void ShowNonActivatedLayers(bool aShow)
const wxSize & GetTextSize() const
Definition: eda_text.h:245
#define IN_EDIT
Item currently edited.
Definition: eda_item.h:104
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 SetTextWidth(int aWidth)
Definition: eda_text.h:247
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
double GetTextAngleDegrees() const
Definition: eda_text.h:183
Common, abstract interface for edit frames.
Information pertinent to a Pcbnew printed circuit board.
Definition: board.h:190
void SetHorizJustify(EDA_TEXT_HJUSTIFY_T aType)
Definition: eda_text.h:208
#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.
void SetArrowLength(int aLength)
Definition: dimension.h:182
void SetTextHeight(int aHeight)
Definition: eda_text.h:250
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.
DIM_TEXT_POSITION
Where to place the text on a dimension.
Definition: dimension.h:47
const wxPoint & GetTextPos() const
Definition: eda_text.h:254
void SetTextThickness(int aWidth)
The TextThickness is that set by the user.
Definition: eda_text.h:166
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
wxString ConvertCrossReferencesToKIIDs(const wxString &aSource)
Convert cross-references back and forth between ${refDes:field} and ${kiid:field}.
Definition: board.cpp:907
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:162