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.

341 lines
7.1 KiB

7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
15 years ago
7 months ago
7 months ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 jp.charras at wanadoo.fr
  5. * Copyright (C) 2011 Wayne Stambaugh <stambaughw@gmail.com>
  6. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <eda_item.h>
  26. #include <eda_group.h>
  27. #include <undo_redo_container.h>
  28. ITEM_PICKER::ITEM_PICKER()
  29. {
  30. m_undoRedoStatus = UNDO_REDO::UNSPECIFIED;
  31. SetItem( nullptr );
  32. m_pickerFlags = 0;
  33. m_link = nullptr;
  34. m_screen = nullptr;
  35. }
  36. ITEM_PICKER::ITEM_PICKER( BASE_SCREEN* aScreen, EDA_ITEM* aItem, UNDO_REDO aUndoRedoStatus )
  37. {
  38. m_undoRedoStatus = aUndoRedoStatus;
  39. SetItem( aItem );
  40. m_pickerFlags = 0;
  41. m_link = nullptr;
  42. m_screen = aScreen;
  43. }
  44. PICKED_ITEMS_LIST::PICKED_ITEMS_LIST()
  45. {
  46. }
  47. PICKED_ITEMS_LIST::~PICKED_ITEMS_LIST()
  48. {
  49. }
  50. void ITEM_PICKER::SetItem( EDA_ITEM* aItem )
  51. {
  52. m_pickedItem = nullptr;
  53. m_pickedItemType = TYPE_NOT_INIT;
  54. if( aItem )
  55. {
  56. m_pickedItem = aItem;
  57. m_pickedItemType = aItem->Type();
  58. if( EDA_GROUP* group = dynamic_cast<EDA_GROUP*>( aItem ) )
  59. m_groupMembers = group->GetGroupMemberIds();
  60. m_groupId = aItem->GetParentGroupId();
  61. }
  62. }
  63. void ITEM_PICKER::SetLink( EDA_ITEM* aItem )
  64. {
  65. m_link = aItem;
  66. if( aItem )
  67. {
  68. if( EDA_GROUP* group = dynamic_cast<EDA_GROUP*>( aItem ) )
  69. m_groupMembers = group->GetGroupMemberIds();
  70. m_groupId = aItem->GetParentGroupId();
  71. }
  72. }
  73. void PICKED_ITEMS_LIST::PushItem( const ITEM_PICKER& aItem )
  74. {
  75. m_ItemsList.push_back( aItem );
  76. }
  77. ITEM_PICKER PICKED_ITEMS_LIST::PopItem()
  78. {
  79. ITEM_PICKER item;
  80. if( m_ItemsList.size() != 0 )
  81. {
  82. item = m_ItemsList.back();
  83. m_ItemsList.pop_back();
  84. }
  85. return item;
  86. }
  87. bool PICKED_ITEMS_LIST::ContainsItem( const EDA_ITEM* aItem ) const
  88. {
  89. for( const ITEM_PICKER& picker : m_ItemsList )
  90. {
  91. if( picker.GetItem() == aItem )
  92. return true;
  93. }
  94. return false;
  95. }
  96. int PICKED_ITEMS_LIST::FindItem( const EDA_ITEM* aItem ) const
  97. {
  98. for( size_t i = 0; i < m_ItemsList.size(); i++ )
  99. {
  100. if( m_ItemsList[i].GetItem() == aItem )
  101. return i;
  102. }
  103. return -1;
  104. }
  105. void PICKED_ITEMS_LIST::ClearItemsList()
  106. {
  107. m_ItemsList.clear();
  108. }
  109. void PICKED_ITEMS_LIST::ClearListAndDeleteItems( std::function<void(EDA_ITEM*)> aItemDeleter )
  110. {
  111. while( GetCount() > 0 )
  112. {
  113. ITEM_PICKER wrapper = PopItem();
  114. if( wrapper.GetItem() == nullptr ) // No more items in list.
  115. break;
  116. // The Link is an undo construct; it is always owned by the undo/redo container
  117. if( wrapper.GetLink() )
  118. aItemDeleter( wrapper.GetLink() );
  119. if( wrapper.GetFlags() & UR_TRANSIENT )
  120. {
  121. aItemDeleter( wrapper.GetItem() );
  122. }
  123. else if( wrapper.GetStatus() == UNDO_REDO::DELETED )
  124. {
  125. // This should really be replaced with UR_TRANSIENT, but currently many clients
  126. // (eeschema in particular) abuse this to achieve non-undo-related deletions.
  127. aItemDeleter( wrapper.GetItem() );
  128. }
  129. }
  130. }
  131. const ITEM_PICKER& PICKED_ITEMS_LIST::GetItemWrapper( unsigned int aIdx ) const
  132. {
  133. return m_ItemsList.at( aIdx );
  134. }
  135. ITEM_PICKER& PICKED_ITEMS_LIST::GetItemWrapper( unsigned int aIdx )
  136. {
  137. return m_ItemsList.at( aIdx );
  138. }
  139. EDA_ITEM* PICKED_ITEMS_LIST::GetPickedItem( unsigned int aIdx ) const
  140. {
  141. if( aIdx < m_ItemsList.size() )
  142. return m_ItemsList[aIdx].GetItem();
  143. return nullptr;
  144. }
  145. BASE_SCREEN* PICKED_ITEMS_LIST::GetScreenForItem( unsigned int aIdx ) const
  146. {
  147. if( aIdx < m_ItemsList.size() )
  148. return m_ItemsList[aIdx].GetScreen();
  149. return nullptr;
  150. }
  151. EDA_ITEM* PICKED_ITEMS_LIST::GetPickedItemLink( unsigned int aIdx ) const
  152. {
  153. if( aIdx < m_ItemsList.size() )
  154. return m_ItemsList[aIdx].GetLink();
  155. return nullptr;
  156. }
  157. UNDO_REDO PICKED_ITEMS_LIST::GetPickedItemStatus( unsigned int aIdx ) const
  158. {
  159. if( aIdx < m_ItemsList.size() )
  160. return m_ItemsList[aIdx].GetStatus();
  161. return UNDO_REDO::UNSPECIFIED;
  162. }
  163. EDA_ITEM_FLAGS PICKED_ITEMS_LIST::GetPickerFlags( unsigned aIdx ) const
  164. {
  165. if( aIdx < m_ItemsList.size() )
  166. return m_ItemsList[aIdx].GetFlags();
  167. return 0;
  168. }
  169. bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, unsigned aIdx )
  170. {
  171. if( aIdx < m_ItemsList.size() )
  172. {
  173. m_ItemsList[aIdx].SetItem( aItem );
  174. return true;
  175. }
  176. return false;
  177. }
  178. bool PICKED_ITEMS_LIST::SetPickedItemLink( EDA_ITEM* aLink, unsigned aIdx )
  179. {
  180. if( aIdx < m_ItemsList.size() )
  181. {
  182. m_ItemsList[aIdx].SetLink( aLink );
  183. return true;
  184. }
  185. return false;
  186. }
  187. bool PICKED_ITEMS_LIST::SetPickedItemStatus( UNDO_REDO aStatus, unsigned aIdx )
  188. {
  189. if( aIdx < m_ItemsList.size() )
  190. {
  191. m_ItemsList[aIdx].SetStatus( aStatus );
  192. return true;
  193. }
  194. return false;
  195. }
  196. bool PICKED_ITEMS_LIST::SetPickerFlags( EDA_ITEM_FLAGS aFlags, unsigned aIdx )
  197. {
  198. if( aIdx < m_ItemsList.size() )
  199. {
  200. m_ItemsList[aIdx].SetFlags( aFlags );
  201. return true;
  202. }
  203. return false;
  204. }
  205. bool PICKED_ITEMS_LIST::RemovePicker( unsigned aIdx )
  206. {
  207. if( aIdx >= m_ItemsList.size() )
  208. return false;
  209. m_ItemsList.erase( m_ItemsList.begin() + aIdx );
  210. return true;
  211. }
  212. void PICKED_ITEMS_LIST::CopyList( const PICKED_ITEMS_LIST& aSource )
  213. {
  214. m_ItemsList = aSource.m_ItemsList; // Vector's copy
  215. }
  216. void PICKED_ITEMS_LIST::ReversePickersListOrder()
  217. {
  218. std::vector <ITEM_PICKER> tmp;
  219. while( !m_ItemsList.empty() )
  220. {
  221. tmp.push_back( m_ItemsList.back() );
  222. m_ItemsList.pop_back();
  223. }
  224. m_ItemsList.swap( tmp );
  225. }
  226. UNDO_REDO_CONTAINER::UNDO_REDO_CONTAINER()
  227. {
  228. }
  229. UNDO_REDO_CONTAINER::~UNDO_REDO_CONTAINER()
  230. {
  231. ClearCommandList();
  232. }
  233. void UNDO_REDO_CONTAINER::ClearCommandList()
  234. {
  235. for( unsigned ii = 0; ii < m_CommandsList.size(); ii++ )
  236. delete m_CommandsList[ii];
  237. m_CommandsList.clear();
  238. }
  239. void UNDO_REDO_CONTAINER::PushCommand( PICKED_ITEMS_LIST* aItem )
  240. {
  241. m_CommandsList.push_back( aItem );
  242. }
  243. PICKED_ITEMS_LIST* UNDO_REDO_CONTAINER::PopCommand()
  244. {
  245. if( m_CommandsList.size() != 0 )
  246. {
  247. PICKED_ITEMS_LIST* item = m_CommandsList.back();
  248. m_CommandsList.pop_back();
  249. return item;
  250. }
  251. return nullptr;
  252. }