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 void PL_EDITOR_FRAME::OnFileHistory( wxCommandEvent& event )
45 {
46  wxString filename;
47 
48  filename = GetFileFromHistory( event.GetId(), _( "Page Layout Description File" ) );
49 
50  if( filename != wxEmptyString )
51  {
52  if( GetScreen()->IsModify() && !IsOK( this,
53  _( "The current page layout has been modified.\n"
54  "Do you wish to discard the changes?" ) ) )
55  return;
56 
58  ::wxSetWorkingDirectory( ::wxPathOnly( filename ) );
59 
60  if( LoadPageLayoutDescrFile( filename ) )
61  {
62  wxString msg;
63  msg.Printf( _( "File <%s> loaded"), GetChars( filename ) );
64  SetStatusText( msg );
65  }
66 
68  }
69 }
70 
71 
72 /* File commands. */
73 void PL_EDITOR_FRAME::Files_io( wxCommandEvent& event )
74 {
75  wxString msg;
76  int id = event.GetId();
77  wxString filename = GetCurrFileName();
79 
80  if( filename.IsEmpty() && id == wxID_SAVE )
81  id = wxID_SAVEAS;
82 
83  switch( id )
84  {
85  case wxID_NEW:
86  case wxID_OPEN:
87  if( GetScreen()->IsModify() && !IsOK( this,
88  _( "The current page layout has been modified.\n"
89  "Do you wish to discard the changes?" ) ) )
90  return;
91  break;
92 
93  default:
94  break;
95  }
96 
97  switch( id )
98  {
99  case wxID_NEW:
100  pglayout.AllowVoidList( true );
101  SetCurrFileName( wxEmptyString );
102  pglayout.ClearList();
103  OnNewPageLayout();
104  break;
105 
107  {
108  wxFileDialog openFileDialog( this, _( "Append Existing Page Layout File" ),
109  wxEmptyString, wxEmptyString,
110  PageLayoutDescrFileWildcard(), wxFD_OPEN );
111 
112  if( openFileDialog.ShowModal() == wxID_CANCEL )
113  return;
114 
115  filename = openFileDialog.GetPath();
116 
117  if( ! InsertPageLayoutDescrFile( filename ) )
118  {
119  msg.Printf( _( "Unable to load %s file" ), GetChars( filename ) );
120  wxMessageBox( msg );
121  }
122  else
123  {
124  GetScreen()->SetModify();
126  m_canvas->Refresh();
127  msg.Printf( _( "File <%s> inserted" ), GetChars( filename ) );
128  SetStatusText( msg );
129  }
130  }
131  break;
132 
133  case wxID_OPEN:
134  {
135  wxFileDialog openFileDialog( this, _( "Open" ), wxEmptyString, wxEmptyString,
136  PageLayoutDescrFileWildcard(), wxFD_OPEN );
137 
138  if( openFileDialog.ShowModal() == wxID_CANCEL )
139  return;
140 
141  filename = openFileDialog.GetPath();
142 
143  if( ! LoadPageLayoutDescrFile( filename ) )
144  {
145  msg.Printf( _( "Unable to load %s file" ), GetChars( filename ) );
146  wxMessageBox( msg );
147  }
148  else
149  {
150  OnNewPageLayout();
151  msg.Printf( _( "File <%s> loaded" ), GetChars( filename ) );
152  SetStatusText( msg );
153  }
154  }
155  break;
156 
157  case wxID_SAVE:
158  if( !SavePageLayoutDescrFile( filename ) )
159  {
160  msg.Printf( _( "Unable to write <%s>" ), GetChars( filename ) );
161  wxMessageBox( msg );
162  }
163  else
164  {
165  msg.Printf( _("File <%s> written"), GetChars( filename ) );
166  SetStatusText( msg );
167  }
168  break;
169 
170  case wxID_SAVEAS:
171  {
172  wxFileDialog openFileDialog( this, _( "Save As" ), wxEmptyString, wxEmptyString,
173  PageLayoutDescrFileWildcard(), wxFD_SAVE );
174 
175  if( openFileDialog.ShowModal() == wxID_CANCEL )
176  return;
177 
178  filename = openFileDialog.GetPath();
179  // Ensure the file has the right extension:
180  // because a name like name.subname.subsubname is legal,
181  // add the right extension without replacing the wxFileName
182  // extension
183  wxFileName fn(filename);
184 
185  if( fn.GetExt() != PageLayoutDescrFileExtension )
186  filename << wxT(".") << PageLayoutDescrFileExtension;
187 
188  if( !SavePageLayoutDescrFile( filename ) )
189  {
190  msg.Printf( _("Unable to create <%s>"), GetChars( filename ) );
191  wxMessageBox( msg );
192  }
193 
194  else
195  {
196  msg.Printf( _("File <%s> written"), GetChars( filename ) );
197  SetStatusText( msg );
198 
199  if( GetCurrFileName().IsEmpty() )
200  SetCurrFileName( filename );
201  }
202  }
203  break;
204 
205  default:
206  wxMessageBox( wxT( "File_io: unexpected command id" ) );
207  break;
208  }
209 }
210 
211 
212 bool PL_EDITOR_FRAME::LoadPageLayoutDescrFile( const wxString& aFullFileName )
213 {
214  if( wxFileExists( aFullFileName ) )
215  {
217  SetCurrFileName( aFullFileName );
218  UpdateFileHistory( aFullFileName );
219  GetScreen()->ClrModify();
220  return true;
221  }
222 
223  return false;
224 }
225 
226 
227 bool PL_EDITOR_FRAME::InsertPageLayoutDescrFile( const wxString& aFullFileName )
228 {
229  if( wxFileExists( aFullFileName ) )
230  {
231  const bool append = true;
233  WORKSHEET_LAYOUT::GetTheInstance().SetPageLayout( aFullFileName, append );
234  return true;
235  }
236 
237  return false;
238 }
239 
240 
241 bool PL_EDITOR_FRAME::SavePageLayoutDescrFile( const wxString& aFullFileName )
242 {
243  if( ! aFullFileName.IsEmpty() )
244  {
245  WORKSHEET_LAYOUT::GetTheInstance().Save( aFullFileName );
246  GetScreen()->ClrModify();
247  return true;
248  }
249 
250  return false;
251 }
void OnFileHistory(wxCommandEvent &event)
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.
virtual void Refresh(bool eraseBackground=true, const wxRect *rect=NULL) override
Update the board display after modifying it bu a python script (note: it is automatically called by a...
Definition: draw_panel.cpp:325
This file is part of the common library.
void EndMouseCapture(int aId=-1, int aCursorId=-1, const wxString &aTitle=wxEmptyString, bool aCallEndFunc=true)
Function EndMouseCapture ends mouse a capture.
bool IsModify() const
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.
Definition: basicframe.cpp:407
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.
const wxString PageLayoutDescrFileExtension
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
Function GetScreen returns 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...
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:93
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.
Definition: basicframe.cpp:419
bool LoadPageLayoutDescrFile(const wxString &aFullFileName)
Function LoadPageLayoutDescrFile Loads a .kicad_wks page layout descr file.
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 ...
int GetDefaultCursor() const
Function GetDefaultCursor.
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Function IsOK displays a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:129
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)