You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

473 lines
13 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 Jon Evans <jon@craftyjon.com>
  5. * Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software: you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation, either version 3 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef _APPEARANCE_CONTROLS_H
  21. #define _APPEARANCE_CONTROLS_H
  22. #include <vector>
  23. #include <board.h>
  24. #include <gal/color4d.h>
  25. #include <layers_id_colors_and_visibility.h>
  26. #include <project/board_project_settings.h>
  27. #include <widgets/appearance_controls_base.h>
  28. class BITMAP_TOGGLE;
  29. class COLOR_SWATCH;
  30. class INDICATOR_ICON;
  31. class PCB_BASE_FRAME;
  32. class ROW_ICON_PROVIDER;
  33. class GRID_BITMAP_TOGGLE_RENDERER;
  34. class WX_COLLAPSIBLE_PANE;
  35. class wxStaticLine;
  36. class wxSlider;
  37. class wxRadioButton;
  38. using KIGFX::COLOR4D;
  39. struct NET_GRID_ENTRY
  40. {
  41. NET_GRID_ENTRY( int aCode, const wxString& aName, const COLOR4D& aColor, bool aVisible )
  42. {
  43. code = aCode;
  44. name = aName;
  45. color = aColor;
  46. visible = aVisible;
  47. }
  48. int code;
  49. wxString name;
  50. COLOR4D color;
  51. bool visible;
  52. };
  53. class NET_GRID_TABLE : public wxGridTableBase
  54. {
  55. public:
  56. enum COLUMNS
  57. {
  58. COL_COLOR,
  59. COL_VISIBILITY,
  60. COL_LABEL,
  61. COL_SIZE
  62. };
  63. static void* ColorToVoid( COLOR4D& aColor )
  64. {
  65. return static_cast<void*>( &aColor );
  66. }
  67. static COLOR4D VoidToColor( void* aColor )
  68. {
  69. return *static_cast<COLOR4D*>( aColor );
  70. }
  71. public:
  72. NET_GRID_TABLE( PCB_BASE_FRAME* aFrame, wxColor aBackgroundColor );
  73. ~NET_GRID_TABLE();
  74. int GetNumberRows() override
  75. {
  76. return m_nets.size();
  77. }
  78. int GetNumberCols() override
  79. {
  80. return COL_SIZE;
  81. }
  82. wxGridCellAttr* GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind ) override;
  83. wxString GetValue( int aRow, int aCol ) override;
  84. void SetValue( int aRow, int aCol, const wxString& aValue ) override;
  85. wxString GetTypeName( int aRow, int aCol ) override;
  86. bool GetValueAsBool( int aRow, int aCol ) override;
  87. void SetValueAsBool( int aRow, int aCol, bool aValue ) override;
  88. void* GetValueAsCustom( int aRow, int aCol, const wxString& aTypeName ) override;
  89. void SetValueAsCustom( int aRow, int aCol, const wxString& aTypeName, void* aValue ) override;
  90. NET_GRID_ENTRY& GetEntry( int aRow );
  91. int GetRowByNetcode( int aCode ) const;
  92. void Rebuild();
  93. void ShowAllNets();
  94. void HideOtherNets( const NET_GRID_ENTRY& aNet );
  95. private:
  96. void updateNetVisibility( const NET_GRID_ENTRY& aNet );
  97. void updateNetColor( const NET_GRID_ENTRY& aNet );
  98. private:
  99. PCB_BASE_FRAME* m_frame;
  100. std::vector<NET_GRID_ENTRY> m_nets;
  101. wxGridCellAttr* m_defaultAttr;
  102. wxGridCellAttr* m_labelAttr;
  103. };
  104. class APPEARANCE_CONTROLS : public APPEARANCE_CONTROLS_BASE, public BOARD_LISTENER
  105. {
  106. public:
  107. /**
  108. * Container for an appearance setting (can control a single board layer, or GAL layer, etc)
  109. */
  110. struct APPEARANCE_SETTING
  111. {
  112. int id;
  113. wxString label;
  114. wxString tooltip;
  115. bool visible;
  116. bool can_control_opacity;
  117. bool spacer;
  118. wxPanel* ctl_panel;
  119. INDICATOR_ICON* ctl_indicator;
  120. BITMAP_TOGGLE* ctl_visibility;
  121. COLOR_SWATCH* ctl_color;
  122. wxStaticText* ctl_text;
  123. wxSlider* ctl_opacity;
  124. APPEARANCE_SETTING( const wxString& aLabel, int aId,
  125. const wxString& aTooltip = wxEmptyString,
  126. bool aCanControlOpacity = false ) :
  127. id( aId ),
  128. label( aLabel ),
  129. tooltip( aTooltip ),
  130. visible( true ),
  131. can_control_opacity( aCanControlOpacity ),
  132. spacer( false ),
  133. ctl_panel( nullptr ),
  134. ctl_indicator( nullptr ),
  135. ctl_visibility( nullptr ),
  136. ctl_color( nullptr ),
  137. ctl_text( nullptr ),
  138. ctl_opacity( nullptr )
  139. {
  140. }
  141. APPEARANCE_SETTING() :
  142. id( -1 ),
  143. label( "" ),
  144. tooltip( "" ),
  145. visible( false ),
  146. can_control_opacity( false ),
  147. spacer( true ),
  148. ctl_panel( nullptr ),
  149. ctl_indicator( nullptr ),
  150. ctl_visibility( nullptr ),
  151. ctl_color( nullptr ),
  152. ctl_text( nullptr ),
  153. ctl_opacity( nullptr )
  154. {
  155. }
  156. };
  157. APPEARANCE_CONTROLS( PCB_BASE_FRAME* aParent, wxWindow* aFocusOwner, bool aFpEditor = false );
  158. ~APPEARANCE_CONTROLS();
  159. wxSize GetBestSize() const;
  160. ///< Update the panel contents from the application and board models.
  161. void OnBoardChanged();
  162. void OnBoardNetSettingsChanged( BOARD& aBoard ) override;
  163. void OnBoardItemAdded( BOARD& aBoard, BOARD_ITEM* aBoardItem ) override;
  164. void OnBoardItemsAdded( BOARD& aBoard, std::vector<BOARD_ITEM*>& aBoardItems ) override;
  165. void OnBoardItemRemoved( BOARD& aBoard, BOARD_ITEM* aBoardItem ) override;
  166. void OnBoardItemsRemoved( BOARD& aBoard, std::vector<BOARD_ITEM*>& aBoardItems ) override;
  167. void OnBoardItemChanged( BOARD& aBoard, BOARD_ITEM* aBoardItem ) override;
  168. void OnBoardItemsChanged( BOARD& aBoard, std::vector<BOARD_ITEM*>& aBoardItems ) override;
  169. ///< Update the colors on all the widgets from the new chosen color theme.
  170. void OnColorThemeChanged();
  171. ///< Update the widget when the active board layer is changed.
  172. void OnLayerChanged();
  173. /// Notifies the panel when a net has been hidden or shown via the external tool.
  174. void OnNetVisibilityChanged( int aNetCode, bool aVisibility );
  175. ///< Manually update visibility for a given layer
  176. void SetLayerVisible( LAYER_NUM aLayer, bool isVisible );
  177. void SetObjectVisible( GAL_LAYER_ID aLayer, bool isVisible = true );
  178. ///< Update the manual layer alpha overrides.
  179. void OnLayerAlphaChanged();
  180. void UpdateDisplayOptions();
  181. ///< Return a list of the layer presets created by the user.
  182. std::vector<LAYER_PRESET> GetUserLayerPresets() const;
  183. ///< Update the current layer presets from those saved in the project file.
  184. void SetUserLayerPresets( std::vector<LAYER_PRESET>& aPresetList );
  185. void ApplyLayerPreset( const wxString& aPresetName );
  186. void ApplyLayerPreset( const LAYER_PRESET& aPreset );
  187. wxString GetActiveLayerPreset() const
  188. {
  189. if( m_currentPreset )
  190. return m_currentPreset->name;
  191. else
  192. return wxEmptyString;
  193. }
  194. const wxArrayString& GetLayerPresetsMRU()
  195. {
  196. return m_presetMRU;
  197. }
  198. void OnColorSwatchChanged( wxCommandEvent& aEvent );
  199. void OnLayerContextMenu( wxCommandEvent& aEvent );
  200. ///< Return the index of the current tab (0-2).
  201. int GetTabIndex() const;
  202. ///< Set the current notebook tab.
  203. void SetTabIndex( int aTab );
  204. protected:
  205. void OnNotebookPageChanged( wxNotebookEvent& event ) override;
  206. void OnSetFocus( wxFocusEvent& aEvent ) override;
  207. void OnSize( wxSizeEvent& aEvent ) override;
  208. void OnNetGridClick( wxGridEvent& event ) override;
  209. void OnNetGridDoubleClick( wxGridEvent& event ) override;
  210. void OnNetGridRightClick( wxGridEvent& event ) override;
  211. void OnNetGridMouseEvent( wxMouseEvent& aEvent );
  212. private:
  213. void createControls();
  214. void rebuildLayers();
  215. void rebuildLayerContextMenu();
  216. void syncColorsAndVisibility();
  217. void rebuildObjects();
  218. void syncObjectSettings();
  219. void rebuildNets();
  220. void loadDefaultLayerPresets();
  221. void rebuildLayerPresetsWidget();
  222. void syncLayerPresetSelection();
  223. void onLayerClick( wxMouseEvent& aEvent );
  224. void onLayerVisibilityChanged( PCB_LAYER_ID aLayer, bool isVisible, bool isFinal );
  225. void onObjectVisibilityChanged( GAL_LAYER_ID aLayer, bool isVisible, bool isFinal );
  226. void setVisibleLayers( LSET aLayers );
  227. void setVisibleObjects( GAL_SET aObjects );
  228. LSET getVisibleLayers();
  229. GAL_SET getVisibleObjects();
  230. void onObjectOpacitySlider( int aLayer, float aOpacity );
  231. void updateLayerPresetSelection( const wxString& aName );
  232. void onLayerPresetChanged( wxCommandEvent& aEvent ) override;
  233. void doApplyLayerPreset( const LAYER_PRESET& aPreset );
  234. void onNetclassVisibilityChanged( wxCommandEvent& aEvent );
  235. void showNetclass( const wxString& aClassName, bool aShow = true );
  236. void onNetContextMenu( wxCommandEvent& aEvent );
  237. void onNetclassColorChanged( wxCommandEvent& aEvent );
  238. wxString netclassNameFromEvent( wxEvent& aEvent );
  239. void onNetColorModeChanged( wxCommandEvent& aEvent );
  240. void onRatsnestModeChanged( wxCommandEvent& aEvent );
  241. void onNetclassContextMenu( wxCommandEvent& aEvent );
  242. void handleBoardItemsChanged();
  243. void passOnFocus();
  244. void idleFocusHandler( wxIdleEvent& aEvent );
  245. void onReadOnlySwatch();
  246. bool doesBoardItemNeedRebuild( BOARD_ITEM* aBoardItem );
  247. bool doesBoardItemNeedRebuild( std::vector<BOARD_ITEM*>& aBoardItems );
  248. PCB_BASE_FRAME* m_frame;
  249. wxWindow* m_focusOwner;
  250. static const APPEARANCE_SETTING s_objectSettings[];
  251. ROW_ICON_PROVIDER* m_iconProvider;
  252. BOARD* m_board;
  253. bool m_isFpEditor;
  254. // Nets grid view
  255. NET_GRID_TABLE* m_netsTable;
  256. GRID_BITMAP_TOGGLE_RENDERER* m_toggleGridRenderer;
  257. /// Grid cell that is being hovered over, for tooltips
  258. wxGridCellCoords m_hoveredCell;
  259. std::vector<std::unique_ptr<APPEARANCE_SETTING>> m_layerSettings;
  260. std::map<PCB_LAYER_ID, APPEARANCE_SETTING*> m_layerSettingsMap;
  261. std::vector<std::unique_ptr<APPEARANCE_SETTING>> m_objectSettings;
  262. std::map<GAL_LAYER_ID, APPEARANCE_SETTING*> m_objectSettingsMap;
  263. std::vector<std::unique_ptr<APPEARANCE_SETTING>> m_netclassSettings;
  264. std::map<wxString, APPEARANCE_SETTING*> m_netclassSettingsMap;
  265. // TODO(JE) Move preset storage to the PCB_CONTROL tool
  266. // Storage for all layer presets
  267. std::map<wxString, LAYER_PRESET> m_layerPresets;
  268. LAYER_PRESET* m_currentPreset;
  269. /// The last user (non-read-only) preset selected by the user
  270. LAYER_PRESET* m_lastSelectedUserPreset;
  271. wxArrayString m_presetMRU;
  272. wxMenu* m_layerContextMenu;
  273. /// Stores wxIDs for each netclass for control event mapping
  274. std::map<int, wxString> m_netclassIdMap;
  275. /// The name of the netclass that was right-clicked
  276. wxString m_contextMenuNetclass;
  277. wxBoxSizer* m_layersOuterSizer;
  278. wxBoxSizer* m_objectsOuterSizer;
  279. // The built-in layer presets
  280. static LAYER_PRESET presetNoLayers;
  281. static LAYER_PRESET presetAllLayers;
  282. static LAYER_PRESET presetAllCopper;
  283. static LAYER_PRESET presetInnerCopper;
  284. static LAYER_PRESET presetFront;
  285. static LAYER_PRESET presetFrontAssembly;
  286. static LAYER_PRESET presetBack;
  287. static LAYER_PRESET presetBackAssembly;
  288. int m_pointSize;
  289. wxColour m_layerPanelColour;
  290. // Layer display options controls
  291. WX_COLLAPSIBLE_PANE* m_paneLayerDisplayOptions;
  292. wxStaticText* m_staticTextContrastModeTitle;
  293. wxRadioButton* m_rbHighContrastNormal;
  294. wxRadioButton* m_rbHighContrastDim;
  295. wxRadioButton* m_rbHighContrastOff;
  296. wxStaticLine* m_layerDisplaySeparator;
  297. wxCheckBox* m_cbFlipBoard;
  298. // Net display options controls
  299. WX_COLLAPSIBLE_PANE* m_paneNetDisplayOptions;
  300. wxStaticText* m_txtNetDisplayTitle;
  301. wxRadioButton* m_rbNetColorAll;
  302. wxRadioButton* m_rbNetColorRatsnest;
  303. wxRadioButton* m_rbNetColorOff;
  304. wxStaticText* m_txtRatsnestVisibility;
  305. wxRadioButton* m_rbRatsnestAllLayers;
  306. wxRadioButton* m_rbRatsnestVisibleLayers;
  307. enum POPUP_ID
  308. {
  309. ID_CHANGE_COLOR = wxID_HIGHEST,
  310. ID_SET_NET_COLOR,
  311. ID_CLEAR_NET_COLOR,
  312. ID_SHOW_ALL_NETS,
  313. ID_HIDE_OTHER_NETS,
  314. ID_HIGHLIGHT_NET,
  315. ID_SELECT_NET,
  316. ID_DESELECT_NET,
  317. ID_SHOW_ALL_COPPER_LAYERS,
  318. ID_HIDE_ALL_COPPER_LAYERS,
  319. ID_HIDE_ALL_BUT_ACTIVE,
  320. ID_PRESET_NO_LAYERS,
  321. ID_PRESET_ALL_LAYERS,
  322. ID_PRESET_FRONT,
  323. ID_PRESET_FRONT_ASSEMBLY,
  324. ID_PRESET_INNER_COPPER,
  325. ID_PRESET_BACK,
  326. ID_PRESET_BACK_ASSEMBLY,
  327. ID_HIDE_ALL_NON_COPPER,
  328. ID_SHOW_ALL_NON_COPPER,
  329. ID_LAST_VALUE
  330. };
  331. };
  332. #endif