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.

122 lines
4.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2023 Ethan Chien <liangtie.qian@gmail.com>
  5. * Copyright (C) 2023 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 SHEET_SYNCHRONIZATION_MODEL_H
  25. #define SHEET_SYNCHRONIZATION_MODEL_H
  26. #include <sch_sheet_path.h>
  27. #include <memory>
  28. #include <optional>
  29. #include <list>
  30. #include <wx/dataview.h>
  31. #include <wx/string.h>
  32. class SHEET_SYNCHRONIZATION_ITEM;
  33. using SHEET_SYNCHRONIZATION_ITE_PTR = std::shared_ptr<SHEET_SYNCHRONIZATION_ITEM>;
  34. using SHEET_SYNCHRONIZATION_ITEM_LIST = std::vector<SHEET_SYNCHRONIZATION_ITE_PTR>;
  35. class SHEET_SYNCHRONIZATION_NOTIFIER;
  36. class SHEET_SYNCHRONIZATION_AGENT;
  37. class SCH_SHEET;
  38. class SCH_SHEET_PATH;
  39. class SHEET_SYNCHRONIZATION_MODEL : public wxDataViewVirtualListModel
  40. {
  41. public:
  42. enum SHEET_SYNCHRONIZATION_COL
  43. {
  44. NAME,
  45. SHAPE,
  46. COL_COUNT
  47. };
  48. enum
  49. {
  50. HIRE_LABEL,
  51. SHEET_PIN,
  52. ASSOCIATED,
  53. MODEL_COUNT
  54. };
  55. static wxString GetColName( int col )
  56. {
  57. switch( col )
  58. {
  59. case NAME: return _( "Name" );
  60. case SHAPE: return _( "Shape" );
  61. default: return {};
  62. }
  63. }
  64. SHEET_SYNCHRONIZATION_MODEL( SHEET_SYNCHRONIZATION_AGENT& aAgent, SCH_SHEET* aSheet,
  65. SCH_SHEET_PATH& aPath );
  66. ~SHEET_SYNCHRONIZATION_MODEL() override;
  67. void GetValueByRow( wxVariant& variant, unsigned row, unsigned col ) const override;
  68. bool SetValueByRow( const wxVariant& variant, unsigned row, unsigned col ) override;
  69. bool GetAttrByRow( unsigned row, unsigned int col, wxDataViewItemAttr& attr ) const override;
  70. void RemoveItems( wxDataViewItemArray const& aItems );
  71. /**
  72. * Add a new item, the notifiers are notified.
  73. */
  74. bool AppendNewItem( std::shared_ptr<SHEET_SYNCHRONIZATION_ITEM> aItem );
  75. /**
  76. * Just append item to the list, the notifiers are not notified.
  77. */
  78. bool AppendItem( std::shared_ptr<SHEET_SYNCHRONIZATION_ITEM> aItem );
  79. SHEET_SYNCHRONIZATION_ITEM_LIST TakeItems( wxDataViewItemArray const& aItems );
  80. SHEET_SYNCHRONIZATION_ITE_PTR TakeItem( wxDataViewItem const& aItem );
  81. SHEET_SYNCHRONIZATION_ITE_PTR GetSynchronizationItem( unsigned aIndex ) const;
  82. void OnRowSelected( std::optional<unsigned> aRow );
  83. void UpdateItems( SHEET_SYNCHRONIZATION_ITEM_LIST aItems );
  84. void AddNotifier( std::shared_ptr<SHEET_SYNCHRONIZATION_NOTIFIER> aNotifier );
  85. void DoNotify();
  86. bool HasSelectedIndex() const { return m_selectedIndex.has_value(); }
  87. std::optional<unsigned int> GetSelectedIndex() const { return m_selectedIndex; }
  88. private:
  89. SHEET_SYNCHRONIZATION_ITEM_LIST m_items;
  90. std::optional<unsigned> m_selectedIndex;
  91. std::list<std::shared_ptr<SHEET_SYNCHRONIZATION_NOTIFIER>> m_notifiers;
  92. SHEET_SYNCHRONIZATION_AGENT& m_agent;
  93. SCH_SHEET* m_sheet;
  94. SCH_SHEET_PATH m_path;
  95. };
  96. #endif