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-2021 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 <core/typeinfo.h>
29 #include <memory>
30 #include <view/view.h>
31 #include <view/view_group.h>
32 #include <view/view_rtree.h>
33 #include <view/wx_view_controls.h>
34 #include <drawing_sheet/ds_proxy_view_item.h>
35 #include <layer_ids.h>
36 #include <sch_screen.h>
37 #include <schematic.h>
38 #include <sch_base_frame.h>
39 
40 #include "sch_view.h"
41 
42 
43 namespace KIGFX {
44 
45 
SCH_VIEW(bool aIsDynamic,SCH_BASE_FRAME * aFrame)46 SCH_VIEW::SCH_VIEW( bool aIsDynamic, SCH_BASE_FRAME* aFrame ) :
47     VIEW( aIsDynamic )
48 {
49     m_frame = aFrame;
50     // Set m_boundary to define the max working area size. The default value is acceptable for
51     // Pcbnew and Gerbview, but too large for Eeschema due to very different internal units.
52     // A full size = 3 * MAX_PAGE_SIZE_MILS size allows a wide margin around the drawing-sheet.
53     double max_size = Mils2iu( MAX_PAGE_SIZE_MILS ) * 3.0;
54     m_boundary.SetOrigin( -max_size/4, -max_size/4 );
55     m_boundary.SetSize( max_size, max_size );
56 }
57 
58 
~SCH_VIEW()59 SCH_VIEW::~SCH_VIEW()
60 {
61 }
62 
63 
Cleanup()64 void SCH_VIEW::Cleanup()
65 {
66     Clear();
67     m_drawingSheet.reset();
68     m_preview.reset();
69 }
70 
71 
SetScale(double aScale,VECTOR2D aAnchor)72 void SCH_VIEW::SetScale( double aScale, VECTOR2D aAnchor )
73 {
74     VIEW::SetScale( aScale, aAnchor );
75 
76     //Redraw selection halos since their width is dependent on zoom
77     if( m_frame )
78         m_frame->RefreshSelection();
79 }
80 
81 
ResizeSheetWorkingArea(const SCH_SCREEN * aScreen)82 void SCH_VIEW::ResizeSheetWorkingArea( const SCH_SCREEN* aScreen )
83 {
84     const PAGE_INFO& page_info = aScreen->GetPageSettings();
85     double max_size_x = page_info.GetWidthIU() * 3.0;
86     double max_size_y = page_info.GetHeightIU() * 3.0;
87     m_boundary.SetOrigin( -max_size_x / 4, -max_size_y / 4 );
88     m_boundary.SetSize( max_size_x, max_size_y );
89 }
90 
91 
DisplaySheet(const SCH_SCREEN * aScreen)92 void SCH_VIEW::DisplaySheet( const SCH_SCREEN *aScreen )
93 {
94     for( SCH_ITEM* item : aScreen->Items() )
95         Add( item );
96 
97     m_drawingSheet.reset( new DS_PROXY_VIEW_ITEM( static_cast<int>( IU_PER_MILS ),
98                                                   &aScreen->GetPageSettings(),
99                                                   &aScreen->Schematic()->Prj(),
100                                                   &aScreen->GetTitleBlock() ) );
101     m_drawingSheet->SetPageNumber( TO_UTF8( aScreen->GetPageNumber() ) );
102     m_drawingSheet->SetSheetCount( aScreen->GetPageCount() );
103     m_drawingSheet->SetFileName( TO_UTF8( aScreen->GetFileName() ) );
104     m_drawingSheet->SetColorLayer( LAYER_SCHEMATIC_DRAWINGSHEET );
105     m_drawingSheet->SetPageBorderColorLayer( LAYER_SCHEMATIC_GRID );
106     m_drawingSheet->SetIsFirstPage( aScreen->GetVirtualPageNumber() == 1 );
107 
108     if( m_frame && m_frame->IsType( FRAME_SCH ) )
109         m_drawingSheet->SetSheetName( TO_UTF8( m_frame->GetScreenDesc() ) );
110     else
111         m_drawingSheet->SetSheetName( "" );
112 
113     ResizeSheetWorkingArea( aScreen );
114 
115     Add( m_drawingSheet.get() );
116 
117     InitPreview();
118 }
119 
120 
DisplaySymbol(LIB_SYMBOL * aSymbol)121 void SCH_VIEW::DisplaySymbol( LIB_SYMBOL* aSymbol )
122 {
123     Clear();
124 
125     if( !aSymbol )
126         return;
127 
128     std::shared_ptr< LIB_SYMBOL > parent;
129     LIB_SYMBOL* drawnSymbol = aSymbol;
130 
131     // Draw the mandatory fields for aliases and parent symbols.
132     for( LIB_ITEM& item : aSymbol->GetDrawItems() )
133     {
134         if( item.Type() != LIB_FIELD_T )
135             continue;
136 
137         LIB_FIELD* field = static_cast< LIB_FIELD* >( &item );
138 
139         wxCHECK2( field, continue );
140 
141         if( field->GetText().IsEmpty() )
142             continue;
143 
144         Add( &item );
145     }
146 
147     // Draw the parent items if the symbol is inherited from another symbol.
148     if( aSymbol->IsAlias() )
149     {
150         parent = aSymbol->GetParent().lock();
151 
152         wxCHECK( parent, /* void */ );
153 
154         drawnSymbol = parent.get();
155     }
156 
157     for( LIB_ITEM& item : drawnSymbol->GetDrawItems() )
158     {
159         // Don't show parent symbol fields.  Users may be confused by shown fields that can not
160         // be edited.
161         if( aSymbol->IsAlias() && item.Type() == LIB_FIELD_T )
162             continue;
163 
164         Add( &item );
165     }
166 
167     InitPreview();
168 }
169 
170 
ClearHiddenFlags()171 void SCH_VIEW::ClearHiddenFlags()
172 {
173     for( auto item : *m_allItems )
174         Hide( item, false );
175 }
176 
177 
HideDrawingSheet()178 void SCH_VIEW::HideDrawingSheet()
179 {
180     //    SetVisible( m_drawingSheet.get(), false );
181 }
182 
183 
184 }; // namespace KIGFX
185