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.

310 lines
11 KiB

5 years ago
5 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2015 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 2015-2022 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef SCH_BASE_FRAME_H_
  25. #define SCH_BASE_FRAME_H_
  26. #include <eda_draw_frame.h>
  27. #include <frame_type.h>
  28. #include <sch_draw_panel.h>
  29. #include <sch_screen.h>
  30. #include <schematic_settings.h>
  31. #include <stddef.h>
  32. #include <utility>
  33. #include <vector>
  34. #include <wx/event.h>
  35. #include <wx/datetime.h>
  36. #include <wx/gdicmn.h>
  37. #include <wx/string.h>
  38. #include <wx/timer.h>
  39. #include <template_fieldnames.h>
  40. class SCH_RENDER_SETTINGS;
  41. class PAGE_INFO;
  42. class TITLE_BLOCK;
  43. class SYMBOL_VIEWER_FRAME;
  44. class SYMBOL_EDIT_FRAME;
  45. class LIB_SYMBOL;
  46. class SYMBOL_LIB;
  47. class SYMBOL_LIBRARY_FILTER;
  48. class LIB_ID;
  49. class SYMBOL_LIB_TABLE;
  50. class EESCHEMA_SETTINGS;
  51. class SYMBOL_EDITOR_SETTINGS;
  52. class NL_SCHEMATIC_PLUGIN;
  53. class PANEL_SCH_SELECTION_FILTER;
  54. #ifdef wxHAS_INOTIFY
  55. #define wxFileSystemWatcher wxInotifyFileSystemWatcher
  56. #elif defined( wxHAS_KQUEUE ) && defined( wxHAVE_FSEVENTS_FILE_NOTIFICATIONS )
  57. #define wxFileSystemWatcher wxFsEventsFileSystemWatcher
  58. #elif defined( wxHAS_KQUEUE )
  59. #define wxFileSystemWatcher wxKqueueFileSystemWatcher
  60. #elif defined( __WINDOWS__ )
  61. #define wxFileSystemWatcher wxMSWFileSystemWatcher
  62. #else
  63. #define wxFileSystemWatcher wxPollingFileSystemWatcher
  64. #endif
  65. class wxFileSystemWatcher;
  66. class wxFileSystemWatcherEvent;
  67. /**
  68. * Load symbol from symbol library table.
  69. *
  70. * Check the symbol library table for the part defined by \a aLibId and optionally
  71. * check the optional cache library.
  72. *
  73. * @param aLibId is the symbol library identifier to load.
  74. * @param aLibTable is the #SYMBOL_LIBRARY_TABLE to load the alias from.
  75. * @param aCacheLib is an optional cache library.
  76. * @param aParent is an optional parent window when displaying an error message.
  77. * @param aShowErrorMessage set to true to show any error messages.
  78. *
  79. * @return The symbol found in the library or NULL if the symbol was not found.
  80. */
  81. LIB_SYMBOL* SchGetLibSymbol( const LIB_ID& aLibId, SYMBOL_LIB_TABLE* aLibTable,
  82. SYMBOL_LIB* aCacheLib = nullptr, wxWindow* aParent = nullptr,
  83. bool aShowErrorMsg = false );
  84. /**
  85. * A shim class between EDA_DRAW_FRAME and several derived classes:
  86. * SYMBOL_EDIT_FRAME, SYMBOL_VIEWER_FRAME, and SCH_EDIT_FRAME, and it brings in a
  87. * common way of handling the provided virtual functions for the derived classes.
  88. *
  89. * The motivation here is to switch onto GetScreen() for the underlying data model.
  90. *
  91. * @author Dick Hollenbeck
  92. */
  93. class SCH_BASE_FRAME : public EDA_DRAW_FRAME
  94. {
  95. public:
  96. SCH_BASE_FRAME( KIWAY* aKiway, wxWindow* aParent, FRAME_T aWindowType, const wxString& aTitle,
  97. const wxPoint& aPosition, const wxSize& aSize, long aStyle,
  98. const wxString & aFrameName );
  99. virtual ~SCH_BASE_FRAME();
  100. void createCanvas();
  101. SCH_DRAW_PANEL* GetCanvas() const override;
  102. SCH_SCREEN* GetScreen() const override;
  103. EESCHEMA_SETTINGS* eeconfig() const;
  104. SYMBOL_EDITOR_SETTINGS* libeditconfig() const;
  105. void LoadSettings( APP_SETTINGS_BASE* aCfg ) override;
  106. void SaveSettings( APP_SETTINGS_BASE* aCfg ) override;
  107. SCH_RENDER_SETTINGS* GetRenderSettings();
  108. COLOR4D GetDrawBgColor() const override;
  109. /**
  110. * Allow some frames to show/hide hidden pins. The default impl shows all pins.
  111. */
  112. virtual bool GetShowAllPins() const { return true; }
  113. void SetPageSettings( const PAGE_INFO& aPageSettings ) override;
  114. const PAGE_INFO& GetPageSettings () const override;
  115. const VECTOR2I GetPageSizeIU() const override;
  116. const VECTOR2I& GetGridOrigin() const override
  117. {
  118. static VECTOR2I zero;
  119. return zero;
  120. }
  121. void SetGridOrigin( const VECTOR2I& aPoint ) override {}
  122. const TITLE_BLOCK& GetTitleBlock() const override;
  123. void SetTitleBlock( const TITLE_BLOCK& aTitleBlock ) override;
  124. void UpdateStatusBar() override;
  125. /**
  126. * Call the library viewer to select symbol to import into schematic.
  127. * If the library viewer is currently running, it is closed and reopened in modal mode.
  128. *
  129. * aAllowFields chooses whether or not features that permit the user to edit fields
  130. * (e.g. footprint selection) should be enabled. This should be false when they would
  131. * have no effect, for example loading a part into symbol_editor.
  132. *
  133. * @param aFilter is an optional #SYMBOL_LIBRARY_FILTER filter to pass the allowed library names
  134. * and/or the library name to load the symbol from and/or some other filter
  135. * @param aHistoryList is the list of previously loaded symbols - will be edited
  136. * @param aHighlight is the name of symbol to highlight in the list.
  137. * highlights none if there isn't one by that name.
  138. * @param aShowFootprints is the whether to show footprints in the dialog.
  139. * @param aAllowFields is whether to allow field editing in the dialog.
  140. *
  141. * @return the selected symbol
  142. */
  143. PICKED_SYMBOL PickSymbolFromLibrary( const SYMBOL_LIBRARY_FILTER* aFilter,
  144. std::vector<PICKED_SYMBOL>& aHistoryList,
  145. std::vector<PICKED_SYMBOL>& aAlreadyPlaced,
  146. bool aShowFootprints, const LIB_ID* aHighlight = nullptr,
  147. bool aAllowFields = true );
  148. /**
  149. * Load symbol from symbol library table.
  150. *
  151. * @param aLibId is the symbol library identifier to load.
  152. * @param aUseCacheLib set to true to fall back to cache library if symbol is not found in
  153. * symbol library table.
  154. * @param aShowErrorMessage set to true to show any error messages.
  155. * @return The symbol found in the library or NULL if the symbol was not found.
  156. */
  157. LIB_SYMBOL* GetLibSymbol( const LIB_ID& aLibId, bool aUseCacheLib = false,
  158. bool aShowErrorMsg = false );
  159. /**
  160. * Display a list of loaded libraries and allows the user to select a library.
  161. *
  162. * This list is sorted, with the library cache always at end of the list
  163. *
  164. * @return the library nickname used in the symbol library table.
  165. */
  166. wxString SelectLibraryFromList();
  167. /**
  168. * Display a dialog asking the user to select a symbol library table.
  169. *
  170. * @param aOptional if set the Cancel button will be relabelled "Skip".
  171. * @return Pointer to the selected symbol library table or nullptr if canceled.
  172. */
  173. SYMBOL_LIB_TABLE* SelectSymLibTable( bool aOptional = false );
  174. virtual void RedrawScreen( const VECTOR2I& aCenterPoint, bool aWarpPointer );
  175. void HardRedraw() override;
  176. /**
  177. * Add an item to the screen (and view)
  178. * aScreen is the screen the item is located on, if not the current screen
  179. */
  180. void AddToScreen( EDA_ITEM* aItem, SCH_SCREEN* aScreen = nullptr );
  181. /**
  182. * Remove an item from the screen (and view)
  183. * aScreen is the screen the item is located on, if not the current screen
  184. */
  185. void RemoveFromScreen( EDA_ITEM* aItem, SCH_SCREEN* aScreen );
  186. /**
  187. * Mark an item for refresh.
  188. */
  189. virtual void UpdateItem( EDA_ITEM* aItem, bool isAddOrDelete = false,
  190. bool aUpdateRtree = false );
  191. /**
  192. * Mark selected items for refresh.
  193. */
  194. void RefreshZoomDependentItems();
  195. /**
  196. * Mark all items for refresh.
  197. */
  198. void SyncView();
  199. void CommonSettingsChanged( bool aEnvVarsChanged, bool aTextVarsChanged ) override;
  200. /**
  201. * Helper to retrieve a layer color from the global color settings
  202. */
  203. COLOR4D GetLayerColor( SCH_LAYER_ID aLayer );
  204. COLOR_SETTINGS* GetColorSettings( bool aForceRefresh = false ) const override;
  205. void ActivateGalCanvas() override;
  206. /**
  207. * Handler for Symbol change events. Responds to the filesystem watcher set in #setSymWatcher.
  208. */
  209. void OnSymChange( wxFileSystemWatcherEvent& aEvent );
  210. /**
  211. * Handler for the filesystem watcher debounce timer.
  212. */
  213. void OnSymChangeDebounceTimer( wxTimerEvent& aEvent );
  214. /**
  215. * Set the modification time of the symbol library table file.
  216. *
  217. * This is used to detect changes to the symbol library table file.
  218. *
  219. * @param aTime is the modification time of the symbol library table file.
  220. */
  221. void SetSymModificationTime( const wxDateTime& aTime )
  222. {
  223. m_watcherLastModified = aTime;
  224. }
  225. protected:
  226. void handleActivateEvent( wxActivateEvent& aEvent ) override;
  227. void handleIconizeEvent( wxIconizeEvent& aEvent ) override;
  228. /**
  229. * Save Symbol Library Tables to disk.
  230. *
  231. * @param aGlobal when true, the Global Table is saved.
  232. * @param aProject when true, the Project Table is saved.
  233. * @return True when all requested actions succeeded.
  234. */
  235. bool saveSymbolLibTables( bool aGlobal, bool aProject );
  236. /**
  237. * Creates (or removes) a watcher on the specified symbol library
  238. * @param aSymbol If nullptr, the watcher is removed. Otherwise, set a change watcher
  239. */
  240. void setSymWatcher( const LIB_ID* aSymbol );
  241. /**
  242. * Selection filter panel doesn't have a dedicated visibility control, so show it if any
  243. * other AUI panel is shown and docked
  244. */
  245. virtual void updateSelectionFilterVisbility() {}
  246. /// These are only used by symbol_editor. Eeschema should be using the one inside
  247. /// the SCHEMATIC.
  248. SCHEMATIC_SETTINGS m_base_frame_defaults;
  249. PANEL_SCH_SELECTION_FILTER* m_selectionFilterPanel;
  250. private:
  251. /// These are file watchers for the symbol library tables.
  252. std::unique_ptr<wxFileSystemWatcher> m_watcher;
  253. wxFileName m_watcherFileName;
  254. wxDateTime m_watcherLastModified;
  255. wxTimer m_watcherDebounceTimer;
  256. NL_SCHEMATIC_PLUGIN* m_spaceMouse;
  257. };
  258. #endif // SCH_BASE_FRAME_H_