KiCad PCB EDA Suite
sch_view.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) 2013-2018 CERN
5  * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
8  * @author Maciej Suminski <maciej.suminski@cern.ch>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, you may find one here:
22  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23  * or you may search the http://www.gnu.org website for the version 2 license,
24  * or you may write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26  */
27 
28 #include <memory>
29 #include <view/view.h>
30 #include <view/view_group.h>
31 #include <view/view_rtree.h>
32 #include <view/wx_view_controls.h>
33 #include <ws_proxy_view_item.h>
35 #include <class_libentry.h>
36 #include <sch_sheet.h>
37 #include <sch_screen.h>
38 #include <sch_component.h>
39 #include <lib_pin.h>
41 #include <sch_edit_frame.h>
42 
43 #include "sch_view.h"
44 
45 
46 namespace KIGFX {
47 
48 
49 SCH_VIEW::SCH_VIEW( bool aIsDynamic, SCH_BASE_FRAME* aFrame ) :
50  VIEW( aIsDynamic )
51 {
52  m_frame = aFrame;
53  // Set m_boundary to define the max working area size. The default value
54  // is acceptable for Pcbnew and Gerbview, but too large for Eeschema due to
55  // very different internal units.
56  // So we have to use a smaller value.
57  // A full size = 3 * MAX_PAGE_SIZE_MILS size allows a wide margin
58  // around the worksheet.
59  double max_size = Mils2iu( MAX_PAGE_SIZE_MILS ) * 3.0;
60  m_boundary.SetOrigin( -max_size/4, -max_size/4 );
61  m_boundary.SetSize( max_size, max_size );
62 
64  m_preview.reset( new KIGFX::VIEW_GROUP() );
65 }
66 
67 
69 {
70 }
71 
72 
73 void SCH_VIEW::SetScale( double aScale, VECTOR2D aAnchor )
74 {
75  VIEW::SetScale( aScale, aAnchor );
76 
77  //Redraw selection halos since their width is dependent on zoom
78  if( m_frame )
80 }
81 
82 
84 {
85  const PAGE_INFO& page_info = aScreen->GetPageSettings();
86  double max_size_x = page_info.GetWidthIU() * 3.0;
87  double max_size_y = page_info.GetHeightIU() * 3.0;
88  m_boundary.SetOrigin( -max_size_x/4, -max_size_y/4 );
89  m_boundary.SetSize( max_size_x, max_size_y );
90 }
91 
92 
94 {
95  for( auto item : aScreen->Items() )
96  Add( item );
97 
98  m_worksheet.reset( new KIGFX::WS_PROXY_VIEW_ITEM( static_cast< int >( IU_PER_MILS ),
99  &aScreen->GetPageSettings(),
100  &aScreen->GetTitleBlock() ) );
101  m_worksheet->SetSheetNumber( aScreen->m_ScreenNumber );
102  m_worksheet->SetSheetCount( aScreen->m_NumberOfScreens );
103  m_worksheet->SetFileName( TO_UTF8( aScreen->GetFileName() ) );
104  m_worksheet->SetColorLayer( LAYER_SCHEMATIC_WORKSHEET );
105 
106  if( m_frame && m_frame->IsType( FRAME_SCH ) )
107  m_worksheet->SetSheetName( TO_UTF8( m_frame->GetScreenDesc() ) );
108  else
109  m_worksheet->SetSheetName( "" );
110 
111  ResizeSheetWorkingArea( aScreen );
112 
114  m_preview.reset( new KIGFX::VIEW_GROUP() );
115 
116  Add( m_worksheet.get() );
117  Add( m_selectionArea.get() );
118  Add( m_preview.get() );
119 }
120 
121 
123 {
124  DisplaySheet( aSheet->GetScreen() );
125 }
126 
127 
129 {
130  Clear();
131 
132  if( !aPart )
133  return;
134 
135  std::shared_ptr< LIB_PART > parent;
136  LIB_PART* drawnPart = aPart;
137 
138  // Draw the mandatory fields for aliases and parent symbols.
139  for( auto& item : aPart->GetDrawItems() )
140  {
141  if( item.Type() != LIB_FIELD_T )
142  continue;
143 
144  LIB_FIELD* field = static_cast< LIB_FIELD* >( &item );
145 
146  if( field->GetId() < MANDATORY_FIELDS )
147  Add( &item );
148  }
149 
150  // Draw the parent items if the symbol is inherited from another symbol.
151  if( aPart->IsAlias() )
152  {
153  parent = aPart->GetParent().lock();
154 
155  wxCHECK( parent, /* void */ );
156 
157  drawnPart = parent.get();
158  }
159 
160  for( auto& item : drawnPart->GetDrawItems() )
161  {
162  // The mandatory fields are already in place so we only add user defined fields.
163  if( item.Type() == LIB_FIELD_T )
164  {
165  LIB_FIELD* field = static_cast< LIB_FIELD* >( &item );
166 
167  if( field->GetId() < MANDATORY_FIELDS )
168  continue;
169  }
170 
171  Add( &item );
172  }
173 
175  m_preview.reset( new KIGFX::VIEW_GROUP() );
176  Add( m_selectionArea.get() );
177  Add( m_preview.get() );
178 }
179 
180 
182 {
183  m_preview->Clear();
184 
185  for( auto item : m_ownedItems )
186  delete item;
187 
188  m_ownedItems.clear();
189  Update( m_preview.get() );
190 }
191 
192 
193 void SCH_VIEW::AddToPreview( EDA_ITEM* aItem, bool aTakeOwnership )
194 {
195  Hide( aItem, false );
196  m_preview->Add( aItem );
197 
198  if( aTakeOwnership )
199  m_ownedItems.push_back( aItem );
200 
201  SetVisible( m_preview.get(), true );
202  Hide( m_preview.get(), false );
203  Update( m_preview.get() );
204 }
205 
206 
207 void SCH_VIEW::ShowPreview( bool aShow )
208 {
209  SetVisible( m_preview.get(), aShow );
210 }
211 
212 
214 {
215  for( auto item : *m_allItems )
216  Hide( item, false );
217 }
218 
219 
221 {
222  // SetVisible( m_worksheet.get(), false );
223 }
224 
225 
227 {
228  if( aItem && aItem->Type() == SCH_COMPONENT_T && aPin )
229  {
230  static_cast<SCH_COMPONENT*>( aItem )->HighlightPin( aPin );
231  Update( aItem, REPAINT );
232  }
233  else if( aItem )
234  {
235  aItem->SetFlags( HIGHLIGHTED );
236  Update( aItem, REPAINT );
237  }
238  else
239  {
240  for( auto item : *m_allItems )
241  {
242  // Not all view items can be highlighted, only EDA_ITEMs
243  // So clear flag of only EDA_ITEMs.
244  EDA_ITEM* eitem = dynamic_cast<EDA_ITEM*>( item );
245 
246  if( eitem )
247  {
248  if( eitem->IsHighlighted() )
249  {
250  eitem->ClearFlags( HIGHLIGHTED );
251  Update( eitem, REPAINT );
252  }
253 
254  if( eitem->Type() == SCH_COMPONENT_T )
255  {
256  // Items inside a component (pins, fields can be highlighted.
257  static_cast<SCH_COMPONENT*>( eitem )->ClearAllHighlightFlags();
258  }
259  }
260  }
261  }
262 }
263 
264 }; // namespace KIGFX
void RefreshSelection()
Mark selected items for refresh.
void Hide(VIEW_ITEM *aItem, bool aHide=true)
Temporarily hides the item in the view (e.g.
Definition: view.cpp:1507
const wxString & GetFileName() const
Definition: sch_screen.h:157
int m_ScreenNumber
Definition: base_screen.h:135
void ClearHiddenFlags()
Clear the hide flag of all items in the view.
Definition: sch_view.cpp:213
void AddToPreview(EDA_ITEM *aItem, bool aTakeOwnership=true)
Definition: sch_view.cpp:193
void DisplayComponent(LIB_PART *aPart)
Definition: sch_view.cpp:128
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:131
void ResizeSheetWorkingArea(SCH_SCREEN *aScreen)
Definition: sch_view.cpp:83
The first 4 are mandatory, and must be instantiated in SCH_COMPONENT and LIB_PART constructors.
Field object used in symbol libraries.
Definition: lib_field.h:59
void DisplaySheet(SCH_SHEET *aSheet)
Definition: sch_view.cpp:122
const TITLE_BLOCK & GetTitleBlock() const
Definition: sch_screen.h:162
void SetSize(const Vec &size)
Definition: box2.h:211
int GetId() const
Definition: lib_field.h:139
SCH_SCREEN * GetScreen()
Definition: sch_sheet.h:282
std::unique_ptr< KIGFX::PREVIEW::SELECTION_AREA > m_selectionArea
Definition: sch_view.h:108
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:152
void HighlightItem(EDA_ITEM *aItem, LIB_PIN *aPin=nullptr)
Definition: sch_view.cpp:226
bool IsHighlighted() const
Definition: base_struct.h:227
WX_VIEW_CONTROLS class definition.
std::vector< EDA_ITEM * > m_ownedItems
Definition: sch_view.h:110
std::shared_ptr< std::vector< VIEW_ITEM * > > m_allItems
Flat list of all items.
Definition: view.h:815
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:48
void HideWorksheet()
Definition: sch_view.cpp:220
Item is being added to the view.
Definition: view_item.h:60
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:257
void Clear()
Function Clear() Removes all items from the view.
Definition: view.cpp:1111
LIB_ITEMS_CONTAINER & GetDrawItems()
Return a reference to the draw item list.
PAGE_INFO describes the page size and margins of a paper page on which to eventually print or plot.
Definition: page_info.h:54
void ShowPreview(bool aShow=true)
Definition: sch_view.cpp:207
Class LIB_PIN definition.
PART_REF & GetParent()
void ClearPreview()
Definition: sch_view.cpp:181
Define a library symbol object.
VIEW_GROUP extends VIEW_ITEM by possibility of grouping items into a single object.
std::unique_ptr< WS_PROXY_VIEW_ITEM > m_worksheet
Definition: sch_view.h:107
virtual void Update(VIEW_ITEM *aItem, int aUpdateFlags)
For dynamic VIEWs, informs the associated VIEW that the graphical representation of this item has cha...
Definition: view.cpp:1540
void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 }) override
Function SetScale() Sets the scaling factor, zooming around a given anchor point.
Definition: sch_view.cpp:73
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:215
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Function SetScale() Sets the scaling factor, zooming around a given anchor point.
Definition: view.cpp:578
#define HIGHLIGHTED
item is drawn in normal colors, when the rest is darkened
Definition: base_struct.h:142
Board layer functions and definitions.
bool IsType(FRAME_T aType) const
BOX2D m_boundary
View boundaries.
Definition: view.h:830
EE_RTREE & Items()
Definition: sch_screen.h:127
bool IsAlias() const
void SetOrigin(const Vec &pos)
Definition: box2.h:209
#define IU_PER_MILS
Definition: plotter.cpp:137
EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boards.
Definition: base_struct.h:166
SCH_BASE_FRAME * m_frame
Definition: sch_view.h:104
void ClearFlags(STATUS_FLAGS aMask=EDA_ITEM_ALL_FLAGS)
Definition: base_struct.h:258
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Sets the item visibility.
Definition: view.cpp:1486
SCH_VIEW(bool aIsDynamic, SCH_BASE_FRAME *aFrame)
Definition: sch_view.cpp:49
int m_NumberOfScreens
Definition: base_screen.h:136
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:346
#define MAX_PAGE_SIZE_MILS
Definition: page_info.h:41
VIEW.
Definition: view.h:61
A shim class between EDA_DRAW_FRAME and several derived classes: LIB_EDIT_FRAME, LIB_VIEW_FRAME,...
std::unique_ptr< KIGFX::VIEW_GROUP > m_preview
Definition: sch_view.h:109
KICAD_T Type() const
Function Type()
Definition: base_struct.h:212
virtual wxString GetScreenDesc() const