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.

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