KiCad PCB EDA Suite
Loading...
Searching...
No Matches
grid_color_swatch_helpers.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 The 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, see <https://www.gnu.org/licenses/>.
18 */
19
21
24#include <wx/checkbox.h>
25#include <wx/dc.h>
26
27
28//-------- Custom wxGridCellRenderers --------------------------------------------------
29
30
32 const KIGFX::COLOR4D& aBackground ) :
33 wxGridCellRenderer(),
34 m_parent( aParent ),
35 m_background( aBackground )
36{
37 switch( aSize )
38 {
39 case SWATCH_MEDIUM: m_size = m_parent->ConvertDialogToPixels( SWATCH_SIZE_MEDIUM_DU ); break;
40 case SWATCH_SMALL: m_size = m_parent->ConvertDialogToPixels( SWATCH_SIZE_SMALL_DU ); break;
41 case SWATCH_LARGE: m_size = m_parent->ConvertDialogToPixels( SWATCH_SIZE_LARGE_DU ); break;
42 case SWATCH_EXPAND: m_size = wxDefaultSize; break;
43 }
44
45 m_checkerboardSize = m_parent->ConvertDialogToPixels( CHECKERBOARD_SIZE_DU );
46 m_checkerboardBg = m_parent->GetBackgroundColour();
47}
48
49
58
59
63
64
65wxGridCellRenderer* GRID_CELL_COLOR_RENDERER::Clone() const
66{
67 return new GRID_CELL_COLOR_RENDERER( *this );
68}
69
70
71wxSize GRID_CELL_COLOR_RENDERER::GetBestSize( wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
72 int row, int col )
73{
74 if( m_size != wxDefaultSize )
75 return m_size;
76
77 wxSize bestSize;
78
79 dc.SetFont( attr.GetFont() );
80 dc.GetTextExtent( "WWW", &bestSize.x, &bestSize.y );
81
82 return bestSize;
83}
84
85
86void GRID_CELL_COLOR_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
87 const wxRect& aRect, int aRow, int aCol, bool isSelected )
88{
89 wxRect rect = aRect;
90
91 // erase background
92 wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
93
94 // draw the swatch
95 COLOR4D color( aGrid.GetTable()->GetValue( aRow, aCol ) );
96 wxSize size = ( m_size == wxDefaultSize ) ? aRect.GetSize() : m_size;
97 wxBitmap bitmap = COLOR_SWATCH::MakeBitmap( color, m_background, size, m_checkerboardSize,
99
100 wxPoint origin = rect.GetTopLeft();
101
102 if( m_size != wxDefaultSize )
103 {
104 int x = std::max( 0, ( aRect.GetWidth() - m_size.x ) / 2 );
105 int y = std::max( 0, ( aRect.GetHeight() - m_size.y ) / 2 );
106 origin += wxPoint( x, y );
107 }
108
109 aDC.DrawBitmap( bitmap, origin, true );
110}
111
112
114{
115 m_checkerboardBg = m_parent ? m_parent->GetBackgroundColour() : wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW );
116}
117
118
119//-------- Custom wxGridCellEditors ----------------------------------------------------
120//
121// Note: this implementation is an adaptation of wxGridCellBoolEditor
122
123
124GRID_CELL_COLOR_SELECTOR::GRID_CELL_COLOR_SELECTOR( wxWindow* aParent, wxGrid* aGrid ) :
125 m_parent( aParent ),
126 m_grid( aGrid ),
128{
129}
130
131
132wxGridCellEditor* GRID_CELL_COLOR_SELECTOR::Clone() const
133{
135}
136
137
138void GRID_CELL_COLOR_SELECTOR::Create( wxWindow* aParent, wxWindowID aId,
139 wxEvtHandler* aEventHandler )
140{
141 // wxWidgets needs a control to hold on to the event handler
142 m_control = new wxTextCtrl( aParent, wxID_ANY, wxEmptyString );
143
144 wxGridCellEditor::Create( aParent, aId, aEventHandler );
145}
146
147
149{
150 return m_value.ToCSSString();
151}
152
153
154void GRID_CELL_COLOR_SELECTOR::BeginEdit( int row, int col, wxGrid* grid )
155{
156 m_value.SetFromWxString( grid->GetTable()->GetValue( row, col ) );
157
158 grid->CallAfter(
159 [this, row, col]()
160 {
161 DIALOG_COLOR_PICKER dialog( m_parent, m_value, false );
162
163 if( dialog.ShowModal() == wxID_OK )
164 m_value = dialog.GetColor();
165
166 m_grid->GetTable()->SetValue( row, col, GetValue() );
167 m_grid->ForceRefresh();
168
169 // Let any clients know
170 wxGridEvent event( m_grid->GetId(), wxEVT_GRID_CELL_CHANGED, m_grid, row, col );
171 event.SetString( GetValue() );
172 m_grid->GetEventHandler()->ProcessEvent( event );
173 } );
174
175 // That's it; we're all done
176 m_grid->HideCellEditControl();
177}
178
179
180bool GRID_CELL_COLOR_SELECTOR::EndEdit( int row, int col, const wxGrid* grid,
181 const wxString& oldval, wxString *newval )
182{
183 if ( newval )
184 *newval = GetValue();
185
186 return true;
187}
188
189
190void GRID_CELL_COLOR_SELECTOR::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
191{
192 aGrid->GetTable()->SetValue( aRow, aCol, GetValue() );
193}
194
195
199
200
static wxBitmap MakeBitmap(const KIGFX::COLOR4D &aColor, const KIGFX::COLOR4D &aBackground, const wxSize &aSize, const wxSize &aCheckerboardSize, const KIGFX::COLOR4D &aCheckerboardBackground, const std::vector< int > &aMargins={ 0, 0, 0, 0 })
KIGFX::COLOR4D GetColor()
int ShowModal() override
GRID_CELL_COLOR_RENDERER(wxWindow *aParent=nullptr, SWATCH_SIZE aSize=SWATCH_EXPAND, const KIGFX::COLOR4D &aBackground=KIGFX::COLOR4D::UNSPECIFIED)
wxSize GetBestSize(wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, int row, int col) override
wxGridCellRenderer * Clone() const override
void Draw(wxGrid &aGrid, wxGridCellAttr &aAttr, wxDC &aDC, const wxRect &aRect, int aRow, int aCol, bool isSelected) override
GRID_CELL_COLOR_SELECTOR(wxWindow *aParent, wxGrid *aGrid)
void ApplyEdit(int aRow, int aCol, wxGrid *aGrid) override
void BeginEdit(int aRow, int aCol, wxGrid *aGrid) override
void Create(wxWindow *aParent, wxWindowID aId, wxEvtHandler *aEventHandler) override
bool EndEdit(int, int, const wxGrid *, const wxString &, wxString *newval) override
wxGridCellEditor * Clone() const override
wxString GetValue() const override
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
static const wxSize SWATCH_SIZE_LARGE_DU(24, 16)
static const wxSize SWATCH_SIZE_MEDIUM_DU(24, 10)
static const wxSize CHECKERBOARD_SIZE_DU(3, 3)
static const wxSize SWATCH_SIZE_SMALL_DU(8, 6)
SWATCH_SIZE
@ SWATCH_MEDIUM
@ SWATCH_LARGE
@ SWATCH_EXPAND
@ SWATCH_SMALL