1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2018-2021 KiCad Developers, see change_log.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
22  */
23 
24 #include <base_units.h>
25 #include <pcb_edit_frame.h>
26 #include <board_design_settings.h>
27 #include <widgets/wx_grid.h>
28 #include <grid_tricks.h>
29 #include <panel_setup_text_and_graphics.h>
30 
31 #include <wx/treebook.h>
32 
33 
34 // Columns of layer classes grid
35 enum
36 {
37     COL_LINE_THICKNESS = 0,
38     COL_TEXT_WIDTH,
39     COL_TEXT_HEIGHT,
40     COL_TEXT_THICKNESS,
41     COL_TEXT_ITALIC,
42     COL_TEXT_UPRIGHT
43 };
44 
45 enum
46 {
47     ROW_SILK = 0,
48     ROW_COPPER,
49     ROW_EDGES,
50     ROW_COURTYARD,
51     ROW_FAB,
52     ROW_OTHERS,
53 
54     ROW_COUNT
55 };
56 
57 
PANEL_SETUP_TEXT_AND_GRAPHICS(PAGED_DIALOG * aParent,PCB_EDIT_FRAME * aFrame)58 PANEL_SETUP_TEXT_AND_GRAPHICS::PANEL_SETUP_TEXT_AND_GRAPHICS( PAGED_DIALOG* aParent,
59                                                               PCB_EDIT_FRAME* aFrame ) :
60         PANEL_SETUP_TEXT_AND_GRAPHICS_BASE( aParent->GetTreebook() ),
61         m_arrowLength( aFrame, m_lblArrowLength, m_dimensionArrowLength, m_arrowLengthUnits ),
62         m_extensionOffset( aFrame, m_lblExtensionOffset, m_dimensionExtensionOffset,
63                            m_dimensionExtensionOffsetUnits )
64 {
65     m_Parent = aParent;
66     m_Frame = aFrame;
67     m_BrdSettings = &m_Frame->GetBoard()->GetDesignSettings();
68 
69     m_grid->SetDefaultRowSize( m_grid->GetDefaultRowSize() + 4 );
70 
71     // Work around a bug in wxWidgets where it fails to recalculate the grid height
72     // after changing the default row size
73     m_grid->AppendRows( 1 );
74     m_grid->DeleteRows( m_grid->GetNumberRows() - 1, 1 );
75 
76     // Gives a suitable size to m_grid columns
77     // The default wxWidget col size is not very good
78     // Calculate a min best size to handle longest usual numeric values:
79     int min_best_width = m_grid->GetTextExtent( "555,555555 mils" ).x;
80 
81     for( int i = 0; i < m_grid->GetNumberCols(); ++i )
82     {
83         // We calculate the column min size only from texts sizes, not using the initial col width
84         // as this initial width is sometimes strange depending on the language (wxGrid bug?)
85         int min_width =  m_grid->GetVisibleWidth( i, true, true, false );
86 
87         m_grid->SetColMinimalWidth( i, min_width );
88 
89         // We use a "best size" >= min_best_width
90         m_grid->SetColSize( i, std::max( min_width, min_best_width ) );
91     }
92 
93     m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
94 }
95 
96 
~PANEL_SETUP_TEXT_AND_GRAPHICS()97 PANEL_SETUP_TEXT_AND_GRAPHICS::~PANEL_SETUP_TEXT_AND_GRAPHICS()
98 {
99     // destroy GRID_TRICKS before m_grid.
100     m_grid->PopEventHandler( true );
101 }
102 
103 
TransferDataToWindow()104 bool PANEL_SETUP_TEXT_AND_GRAPHICS::TransferDataToWindow()
105 {
106     wxColour disabledColour = wxSystemSettings::GetColour( wxSYS_COLOUR_BACKGROUND );
107 
108 #define SET_MILS_CELL( row, col, val ) \
109     m_grid->SetCellValue( row, col, StringFromValue( m_Frame->GetUserUnits(), val, true ) )
110 
111 #define DISABLE_CELL( row, col ) \
112     m_grid->SetReadOnly( row, col ); m_grid->SetCellBackgroundColour( row, col, disabledColour );
113 
114     for( int i = 0; i < ROW_COUNT; ++i )
115     {
116         SET_MILS_CELL( i, COL_LINE_THICKNESS, m_BrdSettings->m_LineThickness[ i ] );
117 
118         if( i == ROW_EDGES || i == ROW_COURTYARD )
119         {
120             DISABLE_CELL( i, COL_TEXT_WIDTH );
121             DISABLE_CELL( i, COL_TEXT_HEIGHT );
122             DISABLE_CELL( i, COL_TEXT_THICKNESS );
123             DISABLE_CELL( i, COL_TEXT_ITALIC );
124             DISABLE_CELL( i, COL_TEXT_UPRIGHT );
125         }
126         else
127         {
128             SET_MILS_CELL( i, COL_TEXT_WIDTH, m_BrdSettings->m_TextSize[ i ].x );
129             SET_MILS_CELL( i, COL_TEXT_HEIGHT, m_BrdSettings->m_TextSize[ i ].y );
130             SET_MILS_CELL( i, COL_TEXT_THICKNESS, m_BrdSettings->m_TextThickness[ i ] );
131             m_grid->SetCellValue( i, COL_TEXT_ITALIC, m_BrdSettings->m_TextItalic[ i ] ? "1" : "" );
132             m_grid->SetCellValue( i, COL_TEXT_UPRIGHT, m_BrdSettings->m_TextUpright[ i ] ? "1" : "" );
133 
134             auto attr = new wxGridCellAttr;
135             attr->SetRenderer( new wxGridCellBoolRenderer() );
136             attr->SetReadOnly();    // not really; we delegate interactivity to GRID_TRICKS
137             attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
138             m_grid->SetAttr( i, COL_TEXT_ITALIC, attr );
139 
140             attr = new wxGridCellAttr;
141             attr->SetRenderer( new wxGridCellBoolRenderer() );
142             attr->SetReadOnly();    // not really; we delegate interactivity to GRID_TRICKS
143             attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
144             m_grid->SetAttr( i, COL_TEXT_UPRIGHT, attr );
145         }
146     }
147 
148     // Work around an issue where wxWidgets doesn't calculate the row width on its own
149     for( int col = 0; col < m_grid->GetNumberCols(); col++ )
150         m_grid->SetColMinimalWidth( col, m_grid->GetVisibleWidth( col, true, true, false ) );
151 
152     m_grid->SetRowLabelSize( m_grid->GetVisibleWidth( -1, true, true, true ) );
153 
154     Layout();
155 
156     wxASSERT_MSG( m_BrdSettings->m_DimensionPrecision <= 4, "Unhandled dimension precision!" );
157 
158     int mode = static_cast<int>( m_BrdSettings->m_DimensionUnitsMode );
159     m_dimensionUnits->SetSelection( mode );
160 
161     int format = static_cast<int>( m_BrdSettings->m_DimensionUnitsFormat );
162     m_dimensionUnitsFormat->SetSelection( format );
163 
164     m_dimensionPrecision->SetSelection( m_BrdSettings->m_DimensionPrecision );
165     m_dimensionSuppressZeroes->SetValue( m_BrdSettings->m_DimensionSuppressZeroes );
166 
167     int position = static_cast<int>( m_BrdSettings->m_DimensionTextPosition );
168     m_dimensionTextPositionMode->SetSelection( position );
169 
170     m_dimensionTextKeepAligned->SetValue( m_BrdSettings->m_DimensionKeepTextAligned );
171     m_arrowLength.SetValue( m_BrdSettings->m_DimensionArrowLength );
172     m_extensionOffset.SetValue( m_BrdSettings->m_DimensionExtensionOffset );
173 
174     return true;
175 }
176 
177 
getGridValue(int aRow,int aCol)178 int PANEL_SETUP_TEXT_AND_GRAPHICS::getGridValue( int aRow, int aCol )
179 {
180     return ValueFromString( m_Frame->GetUserUnits(), m_grid->GetCellValue( aRow, aCol ) );
181 }
182 
183 
TransferDataFromWindow()184 bool PANEL_SETUP_TEXT_AND_GRAPHICS::TransferDataFromWindow()
185 {
186     if( !m_grid->CommitPendingChanges() )
187         return false;
188 
189     for( int i = 0; i < ROW_COUNT; ++i )
190     {
191         m_BrdSettings->m_LineThickness[ i ] = getGridValue( i, COL_LINE_THICKNESS );
192 
193         if( i == ROW_EDGES || i == ROW_COURTYARD )
194             continue;
195 
196         m_BrdSettings->m_TextSize[ i ] = wxSize( getGridValue( i, COL_TEXT_WIDTH ),
197                                                  getGridValue( i, COL_TEXT_HEIGHT ) );
198         m_BrdSettings->m_TextThickness[ i ] = getGridValue( i, COL_TEXT_THICKNESS );
199         m_BrdSettings->m_TextItalic[ i ] =
200                 wxGridCellBoolEditor::IsTrueValue( m_grid->GetCellValue( i, COL_TEXT_ITALIC ) );
201         m_BrdSettings->m_TextUpright[ i ] =
202                 wxGridCellBoolEditor::IsTrueValue( m_grid->GetCellValue( i, COL_TEXT_UPRIGHT ) );
203     }
204 
205     // These are all stored in project file, not board, so no need for OnModify()
206 
207     int mode                                  = m_dimensionUnits->GetSelection();
208     m_BrdSettings->m_DimensionUnitsMode       = static_cast<DIM_UNITS_MODE>( mode );
209     int format                                = m_dimensionUnitsFormat->GetSelection();
210     m_BrdSettings->m_DimensionUnitsFormat     = static_cast<DIM_UNITS_FORMAT>( format );
211     m_BrdSettings->m_DimensionPrecision       = m_dimensionPrecision->GetSelection();
212     m_BrdSettings->m_DimensionSuppressZeroes  = m_dimensionSuppressZeroes->GetValue();
213     int position                              = m_dimensionTextPositionMode->GetSelection();
214     m_BrdSettings->m_DimensionTextPosition    = static_cast<DIM_TEXT_POSITION>( position );
215     m_BrdSettings->m_DimensionKeepTextAligned = m_dimensionTextKeepAligned->GetValue();
216     m_BrdSettings->m_DimensionArrowLength     = m_arrowLength.GetValue();
217     m_BrdSettings->m_DimensionExtensionOffset = m_extensionOffset.GetValue();
218 
219     return true;
220 }
221 
222 
ImportSettingsFrom(BOARD * aBoard)223 void PANEL_SETUP_TEXT_AND_GRAPHICS::ImportSettingsFrom( BOARD* aBoard )
224 {
225     if( !m_grid->CommitPendingChanges() )
226         return;
227 
228     BOARD_DESIGN_SETTINGS* savedSettings = m_BrdSettings;
229 
230     m_BrdSettings = &aBoard->GetDesignSettings();
231     TransferDataToWindow();
232 
233     m_BrdSettings = savedSettings;
234 }
235