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.

364 lines
7.5 KiB

15 years 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 (C) 2021-2024 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 <undo_redo_container.h>
  27. ITEM_PICKER::ITEM_PICKER()
  28. {
  29. m_undoRedoStatus = UNDO_REDO::UNSPECIFIED;
  30. SetItem( nullptr );
  31. m_pickerFlags = 0;
  32. m_link = nullptr;
  33. m_screen = nullptr;
  34. }
  35. ITEM_PICKER::ITEM_PICKER( BASE_SCREEN* aScreen, EDA_ITEM* aItem, UNDO_REDO aUndoRedoStatus )
  36. {
  37. m_undoRedoStatus = aUndoRedoStatus;
  38. SetItem( aItem );
  39. m_pickerFlags = 0;
  40. m_link = nullptr;
  41. m_screen = aScreen;
  42. }
  43. void ITEM_PICKER::SetItem( EDA_ITEM* aItem )
  44. {
  45. m_pickedItem = aItem;
  46. m_pickedItemType = aItem ? aItem->Type() : TYPE_NOT_INIT;
  47. }
  48. PICKED_ITEMS_LIST::PICKED_ITEMS_LIST()
  49. {
  50. }
  51. PICKED_ITEMS_LIST::~PICKED_ITEMS_LIST()
  52. {
  53. }
  54. void PICKED_ITEMS_LIST::PushItem( const ITEM_PICKER& aItem )
  55. {
  56. m_ItemsList.push_back( aItem );
  57. }
  58. ITEM_PICKER PICKED_ITEMS_LIST::PopItem()
  59. {
  60. ITEM_PICKER item;
  61. if( m_ItemsList.size() != 0 )
  62. {
  63. item = m_ItemsList.back();
  64. m_ItemsList.pop_back();
  65. }
  66. return item;
  67. }
  68. bool PICKED_ITEMS_LIST::ContainsItem( const EDA_ITEM* aItem ) const
  69. {
  70. for( const ITEM_PICKER& picker : m_ItemsList )
  71. {
  72. if( picker.GetItem() == aItem )
  73. return true;
  74. }
  75. return false;
  76. }
  77. bool PICKED_ITEMS_LIST::ContainsItemType( KICAD_T aItemType ) const
  78. {
  79. for( const ITEM_PICKER& picker : m_ItemsList )
  80. {
  81. const EDA_ITEM* item = picker.GetItem();
  82. wxCHECK2( item, continue );
  83. if( item->Type() == aItemType )
  84. return true;
  85. }
  86. return false;
  87. }
  88. int PICKED_ITEMS_LIST::FindItem( const EDA_ITEM* aItem ) const
  89. {
  90. for( size_t i = 0; i < m_ItemsList.size(); i++ )
  91. {
  92. if( m_ItemsList[i].GetItem() == aItem )
  93. return i;
  94. }
  95. return -1;
  96. }
  97. void PICKED_ITEMS_LIST::ClearItemsList()
  98. {
  99. m_ItemsList.clear();
  100. }
  101. void PICKED_ITEMS_LIST::ClearListAndDeleteItems( std::function<void(EDA_ITEM*)> aItemDeleter )
  102. {
  103. while( GetCount() > 0 )
  104. {
  105. ITEM_PICKER wrapper = PopItem();
  106. if( wrapper.GetItem() == nullptr ) // No more items in list.
  107. break;
  108. // The Link is an undo construct; it is always owned by the undo/redo container
  109. if( wrapper.GetLink() )
  110. aItemDeleter( wrapper.GetLink() );
  111. if( wrapper.GetFlags() & UR_TRANSIENT )
  112. {
  113. aItemDeleter( wrapper.GetItem() );
  114. }
  115. else if( wrapper.GetStatus() == UNDO_REDO::DELETED )
  116. {
  117. // This should really be replaced with UR_TRANSIENT, but currently many clients
  118. // (eeschema in particular) abuse this to achieve non-undo-related deletions.
  119. aItemDeleter( wrapper.GetItem() );
  120. }
  121. }
  122. }
  123. ITEM_PICKER PICKED_ITEMS_LIST::GetItemWrapper( unsigned int aIdx ) const
  124. {
  125. ITEM_PICKER picker;
  126. if( aIdx < m_ItemsList.size() )
  127. picker = m_ItemsList[aIdx];
  128. return picker;
  129. }
  130. EDA_ITEM* PICKED_ITEMS_LIST::GetPickedItem( unsigned int aIdx ) const
  131. {
  132. if( aIdx < m_ItemsList.size() )
  133. return m_ItemsList[aIdx].GetItem();
  134. return nullptr;
  135. }
  136. BASE_SCREEN* PICKED_ITEMS_LIST::GetScreenForItem( unsigned int aIdx ) const
  137. {
  138. if( aIdx < m_ItemsList.size() )
  139. return m_ItemsList[aIdx].GetScreen();
  140. return nullptr;
  141. }
  142. EDA_ITEM* PICKED_ITEMS_LIST::GetPickedItemLink( unsigned int aIdx ) const
  143. {
  144. if( aIdx < m_ItemsList.size() )
  145. return m_ItemsList[aIdx].GetLink();
  146. return nullptr;
  147. }
  148. UNDO_REDO PICKED_ITEMS_LIST::GetPickedItemStatus( unsigned int aIdx ) const
  149. {
  150. if( aIdx < m_ItemsList.size() )
  151. return m_ItemsList[aIdx].GetStatus();
  152. return UNDO_REDO::UNSPECIFIED;
  153. }
  154. EDA_ITEM_FLAGS PICKED_ITEMS_LIST::GetPickerFlags( unsigned aIdx ) const
  155. {
  156. if( aIdx < m_ItemsList.size() )
  157. return m_ItemsList[aIdx].GetFlags();
  158. return 0;
  159. }
  160. KIID PICKED_ITEMS_LIST::GetPickedItemGroupId( unsigned aIdx ) const
  161. {
  162. if( aIdx < m_ItemsList.size() )
  163. return m_ItemsList[aIdx].GetGroupId();
  164. return KIID();
  165. }
  166. bool PICKED_ITEMS_LIST::SetPickedItemGroupId( KIID aGroupId, unsigned aIdx )
  167. {
  168. if( aIdx < m_ItemsList.size() )
  169. {
  170. m_ItemsList[aIdx].SetGroupId( aGroupId );
  171. return true;
  172. }
  173. return false;
  174. }
  175. bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, unsigned aIdx )
  176. {
  177. if( aIdx < m_ItemsList.size() )
  178. {
  179. m_ItemsList[aIdx].SetItem( aItem );
  180. return true;
  181. }
  182. return false;
  183. }
  184. bool PICKED_ITEMS_LIST::SetPickedItemLink( EDA_ITEM* aLink, unsigned aIdx )
  185. {
  186. if( aIdx < m_ItemsList.size() )
  187. {
  188. m_ItemsList[aIdx].SetLink( aLink );
  189. return true;
  190. }
  191. return false;
  192. }
  193. bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, UNDO_REDO aStatus, unsigned aIdx )
  194. {
  195. if( aIdx < m_ItemsList.size() )
  196. {
  197. m_ItemsList[aIdx].SetItem( aItem );
  198. m_ItemsList[aIdx].SetStatus( aStatus );
  199. return true;
  200. }
  201. return false;
  202. }
  203. bool PICKED_ITEMS_LIST::SetPickedItemStatus( UNDO_REDO aStatus, unsigned aIdx )
  204. {
  205. if( aIdx < m_ItemsList.size() )
  206. {
  207. m_ItemsList[aIdx].SetStatus( aStatus );
  208. return true;
  209. }
  210. return false;
  211. }
  212. bool PICKED_ITEMS_LIST::SetPickerFlags( EDA_ITEM_FLAGS aFlags, unsigned aIdx )
  213. {
  214. if( aIdx < m_ItemsList.size() )
  215. {
  216. m_ItemsList[aIdx].SetFlags( aFlags );
  217. return true;
  218. }
  219. return false;
  220. }
  221. bool PICKED_ITEMS_LIST::RemovePicker( unsigned aIdx )
  222. {
  223. if( aIdx >= m_ItemsList.size() )
  224. return false;
  225. m_ItemsList.erase( m_ItemsList.begin() + aIdx );
  226. return true;
  227. }
  228. void PICKED_ITEMS_LIST::CopyList( const PICKED_ITEMS_LIST& aSource )
  229. {
  230. m_ItemsList = aSource.m_ItemsList; // Vector's copy
  231. }
  232. void PICKED_ITEMS_LIST::ReversePickersListOrder()
  233. {
  234. std::vector <ITEM_PICKER> tmp;
  235. while( !m_ItemsList.empty() )
  236. {
  237. tmp.push_back( m_ItemsList.back() );
  238. m_ItemsList.pop_back();
  239. }
  240. m_ItemsList.swap( tmp );
  241. }
  242. UNDO_REDO_CONTAINER::UNDO_REDO_CONTAINER()
  243. {
  244. }
  245. UNDO_REDO_CONTAINER::~UNDO_REDO_CONTAINER()
  246. {
  247. ClearCommandList();
  248. }
  249. void UNDO_REDO_CONTAINER::ClearCommandList()
  250. {
  251. for( unsigned ii = 0; ii < m_CommandsList.size(); ii++ )
  252. delete m_CommandsList[ii];
  253. m_CommandsList.clear();
  254. }
  255. void UNDO_REDO_CONTAINER::PushCommand( PICKED_ITEMS_LIST* aItem )
  256. {
  257. m_CommandsList.push_back( aItem );
  258. }
  259. PICKED_ITEMS_LIST* UNDO_REDO_CONTAINER::PopCommand()
  260. {
  261. if( m_CommandsList.size() != 0 )
  262. {
  263. PICKED_ITEMS_LIST* item = m_CommandsList.back();
  264. m_CommandsList.pop_back();
  265. return item;
  266. }
  267. return nullptr;
  268. }