KiCad PCB EDA Suite
class_worksheet_layout.cpp
Go to the documentation of this file.
1 
6 /*
7  * This program source code file is part of KiCad, a free EDA CAD application.
8  *
9  * Copyright (C) 1992-2013 Jean-Pierre Charras <jp.charras at wanadoo.fr>.
10  * Copyright (C) 1992-2013 KiCad Developers, see change_log.txt for contributors.
11  *
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 
32 /*
33  * the class WORKSHEET_DATAITEM (and derived ) defines
34  * a basic shape of a page layout ( frame references and title block )
35  * The list of these items is stored in a WORKSHEET_LAYOUT instance.
36  *
37  *
38  * These items cannot be drawn or plot "as this". they should be converted
39  * to a "draw list". When building the draw list:
40  * the WORKSHEET_LAYOUT is used to create a WS_DRAW_ITEM_LIST
41  * coordinates are converted to draw/plot coordinates.
42  * texts are expanded if they contain format symbols.
43  * Items with m_RepeatCount > 1 are created m_RepeatCount times
44  *
45  * the WORKSHEET_LAYOUT is created only once.
46  * the WS_DRAW_ITEM_LIST is created each time the page layout is plot/drawn
47  *
48  * the WORKSHEET_LAYOUT instance is created from a S expression which
49  * describes the page layout (can be the default page layout or a custom file).
50  */
51 
52 #include <fctsys.h>
53 #include <kiface_i.h>
54 #include <drawtxt.h>
55 #include <worksheet.h>
56 #include <class_title_block.h>
59 
60 
61 // The layout shape used in the application
62 // It is accessible by WORKSHEET_LAYOUT::GetTheInstance()
65 
67 {
68  m_allowVoidList = false;
69  m_leftMargin = 10.0; // the left page margin in mm
70  m_rightMargin = 10.0; // the right page margin in mm
71  m_topMargin = 10.0; // the top page margin in mm
72  m_bottomMargin = 10.0; // the bottom page margin in mm
73 }
74 
75 /* static function: returns the instance of WORKSHEET_LAYOUT
76  * used in the application
77  */
79 {
80  if( wksAltInstance )
81  return *wksAltInstance;
82  else
83  return wksTheInstance;
84 }
85 
93 {
94  wksAltInstance = aLayout;
95 }
96 
97 
98 void WORKSHEET_LAYOUT::SetLeftMargin( double aMargin )
99 {
100  m_leftMargin = aMargin; // the left page margin in mm
101 }
102 
103 
104 void WORKSHEET_LAYOUT::SetRightMargin( double aMargin )
105 {
106  m_rightMargin = aMargin; // the right page margin in mm
107 }
108 
109 
110 void WORKSHEET_LAYOUT::SetTopMargin( double aMargin )
111 {
112  m_topMargin = aMargin; // the top page margin in mm
113 }
114 
115 
116 void WORKSHEET_LAYOUT::SetBottomMargin( double aMargin )
117 {
118  m_bottomMargin = aMargin; // the bottom page margin in mm
119 }
120 
121 
123 {
124  for( unsigned ii = 0; ii < m_list.size(); ii++ )
125  delete m_list[ii];
126  m_list.clear();
127 }
128 
129 
130 void WORKSHEET_LAYOUT::Insert( WORKSHEET_DATAITEM* aItem, unsigned aIdx )
131 {
132  if ( aIdx >= GetCount() )
133  Append( aItem );
134  else
135  m_list.insert( m_list.begin() + aIdx, aItem );
136 }
137 
138 
139 bool WORKSHEET_LAYOUT::Remove( unsigned aIdx )
140 {
141  if ( aIdx >= GetCount() )
142  return false;
143  m_list.erase( m_list.begin() + aIdx );
144  return true;
145 }
146 
147 
149 {
150  unsigned idx = 0;
151 
152  while( idx < m_list.size() )
153  {
154  if( m_list[idx] == aItem )
155  break;
156 
157  idx++;
158  }
159 
160  return Remove( idx );
161 }
162 
163 
165 {
166  unsigned idx = 0;
167  while( idx < m_list.size() )
168  {
169  if( m_list[idx] == aItem )
170  return (int) idx;
171 
172  idx++;
173  }
174 
175  return -1;
176 }
177 
178 /* return the item from its index aIdx, or NULL if does not exist
179  */
181 {
182  if( aIdx < m_list.size() )
183  return m_list[aIdx];
184  else
185  return NULL;
186 }
187 
188 
189 const wxString WORKSHEET_LAYOUT::MakeShortFileName( const wxString& aFullFileName,
190  const wxString& aProjectPath )
191 {
192  wxString shortFileName = aFullFileName;
193  wxFileName fn = aFullFileName;
194 
195  if( fn.IsRelative() )
196  return shortFileName;
197 
198  if( ! aProjectPath.IsEmpty() && aFullFileName.StartsWith( aProjectPath ) )
199  {
200  fn.MakeRelativeTo( aProjectPath );
201  shortFileName = fn.GetFullPath();
202  return shortFileName;
203  }
204 
205  wxString fileName = Kiface().KifaceSearch().FindValidPath( fn.GetFullName() );
206 
207  if( !fileName.IsEmpty() )
208  {
209  fn = fileName;
210  shortFileName = fn.GetFullName();
211  return shortFileName;
212  }
213 
214  return shortFileName;
215 }
216 
217 
218 const wxString WORKSHEET_LAYOUT::MakeFullFileName( const wxString& aShortFileName,
219  const wxString& aProjectPath )
220 {
221  wxString fullFileName = ExpandEnvVarSubstitutions( aShortFileName );
222 
223  if( fullFileName.IsEmpty() )
224  return fullFileName;
225 
226  wxFileName fn = fullFileName;
227 
228  if( fn.IsAbsolute() )
229  return fullFileName;
230 
231  // the path is not absolute: search it in project path, and then in
232  // kicad valid paths
233  if( !aProjectPath.IsEmpty() )
234  {
235  fn.MakeAbsolute( aProjectPath );
236 
237  if( wxFileExists( fn.GetFullPath() ) )
238  return fn.GetFullPath();
239  }
240 
241  fn = fullFileName;
242  wxString name = Kiface().KifaceSearch().FindValidPath( fn.GetFullName() );
243 
244  if( !name.IsEmpty() )
245  fullFileName = name;
246 
247  return fullFileName;
248 }
description of graphic items and texts to build a title block
void SetLeftMargin(double aMargin)
void SetTopMargin(double aMargin)
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
Definition: kicad.cpp:52
int GetItemIndex(WORKSHEET_DATAITEM *aItem) const
static WORKSHEET_LAYOUT & GetTheInstance()
static function: returns the instance of WORKSHEET_LAYOUT used in the application ...
static WORKSHEET_LAYOUT * wksAltInstance
void SetBottomMargin(double aMargin)
void Append(WORKSHEET_DATAITEM *aItem)
Add an item to the list of items.
SEARCH_STACK & KifaceSearch()
Only for DSO specific 'non-library' files.
Definition: kiface_i.h:125
const wxString ExpandEnvVarSubstitutions(const wxString &aString)
Function ExpandEnvVarSubstitutions replaces any environment variable references with their values...
Definition: common.cpp:254
WORKSHEET_DATAITEM * GetItem(unsigned aIdx) const
bool Remove(unsigned aIdx)
Remove the item to the list of items at position aIdx.
void ClearList()
erase the list of items
static WORKSHEET_LAYOUT wksTheInstance
WORKSHEET_LAYOUT handles the graphic items list to draw/plot the title block and other items (page re...
unsigned GetCount() const
std::vector< WORKSHEET_DATAITEM * > m_list
static const wxString MakeShortFileName(const wxString &aFullFileName, const wxString &aProjectPath)
static const wxString MakeFullFileName(const wxString &aShortFileName, const wxString &aProjectPath)
Static function.
const char * name
This file is part of the common libary.
classes and function to generate graphics to plt or draw titles blocks and frame references ...
wxString FindValidPath(const wxString &aFileName) const
Definition: search_stack.h:71
static void SetAltInstance(WORKSHEET_LAYOUT *aLayout=NULL)
static function: Set an alternate instance of WORKSHEET_LAYOUT mainly used in page setting dialog ...
void Insert(WORKSHEET_DATAITEM *aItem, unsigned aIdx)
Insert an item to the list of items at position aIdx.
void SetRightMargin(double aMargin)