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.

328 lines
6.9 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 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. int PICKED_ITEMS_LIST::FindItem( const EDA_ITEM* aItem ) const
  78. {
  79. for( size_t i = 0; i < m_ItemsList.size(); i++ )
  80. {
  81. if( m_ItemsList[i].GetItem() == aItem )
  82. return i;
  83. }
  84. return -1;
  85. }
  86. void PICKED_ITEMS_LIST::ClearItemsList()
  87. {
  88. m_ItemsList.clear();
  89. }
  90. void PICKED_ITEMS_LIST::ClearListAndDeleteItems()
  91. {
  92. // Delete items is they are not flagged NEWITEM, or if this is a block operation
  93. while( GetCount() > 0 )
  94. {
  95. ITEM_PICKER wrapper = PopItem();
  96. if( wrapper.GetItem() == nullptr ) // No more items in list.
  97. break;
  98. // The Link is an undo construct; it is always owned by the undo/redo container
  99. if( wrapper.GetLink() )
  100. delete wrapper.GetLink();
  101. if( wrapper.GetFlags() & UR_TRANSIENT )
  102. {
  103. delete wrapper.GetItem();
  104. }
  105. else if( wrapper.GetStatus() == UNDO_REDO::DELETED )
  106. {
  107. // This should really be replaced with UR_TRANSIENT, but currently many clients
  108. // (eeschema in particular) abuse this to achieve non-undo-related deletions.
  109. delete wrapper.GetItem();
  110. }
  111. }
  112. }
  113. ITEM_PICKER PICKED_ITEMS_LIST::GetItemWrapper( unsigned int aIdx ) const
  114. {
  115. ITEM_PICKER picker;
  116. if( aIdx < m_ItemsList.size() )
  117. picker = m_ItemsList[aIdx];
  118. return picker;
  119. }
  120. EDA_ITEM* PICKED_ITEMS_LIST::GetPickedItem( unsigned int aIdx ) const
  121. {
  122. if( aIdx < m_ItemsList.size() )
  123. return m_ItemsList[aIdx].GetItem();
  124. return nullptr;
  125. }
  126. BASE_SCREEN* PICKED_ITEMS_LIST::GetScreenForItem( unsigned int aIdx ) const
  127. {
  128. if( aIdx < m_ItemsList.size() )
  129. return m_ItemsList[aIdx].GetScreen();
  130. return nullptr;
  131. }
  132. EDA_ITEM* PICKED_ITEMS_LIST::GetPickedItemLink( unsigned int aIdx ) const
  133. {
  134. if( aIdx < m_ItemsList.size() )
  135. return m_ItemsList[aIdx].GetLink();
  136. return nullptr;
  137. }
  138. UNDO_REDO PICKED_ITEMS_LIST::GetPickedItemStatus( unsigned int aIdx ) const
  139. {
  140. if( aIdx < m_ItemsList.size() )
  141. return m_ItemsList[aIdx].GetStatus();
  142. return UNDO_REDO::UNSPECIFIED;
  143. }
  144. EDA_ITEM_FLAGS PICKED_ITEMS_LIST::GetPickerFlags( unsigned aIdx ) const
  145. {
  146. if( aIdx < m_ItemsList.size() )
  147. return m_ItemsList[aIdx].GetFlags();
  148. return 0;
  149. }
  150. bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, unsigned aIdx )
  151. {
  152. if( aIdx < m_ItemsList.size() )
  153. {
  154. m_ItemsList[aIdx].SetItem( aItem );
  155. return true;
  156. }
  157. return false;
  158. }
  159. bool PICKED_ITEMS_LIST::SetPickedItemLink( EDA_ITEM* aLink, unsigned aIdx )
  160. {
  161. if( aIdx < m_ItemsList.size() )
  162. {
  163. m_ItemsList[aIdx].SetLink( aLink );
  164. return true;
  165. }
  166. return false;
  167. }
  168. bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, UNDO_REDO aStatus, unsigned aIdx )
  169. {
  170. if( aIdx < m_ItemsList.size() )
  171. {
  172. m_ItemsList[aIdx].SetItem( aItem );
  173. m_ItemsList[aIdx].SetStatus( aStatus );
  174. return true;
  175. }
  176. return false;
  177. }
  178. bool PICKED_ITEMS_LIST::SetPickedItemStatus( UNDO_REDO aStatus, unsigned aIdx )
  179. {
  180. if( aIdx < m_ItemsList.size() )
  181. {
  182. m_ItemsList[aIdx].SetStatus( aStatus );
  183. return true;
  184. }
  185. return false;
  186. }
  187. bool PICKED_ITEMS_LIST::SetPickerFlags( EDA_ITEM_FLAGS aFlags, unsigned aIdx )
  188. {
  189. if( aIdx < m_ItemsList.size() )
  190. {
  191. m_ItemsList[aIdx].SetFlags( aFlags );
  192. return true;
  193. }
  194. return false;
  195. }
  196. bool PICKED_ITEMS_LIST::RemovePicker( unsigned aIdx )
  197. {
  198. if( aIdx >= m_ItemsList.size() )
  199. return false;
  200. m_ItemsList.erase( m_ItemsList.begin() + aIdx );
  201. return true;
  202. }
  203. void PICKED_ITEMS_LIST::CopyList( const PICKED_ITEMS_LIST& aSource )
  204. {
  205. m_ItemsList = aSource.m_ItemsList; // Vector's copy
  206. }
  207. void PICKED_ITEMS_LIST::ReversePickersListOrder()
  208. {
  209. std::vector <ITEM_PICKER> tmp;
  210. while( !m_ItemsList.empty() )
  211. {
  212. tmp.push_back( m_ItemsList.back() );
  213. m_ItemsList.pop_back();
  214. }
  215. m_ItemsList.swap( tmp );
  216. }
  217. UNDO_REDO_CONTAINER::UNDO_REDO_CONTAINER()
  218. {
  219. }
  220. UNDO_REDO_CONTAINER::~UNDO_REDO_CONTAINER()
  221. {
  222. ClearCommandList();
  223. }
  224. void UNDO_REDO_CONTAINER::ClearCommandList()
  225. {
  226. for( unsigned ii = 0; ii < m_CommandsList.size(); ii++ )
  227. delete m_CommandsList[ii];
  228. m_CommandsList.clear();
  229. }
  230. void UNDO_REDO_CONTAINER::PushCommand( PICKED_ITEMS_LIST* aItem )
  231. {
  232. m_CommandsList.push_back( aItem );
  233. }
  234. PICKED_ITEMS_LIST* UNDO_REDO_CONTAINER::PopCommand()
  235. {
  236. if( m_CommandsList.size() != 0 )
  237. {
  238. PICKED_ITEMS_LIST* item = m_CommandsList.back();
  239. m_CommandsList.pop_back();
  240. return item;
  241. }
  242. return nullptr;
  243. }