KiCad PCB EDA Suite
pagelayout_editor/files.cpp
Go to the documentation of this file.
1 
5 /*
6  * This program source code file is part of KiCad, a free EDA CAD application.
7  *
8  * Copyright (C) 2013 CERN
9  * Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
10  *
11  * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, you may find one here:
25  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
26  * or you may search the http://www.gnu.org website for the version 2 license,
27  * or you may write to the Free Software Foundation, Inc.,
28  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
29  */
30 
31 #include <fctsys.h>
32 #include <common.h>
33 #include <class_drawpanel.h>
34 #include <confirm.h>
35 #include <gestfich.h>
36 #include <macros.h>
38 
39 #include <pl_editor_frame.h>
40 #include <properties_frame.h>
41 #include <pl_editor_id.h>
43 
44 
46 {
47  wxCommandEvent saveEvent;
48  saveEvent.SetId( wxID_SAVE );
49  Files_io( saveEvent );
50 
51  return( !GetScreen()->IsModify() );
52 }
53 
54 
55 void PL_EDITOR_FRAME::OnFileHistory( wxCommandEvent& event )
56 {
57  wxString filename;
58 
59  filename = GetFileFromHistory( event.GetId(), _( "Page Layout Description File" ) );
60 
61  if( filename != wxEmptyString )
62  {
63  if( GetScreen()->IsModify() )
64  {
65  if( !HandleUnsavedChanges( this,
66  _( "The current page layout has been modified. Save changes?" ),
67  [&]()->bool { return saveCurrentPageLayout(); } ) )
68  {
69  return;
70  }
71  }
72 
74  ::wxSetWorkingDirectory( ::wxPathOnly( filename ) );
75 
76  if( LoadPageLayoutDescrFile( filename ) )
77  {
78  wxString msg;
79  msg.Printf( _( "File \"%s\" loaded"), GetChars( filename ) );
80  SetStatusText( msg );
81  }
82 
84  }
85 }
86 
87 
88 /* File commands. */
89 void PL_EDITOR_FRAME::Files_io( wxCommandEvent& event )
90 {
91  wxString msg;
92  int id = event.GetId();
93  wxString filename = GetCurrFileName();
95 
96  if( filename.IsEmpty() && id == wxID_SAVE )
97  id = wxID_SAVEAS;
98 
99  if( ( id == wxID_NEW || id == wxID_OPEN ) && GetScreen()->IsModify() )
100  {
101  if( !HandleUnsavedChanges( this, _( "The current page layout has been modified. Save changes?" ),
102  [&]()->bool { return saveCurrentPageLayout(); } ) )
103  {
104  return;
105  }
106  }
107 
108  switch( id )
109  {
110  case wxID_NEW:
111  pglayout.AllowVoidList( true );
112  SetCurrFileName( wxEmptyString );
113  pglayout.ClearList();
114  OnNewPageLayout();
115  break;
116 
118  {
119  wxFileDialog openFileDialog( this, _( "Append Existing Page Layout File" ),
120  wxEmptyString, wxEmptyString,
121  PageLayoutDescrFileWildcard(), wxFD_OPEN );
122 
123  if( openFileDialog.ShowModal() == wxID_CANCEL )
124  return;
125 
126  filename = openFileDialog.GetPath();
127 
128  if( ! InsertPageLayoutDescrFile( filename ) )
129  {
130  msg.Printf( _( "Unable to load %s file" ), GetChars( filename ) );
131  wxMessageBox( msg );
132  }
133  else
134  {
135  GetScreen()->SetModify();
137  m_canvas->Refresh();
138  msg.Printf( _( "File \"%s\" inserted" ), GetChars( filename ) );
139  SetStatusText( msg );
140  }
141  }
142  break;
143 
144  case wxID_OPEN:
145  {
146  wxFileDialog openFileDialog( this, _( "Open" ), wxEmptyString, wxEmptyString,
147  PageLayoutDescrFileWildcard(), wxFD_OPEN );
148 
149  if( openFileDialog.ShowModal() == wxID_CANCEL )
150  return;
151 
152  filename = openFileDialog.GetPath();
153 
154  if( ! LoadPageLayoutDescrFile( filename ) )
155  {
156  msg.Printf( _( "Unable to load %s file" ), GetChars( filename ) );
157  wxMessageBox( msg );
158  }
159  else
160  {
161  OnNewPageLayout();
162  msg.Printf( _( "File \"%s\" loaded" ), GetChars( filename ) );
163  SetStatusText( msg );
164  }
165  }
166  break;
167 
168  case wxID_SAVE:
169  if( !SavePageLayoutDescrFile( filename ) )
170  {
171  msg.Printf( _( "Unable to write \"%s\"" ), GetChars( filename ) );
172  wxMessageBox( msg );
173  }
174  else
175  {
176  msg.Printf( _("File \"%s\" written"), GetChars( filename ) );
177  SetStatusText( msg );
178  }
179  break;
180 
181  case wxID_SAVEAS:
182  {
183  wxFileDialog openFileDialog( this, _( "Save As" ), wxEmptyString, wxEmptyString,
184  PageLayoutDescrFileWildcard(), wxFD_SAVE );
185 
186  if( openFileDialog.ShowModal() == wxID_CANCEL )
187  return;
188 
189  filename = openFileDialog.GetPath();
190  // Ensure the file has the right extension:
191  // because a name like name.subname.subsubname is legal,
192  // add the right extension without replacing the wxFileName
193  // extension
194  wxFileName fn(filename);
195 
196  if( fn.GetExt() != PageLayoutDescrFileExtension )
197  filename << wxT(".") << PageLayoutDescrFileExtension;
198 
199  if( !SavePageLayoutDescrFile( filename ) )
200  {
201  msg.Printf( _("Unable to create \"%s\""), GetChars( filename ) );
202  wxMessageBox( msg );
203  }
204 
205  else
206  {
207  msg.Printf( _("File \"%s\" written"), GetChars( filename ) );
208  SetStatusText( msg );
209 
210  if( GetCurrFileName().IsEmpty() )
211  SetCurrFileName( filename );
212  }
213  }
214  break;
215 
216  default:
217  wxMessageBox( wxT( "File_io: unexpected command id" ) );
218  break;
219  }
220 }
221 
222 
223 bool PL_EDITOR_FRAME::LoadPageLayoutDescrFile( const wxString& aFullFileName )
224 {
225  if( wxFileExists( aFullFileName ) )
226  {
228  SetCurrFileName( aFullFileName );
229  UpdateFileHistory( aFullFileName );
230  GetScreen()->ClrModify();
231  return true;
232  }
233 
234  return false;
235 }
236 
237 
238 bool PL_EDITOR_FRAME::InsertPageLayoutDescrFile( const wxString& aFullFileName )
239 {
240  if( wxFileExists( aFullFileName ) )
241  {
242  const bool append = true;
244  WORKSHEET_LAYOUT::GetTheInstance().SetPageLayout( aFullFileName, append );
245  return true;
246  }
247 
248  return false;
249 }
250 
251 
252 bool PL_EDITOR_FRAME::SavePageLayoutDescrFile( const wxString& aFullFileName )
253 {
254  if( ! aFullFileName.IsEmpty() )
255  {
256  WORKSHEET_LAYOUT::GetTheInstance().Save( aFullFileName );
257  GetScreen()->ClrModify();
258  return true;
259  }
260 
261  return false;
262 }
void OnFileHistory(wxCommandEvent &event)
bool HandleUnsavedChanges(wxWindow *aParent, const wxString &aMessage, const std::function< bool()> &aSaveFunction)
Function HandleUnsavedChanges displays a dialog with Save, Cancel and Discard Changes buttons...
Definition: confirm.cpp:213
wxString PageLayoutDescrFileWildcard()
void AllowVoidList(bool Allow)
In Kicad applications, a page layout description is needed So if the list is empty, a default description is loaded, the first time a page layout is drawn.
This file is part of the common library TODO brief description.
This file is part of the common library.
virtual void EndMouseCapture(int aId=-1, int aCursorId=-1, const wxString &aTitle=wxEmptyString, bool aCallEndFunc=true)
Function EndMouseCapture ends mouse a capture.
bool IsModify() const
Definition: base_screen.h:328
void Save(const wxString &aFullFileName)
Save the description in a file.
void UpdateFileHistory(const wxString &FullFileName, wxFileHistory *aFileHistory=NULL)
Function UpdateFileHistory Updates the list of recently opened files.
static WORKSHEET_LAYOUT & GetTheInstance()
static function: returns the instance of WORKSHEET_LAYOUT used in the application ...
This file contains miscellaneous commonly used macros and functions.
void ClearList()
erase the list of items
void OnNewPageLayout()
Must be called to initialize parameters when a new page layout description is loaded.
The common library.
PL_EDITOR_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
bool SavePageLayoutDescrFile(const wxString &aFullFileName)
Function SavePageLayoutDescrFile Save the current layout in a .kicad_wks page layout descr file...
const std::string PageLayoutDescrFileExtension
WORKSHEET_LAYOUT handles the graphic items list to draw/plot the title block and other items (page re...
EDA_DRAW_PANEL * m_canvas
The area to draw on.
Definition: draw_frame.h:123
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
void SetCurrFileName(const wxString &aName)
Stores the current layout descr file filename.
bool InsertPageLayoutDescrFile(const wxString &aFullFileName)
Function InsertPageLayoutDescrFile Loads a .kicad_wks page layout descr file, and add items to the cu...
wxString GetFileFromHistory(int cmdId, const wxString &type, wxFileHistory *aFileHistory=NULL)
Function GetFileFromHistory fetches the file name from the file history list.
bool LoadPageLayoutDescrFile(const wxString &aFullFileName)
Function LoadPageLayoutDescrFile Loads a .kicad_wks page layout descr file.
void ClrModify()
Definition: base_screen.h:325
The common library.
void SaveCopyInUndoList()
Save a copy of the description (in a S expr string) for Undo/redo commands.
void RebuildDesignTree()
Function RebuildDesignTree Re creates the design graphic tree list items, and ensures each item has a...
classes and function to generate graphics to plt or draw titles blocks and frame references ...
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL)
void SetModify()
Definition: base_screen.h:324
int GetDefaultCursor() const
Function GetDefaultCursor.
const wxString & GetCurrFileName() const
void SetPageLayout(const wxString &aFullFileName=wxEmptyString, bool Append=false)
Populates the list with a custom layout, or the default layout, if no custom layout available...
void Files_io(wxCommandEvent &event)