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 (C) 2018-2022 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
25
28#include <wx/checkbox.h>
29#include <wx/dc.h>
30
31
32//-------- Custom wxGridCellRenderers --------------------------------------------------
33
34
36 const KIGFX::COLOR4D& aBackground ) :
37 wxGridCellRenderer(),
38 m_parent( aParent ),
39 m_background( aBackground )
40{
41 switch( aSize )
42 {
43 case SWATCH_MEDIUM: m_size = m_parent->ConvertDialogToPixels( SWATCH_SIZE_MEDIUM_DU ); break;
44 case SWATCH_SMALL: m_size = m_parent->ConvertDialogToPixels( SWATCH_SIZE_SMALL_DU ); break;
45 case SWATCH_LARGE: m_size = m_parent->ConvertDialogToPixels( SWATCH_SIZE_LARGE_DU ); break;
46 case SWATCH_EXPAND: m_size = wxDefaultSize; break;
47 }
48
49 m_checkerboardSize = m_parent->ConvertDialogToPixels( CHECKERBOARD_SIZE_DU );
50 m_checkerboardBg = m_parent->GetBackgroundColour();
51}
52
53
55{
56 m_parent = aOther.m_parent;
58 m_size = aOther.m_size;
61}
62
63
65{
66}
67
68
69wxGridCellRenderer* GRID_CELL_COLOR_RENDERER::Clone() const
70{
71 return new GRID_CELL_COLOR_RENDERER( *this );
72}
73
74
75wxSize GRID_CELL_COLOR_RENDERER::GetBestSize( wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
76 int row, int col )
77{
78 if( m_size != wxDefaultSize )
79 return m_size;
80
81 wxSize bestSize;
82
83 dc.SetFont( attr.GetFont() );
84 dc.GetTextExtent( "WWW", &bestSize.x, &bestSize.y );
85
86 return bestSize;
87}
88
89
90void GRID_CELL_COLOR_RENDERER::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr, wxDC& aDC,
91 const wxRect& aRect, int aRow, int aCol, bool isSelected )
92{
93 wxRect rect = aRect;
94
95 // erase background
96 wxGridCellRenderer::Draw( aGrid, aAttr, aDC, aRect, aRow, aCol, isSelected );
97
98 // draw the swatch
99 COLOR4D color( aGrid.GetTable()->GetValue( aRow, aCol ) );
100 wxSize size = ( m_size == wxDefaultSize ) ? aRect.GetSize() : m_size;
103
104 wxPoint origin = rect.GetTopLeft();
105
106 if( m_size != wxDefaultSize )
107 {
108 int x = std::max( 0, ( aRect.GetWidth() - m_size.x ) / 2 );
109 int y = std::max( 0, ( aRect.GetHeight() - m_size.y ) / 2 );
110 origin += wxPoint( x, y );
111 }
112
113 aDC.DrawBitmap( bitmap, origin, true );
114}
115
116
118{
119 m_checkerboardBg = m_parent->GetBackgroundColour();
120}
121
122
123//-------- Custom wxGridCellEditors ----------------------------------------------------
124//
125// Note: this implementation is an adaptation of wxGridCellBoolEditor
126
127
128GRID_CELL_COLOR_SELECTOR::GRID_CELL_COLOR_SELECTOR( wxWindow* aParent, wxGrid* aGrid ) :
129 m_parent( aParent ),
130 m_grid( aGrid ),
131 m_value( COLOR4D::UNSPECIFIED )
132{
133}
134
135
136wxGridCellEditor* GRID_CELL_COLOR_SELECTOR::Clone() const
137{
139}
140
141
142void GRID_CELL_COLOR_SELECTOR::Create( wxWindow* aParent, wxWindowID aId,
143 wxEvtHandler* aEventHandler )
144{
145 // wxWidgets needs a control to hold on to the event handler
146 m_control = new wxTextCtrl( aParent, wxID_ANY, wxEmptyString );
147
148 wxGridCellEditor::Create( aParent, aId, aEventHandler );
149}
150
151
153{
154 return m_value.ToCSSString();
155}
156
157
158void GRID_CELL_COLOR_SELECTOR::BeginEdit( int row, int col, wxGrid* grid )
159{
160 m_value.SetFromWxString( grid->GetTable()->GetValue( row, col ) );
161
162 grid->CallAfter(
163 [this, row, col]()
164 {
165 DIALOG_COLOR_PICKER dialog( m_parent, m_value, false );
166
167 if( dialog.ShowModal() == wxID_OK )
168 m_value = dialog.GetColor();
169
170 m_grid->GetTable()->SetValue( row, col, GetValue() );
171 m_grid->ForceRefresh();
172
173 // Let any clients know
174 wxGridEvent event( m_grid->GetId(), wxEVT_GRID_CELL_CHANGED, m_grid, row, col );
175 event.SetString( GetValue() );
176 m_grid->GetEventHandler()->ProcessEvent( event );
177 } );
178
179 // That's it; we're all done
180 m_grid->HideCellEditControl();
181}
182
183
184bool GRID_CELL_COLOR_SELECTOR::EndEdit( int row, int col, const wxGrid* grid,
185 const wxString& oldval, wxString *newval )
186{
187 if ( newval )
188 *newval = GetValue();
189
190 return true;
191}
192
193
194void GRID_CELL_COLOR_SELECTOR::ApplyEdit( int aRow, int aCol, wxGrid* aGrid )
195{
196 aGrid->GetTable()->SetValue( aRow, aCol, GetValue() );
197}
198
199
201{
202}
203
204
int color
Definition: DXF_plotter.cpp:58
static wxBitmap MakeBitmap(const KIGFX::COLOR4D &aColor, const KIGFX::COLOR4D &aBackground, const wxSize &aSize, const wxSize &aCheckerboardSize, const KIGFX::COLOR4D &aCheckerboardBackground)
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:104
bool SetFromWxString(const wxString &aColorString)
Set color values by parsing a string using wxColour::Set().
Definition: color4d.cpp:129
wxString ToCSSString() const
Definition: color4d.cpp:147
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
Definition: color_swatch.h:39
@ SWATCH_MEDIUM
Definition: color_swatch.h:41
@ SWATCH_LARGE
Definition: color_swatch.h:42
@ SWATCH_EXPAND
Definition: color_swatch.h:43
@ SWATCH_SMALL
Definition: color_swatch.h:40