KiCad PCB EDA Suite
ws_data_model.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 1992-2018 Jean-Pierre Charras <jp.charras at wanadoo.fr>.
5  * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 
27 /*
28  * The WS_DATA_ITEM_* classes define the basic shapes of a page layout (frame references
29  * and title block). The list of these items is stored in a WS_DATA_MODEL instance.
30  *
31  * These items cannot be drawn or plotetd "as is". They must be converted to WS_DRAW_*
32  * types. When building the draw list:
33  * - the WS_DATA_MODEL is used to create a WS_DRAW_ITEM_LIST
34  * - coordinates are converted to draw/plot coordinates.
35  * - texts are expanded if they contain format symbols.
36  * - items with m_RepeatCount > 1 are created m_RepeatCount times.
37  *
38  * The WS_DATA_MODEL is created only once.
39  * The WS_DRAW_ITEM_*s are created and maintained by the PlEditor, but are created each time
40  * they're needed for drawing by the clients (Eeschema, Pcbnew, etc.)
41  *
42  * The WS_DATA_MODEL instance is created from a S expression which describes the page
43  * layout (can be the default page layout or a custom file). This format is also used
44  * for undo/redo storage (wrapped in a WS_PROXY_UNDO_ITEM).
45  */
46 
47 #include <kiface_i.h>
48 #include <title_block.h>
49 #include <common.h>
50 #include <eda_item.h>
54 #include <page_layout/ws_painter.h>
55 
56 
57 // The layout shape used in the application
58 // It is accessible by WS_DATA_MODEL::GetTheInstance()
61 
63  m_WSunits2Iu( 1000.0 ),
64  m_DefaultLineWidth( 0.0 ),
65  m_DefaultTextSize( TB_DEFAULT_TEXTSIZE, TB_DEFAULT_TEXTSIZE ),
66  m_DefaultTextThickness( 0.0 ),
67  m_EditMode( false )
68 {
69  m_allowVoidList = false;
70  m_leftMargin = 10.0; // the left page margin in mm
71  m_rightMargin = 10.0; // the right page margin in mm
72  m_topMargin = 10.0; // the top page margin in mm
73  m_bottomMargin = 10.0; // the bottom page margin in mm
74 }
75 
76 /* static function: returns the instance of WS_DATA_MODEL
77  * used in the application
78  */
80 {
81  if( wksAltInstance )
82  return *wksAltInstance;
83  else
84  return wksTheInstance;
85 }
86 
94 {
95  wksAltInstance = aLayout;
96 }
97 
98 
99 void WS_DATA_MODEL::SetupDrawEnvironment( const PAGE_INFO& aPageInfo, double aMilsToIU )
100 {
101 #define MILS_TO_MM (25.4/1000)
102 
103  m_WSunits2Iu = aMilsToIU / MILS_TO_MM;
104 
105  // Left top corner position
106  DPOINT lt_corner;
107  lt_corner.x = GetLeftMargin();
108  lt_corner.y = GetTopMargin();
109  m_LT_Corner = lt_corner;
110 
111  // Right bottom corner position
112  DPOINT rb_corner;
113  rb_corner.x = ( aPageInfo.GetSizeMils().x * MILS_TO_MM ) - GetRightMargin();
114  rb_corner.y = ( aPageInfo.GetSizeMils().y * MILS_TO_MM ) - GetBottomMargin();
115  m_RB_Corner = rb_corner;
116 }
117 
118 
120 {
121  for( WS_DATA_ITEM* item : m_list )
122  delete item;
123 
124  m_list.clear();
125 }
126 
127 
129 {
130  m_list.push_back( aItem );
131 }
132 
133 
135 {
136  auto newEnd = std::remove( m_list.begin(), m_list.end(), aItem );
137  m_list.erase( newEnd, m_list.end() );
138 }
139 
140 
142 {
143  unsigned idx = 0;
144  while( idx < m_list.size() )
145  {
146  if( m_list[idx] == aItem )
147  return (int) idx;
148 
149  idx++;
150  }
151 
152  return -1;
153 }
154 
155 /* return the item from its index aIdx, or NULL if does not exist
156  */
157 WS_DATA_ITEM* WS_DATA_MODEL::GetItem( unsigned aIdx ) const
158 {
159  if( aIdx < m_list.size() )
160  return m_list[aIdx];
161  else
162  return nullptr;
163 }
164 
165 
166 const wxString WS_DATA_MODEL::MakeShortFileName( const wxString& aFullFileName,
167  const wxString& aProjectPath )
168 {
169  wxString shortFileName = aFullFileName;
170  wxFileName fn = aFullFileName;
171 
172  if( fn.IsRelative() )
173  return shortFileName;
174 
175  if( ! aProjectPath.IsEmpty() && aFullFileName.StartsWith( aProjectPath ) )
176  {
177  fn.MakeRelativeTo( aProjectPath );
178  shortFileName = fn.GetFullPath();
179  return shortFileName;
180  }
181 
182  wxString fileName = Kiface().KifaceSearch().FindValidPath( fn.GetFullName() );
183 
184  if( !fileName.IsEmpty() )
185  {
186  fn = fileName;
187  shortFileName = fn.GetFullName();
188  return shortFileName;
189  }
190 
191  return shortFileName;
192 }
193 
194 
195 const wxString WS_DATA_MODEL::MakeFullFileName( const wxString& aShortFileName,
196  const wxString& aProjectPath )
197 {
198  wxString fullFileName = ExpandEnvVarSubstitutions( aShortFileName, nullptr );
199 
200  if( fullFileName.IsEmpty() )
201  return fullFileName;
202 
203  wxFileName fn = fullFileName;
204 
205  if( fn.IsAbsolute() )
206  return fullFileName;
207 
208  // the path is not absolute: search it in project path, and then in
209  // kicad valid paths
210  if( !aProjectPath.IsEmpty() )
211  {
212  fn.MakeAbsolute( aProjectPath );
213 
214  if( wxFileExists( fn.GetFullPath() ) )
215  return fn.GetFullPath();
216  }
217 
218  fn = fullFileName;
219  wxString name = Kiface().KifaceSearch().FindValidPath( fn.GetFullName() );
220 
221  if( !name.IsEmpty() )
222  fullFileName = name;
223 
224  return fullFileName;
225 }
int GetItemIndex(WS_DATA_ITEM *aItem) const
WS_DATA_MODEL handles the graphic items list to draw/plot the frame and title block.
Definition: ws_data_model.h:38
DPOINT m_RB_Corner
Definition: ws_data_model.h:52
static WS_DATA_MODEL * wksAltInstance
wxString FindValidPath(const wxString &aFileName) const
Definition: search_stack.h:73
double GetTopMargin()
Definition: ws_data_model.h:89
Work sheet structure type definitions.
Definition: ws_data_item.h:97
std::vector< WS_DATA_ITEM * > m_list
Definition: ws_data_model.h:40
double m_bottomMargin
Definition: ws_data_model.h:47
WS_DATA_ITEM * GetItem(unsigned aIdx) const
bool m_allowVoidList
Definition: ws_data_model.h:41
#define TB_DEFAULT_TEXTSIZE
Definition: ws_data_item.h:40
const wxString ExpandEnvVarSubstitutions(const wxString &aString, PROJECT *aProject)
Replace any environment variable & text variable references with their values.
Definition: common.cpp:255
DPOINT m_LT_Corner
Definition: ws_data_model.h:53
double GetBottomMargin()
Definition: ws_data_model.h:92
double m_leftMargin
Definition: ws_data_model.h:44
void ClearList()
erase the list of items
static WS_DATA_MODEL & GetTheInstance()
static function: returns the instance of WS_DATA_MODEL used in the application
static void SetAltInstance(WS_DATA_MODEL *aLayout=NULL)
static function: Set an alternate instance of WS_DATA_MODEL mainly used in page setting dialog
SEARCH_STACK & KifaceSearch()
Only for DSO specific 'non-library' files.
Definition: kiface_i.h:127
static const wxString MakeShortFileName(const wxString &aFullFileName, const wxString &aProjectPath)
double GetRightMargin()
Definition: ws_data_model.h:86
PAGE_INFO describes the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:54
KIFACE_I & Kiface()
Global KIFACE_I "get" accessor.
double m_topMargin
Definition: ws_data_model.h:46
const wxSize & GetSizeMils() const
Definition: page_info.h:143
#define MILS_TO_MM
void Remove(WS_DATA_ITEM *aItem)
void SetupDrawEnvironment(const PAGE_INFO &aPageInfo, double aMilsToIU)
static WS_DATA_MODEL wksTheInstance
static const wxString MakeFullFileName(const wxString &aShortFileName, const wxString &aProjectPath)
Static function.
const char * name
Definition: DXF_plotter.cpp:59
double GetLeftMargin()
Definition: ws_data_model.h:83
double m_WSunits2Iu
Definition: ws_data_model.h:50
The common library.
void Append(WS_DATA_ITEM *aItem)
double m_rightMargin
Definition: ws_data_model.h:45