KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_barcode_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) 2019 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2013 Dick Hollenbeck, [email protected]
6 * Copyright (C) 2008-2013 Wayne Stambaugh <[email protected]>
7 * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23#include <core/type_helpers.h>
24#include <base_units.h>
25#include <bitmaps.h>
26#include <board_commit.h>
27#include <pcb_barcode.h>
28#include <pcb_text.h>
29#include <layer_ids.h>
30#include <math/util.h>
32#include <pcb_base_frame.h>
33#include <pcb_base_edit_frame.h>
34#include <pcb_painter.h>
35#include <pcbnew_settings.h>
37#include <tool/tool_manager.h>
38#include <view/view_controls.h>
41#include <wx/msgdlg.h>
42// For BOX2D viewport checks
43#include <math/box2.h>
44
45
48 m_parent( aParent ),
57{
58 m_currentBarcode = aBarcode; // aBarcode can not be NULL (no FOOTPRINT editor?)
59
60 m_board = m_parent->GetBoard();
61
62 // Use the board as parent so text variables like ${PART_NUMBER} can be resolved
63 // in the preview. The dummy barcode is not actually added to the board.
65
66 // Initialize canvas to be able to display the dummy barcode:
68
69 m_sdbSizerOK->SetDefault();
70}
71
72
77
78
79void DIALOG_BARCODE_PROPERTIES::OnCancel( wxCommandEvent& event )
80{
81 // Mandatory to avoid m_panelShowPadGal trying to draw something
82 // in a non valid context during closing process:
83 m_panelShowBarcodeGal->StopDrawing();
84
85 // Now call default handler for wxID_CANCEL command event
86 event.Skip();
87}
88
89
91{
92 // Initialize the canvas to display the barcode
93
94 m_panelShowBarcodeGal->UpdateColors();
95 m_panelShowBarcodeGal->SwitchBackend( m_parent->GetCanvas()->GetBackend() );
96 m_panelShowBarcodeGal->SetStealsFocus( false );
97
99
100 KIGFX::VIEW* view = m_panelShowBarcodeGal->GetView();
101
102 // gives a non null grid size (0.001mm) because GAL layer does not like a 0 size grid:
103 double gridsize = pcbIUScale.mmToIU( 0.001 );
104 view->GetGAL()->SetGridSize( VECTOR2D( gridsize, gridsize ) );
105 // And do not show the grid:
106 view->GetGAL()->SetGridVisibility( false );
107 view->Add( m_dummyBarcode );
108
109 m_panelShowBarcodeGal->StartDrawing();
110 Connect( wxEVT_SIZE, wxSizeEventHandler( DIALOG_BARCODE_PROPERTIES::OnResize ) );
111}
112
113
115{
116 // Copy current barcode into our working copy
117 if( m_currentBarcode )
119
120 m_dummyBarcode->AssembleBarcode();
121
123
124 // Configure the layers list selector. Note that footprints are built outside the current
125 // board and so we may need to show all layers if the barcode is on an unactivated layer.
126 if( !m_parent->GetBoard()->IsLayerEnabled( m_dummyBarcode->GetLayer() ) )
127 m_cbLayer->ShowNonActivatedLayers( true );
128
129 m_cbLayer->SetLayersHotkeys( false );
130 m_cbLayer->SetBoardFrame( m_parent );
131 m_cbLayer->Resync();
132}
133
134
135void DIALOG_BARCODE_PROPERTIES::OnResize( wxSizeEvent& event )
136{
137 m_panelShowBarcodeGal->Refresh();
138 event.Skip();
139}
140
141
142void DIALOG_BARCODE_PROPERTIES::OnUpdateUI( wxUpdateUIEvent& event )
143{
144 // Error correction options are only meaningful for QR codes
145 bool enableEC = m_barcode->GetSelection() >= to_underlying( BARCODE_T::QR_CODE );
146 m_errorCorrection->Enable( enableEC );
147
148 m_textSize.Enable( m_cbShowText->GetValue() );
149
150 m_knockoutMarginX.Enable( m_cbKnockout->GetValue() );
151 m_knockoutMarginY.Enable( m_cbKnockout->GetValue() );
152
153 if( enableEC )
154 {
155 // Micro QR codes do not support High (H) error correction level
156 bool isMicroQR = ( m_barcode->GetSelection() == to_underlying( BARCODE_T::MICRO_QR_CODE ) );
157
158 // Enable/disable the High option (index 3)
159 m_errorCorrection->Enable( 3, !isMicroQR );
160
161 // If currently High is selected and we switched to Micro QR, change to a valid option
162 if( isMicroQR && m_errorCorrection->GetSelection() == 3 )
163 {
164 m_errorCorrection->SetSelection( 2 ); // Default to Q (Quartile), consistent with SetErrorCorrection
165 }
166 }
167}
168
169
171{
172 initValues();
173
174 if( m_currentBarcode )
176
177 m_textInput->ChangeValue( m_dummyBarcode->GetText() );
178 m_cbLocked->SetValue( m_dummyBarcode->IsLocked() );
179 m_cbLayer->SetLayerSelection( m_dummyBarcode->GetLayer() );
180
181 // Position
182 m_posX.ChangeValue( m_dummyBarcode->GetPosition().x );
183 m_posY.ChangeValue( m_dummyBarcode->GetPosition().y );
184
185 // Size
186 m_sizeX.ChangeValue( m_dummyBarcode->GetWidth() );
187 m_sizeY.ChangeValue( m_dummyBarcode->GetHeight() );
188 m_textSize.ChangeValue( m_dummyBarcode->GetTextSize() );
189
190 // Orientation
191 m_orientation.ChangeAngleValue( m_dummyBarcode->GetAngle() );
192
193 // Show text option
194 m_cbShowText->SetValue( m_dummyBarcode->GetShowText() );
195
196 m_cbKnockout->SetValue( m_dummyBarcode->IsKnockout() );
197 m_knockoutMarginX.ChangeValue( m_dummyBarcode->GetMargin().x );
198 m_knockoutMarginY.ChangeValue( m_dummyBarcode->GetMargin().y );
199
200 // Barcode type
201 switch( m_dummyBarcode->GetKind() )
202 {
203 case BARCODE_T::CODE_39: m_barcode->SetSelection( 0 ); break;
204 case BARCODE_T::CODE_128: m_barcode->SetSelection( 1 ); break;
205 case BARCODE_T::DATA_MATRIX: m_barcode->SetSelection( 2 ); break;
206 case BARCODE_T::QR_CODE: m_barcode->SetSelection( 3 ); break;
207 case BARCODE_T::MICRO_QR_CODE: m_barcode->SetSelection( 4 ); break;
208 default: m_barcode->SetSelection( 0 ); break;
209 }
210
211 // Error correction level
212 switch( m_dummyBarcode->GetErrorCorrection() )
213 {
214 case BARCODE_ECC_T::L: m_errorCorrection->SetSelection( 0 ); break;
215 case BARCODE_ECC_T::M: m_errorCorrection->SetSelection( 1 ); break;
216 case BARCODE_ECC_T::Q: m_errorCorrection->SetSelection( 2 ); break;
217 case BARCODE_ECC_T::H: m_errorCorrection->SetSelection( 3 ); break;
218 default: m_errorCorrection->SetSelection( 0 ); break;
219 }
220
221 // Now all widgets have the size fixed, call finishDialogSettings
223
225
226 wxUpdateUIEvent dummy;
227 OnUpdateUI( dummy );
228
229 return true;
230}
231
232
234{
236
237 if( !m_dummyBarcode->GetText().empty() && m_dummyBarcode->GetSymbolPoly().OutlineCount() == 0 )
238 {
239 wxMessageBox( m_dummyBarcode->GetLastError(), _( "Barcode Error" ), wxOK | wxICON_ERROR, this );
240 return false;
241 }
242
243 BOARD_COMMIT commit( m_parent );
244 commit.Modify( m_currentBarcode );
245
247 return false;
248
249 m_parent->GetCanvas()->Refresh();
250
251 commit.Push( _( "Modify barcode" ) );
252
253 return true;
254}
255
256
258{
259 if( !aBarcode )
260 return false;
261
262 aBarcode->SetText( m_textInput->GetValue() );
263 aBarcode->SetLocked( m_cbLocked->GetValue() );
264 aBarcode->SetLayer( ToLAYER_ID( m_cbLayer->GetLayerSelection() ) );
265
266 // Position
267 aBarcode->SetPosition( VECTOR2I( m_posX.GetIntValue(), m_posY.GetIntValue() ) );
268
269 // Size
270 aBarcode->SetWidth( m_sizeX.GetIntValue() );
271 aBarcode->SetHeight( m_sizeY.GetIntValue() );
272 aBarcode->SetTextSize( m_textSize.GetIntValue() );
273
274 // Orientation
275 EDA_ANGLE oldAngle = aBarcode->GetAngle();
276 EDA_ANGLE newAngle = m_orientation.GetAngleValue();
277
278 if( newAngle != oldAngle )
279 aBarcode->Rotate( aBarcode->GetPosition(), newAngle - oldAngle );
280
281 // Knockout
282 aBarcode->SetIsKnockout( m_cbKnockout->GetValue() );
283 aBarcode->SetMargin( VECTOR2I( m_knockoutMarginX.GetIntValue(), m_knockoutMarginY.GetIntValue() ) );
284
285 // Show text
286 aBarcode->SetShowText( m_cbShowText->GetValue() );
287
288 // Barcode kind
289 switch( m_barcode->GetSelection() )
290 {
291 case 0: aBarcode->SetKind( BARCODE_T::CODE_39 ); break;
292 case 1: aBarcode->SetKind( BARCODE_T::CODE_128 ); break;
293 case 2: aBarcode->SetKind( BARCODE_T::DATA_MATRIX ); break;
294 case 3: aBarcode->SetKind( BARCODE_T::QR_CODE ); break;
295 case 4: aBarcode->SetKind( BARCODE_T::MICRO_QR_CODE ); break;
296 default: aBarcode->SetKind( BARCODE_T::QR_CODE ); break;
297 }
298
299 switch( m_errorCorrection->GetSelection() )
300 {
301 case 0: aBarcode->SetErrorCorrection( BARCODE_ECC_T::L ); break;
302 case 1: aBarcode->SetErrorCorrection( BARCODE_ECC_T::M ); break;
303 case 2: aBarcode->SetErrorCorrection( BARCODE_ECC_T::Q ); break;
304 case 3: aBarcode->SetErrorCorrection( BARCODE_ECC_T::H ); break;
305 default: aBarcode->SetErrorCorrection( BARCODE_ECC_T::L ); break;
306 }
307
308 aBarcode->AssembleBarcode();
309
310 return true;
311}
312
313
315{
316 KIGFX::VIEW* view = m_panelShowBarcodeGal->GetView();
317
318 // Compute the polygon bbox for the current dummy barcode (symbol + text/knockout as applicable)
319 const SHAPE_POLY_SET& poly = m_dummyBarcode->GetPolyShape();
320
321 if( poly.OutlineCount() > 0 )
322 {
323 BOX2I bbI = poly.BBox();
324
325 // Autozoom
326 view->SetViewport( BOX2D( bbI.GetOrigin(), bbI.GetSize() ) );
327
328 // Add a margin
329 view->SetScale( view->GetScale() * 0.7 );
330 }
331
332 view->SetCenter( VECTOR2D( m_dummyBarcode->GetPosition() ) );
333 view->Update( m_dummyBarcode );
334 m_panelShowBarcodeGal->Refresh();
335}
336
337
339{
341 {
343 OnModify();
344 }
345}
346
347
349{
351 {
353 OnModify();
354 }
355}
356
357
359{
360 DIALOG_BARCODE_PROPERTIES dlg( this, aBarcode );
361 dlg.ShowModal();
362}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
BOX2< VECTOR2D > BOX2D
Definition box2.h:919
virtual void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
void SetLocked(bool aLocked) override
Definition board_item.h:356
constexpr const Vec & GetOrigin() const
Definition box2.h:206
constexpr const SizeVec & GetSize() const
Definition box2.h:202
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
DIALOG_BARCODE_PROPERTIES_BASE(wxWindow *parent, wxWindowID id=wxID_DIALOG_EDIT_PAD, const wxString &title=_("Barcode Properties"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
DIALOG_BARCODE_PROPERTIES, derived from DIALOG_BARCODE_PROPERTIES_BASE, created by wxFormBuilder.
void OnUpdateUI(wxUpdateUIEvent &event) override
bool transferDataToBarcode(PCB_BARCODE *aBarcode)
Copy values from dialog field to aBarcode's members.
DIALOG_BARCODE_PROPERTIES(PCB_BASE_FRAME *aParent, PCB_BARCODE *aBarcode)
void OnTextValueChanged(wxKeyEvent &event) override
void OnValuesChanged(wxCommandEvent &event) override
Update the graphical barcode shown in the panel.
void OnCancel(wxCommandEvent &event) override
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
int ShowModal() override
void SetGridSize(const VECTOR2D &aGridSize)
Set the grid size.
void SetGridVisibility(bool aVisibility)
Set the visibility setting of the grid.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:63
double GetScale() const
Definition view.h:281
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Set the scaling factor, zooming around a given anchor point.
Definition view.cpp:637
void SetViewport(const BOX2D &aViewport)
Set the visible area of the VIEW.
Definition view.cpp:609
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition view.cpp:300
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition view.cpp:1835
GAL * GetGAL() const
Return the GAL this view is using to draw graphical primitives.
Definition view.h:207
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition view.cpp:663
void SetKind(BARCODE_T aKind)
void SetTextSize(int aTextSize)
Change the height of the human-readable text displayed below the barcode.
void SetMargin(const VECTOR2I &aMargin)
void SetErrorCorrection(BARCODE_ECC_T aErrorCorrection)
Set the error correction level used for QR codes.
void SetShowText(bool aShow)
void SetWidth(int aWidth)
void AssembleBarcode() const
Assemble the barcode polygon and text polygons into a single polygonal representation.
void SetHeight(int aHeight)
VECTOR2I GetPosition() const override
Get the position (center) of the barcode in internal units.
void SetPosition(const VECTOR2I &aPos) override
void SetLayer(PCB_LAYER_ID aLayer) override
Set the drawing layer for the barcode and its text.
void SetIsKnockout(bool aEnable) override
void Rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle) override
Rotate the barcode around a given centre by the given angle.
EDA_ANGLE GetAngle() const
void SetText(const wxString &aText)
Set the barcode content text to encode.
void ShowBarcodePropertiesDialog(PCB_BARCODE *aText)
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
Represent a set of closed polygons.
int OutlineCount() const
Return the number of outlines in the set.
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
#define _(s)
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition lset.cpp:750
BARCODE class definition.
std::vector< FAB_LAYER_COLOR > dummy
constexpr auto to_underlying(E e) noexcept
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682