2013-07-19 18:27:22 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2013-08-13 17:51:22 +00:00
|
|
|
* Copyright (C) 2013 CERN
|
2023-02-09 13:15:01 +00:00
|
|
|
* Copyright (C) 2017-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2017-11-12 00:31:38 +00:00
|
|
|
*
|
2013-08-13 17:51:22 +00:00
|
|
|
* @author Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2013-07-19 18:27:22 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <confirm.h>
|
|
|
|
#include <gestfich.h>
|
2021-02-22 23:47:17 +00:00
|
|
|
#include <drawing_sheet/ds_data_model.h>
|
2021-06-10 22:39:03 +00:00
|
|
|
#include <drawing_sheet/ds_file_versions.h>
|
2021-02-01 04:57:41 +00:00
|
|
|
#include <paths.h>
|
2022-12-28 22:03:03 +00:00
|
|
|
#include <widgets/wx_infobar.h>
|
2013-07-19 18:27:22 +00:00
|
|
|
#include <wildcards_and_files_ext.h>
|
|
|
|
|
2023-05-25 00:07:46 +00:00
|
|
|
#include <kiplatform/io.h>
|
|
|
|
|
2020-10-13 05:04:31 +00:00
|
|
|
#include "pl_editor_frame.h"
|
|
|
|
#include "pl_editor_id.h"
|
|
|
|
#include "properties_frame.h"
|
2018-08-11 20:46:03 +00:00
|
|
|
|
2021-05-01 07:50:29 +00:00
|
|
|
#include <wx/filedlg.h>
|
2022-03-08 04:08:54 +00:00
|
|
|
#include <wx/filename.h>
|
|
|
|
#include <wx/stdpaths.h>
|
2021-05-01 07:50:29 +00:00
|
|
|
|
2018-08-11 20:46:03 +00:00
|
|
|
bool PL_EDITOR_FRAME::saveCurrentPageLayout()
|
|
|
|
{
|
|
|
|
wxCommandEvent saveEvent;
|
|
|
|
saveEvent.SetId( wxID_SAVE );
|
|
|
|
Files_io( saveEvent );
|
|
|
|
|
2019-12-19 15:34:01 +00:00
|
|
|
return !IsContentModified();
|
2018-08-11 20:46:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
void PL_EDITOR_FRAME::OnFileHistory( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
wxString filename;
|
|
|
|
|
2021-05-30 22:56:24 +00:00
|
|
|
filename = GetFileFromHistory( event.GetId(), _( "Drawing Sheet File" ) );
|
2013-07-19 18:27:22 +00:00
|
|
|
|
|
|
|
if( filename != wxEmptyString )
|
|
|
|
{
|
2019-12-19 15:34:01 +00:00
|
|
|
if( IsContentModified() )
|
2018-08-11 20:46:03 +00:00
|
|
|
{
|
2021-05-30 22:56:24 +00:00
|
|
|
if( !HandleUnsavedChanges( this, _( "The current drawing sheet has been modified. "
|
2019-06-17 15:59:39 +00:00
|
|
|
"Save changes?" ),
|
2021-10-01 20:49:14 +00:00
|
|
|
[&]() -> bool
|
|
|
|
{
|
|
|
|
return saveCurrentPageLayout();
|
|
|
|
} ) )
|
2018-10-19 10:02:38 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2018-08-11 20:46:03 +00:00
|
|
|
}
|
2013-07-19 18:27:22 +00:00
|
|
|
|
|
|
|
::wxSetWorkingDirectory( ::wxPathOnly( filename ) );
|
2013-07-20 19:36:19 +00:00
|
|
|
|
2021-05-30 22:56:24 +00:00
|
|
|
if( LoadDrawingSheetFile( filename ) )
|
2018-08-11 20:46:03 +00:00
|
|
|
{
|
|
|
|
wxString msg;
|
2020-10-16 00:39:55 +00:00
|
|
|
msg.Printf( _( "File \"%s\" loaded"), filename );
|
2018-08-11 20:46:03 +00:00
|
|
|
SetStatusText( msg );
|
|
|
|
}
|
2017-11-12 00:31:38 +00:00
|
|
|
|
2021-05-30 22:56:24 +00:00
|
|
|
OnNewDrawingSheet();
|
2013-07-19 18:27:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 00:31:38 +00:00
|
|
|
|
2020-02-25 15:46:56 +00:00
|
|
|
void PL_EDITOR_FRAME::OnClearFileHistory( wxCommandEvent& aEvent )
|
|
|
|
{
|
|
|
|
ClearFileHistory();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
/* File commands. */
|
|
|
|
void PL_EDITOR_FRAME::Files_io( wxCommandEvent& event )
|
|
|
|
{
|
2019-05-25 11:05:39 +00:00
|
|
|
wxString msg;
|
|
|
|
int id = event.GetId();
|
2020-05-16 23:08:01 +00:00
|
|
|
wxString filename = GetCurrentFileName();
|
2021-02-22 23:47:17 +00:00
|
|
|
DS_DATA_MODEL& pglayout = DS_DATA_MODEL::GetTheInstance();
|
2013-07-19 18:27:22 +00:00
|
|
|
|
|
|
|
if( filename.IsEmpty() && id == wxID_SAVE )
|
|
|
|
id = wxID_SAVEAS;
|
|
|
|
|
2019-12-19 15:34:01 +00:00
|
|
|
if( ( id == wxID_NEW || id == wxID_OPEN ) && IsContentModified() )
|
2013-07-19 18:27:22 +00:00
|
|
|
{
|
2021-05-31 18:23:01 +00:00
|
|
|
if( !HandleUnsavedChanges( this, _( "The current drawing sheet has been modified. "
|
2019-06-17 15:59:39 +00:00
|
|
|
"Save changes?" ),
|
2021-10-01 20:49:14 +00:00
|
|
|
[&]() -> bool
|
|
|
|
{
|
|
|
|
return saveCurrentPageLayout();
|
|
|
|
} ) )
|
2018-08-11 20:46:03 +00:00
|
|
|
{
|
2013-07-19 18:27:22 +00:00
|
|
|
return;
|
2018-08-11 20:46:03 +00:00
|
|
|
}
|
2013-07-19 18:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch( id )
|
|
|
|
{
|
|
|
|
case wxID_NEW:
|
|
|
|
pglayout.AllowVoidList( true );
|
2020-05-16 23:08:01 +00:00
|
|
|
SetCurrentFileName( wxEmptyString );
|
2013-07-19 18:27:22 +00:00
|
|
|
pglayout.ClearList();
|
2021-05-30 22:56:24 +00:00
|
|
|
OnNewDrawingSheet();
|
2013-07-19 18:27:22 +00:00
|
|
|
break;
|
|
|
|
|
2013-08-13 17:51:22 +00:00
|
|
|
case ID_APPEND_DESCR_FILE:
|
2013-07-19 18:27:22 +00:00
|
|
|
{
|
2021-02-22 16:37:43 +00:00
|
|
|
wxFileDialog openFileDialog( this, _( "Append Existing Drawing Sheet" ),
|
2017-11-12 00:31:38 +00:00
|
|
|
wxEmptyString, wxEmptyString,
|
2021-05-30 22:56:24 +00:00
|
|
|
DrawingSheetFileWildcard(), wxFD_OPEN );
|
2013-07-19 18:27:22 +00:00
|
|
|
|
2017-11-12 00:31:38 +00:00
|
|
|
if( openFileDialog.ShowModal() == wxID_CANCEL )
|
2013-07-19 18:27:22 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
filename = openFileDialog.GetPath();
|
2017-11-12 00:31:38 +00:00
|
|
|
|
2021-05-30 22:56:24 +00:00
|
|
|
if( !InsertDrawingSheetFile( filename ) )
|
2013-07-19 18:27:22 +00:00
|
|
|
{
|
2020-10-16 00:39:55 +00:00
|
|
|
msg.Printf( _( "Unable to load %s file" ), filename );
|
2020-12-09 00:28:48 +00:00
|
|
|
DisplayErrorMessage( this, msg );
|
2013-07-19 18:27:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-28 19:07:04 +00:00
|
|
|
GetScreen()->SetContentModified();
|
2019-06-13 13:51:06 +00:00
|
|
|
HardRedraw();
|
2020-10-16 00:39:55 +00:00
|
|
|
msg.Printf( _( "File \"%s\" inserted" ), filename );
|
2013-07-19 18:27:22 +00:00
|
|
|
SetStatusText( msg );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case wxID_OPEN:
|
|
|
|
{
|
2023-10-23 17:01:04 +00:00
|
|
|
wxFileDialog openFileDialog( this, _( "Open Drawing Sheet" ), wxEmptyString, wxEmptyString,
|
2021-05-30 22:56:24 +00:00
|
|
|
DrawingSheetFileWildcard(), wxFD_OPEN );
|
2013-07-19 18:27:22 +00:00
|
|
|
|
2017-11-12 00:31:38 +00:00
|
|
|
if( openFileDialog.ShowModal() == wxID_CANCEL )
|
2013-07-19 18:27:22 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
filename = openFileDialog.GetPath();
|
2017-11-12 00:31:38 +00:00
|
|
|
|
2021-05-30 22:56:24 +00:00
|
|
|
if( !LoadDrawingSheetFile( filename ) )
|
2013-07-19 18:27:22 +00:00
|
|
|
{
|
2020-10-16 00:39:55 +00:00
|
|
|
msg.Printf( _( "Unable to load %s file" ), filename );
|
2020-12-09 00:28:48 +00:00
|
|
|
DisplayErrorMessage( this, msg );
|
2013-07-19 18:27:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-30 22:56:24 +00:00
|
|
|
OnNewDrawingSheet();
|
2020-12-08 23:58:07 +00:00
|
|
|
msg.Printf( _( "File \"%s\" saved." ), filename );
|
2013-07-19 18:27:22 +00:00
|
|
|
SetStatusText( msg );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case wxID_SAVE:
|
2021-05-30 22:56:24 +00:00
|
|
|
if( !SaveDrawingSheetFile( filename ) )
|
2013-07-19 18:27:22 +00:00
|
|
|
{
|
2021-06-27 13:24:02 +00:00
|
|
|
msg.Printf( _( "Unable to write '%s'." ), filename );
|
2020-12-09 00:28:48 +00:00
|
|
|
DisplayErrorMessage( this, msg );
|
2013-07-19 18:27:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-06-27 13:24:02 +00:00
|
|
|
msg.Printf( _("File '%s' saved."), filename );
|
2013-07-19 18:27:22 +00:00
|
|
|
SetStatusText( msg );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case wxID_SAVEAS:
|
|
|
|
{
|
2021-02-01 04:57:41 +00:00
|
|
|
wxString dir = PATHS::GetUserTemplatesPath();
|
2023-10-23 17:01:04 +00:00
|
|
|
wxFileDialog openFileDialog( this, _( "Save Drawing Sheet As" ), dir, wxEmptyString,
|
2021-05-30 22:56:24 +00:00
|
|
|
DrawingSheetFileWildcard(),
|
2021-02-01 04:57:41 +00:00
|
|
|
wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
|
2013-07-19 18:27:22 +00:00
|
|
|
|
2017-11-12 00:31:38 +00:00
|
|
|
if( openFileDialog.ShowModal() == wxID_CANCEL )
|
2013-07-19 18:27:22 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
filename = openFileDialog.GetPath();
|
2013-08-29 07:20:09 +00:00
|
|
|
// Ensure the file has the right extension:
|
2013-08-30 19:28:31 +00:00
|
|
|
// because a name like name.subname.subsubname is legal,
|
|
|
|
// add the right extension without replacing the wxFileName
|
|
|
|
// extension
|
2013-08-29 07:20:09 +00:00
|
|
|
wxFileName fn(filename);
|
2013-08-30 19:28:31 +00:00
|
|
|
|
2021-05-30 22:56:24 +00:00
|
|
|
if( fn.GetExt() != DrawingSheetFileExtension )
|
|
|
|
filename << wxT(".") << DrawingSheetFileExtension;
|
2013-08-30 19:28:31 +00:00
|
|
|
|
2021-05-30 22:56:24 +00:00
|
|
|
if( !SaveDrawingSheetFile( filename ) )
|
2013-07-19 18:27:22 +00:00
|
|
|
{
|
2021-07-05 12:40:38 +00:00
|
|
|
msg.Printf( _( "Failed to create file '%s'." ), filename );
|
2020-12-09 00:28:48 +00:00
|
|
|
DisplayErrorMessage( this, msg );
|
2013-07-19 18:27:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-28 21:09:06 +00:00
|
|
|
msg.Printf( _( "File '%s' saved." ), filename );
|
2013-07-19 18:27:22 +00:00
|
|
|
SetStatusText( msg );
|
2017-11-12 00:31:38 +00:00
|
|
|
|
2023-10-28 21:09:06 +00:00
|
|
|
SetCurrentFileName( filename );
|
|
|
|
UpdateTitleAndInfo();
|
2013-07-19 18:27:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-12 00:31:38 +00:00
|
|
|
|
2021-05-30 22:56:24 +00:00
|
|
|
bool PL_EDITOR_FRAME::LoadDrawingSheetFile( const wxString& aFullFileName )
|
2013-07-19 18:27:22 +00:00
|
|
|
{
|
|
|
|
if( wxFileExists( aFullFileName ) )
|
|
|
|
{
|
2021-05-01 17:17:39 +00:00
|
|
|
bool loaded = false;
|
|
|
|
|
|
|
|
loaded = DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( aFullFileName );
|
|
|
|
|
|
|
|
if( !loaded )
|
|
|
|
{
|
|
|
|
ShowInfoBarError( _( "Error reading drawing sheet" ), true );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-16 23:08:01 +00:00
|
|
|
SetCurrentFileName( aFullFileName );
|
2013-07-19 18:27:22 +00:00
|
|
|
UpdateFileHistory( aFullFileName );
|
2021-05-28 19:07:04 +00:00
|
|
|
GetScreen()->SetContentModified( false );
|
2020-05-18 23:44:58 +00:00
|
|
|
|
|
|
|
wxFileName fn = aFullFileName;
|
2020-05-20 01:31:47 +00:00
|
|
|
m_infoBar->Dismiss();
|
2020-05-18 23:44:58 +00:00
|
|
|
|
2021-06-10 22:39:03 +00:00
|
|
|
if( DS_DATA_MODEL::GetTheInstance().GetFileFormatVersionAtLoad() < SEXPR_WORKSHEET_FILE_VERSION )
|
|
|
|
{
|
|
|
|
m_infoBar->RemoveAllButtons();
|
|
|
|
m_infoBar->AddCloseButton();
|
|
|
|
m_infoBar->ShowMessage( _( "This file was created by an older version of KiCad. "
|
|
|
|
"It will be converted to the new format when saved." ),
|
|
|
|
wxICON_WARNING, WX_INFOBAR::MESSAGE_TYPE::OUTDATED_SAVE );
|
|
|
|
}
|
|
|
|
|
2020-05-18 23:44:58 +00:00
|
|
|
if( fn.FileExists() && !fn.IsFileWritable() )
|
|
|
|
{
|
2022-09-10 20:26:25 +00:00
|
|
|
m_infoBar->RemoveAllButtons();
|
|
|
|
m_infoBar->AddCloseButton();
|
|
|
|
m_infoBar->ShowMessage( _( "Layout file is read only." ),
|
|
|
|
wxICON_WARNING, WX_INFOBAR::MESSAGE_TYPE::OUTDATED_SAVE );
|
2020-05-18 23:44:58 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 18:27:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-12 00:31:38 +00:00
|
|
|
|
2021-05-30 22:56:24 +00:00
|
|
|
bool PL_EDITOR_FRAME::InsertDrawingSheetFile( const wxString& aFullFileName )
|
2013-07-19 18:27:22 +00:00
|
|
|
{
|
|
|
|
if( wxFileExists( aFullFileName ) )
|
|
|
|
{
|
|
|
|
const bool append = true;
|
2013-07-26 12:50:29 +00:00
|
|
|
SaveCopyInUndoList();
|
2021-02-22 23:47:17 +00:00
|
|
|
DS_DATA_MODEL::GetTheInstance().LoadDrawingSheet( aFullFileName, append );
|
2013-07-19 18:27:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-30 22:56:24 +00:00
|
|
|
bool PL_EDITOR_FRAME::SaveDrawingSheetFile( const wxString& aFullFileName )
|
2013-07-19 18:27:22 +00:00
|
|
|
{
|
2020-06-19 23:31:26 +00:00
|
|
|
if( !aFullFileName.IsEmpty() )
|
2013-07-19 18:27:22 +00:00
|
|
|
{
|
2022-10-30 08:17:35 +00:00
|
|
|
wxString tempFile = wxFileName::CreateTempFileName( "pledit" );
|
2020-06-19 23:31:26 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2022-03-08 04:08:54 +00:00
|
|
|
DS_DATA_MODEL::GetTheInstance().Save( tempFile );
|
2020-06-19 23:31:26 +00:00
|
|
|
}
|
2021-02-20 16:58:12 +00:00
|
|
|
catch( const IO_ERROR& )
|
2020-06-19 23:31:26 +00:00
|
|
|
{
|
|
|
|
// In case we started a file but didn't fully write it, clean up
|
2022-03-08 04:08:54 +00:00
|
|
|
wxRemoveFile( tempFile);
|
2020-06-19 23:31:26 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-25 00:07:46 +00:00
|
|
|
// Preserve the permissions of the current file
|
|
|
|
KIPLATFORM::IO::DuplicatePermissions( aFullFileName, tempFile );
|
|
|
|
|
2022-03-08 04:08:54 +00:00
|
|
|
if( !wxRenameFile( tempFile, aFullFileName ) )
|
2020-06-19 23:31:26 +00:00
|
|
|
return false;
|
|
|
|
|
2022-09-10 20:26:25 +00:00
|
|
|
if( m_infoBar->GetMessageType() == WX_INFOBAR::MESSAGE_TYPE::OUTDATED_SAVE )
|
|
|
|
m_infoBar->Dismiss();
|
|
|
|
|
2021-05-28 19:07:04 +00:00
|
|
|
GetScreen()->SetContentModified( false );
|
2023-02-09 13:15:01 +00:00
|
|
|
UpdateTitleAndInfo();
|
2013-07-19 18:27:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2022-09-14 22:28:09 +00:00
|
|
|
|
|
|
|
void PL_EDITOR_FRAME::DoWithAcceptedFiles()
|
|
|
|
{
|
2022-09-19 23:19:15 +00:00
|
|
|
for( const wxFileName& file : m_AcceptedFiles )
|
2022-09-14 22:28:09 +00:00
|
|
|
OpenProjectFiles( { file.GetFullPath() }, KICTL_CREATE );
|
2023-05-25 00:07:46 +00:00
|
|
|
}
|