KiCad PCB EDA Suite
widget_eeschema_color_config.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) 2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2016 KiCad Developers, see CHANGELOG.TXT for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 /* Set up colors to draw items in Eeschema
26  */
27 
28 #include <fctsys.h>
29 #include <draw_frame.h>
30 #include <class_drawpanel.h>
31 
32 #include <general.h>
33 
35 #include <../common/widgets/color4Dpickerdlg.h>
36 
37 // Specify the width and height of every (color-displaying / bitmap) button
38 const int BUTT_SIZE_X = 16;
39 const int BUTT_SIZE_Y = 16;
40 
41 
42 /********************/
43 /* Layer menu list. */
44 /********************/
45 
47 {
48  wxString m_Name;
49  int m_Layer;
50 };
51 
53 {
54  wxString m_Name;
56 };
57 
59  { _( "Wire" ), LAYER_WIRE },
60  { _( "Bus" ), LAYER_BUS },
61  { _( "Junction" ), LAYER_JUNCTION },
62  { _( "Label" ), LAYER_LOCLABEL },
63  { _( "Global label" ), LAYER_GLOBLABEL },
64  { _( "Net name" ), LAYER_NETNAM },
65  { _( "Notes" ), LAYER_NOTES },
66  { _( "No connect symbol" ), LAYER_NOCONNECT },
67  { wxT( "" ), -1 } // Sentinel marking end of list.
68 };
69 
71  { _( "Body" ), LAYER_DEVICE },
72  { _( "Body background" ), LAYER_DEVICE_BACKGROUND },
73  { _( "Pin" ), LAYER_PIN },
74  { _( "Pin number" ), LAYER_PINNUM },
75  { _( "Pin name" ), LAYER_PINNAM },
76  { _( "Reference" ), LAYER_REFERENCEPART },
77  { _( "Value" ), LAYER_VALUEPART },
78  { _( "Fields" ), LAYER_FIELDS },
79  { wxT( "" ), -1 } // Sentinel marking end of list.
80 };
81 
83  { _( "Sheet" ), LAYER_SHEET },
84  { _( "Sheet file name" ), LAYER_SHEETFILENAME },
85  { _( "Sheet name" ), LAYER_SHEETNAME },
86  { _( "Sheet label" ), LAYER_SHEETLABEL },
87  { _( "Hierarchical label" ),LAYER_HIERLABEL },
88  { wxT( "" ), -1 } // Sentinel marking end of list.
89 };
90 
92  { _( "ERC warning" ), LAYER_ERC_WARN },
93  { _( "ERC error" ), LAYER_ERC_ERR },
94  { _( "Grid" ), LAYER_SCHEMATIC_GRID },
95  { _( "Brightened" ), LAYER_BRIGHTENED },
96  { wxT( "" ), -1 } // Sentinel marking end of list.
97 };
98 
99 
101  { _( "General" ), generalColorButtons },
102  { _( "Component" ), componentColorButtons },
103  { _( "Sheet" ), sheetColorButtons },
104  { _( "Miscellaneous" ), miscColorButtons },
105  { wxT( "" ), NULL }
106 };
107 
109 
111 
112 
114  wxPanel( aParent ), m_drawFrame( aDrawFrame )
115 {
116  CreateControls();
117 }
118 
119 
121 {
122  wxStaticText* label;
123  int buttonId = 1800;
124 
125  m_mainBoxSizer = new wxBoxSizer( wxHORIZONTAL );
126  SetSizer( m_mainBoxSizer );
127 
128  BUTTONINDEX* groups = buttonGroups;
129  wxBoxSizer* columnBoxSizer = NULL;
130 
131  while( groups->m_Buttons != NULL )
132  {
133  COLORBUTTON* buttons = groups->m_Buttons;
134 
135  columnBoxSizer = new wxBoxSizer( wxVERTICAL );
136  m_mainBoxSizer->Add( columnBoxSizer, 1, wxALIGN_TOP | wxLEFT | wxTOP, 5 );
137  wxBoxSizer* rowBoxSizer = new wxBoxSizer( wxHORIZONTAL );
138  columnBoxSizer->Add( rowBoxSizer, 0, wxGROW | wxLEFT | wxRIGHT | wxBOTTOM, 5 );
139 
140  // Add a text string to identify the column of color select buttons.
141  label = new wxStaticText( this, wxID_ANY, groups->m_Name );
142 
143  // Make the column label font bold.
144  wxFont font( label->GetFont() );
145  font.SetWeight( wxFONTWEIGHT_BOLD );
146  label->SetFont( font );
147 
148  rowBoxSizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
149 
150  while( buttons->m_Layer >= 0 )
151  {
152  rowBoxSizer = new wxBoxSizer( wxHORIZONTAL );
153  columnBoxSizer->Add( rowBoxSizer, 0, wxGROW | wxALL, 0 );
154 
156  currentColors[ SCH_LAYER_INDEX( buttons->m_Layer ) ] = color;
157 
158  wxMemoryDC iconDC;
159  wxBitmap bitmap( BUTT_SIZE_X, BUTT_SIZE_Y );
160 
161  iconDC.SelectObject( bitmap );
162  iconDC.SetPen( *wxBLACK_PEN );
163 
164  wxBrush brush;
165  brush.SetColour( color.ToColour() );
166  brush.SetStyle( wxBRUSHSTYLE_SOLID );
167  iconDC.SetBrush( brush );
168  iconDC.DrawRectangle( 0, 0, BUTT_SIZE_X, BUTT_SIZE_Y );
169 
170  wxBitmapButton* bitmapButton = new wxBitmapButton(
171  this, buttonId, bitmap, wxDefaultPosition,
172  wxSize( BUTT_SIZE_X+8, BUTT_SIZE_Y+6 ) );
173  bitmapButton->SetClientData( (void*) buttons );
174 
175  rowBoxSizer->Add( bitmapButton, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT | wxBOTTOM, 5 );
176 
177  label = new wxStaticText( this, wxID_ANY, wxGetTranslation( buttons->m_Name ) );
178  rowBoxSizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT | wxBOTTOM, 5 );
179  buttonId += 1;
180  buttons++;
181  }
182 
183  groups++;
184  }
185 
186  COLOR4D bgColor = GetDrawFrame()->GetDrawBgColor();
187  wxMemoryDC iconDC;
188  wxBitmap bitmap( BUTT_SIZE_X, BUTT_SIZE_Y );
189 
190  iconDC.SelectObject( bitmap );
191  iconDC.SetPen( *wxBLACK_PEN );
192 
193  wxBrush brush;
194  brush.SetColour( bgColor.ToColour() );
195  brush.SetStyle( wxBRUSHSTYLE_SOLID );
196  iconDC.SetBrush( brush );
197  iconDC.DrawRectangle( 0, 0, BUTT_SIZE_X, BUTT_SIZE_Y );
198 
199  buttonId++;
200  wxBitmapButton* selBgColorBtn = new wxBitmapButton(
201  this, buttonId, bitmap, wxDefaultPosition,
202  wxSize( BUTT_SIZE_X + 8, BUTT_SIZE_Y + 6 ) );
203  selBgColorBtn->SetClientData( (void*) &bgColorButton );
204 
205  Connect( 1800, buttonId, wxEVT_COMMAND_BUTTON_CLICKED,
206  wxCommandEventHandler( WIDGET_EESCHEMA_COLOR_CONFIG::SetColor ) );
207 
208  wxStaticText* bgColorLabel = new wxStaticText( this, wxID_ANY, _( "Background Color" ) );
209  wxFont font( bgColorLabel->GetFont() );
210  font.SetWeight( wxFONTWEIGHT_BOLD );
211  bgColorLabel->SetFont( font );
212 
213  if( columnBoxSizer )
214  {
215  // Add a spacer to improve appearance.
216  columnBoxSizer->AddSpacer( 5 );
217  columnBoxSizer->Add( bgColorLabel, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
218  columnBoxSizer->Add( selBgColorBtn, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT | wxBOTTOM, 5 );
219  }
220 
221  currentColors[ SCH_LAYER_INDEX( LAYER_SCHEMATIC_BACKGROUND ) ] = bgColor;
222 
223  // Dialog now needs to be resized, but the associated command is found elsewhere.
224 }
225 
226 
227 void WIDGET_EESCHEMA_COLOR_CONFIG::SetColor( wxCommandEvent& event )
228 {
229  wxBitmapButton* button = (wxBitmapButton*) event.GetEventObject();
230 
231  wxCHECK_RET( button != NULL, wxT( "Color button event object is NULL." ) );
232 
233  COLORBUTTON* colorButton = (COLORBUTTON*) button->GetClientData();
234 
235  wxCHECK_RET( colorButton != NULL, wxT( "Client data not set for color button." ) );
236  COLOR4D oldColor = currentColors[ SCH_LAYER_INDEX( colorButton->m_Layer ) ];
237  COLOR4D newColor = COLOR4D::UNSPECIFIED;
238  COLOR4D_PICKER_DLG dialog( this, oldColor, false );
239 
240  if( dialog.ShowModal() == wxID_OK )
241  {
242  newColor = dialog.GetColor();
243  }
244 
245  if( newColor == COLOR4D::UNSPECIFIED || oldColor == newColor )
246  return;
247 
248  currentColors[ SCH_LAYER_INDEX( colorButton->m_Layer ) ] = newColor;
249 
250  wxMemoryDC iconDC;
251 
252  wxBitmap bitmap = button->GetBitmapLabel();
253  iconDC.SelectObject( bitmap );
254  iconDC.SetPen( *wxBLACK_PEN );
255 
256  wxBrush brush;
257  brush.SetColour( newColor.ToColour() );
258  brush.SetStyle( wxBRUSHSTYLE_SOLID );
259 
260  iconDC.SetBrush( brush );
261  iconDC.DrawRectangle( 0, 0, BUTT_SIZE_X, BUTT_SIZE_Y );
262  button->SetBitmapLabel( bitmap );
263  button->Refresh();
264 
265  Refresh( false );
266 }
267 
268 
270 {
271  bool warning = false;
272 
273  // Check for color conflicts with background color to give user a chance to bail
274  // out before making changes.
275 
276  COLOR4D bgcolor = currentColors[ SCH_LAYER_INDEX( LAYER_SCHEMATIC_BACKGROUND ) ];
277 
278  for( SCH_LAYER_ID clyr = LAYER_WIRE; clyr < SCH_LAYER_ID_END; ++clyr )
279  {
280  if( bgcolor == currentColors[ SCH_LAYER_INDEX( clyr ) ] && clyr != LAYER_SCHEMATIC_BACKGROUND )
281  {
282  warning = true;
283  break;
284  }
285  }
286 
287  // Prompt the user if an item has the same color as the background
288  // because this item cannot be seen:
289  if( warning )
290  {
291  if( wxMessageBox( _( "Some items have the same color as the background\n"
292  "and they will not be seen on the screen. Are you\n"
293  "sure you want to use these colors?" ),
294  _( "Warning" ),
295  wxYES_NO | wxICON_QUESTION, this ) == wxNO )
296  return false;
297  }
298 
299  // Update color of background
300  GetDrawFrame()->SetDrawBgColor( bgcolor );
301  currentColors[ SCH_LAYER_INDEX( LAYER_SCHEMATIC_BACKGROUND ) ] = bgcolor;
302 
303 
304  for( SCH_LAYER_ID clyr = LAYER_WIRE; clyr < SCH_LAYER_ID_END; ++clyr )
305  {
306  SetLayerColor( currentColors[ SCH_LAYER_INDEX( clyr ) ], clyr );
307  }
308 
311 
312  return true;
313 }
314 
315 
317 {
318  wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
319 
320  sizer->Add( this, 1, wxALL | wxEXPAND, 0 );
321  aPanel->SetSizer( sizer );
322 }
void SetLayerColor(COLOR4D aColor, SCH_LAYER_ID aLayer)
Definition: eeschema.cpp:174
KIGFX::COLOR4D GetColor()
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Update the board display after modifying it bu a python script (note: it is automatically called by a...
Definition: draw_panel.cpp:325
EDA_DRAW_PANEL * GetCanvas()
Definition: draw_frame.h:304
static COLORBUTTON generalColorButtons[]
static COLOR4D currentColors[SCH_LAYER_ID_COUNT]
WIDGET_EESCHEMA_COLOR_CONFIG(wxWindow *aParent, EDA_DRAW_FRAME *aDrawFrame)
virtual void SetDrawBgColor(COLOR4D aColor)
Definition: draw_frame.h:296
static BUTTONINDEX buttonGroups[]
#define SCH_LAYER_INDEX(x)
static COLORBUTTON bgColorButton
#define SCH_LAYER_ID_COUNT
virtual void SetGridColor(COLOR4D aColor)
Function SetGridColor() , virtual.
Definition: draw_frame.h:476
Class EDA_DRAW_FRAME is the base class for create windows for drawing purpose.
Definition: draw_frame.h:53
COLOR4D GetLayerColor(SCH_LAYER_ID aLayer)
Definition: eeschema.cpp:167
void InstallOnPanel(wxPanel *aPanel)
Method InstallOnPanel Install this WIDGET_EESCHEMA_COLOR_CONFIG onto an empty panel.
static COLORBUTTON sheetColorButtons[]
virtual COLOR4D GetDrawBgColor() const
Definition: draw_frame.h:291
void Refresh()
Update the board display after modifying it bu a python script (note: it is automatically called by a...
SCH_LAYER_ID
Eeschema drawing layers.
static COLORBUTTON miscColorButtons[]
static COLORBUTTON componentColorButtons[]
const int BUTT_SIZE_Y
const int BUTT_SIZE_X
virtual EDA_DRAW_FRAME * GetDrawFrame()
Class COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39