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 color Layers for Eeschema
26  */
27 
28 #include <fctsys.h>
29 #include <draw_frame.h>
30 #include <class_drawpanel.h>
31 
32 #include <general.h>
33 
35 
36 // Specify the width and height of every (color-displaying / bitmap) button
37 const int BUTT_SIZE_X = 16;
38 const int BUTT_SIZE_Y = 16;
39 
40 
41 /********************/
42 /* Layer menu list. */
43 /********************/
44 
46 {
47  wxString m_Name;
48  int m_Layer;
49 };
50 
52 {
53  wxString m_Name;
55 };
56 
58  { _( "Wire" ), LAYER_WIRE },
59  { _( "Bus" ), LAYER_BUS },
60  { _( "Junction" ), LAYER_JUNCTION },
61  { _( "Label" ), LAYER_LOCLABEL },
62  { _( "Global label" ), LAYER_GLOBLABEL },
63  { _( "Net name" ), LAYER_NETNAM },
64  { _( "Notes" ), LAYER_NOTES },
65  { _( "No connect symbol" ), LAYER_NOCONNECT },
66  { wxT( "" ), -1 } // Sentinel marking end of list.
67 };
68 
70  { _( "Body" ), LAYER_DEVICE },
71  { _( "Body background" ), LAYER_DEVICE_BACKGROUND },
72  { _( "Pin" ), LAYER_PIN },
73  { _( "Pin number" ), LAYER_PINNUM },
74  { _( "Pin name" ), LAYER_PINNAM },
75  { _( "Reference" ), LAYER_REFERENCEPART },
76  { _( "Value" ), LAYER_VALUEPART },
77  { _( "Fields" ), LAYER_FIELDS },
78  { wxT( "" ), -1 } // Sentinel marking end of list.
79 };
80 
82  { _( "Sheet" ), LAYER_SHEET },
83  { _( "Sheet file name" ), LAYER_SHEETFILENAME },
84  { _( "Sheet name" ), LAYER_SHEETNAME },
85  { _( "Sheet label" ), LAYER_SHEETLABEL },
86  { _( "Hierarchical label" ),LAYER_HIERLABEL },
87  { wxT( "" ), -1 } // Sentinel marking end of list.
88 };
89 
91  { _( "ERC warning" ), LAYER_ERC_WARN },
92  { _( "ERC error" ), LAYER_ERC_ERR },
93  { _( "Grid" ), LAYER_SCHEMATIC_GRID },
94  { _( "Brightened" ), LAYER_BRIGHTENED },
95  { wxT( "" ), -1 } // Sentinel marking end of list.
96 };
97 
98 
100  { _( "General" ), generalColorButtons },
101  { _( "Component" ), componentColorButtons },
102  { _( "Sheet" ), sheetColorButtons },
103  { _( "Miscellaneous" ), miscColorButtons },
104  { wxT( "" ), NULL }
105 };
106 
108 
110 
111 
113  wxPanel( aParent ), m_drawFrame( aDrawFrame )
114 {
115  CreateControls();
116 }
117 
118 
120 {
121  wxStaticText* label;
122  int buttonId = 1800;
123 
124  m_mainBoxSizer = new wxBoxSizer( wxHORIZONTAL );
125  SetSizer( m_mainBoxSizer );
126 
127  BUTTONINDEX* groups = buttonGroups;
128  wxBoxSizer* columnBoxSizer = NULL;
129 
130  while( groups->m_Buttons != NULL )
131  {
132  COLORBUTTON* buttons = groups->m_Buttons;
133 
134  columnBoxSizer = new wxBoxSizer( wxVERTICAL );
135  m_mainBoxSizer->Add( columnBoxSizer, 1, wxALIGN_TOP | wxLEFT | wxTOP, 5 );
136  wxBoxSizer* rowBoxSizer = new wxBoxSizer( wxHORIZONTAL );
137  columnBoxSizer->Add( rowBoxSizer, 0, wxGROW | wxLEFT | wxRIGHT | wxBOTTOM, 5 );
138 
139  // Add a text string to identify the column of color select buttons.
140  label = new wxStaticText( this, wxID_ANY, groups->m_Name );
141 
142  // Make the column label font bold.
143  wxFont font( label->GetFont() );
144  font.SetWeight( wxFONTWEIGHT_BOLD );
145  label->SetFont( font );
146 
147  rowBoxSizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
148 
149  while( buttons->m_Layer >= 0 )
150  {
151  rowBoxSizer = new wxBoxSizer( wxHORIZONTAL );
152  columnBoxSizer->Add( rowBoxSizer, 0, wxGROW | wxALL, 0 );
153 
155  currentColors[ SCH_LAYER_INDEX( buttons->m_Layer ) ] = color;
156 
157  wxMemoryDC iconDC;
158  wxBitmap bitmap( BUTT_SIZE_X, BUTT_SIZE_Y );
159 
160  iconDC.SelectObject( bitmap );
161  iconDC.SetPen( *wxBLACK_PEN );
162 
163  wxBrush brush;
164  brush.SetColour( color.ToColour() );
165  brush.SetStyle( wxBRUSHSTYLE_SOLID );
166  iconDC.SetBrush( brush );
167  iconDC.DrawRectangle( 0, 0, BUTT_SIZE_X, BUTT_SIZE_Y );
168 
169  wxBitmapButton* bitmapButton = new wxBitmapButton(
170  this, buttonId, bitmap, wxDefaultPosition,
171  wxSize( BUTT_SIZE_X+8, BUTT_SIZE_Y+6 ) );
172  bitmapButton->SetClientData( (void*) buttons );
173 
174  rowBoxSizer->Add( bitmapButton, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT | wxBOTTOM, 5 );
175 
176  label = new wxStaticText( this, wxID_ANY, wxGetTranslation( buttons->m_Name ) );
177  rowBoxSizer->Add( label, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT | wxBOTTOM, 5 );
178  buttonId += 1;
179  buttons++;
180  }
181 
182  groups++;
183  }
184 
185  COLOR4D bgColor = GetDrawFrame()->GetDrawBgColor();
186  wxMemoryDC iconDC;
187  wxBitmap bitmap( BUTT_SIZE_X, BUTT_SIZE_Y );
188 
189  iconDC.SelectObject( bitmap );
190  iconDC.SetPen( *wxBLACK_PEN );
191 
192  wxBrush brush;
193  brush.SetColour( bgColor.ToColour() );
194  brush.SetStyle( wxBRUSHSTYLE_SOLID );
195  iconDC.SetBrush( brush );
196  iconDC.DrawRectangle( 0, 0, BUTT_SIZE_X, BUTT_SIZE_Y );
197 
198  buttonId++;
199  wxBitmapButton* selBgColorBtn = new wxBitmapButton(
200  this, buttonId, bitmap, wxDefaultPosition,
201  wxSize( BUTT_SIZE_X + 8, BUTT_SIZE_Y + 6 ) );
202  selBgColorBtn->SetClientData( (void*) &bgColorButton );
203 
204  Connect( 1800, buttonId, wxEVT_COMMAND_BUTTON_CLICKED,
205  wxCommandEventHandler( WIDGET_EESCHEMA_COLOR_CONFIG::SetColor ) );
206 
207  wxStaticText* bgColorLabel = new wxStaticText( this, wxID_ANY, _( "Background Color" ) );
208  wxFont font( bgColorLabel->GetFont() );
209  font.SetWeight( wxFONTWEIGHT_BOLD );
210  bgColorLabel->SetFont( font );
211 
212  if( columnBoxSizer )
213  {
214  // Add a spacer to improve appearance.
215  columnBoxSizer->AddSpacer( 5 );
216  columnBoxSizer->Add( bgColorLabel, 1, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
217  columnBoxSizer->Add( selBgColorBtn, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT | wxBOTTOM, 5 );
218  }
219 
220  currentColors[ SCH_LAYER_INDEX( LAYER_SCHEMATIC_BACKGROUND ) ] = bgColor;
221 
222  // Dialog now needs to be resized, but the associated command is found elsewhere.
223 }
224 
225 
226 void WIDGET_EESCHEMA_COLOR_CONFIG::SetColor( wxCommandEvent& event )
227 {
228  wxBitmapButton* button = (wxBitmapButton*) event.GetEventObject();
229 
230  wxCHECK_RET( button != NULL, wxT( "Color button event object is NULL." ) );
231 
232  COLORBUTTON* colorButton = (COLORBUTTON*) button->GetClientData();
233 
234  wxCHECK_RET( colorButton != NULL, wxT( "Client data not set for color button." ) );
235 
236  wxColourData colourData;
237  colourData.SetColour( currentColors[ SCH_LAYER_INDEX( colorButton->m_Layer ) ].ToColour() );
238  wxColourDialog *dialog = new wxColourDialog( this, &colourData );
239 
240  COLOR4D newColor = COLOR4D::UNSPECIFIED;
241 
242  if( dialog->ShowModal() == wxID_OK )
243  {
244  newColor = COLOR4D( dialog->GetColourData().GetColour() );
245  }
246 
247  if( newColor == COLOR4D::UNSPECIFIED ||
248  currentColors[ SCH_LAYER_INDEX( colorButton->m_Layer ) ] == newColor )
249  return;
250 
251  currentColors[ SCH_LAYER_INDEX( colorButton->m_Layer ) ] = newColor;
252 
253  wxMemoryDC iconDC;
254 
255  wxBitmap bitmap = button->GetBitmapLabel();
256  iconDC.SelectObject( bitmap );
257  iconDC.SetPen( *wxBLACK_PEN );
258 
259  wxBrush brush;
260  brush.SetColour( newColor.ToColour() );
261  brush.SetStyle( wxBRUSHSTYLE_SOLID );
262 
263  iconDC.SetBrush( brush );
264  iconDC.DrawRectangle( 0, 0, BUTT_SIZE_X, BUTT_SIZE_Y );
265  button->SetBitmapLabel( bitmap );
266  button->Refresh();
267 
268  Refresh( false );
269 }
270 
271 
273 {
274  bool warning = false;
275 
276  // Check for color conflicts with background color to give user a chance to bail
277  // out before making changes.
278 
279  COLOR4D bgcolor = currentColors[ SCH_LAYER_INDEX( LAYER_SCHEMATIC_BACKGROUND ) ];
280 
281  for( SCH_LAYER_ID clyr = LAYER_WIRE; clyr < SCH_LAYER_ID_END; ++clyr )
282  {
283  if( bgcolor == currentColors[ SCH_LAYER_INDEX( clyr ) ] && clyr != LAYER_SCHEMATIC_BACKGROUND )
284  {
285  warning = true;
286  break;
287  }
288  }
289 
290  // Prompt the user if an item has the same color as the background
291  // because this item cannot be seen:
292  if( warning )
293  {
294  if( wxMessageBox( _( "Some items have the same color as the background\n"
295  "and they will not be seen on the screen. Are you\n"
296  "sure you want to use these colors?" ),
297  _( "Warning" ),
298  wxYES_NO | wxICON_QUESTION, this ) == wxNO )
299  return false;
300  }
301 
302  // Update color of background
303  GetDrawFrame()->SetDrawBgColor( bgcolor );
304  currentColors[ SCH_LAYER_INDEX( LAYER_SCHEMATIC_BACKGROUND ) ] = bgcolor;
305 
306 
307  for( SCH_LAYER_ID clyr = LAYER_WIRE; clyr < SCH_LAYER_ID_END; ++clyr )
308  {
309  SetLayerColor( currentColors[ SCH_LAYER_INDEX( clyr ) ], clyr );
310  }
311 
314 
315  return true;
316 }
317 
318 
320 {
321  wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
322 
323  sizer->Add( this, 1, wxALL | wxEXPAND, 0 );
324  aPanel->SetSizer( sizer );
325 }
void SetLayerColor(COLOR4D aColor, SCH_LAYER_ID aLayer)
Definition: eeschema.cpp:173
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Definition: draw_panel.cpp:326
EDA_DRAW_PANEL * GetCanvas()
Definition: draw_frame.h:299
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:291
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:457
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:166
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:286
void Refresh()
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