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, you may find one here:
21 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22 * or you may search the http://www.gnu.org website for the version 2 license,
23 * or you may write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 */
26
27#include <core/type_helpers.h>
28#include <base_units.h>
29#include <bitmaps.h>
30#include <board_commit.h>
31#include <pcb_barcode.h>
32#include <pcb_text.h>
33#include <layer_ids.h>
34#include <math/util.h>
36#include <pcb_base_frame.h>
37#include <pcb_base_edit_frame.h>
38#include <pcb_painter.h>
39#include <pcbnew_settings.h>
41#include <tool/tool_manager.h>
42#include <view/view_controls.h>
45// For BOX2D viewport checks
46#include <math/box2.h>
47
48
51 m_parent( aParent ),
60{
61 m_currentBarcode = aBarcode; // aBarcode can not be NULL (no FOOTPRINT editor?)
62
63 m_board = m_parent->GetBoard();
64
65 // Use the board as parent so text variables like ${PART_NUMBER} can be resolved
66 // in the preview. The dummy barcode is not actually added to the board.
68
69 // Initialize canvas to be able to display the dummy barcode:
71
72 m_sdbSizerOK->SetDefault();
73}
74
75
80
81
82void DIALOG_BARCODE_PROPERTIES::OnCancel( wxCommandEvent& event )
83{
84 // Mandatory to avoid m_panelShowPadGal trying to draw something
85 // in a non valid context during closing process:
86 m_panelShowBarcodeGal->StopDrawing();
87
88 // Now call default handler for wxID_CANCEL command event
89 event.Skip();
90}
91
92
94{
95 // Initialize the canvas to display the barcode
96
97 m_panelShowBarcodeGal->UpdateColors();
98 m_panelShowBarcodeGal->SwitchBackend( m_parent->GetCanvas()->GetBackend() );
99 m_panelShowBarcodeGal->SetStealsFocus( false );
100
101 m_panelShowBarcodeGal->Show();
102
103 KIGFX::VIEW* view = m_panelShowBarcodeGal->GetView();
104
105 // gives a non null grid size (0.001mm) because GAL layer does not like a 0 size grid:
106 double gridsize = pcbIUScale.mmToIU( 0.001 );
107 view->GetGAL()->SetGridSize( VECTOR2D( gridsize, gridsize ) );
108 // And do not show the grid:
109 view->GetGAL()->SetGridVisibility( false );
110 view->Add( m_dummyBarcode );
111
112 m_panelShowBarcodeGal->StartDrawing();
113 Connect( wxEVT_SIZE, wxSizeEventHandler( DIALOG_BARCODE_PROPERTIES::OnResize ) );
114}
115
116
118{
119 // Copy current barcode into our working copy
120 if( m_currentBarcode )
122
123 m_dummyBarcode->AssembleBarcode();
124
126
127 // Configure the layers list selector. Note that footprints are built outside the current
128 // board and so we may need to show all layers if the barcode is on an unactivated layer.
129 if( !m_parent->GetBoard()->IsLayerEnabled( m_dummyBarcode->GetLayer() ) )
130 m_cbLayer->ShowNonActivatedLayers( true );
131
132 m_cbLayer->SetLayersHotkeys( false );
133 m_cbLayer->SetBoardFrame( m_parent );
134 m_cbLayer->Resync();
135}
136
137
138void DIALOG_BARCODE_PROPERTIES::OnResize( wxSizeEvent& event )
139{
140 m_panelShowBarcodeGal->Refresh();
141 event.Skip();
142}
143
144
145void DIALOG_BARCODE_PROPERTIES::OnUpdateUI( wxUpdateUIEvent& event )
146{
147 // Error correction options are only meaningful for QR codes
148 bool enableEC = m_barcode->GetSelection() >= to_underlying( BARCODE_T::QR_CODE );
149 m_errorCorrection->Enable( enableEC );
150
151 m_textSize.Enable( m_cbShowText->GetValue() );
152
153 m_knockoutMarginX.Enable( m_cbKnockout->GetValue() );
154 m_knockoutMarginY.Enable( m_cbKnockout->GetValue() );
155
156 if( enableEC )
157 {
158 // Micro QR codes do not support High (H) error correction level
159 bool isMicroQR = ( m_barcode->GetSelection() == to_underlying( BARCODE_T::MICRO_QR_CODE ) );
160
161 // Enable/disable the High option (index 3)
162 m_errorCorrection->Enable( 3, !isMicroQR );
163
164 // If currently High is selected and we switched to Micro QR, change to a valid option
165 if( isMicroQR && m_errorCorrection->GetSelection() == 3 )
166 {
167 m_errorCorrection->SetSelection( 2 ); // Default to Q (Quartile), consistent with SetErrorCorrection
168 }
169 }
170}
171
172
174{
175 initValues();
176
177 if( m_currentBarcode )
179
180 m_textInput->ChangeValue( m_dummyBarcode->GetText() );
181 m_cbLocked->SetValue( m_dummyBarcode->IsLocked() );
182 m_cbLayer->SetLayerSelection( m_dummyBarcode->GetLayer() );
183
184 // Position
185 m_posX.ChangeValue( m_dummyBarcode->GetPosition().x );
186 m_posY.ChangeValue( m_dummyBarcode->GetPosition().y );
187
188 // Size
189 m_sizeX.ChangeValue( m_dummyBarcode->GetWidth() );
190 m_sizeY.ChangeValue( m_dummyBarcode->GetHeight() );
191 m_textSize.ChangeValue( m_dummyBarcode->GetTextSize() );
192
193 // Orientation
194 m_orientation.ChangeAngleValue( m_dummyBarcode->GetAngle() );
195
196 // Show text option
197 m_cbShowText->SetValue( m_dummyBarcode->Text().IsVisible() );
198
199 m_cbKnockout->SetValue( m_dummyBarcode->IsKnockout() );
200 m_knockoutMarginX.ChangeValue( m_dummyBarcode->GetMargin().x );
201 m_knockoutMarginY.ChangeValue( m_dummyBarcode->GetMargin().y );
202
203 // Barcode type
204 switch( m_dummyBarcode->GetKind() )
205 {
206 case BARCODE_T::CODE_39: m_barcode->SetSelection( 0 ); break;
207 case BARCODE_T::CODE_128: m_barcode->SetSelection( 1 ); break;
208 case BARCODE_T::DATA_MATRIX: m_barcode->SetSelection( 2 ); break;
209 case BARCODE_T::QR_CODE: m_barcode->SetSelection( 3 ); break;
210 case BARCODE_T::MICRO_QR_CODE: m_barcode->SetSelection( 4 ); break;
211 default: m_barcode->SetSelection( 0 ); break;
212 }
213
214 // Error correction level
215 switch( m_dummyBarcode->GetErrorCorrection() )
216 {
217 case BARCODE_ECC_T::L: m_errorCorrection->SetSelection( 0 ); break;
218 case BARCODE_ECC_T::M: m_errorCorrection->SetSelection( 1 ); break;
219 case BARCODE_ECC_T::Q: m_errorCorrection->SetSelection( 2 ); break;
220 case BARCODE_ECC_T::H: m_errorCorrection->SetSelection( 3 ); break;
221 default: m_errorCorrection->SetSelection( 0 ); break;
222 }
223
224 // Now all widgets have the size fixed, call finishDialogSettings
226
228
229 wxUpdateUIEvent dummy;
230 OnUpdateUI( dummy );
231
232 return true;
233}
234
235
237{
238 BOARD_COMMIT commit( m_parent );
239 commit.Modify( m_currentBarcode );
240
242 return false;
243
244 m_parent->GetCanvas()->Refresh();
245
246 commit.Push( _( "Modify barcode" ) );
247
248 return true;
249}
250
251
253{
254 if( !aBarcode )
255 return false;
256
257 aBarcode->SetText( m_textInput->GetValue() );
258 aBarcode->SetLocked( m_cbLocked->GetValue() );
259 aBarcode->SetLayer( ToLAYER_ID( m_cbLayer->GetLayerSelection() ) );
260
261 // Position
262 aBarcode->SetPosition( VECTOR2I( m_posX.GetIntValue(), m_posY.GetIntValue() ) );
263
264 // Size
265 aBarcode->SetWidth( m_sizeX.GetIntValue() );
266 aBarcode->SetHeight( m_sizeY.GetIntValue() );
267 aBarcode->SetTextSize( m_textSize.GetIntValue() );
268
269 // Orientation
270 EDA_ANGLE oldAngle = aBarcode->GetAngle();
271 EDA_ANGLE newAngle = m_orientation.GetAngleValue();
272
273 if( newAngle != oldAngle )
274 aBarcode->Rotate( aBarcode->GetPosition(), newAngle - oldAngle );
275
276 // Knockout
277 aBarcode->SetIsKnockout( m_cbKnockout->GetValue() );
278 aBarcode->SetMargin( VECTOR2I( m_knockoutMarginX.GetIntValue(), m_knockoutMarginY.GetIntValue() ) );
279
280 // Show text
281 aBarcode->Text().SetVisible( m_cbShowText->GetValue() );
282
283 // Barcode kind
284 switch( m_barcode->GetSelection() )
285 {
286 case 0: aBarcode->SetKind( BARCODE_T::CODE_39 ); break;
287 case 1: aBarcode->SetKind( BARCODE_T::CODE_128 ); break;
288 case 2: aBarcode->SetKind( BARCODE_T::DATA_MATRIX ); break;
289 case 3: aBarcode->SetKind( BARCODE_T::QR_CODE ); break;
290 case 4: aBarcode->SetKind( BARCODE_T::MICRO_QR_CODE ); break;
291 default: aBarcode->SetKind( BARCODE_T::QR_CODE ); break;
292 }
293
294 switch( m_errorCorrection->GetSelection() )
295 {
296 case 0: aBarcode->SetErrorCorrection( BARCODE_ECC_T::L ); break;
297 case 1: aBarcode->SetErrorCorrection( BARCODE_ECC_T::M ); break;
298 case 2: aBarcode->SetErrorCorrection( BARCODE_ECC_T::Q ); break;
299 case 3: aBarcode->SetErrorCorrection( BARCODE_ECC_T::H ); break;
300 default: aBarcode->SetErrorCorrection( BARCODE_ECC_T::L ); break;
301 }
302
303 aBarcode->AssembleBarcode();
304
305 return true;
306}
307
308
310{
311 KIGFX::VIEW* view = m_panelShowBarcodeGal->GetView();
312
313 // Compute the polygon bbox for the current dummy barcode (symbol + text/knockout as applicable)
314 const SHAPE_POLY_SET& poly = m_dummyBarcode->GetPolyShape();
315
316 if( poly.OutlineCount() > 0 )
317 {
318 BOX2I bbI = poly.BBox();
319
320 // Autozoom
321 view->SetViewport( BOX2D( bbI.GetOrigin(), bbI.GetSize() ) );
322
323 // Add a margin
324 view->SetScale( view->GetScale() * 0.7 );
325 }
326
327 view->SetCenter( VECTOR2D( m_dummyBarcode->GetPosition() ) );
328 view->Update( m_dummyBarcode );
329 m_panelShowBarcodeGal->Refresh();
330}
331
332
334{
336 {
338 OnModify();
339 }
340}
341
342
344{
346 {
348 OnModify();
349 }
350}
351
352
354{
355 DIALOG_BARCODE_PROPERTIES dlg( this, aBarcode );
356 dlg.ShowModal();
357}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
BOX2< VECTOR2D > BOX2D
Definition box2.h:923
virtual void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
void SetLocked(bool aLocked) override
Definition board_item.h:327
constexpr const Vec & GetOrigin() const
Definition box2.h:210
constexpr const SizeVec & GetSize() const
Definition box2.h:206
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:106
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
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:398
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:66
double GetScale() const
Definition view.h:276
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Set the scaling factor, zooming around a given anchor point.
Definition view.cpp:571
void SetViewport(const BOX2D &aViewport)
Set the visible area of the VIEW.
Definition view.cpp:543
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition view.cpp:298
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:1696
GAL * GetGAL() const
Return the GAL this view is using to draw graphical primitives.
Definition view.h:202
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition view.cpp:597
PCB_TEXT & Text()
Access the internal PCB_TEXT object used for showing the human-readable text.
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 SetWidth(int aWidth)
void SetHeight(int aHeight)
void AssembleBarcode()
Assemble the barcode polygon and text polygons into a single polygonal representation.
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:754
BARCODE class definition.
std::vector< FAB_LAYER_COLOR > dummy
constexpr auto to_underlying(E e) noexcept
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695
VECTOR2< double > VECTOR2D
Definition vector2d.h:694