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.

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