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  {
82  case wxID_NEW:
83  case wxID_OPEN:
84  if( GetScreen()->IsModify() && !IsOK( this,
85  _( "The current page layout has been modified.\n"
86  "Do you wish to discard the changes?" ) ) )
87  return;
88  break;
89 
90  default:
91  break;
92  }
93 
94 
95  switch( id )
96  {
98  pglayout.SetPageLayout();
100  break;
101 
102  case wxID_NEW:
103  pglayout.AllowVoidList( true );
104  SetCurrFileName( wxEmptyString );
105  pglayout.ClearList();
106  OnNewPageLayout();
107  break;
108 
110  {
111  wxFileDialog openFileDialog(this, _("Append Page Layout Descr File"),
112  wxEmptyString,
113  wxEmptyString, PageLayoutDescrFileWildcard, wxFD_OPEN);
114 
115  if (openFileDialog.ShowModal() == wxID_CANCEL)
116  return;
117 
118  filename = openFileDialog.GetPath();
119  if( ! InsertPageLayoutDescrFile( filename ) )
120  {
121  msg.Printf( _("Unable to load %s file"), GetChars( filename ) );
122  wxMessageBox( msg );
123  }
124  else
125  {
126  GetScreen()->SetModify();
128  m_canvas->Refresh();
129  msg.Printf( _("File <%s> inserted"), GetChars( filename ) );
130  SetStatusText( msg );
131  }
132  }
133  break;
134 
135  case wxID_OPEN:
136  {
137  wxFileDialog openFileDialog(this, _("Open file"), wxEmptyString,
138  wxEmptyString, PageLayoutDescrFileWildcard, wxFD_OPEN);
139 
140  if (openFileDialog.ShowModal() == wxID_CANCEL)
141  return;
142 
143  filename = openFileDialog.GetPath();
144  if( ! LoadPageLayoutDescrFile( filename ) )
145  {
146  msg.Printf( _("Unable to load %s file"), GetChars( filename ) );
147  wxMessageBox( msg );
148  }
149  else
150  {
151  OnNewPageLayout();
152  msg.Printf( _("File <%s> loaded"), GetChars( filename ) );
153  SetStatusText( msg );
154  }
155  }
156  break;
157 
158  case wxID_SAVE:
159  if( !SavePageLayoutDescrFile( filename ) )
160  {
161  msg.Printf( _("Unable to write <%s>"), GetChars( filename ) );
162  wxMessageBox( msg );
163  }
164  else
165  {
166  msg.Printf( _("File <%s> written"), GetChars( filename ) );
167  SetStatusText( msg );
168  }
169  break;
170 
171  case wxID_SAVEAS:
172  {
173  wxFileDialog openFileDialog(this, _("Create file"), wxEmptyString,
174  wxEmptyString, PageLayoutDescrFileWildcard, wxFD_SAVE);
175 
176  if (openFileDialog.ShowModal() == wxID_CANCEL)
177  return;
178 
179  filename = openFileDialog.GetPath();
180  // Ensure the file has the right extension:
181  // because a name like name.subname.subsubname is legal,
182  // add the right extension without replacing the wxFileName
183  // extension
184  wxFileName fn(filename);
185 
186  if( fn.GetExt() != PageLayoutDescrFileExtension )
187  filename << wxT(".") << PageLayoutDescrFileExtension;
188 
189  if( !SavePageLayoutDescrFile( filename ) )
190  {
191  msg.Printf( _("Unable to create <%s>"), GetChars( filename ) );
192  wxMessageBox( msg );
193  }
194 
195  else
196  {
197  msg.Printf( _("File <%s> written"), GetChars( filename ) );
198  SetStatusText( msg );
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 /* Loads a .kicad_wks page layout descr file
212  */
213 bool PL_EDITOR_FRAME::LoadPageLayoutDescrFile( const wxString& aFullFileName )
214 {
215  if( wxFileExists( aFullFileName ) )
216  {
218  SetCurrFileName( aFullFileName );
219  UpdateFileHistory( aFullFileName );
220  GetScreen()->ClrModify();
221  return true;
222  }
223 
224  return false;
225 }
226 
227 /* Inserts a .kicad_wks page layout descr file
228  * same as LoadPageLayoutDescrFile, but the new data is added
229  * to the previous data.
230  */
231 bool PL_EDITOR_FRAME::InsertPageLayoutDescrFile( const wxString& aFullFileName )
232 {
233  if( wxFileExists( aFullFileName ) )
234  {
235  const bool append = true;
237  WORKSHEET_LAYOUT::GetTheInstance().SetPageLayout( aFullFileName, append );
238  return true;
239  }
240 
241  return false;
242 }
243 
244 
245 /* Save the current layout in a .kicad_wks page layout descr file
246  */
247 bool PL_EDITOR_FRAME::SavePageLayoutDescrFile( const wxString& aFullFileName )
248 {
249  if( ! aFullFileName.IsEmpty() )
250  {
251  WORKSHEET_LAYOUT::GetTheInstance().Save( aFullFileName );
252  GetScreen()->ClrModify();
253  return true;
254  }
255 
256  return false;
257 }
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
Definition: draw_panel.cpp:326
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:111
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)