KiCad PCB EDA Suite
Loading...
Searching...
No Matches
ds_proxy_view_item.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-2020 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
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, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <algorithm>
23#include <common.h>
24#include <layer_ids.h>
25#include <page_info.h>
31#include <eda_text.h>
33#include <project.h>
34#include <text_var_dependency.h>
35#include <view/view.h>
36
37using namespace KIGFX;
38
40 const PROJECT* aProject, const TITLE_BLOCK* aTitleBlock,
41 const std::map<wxString, wxString>* aProperties ) :
42 EDA_ITEM( NOT_USED ), // this item is never added to a BOARD so it needs no type
43 m_iuScale( aIuScale ),
44 m_titleBlock( aTitleBlock ),
45 m_pageInfo( aPageInfo ),
46 m_pageNumber( "1" ),
47 m_sheetCount( 1 ),
48 m_isFirstPage( false ),
49 m_project( aProject ),
50 m_properties( aProperties ),
53{
54}
55
56
58{
59 BOX2I bbox;
60
61 if( m_pageInfo )
62 {
63 bbox.SetOrigin( VECTOR2I( 0, 0 ) );
64 bbox.SetEnd( VECTOR2I( m_iuScale.MilsToIU( m_pageInfo->GetWidthMils() ),
65 m_iuScale.MilsToIU( m_pageInfo->GetHeightMils() ) ) );
66 }
67 else
68 {
69 bbox.SetMaximum();
70 }
71
72 return bbox;
73}
74
75
77 const std::map<wxString, wxString>* aProperties,
78 DS_DRAW_ITEM_LIST* aDrawList ) const
79{
80 RENDER_SETTINGS* settings = aView->GetPainter()->GetSettings();
81 wxString fileName( m_fileName.c_str(), wxConvUTF8 );
82 wxString sheetName( m_sheetName.c_str(), wxConvUTF8 );
83 wxString sheetPath( m_sheetPath.c_str(), wxConvUTF8 );
84 wxString variantName( m_variantName.c_str(), wxConvUTF8 );
85 wxString variantDesc( m_variantDesc.c_str(), wxConvUTF8 );
86
87 aDrawList->SetDefaultPenSize( (int) settings->GetDrawingSheetLineWidth() );
88 aDrawList->SetIsFirstPage( m_isFirstPage );
89 aDrawList->SetPageNumber( m_pageNumber );
90 aDrawList->SetSheetCount( m_sheetCount );
91 aDrawList->SetFileName( fileName );
92 aDrawList->SetSheetName( sheetName );
93 aDrawList->SetSheetPath( sheetPath );
94 aDrawList->SetSheetLayer( settings->GetLayerName() );
95 aDrawList->SetVariantName( variantName );
96 aDrawList->SetVariantDesc( variantDesc );
97 aDrawList->SetProject( m_project );
98 aDrawList->SetProperties( aProperties );
99
101}
102
103
104void DS_PROXY_VIEW_ITEM::ViewDraw( int aLayer, VIEW* aView ) const
105{
106 GAL* gal = aView->GetGAL();
107 RENDER_SETTINGS* settings = aView->GetPainter()->GetSettings();
108 DS_DRAW_ITEM_LIST drawList( m_iuScale );
109
110 buildDrawList( aView, m_properties, &drawList );
111
112 BOX2I viewport = BOX2ISafe( aView->GetViewport() );
113
114 // Draw the title block normally even if the view is flipped
115 bool flipped = gal->IsFlippedX();
116
117 if( flipped )
118 {
119 int pageWidth = m_iuScale.MilsToIU( m_pageInfo->GetWidthMils() );
120
121 gal->Save();
122 gal->Translate( VECTOR2D( pageWidth, 0 ) );
123 gal->Scale( VECTOR2D( -1.0, 1.0 ) );
124
125 int right = pageWidth - viewport.GetLeft();
126 int left = right - viewport.GetWidth();
127 viewport.SetOrigin( left, viewport.GetTop() );
128 }
129
130 DS_PAINTER ws_painter( gal );
131 auto ws_settings = static_cast<DS_RENDER_SETTINGS*>( ws_painter.GetSettings() );
132
133 ws_settings->SetNormalColor( settings->GetLayerColor( m_colorLayer ) );
134 ws_settings->SetSelectedColor( settings->GetLayerColor( LAYER_SELECT_OVERLAY ) );
135 ws_settings->SetBrightenedColor( settings->GetLayerColor( LAYER_BRIGHTENED ) );
136 ws_settings->SetPageBorderColor( settings->GetLayerColor( m_pageBorderColorLayer ) );
137 ws_settings->SetDefaultFont( settings->GetDefaultFont() );
138
139 // Draw all the components that make the drawing sheet
140 for( DS_DRAW_ITEM_BASE* item = drawList.GetFirst(); item; item = drawList.GetNext() )
141 {
142 if( viewport.Intersects( item->GetApproxBBox() ) )
143 ws_painter.Draw( item, LAYER_DRAWINGSHEET );
144 }
145
146 // Draw gray line that outlines the sheet size
147 if( settings->GetShowPageLimits() )
148 ws_painter.DrawBorder( m_pageInfo, m_iuScale.IU_PER_MILS );
149
150 if( flipped )
151 gal->Restore();
152}
153
154
155std::vector<int> DS_PROXY_VIEW_ITEM::ViewGetLayers() const
156{
157 std::vector<int> layer{ LAYER_DRAWINGSHEET };
158 return layer;
159}
160
161
162std::vector<TEXT_VAR_REF_KEY> DS_PROXY_VIEW_ITEM::CollectTextVarKeys() const
163{
164 // Scan the drawing-sheet *template* data model, not the resolved draw
165 // list: SyncDrawItems expands `${...}` against the current title block
166 // before populating DS_DRAW_ITEM_TEXT, so the references are lost by the
167 // time we'd see draw items. The template's m_TextBase preserves them.
168 std::vector<TEXT_VAR_REF_KEY> keys;
169
170 for( DS_DATA_ITEM* item : DS_DATA_MODEL::GetTheInstance().GetItems() )
171 {
172 if( item->GetType() != DS_DATA_ITEM::DS_TEXT )
173 continue;
174
175 const DS_DATA_ITEM_TEXT* textItem = static_cast<const DS_DATA_ITEM_TEXT*>( item );
176 std::vector<TEXT_VAR_REF_KEY> itemKeys = ExtractTextVarReferences( textItem->m_TextBase );
177
178 for( const TEXT_VAR_REF_KEY& ref : itemKeys )
179 {
180 if( ref.IsTrackable() )
181 keys.push_back( ref );
182 }
183 }
184
185 // De-duplicate — the same ${REVISION} reference may appear on multiple
186 // draw items; a single registration per unique key is enough.
187 std::sort( keys.begin(), keys.end(),
188 []( const TEXT_VAR_REF_KEY& a, const TEXT_VAR_REF_KEY& b )
189 {
190 if( a.kind != b.kind )
191 return static_cast<int>( a.kind ) < static_cast<int>( b.kind );
192
193 if( a.primary != b.primary )
194 return a.primary < b.primary;
195
196 return a.secondary < b.secondary;
197 } );
198
199 keys.erase( std::unique( keys.begin(), keys.end() ), keys.end() );
200
201 return keys;
202}
203
204
206{
207 // Detach path: unregister from any previously attached tracker.
208 if( !aTracker )
209 {
211 m_attachedTracker->UnregisterItem( this );
212
213 m_attachedTracker = nullptr;
214 return;
215 }
216
217 // Re-attach to a different tracker: unregister from the old one first.
218 if( m_attachedTracker && m_attachedTracker != aTracker )
219 m_attachedTracker->UnregisterItem( this );
220
221 m_attachedTracker = aTracker;
222 aTracker->RegisterItem( this, CollectTextVarKeys() );
223}
224
225
227{
229 m_attachedTracker->UnregisterItem( this );
230}
231
232
234{
235 int accuracy = (int) aView->ToWorld( 5.0 ); // five pixels at current zoom
236 DS_DRAW_ITEM_LIST drawList( m_iuScale );
237
238 buildDrawList( aView, m_properties, &drawList );
239
240 for( DS_DRAW_ITEM_BASE* item = drawList.GetFirst(); item; item = drawList.GetNext() )
241 {
242 if( item->HitTest( aPosition, accuracy ) )
243 return true;
244 }
245
246 return false;
247}
constexpr BOX2I BOX2ISafe(const BOX2D &aInput)
Definition box2.h:925
BOX2< VECTOR2I > BOX2I
Definition box2.h:918
constexpr void SetMaximum()
Definition box2.h:76
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:233
constexpr size_type GetWidth() const
Definition box2.h:210
constexpr coord_type GetLeft() const
Definition box2.h:224
constexpr void SetEnd(coord_type x, coord_type y)
Definition box2.h:293
constexpr coord_type GetTop() const
Definition box2.h:225
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition box2.h:307
Drawing sheet structure type definitions.
static DS_DATA_MODEL & GetTheInstance()
Return the instance of DS_DATA_MODEL used in the application.
Base class to handle basic graphic items.
Store the list of graphic items: rect, lines, polygons and texts to draw/plot the title block and fra...
DS_DRAW_ITEM_BASE * GetFirst()
void SetVariantName(const wxString &aVariant)
Set the current variant name and description to draw/plot.
void BuildDrawItemsList(const PAGE_INFO &aPageInfo, const TITLE_BLOCK &aTitleBlock)
Drawing or plot the drawing sheet.
void SetSheetPath(const wxString &aSheetPath)
Set the sheet path to draw/plot.
void SetFileName(const wxString &aFileName)
Set the filename to draw/plot.
void SetDefaultPenSize(int aPenSize)
void SetVariantDesc(const wxString &aDesc)
void SetSheetName(const wxString &aSheetName)
Set the sheet name to draw/plot.
void SetIsFirstPage(bool aIsFirstPage)
Set if the page is the first page.
void SetProperties(const std::map< wxString, wxString > *aProps)
Set properties used for text variable resolution.
void SetSheetLayer(const wxString &aSheetLayer)
Set the sheet layer to draw/plot.
void SetSheetCount(int aSheetCount)
Set the value of the count of sheets, for basic inscriptions.
void SetPageNumber(const wxString &aPageNumber)
Set the value of the sheet number.
DS_DRAW_ITEM_BASE * GetNext()
void SetProject(const PROJECT *aProject)
const PROJECT * m_project
std::vector< int > ViewGetLayers() const override
int m_colorLayer
Layer that is used for drawing sheet color (LAYER_DRAWINGSHEET is always used for visibility).
const BOX2I ViewBBox() const override
const TITLE_BLOCK * m_titleBlock
const EDA_IU_SCALE & m_iuScale
const std::map< wxString, wxString > * m_properties
void AttachToTracker(TEXT_VAR_TRACKER *aTracker)
Register this proxy with aTracker as a dependent on every title-block source variable its current tem...
bool HitTestDrawingSheetItems(KIGFX::VIEW *aView, const VECTOR2I &aPosition)
TEXT_VAR_TRACKER * m_attachedTracker
Tracker this proxy is currently registered with (null if detached).
void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
DS_PROXY_VIEW_ITEM(const EDA_IU_SCALE &aIuScale, const PAGE_INFO *aPageInfo, const PROJECT *aProject, const TITLE_BLOCK *aTitleBlock, const std::map< wxString, wxString > *aProperties)
void buildDrawList(KIGFX::VIEW *aView, const std::map< wxString, wxString > *aProperties, DS_DRAW_ITEM_LIST *aDrawList) const
const PAGE_INFO * m_pageInfo
int m_pageBorderColorLayer
Layer that is used for page border color.
std::vector< TEXT_VAR_REF_KEY > CollectTextVarKeys() const
Walk the current drawing-sheet definition and collect every ${...} reference encountered in its text ...
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:37
Methods for painting drawing sheet items.
Definition ds_painter.h:94
void DrawBorder(const PAGE_INFO *aPageInfo, int aScaleFactor) const
virtual RENDER_SETTINGS * GetSettings() override
Return a pointer to current settings that are going to be used when drawing items.
Definition ds_painter.h:106
virtual bool Draw(const VIEW_ITEM *, int) override
Takes an instance of VIEW_ITEM and passes it to a function that knows how to draw the item.
Store page-layout-specific render settings.
Definition ds_painter.h:42
void SetNormalColor(const COLOR4D &aColor)
Definition ds_painter.h:62
Abstract interface for drawing on a 2D-surface.
virtual void Translate(const VECTOR2D &aTranslation)
Translate the context.
virtual void Restore()
Restore the context.
virtual void Scale(const VECTOR2D &aScale)
Scale the context.
virtual void Save()
Save the context.
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
float GetDrawingSheetLineWidth() const
const wxString & GetDefaultFont() const
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
const wxString & GetLayerName() const
virtual bool GetShowPageLimits() const
friend class VIEW
Definition view_item.h:201
BOX2D GetViewport() const
Return the current viewport visible area rectangle.
Definition view.cpp:597
GAL * GetGAL() const
Return the GAL this view is using to draw graphical primitives.
Definition view.h:207
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Converts a screen space point/vector to a point/vector in world space coordinates.
Definition view.cpp:534
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition view.h:225
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:75
Container for project specific data.
Definition project.h:62
Coordinates the dependency index with change notifications.
void RegisterItem(EDA_ITEM *aItem, const std::vector< TEXT_VAR_REF_KEY > &aKeys)
Register aItem in the index.
Hold the information shown in the lower right corner of a plot, printout, or editing view.
Definition title_block.h:37
std::vector< TEXT_VAR_REF_KEY > ExtractTextVarReferences(const wxString &aSource)
Lex-scan aSource and return every ${...} reference that appears, without resolving.
Definition common.cpp:431
The common library.
@ LAYER_PAGE_LIMITS
Color for drawing the page extents (visibility stored in PCBNEW_SETTINGS::m_ShowPageLimits)
Definition layer_ids.h:325
@ LAYER_DRAWINGSHEET
Sheet frame and title block.
Definition layer_ids.h:274
@ LAYER_SELECT_OVERLAY
Selected items overlay.
Definition layer_ids.h:276
@ LAYER_BRIGHTENED
Definition layer_ids.h:489
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:29
Identifies a single resolvable source that a text item's ${...} reference depends on.
const int accuracy
@ NOT_USED
the 3d code uses this value
Definition typeinfo.h:72
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682