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 m_dummyBarcode = new PCB_BARCODE( nullptr );
66
67 // Initialize canvas to be able to display the dummy barcode:
69
70 m_sdbSizerOK->SetDefault();
71}
72
73
78
79
80void DIALOG_BARCODE_PROPERTIES::OnCancel( wxCommandEvent& event )
81{
82 // Mandatory to avoid m_panelShowPadGal trying to draw something
83 // in a non valid context during closing process:
84 m_panelShowBarcodeGal->StopDrawing();
85
86 // Now call default handler for wxID_CANCEL command event
87 event.Skip();
88}
89
90
92{
93 // Initialize the canvas to display the barcode
94
95 m_panelShowBarcodeGal->UpdateColors();
96 m_panelShowBarcodeGal->SwitchBackend( m_parent->GetCanvas()->GetBackend() );
97 m_panelShowBarcodeGal->SetStealsFocus( false );
98
100
101 KIGFX::VIEW* view = m_panelShowBarcodeGal->GetView();
102
103 // gives a non null grid size (0.001mm) because GAL layer does not like a 0 size grid:
104 double gridsize = pcbIUScale.mmToIU( 0.001 );
105 view->GetGAL()->SetGridSize( VECTOR2D( gridsize, gridsize ) );
106 // And do not show the grid:
107 view->GetGAL()->SetGridVisibility( false );
108 view->Add( m_dummyBarcode );
109
110 m_panelShowBarcodeGal->StartDrawing();
111 Connect( wxEVT_SIZE, wxSizeEventHandler( DIALOG_BARCODE_PROPERTIES::OnResize ) );
112}
113
114
116{
117 // Copy current barcode into our working copy
118 if( m_currentBarcode )
120
121 m_dummyBarcode->AssembleBarcode();
122
124
125 // Configure the layers list selector. Note that footprints are built outside the current
126 // board and so we may need to show all layers if the barcode is on an unactivated layer.
127 if( !m_parent->GetBoard()->IsLayerEnabled( m_dummyBarcode->GetLayer() ) )
128 m_cbLayer->ShowNonActivatedLayers( true );
129
130 m_cbLayer->SetLayersHotkeys( false );
131 m_cbLayer->SetBoardFrame( m_parent );
132 m_cbLayer->Resync();
133}
134
135
136void DIALOG_BARCODE_PROPERTIES::OnResize( wxSizeEvent& event )
137{
138 m_panelShowBarcodeGal->Refresh();
139 event.Skip();
140}
141
142
143void DIALOG_BARCODE_PROPERTIES::OnUpdateUI( wxUpdateUIEvent& event )
144{
145 // Error correction options are only meaningful for QR codes
146 bool enableEC = m_barcode->GetSelection() >= to_underlying( BARCODE_T::QR_CODE );
147 m_errorCorrection->Enable( enableEC );
148
149 m_textSize.Enable( m_cbShowText->GetValue() );
150
151 m_knockoutMarginX.Enable( m_cbKnockout->GetValue() );
152 m_knockoutMarginY.Enable( m_cbKnockout->GetValue() );
153
154 if( enableEC )
155 {
156 // Micro QR codes do not support High (H) error correction level
157 bool isMicroQR = ( m_barcode->GetSelection() == to_underlying( BARCODE_T::MICRO_QR_CODE ) );
158
159 // Enable/disable the High option (index 3)
160 m_errorCorrection->Enable( 3, !isMicroQR );
161
162 // If currently High is selected and we switched to Micro QR, change to a valid option
163 if( isMicroQR && m_errorCorrection->GetSelection() == 3 )
164 {
165 m_errorCorrection->SetSelection( 2 ); // Default to Q (Quartile), consistent with SetErrorCorrection
166 }
167 }
168}
169
170
172{
173 initValues();
174
175 if( m_currentBarcode )
177
178 m_textInput->ChangeValue( m_dummyBarcode->GetText() );
179 m_cbLocked->SetValue( m_dummyBarcode->IsLocked() );
180 m_cbLayer->SetLayerSelection( m_dummyBarcode->GetLayer() );
181
182 // Position
183 m_posX.ChangeValue( m_dummyBarcode->GetPosition().x );
184 m_posY.ChangeValue( m_dummyBarcode->GetPosition().y );
185
186 // Size
187 m_sizeX.ChangeValue( m_dummyBarcode->GetWidth() );
188 m_sizeY.ChangeValue( m_dummyBarcode->GetHeight() );
189 m_textSize.ChangeValue( m_dummyBarcode->GetTextSize() );
190
191 // Orientation
192 m_orientation.ChangeAngleValue( m_dummyBarcode->GetAngle() );
193
194 // Show text option
195 m_cbShowText->SetValue( m_dummyBarcode->Text().IsVisible() );
196
197 m_cbKnockout->SetValue( m_dummyBarcode->IsKnockout() );
198 m_knockoutMarginX.ChangeValue( m_dummyBarcode->GetMargin().x );
199 m_knockoutMarginY.ChangeValue( m_dummyBarcode->GetMargin().y );
200
201 // Barcode type
202 switch( m_dummyBarcode->GetKind() )
203 {
204 case BARCODE_T::CODE_39: m_barcode->SetSelection( 0 ); break;
205 case BARCODE_T::CODE_128: m_barcode->SetSelection( 1 ); break;
206 case BARCODE_T::DATA_MATRIX: m_barcode->SetSelection( 2 ); break;
207 case BARCODE_T::QR_CODE: m_barcode->SetSelection( 3 ); break;
208 case BARCODE_T::MICRO_QR_CODE: m_barcode->SetSelection( 4 ); break;
209 default: m_barcode->SetSelection( 0 ); break;
210 }
211
212 // Error correction level
213 switch( m_dummyBarcode->GetErrorCorrection() )
214 {
215 case BARCODE_ECC_T::L: m_errorCorrection->SetSelection( 0 ); break;
216 case BARCODE_ECC_T::M: m_errorCorrection->SetSelection( 1 ); break;
217 case BARCODE_ECC_T::Q: m_errorCorrection->SetSelection( 2 ); break;
218 case BARCODE_ECC_T::H: m_errorCorrection->SetSelection( 3 ); break;
219 default: m_errorCorrection->SetSelection( 0 ); break;
220 }
221
222 // Now all widgets have the size fixed, call finishDialogSettings
224
226
227 wxUpdateUIEvent dummy;
228 OnUpdateUI( dummy );
229
230 return true;
231}
232
233
235{
236 BOARD_COMMIT commit( m_parent );
237 commit.Modify( m_currentBarcode );
238
240 return false;
241
242 m_parent->GetCanvas()->Refresh();
243
244 commit.Push( _( "Modify barcode" ) );
245
246 return true;
247}
248
249
251{
252 if( !aBarcode )
253 return false;
254
255 aBarcode->SetText( m_textInput->GetValue() );
256 aBarcode->SetLocked( m_cbLocked->GetValue() );
257 aBarcode->SetLayer( ToLAYER_ID( m_cbLayer->GetLayerSelection() ) );
258
259 // Position
260 aBarcode->SetPosition( VECTOR2I( m_posX.GetIntValue(), m_posY.GetIntValue() ) );
261
262 // Size
263 aBarcode->SetWidth( m_sizeX.GetIntValue() );
264 aBarcode->SetHeight( m_sizeY.GetIntValue() );
265 aBarcode->SetTextSize( m_textSize.GetIntValue() );
266
267 // Orientation
268 EDA_ANGLE oldAngle = aBarcode->GetAngle();
269 EDA_ANGLE newAngle = m_orientation.GetAngleValue();
270
271 if( newAngle != oldAngle )
272 aBarcode->Rotate( aBarcode->GetPosition(), newAngle - oldAngle );
273
274 // Knockout
275 aBarcode->SetIsKnockout( m_cbKnockout->GetValue() );
276 aBarcode->SetMargin( VECTOR2I( m_knockoutMarginX.GetIntValue(), m_knockoutMarginY.GetIntValue() ) );
277
278 // Show text
279 aBarcode->Text().SetVisible( m_cbShowText->GetValue() );
280
281 // Barcode kind
282 switch( m_barcode->GetSelection() )
283 {
284 case 0: aBarcode->SetKind( BARCODE_T::CODE_39 ); break;
285 case 1: aBarcode->SetKind( BARCODE_T::CODE_128 ); break;
286 case 2: aBarcode->SetKind( BARCODE_T::DATA_MATRIX ); break;
287 case 3: aBarcode->SetKind( BARCODE_T::QR_CODE ); break;
288 case 4: aBarcode->SetKind( BARCODE_T::MICRO_QR_CODE ); break;
289 default: aBarcode->SetKind( BARCODE_T::QR_CODE ); break;
290 }
291
292 switch( m_errorCorrection->GetSelection() )
293 {
294 case 0: aBarcode->SetErrorCorrection( BARCODE_ECC_T::L ); break;
295 case 1: aBarcode->SetErrorCorrection( BARCODE_ECC_T::M ); break;
296 case 2: aBarcode->SetErrorCorrection( BARCODE_ECC_T::Q ); break;
297 case 3: aBarcode->SetErrorCorrection( BARCODE_ECC_T::H ); break;
298 default: aBarcode->SetErrorCorrection( BARCODE_ECC_T::L ); break;
299 }
300
301 aBarcode->AssembleBarcode();
302
303 return true;
304}
305
306
308{
309 KIGFX::VIEW* view = m_panelShowBarcodeGal->GetView();
310
311 // Compute the polygon bbox for the current dummy barcode (symbol + text/knockout as applicable)
312 const SHAPE_POLY_SET& poly = m_dummyBarcode->GetPolyShape();
313
314 if( poly.OutlineCount() > 0 )
315 {
316 BOX2I bbI = poly.BBox();
317
318 // Autozoom
319 view->SetViewport( BOX2D( bbI.GetOrigin(), bbI.GetSize() ) );
320
321 // Add a margin
322 view->SetScale( view->GetScale() * 0.7 );
323 }
324
325 view->SetCenter( VECTOR2D( m_dummyBarcode->GetPosition() ) );
326 view->Update( m_dummyBarcode );
327 m_panelShowBarcodeGal->Refresh();
328}
329
330
332{
334 {
336 OnModify();
337 }
338}
339
340
342{
343 DIALOG_BARCODE_PROPERTIES dlg( this, aBarcode );
344 dlg.ShowModal();
345}
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:323
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 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:397
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:570
void SetViewport(const BOX2D &aViewport)
Set the visible area of the VIEW.
Definition view.cpp:542
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:1685
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:596
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:737
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