KiCad PCB EDA Suite
color_swatch.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) 2017-2020 KiCad Developers, see AUTHORS.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 <widgets/color_swatch.h>
25 #include <wx/dcmemory.h>
26 
27 #include "dialog_color_picker.h"
28 #include <memory>
29 
30 wxDEFINE_EVENT(COLOR_SWATCH_CHANGED, wxCommandEvent);
31 
32 using KIGFX::COLOR4D;
33 
34 
35 // See selcolor.cpp:
36 extern COLOR4D DisplayColorFrame( wxWindow* aParent, COLOR4D aOldColor );
37 
38 
44 wxBitmap COLOR_SWATCH::MakeBitmap( COLOR4D aColor, COLOR4D aBackground, wxSize aSize,
45  wxSize aCheckerboardSize, COLOR4D aCheckerboardBackground )
46 {
47  wxBitmap bitmap( aSize );
48  wxBrush brush;
49  wxPen pen;
50  wxMemoryDC iconDC;
51 
52  iconDC.SelectObject( bitmap );
53  brush.SetStyle( wxBRUSHSTYLE_SOLID );
54 
55  if( aColor == COLOR4D::UNSPECIFIED )
56  {
57  // Draw a checkerboard
58  COLOR4D white;
59  COLOR4D black;
60  bool rowCycle;
61 
62  if( aCheckerboardBackground.GetBrightness() > 0.4 )
63  {
64  white = COLOR4D::WHITE;
65  black = white.Darkened( 0.15 );
66  rowCycle = true;
67  }
68  else
69  {
70  black = COLOR4D::BLACK;
71  white = black.Brightened( 0.15 );
72  rowCycle = false;
73  }
74 
75  for( int x = 0; x < aSize.x; x += aCheckerboardSize.x )
76  {
77  bool colCycle = rowCycle;
78 
79  for( int y = 0; y < aSize.y; y += aCheckerboardSize.y )
80  {
81  COLOR4D color = colCycle ? black : white;
82  brush.SetColour( color.ToColour() );
83  pen.SetColour( color.ToColour() );
84 
85  iconDC.SetBrush( brush );
86  iconDC.SetPen( pen );
87  iconDC.DrawRectangle( x, y, x + aCheckerboardSize.x, y + aCheckerboardSize.y );
88 
89  colCycle = !colCycle;
90  }
91 
92  rowCycle = !rowCycle;
93  }
94  }
95  else
96  {
97  brush.SetColour( aBackground.WithAlpha(1.0).ToColour() );
98  pen.SetColour( aBackground.WithAlpha(1.0).ToColour() );
99 
100  iconDC.SetBrush( brush );
101  iconDC.SetPen( pen );
102  iconDC.DrawRectangle( 0, 0, aSize.x, aSize.y );
103 
104  brush.SetColour( aColor.ToColour() );
105  pen.SetColour( aColor.ToColour() );
106 
107  iconDC.SetBrush( brush );
108  iconDC.SetPen( pen );
109  iconDC.DrawRectangle( 0, 0, aSize.x, aSize.y );
110  }
111 
112  return bitmap;
113 }
114 
115 
116 COLOR_SWATCH::COLOR_SWATCH( wxWindow* aParent, COLOR4D aColor, int aID, COLOR4D aBackground,
117  const COLOR4D aDefault, SWATCH_SIZE aSwatchSize ) :
118  wxPanel( aParent, aID ),
119  m_color( aColor ),
120  m_background( aBackground ),
121  m_default( aDefault ),
122  m_readOnly( false )
123 {
124  wxASSERT_MSG( aSwatchSize != SWATCH_EXPAND, "SWATCH_EXPAND not supported in COLOR_SWATCH" );
125 
126  switch( aSwatchSize )
127  {
128  case SWATCH_MEDIUM: m_size = ConvertDialogToPixels( SWATCH_SIZE_MEDIUM_DU ); break;
129  case SWATCH_SMALL: m_size = ConvertDialogToPixels( SWATCH_SIZE_SMALL_DU ); break;
130  case SWATCH_LARGE: m_size = ConvertDialogToPixels( SWATCH_SIZE_LARGE_DU ); break;
131  case SWATCH_EXPAND: m_size = ConvertDialogToPixels( SWATCH_SIZE_LARGE_DU ); break;
132  }
133 
134  m_checkerboardSize = ConvertDialogToPixels( CHECKERBOARD_SIZE_DU );
135  m_checkerboardBg = aParent->GetBackgroundColour();
136 
137  auto sizer = new wxBoxSizer( wxHORIZONTAL );
138  SetSizer( sizer );
139 
140  wxBitmap bitmap = COLOR_SWATCH::MakeBitmap( aColor, aBackground, m_size,
142  m_swatch = new wxStaticBitmap( this, aID, bitmap );
143 
144  sizer->Add( m_swatch, 0, 0 );
145 
146  setupEvents();
147 }
148 
149 
150 COLOR_SWATCH::COLOR_SWATCH( wxWindow *aParent, wxWindowID aID, const wxPoint &aPos,
151  const wxSize &aSize, long aStyle ) :
152  wxPanel( aParent, aID, aPos, aSize, aStyle ),
153  m_readOnly( false )
154 {
155  if( aSize == wxDefaultSize )
156  m_size = ConvertDialogToPixels( SWATCH_SIZE_MEDIUM_DU );
157  else
158  m_size = aSize;
159 
160  m_checkerboardSize = ConvertDialogToPixels( CHECKERBOARD_SIZE_DU );
161  m_checkerboardBg = aParent->GetBackgroundColour();
162 
163  SetSize( m_size );
164 
165  auto sizer = new wxBoxSizer( wxHORIZONTAL );
166  SetSizer( sizer );
167 
168  wxBitmap bitmap = COLOR_SWATCH::MakeBitmap( COLOR4D::UNSPECIFIED, COLOR4D::UNSPECIFIED,
170  m_swatch = new wxStaticBitmap( this, aID, bitmap );
171 
172  sizer->Add( m_swatch, 0, 0 );
173 
174  setupEvents();
175 }
176 
177 
179 {
180  wxWindow* topLevelParent = GetParent();
181 
182  while( topLevelParent && !topLevelParent->IsTopLevel() )
183  topLevelParent = topLevelParent->GetParent();
184 
185  if( topLevelParent && dynamic_cast<DIALOG_SHIM*>( topLevelParent ) )
186  {
187  m_swatch->Bind( wxEVT_LEFT_DOWN,
188  [this] ( wxMouseEvent& aEvt )
189  {
191  } );
192  }
193  else
194  {
195  // forward click to any other listeners, since we don't want them
196  m_swatch->Bind( wxEVT_LEFT_DOWN, &COLOR_SWATCH::rePostEvent, this );
197 
198  // bind the events that trigger the dialog
199  m_swatch->Bind( wxEVT_LEFT_DCLICK,
200  [this] ( wxMouseEvent& aEvt )
201  {
203  } );
204  }
205 
206  m_swatch->Bind( wxEVT_MIDDLE_DOWN,
207  [this] ( wxMouseEvent& aEvt )
208  {
210  } );
211 
212  m_swatch->Bind( wxEVT_RIGHT_DOWN, &COLOR_SWATCH::rePostEvent, this );
213 }
214 
215 
216 void COLOR_SWATCH::rePostEvent( wxEvent& aEvent )
217 {
218  wxPostEvent( this, aEvent );
219 }
220 
221 
222 static void sendSwatchChangeEvent( COLOR_SWATCH& aSender )
223 {
224  wxCommandEvent changeEvt( COLOR_SWATCH_CHANGED );
225 
226  // use this class as the object (alternative might be to
227  // set a custom event class but that's more work)
228  changeEvt.SetEventObject( &aSender );
229 
230  wxPostEvent( &aSender, changeEvt );
231 }
232 
233 
234 void COLOR_SWATCH::SetSwatchColor( COLOR4D aColor, bool aSendEvent )
235 {
236  m_color = aColor;
237 
239  m_swatch->SetBitmap( bm );
240 
241  if( aSendEvent )
242  sendSwatchChangeEvent( *this );
243 }
244 
245 
247 {
248  m_default = aColor;
249 }
250 
251 
253 {
254  m_background = aBackground;
256  m_swatch->SetBitmap( bm );
257 }
258 
259 
261 {
262  return m_color;
263 }
264 
265 
267 {
268  if( m_readOnly )
269  {
270  if( m_readOnlyCallback )
272 
273  return;
274  }
275 
276  DIALOG_COLOR_PICKER dialog( ::wxGetTopLevelParent( this ), m_color, true, nullptr, m_default );
277 
278  if( dialog.ShowModal() == wxID_OK )
279  {
280  COLOR4D newColor = dialog.GetColor();
281 
282  if( newColor != COLOR4D::UNSPECIFIED || m_default == COLOR4D::UNSPECIFIED )
283  {
284  m_color = newColor;
285 
286  wxBitmap bm = MakeBitmap( newColor, m_background, m_size, m_checkerboardSize,
288  m_swatch->SetBitmap( bm );
289 
290  sendSwatchChangeEvent( *this );
291  }
292  }
293 }
void SetSwatchColor(KIGFX::COLOR4D aColor, bool aSendEvent)
Set the current swatch color directly.
COLOR4D DisplayColorFrame(wxWindow *aParent, COLOR4D aOldColor)
KIGFX::COLOR4D GetSwatchColor() const
double GetBrightness() const
Function GetBrightness Returns the brightness value of the color ranged from 0.0 to 1....
Definition: color4d.h:337
COLOR_SWATCH(wxWindow *aParent, KIGFX::COLOR4D aColor, int aID, KIGFX::COLOR4D aBackground, const KIGFX::COLOR4D aDefault, SWATCH_SIZE aSwatchType)
Construct a COLOR_SWATCH.
void setupEvents()
void SetDefaultColor(KIGFX::COLOR4D aColor)
Sets the color that will be chosen with the "Reset to Default" button in the chooser.
int color
Definition: DXF_plotter.cpp:60
static wxBitmap MakeBitmap(KIGFX::COLOR4D aColor, KIGFX::COLOR4D aBackground, wxSize aSize, wxSize aCheckerboardSize, KIGFX::COLOR4D aCheckerboardBackground)
Make a simple color swatch bitmap.
static const wxSize SWATCH_SIZE_LARGE_DU(24, 16)
COLOR4D WithAlpha(double aAlpha) const
Function WithAlpha Returns a colour with the same colour, but the given alpha.
Definition: color4d.h:315
void SetSwatchBackground(KIGFX::COLOR4D aBackground)
Set the swatch background color.
Definition: color4d.h:45
COLOR4D Darkened(double aFactor) const
Function Darkened Returns a color that is darker by a given factor, without modifying object.
Definition: color4d.h:283
KIGFX::COLOR4D m_default
Definition: color_swatch.h:123
COLOR4D Brightened(double aFactor) const
Function Brightened Returns a color that is brighter by a given factor, without modifying object.
Definition: color4d.h:267
wxSize m_checkerboardSize
Definition: color_swatch.h:128
KIGFX::COLOR4D m_background
Definition: color_swatch.h:122
static const wxSize SWATCH_SIZE_MEDIUM_DU(24, 10)
std::function< void()> m_readOnlyCallback
Definition: color_swatch.h:133
Definition: color4d.h:49
wxStaticBitmap * m_swatch
Definition: color_swatch.h:125
wxDEFINE_EVENT(COLOR_SWATCH_CHANGED, wxCommandEvent)
KIGFX::COLOR4D m_checkerboardBg
Definition: color_swatch.h:129
bool m_readOnly
A read-only swatch won't show the color chooser dialog but otherwise works normally.
Definition: color_swatch.h:132
static void sendSwatchChangeEvent(COLOR_SWATCH &aSender)
void rePostEvent(wxEvent &aEvent)
Pass unwanted events on to listeners of this object.
static const wxSize CHECKERBOARD_SIZE_DU(3, 3)
KIGFX::COLOR4D GetColor()
Class representing a simple color swatch, of the kind used to set layer colors.
Definition: color_swatch.h:57
static const wxSize SWATCH_SIZE_SMALL_DU(8, 6)
void GetNewSwatchColor()
Prompt for a new colour, using the colour picker dialog.
KIGFX::COLOR4D m_color
Definition: color_swatch.h:121
SWATCH_SIZE
Definition: color_swatch.h:38
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:100