KiCad PCB EDA Suite
Loading...
Searching...
No Matches
grid_striped_renderer.h
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 3
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
20#pragma once
21
22#include <utility>
23
24#include <wx/brush.h>
25#include <wx/colour.h>
26#include <wx/dc.h>
27#include <wx/grid.h>
28#include <wx/generic/gridctrl.h>
29#include <wx/pen.h>
30#include <wx/settings.h>
31
32class wxGrid;
33
34template<typename T = wxGridCellStringRenderer>
36{
37public:
38 template <typename... Args>
39 STRIPED_CELL_RENDERER( const wxColour& aStripeOnDarkBackground, const wxColour& aStripeOnLightBackground,
40 Args&&... aArgs ) :
41 T( std::forward<Args>( aArgs )... ),
42 m_stripeOnDarkBackground( aStripeOnDarkBackground ),
43 m_stripeOnLightBackground( aStripeOnLightBackground )
44 {
45 }
46
47 void Draw( wxGrid& grid, wxGridCellAttr& attr, wxDC& dc,
48 const wxRect& rect, int row, int col, bool isSelected ) override
49 {
50 // Draw the foreground content using the base renderer first
51 T::Draw( grid, attr, dc, rect, row, col, isSelected );
52
53 // Overlay striped background for empty cells
54 if( grid.GetCellValue( row, col ).IsEmpty() )
55 drawStripedBackground( dc, attr, rect, isSelected );
56 }
57
58 wxGridCellRenderer* Clone() const override
59 {
60 return new STRIPED_CELL_RENDERER<T>(*this);
61 }
62
63private:
64 void drawStripedBackground(wxDC& dc, wxGridCellAttr& attr, const wxRect& rect, bool isSelected) const
65 {
66 if( isSelected )
67 {
68 // For selected cells, use the selection color
69 dc.SetBrush( wxBrush( wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) ) );
70 dc.SetPen( *wxTRANSPARENT_PEN );
71 dc.DrawRectangle( rect );
72 return;
73 }
74
75 // First fill with background color
76 wxColour bgColor = attr.GetBackgroundColour();
77 dc.SetBrush( wxBrush( bgColor ) );
78 dc.SetPen( *wxTRANSPARENT_PEN );
79 dc.DrawRectangle( rect );
80
81 // Draw diagonal stripes
82 const int stripeSpacing = 20; // Distance between diagonal lines
83
84 int bgLuminance = bgColor.GetLuminance();
85 wxColour stripeColor = bgLuminance < 128 ? m_stripeOnDarkBackground : m_stripeOnLightBackground;
86
87 wxPen stripePen( stripeColor, 1, wxPENSTYLE_SOLID );
88 dc.SetPen( stripePen );
89
90 // Calculate the diagonal stripes from top-left to bottom-right
91 int startX = rect.GetLeft() - rect.GetHeight();
92 int endX = rect.GetRight() + rect.GetHeight();
93
94 // Draw diagonal lines spaced evenly
95 for( int x = startX; x < endX; x += stripeSpacing )
96 {
97 int x1 = x;
98 int y1 = rect.GetTop();
99 int x2 = x + rect.GetHeight();
100 int y2 = rect.GetBottom();
101
102 // Clip the line to the rectangle bounds
103 if( x1 < rect.GetLeft() )
104 {
105 int deltaY = rect.GetLeft() - x1;
106 x1 = rect.GetLeft();
107 y1 = rect.GetTop() + deltaY;
108 }
109
110 if( x2 > rect.GetRight() )
111 {
112 int deltaY = x2 - rect.GetRight();
113 x2 = rect.GetRight();
114 y2 = rect.GetBottom() - deltaY;
115 }
116
117 // Only draw if the line is within the rectangle
118 if( x1 <= rect.GetRight() && x2 >= rect.GetLeft() && y1 <= rect.GetBottom() && y2 >= rect.GetTop() )
119 {
120 dc.DrawLine( x1, y1, x2, y2 );
121 }
122 }
123 }
124
127};
128
129
wxGridCellRenderer * Clone() const override
STRIPED_CELL_RENDERER(const wxColour &aStripeOnDarkBackground, const wxColour &aStripeOnLightBackground, Args &&... aArgs)
void drawStripedBackground(wxDC &dc, wxGridCellAttr &attr, const wxRect &rect, bool isSelected) const
void Draw(wxGrid &grid, wxGridCellAttr &attr, wxDC &dc, const wxRect &rect, int row, int col, bool isSelected) override
STRIPED_CELL_RENDERER< wxGridCellStringRenderer > STRIPED_STRING_RENDERER
STRIPED_CELL_RENDERER< wxGridCellNumberRenderer > STRIPED_NUMBER_RENDERER
STRIPED_CELL_RENDERER< wxGridCellBoolRenderer > STRIPED_BOOL_RENDERER
STL namespace.