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.

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