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.

350 lines
10 KiB

4 years ago
5 years ago
5 years ago
5 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <board_commit.h>
  24. #include <confirm.h>
  25. #include <dialog_footprint_properties_fp_editor.h>
  26. #include <footprint_edit_frame.h>
  27. #include <footprint_tree_pane.h>
  28. #include <fp_lib_table.h>
  29. #include <functional>
  30. #include <kiway_express.h>
  31. #include <pcbnew_id.h>
  32. #include <ratsnest/ratsnest_data.h>
  33. #include <settings/color_settings.h>
  34. #include <tool/tool_manager.h>
  35. #include <tools/pcb_actions.h>
  36. #include <widgets/appearance_controls.h>
  37. #include <widgets/lib_tree.h>
  38. #include <pcb_layer_box_selector.h>
  39. #include <pcb_dimension.h>
  40. #include <dialogs/dialog_dimension_properties.h>
  41. using namespace std::placeholders;
  42. void FOOTPRINT_EDIT_FRAME::OnLoadFootprintFromBoard( wxCommandEvent& event )
  43. {
  44. LoadFootprintFromBoard( nullptr );
  45. }
  46. void FOOTPRINT_EDIT_FRAME::LoadFootprintFromLibrary( LIB_ID aFPID )
  47. {
  48. bool is_last_fp_from_brd = IsCurrentFPFromBoard();
  49. FOOTPRINT* footprint = LoadFootprint( aFPID );
  50. if( !footprint )
  51. return;
  52. if( !Clear_Pcb( true ) )
  53. return;
  54. GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
  55. AddFootprintToBoard( footprint );
  56. footprint->ClearFlags();
  57. // if either m_Reference or m_Value are gone, reinstall them -
  58. // otherwise you cannot see what you are doing on board
  59. FP_TEXT* ref = &footprint->Reference();
  60. FP_TEXT* val = &footprint->Value();
  61. if( val && ref )
  62. {
  63. ref->SetType( FP_TEXT::TEXT_is_REFERENCE ); // just in case ...
  64. if( ref->GetLength() == 0 )
  65. ref->SetText( wxT( "Ref**" ) );
  66. val->SetType( FP_TEXT::TEXT_is_VALUE ); // just in case ...
  67. if( val->GetLength() == 0 )
  68. val->SetText( wxT( "Val**" ) );
  69. }
  70. Zoom_Automatique( false );
  71. Update3DView( true, true );
  72. GetScreen()->SetContentModified( false );
  73. UpdateView();
  74. GetCanvas()->Refresh();
  75. // Update the save items if needed.
  76. if( is_last_fp_from_brd )
  77. {
  78. ReCreateMenuBar();
  79. ReCreateHToolbar();
  80. }
  81. m_treePane->GetLibTree()->ExpandLibId( aFPID );
  82. m_centerItemOnIdle = aFPID;
  83. Bind( wxEVT_IDLE, &FOOTPRINT_EDIT_FRAME::centerItemIdleHandler, this );
  84. m_treePane->GetLibTree()->RefreshLibTree(); // update highlighting
  85. }
  86. void FOOTPRINT_EDIT_FRAME::centerItemIdleHandler( wxIdleEvent& aEvent )
  87. {
  88. m_treePane->GetLibTree()->CenterLibId( m_centerItemOnIdle );
  89. Unbind( wxEVT_IDLE, &FOOTPRINT_EDIT_FRAME::centerItemIdleHandler, this );
  90. }
  91. void FOOTPRINT_EDIT_FRAME::SelectLayer( wxCommandEvent& event )
  92. {
  93. SetActiveLayer( ToLAYER_ID( m_selLayerBox->GetLayerSelection() ) );
  94. if( GetDisplayOptions().m_ContrastModeDisplay != HIGH_CONTRAST_MODE::NORMAL )
  95. GetCanvas()->Refresh();
  96. }
  97. void FOOTPRINT_EDIT_FRAME::OnSaveFootprintToBoard( wxCommandEvent& event )
  98. {
  99. SaveFootprintToBoard( true );
  100. }
  101. class BASIC_FOOTPRINT_INFO : public FOOTPRINT_INFO
  102. {
  103. public:
  104. BASIC_FOOTPRINT_INFO( FOOTPRINT* aFootprint )
  105. {
  106. m_nickname = aFootprint->GetFPID().GetLibNickname().wx_str();
  107. m_fpname = aFootprint->GetFPID().GetLibItemName().wx_str();
  108. m_pad_count = aFootprint->GetPadCount( DO_NOT_INCLUDE_NPTH );
  109. m_unique_pad_count = aFootprint->GetUniquePadCount( DO_NOT_INCLUDE_NPTH );
  110. m_keywords = aFootprint->GetKeywords();
  111. m_doc = aFootprint->GetDescription();
  112. m_loaded = true;
  113. }
  114. };
  115. void FOOTPRINT_EDIT_FRAME::UpdateLibraryTree( const wxDataViewItem& aTreeItem,
  116. FOOTPRINT* aFootprint )
  117. {
  118. BASIC_FOOTPRINT_INFO footprintInfo( aFootprint );
  119. if( aTreeItem.IsOk() ) // Can be not found in tree if the current footprint is imported
  120. // from file therefore not yet in tree.
  121. {
  122. static_cast<LIB_TREE_NODE_LIB_ID*>( aTreeItem.GetID() )->Update( &footprintInfo );
  123. m_treePane->GetLibTree()->RefreshLibTree();
  124. }
  125. }
  126. void FOOTPRINT_EDIT_FRAME::editFootprintProperties( FOOTPRINT* aFootprint )
  127. {
  128. LIB_ID oldFPID = aFootprint->GetFPID();
  129. DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR dialog( this, aFootprint );
  130. dialog.ShowModal();
  131. // Update library tree and title in case of a name change
  132. wxDataViewItem treeItem = m_adapter->FindItem( oldFPID );
  133. UpdateLibraryTree( treeItem, aFootprint );
  134. UpdateTitle();
  135. UpdateMsgPanel();
  136. }
  137. void FOOTPRINT_EDIT_FRAME::OnEditItemRequest( BOARD_ITEM* aItem )
  138. {
  139. switch( aItem->Type() )
  140. {
  141. case PCB_PAD_T:
  142. ShowPadPropertiesDialog( static_cast<PAD*>( aItem ) );
  143. break;
  144. case PCB_FOOTPRINT_T:
  145. editFootprintProperties( static_cast<FOOTPRINT*>( aItem ) );
  146. GetCanvas()->Refresh();
  147. break;
  148. case PCB_FP_TEXT_T:
  149. ShowTextPropertiesDialog( aItem );
  150. break;
  151. case PCB_FP_TEXTBOX_T:
  152. ShowTextBoxPropertiesDialog( aItem );
  153. break;
  154. case PCB_FP_SHAPE_T :
  155. ShowGraphicItemPropertiesDialog( aItem );
  156. break;
  157. case PCB_FP_DIM_ALIGNED_T:
  158. case PCB_FP_DIM_CENTER_T:
  159. case PCB_FP_DIM_RADIAL_T:
  160. case PCB_FP_DIM_ORTHOGONAL_T:
  161. case PCB_FP_DIM_LEADER_T:
  162. {
  163. DIALOG_DIMENSION_PROPERTIES dlg( this, static_cast<PCB_DIMENSION_BASE*>( aItem ) );
  164. dlg.ShowQuasiModal();
  165. break;
  166. }
  167. case PCB_FP_ZONE_T:
  168. {
  169. ZONE* zone = static_cast<ZONE*>( aItem );
  170. bool success = false;
  171. ZONE_SETTINGS zoneSettings;
  172. zoneSettings << *static_cast<ZONE*>( aItem );
  173. if( zone->GetIsRuleArea() )
  174. {
  175. success = InvokeRuleAreaEditor( this, &zoneSettings ) == wxID_OK;
  176. }
  177. else if( zone->IsOnCopperLayer() )
  178. {
  179. success = InvokeCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
  180. }
  181. else
  182. {
  183. success = InvokeNonCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
  184. }
  185. if( success )
  186. {
  187. BOARD_COMMIT commit( this );
  188. commit.Modify( zone );
  189. commit.Push( _( "Edit Zone" ) );
  190. zoneSettings.ExportSetting( *static_cast<ZONE*>( aItem ) );
  191. }
  192. break;
  193. }
  194. case PCB_GROUP_T:
  195. m_toolManager->RunAction( PCB_ACTIONS::groupProperties, true, aItem );
  196. break;
  197. default:
  198. wxFAIL_MSG( wxT( "FOOTPRINT_EDIT_FRAME::OnEditItemRequest: unsupported item type " )
  199. + aItem->GetClass() );
  200. break;
  201. }
  202. }
  203. COLOR4D FOOTPRINT_EDIT_FRAME::GetGridColor()
  204. {
  205. return GetColorSettings()->GetColor( LAYER_GRID );
  206. }
  207. void FOOTPRINT_EDIT_FRAME::SetActiveLayer( PCB_LAYER_ID aLayer )
  208. {
  209. PCB_BASE_FRAME::SetActiveLayer( aLayer );
  210. m_appearancePanel->OnLayerChanged();
  211. m_toolManager->RunAction( PCB_ACTIONS::layerChanged ); // notify other tools
  212. GetCanvas()->SetFocus(); // allow capture of hotkeys
  213. GetCanvas()->SetHighContrastLayer( aLayer );
  214. GetCanvas()->Refresh();
  215. }
  216. bool FOOTPRINT_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, int aCtl )
  217. {
  218. if( !Clear_Pcb( true ) )
  219. return false; // this command is aborted
  220. GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
  221. ImportFootprint( aFileSet[ 0 ] );
  222. if( GetBoard()->GetFirstFootprint() )
  223. GetBoard()->GetFirstFootprint()->ClearFlags();
  224. GetScreen()->SetContentModified( false );
  225. Zoom_Automatique( false );
  226. GetCanvas()->Refresh();
  227. return true;
  228. }
  229. void FOOTPRINT_EDIT_FRAME::KiwayMailIn( KIWAY_EXPRESS& mail )
  230. {
  231. const std::string& payload = mail.GetPayload();
  232. switch( mail.Command() )
  233. {
  234. case MAIL_FP_EDIT:
  235. if( !payload.empty() )
  236. {
  237. wxFileName fpFileName( payload );
  238. wxString libNickname;
  239. wxString msg;
  240. FP_LIB_TABLE* libTable = Prj().PcbFootprintLibs();
  241. const LIB_TABLE_ROW* libTableRow = libTable->FindRowByURI( fpFileName.GetPath() );
  242. if( !libTableRow )
  243. {
  244. msg.Printf( _( "The current configuration does not include a library named '%s'.\n"
  245. "Use Manage Footprint Libraries to edit the configuration." ),
  246. fpFileName.GetPath() );
  247. DisplayErrorMessage( this, _( "Library not found in footprint library table." ),
  248. msg );
  249. break;
  250. }
  251. libNickname = libTableRow->GetNickName();
  252. if( !libTable->HasLibrary( libNickname, true ) )
  253. {
  254. msg.Printf( _( "The library '%s' is not enabled in the current configuration.\n"
  255. "Use Manage Footprint Libraries to edit the configuration." ),
  256. libNickname );
  257. DisplayErrorMessage( this, _( "Footprint library not enabled." ), msg );
  258. break;
  259. }
  260. LIB_ID fpId( libNickname, fpFileName.GetName() );
  261. if( m_treePane )
  262. {
  263. m_treePane->GetLibTree()->SelectLibId( fpId );
  264. wxCommandEvent event( SYMBOL_SELECTED );
  265. wxPostEvent( m_treePane, event );
  266. }
  267. }
  268. break;
  269. default:
  270. ;
  271. }
  272. }