KiCad PCB EDA Suite
design_inspector.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) 2019 jp.charras at wanadoo.fr
5  * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <wx/imaglist.h>
26 #include <wx/wupdlock.h>
27 #include <fctsys.h>
28 #include <ws_draw_item.h>
29 #include <ws_data_model.h>
30 #include <pl_editor_id.h>
31 #include <design_inspector.h>
32 #include <tool/tool_manager.h>
33 #include <tools/pl_actions.h>
35 #include <properties_frame.h>
36 
37 /* XPM
38  * This bitmap is used to show item types
39  */
40 static const char* root_xpm[] =
41 {
42  "12 12 2 1",
43  " c None",
44  "x c #008080",
45  " xxxx ",
46  " xxx ",
47  " xxx ",
48  " xxx ",
49  "xxxxxxxxxxx ",
50  "xxxxxxxxxxxx",
51  "xxxxxxxxxxx ",
52  " xxx ",
53  " xxx ",
54  " xxx ",
55  " xxxx ",
56  " "
57 };
58 
59 static const char* line_xpm[] =
60 {
61  "12 12 2 1",
62  " c None",
63  "x c #008080",
64  "xx ",
65  "xx ",
66  "xx ",
67  "xx ",
68  "xx ",
69  "xx ",
70  "xx ",
71  "xx ",
72  "xx ",
73  "xx ",
74  "xxxxxxxxxxxx",
75  "xxxxxxxxxxxx"
76 };
77 
78 static const char* rect_xpm[] =
79 {
80  "12 12 2 1",
81  " c None",
82  "x c #000080",
83  "xxxxxxxxxxxx",
84  "xxxxxxxxxxxx",
85  "xx xx",
86  "xx xx",
87  "xx xx",
88  "xx xx",
89  "xx xx",
90  "xx xx",
91  "xx xx",
92  "xx xx",
93  "xxxxxxxxxxxx",
94  "xxxxxxxxxxxx"
95 };
96 
97 static const char* text_xpm[] =
98 {
99  "12 12 2 1",
100  " c None",
101  "x c #800000",
102  " xxxxxxxxxx ",
103  "xxxxxxxxxxxx",
104  "xx xx xx",
105  " xx ",
106  " xx ",
107  " xx ",
108  " xx ",
109  " xx ",
110  " xx ",
111  " xx ",
112  " xxxx ",
113  " xxxxxx "
114 };
115 
116 static const char* poly_xpm[] =
117 {
118  "12 12 2 1",
119  " c None",
120  "x c #008000",
121  " xx ",
122  " xxxx ",
123  " xxxxxx ",
124  " xxxxxxxx ",
125  " xxxxxxxxxx ",
126  "xxxxxxxxxxxx",
127  "xxxxxxxxxxxx",
128  " xxxxxxxxxx ",
129  " xxxxxxxx ",
130  " xxxxxx ",
131  " xxxx ",
132  " xx "
133 };
134 
135 static const char* img_xpm[] =
136 {
137  "12 12 2 1",
138  " c None",
139  "x c #800000",
140  " xx ",
141  " xxxxxx ",
142  " xx xx ",
143  "xx xx",
144  "xx xx",
145  " xx xx ",
146  " xxxxxx ",
147  " xx ",
148  " xx ",
149  " xx ",
150  " xx ",
151  " xx "
152 };
153 
154 // A helper class to draw these bitmaps into a wxGrid cell:
155 class BitmapGridCellRenderer : public wxGridCellStringRenderer
156 {
157  const char** m_BitmapXPM;
158 public:
159  BitmapGridCellRenderer( const char** aBitmapXPM )
160  {
161  m_BitmapXPM = aBitmapXPM;
162  }
163 
164  void Draw( wxGrid& aGrid, wxGridCellAttr& aAttr,
165  wxDC& aDc, const wxRect& aRect,
166  int aRow, int aCol, bool aIsSelected) override;
167 };
168 
169 // Column ids for m_gridListItems
171 {
177  COL_COUNT //Sentinel
178 };
179 
180 
182  DIALOG_INSPECTOR_BASE( aParent )
183 {
184  m_editorFrame = aParent;
186 
187  // Now all widgets have the size fixed, call FinishDialogSettings
189 }
190 
191 
193 {
194 }
195 
196 
197 
199 {
200  wxWindowUpdateLocker dummy( this ); // Avoid flicker when rebuilding the tree
201  const PAGE_INFO& page_info = m_editorFrame->GetPageLayout().GetPageSettings();
202 
203  if( m_gridListItems->GetNumberRows() > 1 )
204  m_gridListItems->DeleteRows( 1, m_gridListItems->GetNumberRows() - 1 );
205 
206  m_itemsList.clear();
207 
209 
210  wxFileName fn( ((PL_EDITOR_FRAME*) GetParent())->GetCurrFileName() );
211 
212  if( fn.GetName().IsEmpty() )
213  SetTitle( "<default page layout>" );
214  else
215  SetTitle( fn.GetName() );
216 
217  // The first item is the layout: Display info about the page: fmt, size...
218  int row = 0;
219  GetGridList()->SetCellValue( row, COL_TYPENAME, _( "Layout" ) );
220  GetGridList()->SetCellValue( row, COL_COMMENT, page_info.GetType() ); // Display page format name
221  GetGridList()->SetCellValue( row, COL_REPEAT_NUMBER, "-" );
222  wxSize page_sizeIU = m_editorFrame->GetPageSizeIU();
223  GetGridList()->SetCellValue( row, COL_TEXTSTRING, wxString::Format( _( "Size: %.1fx%.1fmm" ),
224  Iu2Millimeter( page_sizeIU.x ), Iu2Millimeter( page_sizeIU.y ) ) );
225  GetGridList()->SetCellRenderer (row, COL_BITMAP, new BitmapGridCellRenderer( root_xpm ) );
226  GetGridList()->SetReadOnly( row, COL_BITMAP );
227  m_itemsList.push_back( nullptr ); // this item is not a WS_DATA_ITEM, just a pseudo item
228 
229  // Now adding all current items
230  row++;
231  for( WS_DATA_ITEM* item : pglayout.GetItems() )
232  {
233  const char** img = nullptr;
234 
235  switch( item->GetType() )
236  {
238  img = line_xpm;
239  break;
240 
242  img = rect_xpm;
243  break;
244 
246  img = text_xpm;
247  break;
248 
250  img = poly_xpm;
251  break;
252 
254  img = img_xpm;
255  break;
256  }
257 
258  GetGridList()->AppendRows( 1 );
259  GetGridList()->SetCellRenderer (row, COL_BITMAP, new BitmapGridCellRenderer( img ) );
260  GetGridList()->SetReadOnly( row, COL_BITMAP );
261  GetGridList()->SetCellValue( row, COL_TYPENAME,item->GetClassName() );
262  GetGridList()->SetCellValue( row, COL_REPEAT_NUMBER,
263  wxString::Format( "%d", item->m_RepeatCount ) );
264  GetGridList()->SetCellValue( row, COL_COMMENT, item->m_Info );
265 
266  if( item->GetType() == WS_DATA_ITEM::WS_TEXT )
267  {
268  WS_DATA_ITEM_TEXT* t_item = static_cast<WS_DATA_ITEM_TEXT*>( item );
269  GetGridList()->SetCellValue( row, COL_TEXTSTRING, t_item->m_TextBase );
270  }
271 
272  m_itemsList.push_back( item );
273  row++;
274  }
275 
276  // Now resize the columns:
277  int cols_to_resize[] =
278  {
280  };
281 
282  for( int ii = 0; ; ii++ )
283  {
284  int col = cols_to_resize[ii];
285 
286  if( col == COL_COUNT )
287  break;
288 
289  if( col == COL_BITMAP )
290  {
291  #define BITMAP_SIZE 16
292  GetGridList()->SetColMinimalWidth( col, BITMAP_SIZE*2 );
293  GetGridList()->AutoSizeColumn( col, false );
294  }
295  else
296  GetGridList()->AutoSizeColumn( col );
297 
298  GetGridList()->AutoSizeColLabelSize( col );
299  }
300 }
301 
302 
303 // Select the row corresponding to the WS_DATA_ITEM aItem
305 {
306  // m_itemsList[0] is not a true WS_DATA_ITEM
307  for( unsigned row = 1; row < m_itemsList.size(); ++row )
308  {
309  if( m_itemsList[row] == aItem )
310  {
311  GetGridList()->GoToCell( row, COL_TYPENAME );
312  GetGridList()->SelectRow( row );
313  break;
314  }
315  }
316 }
317 
318 //return the page layout item managed by the cell
320 {
321  return ( aRow >= 0 && aRow < (int)m_itemsList.size() ) ? m_itemsList[aRow]: nullptr;
322 }
323 
324 
325 /* return the page layout item managed by the selected cell (or NULL)
326  */
328 {
329  int idx = GetGridList()->GetGridCursorRow();
330  return GetWsDataItem( idx );
331 }
332 
333 
334 void DIALOG_INSPECTOR::onCellClicked( wxGridEvent& event )
335 {
336  int row = event.GetRow();
337  GetGridList()->SelectRow( row );
338 
339  WS_DATA_ITEM* item = GetWsDataItem( row );
340 
341  if( !item ) // only WS_DATA_ITEM are returned.
342  return;
343 
344  // Select this item in page layout editor, and update the properties panel:
346  selectionTool->ClearSelection();
347  EDA_ITEM* draw_item = item->GetDrawItems()[0];
348  selectionTool->AddItemToSel( draw_item );
351 }
352 
353 
354 void BitmapGridCellRenderer::Draw( wxGrid& aGrid, wxGridCellAttr& aAttr,
355  wxDC& aDc, const wxRect& aRect,
356  int aRow, int aCol, bool aIsSelected)
357 {
358  wxGridCellStringRenderer::Draw( aGrid, aAttr, aDc, aRect, aRow, aCol, aIsSelected);
359  wxBitmap bm( m_BitmapXPM );
360  aDc.DrawBitmap( bm,aRect.GetX()+5, aRect.GetY()+2, true);
361 }
362 
363 
365 {
366  DIALOG_INSPECTOR dlg( this );
367 
368  dlg.ShowModal();
369 }
WS_DATA_MODEL handles the graphic items list to draw/plot the frame and title block.
Definition: ws_data_model.h:39
DIALOG_INSPECTOR(PL_EDITOR_FRAME *aParent)
PROPERTIES_FRAME * GetPropertiesFrame()
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
Class DIALOG_INSPECTOR_BASE.
PL_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
Work sheet structure type definitions.
Definition: ws_data_item.h:93
Class PL_EDITOR_FRAME is the main window used in the page layout editor.
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
static const char * line_xpm[]
int ClearSelection(const TOOL_EVENT &aEvent)
WS_DATA_ITEM * GetWsDataItem(int aRow) const
const wxString & GetType() const
Definition: page_info.h:97
std::vector< WS_DATA_ITEM * > & GetItems()
const wxSize GetPageSizeIU() const override
Works off of GetPageSettings() to return the size of the paper page in the internal units of this par...
void onCellClicked(wxGridEvent &event) override
int AddItemToSel(const TOOL_EVENT &aEvent)
PAGE_INFO & GetPageSettings()
static WS_DATA_MODEL & GetTheInstance()
static function: returns the instance of WS_DATA_MODEL used in the application
static const char * img_xpm[]
std::vector< WS_DATA_ITEM * > m_itemsList
void SelectRow(WS_DATA_ITEM *aItem)
void CopyPrmsFromItemToPanel(WS_DATA_ITEM *aItem)
Class PAGE_INFO describes the page size and margins of a paper page on which to eventually print or p...
Definition: page_info.h:54
void ShowDesignInspector()
Show the dialog displaying the list of WS_DATA_ITEM items in the page layout.
#define BITMAP_SIZE
static const char * rect_xpm[]
static const char * root_xpm[]
#define _(s)
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
const std::vector< WS_DRAW_ITEM_BASE * > & GetDrawItems() const
Definition: ws_data_item.h:128
PL_EDITOR_FRAME * m_editorFrame
BitmapGridCellRenderer(const char **aBitmapXPM)
WS_DATA_ITEM * GetSelectedWsDataItem() const
wxGrid * GetGridList() const
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
static const char * poly_xpm[]
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:163
Class DESIGN_INSPECTOR is the left window showing the list of items.
void Draw(wxGrid &aGrid, wxGridCellAttr &aAttr, wxDC &aDc, const wxRect &aRect, int aRow, int aCol, bool aIsSelected) override
const PL_EDITOR_LAYOUT & GetPageLayout() const
static const char * text_xpm[]