1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2018-2021 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 #ifndef FIELDS_GRID_TABLE_H
25 #define FIELDS_GRID_TABLE_H
26 
27 #include <base_units.h>
28 #include <sch_validators.h>
29 #include <wx/grid.h>
30 #include <sch_symbol.h>
31 #include <grid_tricks.h>
32 #include <validators.h>
33 
34 class SCH_BASE_FRAME;
35 class DIALOG_SHIM;
36 
37 
38 class FIELDS_GRID_TRICKS : public GRID_TRICKS
39 {
40 public:
FIELDS_GRID_TRICKS(WX_GRID * aGrid,DIALOG_SHIM * aDialog)41     FIELDS_GRID_TRICKS( WX_GRID* aGrid, DIALOG_SHIM* aDialog ) :
42         GRID_TRICKS( aGrid ),
43         m_dlg( aDialog )
44     {}
45 
46 protected:
47     virtual void showPopupMenu( wxMenu& menu ) override;
48     virtual void doPopupSelection( wxCommandEvent& event ) override;
49     DIALOG_SHIM* m_dlg;
50 };
51 
52 
53 enum FIELDS_DATA_COL_ORDER
54 {
55     FDC_NAME,
56     FDC_VALUE,
57     FDC_SHOWN,
58     FDC_H_ALIGN,
59     FDC_V_ALIGN,
60     FDC_ITALIC,
61     FDC_BOLD,
62     FDC_TEXT_SIZE,
63     FDC_ORIENTATION,
64     FDC_POSX,
65     FDC_POSY,
66 
67     FDC_COUNT       // keep as last
68 };
69 
70 
71 template <class T>
72 class FIELDS_GRID_TABLE : public wxGridTableBase, public std::vector<T>
73 {
74 public:
75     FIELDS_GRID_TABLE( DIALOG_SHIM* aDialog, SCH_BASE_FRAME* aFrame, WX_GRID* aGrid,
76                        LIB_SYMBOL* aSymbol );
77     FIELDS_GRID_TABLE( DIALOG_SHIM* aDialog, SCH_BASE_FRAME* aFrame, WX_GRID* aGrid,
78                        SCH_SHEET* aSheet );
79     ~FIELDS_GRID_TABLE();
80 
GetNumberRows()81     int GetNumberRows() override { return (int) this->size(); }
GetNumberCols()82     int GetNumberCols() override { return FDC_COUNT; }
83 
84     wxString GetColLabelValue( int aCol ) override;
85 
IsEmptyCell(int row,int col)86     bool IsEmptyCell( int row, int col ) override
87     {
88         return false;   // don't allow adjacent cell overflow, even if we are actually empty
89     }
90 
91     bool CanGetValueAs( int aRow, int aCol, const wxString& aTypeName ) override;
92     bool CanSetValueAs( int aRow, int aCol, const wxString& aTypeName ) override;
93     wxGridCellAttr* GetAttr( int row, int col, wxGridCellAttr::wxAttrKind kind ) override;
94 
95     wxString GetValue( int aRow, int aCol ) override;
96     bool GetValueAsBool( int aRow, int aCol ) override;
97 
98     void SetValue( int aRow, int aCol, const wxString& aValue ) override;
99     void SetValueAsBool( int aRow, int aCol, bool aValue ) override;
100 
101     wxString StringFromBool( bool aValue ) const;
102     bool BoolFromString( wxString aValue ) const;
103 
104 protected:
105     void initGrid( WX_GRID* aGrid );
106 
107 private:
108     SCH_BASE_FRAME* m_frame;
109     DIALOG_SHIM*    m_dialog;
110     EDA_UNITS       m_userUnits;
111     WX_GRID*        m_grid;
112     KICAD_T         m_parentType;
113     int             m_mandatoryFieldCount;
114     LIB_SYMBOL*     m_part;
115     wxString        m_curdir;
116 
117     SCH_FIELD_VALIDATOR   m_fieldNameValidator;
118     SCH_FIELD_VALIDATOR   m_referenceValidator;
119     SCH_FIELD_VALIDATOR   m_valueValidator;
120     LIB_ID_VALIDATOR      m_libIdValidator;
121     SCH_FIELD_VALIDATOR   m_urlValidator;
122     SCH_FIELD_VALIDATOR   m_nonUrlValidator;
123     SCH_FIELD_VALIDATOR   m_filepathValidator;
124 
125     wxGridCellAttr*       m_readOnlyAttr;
126     wxGridCellAttr*       m_fieldNameAttr;
127     wxGridCellAttr*       m_referenceAttr;
128     wxGridCellAttr*       m_valueAttr;
129     wxGridCellAttr*       m_footprintAttr;
130     wxGridCellAttr*       m_urlAttr;
131     wxGridCellAttr*       m_nonUrlAttr;
132     wxGridCellAttr*       m_filepathAttr;
133     wxGridCellAttr*       m_boolAttr;
134     wxGridCellAttr*       m_vAlignAttr;
135     wxGridCellAttr*       m_hAlignAttr;
136     wxGridCellAttr*       m_orientationAttr;
137 };
138 
139 
140 #endif  // FIELDS_GRID_TABLE_H
141