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.

491 lines
14 KiB

4 years ago
3 years ago
  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-2023 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 <layer_ids.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 PCBNEW_SETTINGS;
  33. class ROW_ICON_PROVIDER;
  34. class GRID_BITMAP_TOGGLE_RENDERER;
  35. class WX_COLLAPSIBLE_PANE;
  36. class wxStaticLine;
  37. class wxSlider;
  38. class wxRadioButton;
  39. using KIGFX::COLOR4D;
  40. struct NET_GRID_ENTRY
  41. {
  42. NET_GRID_ENTRY( int aCode, const wxString& aName, const COLOR4D& aColor, bool aVisible )
  43. {
  44. code = aCode;
  45. name = aName;
  46. color = aColor;
  47. visible = aVisible;
  48. }
  49. int code;
  50. wxString name;
  51. COLOR4D color;
  52. bool visible;
  53. };
  54. class NET_GRID_TABLE : public wxGridTableBase
  55. {
  56. public:
  57. enum COLUMNS
  58. {
  59. COL_COLOR,
  60. COL_VISIBILITY,
  61. COL_LABEL,
  62. COL_SIZE
  63. };
  64. static void* ColorToVoid( COLOR4D& aColor )
  65. {
  66. return static_cast<void*>( &aColor );
  67. }
  68. static COLOR4D VoidToColor( void* aColor )
  69. {
  70. return *static_cast<COLOR4D*>( aColor );
  71. }
  72. public:
  73. NET_GRID_TABLE( PCB_BASE_FRAME* aFrame, wxColor aBackgroundColor );
  74. ~NET_GRID_TABLE();
  75. int GetNumberRows() override
  76. {
  77. return m_nets.size();
  78. }
  79. int GetNumberCols() override
  80. {
  81. return COL_SIZE;
  82. }
  83. wxGridCellAttr* GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind ) override;
  84. wxString GetValue( int aRow, int aCol ) override;
  85. void SetValue( int aRow, int aCol, const wxString& aValue ) override;
  86. wxString GetTypeName( int aRow, int aCol ) override;
  87. bool GetValueAsBool( int aRow, int aCol ) override;
  88. void SetValueAsBool( int aRow, int aCol, bool aValue ) override;
  89. void* GetValueAsCustom( int aRow, int aCol, const wxString& aTypeName ) override;
  90. void SetValueAsCustom( int aRow, int aCol, const wxString& aTypeName, void* aValue ) override;
  91. NET_GRID_ENTRY& GetEntry( int aRow );
  92. int GetRowByNetcode( int aCode ) const;
  93. void Rebuild();
  94. void ShowAllNets();
  95. void HideOtherNets( const NET_GRID_ENTRY& aNet );
  96. private:
  97. void updateNetVisibility( const NET_GRID_ENTRY& aNet );
  98. void updateNetColor( const NET_GRID_ENTRY& aNet );
  99. private:
  100. PCB_BASE_FRAME* m_frame;
  101. std::vector<NET_GRID_ENTRY> m_nets;
  102. wxGridCellAttr* m_defaultAttr;
  103. wxGridCellAttr* m_labelAttr;
  104. };
  105. class APPEARANCE_CONTROLS : public APPEARANCE_CONTROLS_BASE, public BOARD_LISTENER
  106. {
  107. public:
  108. /**
  109. * Container for an appearance setting (can control a single board layer, or GAL layer, etc)
  110. */
  111. struct APPEARANCE_SETTING
  112. {
  113. int id;
  114. wxString label;
  115. wxString tooltip;
  116. bool visible;
  117. bool can_control_opacity;
  118. bool spacer;
  119. wxPanel* ctl_panel;
  120. INDICATOR_ICON* ctl_indicator;
  121. BITMAP_TOGGLE* ctl_visibility;
  122. COLOR_SWATCH* ctl_color;
  123. wxStaticText* ctl_text;
  124. wxSlider* ctl_opacity;
  125. APPEARANCE_SETTING( const wxString& aLabel, int aId,
  126. const wxString& aTooltip = wxEmptyString,
  127. bool aCanControlOpacity = false ) :
  128. id( aId ),
  129. label( aLabel ),
  130. tooltip( aTooltip ),
  131. visible( true ),
  132. can_control_opacity( aCanControlOpacity ),
  133. spacer( false ),
  134. ctl_panel( nullptr ),
  135. ctl_indicator( nullptr ),
  136. ctl_visibility( nullptr ),
  137. ctl_color( nullptr ),
  138. ctl_text( nullptr ),
  139. ctl_opacity( nullptr )
  140. {
  141. }
  142. APPEARANCE_SETTING() :
  143. id( -1 ),
  144. visible( false ),
  145. can_control_opacity( false ),
  146. spacer( true ),
  147. ctl_panel( nullptr ),
  148. ctl_indicator( nullptr ),
  149. ctl_visibility( nullptr ),
  150. ctl_color( nullptr ),
  151. ctl_text( nullptr ),
  152. ctl_opacity( nullptr )
  153. {
  154. }
  155. };
  156. APPEARANCE_CONTROLS( PCB_BASE_FRAME* aParent, wxWindow* aFocusOwner, bool aFpEditor = false );
  157. ~APPEARANCE_CONTROLS();
  158. wxSize GetBestSize() const;
  159. void OnLanguageChanged();
  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* aItem ) override;
  164. void OnBoardItemsAdded( BOARD& aBoard, std::vector<BOARD_ITEM*>& aItems ) override;
  165. void OnBoardItemRemoved( BOARD& aBoard, BOARD_ITEM* aItem ) override;
  166. void OnBoardItemsRemoved( BOARD& aBoard, std::vector<BOARD_ITEM*>& aItems ) override;
  167. void OnBoardItemChanged( BOARD& aBoard, BOARD_ITEM* aItem ) override;
  168. void OnBoardItemsChanged( BOARD& aBoard, std::vector<BOARD_ITEM*>& aItems ) override;
  169. ///< Update the colors on all the widgets from the new chosen color theme.
  170. void OnColorThemeChanged();
  171. ///< Respond to change in OS's DarkMode
  172. void OnDarkModeToggle();
  173. ///< Update the widget when the active board layer is changed.
  174. void OnLayerChanged();
  175. /// Notifies the panel when a net has been hidden or shown via the external tool.
  176. void OnNetVisibilityChanged( int aNetCode, bool aVisibility );
  177. ///< Manually update visibility for a given layer
  178. void SetLayerVisible( int aLayer, bool isVisible );
  179. void SetObjectVisible( GAL_LAYER_ID aLayer, bool isVisible = true );
  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() { return m_presetMRU; }
  195. ///< Return a list of viewports created by the user.
  196. std::vector<VIEWPORT> GetUserViewports() const;
  197. ///< Update the current viewports from those saved in the project file.
  198. void SetUserViewports( std::vector<VIEWPORT>& aPresetList );
  199. void ApplyViewport( const wxString& aPresetName );
  200. void ApplyViewport( const VIEWPORT& aPreset );
  201. const wxArrayString& GetViewportsMRU() { return m_viewportMRU; }
  202. void OnColorSwatchChanged( wxCommandEvent& aEvent );
  203. void OnLayerContextMenu( wxCommandEvent& aEvent );
  204. ///< Return the index of the current tab (0-2).
  205. int GetTabIndex() const;
  206. ///< Set the current notebook tab.
  207. void SetTabIndex( int aTab );
  208. /**
  209. * Function to force a redraw of the collapsible panes in this control.
  210. */
  211. void RefreshCollapsiblePanes();
  212. bool IsLayerOptionsExpanded();
  213. bool IsNetOptionsExpanded();
  214. protected:
  215. void OnNotebookPageChanged( wxNotebookEvent& event ) override;
  216. void OnSetFocus( wxFocusEvent& aEvent ) override;
  217. void OnSize( wxSizeEvent& aEvent ) override;
  218. void OnNetGridClick( wxGridEvent& event ) override;
  219. void OnNetGridDoubleClick( wxGridEvent& event ) override;
  220. void OnNetGridRightClick( wxGridEvent& event ) override;
  221. void OnNetGridMouseEvent( wxMouseEvent& aEvent );
  222. private:
  223. void createControls();
  224. void rebuildLayers();
  225. void rebuildLayerContextMenu();
  226. void syncColorsAndVisibility();
  227. void rebuildObjects();
  228. void syncObjectSettings();
  229. void buildNetClassMenu( wxMenu& aMenu, bool isDefaultClass, const wxString& aName );
  230. void rebuildNets();
  231. void loadDefaultLayerPresets();
  232. void rebuildLayerPresetsWidget();
  233. void syncLayerPresetSelection();
  234. void rebuildViewportsWidget();
  235. void onLayerLeftClick( wxMouseEvent& aEvent );
  236. void rightClickHandler( wxMouseEvent& aEvent );
  237. void onLayerVisibilityToggled( PCB_LAYER_ID aLayer );
  238. void onObjectVisibilityChanged( GAL_LAYER_ID aLayer, bool isVisible, bool isFinal );
  239. void setVisibleLayers( LSET aLayers );
  240. void setVisibleObjects( GAL_SET aObjects );
  241. LSET getVisibleLayers();
  242. GAL_SET getVisibleObjects();
  243. void onObjectOpacitySlider( int aLayer, float aOpacity );
  244. void updateLayerPresetSelection( const wxString& aName );
  245. void onLayerPresetChanged( wxCommandEvent& aEvent ) override;
  246. void doApplyLayerPreset( const LAYER_PRESET& aPreset );
  247. void updateViewportSelection( const wxString& aName );
  248. void onViewportChanged( wxCommandEvent& aEvent ) override;
  249. void doApplyViewport( const VIEWPORT& aViewport );
  250. void onNetclassVisibilityChanged( wxCommandEvent& aEvent );
  251. void showNetclass( const wxString& aClassName, bool aShow = true );
  252. void onNetContextMenu( wxCommandEvent& aEvent );
  253. void onNetclassColorChanged( wxCommandEvent& aEvent );
  254. wxString netclassNameFromEvent( wxEvent& aEvent );
  255. void onNetColorMode( wxCommandEvent& aEvent );
  256. void onRatsnestMode( wxCommandEvent& aEvent );
  257. void onNetclassContextMenu( wxCommandEvent& aEvent );
  258. void handleBoardItemsChanged();
  259. void passOnFocus();
  260. void idleFocusHandler( wxIdleEvent& aEvent );
  261. void onReadOnlySwatch();
  262. bool doesBoardItemNeedRebuild( BOARD_ITEM* aBoardItem );
  263. bool doesBoardItemNeedRebuild( std::vector<BOARD_ITEM*>& aBoardItems );
  264. PCB_BASE_FRAME* m_frame;
  265. wxWindow* m_focusOwner;
  266. static const APPEARANCE_SETTING s_objectSettings[];
  267. ROW_ICON_PROVIDER* m_iconProvider;
  268. BOARD* m_board;
  269. bool m_isFpEditor;
  270. // Nets grid view
  271. NET_GRID_TABLE* m_netsTable;
  272. GRID_BITMAP_TOGGLE_RENDERER* m_toggleGridRenderer;
  273. /// Grid cell that is being hovered over, for tooltips
  274. wxGridCellCoords m_hoveredCell;
  275. std::vector<std::unique_ptr<APPEARANCE_SETTING>> m_layerSettings;
  276. std::map<PCB_LAYER_ID, APPEARANCE_SETTING*> m_layerSettingsMap;
  277. std::vector<std::unique_ptr<APPEARANCE_SETTING>> m_objectSettings;
  278. std::map<GAL_LAYER_ID, APPEARANCE_SETTING*> m_objectSettingsMap;
  279. std::vector<std::unique_ptr<APPEARANCE_SETTING>> m_netclassSettings;
  280. std::map<wxString, APPEARANCE_SETTING*> m_netclassSettingsMap;
  281. // TODO(JE) Move preset storage to the PCB_CONTROL tool
  282. std::map<wxString, LAYER_PRESET> m_layerPresets;
  283. LAYER_PRESET* m_currentPreset;
  284. LAYER_PRESET* m_lastSelectedUserPreset;
  285. wxArrayString m_presetMRU;
  286. std::map<wxString, VIEWPORT> m_viewports;
  287. VIEWPORT* m_lastSelectedViewport;
  288. wxArrayString m_viewportMRU;
  289. wxMenu* m_layerContextMenu;
  290. /// Stores wxIDs for each netclass for control event mapping
  291. std::map<int, wxString> m_netclassIdMap;
  292. /// The name of the netclass that was right-clicked
  293. wxString m_contextMenuNetclass;
  294. wxBoxSizer* m_layersOuterSizer;
  295. wxBoxSizer* m_objectsOuterSizer;
  296. // The built-in layer presets
  297. static LAYER_PRESET presetNoLayers;
  298. static LAYER_PRESET presetAllLayers;
  299. static LAYER_PRESET presetAllCopper;
  300. static LAYER_PRESET presetInnerCopper;
  301. static LAYER_PRESET presetFront;
  302. static LAYER_PRESET presetFrontAssembly;
  303. static LAYER_PRESET presetBack;
  304. static LAYER_PRESET presetBackAssembly;
  305. // a LAYER_PRESET used only to store the objects visibility of the
  306. // last selected built-in LAYER_PRESET preset
  307. static LAYER_PRESET m_lastBuiltinPreset;
  308. int m_pointSize;
  309. wxColour m_layerPanelColour;
  310. // Layer display options controls
  311. WX_COLLAPSIBLE_PANE* m_paneLayerDisplayOptions;
  312. wxStaticText* m_inactiveLayersLabel;
  313. wxRadioButton* m_rbHighContrastNormal;
  314. wxRadioButton* m_rbHighContrastDim;
  315. wxRadioButton* m_rbHighContrastOff;
  316. wxStaticLine* m_layerDisplaySeparator;
  317. wxCheckBox* m_cbFlipBoard;
  318. // Net display options controls
  319. WX_COLLAPSIBLE_PANE* m_paneNetDisplayOptions;
  320. wxStaticText* m_txtNetDisplayTitle;
  321. wxRadioButton* m_rbNetColorAll;
  322. wxRadioButton* m_rbNetColorRatsnest;
  323. wxRadioButton* m_rbNetColorOff;
  324. wxStaticText* m_txtRatsnestVisibility;
  325. wxRadioButton* m_rbRatsnestAllLayers;
  326. wxRadioButton* m_rbRatsnestVisLayers;
  327. wxRadioButton* m_rbRatsnestNone;
  328. enum POPUP_ID
  329. {
  330. ID_CHANGE_COLOR = wxID_HIGHEST,
  331. ID_SET_NET_COLOR,
  332. ID_CLEAR_NET_COLOR,
  333. ID_SHOW_ALL_NETS,
  334. ID_HIDE_OTHER_NETS,
  335. ID_HIGHLIGHT_NET,
  336. ID_SELECT_NET,
  337. ID_DESELECT_NET,
  338. ID_SHOW_ALL_COPPER_LAYERS,
  339. ID_HIDE_ALL_COPPER_LAYERS,
  340. ID_HIDE_ALL_BUT_ACTIVE,
  341. ID_PRESET_NO_LAYERS,
  342. ID_PRESET_ALL_LAYERS,
  343. ID_PRESET_FRONT,
  344. ID_PRESET_FRONT_ASSEMBLY,
  345. ID_PRESET_INNER_COPPER,
  346. ID_PRESET_BACK,
  347. ID_PRESET_BACK_ASSEMBLY,
  348. ID_HIDE_ALL_NON_COPPER,
  349. ID_SHOW_ALL_NON_COPPER,
  350. ID_LAST_VALUE
  351. };
  352. };
  353. #endif