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.

291 lines
10 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/fswatcher.h>
  36. #include <wx/datetime.h>
  37. #include <wx/gdicmn.h>
  38. #include <wx/string.h>
  39. #include <wx/timer.h>
  40. #include <template_fieldnames.h>
  41. namespace KIGFX
  42. {
  43. class SCH_RENDER_SETTINGS;
  44. }
  45. class PAGE_INFO;
  46. class TITLE_BLOCK;
  47. class SYMBOL_VIEWER_FRAME;
  48. class SYMBOL_EDIT_FRAME;
  49. class LIB_SYMBOL;
  50. class SYMBOL_LIB;
  51. class SYMBOL_LIBRARY_FILTER;
  52. class LIB_ID;
  53. class SYMBOL_LIB_TABLE;
  54. class EESCHEMA_SETTINGS;
  55. class SYMBOL_EDITOR_SETTINGS;
  56. class NL_SCHEMATIC_PLUGIN;
  57. /**
  58. * Load symbol from symbol library table.
  59. *
  60. * Check the symbol library table for the part defined by \a aLibId and optionally
  61. * check the optional cache library.
  62. *
  63. * @param aLibId is the symbol library identifier to load.
  64. * @param aLibTable is the #SYMBOL_LIBRARY_TABLE to load the alias from.
  65. * @param aCacheLib is an optional cache library.
  66. * @param aParent is an optional parent window when displaying an error message.
  67. * @param aShowErrorMessage set to true to show any error messages.
  68. *
  69. * @return The symbol found in the library or NULL if the symbol was not found.
  70. */
  71. LIB_SYMBOL* SchGetLibSymbol( const LIB_ID& aLibId, SYMBOL_LIB_TABLE* aLibTable,
  72. SYMBOL_LIB* aCacheLib = nullptr, wxWindow* aParent = nullptr,
  73. bool aShowErrorMsg = false );
  74. /**
  75. * A shim class between EDA_DRAW_FRAME and several derived classes:
  76. * SYMBOL_EDIT_FRAME, SYMBOL_VIEWER_FRAME, and SCH_EDIT_FRAME, and it brings in a
  77. * common way of handling the provided virtual functions for the derived classes.
  78. *
  79. * The motivation here is to switch onto GetScreen() for the underlying data model.
  80. *
  81. * @author Dick Hollenbeck
  82. */
  83. class SCH_BASE_FRAME : public EDA_DRAW_FRAME
  84. {
  85. public:
  86. SCH_BASE_FRAME( KIWAY* aKiway, wxWindow* aParent, FRAME_T aWindowType, const wxString& aTitle,
  87. const wxPoint& aPosition, const wxSize& aSize, long aStyle,
  88. const wxString & aFrameName );
  89. virtual ~SCH_BASE_FRAME();
  90. void createCanvas();
  91. SCH_DRAW_PANEL* GetCanvas() const override;
  92. SCH_SCREEN* GetScreen() const override;
  93. EESCHEMA_SETTINGS* eeconfig() const;
  94. SYMBOL_EDITOR_SETTINGS* libeditconfig() const;
  95. void LoadSettings( APP_SETTINGS_BASE* aCfg ) override;
  96. void SaveSettings( APP_SETTINGS_BASE* aCfg ) override;
  97. KIGFX::SCH_RENDER_SETTINGS* GetRenderSettings();
  98. COLOR4D GetDrawBgColor() const override;
  99. /**
  100. * Allow some frames to show/hide hidden pins. The default impl shows all pins.
  101. */
  102. virtual bool GetShowAllPins() const { return true; }
  103. void SetPageSettings( const PAGE_INFO& aPageSettings ) override;
  104. const PAGE_INFO& GetPageSettings () const override;
  105. const VECTOR2I GetPageSizeIU() const override;
  106. const VECTOR2I& GetGridOrigin() const override
  107. {
  108. static VECTOR2I zero;
  109. return zero;
  110. }
  111. void SetGridOrigin( const VECTOR2I& aPoint ) override {}
  112. const TITLE_BLOCK& GetTitleBlock() const override;
  113. void SetTitleBlock( const TITLE_BLOCK& aTitleBlock ) override;
  114. void UpdateStatusBar() override;
  115. /**
  116. * Call the library viewer to select symbol to import into schematic.
  117. * If the library viewer is currently running, it is closed and reopened in modal mode.
  118. *
  119. * aAllowFields chooses whether or not features that permit the user to edit fields
  120. * (e.g. footprint selection) should be enabled. This should be false when they would
  121. * have no effect, for example loading a part into symbol_editor.
  122. *
  123. * @param aFilter is an optional #SYMBOL_LIBRARY_FILTER filter to pass the allowed library names
  124. * and/or the library name to load the symbol from and/or some other filter
  125. * @param aHistoryList is the list of previously loaded symbols - will be edited
  126. * @param aHighlight is the name of symbol to highlight in the list.
  127. * highlights none if there isn't one by that name.
  128. * @param aShowFootprints is the whether to show footprints in the dialog.
  129. * @param aAllowFields is whether to allow field editing in the dialog.
  130. *
  131. * @return the selected symbol
  132. */
  133. PICKED_SYMBOL PickSymbolFromLibrary( const SYMBOL_LIBRARY_FILTER* aFilter,
  134. std::vector<PICKED_SYMBOL>& aHistoryList,
  135. std::vector<PICKED_SYMBOL>& aAlreadyPlaced,
  136. bool aShowFootprints, const LIB_ID* aHighlight = nullptr,
  137. bool aAllowFields = true );
  138. /**
  139. * Load symbol from symbol library table.
  140. *
  141. * @param aLibId is the symbol library identifier to load.
  142. * @param aUseCacheLib set to true to fall back to cache library if symbol is not found in
  143. * symbol library table.
  144. * @param aShowErrorMessage set to true to show any error messages.
  145. * @return The symbol found in the library or NULL if the symbol was not found.
  146. */
  147. LIB_SYMBOL* GetLibSymbol( const LIB_ID& aLibId, bool aUseCacheLib = false,
  148. bool aShowErrorMsg = false );
  149. /**
  150. * Display a list of loaded libraries and allows the user to select a library.
  151. *
  152. * This list is sorted, with the library cache always at end of the list
  153. *
  154. * @return the library nickname used in the symbol library table.
  155. */
  156. wxString SelectLibraryFromList();
  157. /**
  158. * Display a dialog asking the user to select a symbol library table.
  159. *
  160. * @param aOptional if set the Cancel button will be relabelled "Skip".
  161. * @return Pointer to the selected symbol library table or nullptr if canceled.
  162. */
  163. SYMBOL_LIB_TABLE* SelectSymLibTable( bool aOptional = false );
  164. virtual void RedrawScreen( const VECTOR2I& aCenterPoint, bool aWarpPointer );
  165. void HardRedraw() override;
  166. /**
  167. * Add an item to the screen (and view)
  168. * aScreen is the screen the item is located on, if not the current screen
  169. */
  170. void AddToScreen( EDA_ITEM* aItem, SCH_SCREEN* aScreen = nullptr );
  171. /**
  172. * Remove an item from the screen (and view)
  173. * aScreen is the screen the item is located on, if not the current screen
  174. */
  175. void RemoveFromScreen( EDA_ITEM* aItem, SCH_SCREEN* aScreen );
  176. /**
  177. * Mark an item for refresh.
  178. */
  179. virtual void UpdateItem( EDA_ITEM* aItem, bool isAddOrDelete = false,
  180. bool aUpdateRtree = false );
  181. /**
  182. * Mark selected items for refresh.
  183. */
  184. void RefreshZoomDependentItems();
  185. /**
  186. * Mark all items for refresh.
  187. */
  188. void SyncView();
  189. void CommonSettingsChanged( bool aEnvVarsChanged, bool aTextVarsChanged ) override;
  190. /**
  191. * Helper to retrieve a layer color from the global color settings
  192. */
  193. COLOR4D GetLayerColor( SCH_LAYER_ID aLayer );
  194. COLOR_SETTINGS* GetColorSettings( bool aForceRefresh = false ) const override;
  195. void ActivateGalCanvas() override;
  196. /**
  197. * Handler for Symbol change events. Responds to the filesystem watcher set in #setSymWatcher.
  198. */
  199. void OnSymChange( wxFileSystemWatcherEvent& aEvent );
  200. /**
  201. * Handler for the filesystem watcher debounce timer.
  202. */
  203. void OnSymChangeDebounceTimer( wxTimerEvent& aEvent );
  204. /**
  205. * Set the modification time of the symbol library table file.
  206. *
  207. * This is used to detect changes to the symbol library table file.
  208. *
  209. * @param aTime is the modification time of the symbol library table file.
  210. */
  211. void SetSymModificationTime( const wxDateTime& aTime )
  212. {
  213. m_watcherLastModified = aTime;
  214. }
  215. protected:
  216. void handleActivateEvent( wxActivateEvent& aEvent ) override;
  217. void handleIconizeEvent( wxIconizeEvent& aEvent ) override;
  218. /**
  219. * Save Symbol Library Tables to disk.
  220. *
  221. * @param aGlobal when true, the Global Table is saved.
  222. * @param aProject when true, the Project Table is saved.
  223. * @return True when all requested actions succeeded.
  224. */
  225. bool saveSymbolLibTables( bool aGlobal, bool aProject );
  226. /**
  227. * Creates (or removes) a watcher on the specified symbol library
  228. * @param aSymbol If nullptr, the watcher is removed. Otherwise, set a change watcher
  229. */
  230. void setSymWatcher( const LIB_ID* aSymbol );
  231. /// These are only used by symbol_editor. Eeschema should be using the one inside
  232. /// the SCHEMATIC.
  233. SCHEMATIC_SETTINGS m_base_frame_defaults;
  234. private:
  235. /// These are file watchers for the symbol library tables.
  236. std::unique_ptr<wxFileSystemWatcher> m_watcher;
  237. wxFileName m_watcherFileName;
  238. wxDateTime m_watcherLastModified;
  239. wxTimer m_watcherDebounceTimer;
  240. NL_SCHEMATIC_PLUGIN* m_spaceMouse;
  241. };
  242. #endif // SCH_BASE_FRAME_H_