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.

333 lines
11 KiB

4 years ago
4 years ago
4 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014-2016 CERN
  5. * Copyright (C) 2021-2022 KiCad Developers, see AUTHORS.txt for contributors.
  6. * @author Maciej Suminski <maciej.suminski@cern.ch>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. // kicad_curl_easy.h must be included before wxWidgets because on Windows (msys2), there are
  26. // collision with wx headers and curl.h defs
  27. #include <kicad_curl/kicad_curl_easy.h>
  28. #include <bitmaps.h>
  29. #include <build_version.h>
  30. #include <common.h> // for SearchHelpFileFullPath
  31. #include <pgm_base.h>
  32. #include <tool/actions.h>
  33. #include <tool/tool_manager.h>
  34. #include <eda_draw_frame.h>
  35. #include <view/view.h>
  36. #include <gal/graphics_abstraction_layer.h>
  37. #include <base_screen.h>
  38. #include <tool/common_control.h>
  39. #include <id.h>
  40. #include <kiface_base.h>
  41. #include <dialogs/dialog_configure_paths.h>
  42. #include <eda_doc.h>
  43. #include <wx/msgdlg.h>
  44. #define URL_GET_INVOLVED "https://kicad.org/contribute/"
  45. #define URL_DONATE "https://go.kicad.org/app-donate"
  46. #define URL_DOCUMENTATION "https://docs.kicad.org/"
  47. /// URL to launch a new issue with pre-populated description
  48. wxString COMMON_CONTROL::m_bugReportUrl =
  49. "https://gitlab.com/kicad/code/kicad/issues/new?issue[description]=%s";
  50. /// Issue template to use for reporting bugs (this should not be translated)
  51. wxString COMMON_CONTROL::m_bugReportTemplate =
  52. "<!-- Before Creating a New Issue:\n"
  53. "* Search the issue tracker to verify the issue has not already been reported.\n"
  54. "* Only report one problem per issue. -->\n"
  55. "\n"
  56. "# Description\n"
  57. "<!-- What is the current behavior and what is the expected behavior? -->\n"
  58. "<!-- Please attach screenshots if they will help explain the problem. -->\n"
  59. "\n"
  60. "# Steps to reproduce\n"
  61. "<!-- Please include a screen recording if it will help explain how to reproduce. -->\n"
  62. "<!-- If this issue is specific to a project, please attach it. -->\n"
  63. "1.\n"
  64. "2.\n"
  65. "# KiCad Version\n"
  66. "\n"
  67. "```\n"
  68. "%s\n"
  69. "```";
  70. void COMMON_CONTROL::Reset( RESET_REASON aReason )
  71. {
  72. m_frame = getEditFrame<EDA_BASE_FRAME>();
  73. }
  74. int COMMON_CONTROL::OpenPreferences( const TOOL_EVENT& aEvent )
  75. {
  76. wxCommandEvent dummy;
  77. m_frame->OnPreferences( dummy );
  78. return 0;
  79. }
  80. int COMMON_CONTROL::ConfigurePaths( const TOOL_EVENT& aEvent )
  81. {
  82. // If _pcbnew.kiface is running have it put up the dialog so the 3D paths can also
  83. // be edited
  84. KIFACE* pcbnew = m_frame->Kiway().KiFACE( KIWAY::FACE_PCB, false );
  85. if( pcbnew )
  86. {
  87. try
  88. {
  89. pcbnew->CreateWindow( m_frame, DIALOG_CONFIGUREPATHS, &m_frame->Kiway() );
  90. }
  91. catch( ... )
  92. {
  93. // Do nothing here.
  94. // A error message is displayed after trying to load _pcbnew.kiface.
  95. }
  96. }
  97. else
  98. {
  99. DIALOG_CONFIGURE_PATHS dlg( m_frame, nullptr );
  100. // Use QuasiModal so that HTML help window will work
  101. if( dlg.ShowQuasiModal() == wxID_OK )
  102. m_frame->Kiway().CommonSettingsChanged( true, false );
  103. }
  104. return 0;
  105. }
  106. int COMMON_CONTROL::ShowLibraryTable( const TOOL_EVENT& aEvent )
  107. {
  108. if( aEvent.IsAction( &ACTIONS::showSymbolLibTable ) )
  109. {
  110. try // Sch frame was not available, try to start it
  111. {
  112. KIFACE* kiface = m_frame->Kiway().KiFACE( KIWAY::FACE_SCH );
  113. kiface->CreateWindow( m_frame, DIALOG_SCH_LIBRARY_TABLE, &m_frame->Kiway() );
  114. }
  115. catch( ... )
  116. {
  117. // _eeschema.kiface is not available: it contains the library table dialog.
  118. // Do nothing here.
  119. // A error message is displayed after trying to load _eeschema.kiface.
  120. }
  121. }
  122. else if( aEvent.IsAction( &ACTIONS::showFootprintLibTable ) )
  123. {
  124. try // Pcb frame was not available, try to start it
  125. {
  126. KIFACE* kiface = m_frame->Kiway().KiFACE( KIWAY::FACE_PCB );
  127. kiface->CreateWindow( m_frame, DIALOG_PCB_LIBRARY_TABLE, &m_frame->Kiway() );
  128. }
  129. catch( ... )
  130. {
  131. // _pcbnew.kiface is not available: it contains the library table dialog.
  132. // Do nothing here.
  133. // A error message is displayed after trying to load _pcbnew.kiface.
  134. }
  135. }
  136. return 0;
  137. }
  138. int COMMON_CONTROL::ShowPlayer( const TOOL_EVENT& aEvent )
  139. {
  140. FRAME_T playerType = aEvent.Parameter<FRAME_T>();
  141. KIWAY_PLAYER* editor = m_frame->Kiway().Player( playerType, true );
  142. // editor can be null if Player() fails:
  143. wxCHECK_MSG( editor != nullptr, 0, wxT( "Cannot open/create the editor frame" ) );
  144. // Needed on Windows, other platforms do not use it, but it creates no issue
  145. if( editor->IsIconized() )
  146. editor->Iconize( false );
  147. editor->Raise();
  148. // Raising the window does not set the focus on Linux. This should work on
  149. // any platform.
  150. if( wxWindow::FindFocus() != editor )
  151. editor->SetFocus();
  152. return 0;
  153. }
  154. int COMMON_CONTROL::ShowHelp( const TOOL_EVENT& aEvent )
  155. {
  156. wxString helpFile;
  157. wxString msg;
  158. // the URL of help files is "https://docs.kicad.org/<version>/<language>/<name>/"
  159. const wxString baseUrl = URL_DOCUMENTATION + GetMajorMinorVersion() + wxT( "/" )
  160. + Pgm().GetLocale()->GetName().BeforeLast( '_' ) + wxT( "/" );
  161. /* We have to get document for beginners,
  162. * or the full specific doc
  163. * if event id is wxID_INDEX, we want the document for beginners.
  164. * else the specific doc file (its name is in Kiface().GetHelpFileName())
  165. * The document for beginners is the same for all KiCad utilities
  166. */
  167. if( aEvent.IsAction( &ACTIONS::gettingStarted ) )
  168. {
  169. // List of possible names for Getting Started in KiCad
  170. const wxChar* names[2] = {
  171. wxT( "getting_started_in_kicad" ),
  172. wxT( "Getting_Started_in_KiCad" )
  173. };
  174. // Search for "getting_started_in_kicad.html" or "getting_started_in_kicad.pdf"
  175. // or "Getting_Started_in_KiCad.html" or "Getting_Started_in_KiCad.pdf"
  176. for( auto& name : names )
  177. {
  178. helpFile = SearchHelpFileFullPath( name );
  179. if( !helpFile.IsEmpty() )
  180. break;
  181. }
  182. if( !helpFile )
  183. {
  184. msg = wxString::Format( _( "Help file '%s' or\n'%s' could not be found.\n"
  185. "Do you want to access the KiCad online help?" ),
  186. names[0], names[1] );
  187. wxMessageDialog dlg( nullptr, msg, _( "File Not Found" ),
  188. wxYES_NO | wxNO_DEFAULT | wxCANCEL );
  189. if( dlg.ShowModal() != wxID_YES )
  190. return -1;
  191. helpFile = baseUrl + names[0] + "/";
  192. }
  193. }
  194. else
  195. {
  196. wxString base_name = m_frame->help_name();
  197. helpFile = SearchHelpFileFullPath( base_name );
  198. if( !helpFile )
  199. {
  200. msg = wxString::Format( _( "Help file '%s' could not be found.\n"
  201. "Do you want to access the KiCad online help?" ),
  202. base_name );
  203. wxMessageDialog dlg( nullptr, msg, _( "File Not Found" ),
  204. wxYES_NO | wxNO_DEFAULT | wxCANCEL );
  205. if( dlg.ShowModal() != wxID_YES )
  206. return -1;
  207. helpFile = baseUrl + base_name + "/";
  208. }
  209. }
  210. GetAssociatedDocument( m_frame, helpFile, &m_frame->Prj() );
  211. return 0;
  212. }
  213. int COMMON_CONTROL::ListHotKeys( const TOOL_EVENT& aEvent )
  214. {
  215. DisplayHotkeyList( m_frame );
  216. return 0;
  217. }
  218. int COMMON_CONTROL::GetInvolved( const TOOL_EVENT& aEvent )
  219. {
  220. if( !wxLaunchDefaultBrowser( URL_GET_INVOLVED ) )
  221. {
  222. wxString msg;
  223. msg.Printf( _( "Could not launch the default browser.\n"
  224. "For information on how to help the KiCad project, visit %s" ),
  225. URL_GET_INVOLVED );
  226. wxMessageBox( msg, _( "Get involved with KiCad" ), wxOK, m_frame );
  227. }
  228. return 0;
  229. }
  230. int COMMON_CONTROL::Donate( const TOOL_EVENT& aEvent )
  231. {
  232. if( !wxLaunchDefaultBrowser( URL_DONATE ) )
  233. {
  234. wxString msg;
  235. msg.Printf( _( "Could not launch the default browser.\n"
  236. "To donate to the KiCad project, visit %s" ),
  237. URL_DONATE );
  238. wxMessageBox( msg, _( "Donate to KiCad" ), wxOK, m_frame );
  239. }
  240. return 0;
  241. }
  242. int COMMON_CONTROL::ReportBug( const TOOL_EVENT& aEvent )
  243. {
  244. if( WarnUserIfOperatingSystemUnsupported() )
  245. return 0;
  246. wxString version = GetVersionInfoData( m_frame->GetAboutTitle(), false, true );
  247. wxString message;
  248. message.Printf( m_bugReportTemplate, version );
  249. KICAD_CURL_EASY kcurl;
  250. wxString url_string;
  251. url_string.Printf( m_bugReportUrl, kcurl.Escape( std::string( message.utf8_str() ) ) );
  252. wxLaunchDefaultBrowser( url_string );
  253. return 0;
  254. }
  255. void COMMON_CONTROL::setTransitions()
  256. {
  257. Go( &COMMON_CONTROL::OpenPreferences, ACTIONS::openPreferences.MakeEvent() );
  258. Go( &COMMON_CONTROL::ConfigurePaths, ACTIONS::configurePaths.MakeEvent() );
  259. Go( &COMMON_CONTROL::ShowLibraryTable, ACTIONS::showSymbolLibTable.MakeEvent() );
  260. Go( &COMMON_CONTROL::ShowLibraryTable, ACTIONS::showFootprintLibTable.MakeEvent() );
  261. Go( &COMMON_CONTROL::ShowPlayer, ACTIONS::showSymbolBrowser.MakeEvent() );
  262. Go( &COMMON_CONTROL::ShowPlayer, ACTIONS::showSymbolEditor.MakeEvent() );
  263. Go( &COMMON_CONTROL::ShowPlayer, ACTIONS::showFootprintBrowser.MakeEvent() );
  264. Go( &COMMON_CONTROL::ShowPlayer, ACTIONS::showFootprintEditor.MakeEvent() );
  265. Go( &COMMON_CONTROL::ShowHelp, ACTIONS::gettingStarted.MakeEvent() );
  266. Go( &COMMON_CONTROL::ShowHelp, ACTIONS::help.MakeEvent() );
  267. Go( &COMMON_CONTROL::ListHotKeys, ACTIONS::listHotKeys.MakeEvent() );
  268. Go( &COMMON_CONTROL::GetInvolved, ACTIONS::getInvolved.MakeEvent() );
  269. Go( &COMMON_CONTROL::Donate, ACTIONS::donate.MakeEvent() );
  270. Go( &COMMON_CONTROL::ReportBug, ACTIONS::reportBug.MakeEvent() );
  271. }