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.

361 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 <pcb_marker.h>
  32. #include <pad.h>
  33. #include <zone.h>
  34. #include <settings/color_settings.h>
  35. #include <tool/tool_manager.h>
  36. #include <tools/pcb_actions.h>
  37. #include <tools/footprint_editor_control.h>
  38. #include <widgets/appearance_controls.h>
  39. #include <widgets/lib_tree.h>
  40. #include <pcb_layer_box_selector.h>
  41. #include <pcb_dimension.h>
  42. #include <dialogs/dialog_dimension_properties.h>
  43. using namespace std::placeholders;
  44. void FOOTPRINT_EDIT_FRAME::OnLoadFootprintFromBoard( wxCommandEvent& event )
  45. {
  46. LoadFootprintFromBoard( nullptr );
  47. }
  48. void FOOTPRINT_EDIT_FRAME::LoadFootprintFromLibrary( LIB_ID aFPID )
  49. {
  50. bool is_last_fp_from_brd = IsCurrentFPFromBoard();
  51. FOOTPRINT* footprint = LoadFootprint( aFPID );
  52. if( !footprint )
  53. return;
  54. if( !Clear_Pcb( true ) )
  55. return;
  56. GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
  57. AddFootprintToBoard( footprint );
  58. footprint->ClearFlags();
  59. // if either m_Reference or m_Value are gone, reinstall them -
  60. // otherwise you cannot see what you are doing on board
  61. FP_TEXT* ref = &footprint->Reference();
  62. FP_TEXT* val = &footprint->Value();
  63. if( val && ref )
  64. {
  65. ref->SetType( FP_TEXT::TEXT_is_REFERENCE ); // just in case ...
  66. if( ref->GetLength() == 0 )
  67. ref->SetText( wxT( "Ref**" ) );
  68. val->SetType( FP_TEXT::TEXT_is_VALUE ); // just in case ...
  69. if( val->GetLength() == 0 )
  70. val->SetText( wxT( "Val**" ) );
  71. }
  72. Zoom_Automatique( false );
  73. Update3DView( true, true );
  74. GetScreen()->SetContentModified( false );
  75. UpdateView();
  76. GetCanvas()->Refresh();
  77. // Update the save items if needed.
  78. if( is_last_fp_from_brd )
  79. {
  80. ReCreateMenuBar();
  81. ReCreateHToolbar();
  82. }
  83. m_treePane->GetLibTree()->ExpandLibId( aFPID );
  84. m_centerItemOnIdle = aFPID;
  85. Bind( wxEVT_IDLE, &FOOTPRINT_EDIT_FRAME::centerItemIdleHandler, this );
  86. m_treePane->GetLibTree()->RefreshLibTree(); // update highlighting
  87. }
  88. void FOOTPRINT_EDIT_FRAME::centerItemIdleHandler( wxIdleEvent& aEvent )
  89. {
  90. m_treePane->GetLibTree()->CenterLibId( m_centerItemOnIdle );
  91. Unbind( wxEVT_IDLE, &FOOTPRINT_EDIT_FRAME::centerItemIdleHandler, this );
  92. }
  93. void FOOTPRINT_EDIT_FRAME::SelectLayer( wxCommandEvent& event )
  94. {
  95. SetActiveLayer( ToLAYER_ID( m_selLayerBox->GetLayerSelection() ) );
  96. if( GetDisplayOptions().m_ContrastModeDisplay != HIGH_CONTRAST_MODE::NORMAL )
  97. GetCanvas()->Refresh();
  98. }
  99. void FOOTPRINT_EDIT_FRAME::OnSaveFootprintToBoard( wxCommandEvent& event )
  100. {
  101. SaveFootprintToBoard( true );
  102. }
  103. class BASIC_FOOTPRINT_INFO : public FOOTPRINT_INFO
  104. {
  105. public:
  106. BASIC_FOOTPRINT_INFO( FOOTPRINT* aFootprint )
  107. {
  108. m_nickname = aFootprint->GetFPID().GetLibNickname().wx_str();
  109. m_fpname = aFootprint->GetFPID().GetLibItemName().wx_str();
  110. m_pad_count = aFootprint->GetPadCount( DO_NOT_INCLUDE_NPTH );
  111. m_unique_pad_count = aFootprint->GetUniquePadCount( DO_NOT_INCLUDE_NPTH );
  112. m_keywords = aFootprint->GetKeywords();
  113. m_doc = aFootprint->GetDescription();
  114. m_loaded = true;
  115. }
  116. };
  117. void FOOTPRINT_EDIT_FRAME::UpdateLibraryTree( const wxDataViewItem& aTreeItem,
  118. FOOTPRINT* aFootprint )
  119. {
  120. BASIC_FOOTPRINT_INFO footprintInfo( aFootprint );
  121. if( aTreeItem.IsOk() ) // Can be not found in tree if the current footprint is imported
  122. // from file therefore not yet in tree.
  123. {
  124. static_cast<LIB_TREE_NODE_LIB_ID*>( aTreeItem.GetID() )->Update( &footprintInfo );
  125. m_treePane->GetLibTree()->RefreshLibTree();
  126. }
  127. }
  128. void FOOTPRINT_EDIT_FRAME::editFootprintProperties( FOOTPRINT* aFootprint )
  129. {
  130. LIB_ID oldFPID = aFootprint->GetFPID();
  131. DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR dialog( this, aFootprint );
  132. dialog.ShowModal();
  133. // Update library tree and title in case of a name change
  134. wxDataViewItem treeItem = m_adapter->FindItem( oldFPID );
  135. UpdateLibraryTree( treeItem, aFootprint );
  136. UpdateTitle();
  137. UpdateMsgPanel();
  138. }
  139. void FOOTPRINT_EDIT_FRAME::OnEditItemRequest( BOARD_ITEM* aItem )
  140. {
  141. switch( aItem->Type() )
  142. {
  143. case PCB_BITMAP_T:
  144. ShowBitmapPropertiesDialog( aItem );
  145. break;
  146. case PCB_PAD_T:
  147. ShowPadPropertiesDialog( static_cast<PAD*>( aItem ) );
  148. break;
  149. case PCB_FOOTPRINT_T:
  150. editFootprintProperties( static_cast<FOOTPRINT*>( aItem ) );
  151. GetCanvas()->Refresh();
  152. break;
  153. case PCB_FP_TEXT_T:
  154. ShowTextPropertiesDialog( aItem );
  155. break;
  156. case PCB_FP_TEXTBOX_T:
  157. ShowTextBoxPropertiesDialog( aItem );
  158. break;
  159. case PCB_FP_SHAPE_T :
  160. ShowGraphicItemPropertiesDialog( aItem );
  161. break;
  162. case PCB_FP_DIM_ALIGNED_T:
  163. case PCB_FP_DIM_CENTER_T:
  164. case PCB_FP_DIM_RADIAL_T:
  165. case PCB_FP_DIM_ORTHOGONAL_T:
  166. case PCB_FP_DIM_LEADER_T:
  167. {
  168. DIALOG_DIMENSION_PROPERTIES dlg( this, static_cast<PCB_DIMENSION_BASE*>( aItem ) );
  169. dlg.ShowQuasiModal();
  170. break;
  171. }
  172. case PCB_FP_ZONE_T:
  173. {
  174. ZONE* zone = static_cast<ZONE*>( aItem );
  175. bool success = false;
  176. ZONE_SETTINGS zoneSettings;
  177. zoneSettings << *static_cast<ZONE*>( aItem );
  178. if( zone->GetIsRuleArea() )
  179. {
  180. success = InvokeRuleAreaEditor( this, &zoneSettings ) == wxID_OK;
  181. }
  182. else if( zone->IsOnCopperLayer() )
  183. {
  184. success = InvokeCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
  185. }
  186. else
  187. {
  188. success = InvokeNonCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
  189. }
  190. if( success )
  191. {
  192. BOARD_COMMIT commit( this );
  193. commit.Modify( zone );
  194. commit.Push( _( "Edit Zone" ) );
  195. zoneSettings.ExportSetting( *static_cast<ZONE*>( aItem ) );
  196. }
  197. break;
  198. }
  199. case PCB_GROUP_T:
  200. m_toolManager->RunAction( PCB_ACTIONS::groupProperties, true, 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->RunAction( 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. default:
  278. break;
  279. }
  280. }