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#include <wx/msgdlg.h>
46// For BOX2D viewport checks
47#include <math/box2.h>
48
49
52 m_parent( aParent ),
61{
62 m_currentBarcode = aBarcode; // aBarcode can not be NULL (no FOOTPRINT editor?)
63
64 m_board = m_parent->GetBoard();
65
66 // Use the board as parent so text variables like ${PART_NUMBER} can be resolved
67 // in the preview. The dummy barcode is not actually added to the board.
69
70 // Initialize canvas to be able to display the dummy barcode:
72
73 m_sdbSizerOK->SetDefault();
74}
75
76
81
82
83void DIALOG_BARCODE_PROPERTIES::OnCancel( wxCommandEvent& event )
84{
85 // Mandatory to avoid m_panelShowPadGal trying to draw something
86 // in a non valid context during closing process:
87 m_panelShowBarcodeGal->StopDrawing();
88
89 // Now call default handler for wxID_CANCEL command event
90 event.Skip();
91}
92
93
95{
96 // Initialize the canvas to display the barcode
97
98 m_panelShowBarcodeGal->UpdateColors();
99 m_panelShowBarcodeGal->SwitchBackend( m_parent->GetCanvas()->GetBackend() );
100 m_panelShowBarcodeGal->SetStealsFocus( false );
101
102 m_panelShowBarcodeGal->Show();
103
104 KIGFX::VIEW* view = m_panelShowBarcodeGal->GetView();
105
106 // gives a non null grid size (0.001mm) because GAL layer does not like a 0 size grid:
107 double gridsize = pcbIUScale.mmToIU( 0.001 );
108 view->GetGAL()->SetGridSize( VECTOR2D( gridsize, gridsize ) );
109 // And do not show the grid:
110 view->GetGAL()->SetGridVisibility( false );
111 view->Add( m_dummyBarcode );
112
113 m_panelShowBarcodeGal->StartDrawing();
114 Connect( wxEVT_SIZE, wxSizeEventHandler( DIALOG_BARCODE_PROPERTIES::OnResize ) );
115}
116
117
119{
120 // Copy current barcode into our working copy
121 if( m_currentBarcode )
123
124 m_dummyBarcode->AssembleBarcode();
125
127
128 // Configure the layers list selector. Note that footprints are built outside the current
129 // board and so we may need to show all layers if the barcode is on an unactivated layer.
130 if( !m_parent->GetBoard()->IsLayerEnabled( m_dummyBarcode->GetLayer() ) )
131 m_cbLayer->ShowNonActivatedLayers( true );
132
133 m_cbLayer->SetLayersHotkeys( false );
134 m_cbLayer->SetBoardFrame( m_parent );
135 m_cbLayer->Resync();
136}
137
138
139void DIALOG_BARCODE_PROPERTIES::OnResize( wxSizeEvent& event )
140{
141 m_panelShowBarcodeGal->Refresh();
142 event.Skip();
143}
144
145
146void DIALOG_BARCODE_PROPERTIES::OnUpdateUI( wxUpdateUIEvent& event )
147{
148 // Error correction options are only meaningful for QR codes
149 bool enableEC = m_barcode->GetSelection() >= to_underlying( BARCODE_T::QR_CODE );
150 m_errorCorrection->Enable( enableEC );
151
152 m_textSize.Enable( m_cbShowText->GetValue() );
153
154 m_knockoutMarginX.Enable( m_cbKnockout->GetValue() );
155 m_knockoutMarginY.Enable( m_cbKnockout->GetValue() );
156
157 if( enableEC )
158 {
159 // Micro QR codes do not support High (H) error correction level
160 bool isMicroQR = ( m_barcode->GetSelection() == to_underlying( BARCODE_T::MICRO_QR_CODE ) );
161
162 // Enable/disable the High option (index 3)
163 m_errorCorrection->Enable( 3, !isMicroQR );
164
165 // If currently High is selected and we switched to Micro QR, change to a valid option
166 if( isMicroQR && m_errorCorrection->GetSelection() == 3 )
167 {
168 m_errorCorrection->SetSelection( 2 ); // Default to Q (Quartile), consistent with SetErrorCorrection
169 }
170 }
171}
172
173
175{
176 initValues();
177
178 if( m_currentBarcode )
180
181 m_textInput->ChangeValue( m_dummyBarcode->GetText() );
182 m_cbLocked->SetValue( m_dummyBarcode->IsLocked() );
183 m_cbLayer->SetLayerSelection( m_dummyBarcode->GetLayer() );
184
185 // Position
186 m_posX.ChangeValue( m_dummyBarcode->GetPosition().x );
187 m_posY.ChangeValue( m_dummyBarcode->GetPosition().y );
188
189 // Size
190 m_sizeX.ChangeValue( m_dummyBarcode->GetWidth() );
191 m_sizeY.ChangeValue( m_dummyBarcode->GetHeight() );
192 m_textSize.ChangeValue( m_dummyBarcode->GetTextSize() );
193
194 // Orientation
195 m_orientation.ChangeAngleValue( m_dummyBarcode->GetAngle() );
196
197 // Show text option
198 m_cbShowText->SetValue( m_dummyBarcode->GetShowText() );
199
200 m_cbKnockout->SetValue( m_dummyBarcode->IsKnockout() );
201 m_knockoutMarginX.ChangeValue( m_dummyBarcode->GetMargin().x );
202 m_knockoutMarginY.ChangeValue( m_dummyBarcode->GetMargin().y );
203
204 // Barcode type
205 switch( m_dummyBarcode->GetKind() )
206 {
207 case BARCODE_T::CODE_39: m_barcode->SetSelection( 0 ); break;
208 case BARCODE_T::CODE_128: m_barcode->SetSelection( 1 ); break;
209 case BARCODE_T::DATA_MATRIX: m_barcode->SetSelection( 2 ); break;
210 case BARCODE_T::QR_CODE: m_barcode->SetSelection( 3 ); break;
211 case BARCODE_T::MICRO_QR_CODE: m_barcode->SetSelection( 4 ); break;
212 default: m_barcode->SetSelection( 0 ); break;
213 }
214
215 // Error correction level
216 switch( m_dummyBarcode->GetErrorCorrection() )
217 {
218 case BARCODE_ECC_T::L: m_errorCorrection->SetSelection( 0 ); break;
219 case BARCODE_ECC_T::M: m_errorCorrection->SetSelection( 1 ); break;
220 case BARCODE_ECC_T::Q: m_errorCorrection->SetSelection( 2 ); break;
221 case BARCODE_ECC_T::H: m_errorCorrection->SetSelection( 3 ); break;
222 default: m_errorCorrection->SetSelection( 0 ); break;
223 }
224
225 // Now all widgets have the size fixed, call finishDialogSettings
227
229
230 wxUpdateUIEvent dummy;
231 OnUpdateUI( dummy );
232
233 return true;
234}
235
236
238{
240
241 if( !m_dummyBarcode->GetText().empty() && m_dummyBarcode->GetSymbolPoly().OutlineCount() == 0 )
242 {
243 wxMessageBox( m_dummyBarcode->GetLastError(), _( "Barcode Error" ), wxOK | wxICON_ERROR, this );
244 return false;
245 }
246
247 BOARD_COMMIT commit( m_parent );
248 commit.Modify( m_currentBarcode );
249
251 return false;
252
253 m_parent->GetCanvas()->Refresh();
254
255 commit.Push( _( "Modify barcode" ) );
256
257 return true;
258}
259
260
262{
263 if( !aBarcode )
264 return false;
265
266 aBarcode->SetText( m_textInput->GetValue() );
267 aBarcode->SetLocked( m_cbLocked->GetValue() );
268 aBarcode->SetLayer( ToLAYER_ID( m_cbLayer->GetLayerSelection() ) );
269
270 // Position
271 aBarcode->SetPosition( VECTOR2I( m_posX.GetIntValue(), m_posY.GetIntValue() ) );
272
273 // Size
274 aBarcode->SetWidth( m_sizeX.GetIntValue() );
275 aBarcode->SetHeight( m_sizeY.GetIntValue() );
276 aBarcode->SetTextSize( m_textSize.GetIntValue() );
277
278 // Orientation
279 EDA_ANGLE oldAngle = aBarcode->GetAngle();
280 EDA_ANGLE newAngle = m_orientation.GetAngleValue();
281
282 if( newAngle != oldAngle )
283 aBarcode->Rotate( aBarcode->GetPosition(), newAngle - oldAngle );
284
285 // Knockout
286 aBarcode->SetIsKnockout( m_cbKnockout->GetValue() );
287 aBarcode->SetMargin( VECTOR2I( m_knockoutMarginX.GetIntValue(), m_knockoutMarginY.GetIntValue() ) );
288
289 // Show text
290 aBarcode->SetShowText( m_cbShowText->GetValue() );
291
292 // Barcode kind
293 switch( m_barcode->GetSelection() )
294 {
295 case 0: aBarcode->SetKind( BARCODE_T::CODE_39 ); break;
296 case 1: aBarcode->SetKind( BARCODE_T::CODE_128 ); break;
297 case 2: aBarcode->SetKind( BARCODE_T::DATA_MATRIX ); break;
298 case 3: aBarcode->SetKind( BARCODE_T::QR_CODE ); break;
299 case 4: aBarcode->SetKind( BARCODE_T::MICRO_QR_CODE ); break;
300 default: aBarcode->SetKind( BARCODE_T::QR_CODE ); break;
301 }
302
303 switch( m_errorCorrection->GetSelection() )
304 {
305 case 0: aBarcode->SetErrorCorrection( BARCODE_ECC_T::L ); break;
306 case 1: aBarcode->SetErrorCorrection( BARCODE_ECC_T::M ); break;
307 case 2: aBarcode->SetErrorCorrection( BARCODE_ECC_T::Q ); break;
308 case 3: aBarcode->SetErrorCorrection( BARCODE_ECC_T::H ); break;
309 default: aBarcode->SetErrorCorrection( BARCODE_ECC_T::L ); break;
310 }
311
312 aBarcode->AssembleBarcode();
313
314 return true;
315}
316
317
319{
320 KIGFX::VIEW* view = m_panelShowBarcodeGal->GetView();
321
322 // Compute the polygon bbox for the current dummy barcode (symbol + text/knockout as applicable)
323 const SHAPE_POLY_SET& poly = m_dummyBarcode->GetPolyShape();
324
325 if( poly.OutlineCount() > 0 )
326 {
327 BOX2I bbI = poly.BBox();
328
329 // Autozoom
330 view->SetViewport( BOX2D( bbI.GetOrigin(), bbI.GetSize() ) );
331
332 // Add a margin
333 view->SetScale( view->GetScale() * 0.7 );
334 }
335
336 view->SetCenter( VECTOR2D( m_dummyBarcode->GetPosition() ) );
337 view->Update( m_dummyBarcode );
338 m_panelShowBarcodeGal->Refresh();
339}
340
341
343{
345 {
347 OnModify();
348 }
349}
350
351
353{
355 {
357 OnModify();
358 }
359}
360
361
363{
364 DIALOG_BARCODE_PROPERTIES dlg( this, aBarcode );
365 dlg.ShowModal();
366}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:125
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:359
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
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:67
double GetScale() const
Definition view.h:285
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Set the scaling factor, zooming around a given anchor point.
Definition view.cpp:641
void SetViewport(const BOX2D &aViewport)
Set the visible area of the VIEW.
Definition view.cpp:613
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition view.cpp:304
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:1828
GAL * GetGAL() const
Return the GAL this view is using to draw graphical primitives.
Definition view.h:211
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition view.cpp:667
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:754
BARCODE class definition.
std::vector< FAB_LAYER_COLOR > dummy
constexpr auto to_underlying(E e) noexcept
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687
VECTOR2< double > VECTOR2D
Definition vector2d.h:686