1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2013 Dick Hollenbeck, dick@softplc.com
6  * Copyright (C) 2008-2013 Wayne Stambaugh <stambaughw@gmail.com>
7  * Copyright (C) 1992-2021 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 #ifndef _DIALOG_PAD_PROPERTIES_H_
28 #define _DIALOG_PAD_PROPERTIES_H_
29 
30 #include <pcb_base_frame.h>
31 #include <base_units.h>
32 #include <wx/valnum.h>
33 #include <board.h>
34 #include <footprint.h>
35 #include <pad_shapes.h>
36 #include <pcb_shape.h>
37 #include <origin_viewitem.h>
38 #include <dialog_pad_properties_base.h>
39 #include <widgets/text_ctrl_eval.h>
40 #include <pcb_draw_panel_gal.h>
41 #include <widgets/unit_binder.h>
42 
43 /**
44  * DIALOG_PAD_PROPERTIES, derived from DIALOG_PAD_PROPERTIES_BASE,
45  * created by wxFormBuilder
46  */
47 // The wxWidgets window name. Used to retrieve the dialog by window name
48 #define PAD_PROPERTIES_DLG_NAME "pad_properties_dlg_name"
49 
50 class DIALOG_PAD_PROPERTIES : public DIALOG_PAD_PROPERTIES_BASE
51 {
52 public:
53     DIALOG_PAD_PROPERTIES( PCB_BASE_FRAME* aParent, PAD* aPad );
54     ~DIALOG_PAD_PROPERTIES();
55 
56 private:
57     void prepareCanvas();       // Initialize the canvases (legacy or gal) to display the pad
58     void initValues();
59     void displayPrimitivesList();
60     bool padValuesOK();         ///< test if all values are acceptable for the pad
61     void redraw();
62     void editPrimitive();
63     void updateRoundRectCornerValues();
64     void enablePrimitivePage( bool aEnable );   ///< enable (or disable) the primitive page editor
65 
66     /**
67      * Updates the CheckBox states in pad layers list, based on the layer_mask (if non-empty)
68      * or the default layers for the current pad type.
69      */
70     void updatePadLayersList( LSET layer_mask, bool remove_unconnected, bool keep_top_bottom );
71 
72     /// Copy values from dialog field to aPad's members
73     bool transferDataToPad( PAD* aPad );
74 
75     bool Show( bool aShow ) override;
76 
77     // event handlers:
78     void OnInitDialog( wxInitDialogEvent& event ) override;
79     void OnResize( wxSizeEvent& event );
80 	void OnCancel( wxCommandEvent& event ) override;
81     void OnUpdateUI( wxUpdateUIEvent& event ) override;
82 
83     void OnUpdateUINonCopperWarning( wxUpdateUIEvent& event ) override;
84 
85     void OnPadShapeSelection( wxCommandEvent& event ) override;
86     void OnDrillShapeSelected( wxCommandEvent& event ) override;
87 	void onChangePadMode( wxCommandEvent& event ) override;
88 	void OnOffsetCheckbox( wxCommandEvent& event ) override;
89 	void OnPadToDieCheckbox( wxCommandEvent& event ) override;
90 
91     void PadOrientEvent( wxCommandEvent& event ) override;
92     void PadTypeSelected( wxCommandEvent& event ) override;
93 
94     void UpdateLayersDropdown();
95     void OnSetCopperLayers( wxCommandEvent& event ) override;
96     void OnSetLayers( wxCommandEvent& event ) override;
97 
98     // Called when corner setup value is changed for rounded rect pads
99     void onCornerSizePercentChange( wxCommandEvent& event ) override;
100     void onCornerRadiusChange( wxCommandEvent& event ) override;
101 
102     /// Called when a dimension has changed.
103     /// Update the graphical pad shown in the panel.
104     void OnValuesChanged( wxCommandEvent& event ) override;
105 
106     /// Updates the different parameters for the component being edited.
107     /// Automatically fired from the OK button click.
108 
109     bool TransferDataFromWindow() override;
110     bool TransferDataToWindow() override;
111 
112     /// Event handlers of basic shapes list panel
113     void onDeletePrimitive( wxCommandEvent& event ) override;
114     void onEditPrimitive( wxCommandEvent& event ) override;
115     void onAddPrimitive( wxCommandEvent& event ) override;
116     void onGeometryTransform( wxCommandEvent& event ) override;
117     void onDuplicatePrimitive( wxCommandEvent& event ) override;
118 
119     /// Called on a double click on the basic shapes list
120     void onPrimitiveDClick( wxMouseEvent& event ) override;
121     /// Called on selection/deselection of a basic shape
122 	void OnPrimitiveSelection( wxListEvent& event ) override;
123 
124     /// Return the pad property currently selected
125     PAD_PROP getSelectedProperty();
126 
127 private:
128     PCB_BASE_FRAME* m_parent;
129     PAD*    m_currentPad;           // pad currently being edited
130     PAD*    m_dummyPad;             // a working copy used to show changes
131     PAD*    m_padMaster;            // pad used to create new pads in board or footprint editor
132     BOARD*  m_board;                // the main board: this is the board handled by the PCB
133                                     //    editor or the dummy board used by the footprint editor
134     bool    m_isFlipped;            // indicates the parent footprint is flipped (mirrored) in
135                                     //    which case some Y coordinates values must be negated
136     bool    m_canUpdate;
137     bool    m_canEditNetName;       // true only if the caller is the board editor
138     bool    m_isFpEditor;           // true if the caller is the footprint editor
139 
140     std::vector<std::shared_ptr<PCB_SHAPE>> m_primitives;     // the custom shape primitives in
141                                                               // local coords, orient 0
142                                                               // must define a single copper area
143     COLOR4D                       m_selectedColor; // color used to draw selected primitives when
144                                                    //     editing a custom pad shape
145 
146     std::vector<PCB_SHAPE*>       m_highlight;     // shapes highlighted in GAL mode
147     PCB_DRAW_PANEL_GAL*           m_padPreviewGAL;
148     KIGFX::ORIGIN_VIEWITEM*       m_axisOrigin;    // origin of the preview canvas
149     static bool                   m_sketchPreview; // session storage
150 
151     UNIT_BINDER m_posX, m_posY;
152     UNIT_BINDER m_sizeX, m_sizeY;
153     UNIT_BINDER m_offsetX, m_offsetY;
154     UNIT_BINDER m_padToDie;
155     UNIT_BINDER m_trapDelta;
156     UNIT_BINDER m_cornerRadius;
157     UNIT_BINDER m_cornerRatio;
158     UNIT_BINDER m_chamferRatio;
159     UNIT_BINDER m_mixedCornerRatio, m_mixedChamferRatio;
160     UNIT_BINDER m_holeX, m_holeY;
161     UNIT_BINDER m_clearance;
162     UNIT_BINDER m_maskMargin;
163     UNIT_BINDER m_pasteMargin, m_pasteMarginRatio;
164     UNIT_BINDER m_spokeWidth, m_thermalGap;
165     UNIT_BINDER m_pad_orientation;
166 };
167 
168 /**
169  * A dialog to edit basic shape parameters.
170  *
171  * Polygonal shape is not handled by this dialog.
172  */
173 class DIALOG_PAD_PRIMITIVES_PROPERTIES: public DIALOG_PAD_PRIMITIVES_PROPERTIES_BASE
174 {
175 public:
176     DIALOG_PAD_PRIMITIVES_PROPERTIES( wxWindow* aParent, PCB_BASE_FRAME* aFrame,
177                                       PCB_SHAPE* aShape );
178 
179     /**
180      * Transfer data out of the GUI.
181      */
182     bool TransferDataFromWindow() override;
183 
184 private:
185     /**
186      * Function TransferDataToWindow
187      * Transfer data into the GUI.
188      */
189     bool TransferDataToWindow() override;
190 
191     // The basic shape currently edited
192     PCB_SHAPE*   m_shape;
193 
194     UNIT_BINDER  m_startX;
195     UNIT_BINDER  m_startY;
196     UNIT_BINDER  m_ctrl1X;
197     UNIT_BINDER  m_ctrl1Y;
198     UNIT_BINDER  m_ctrl2X;
199     UNIT_BINDER  m_ctrl2Y;
200     UNIT_BINDER  m_endX;
201     UNIT_BINDER  m_endY;
202     UNIT_BINDER  m_radius;
203     UNIT_BINDER  m_thickness;
204 };
205 
206 
207 /**
208  * A dialog to edit basic polygonal shape parameters.
209  */
210 class DIALOG_PAD_PRIMITIVE_POLY_PROPS: public DIALOG_PAD_PRIMITIVE_POLY_PROPS_BASE
211 {
212 public:
213     DIALOG_PAD_PRIMITIVE_POLY_PROPS( wxWindow* aParent, PCB_BASE_FRAME* aFrame,
214                                      PCB_SHAPE* aShape );
215     ~DIALOG_PAD_PRIMITIVE_POLY_PROPS();
216 
217     /**
218      * Transfer data out of the GUI.
219      */
220     bool TransferDataFromWindow() override;
221 
222 private:
223     /**
224      * Transfer data into the GUI.
225      */
226     bool TransferDataToWindow() override;
227 
228     /**
229      * Test for a valid polygon (a not self intersectiong polygon).
230      */
231     bool Validate() override;
232 
233     // Events handlers:
234     void OnButtonAdd( wxCommandEvent& event ) override;
235     void OnButtonDelete( wxCommandEvent& event ) override;
236     void onPaintPolyPanel( wxPaintEvent& event ) override;
237     void onPolyPanelResize( wxSizeEvent& event ) override;
238     void onGridSelect( wxGridRangeSelectEvent& event ) override;
239     void onCellChanging( wxGridEvent& event );
onCellSelect(wxGridEvent & event)240     void onCellSelect( wxGridEvent& event ) override
241     {
242         event.Skip();
243     }
244 
245     bool doValidate( bool aRemoveRedundantCorners );
246 
247     // The basic shape currently edited
248     PCB_SHAPE*           m_shape;
249 
250     // The working copy of the basic shape currently edited
251     std::vector<wxPoint> m_currPoints;
252 
253     UNIT_BINDER          m_thickness;
254 };
255 
256 
257 /**
258  * A dialog to apply geometry transforms to a shape or set of shapes (move, rotate around
259  * origin, scaling factor, duplication).
260  *
261  * Shapes are scaled then moved then rotated.
262  */
263 
264 class DIALOG_PAD_PRIMITIVES_TRANSFORM : public DIALOG_PAD_PRIMITIVES_TRANSFORM_BASE
265 {
266 public:
267     DIALOG_PAD_PRIMITIVES_TRANSFORM( wxWindow* aParent, PCB_BASE_FRAME* aFrame,
268                                      std::vector<std::shared_ptr<PCB_SHAPE>>& aList,
269                                      bool aShowDuplicate );
270 
271     /**
272      * Apply geometric transform (rotation, move, scale) defined in dialog
273      * aDuplicate = 1 .. n to duplicate the list of shapes
274      * aDuplicate = 0 to transform the list of shapes
275      * The duplicated items are transformed, but the initial shpes are not modified.
276      * The duplicated items are added to aList
277      */
278     void Transform( std::vector<std::shared_ptr<PCB_SHAPE>>* aList = nullptr,
279                     int aDuplicateCount = 0 );
280 
281     /**
282      * @return the number of duplicate, chosen by user.
283      */
GetDuplicateCount()284     int GetDuplicateCount() { return m_spinCtrlDuplicateCount->GetValue(); }
285 
286 private:
287     std::vector<std::shared_ptr<PCB_SHAPE>>& m_list;
288 
289     UNIT_BINDER  m_vectorX;
290     UNIT_BINDER  m_vectorY;
291     UNIT_BINDER  m_rotation;
292 };
293 
294 #endif      // #ifndef _DIALOG_PAD_PROPERTIES_H_
295