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.

304 lines
6.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@verizon.net>
  6. * Copyright (C) 2018 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 <fctsys.h>
  26. #include <base_struct.h>
  27. #include <undo_redo_container.h>
  28. ITEM_PICKER::ITEM_PICKER( EDA_ITEM* aItem, UNDO_REDO_T aUndoRedoStatus )
  29. {
  30. m_undoRedoStatus = aUndoRedoStatus;
  31. SetItem( aItem );
  32. m_pickerFlags = 0;
  33. m_link = NULL;
  34. }
  35. PICKED_ITEMS_LIST::PICKED_ITEMS_LIST()
  36. {
  37. m_Status = UR_UNSPECIFIED;
  38. }
  39. PICKED_ITEMS_LIST::~PICKED_ITEMS_LIST()
  40. {
  41. }
  42. void PICKED_ITEMS_LIST::PushItem( const ITEM_PICKER& aItem )
  43. {
  44. m_ItemsList.push_back( aItem );
  45. }
  46. ITEM_PICKER PICKED_ITEMS_LIST::PopItem()
  47. {
  48. ITEM_PICKER item;
  49. if( m_ItemsList.size() != 0 )
  50. {
  51. item = m_ItemsList.back();
  52. m_ItemsList.pop_back();
  53. }
  54. return item;
  55. }
  56. bool PICKED_ITEMS_LIST::ContainsItem( const EDA_ITEM* aItem ) const
  57. {
  58. for( size_t i = 0; i < m_ItemsList.size(); i++ )
  59. {
  60. if( m_ItemsList[ i ].GetItem() == aItem )
  61. return true;
  62. }
  63. return false;
  64. }
  65. int PICKED_ITEMS_LIST::FindItem( const EDA_ITEM* aItem ) const
  66. {
  67. for( size_t i = 0; i < m_ItemsList.size(); i++ )
  68. {
  69. if( m_ItemsList[i].GetItem() == aItem )
  70. return i;
  71. }
  72. return -1;
  73. }
  74. void PICKED_ITEMS_LIST::ClearItemsList()
  75. {
  76. m_ItemsList.clear();
  77. }
  78. void PICKED_ITEMS_LIST::ClearListAndDeleteItems()
  79. {
  80. // Delete items is they are not flagged UR_NEW, or if this is a block operation
  81. while( GetCount() > 0 )
  82. {
  83. ITEM_PICKER wrapper = PopItem();
  84. if( wrapper.GetItem() == NULL ) // No more item in list.
  85. break;
  86. // The Link is an undo construct; it is always owned by the undo/redo container
  87. if( wrapper.GetLink() )
  88. delete wrapper.GetLink();
  89. if( wrapper.GetFlags() & UR_TRANSIENT )
  90. {
  91. delete wrapper.GetItem();
  92. }
  93. else if( wrapper.GetStatus() == UR_DELETED )
  94. {
  95. // This should really be replaced with UR_TRANSIENT, but currently many clients
  96. // (eeschema in particular) abuse this to achieve non-undo-related deletions.
  97. delete wrapper.GetItem();
  98. }
  99. }
  100. }
  101. ITEM_PICKER PICKED_ITEMS_LIST::GetItemWrapper( unsigned int aIdx ) const
  102. {
  103. ITEM_PICKER picker;
  104. if( aIdx < m_ItemsList.size() )
  105. picker = m_ItemsList[aIdx];
  106. return picker;
  107. }
  108. EDA_ITEM* PICKED_ITEMS_LIST::GetPickedItem( unsigned int aIdx ) const
  109. {
  110. if( aIdx < m_ItemsList.size() )
  111. return m_ItemsList[aIdx].GetItem();
  112. return NULL;
  113. }
  114. EDA_ITEM* PICKED_ITEMS_LIST::GetPickedItemLink( unsigned int aIdx ) const
  115. {
  116. if( aIdx < m_ItemsList.size() )
  117. return m_ItemsList[aIdx].GetLink();
  118. return NULL;
  119. }
  120. UNDO_REDO_T PICKED_ITEMS_LIST::GetPickedItemStatus( unsigned int aIdx ) const
  121. {
  122. if( aIdx < m_ItemsList.size() )
  123. return m_ItemsList[aIdx].GetStatus();
  124. return UR_UNSPECIFIED;
  125. }
  126. STATUS_FLAGS PICKED_ITEMS_LIST::GetPickerFlags( unsigned aIdx ) const
  127. {
  128. if( aIdx < m_ItemsList.size() )
  129. return m_ItemsList[aIdx].GetFlags();
  130. return 0;
  131. }
  132. bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, unsigned aIdx )
  133. {
  134. if( aIdx < m_ItemsList.size() )
  135. {
  136. m_ItemsList[aIdx].SetItem( aItem );
  137. return true;
  138. }
  139. return false;
  140. }
  141. bool PICKED_ITEMS_LIST::SetPickedItemLink( EDA_ITEM* aLink, unsigned aIdx )
  142. {
  143. if( aIdx < m_ItemsList.size() )
  144. {
  145. m_ItemsList[aIdx].SetLink( aLink );
  146. return true;
  147. }
  148. return false;
  149. }
  150. bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, UNDO_REDO_T aStatus, unsigned aIdx )
  151. {
  152. if( aIdx < m_ItemsList.size() )
  153. {
  154. m_ItemsList[aIdx].SetItem( aItem );
  155. m_ItemsList[aIdx].SetStatus( aStatus );
  156. return true;
  157. }
  158. return false;
  159. }
  160. bool PICKED_ITEMS_LIST::SetPickedItemStatus( UNDO_REDO_T aStatus, unsigned aIdx )
  161. {
  162. if( aIdx < m_ItemsList.size() )
  163. {
  164. m_ItemsList[aIdx].SetStatus( aStatus );
  165. return true;
  166. }
  167. return false;
  168. }
  169. bool PICKED_ITEMS_LIST::SetPickerFlags( STATUS_FLAGS aFlags, unsigned aIdx )
  170. {
  171. if( aIdx < m_ItemsList.size() )
  172. {
  173. m_ItemsList[aIdx].SetFlags( aFlags );
  174. return true;
  175. }
  176. return false;
  177. }
  178. bool PICKED_ITEMS_LIST::RemovePicker( unsigned aIdx )
  179. {
  180. if( aIdx >= m_ItemsList.size() )
  181. return false;
  182. m_ItemsList.erase( m_ItemsList.begin() + aIdx );
  183. return true;
  184. }
  185. void PICKED_ITEMS_LIST::CopyList( const PICKED_ITEMS_LIST& aSource )
  186. {
  187. m_ItemsList = aSource.m_ItemsList; // Vector's copy
  188. }
  189. void PICKED_ITEMS_LIST::ReversePickersListOrder()
  190. {
  191. std::vector <ITEM_PICKER> tmp;
  192. while( !m_ItemsList.empty() )
  193. {
  194. tmp.push_back( m_ItemsList.back() );
  195. m_ItemsList.pop_back();
  196. }
  197. m_ItemsList.swap( tmp );
  198. }
  199. /**********************************************/
  200. /********** UNDO_REDO_CONTAINER ***************/
  201. /**********************************************/
  202. UNDO_REDO_CONTAINER::UNDO_REDO_CONTAINER()
  203. {
  204. }
  205. UNDO_REDO_CONTAINER::~UNDO_REDO_CONTAINER()
  206. {
  207. ClearCommandList();
  208. }
  209. void UNDO_REDO_CONTAINER::ClearCommandList()
  210. {
  211. for( unsigned ii = 0; ii < m_CommandsList.size(); ii++ )
  212. delete m_CommandsList[ii];
  213. m_CommandsList.clear();
  214. }
  215. void UNDO_REDO_CONTAINER::PushCommand( PICKED_ITEMS_LIST* aItem )
  216. {
  217. m_CommandsList.push_back( aItem );
  218. }
  219. PICKED_ITEMS_LIST* UNDO_REDO_CONTAINER::PopCommand()
  220. {
  221. if( m_CommandsList.size() != 0 )
  222. {
  223. PICKED_ITEMS_LIST* item = m_CommandsList.back();
  224. m_CommandsList.pop_back();
  225. return item;
  226. }
  227. return NULL;
  228. }