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.

319 lines
9.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2017 jean-pierre Charras jp.charras at wanadoo.fr
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <sch_edit_frame.h>
  25. #include <bitmaps.h>
  26. #include <dialog_annotate_base.h>
  27. #include <eeschema_settings.h>
  28. #include <kiface_base.h>
  29. #include <widgets/wx_html_report_panel.h>
  30. #include <schematic.h>
  31. #include <sch_commit.h>
  32. // A window name for the annotate dialog to retrieve is if not destroyed
  33. #define DLG_WINDOW_NAME "DialogAnnotateWindowName"
  34. /**
  35. * A dialog to set/clear reference designators of a schematic with different options.
  36. */
  37. class DIALOG_ANNOTATE: public DIALOG_ANNOTATE_BASE
  38. {
  39. public:
  40. DIALOG_ANNOTATE( SCH_EDIT_FRAME* parent, const wxString& message );
  41. ~DIALOG_ANNOTATE();
  42. private:
  43. /// Initialize member variables.
  44. void InitValues();
  45. void OnOptionChanged( wxCommandEvent& event ) override;
  46. void OnClearAnnotationClick( wxCommandEvent& event ) override;
  47. void OnCloseClick( wxCommandEvent& event ) override;
  48. void OnClose( wxCloseEvent& event ) override;
  49. void OnAnnotateClick( wxCommandEvent& event ) override;
  50. // User functions:
  51. bool GetResetItems();
  52. ANNOTATE_SCOPE_T GetScope();
  53. bool GetRecursive();
  54. ANNOTATE_ORDER_T GetSortOrder();
  55. ANNOTATE_ALGO_T GetAnnotateAlgo();
  56. int GetStartNumber();
  57. SCH_EDIT_FRAME* m_Parent;
  58. };
  59. DIALOG_ANNOTATE::DIALOG_ANNOTATE( SCH_EDIT_FRAME* parent, const wxString& message )
  60. : DIALOG_ANNOTATE_BASE( parent )
  61. {
  62. SetName( DLG_WINDOW_NAME );
  63. m_Parent = parent;
  64. if( !message.IsEmpty() )
  65. {
  66. m_infoBar->RemoveAllButtons();
  67. m_infoBar->ShowMessage( message );
  68. m_rbScope_Schematic->SetValue( true );
  69. m_rbScope_Schematic->Enable( false );
  70. }
  71. m_MessageWindow->SetLabel( _( "Annotation Messages:" ) );
  72. m_MessageWindow->SetFileName( Prj().GetProjectPath() + wxT( "report.txt" ) );
  73. SetupStandardButtons( { { wxID_OK, _( "Annotate" ) },
  74. { wxID_CANCEL, _( "Close" ) } } );
  75. InitValues();
  76. Layout();
  77. // When all widgets have the size fixed, call FinishDialogSettings
  78. finishDialogSettings();
  79. }
  80. DIALOG_ANNOTATE::~DIALOG_ANNOTATE()
  81. {
  82. auto cfg = static_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
  83. cfg->m_AnnotatePanel.sort_order = GetSortOrder();
  84. cfg->m_AnnotatePanel.method = GetAnnotateAlgo();
  85. cfg->m_AnnotatePanel.options = m_rbOptions->GetSelection();
  86. if( m_rbScope_Schematic->IsEnabled() )
  87. {
  88. cfg->m_AnnotatePanel.scope = GetScope();
  89. cfg->m_AnnotatePanel.recursive = GetRecursive();
  90. }
  91. cfg->m_AnnotatePanel.messages_filter = m_MessageWindow->GetVisibleSeverities();
  92. // Get the "start annotation after" value from dialog and update project settings if changed
  93. int startNum = GetStartNumber();
  94. SCH_EDIT_FRAME* schFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_parentFrame );
  95. if( schFrame )
  96. {
  97. SCHEMATIC_SETTINGS& projSettings = schFrame->Schematic().Settings();
  98. // If the user has updated the start annotation number then update the project file.
  99. // We manually update the project file here in case the user has changed the value
  100. // and just clicked the "Close" button on the annotation dialog.
  101. if( projSettings.m_AnnotateStartNum != startNum )
  102. {
  103. projSettings.m_AnnotateStartNum = startNum;
  104. schFrame->OnModify();
  105. }
  106. }
  107. }
  108. void DIALOG_ANNOTATE::InitValues()
  109. {
  110. EESCHEMA_SETTINGS* cfg = static_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
  111. int option;
  112. if( m_rbScope_Schematic->IsEnabled() )
  113. {
  114. switch( cfg->m_AnnotatePanel.scope )
  115. {
  116. default:
  117. case ANNOTATE_ALL: m_rbScope_Schematic->SetValue( true ); break;
  118. case ANNOTATE_CURRENT_SHEET: m_rbScope_Sheet->SetValue( true ); break;
  119. case ANNOTATE_SELECTION: m_rbScope_Selection->SetValue( true ); break;
  120. }
  121. m_checkRecursive->SetValue( cfg->m_AnnotatePanel.recursive );
  122. }
  123. m_rbOptions->SetSelection( cfg->m_AnnotatePanel.options );
  124. option = cfg->m_AnnotatePanel.sort_order;
  125. switch( option )
  126. {
  127. default:
  128. case SORT_BY_X_POSITION: m_rbSortBy_X_Position->SetValue( true ); break;
  129. case SORT_BY_Y_POSITION: m_rbSortBy_Y_Position->SetValue( true ); break;
  130. }
  131. option = cfg->m_AnnotatePanel.method;
  132. switch( option )
  133. {
  134. default:
  135. case INCREMENTAL_BY_REF: m_rbFirstFree->SetValue( true ); break;
  136. case SHEET_NUMBER_X_100: m_rbSheetX100->SetValue( true ); break;
  137. case SHEET_NUMBER_X_1000: m_rbSheetX1000->SetValue( true ); break;
  138. }
  139. int annotateStartNum = 0; // Default "start after" value for annotation
  140. // See if we can get a "start after" value from the project settings
  141. SCH_EDIT_FRAME* schFrame = dynamic_cast<SCH_EDIT_FRAME*>( m_parentFrame );
  142. if( schFrame )
  143. {
  144. SCHEMATIC_SETTINGS& projSettings = schFrame->Schematic().Settings();
  145. annotateStartNum = projSettings.m_AnnotateStartNum;
  146. }
  147. m_textNumberAfter->SetValue( wxString::Format( wxT( "%d" ), annotateStartNum ) );
  148. annotate_down_right_bitmap->SetBitmap( KiBitmapBundle( BITMAPS::annotate_down_right ) );
  149. annotate_right_down_bitmap->SetBitmap( KiBitmapBundle( BITMAPS::annotate_right_down ) );
  150. m_MessageWindow->SetVisibleSeverities( cfg->m_AnnotatePanel.messages_filter );
  151. m_MessageWindow->MsgPanelSetMinSize( wxSize( -1, 160 ) );
  152. }
  153. // This is a modeless dialog so we have to handle these ourselves.
  154. void DIALOG_ANNOTATE::OnCloseClick( wxCommandEvent& event )
  155. {
  156. Close();
  157. }
  158. void DIALOG_ANNOTATE::OnClose( wxCloseEvent& event )
  159. {
  160. Destroy();
  161. }
  162. void DIALOG_ANNOTATE::OnAnnotateClick( wxCommandEvent& event )
  163. {
  164. SCH_COMMIT commit( m_Parent );
  165. m_MessageWindow->Clear();
  166. REPORTER& reporter = m_MessageWindow->Reporter();
  167. m_MessageWindow->SetLazyUpdate( true ); // Don't update after each message
  168. m_Parent->AnnotateSymbols( &commit, GetScope(), GetSortOrder(), GetAnnotateAlgo(),
  169. GetRecursive(), GetStartNumber(), GetResetItems(), true, reporter );
  170. commit.Push( _( "Annotate" ) );
  171. m_MessageWindow->Flush( true ); // Now update to show all messages
  172. }
  173. void DIALOG_ANNOTATE::OnClearAnnotationClick( wxCommandEvent& event )
  174. {
  175. m_MessageWindow->Clear();
  176. m_Parent->DeleteAnnotation( GetScope(), GetRecursive(), m_MessageWindow->Reporter() );
  177. m_MessageWindow->Flush( true ); // Now update to show all messages
  178. }
  179. void DIALOG_ANNOTATE::OnOptionChanged( wxCommandEvent& event )
  180. {
  181. m_sdbSizer1OK->Enable( true );
  182. m_sdbSizer1OK->SetDefault();
  183. }
  184. bool DIALOG_ANNOTATE::GetResetItems()
  185. {
  186. return m_rbOptions->GetSelection() >= 1;
  187. }
  188. ANNOTATE_SCOPE_T DIALOG_ANNOTATE::GetScope()
  189. {
  190. if( m_rbScope_Schematic->GetValue() )
  191. return ANNOTATE_ALL;
  192. else if( m_rbScope_Sheet->GetValue() )
  193. return ANNOTATE_CURRENT_SHEET;
  194. else
  195. return ANNOTATE_SELECTION;
  196. }
  197. bool DIALOG_ANNOTATE::GetRecursive()
  198. {
  199. return m_checkRecursive->GetValue();
  200. }
  201. ANNOTATE_ORDER_T DIALOG_ANNOTATE::GetSortOrder()
  202. {
  203. if( m_rbSortBy_Y_Position->GetValue() )
  204. return SORT_BY_Y_POSITION;
  205. else
  206. return SORT_BY_X_POSITION;
  207. }
  208. ANNOTATE_ALGO_T DIALOG_ANNOTATE::GetAnnotateAlgo()
  209. {
  210. if( m_rbSheetX100->GetValue() )
  211. return SHEET_NUMBER_X_100;
  212. else if( m_rbSheetX1000->GetValue() )
  213. return SHEET_NUMBER_X_1000;
  214. else
  215. return INCREMENTAL_BY_REF;
  216. }
  217. int DIALOG_ANNOTATE::GetStartNumber()
  218. {
  219. return EDA_UNIT_UTILS::UI::ValueFromString( m_textNumberAfter->GetValue() );
  220. }
  221. void SCH_EDIT_FRAME::OnAnnotate()
  222. {
  223. DIALOG_ANNOTATE* dlg =
  224. static_cast<DIALOG_ANNOTATE*>( wxWindow::FindWindowByName( DLG_WINDOW_NAME ) );
  225. if( !dlg )
  226. {
  227. dlg = new DIALOG_ANNOTATE( this, wxEmptyString );
  228. dlg->Show( true );
  229. }
  230. else // The dialog is already opened, perhaps not visible
  231. {
  232. dlg->Show( true );
  233. }
  234. }
  235. int SCH_EDIT_FRAME::ModalAnnotate( const wxString& aMessage )
  236. {
  237. DIALOG_ANNOTATE dlg( this, aMessage );
  238. return dlg.ShowModal();
  239. }