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.

345 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. if( m_zoomSelectBox->GetSelection() == 0 )
  71. Zoom_Automatique( false );
  72. Update3DView( true, true );
  73. GetScreen()->SetContentModified( false );
  74. UpdateView();
  75. GetCanvas()->Refresh();
  76. // Update the save items if needed.
  77. if( is_last_fp_from_brd )
  78. {
  79. ReCreateMenuBar();
  80. ReCreateHToolbar();
  81. }
  82. m_treePane->GetLibTree()->ExpandLibId( aFPID );
  83. m_centerItemOnIdle = aFPID;
  84. Bind( wxEVT_IDLE, &FOOTPRINT_EDIT_FRAME::centerItemIdleHandler, this );
  85. m_treePane->GetLibTree()->RefreshLibTree(); // update highlighting
  86. }
  87. void FOOTPRINT_EDIT_FRAME::centerItemIdleHandler( wxIdleEvent& aEvent )
  88. {
  89. m_treePane->GetLibTree()->CenterLibId( m_centerItemOnIdle );
  90. Unbind( wxEVT_IDLE, &FOOTPRINT_EDIT_FRAME::centerItemIdleHandler, this );
  91. }
  92. void FOOTPRINT_EDIT_FRAME::SelectLayer( wxCommandEvent& event )
  93. {
  94. SetActiveLayer( ToLAYER_ID( m_selLayerBox->GetLayerSelection() ) );
  95. if( GetDisplayOptions().m_ContrastModeDisplay != HIGH_CONTRAST_MODE::NORMAL )
  96. GetCanvas()->Refresh();
  97. }
  98. void FOOTPRINT_EDIT_FRAME::OnSaveFootprintToBoard( wxCommandEvent& event )
  99. {
  100. SaveFootprintToBoard( true );
  101. }
  102. class BASIC_FOOTPRINT_INFO : public FOOTPRINT_INFO
  103. {
  104. public:
  105. BASIC_FOOTPRINT_INFO( FOOTPRINT* aFootprint )
  106. {
  107. m_nickname = aFootprint->GetFPID().GetLibNickname().wx_str();
  108. m_fpname = aFootprint->GetFPID().GetLibItemName().wx_str();
  109. m_pad_count = aFootprint->GetPadCount( DO_NOT_INCLUDE_NPTH );
  110. m_unique_pad_count = aFootprint->GetUniquePadCount( DO_NOT_INCLUDE_NPTH );
  111. m_keywords = aFootprint->GetKeywords();
  112. m_doc = aFootprint->GetDescription();
  113. m_loaded = true;
  114. }
  115. };
  116. void FOOTPRINT_EDIT_FRAME::editFootprintProperties( FOOTPRINT* aFootprint )
  117. {
  118. LIB_ID oldFPID = aFootprint->GetFPID();
  119. DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR dialog( this, aFootprint );
  120. dialog.ShowModal();
  121. // Update library tree
  122. BASIC_FOOTPRINT_INFO footprintInfo( aFootprint );
  123. wxDataViewItem treeItem = m_adapter->FindItem( oldFPID );
  124. if( treeItem.IsOk() ) // Can be not found in tree if the current footprint is imported
  125. // from file therefore not yet in tree.
  126. {
  127. static_cast<LIB_TREE_NODE_LIB_ID*>( treeItem.GetID() )->Update( &footprintInfo );
  128. m_treePane->GetLibTree()->RefreshLibTree();
  129. }
  130. UpdateTitle(); // in case of a name change...
  131. UpdateMsgPanel();
  132. }
  133. void FOOTPRINT_EDIT_FRAME::OnEditItemRequest( BOARD_ITEM* aItem )
  134. {
  135. switch( aItem->Type() )
  136. {
  137. case PCB_PAD_T:
  138. ShowPadPropertiesDialog( static_cast<PAD*>( aItem ) );
  139. break;
  140. case PCB_FOOTPRINT_T:
  141. editFootprintProperties( static_cast<FOOTPRINT*>( aItem ) );
  142. GetCanvas()->Refresh();
  143. break;
  144. case PCB_FP_TEXT_T:
  145. ShowTextPropertiesDialog( aItem );
  146. break;
  147. case PCB_FP_TEXTBOX_T:
  148. ShowTextBoxPropertiesDialog( aItem );
  149. break;
  150. case PCB_FP_SHAPE_T :
  151. ShowGraphicItemPropertiesDialog( aItem );
  152. break;
  153. case PCB_FP_DIM_ALIGNED_T:
  154. case PCB_FP_DIM_CENTER_T:
  155. case PCB_FP_DIM_RADIAL_T:
  156. case PCB_FP_DIM_ORTHOGONAL_T:
  157. case PCB_FP_DIM_LEADER_T:
  158. {
  159. DIALOG_DIMENSION_PROPERTIES dlg( this, static_cast<PCB_DIMENSION_BASE*>( aItem ) );
  160. dlg.ShowQuasiModal();
  161. break;
  162. }
  163. case PCB_FP_ZONE_T:
  164. {
  165. ZONE* zone = static_cast<ZONE*>( aItem );
  166. bool success = false;
  167. ZONE_SETTINGS zoneSettings;
  168. zoneSettings << *static_cast<ZONE*>( aItem );
  169. if( zone->GetIsRuleArea() )
  170. {
  171. success = InvokeRuleAreaEditor( this, &zoneSettings ) == wxID_OK;
  172. }
  173. else if( zone->IsOnCopperLayer() )
  174. {
  175. success = InvokeCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
  176. }
  177. else
  178. {
  179. success = InvokeNonCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
  180. }
  181. if( success )
  182. {
  183. BOARD_COMMIT commit( this );
  184. commit.Modify( zone );
  185. commit.Push( _( "Edit Zone" ) );
  186. zoneSettings.ExportSetting( *static_cast<ZONE*>( aItem ) );
  187. }
  188. break;
  189. }
  190. case PCB_GROUP_T:
  191. m_toolManager->RunAction( PCB_ACTIONS::groupProperties, true, aItem );
  192. break;
  193. default:
  194. wxFAIL_MSG( "FOOTPRINT_EDIT_FRAME::OnEditItemRequest: unsupported item type "
  195. + aItem->GetClass() );
  196. break;
  197. }
  198. }
  199. COLOR4D FOOTPRINT_EDIT_FRAME::GetGridColor()
  200. {
  201. return GetColorSettings()->GetColor( LAYER_GRID );
  202. }
  203. void FOOTPRINT_EDIT_FRAME::SetActiveLayer( PCB_LAYER_ID aLayer )
  204. {
  205. PCB_BASE_FRAME::SetActiveLayer( aLayer );
  206. m_appearancePanel->OnLayerChanged();
  207. m_toolManager->RunAction( PCB_ACTIONS::layerChanged ); // notify other tools
  208. GetCanvas()->SetFocus(); // allow capture of hotkeys
  209. GetCanvas()->SetHighContrastLayer( aLayer );
  210. GetCanvas()->Refresh();
  211. }
  212. bool FOOTPRINT_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, int aCtl )
  213. {
  214. if( !Clear_Pcb( true ) )
  215. return false; // this command is aborted
  216. GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
  217. ImportFootprint( aFileSet[ 0 ] );
  218. if( GetBoard()->GetFirstFootprint() )
  219. GetBoard()->GetFirstFootprint()->ClearFlags();
  220. GetScreen()->SetContentModified( false );
  221. Zoom_Automatique( false );
  222. GetCanvas()->Refresh();
  223. return true;
  224. }
  225. void FOOTPRINT_EDIT_FRAME::KiwayMailIn( KIWAY_EXPRESS& mail )
  226. {
  227. const std::string& payload = mail.GetPayload();
  228. switch( mail.Command() )
  229. {
  230. case MAIL_FP_EDIT:
  231. if( !payload.empty() )
  232. {
  233. wxFileName fpFileName( payload );
  234. wxString libNickname;
  235. wxString msg;
  236. FP_LIB_TABLE* libTable = Prj().PcbFootprintLibs();
  237. const LIB_TABLE_ROW* libTableRow = libTable->FindRowByURI( fpFileName.GetPath() );
  238. if( !libTableRow )
  239. {
  240. msg.Printf( _( "The current configuration does not include a library named '%s'.\n"
  241. "Use Manage Footprint Libraries to edit the configuration." ),
  242. fpFileName.GetPath() );
  243. DisplayErrorMessage( this, _( "Library not found in footprint library table." ),
  244. msg );
  245. break;
  246. }
  247. libNickname = libTableRow->GetNickName();
  248. if( !libTable->HasLibrary( libNickname, true ) )
  249. {
  250. msg.Printf( _( "The library '%s' is not enabled in the current configuration.\n"
  251. "Use Manage Footprint Libraries to edit the configuration." ),
  252. libNickname );
  253. DisplayErrorMessage( this, _( "Footprint library not enabled." ), msg );
  254. break;
  255. }
  256. LIB_ID fpId( libNickname, fpFileName.GetName() );
  257. if( m_treePane )
  258. {
  259. m_treePane->GetLibTree()->SelectLibId( fpId );
  260. wxCommandEvent event( SYMBOL_SELECTED );
  261. wxPostEvent( m_treePane, event );
  262. }
  263. }
  264. break;
  265. default:
  266. ;
  267. }
  268. }