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.

402 lines
12 KiB

4 years ago
3 years ago
2 years ago
5 years ago
2 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The 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_table.h>
  35. #include <pcb_shape.h>
  36. #include <pad.h>
  37. #include <zone.h>
  38. #include <settings/color_settings.h>
  39. #include <tool/tool_manager.h>
  40. #include <tools/pcb_actions.h>
  41. #include <tools/footprint_editor_control.h>
  42. #include <widgets/appearance_controls.h>
  43. #include <widgets/lib_tree.h>
  44. #include <pcb_layer_box_selector.h>
  45. #include <pcb_dimension.h>
  46. #include <project_pcb.h>
  47. #include <view/view_controls.h>
  48. #include <dialogs/dialog_dimension_properties.h>
  49. #include <dialogs/dialog_table_properties.h>
  50. using namespace std::placeholders;
  51. void FOOTPRINT_EDIT_FRAME::OnLoadFootprintFromBoard( wxCommandEvent& event )
  52. {
  53. LoadFootprintFromBoard( nullptr );
  54. }
  55. void FOOTPRINT_EDIT_FRAME::LoadFootprintFromLibrary( LIB_ID aFPID )
  56. {
  57. bool is_last_fp_from_brd = IsCurrentFPFromBoard();
  58. FOOTPRINT* footprint = LoadFootprint( aFPID );
  59. if( !footprint )
  60. return;
  61. if( !Clear_Pcb( true ) )
  62. return;
  63. GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
  64. AddFootprintToBoard( footprint );
  65. footprint->ClearFlags();
  66. // if either reference or value are missing, reinstall them -
  67. // otherwise you cannot see what you are doing on board
  68. if( footprint->Reference().GetText().IsEmpty() )
  69. footprint->SetReference( wxT( "Ref**" ) );
  70. if( footprint->Value().GetText().IsEmpty() )
  71. footprint->SetValue( wxT( "Val**" ) );
  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. wxASSERT( aFootprint );
  109. m_nickname = aFootprint->GetFPID().GetLibNickname().wx_str();
  110. m_fpname = aFootprint->GetFPID().GetLibItemName().wx_str();
  111. m_pad_count = aFootprint->GetPadCount( DO_NOT_INCLUDE_NPTH );
  112. m_unique_pad_count = aFootprint->GetUniquePadCount( DO_NOT_INCLUDE_NPTH );
  113. m_keywords = aFootprint->GetKeywords();
  114. m_doc = aFootprint->GetLibDescription();
  115. m_loaded = true;
  116. }
  117. };
  118. void FOOTPRINT_EDIT_FRAME::UpdateLibraryTree( const wxDataViewItem& aTreeItem,
  119. FOOTPRINT* aFootprint )
  120. {
  121. wxCHECK( aFootprint, /* void */ );
  122. BASIC_FOOTPRINT_INFO footprintInfo( aFootprint );
  123. if( aTreeItem.IsOk() ) // Can be not found in tree if the current footprint is imported
  124. // from file therefore not yet in tree.
  125. {
  126. static_cast<LIB_TREE_NODE_ITEM*>( aTreeItem.GetID() )->Update( &footprintInfo );
  127. m_treePane->GetLibTree()->RefreshLibTree();
  128. }
  129. }
  130. void FOOTPRINT_EDIT_FRAME::editFootprintProperties( FOOTPRINT* aFootprint )
  131. {
  132. LIB_ID oldFPID = aFootprint->GetFPID();
  133. DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR dialog( this, aFootprint );
  134. dialog.ShowQuasiModal();
  135. // Update library tree and title in case of a name change
  136. wxDataViewItem treeItem = m_adapter->FindItem( oldFPID );
  137. UpdateLibraryTree( treeItem, aFootprint );
  138. UpdateTitle();
  139. UpdateMsgPanel();
  140. }
  141. void FOOTPRINT_EDIT_FRAME::OnEditItemRequest( BOARD_ITEM* aItem )
  142. {
  143. switch( aItem->Type() )
  144. {
  145. case PCB_REFERENCE_IMAGE_T:
  146. ShowReferenceImagePropertiesDialog( aItem );
  147. break;
  148. case PCB_PAD_T:
  149. ShowPadPropertiesDialog( static_cast<PAD*>( aItem ) );
  150. break;
  151. case PCB_FOOTPRINT_T:
  152. editFootprintProperties( static_cast<FOOTPRINT*>( aItem ) );
  153. GetCanvas()->Refresh();
  154. break;
  155. case PCB_FIELD_T:
  156. case PCB_TEXT_T:
  157. ShowTextPropertiesDialog( static_cast<PCB_TEXT*>( aItem ) );
  158. break;
  159. case PCB_TEXTBOX_T:
  160. ShowTextBoxPropertiesDialog( static_cast<PCB_TEXTBOX*>( aItem ) );
  161. break;
  162. case PCB_TABLE_T:
  163. {
  164. DIALOG_TABLE_PROPERTIES dlg( this, static_cast<PCB_TABLE*>( aItem ) );
  165. //QuasiModal required for Scintilla auto-complete
  166. dlg.ShowQuasiModal();
  167. break;
  168. }
  169. case PCB_SHAPE_T :
  170. ShowGraphicItemPropertiesDialog( static_cast<PCB_SHAPE*>( aItem ) );
  171. break;
  172. case PCB_DIM_ALIGNED_T:
  173. case PCB_DIM_CENTER_T:
  174. case PCB_DIM_RADIAL_T:
  175. case PCB_DIM_ORTHOGONAL_T:
  176. case PCB_DIM_LEADER_T:
  177. {
  178. DIALOG_DIMENSION_PROPERTIES dlg( this, static_cast<PCB_DIMENSION_BASE*>( aItem ) );
  179. // TODO: why is this QuasiModal?
  180. dlg.ShowQuasiModal();
  181. break;
  182. }
  183. case PCB_ZONE_T:
  184. {
  185. ZONE* zone = static_cast<ZONE*>( aItem );
  186. bool success = false;
  187. ZONE_SETTINGS zoneSettings;
  188. zoneSettings << *static_cast<ZONE*>( aItem );
  189. if( zone->GetIsRuleArea() )
  190. {
  191. success = InvokeRuleAreaEditor( this, &zoneSettings ) == wxID_OK;
  192. }
  193. else if( zone->IsOnCopperLayer() )
  194. {
  195. success = InvokeCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
  196. }
  197. else
  198. {
  199. success = InvokeNonCopperZonesEditor( this, &zoneSettings ) == wxID_OK;
  200. }
  201. if( success )
  202. {
  203. BOARD_COMMIT commit( this );
  204. commit.Modify( zone );
  205. commit.Push( _( "Edit Zone" ) );
  206. zoneSettings.ExportSetting( *static_cast<ZONE*>( aItem ) );
  207. }
  208. break;
  209. }
  210. case PCB_GROUP_T:
  211. m_toolManager->RunAction( PCB_ACTIONS::groupProperties, static_cast<PCB_GROUP*>( aItem ) );
  212. break;
  213. case PCB_MARKER_T:
  214. m_toolManager->GetTool<FOOTPRINT_EDITOR_CONTROL>()->CrossProbe( static_cast<PCB_MARKER*>( aItem ) );
  215. break;
  216. default:
  217. wxFAIL_MSG( wxT( "FOOTPRINT_EDIT_FRAME::OnEditItemRequest: unsupported item type " )
  218. + aItem->GetClass() );
  219. break;
  220. }
  221. }
  222. COLOR4D FOOTPRINT_EDIT_FRAME::GetGridColor()
  223. {
  224. return GetColorSettings()->GetColor( LAYER_GRID );
  225. }
  226. void FOOTPRINT_EDIT_FRAME::SetActiveLayer( PCB_LAYER_ID aLayer )
  227. {
  228. const PCB_LAYER_ID oldLayer = GetActiveLayer();
  229. if( oldLayer == aLayer )
  230. return;
  231. PCB_BASE_FRAME::SetActiveLayer( aLayer );
  232. /*
  233. * Follow the PCB editor logic for showing/hiding clearance layers: show only for
  234. * the active copper layer or a front/back non-copper layer.
  235. */
  236. const auto getClearanceLayerForActive = []( PCB_LAYER_ID aActiveLayer ) -> std::optional<int>
  237. {
  238. if( IsCopperLayer( aActiveLayer ) )
  239. return CLEARANCE_LAYER_FOR( aActiveLayer );
  240. return std::nullopt;
  241. };
  242. if( std::optional<int> oldClearanceLayer = getClearanceLayerForActive( oldLayer ) )
  243. GetCanvas()->GetView()->SetLayerVisible( *oldClearanceLayer, false );
  244. if( std::optional<int> newClearanceLayer = getClearanceLayerForActive( aLayer ) )
  245. GetCanvas()->GetView()->SetLayerVisible( *newClearanceLayer, true );
  246. m_appearancePanel->OnLayerChanged();
  247. m_toolManager->RunAction( PCB_ACTIONS::layerChanged ); // notify other tools
  248. GetCanvas()->SetFocus(); // allow capture of hotkeys
  249. GetCanvas()->SetHighContrastLayer( aLayer );
  250. GetCanvas()->Refresh();
  251. }
  252. bool FOOTPRINT_EDIT_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, int aCtl )
  253. {
  254. if( !Clear_Pcb( true ) )
  255. return false; // this command is aborted
  256. GetCanvas()->GetViewControls()->SetCrossHairCursorPosition( VECTOR2D( 0, 0 ), false );
  257. ImportFootprint( aFileSet[ 0 ] );
  258. if( GetBoard()->GetFirstFootprint() )
  259. GetBoard()->GetFirstFootprint()->ClearFlags();
  260. GetScreen()->SetContentModified( false );
  261. Zoom_Automatique( false );
  262. GetCanvas()->Refresh();
  263. return true;
  264. }
  265. void FOOTPRINT_EDIT_FRAME::KiwayMailIn( KIWAY_EXPRESS& mail )
  266. {
  267. const std::string& payload = mail.GetPayload();
  268. switch( mail.Command() )
  269. {
  270. case MAIL_FP_EDIT:
  271. if( !payload.empty() )
  272. {
  273. wxFileName fpFileName( payload );
  274. wxString libNickname;
  275. wxString msg;
  276. FP_LIB_TABLE* libTable = PROJECT_PCB::PcbFootprintLibs( &Prj() );
  277. const LIB_TABLE_ROW* libTableRow = libTable->FindRowByURI( fpFileName.GetPath() );
  278. if( !libTableRow )
  279. {
  280. msg.Printf( _( "The current configuration does not include the footprint library '%s'." ),
  281. fpFileName.GetPath() );
  282. msg += wxS( "\n" ) + _( "Use Manage Footprint Libraries to edit the configuration." );
  283. DisplayErrorMessage( this, _( "Library not found in footprint library table." ),
  284. msg );
  285. break;
  286. }
  287. libNickname = libTableRow->GetNickName();
  288. if( !libTable->HasLibrary( libNickname, true ) )
  289. {
  290. msg.Printf( _( "The footprint library '%s' is not enabled in the current configuration." ),
  291. libNickname );
  292. msg += wxS( "\n" ) + _( "Use Manage Footprint Libraries to edit the configuration." );
  293. DisplayErrorMessage( this, _( "Footprint library not enabled." ), msg );
  294. break;
  295. }
  296. LIB_ID fpId( libNickname, fpFileName.GetName() );
  297. if( m_treePane )
  298. {
  299. m_treePane->GetLibTree()->SelectLibId( fpId );
  300. wxCommandEvent event( EVT_LIBITEM_CHOSEN );
  301. wxPostEvent( m_treePane, event );
  302. }
  303. }
  304. break;
  305. case MAIL_RELOAD_LIB:
  306. SyncLibraryTree( true );
  307. RefreshLibraryTree();
  308. break;
  309. default:
  310. break;
  311. }
  312. }