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  * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, you may find one here:
23  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
24  * or you may search the http://www.gnu.org website for the version 2 license,
25  * or you may write to the Free Software Foundation, Inc.,
26  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28 
29 #include <fctsys.h>
30 #include <common.h>
31 #include <class_drawpanel.h>
32 #include <confirm.h>
33 #include <gestfich.h>
34 #include <macros.h>
36 
37 #include <pl_editor_frame.h>
38 #include <properties_frame.h>
39 #include <pl_editor_id.h>
41 
42 void PL_EDITOR_FRAME::OnFileHistory( wxCommandEvent& event )
43 {
44  wxString filename;
45 
46  filename = GetFileFromHistory( event.GetId(), _( "Page Layout Description File" ) );
47 
48  if( filename != wxEmptyString )
49  {
50  if( GetScreen()->IsModify() && !IsOK( this,
51  _( "The current page layout has been modified.\n"
52  "Do you wish to discard the changes?" ) ) )
53  return;
54 
56  ::wxSetWorkingDirectory( ::wxPathOnly( filename ) );
57  if( LoadPageLayoutDescrFile( filename ) )
58  {
59  wxString msg;
60  msg.Printf( _("File <%s> loaded"), GetChars( filename ) );
61  SetStatusText( msg );
62  }
63 
65  }
66 }
67 
68 /* File commands. */
69 void PL_EDITOR_FRAME::Files_io( wxCommandEvent& event )
70 {
71  wxString msg;
72  int id = event.GetId();
73  wxString filename = GetCurrFileName();
75 
76  if( filename.IsEmpty() && id == wxID_SAVE )
77  id = wxID_SAVEAS;
78 
79  switch( id )
80  {
81  case wxID_NEW:
82  case wxID_OPEN:
83  if( GetScreen()->IsModify() && !IsOK( this,
84  _( "The current page layout has been modified.\n"
85  "Do you wish to discard the changes?" ) ) )
86  return;
87  break;
88 
89  default:
90  break;
91  }
92 
93 
94  switch( id )
95  {
96  case wxID_NEW:
97  pglayout.AllowVoidList( true );
98  SetCurrFileName( wxEmptyString );
99  pglayout.ClearList();
100  OnNewPageLayout();
101  break;
102 
104  {
105  wxFileDialog openFileDialog(this, _("Append Existing Page Layout File"),
106  wxEmptyString,
107  wxEmptyString, PageLayoutDescrFileWildcard, wxFD_OPEN);
108 
109  if (openFileDialog.ShowModal() == wxID_CANCEL)
110  return;
111 
112  filename = openFileDialog.GetPath();
113  if( ! InsertPageLayoutDescrFile( filename ) )
114  {
115  msg.Printf( _("Unable to load %s file"), GetChars( filename ) );
116  wxMessageBox( msg );
117  }
118  else
119  {
120  GetScreen()->SetModify();
122  m_canvas->Refresh();
123  msg.Printf( _("File <%s> inserted"), GetChars( filename ) );
124  SetStatusText( msg );
125  }
126  }
127  break;
128 
129  case wxID_OPEN:
130  {
131  wxFileDialog openFileDialog(this, _("Open"), wxEmptyString,
132  wxEmptyString, PageLayoutDescrFileWildcard, wxFD_OPEN);
133 
134  if (openFileDialog.ShowModal() == wxID_CANCEL)
135  return;
136 
137  filename = openFileDialog.GetPath();
138  if( ! LoadPageLayoutDescrFile( filename ) )
139  {
140  msg.Printf( _("Unable to load %s file"), GetChars( filename ) );
141  wxMessageBox( msg );
142  }
143  else
144  {
145  OnNewPageLayout();
146  msg.Printf( _("File <%s> loaded"), GetChars( filename ) );
147  SetStatusText( msg );
148  }
149  }
150  break;
151 
152  case wxID_SAVE:
153  if( !SavePageLayoutDescrFile( filename ) )
154  {
155  msg.Printf( _("Unable to write <%s>"), GetChars( filename ) );
156  wxMessageBox( msg );
157  }
158  else
159  {
160  msg.Printf( _("File <%s> written"), GetChars( filename ) );
161  SetStatusText( msg );
162  }
163  break;
164 
165  case wxID_SAVEAS:
166  {
167  wxFileDialog openFileDialog(this, _("Save As"), wxEmptyString,
168  wxEmptyString, PageLayoutDescrFileWildcard, wxFD_SAVE);
169 
170  if (openFileDialog.ShowModal() == wxID_CANCEL)
171  return;
172 
173  filename = openFileDialog.GetPath();
174  // Ensure the file has the right extension:
175  // because a name like name.subname.subsubname is legal,
176  // add the right extension without replacing the wxFileName
177  // extension
178  wxFileName fn(filename);
179 
180  if( fn.GetExt() != PageLayoutDescrFileExtension )
181  filename << wxT(".") << PageLayoutDescrFileExtension;
182 
183  if( !SavePageLayoutDescrFile( filename ) )
184  {
185  msg.Printf( _("Unable to create <%s>"), GetChars( filename ) );
186  wxMessageBox( msg );
187  }
188 
189  else
190  {
191  msg.Printf( _("File <%s> written"), GetChars( filename ) );
192  SetStatusText( msg );
193  if( GetCurrFileName().IsEmpty() )
194  SetCurrFileName( filename );
195  }
196  }
197  break;
198 
199  default:
200  wxMessageBox( wxT( "File_io: unexpected command id" ) );
201  break;
202  }
203 }
204 
205 /* Loads a .kicad_wks page layout descr file
206  */
207 bool PL_EDITOR_FRAME::LoadPageLayoutDescrFile( const wxString& aFullFileName )
208 {
209  if( wxFileExists( aFullFileName ) )
210  {
212  SetCurrFileName( aFullFileName );
213  UpdateFileHistory( aFullFileName );
214  GetScreen()->ClrModify();
215  return true;
216  }
217 
218  return false;
219 }
220 
221 /* Inserts a .kicad_wks page layout descr file
222  * same as LoadPageLayoutDescrFile, but the new data is added
223  * to the previous data.
224  */
225 bool PL_EDITOR_FRAME::InsertPageLayoutDescrFile( const wxString& aFullFileName )
226 {
227  if( wxFileExists( aFullFileName ) )
228  {
229  const bool append = true;
231  WORKSHEET_LAYOUT::GetTheInstance().SetPageLayout( aFullFileName, append );
232  return true;
233  }
234 
235  return false;
236 }
237 
238 
239 /* Save the current layout in a .kicad_wks page layout descr file
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)
const 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:388
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
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:92
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:400
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 ...
const wxString PageLayoutDescrFileExtension
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)