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.

365 lines
11 KiB

3 years ago
4 years ago
3 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-2022 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 <pcb_group.h>
  32. #include <pcb_marker.h>
  33. #include <pcb_textbox.h>
  34. #include <pcb_shape.h>
  35. #include <pad.h>
  36. #include <zone.h>
  37. #include <settings/color_settings.h>
  38. #include <tool/tool_manager.h>
  39. #include <tools/pcb_actions.h>
  40. #include <tools/footprint_editor_control.h>
  41. #include <widgets/appearance_controls.h>
  42. #include <widgets/lib_tree.h>
  43. #include <pcb_layer_box_selector.h>
  44. #include <pcb_dimension.h>
  45. #include <dialogs/dialog_dimension_properties.h>
  46. using namespace std::placeholders;
  47. void FOOTPRINT_EDIT_FRAME::OnLoadFootprintFromBoard( wxCommandEvent& event )
  48. {
  49. LoadFootprintFromBoard( nullptr );
  50. }
  51. void FOOTPRINT_EDIT_FRAME::LoadFootprintFromLibrary( LIB_ID aFPID )
  52. {
  53. bool is_last_fp_from_brd = IsCurrentFPFromBoard();
  54. FOOTPRINT* footprint = LoadFootprint( aFPID );
  55. if( !footprint )
  56. return;
  57. if( !Clear_Pcb( true ) )
  58. return;
  59. GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
  60. AddFootprintToBoard( footprint );
  61. footprint->ClearFlags();
  62. // if either reference or value are missing, reinstall them -
  63. // otherwise you cannot see what you are doing on board
  64. if( footprint->Reference().GetText().IsEmpty() )
  65. footprint->SetReference( wxT( "Ref**" ) );
  66. if( footprint->Value().GetText().IsEmpty() )
  67. footprint->SetValue( wxT( "Val**" ) );
  68. Zoom_Automatique( false );
  69. Update3DView( true, true );
  70. GetScreen()->SetContentModified( false );
  71. UpdateView();
  72. GetCanvas()->Refresh();
  73. // Update the save items if needed.
  74. if( is_last_fp_from_brd )
  75. {
  76. ReCreateMenuBar();
  77. ReCreateHToolbar();
  78. }
  79. m_treePane->GetLibTree()->ExpandLibId( aFPID );
  80. m_centerItemOnIdle = aFPID;
  81. Bind( wxEVT_IDLE, &FOOTPRINT_EDIT_FRAME::centerItemIdleHandler, this );
  82. m_treePane->GetLibTree()->RefreshLibTree(); // update highlighting
  83. }
  84. void FOOTPRINT_EDIT_FRAME::centerItemIdleHandler( wxIdleEvent& aEvent )
  85. {
  86. m_treePane->GetLibTree()->CenterLibId( m_centerItemOnIdle );
  87. Unbind( wxEVT_IDLE, &FOOTPRINT_EDIT_FRAME::centerItemIdleHandler, this );
  88. }
  89. void FOOTPRINT_EDIT_FRAME::SelectLayer( wxCommandEvent& event )
  90. {
  91. SetActiveLayer( ToLAYER_ID( m_selLayerBox->GetLayerSelection() ) );
  92. if( GetDisplayOptions().m_ContrastModeDisplay != HIGH_CONTRAST_MODE::NORMAL )
  93. GetCanvas()->Refresh();
  94. }
  95. void FOOTPRINT_EDIT_FRAME::OnSaveFootprintToBoard( wxCommandEvent& event )
  96. {
  97. SaveFootprintToBoard( true );
  98. }
  99. class BASIC_FOOTPRINT_INFO : public FOOTPRINT_INFO
  100. {
  101. public:
  102. BASIC_FOOTPRINT_INFO( FOOTPRINT* aFootprint )
  103. {
  104. wxASSERT( aFootprint );
  105. m_nickname = aFootprint->GetFPID().GetLibNickname().wx_str();
  106. m_fpname = aFootprint->GetFPID().GetLibItemName().wx_str();
  107. m_pad_count = aFootprint->GetPadCount( DO_NOT_INCLUDE_NPTH );
  108. m_unique_pad_count = aFootprint->GetUniquePadCount( DO_NOT_INCLUDE_NPTH );
  109. m_keywords = aFootprint->GetKeywords();
  110. m_doc = aFootprint->GetLibDescription();
  111. m_loaded = true;
  112. }
  113. };
  114. void FOOTPRINT_EDIT_FRAME::UpdateLibraryTree( const wxDataViewItem& aTreeItem,
  115. FOOTPRINT* aFootprint )
  116. {
  117. wxCHECK( aFootprint, /* void */ );
  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_BITMAP_T:
  142. ShowBitmapPropertiesDialog( aItem );
  143. break;
  144. case PCB_PAD_T:
  145. ShowPadPropertiesDialog( static_cast<PAD*>( aItem ) );
  146. break;
  147. case PCB_FOOTPRINT_T:
  148. editFootprintProperties( static_cast<FOOTPRINT*>( aItem ) );
  149. GetCanvas()->Refresh();
  150. break;
  151. case PCB_FIELD_T:
  152. case PCB_TEXT_T:
  153. ShowTextPropertiesDialog( static_cast<PCB_TEXT*>( aItem ) );
  154. break;
  155. case PCB_TEXTBOX_T:
  156. ShowTextBoxPropertiesDialog( static_cast<PCB_TEXTBOX*>( aItem ) );
  157. break;
  158. case PCB_SHAPE_T :
  159. ShowGraphicItemPropertiesDialog( static_cast<PCB_SHAPE*>( aItem ) );
  160. break;
  161. case PCB_DIM_ALIGNED_T:
  162. case PCB_DIM_CENTER_T:
  163. case PCB_DIM_RADIAL_T:
  164. case PCB_DIM_ORTHOGONAL_T:
  165. case PCB_DIM_LEADER_T:
  166. {
  167. DIALOG_DIMENSION_PROPERTIES dlg( this, static_cast<PCB_DIMENSION_BASE*>( aItem ) );
  168. dlg.ShowQuasiModal();
  169. break;
  170. }
  171. case PCB_ZONE_T:
  172. {
  173. ZONE* zone = static_cast<ZONE*>( aItem );
  174. bool success = false;
  175. ZONE_SETTINGS zoneSettings;
  176. zoneSettings << *static_cast<ZONE*>( aItem );
  177. if( zone->GetIsRuleArea() )
  178. {
  179. success = InvokeRuleAreaEditor( this, &zoneSettings ) == wxID_OK;
  180. }
  181. else if( zone->IsOnCopperLayer() )
  182. {
  183. success = InvokeCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
  184. }
  185. else
  186. {
  187. success = InvokeNonCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
  188. }
  189. if( success )
  190. {
  191. BOARD_COMMIT commit( this );
  192. commit.Modify( zone );
  193. commit.Push( _( "Edit Zone" ) );
  194. zoneSettings.ExportSetting( *static_cast<ZONE*>( aItem ) );
  195. }
  196. break;
  197. }
  198. case PCB_GROUP_T:
  199. m_toolManager->RunAction<PCB_GROUP*>( PCB_ACTIONS::groupProperties,
  200. static_cast<PCB_GROUP*>( aItem ) );
  201. break;
  202. case PCB_MARKER_T:
  203. m_toolManager->GetTool<FOOTPRINT_EDITOR_CONTROL>()->CrossProbe( static_cast<PCB_MARKER*>( aItem ) );
  204. break;
  205. default:
  206. wxFAIL_MSG( wxT( "FOOTPRINT_EDIT_FRAME::OnEditItemRequest: unsupported item type " )
  207. + aItem->GetClass() );
  208. break;
  209. }
  210. }
  211. COLOR4D FOOTPRINT_EDIT_FRAME::GetGridColor()
  212. {
  213. return GetColorSettings()->GetColor( LAYER_GRID );
  214. }
  215. void FOOTPRINT_EDIT_FRAME::SetActiveLayer( PCB_LAYER_ID aLayer )
  216. {
  217. PCB_BASE_FRAME::SetActiveLayer( aLayer );
  218. m_appearancePanel->OnLayerChanged();
  219. m_toolManager->PostAction( PCB_ACTIONS::layerChanged ); // notify other tools
  220. GetCanvas()->SetFocus(); // allow capture of hotkeys
  221. GetCanvas()->SetHighContrastLayer( aLayer );
  222. GetCanvas()->Refresh();
  223. }
  224. bool FOOTPRINT_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, int aCtl )
  225. {
  226. if( !Clear_Pcb( true ) )
  227. return false; // this command is aborted
  228. GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
  229. ImportFootprint( aFileSet[ 0 ] );
  230. if( GetBoard()->GetFirstFootprint() )
  231. GetBoard()->GetFirstFootprint()->ClearFlags();
  232. GetScreen()->SetContentModified( false );
  233. Zoom_Automatique( false );
  234. GetCanvas()->Refresh();
  235. return true;
  236. }
  237. void FOOTPRINT_EDIT_FRAME::KiwayMailIn( KIWAY_EXPRESS& mail )
  238. {
  239. const std::string& payload = mail.GetPayload();
  240. switch( mail.Command() )
  241. {
  242. case MAIL_FP_EDIT:
  243. if( !payload.empty() )
  244. {
  245. wxFileName fpFileName( payload );
  246. wxString libNickname;
  247. wxString msg;
  248. FP_LIB_TABLE* libTable = Prj().PcbFootprintLibs();
  249. const LIB_TABLE_ROW* libTableRow = libTable->FindRowByURI( fpFileName.GetPath() );
  250. if( !libTableRow )
  251. {
  252. msg.Printf( _( "The current configuration does not include a library named '%s'.\n"
  253. "Use Manage Footprint Libraries to edit the configuration." ),
  254. fpFileName.GetPath() );
  255. DisplayErrorMessage( this, _( "Library not found in footprint library table." ),
  256. msg );
  257. break;
  258. }
  259. libNickname = libTableRow->GetNickName();
  260. if( !libTable->HasLibrary( libNickname, true ) )
  261. {
  262. msg.Printf( _( "The library '%s' is not enabled in the current configuration.\n"
  263. "Use Manage Footprint Libraries to edit the configuration." ),
  264. libNickname );
  265. DisplayErrorMessage( this, _( "Footprint library not enabled." ), msg );
  266. break;
  267. }
  268. LIB_ID fpId( libNickname, fpFileName.GetName() );
  269. if( m_treePane )
  270. {
  271. m_treePane->GetLibTree()->SelectLibId( fpId );
  272. wxCommandEvent event( SYMBOL_SELECTED );
  273. wxPostEvent( m_treePane, event );
  274. }
  275. }
  276. break;
  277. case MAIL_RELOAD_LIB:
  278. SyncLibraryTree( true );
  279. RefreshLibraryTree();
  280. break;
  281. default:
  282. break;
  283. }
  284. }