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  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
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 #include <memory>
27 #include <view/view.h>
28 #include <view/view_group.h>
29 #include <view/view_rtree.h>
30 #include <view/wx_view_controls.h>
31 #include <worksheet_viewitem.h>
33 #include <class_libentry.h>
34 #include <sch_sheet.h>
35 #include <sch_screen.h>
36 #include <sch_component.h>
37 #include <lib_pin.h>
39 #include <sch_edit_frame.h>
40 
41 #include "sch_view.h"
42 
43 
44 namespace KIGFX {
45 
46 
47 SCH_VIEW::SCH_VIEW( bool aIsDynamic, SCH_BASE_FRAME* aFrame ) :
48  VIEW( aIsDynamic )
49 {
50  m_frame = aFrame;
51  // Set m_boundary to define the max working area size. The default value
52  // is acceptable for Pcbnew and Gerbview, but too large for Eeschema due to
53  // very different internal units.
54  // So we have to use a smaller value.
55  // A full size = 3 * MAX_PAGE_SIZE_EDITORS_MILS size allows a wide margin
56  // around the worksheet.
57  double max_size = MAX_PAGE_SIZE_EDITORS_MILS * IU_PER_MILS * 3.0;
58  m_boundary.SetOrigin( -max_size/4, -max_size/4 );
59  m_boundary.SetSize( max_size, max_size );
60 
62  m_preview.reset( new KIGFX::VIEW_GROUP() );
63 }
64 
65 
67 {
68 }
69 
70 
72 {
73  const PAGE_INFO& page_info = aScreen->GetPageSettings();
74  // A full size = 3 * page size allows a wide margin around the worksheet.
75  // This is useful to have a large working area.
76  double max_size_x = page_info.GetWidthMils() * IU_PER_MILS * 2.0;
77  double max_size_y = page_info.GetHeightMils() * IU_PER_MILS * 2.0;
78  m_boundary.SetOrigin( -max_size_x /4, -max_size_y/4 );
79  m_boundary.SetSize( max_size_x, max_size_y );
80 }
81 
82 
84 {
85  for( auto item = aScreen->GetDrawItems(); item; item = item->Next() )
86  Add( item );
87 
88  m_worksheet.reset( new KIGFX::WORKSHEET_VIEWITEM( 1, &aScreen->GetPageSettings(),
89  &aScreen->GetTitleBlock() ) );
90  m_worksheet->SetSheetNumber( aScreen->m_ScreenNumber );
91  m_worksheet->SetSheetCount( aScreen->m_NumberOfScreens );
92  m_worksheet->SetFileName( TO_UTF8( aScreen->GetFileName() ) );
93 
94  if( m_frame && m_frame->IsType( FRAME_SCH ) )
95  m_worksheet->SetSheetName( TO_UTF8( m_frame->GetScreenDesc() ) );
96  else
97  m_worksheet->SetSheetName( "" );
98 
99  ResizeSheetWorkingArea( aScreen );
100 
102  m_preview.reset( new KIGFX::VIEW_GROUP() );
103 
104  Add( m_worksheet.get() );
105  Add( m_selectionArea.get() );
106  Add( m_preview.get() );
107 }
108 
109 
111 {
112  DisplaySheet( aSheet->GetScreen() );
113 }
114 
115 
117 {
118  Clear();
119 
120  if( !aPart )
121  return;
122 
123  for( auto& item : aPart->GetDrawItems() )
124  Add( &item );
125 
127  m_preview.reset( new KIGFX::VIEW_GROUP() );
128  Add( m_selectionArea.get() );
129  Add( m_preview.get() );
130 }
131 
132 
134 {
135  m_preview->Clear();
136 
137  for( auto item : m_ownedItems )
138  delete item;
139 
140  m_ownedItems.clear();
141  Update( m_preview.get() );
142 }
143 
144 
145 void SCH_VIEW::AddToPreview( EDA_ITEM* aItem, bool aTakeOwnership )
146 {
147  Hide( aItem, false );
148  m_preview->Add( aItem );
149 
150  if( aTakeOwnership )
151  m_ownedItems.push_back( aItem );
152 
153  SetVisible( m_preview.get(), true );
154  Hide( m_preview.get(), false );
155  Update( m_preview.get() );
156 }
157 
158 
159 void SCH_VIEW::ShowSelectionArea( bool aShow )
160 {
161  if( aShow )
162  {
163  // Reset seleciton area so the previous one doesn't flash before the first
164  // mouse move updates it
165  m_selectionArea->SetOrigin( VECTOR2I() );
166  m_selectionArea->SetEnd( VECTOR2I() );
167  }
168 
169  SetVisible( m_selectionArea.get(), aShow );
170 }
171 
172 
173 void SCH_VIEW::ShowPreview( bool aShow )
174 {
175  SetVisible( m_preview.get(), aShow );
176 }
177 
178 
180 {
181  for( auto item : *m_allItems )
182  Hide( item, false );
183 }
184 
185 
187 {
188  // SetVisible( m_worksheet.get(), false );
189 }
190 
192 {
193  if( !aItem )
194  {
195  for( auto item : *m_allItems )
196  {
197  // Not all view items can be highlighted, only EDA_ITEMs
198  // So clear flag of only EDA_ITEMs.
199  auto eitem = dynamic_cast<EDA_ITEM *>( item );
200 
201  if( eitem )
202  {
203  eitem->ClearFlags( HIGHLIGHTED );
204 
205  if( eitem->Type() == SCH_COMPONENT_T )
206  {
207  // Items inside a component (pins, fields can be highlighted.
208  static_cast<SCH_COMPONENT*>( eitem )->ClearAllHighlightFlags();
209  }
210  }
211  }
212  }
213  else
214  {
215  if( ( aItem->Type() == SCH_COMPONENT_T ) && aPin )
216  {
217  static_cast<SCH_COMPONENT*>( aItem )->HighlightPin( aPin );
218  }
219  else
220  aItem->SetFlags( HIGHLIGHTED );
221  }
222 
223  // ugly but I guess OK for the moment...
224  UpdateAllItems( ALL );
225 }
226 
227 }; // namespace KIGFX
228 
Definition of the SCH_SHEET class for Eeschema.
void Hide(VIEW_ITEM *aItem, bool aHide=true)
Temporarily hides the item in the view (e.g.
Definition: view.cpp:1506
const wxString & GetFileName() const
Definition: sch_screen.h:131
int m_ScreenNumber
Definition: base_screen.h:216
void ClearHiddenFlags()
Clear the hide flag of all items in the view.
Definition: sch_view.cpp:179
void AddToPreview(EDA_ITEM *aItem, bool aTakeOwnership=true)
Definition: sch_view.cpp:145
void DisplayComponent(LIB_PART *aPart)
Definition: sch_view.cpp:116
Class CAIRO_GAL is the cairo implementation of the graphics abstraction layer.
Definition: class_module.h:57
SCH_ITEM * Next() const
int GetHeightMils() const
Definition: page_info.h:140
void ResizeSheetWorkingArea(SCH_SCREEN *aScreen)
Definition: sch_view.cpp:71
void UpdateAllItems(int aUpdateFlags)
Updates all items in the view according to the given flags.
Definition: view.cpp:1443
void ShowSelectionArea(bool aShow=true)
Definition: sch_view.cpp:159
void DisplaySheet(SCH_SHEET *aSheet)
Definition: sch_view.cpp:110
const TITLE_BLOCK & GetTitleBlock() const
Definition: sch_screen.h:136
void SetSize(const Vec &size)
Definition: box2.h:210
Item needs to be redrawn.
Definition: view_item.h:61
VECTOR2< int > VECTOR2I
Definition: vector2d.h:587
SCH_SCREEN * GetScreen()
Definition: sch_sheet.h:279
std::unique_ptr< KIGFX::PREVIEW::SELECTION_AREA > m_selectionArea
Definition: sch_view.h:109
const PAGE_INFO & GetPageSettings() const
Definition: sch_screen.h:126
void HighlightItem(EDA_ITEM *aItem, LIB_PIN *aPin=nullptr)
Definition: sch_view.cpp:191
WX_VIEW_CONTROLS class definition.
std::vector< EDA_ITEM * > m_ownedItems
Definition: sch_view.h:111
std::shared_ptr< std::vector< VIEW_ITEM * > > m_allItems
Flat list of all items.
Definition: view.h:826
Class that handles properties and drawing of worksheet layout.
#define TO_UTF8(wxstring)
Macro TO_UTF8 converts a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: macros.h:47
void HideWorksheet()
Definition: sch_view.cpp:186
void SetFlags(STATUS_FLAGS aMask)
Definition: base_struct.h:256
void Clear()
Function Clear() Removes all items from the view.
Definition: view.cpp:1110
LIB_ITEMS_CONTAINER & GetDrawItems()
Return a reference to the draw item list.
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 ShowPreview(bool aShow=true)
Definition: sch_view.cpp:173
Class LIB_ITEM definition.
void ClearPreview()
Definition: sch_view.cpp:133
Define a library symbol object.
VIEW_GROUP extends VIEW_ITEM by possibility of grouping items into a single object.
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:1539
#define MAX_PAGE_SIZE_EDITORS_MILS
Definition: page_info.h:41
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition: sch_sheet.h:209
Definitions for the Eeschema program SCH_SCREEN class.
Definition the SCH_COMPONENT class for Eeschema.
std::unique_ptr< WORKSHEET_VIEWITEM > m_worksheet
Definition: sch_view.h:108
#define HIGHLIGHTED
item is drawn in normal colors, when the rest is darkened
Definition: base_struct.h:137
Board layer functions and definitions.
bool IsType(FRAME_T aType) const
BOX2D m_boundary
View boundaries.
Definition: view.h:841
void SetOrigin(const Vec &pos)
Definition: box2.h:208
#define IU_PER_MILS
Definition: plotter.cpp:134
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:154
SCH_BASE_FRAME * m_frame
Definition: sch_view.h:106
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Sets the item visibility.
Definition: view.cpp:1485
SCH_VIEW(bool aIsDynamic, SCH_BASE_FRAME *aFrame)
Definition: sch_view.cpp:47
int m_NumberOfScreens
Definition: base_screen.h:217
Class SELECTION_AREA.
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Function Add() Adds a VIEW_ITEM to the view.
Definition: view.cpp:346
Class 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:110
int GetWidthMils() const
Definition: page_info.h:137
KICAD_T Type() const
Function Type()
Definition: base_struct.h:201
virtual wxString GetScreenDesc() const
SCH_ITEM * GetDrawItems() const
Definition: sch_screen.h:160