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, 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 <algorithm>
27#include <common.h>
28#include <layer_ids.h>
29#include <page_info.h>
35#include <eda_text.h>
37#include <project.h>
38#include <text_var_dependency.h>
39#include <view/view.h>
40
41using namespace KIGFX;
42
44 const PROJECT* aProject, const TITLE_BLOCK* aTitleBlock,
45 const std::map<wxString, wxString>* aProperties ) :
46 EDA_ITEM( NOT_USED ), // this item is never added to a BOARD so it needs no type
47 m_iuScale( aIuScale ),
48 m_titleBlock( aTitleBlock ),
49 m_pageInfo( aPageInfo ),
50 m_pageNumber( "1" ),
51 m_sheetCount( 1 ),
52 m_isFirstPage( false ),
53 m_project( aProject ),
54 m_properties( aProperties ),
57{
58}
59
60
62{
63 BOX2I bbox;
64
65 if( m_pageInfo )
66 {
67 bbox.SetOrigin( VECTOR2I( 0, 0 ) );
68 bbox.SetEnd( VECTOR2I( m_iuScale.MilsToIU( m_pageInfo->GetWidthMils() ),
69 m_iuScale.MilsToIU( m_pageInfo->GetHeightMils() ) ) );
70 }
71 else
72 {
73 bbox.SetMaximum();
74 }
75
76 return bbox;
77}
78
79
81 const std::map<wxString, wxString>* aProperties,
82 DS_DRAW_ITEM_LIST* aDrawList ) const
83{
84 RENDER_SETTINGS* settings = aView->GetPainter()->GetSettings();
85 wxString fileName( m_fileName.c_str(), wxConvUTF8 );
86 wxString sheetName( m_sheetName.c_str(), wxConvUTF8 );
87 wxString sheetPath( m_sheetPath.c_str(), wxConvUTF8 );
88 wxString variantName( m_variantName.c_str(), wxConvUTF8 );
89 wxString variantDesc( m_variantDesc.c_str(), wxConvUTF8 );
90
91 aDrawList->SetDefaultPenSize( (int) settings->GetDrawingSheetLineWidth() );
92 aDrawList->SetIsFirstPage( m_isFirstPage );
93 aDrawList->SetPageNumber( m_pageNumber );
94 aDrawList->SetSheetCount( m_sheetCount );
95 aDrawList->SetFileName( fileName );
96 aDrawList->SetSheetName( sheetName );
97 aDrawList->SetSheetPath( sheetPath );
98 aDrawList->SetSheetLayer( settings->GetLayerName() );
99 aDrawList->SetVariantName( variantName );
100 aDrawList->SetVariantDesc( variantDesc );
101 aDrawList->SetProject( m_project );
102 aDrawList->SetProperties( aProperties );
103
105}
106
107
108void DS_PROXY_VIEW_ITEM::ViewDraw( int aLayer, VIEW* aView ) const
109{
110 GAL* gal = aView->GetGAL();
111 RENDER_SETTINGS* settings = aView->GetPainter()->GetSettings();
112 DS_DRAW_ITEM_LIST drawList( m_iuScale );
113
114 buildDrawList( aView, m_properties, &drawList );
115
116 BOX2I viewport = BOX2ISafe( aView->GetViewport() );
117
118 // Draw the title block normally even if the view is flipped
119 bool flipped = gal->IsFlippedX();
120
121 if( flipped )
122 {
123 int pageWidth = m_iuScale.MilsToIU( m_pageInfo->GetWidthMils() );
124
125 gal->Save();
126 gal->Translate( VECTOR2D( pageWidth, 0 ) );
127 gal->Scale( VECTOR2D( -1.0, 1.0 ) );
128
129 int right = pageWidth - viewport.GetLeft();
130 int left = right - viewport.GetWidth();
131 viewport.SetOrigin( left, viewport.GetTop() );
132 }
133
134 DS_PAINTER ws_painter( gal );
135 auto ws_settings = static_cast<DS_RENDER_SETTINGS*>( ws_painter.GetSettings() );
136
137 ws_settings->SetNormalColor( settings->GetLayerColor( m_colorLayer ) );
138 ws_settings->SetSelectedColor( settings->GetLayerColor( LAYER_SELECT_OVERLAY ) );
139 ws_settings->SetBrightenedColor( settings->GetLayerColor( LAYER_BRIGHTENED ) );
140 ws_settings->SetPageBorderColor( settings->GetLayerColor( m_pageBorderColorLayer ) );
141 ws_settings->SetDefaultFont( settings->GetDefaultFont() );
142
143 // Draw all the components that make the drawing sheet
144 for( DS_DRAW_ITEM_BASE* item = drawList.GetFirst(); item; item = drawList.GetNext() )
145 {
146 if( viewport.Intersects( item->GetApproxBBox() ) )
147 ws_painter.Draw( item, LAYER_DRAWINGSHEET );
148 }
149
150 // Draw gray line that outlines the sheet size
151 if( settings->GetShowPageLimits() )
152 ws_painter.DrawBorder( m_pageInfo, m_iuScale.IU_PER_MILS );
153
154 if( flipped )
155 gal->Restore();
156}
157
158
159std::vector<int> DS_PROXY_VIEW_ITEM::ViewGetLayers() const
160{
161 std::vector<int> layer{ LAYER_DRAWINGSHEET };
162 return layer;
163}
164
165
166std::vector<TEXT_VAR_REF_KEY> DS_PROXY_VIEW_ITEM::CollectTextVarKeys() const
167{
168 // Scan the drawing-sheet *template* data model, not the resolved draw
169 // list: SyncDrawItems expands `${...}` against the current title block
170 // before populating DS_DRAW_ITEM_TEXT, so the references are lost by the
171 // time we'd see draw items. The template's m_TextBase preserves them.
172 std::vector<TEXT_VAR_REF_KEY> keys;
173
174 for( DS_DATA_ITEM* item : DS_DATA_MODEL::GetTheInstance().GetItems() )
175 {
176 if( item->GetType() != DS_DATA_ITEM::DS_TEXT )
177 continue;
178
179 const DS_DATA_ITEM_TEXT* textItem = static_cast<const DS_DATA_ITEM_TEXT*>( item );
180 std::vector<TEXT_VAR_REF_KEY> itemKeys = ExtractTextVarReferences( textItem->m_TextBase );
181
182 for( const TEXT_VAR_REF_KEY& ref : itemKeys )
183 {
184 if( ref.IsTrackable() )
185 keys.push_back( ref );
186 }
187 }
188
189 // De-duplicate — the same ${REVISION} reference may appear on multiple
190 // draw items; a single registration per unique key is enough.
191 std::sort( keys.begin(), keys.end(),
192 []( const TEXT_VAR_REF_KEY& a, const TEXT_VAR_REF_KEY& b )
193 {
194 if( a.kind != b.kind )
195 return static_cast<int>( a.kind ) < static_cast<int>( b.kind );
196
197 if( a.primary != b.primary )
198 return a.primary < b.primary;
199
200 return a.secondary < b.secondary;
201 } );
202
203 keys.erase( std::unique( keys.begin(), keys.end() ), keys.end() );
204
205 return keys;
206}
207
208
210{
211 // Detach path: unregister from any previously attached tracker.
212 if( !aTracker )
213 {
215 m_attachedTracker->UnregisterItem( this );
216
217 m_attachedTracker = nullptr;
218 return;
219 }
220
221 // Re-attach to a different tracker: unregister from the old one first.
222 if( m_attachedTracker && m_attachedTracker != aTracker )
223 m_attachedTracker->UnregisterItem( this );
224
225 m_attachedTracker = aTracker;
226 aTracker->RegisterItem( this, CollectTextVarKeys() );
227}
228
229
231{
233 m_attachedTracker->UnregisterItem( this );
234}
235
236
238{
239 int accuracy = (int) aView->ToWorld( 5.0 ); // five pixels at current zoom
240 DS_DRAW_ITEM_LIST drawList( m_iuScale );
241
242 buildDrawList( aView, m_properties, &drawList );
243
244 for( DS_DRAW_ITEM_BASE* item = drawList.GetFirst(); item; item = drawList.GetNext() )
245 {
246 if( item->HitTest( aPosition, accuracy ) )
247 return true;
248 }
249
250 return false;
251}
constexpr BOX2I BOX2ISafe(const BOX2D &aInput)
Definition box2.h:929
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
constexpr void SetMaximum()
Definition box2.h:80
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:237
constexpr size_type GetWidth() const
Definition box2.h:214
constexpr coord_type GetLeft() const
Definition box2.h:228
constexpr void SetEnd(coord_type x, coord_type y)
Definition box2.h:297
constexpr coord_type GetTop() const
Definition box2.h:229
constexpr bool Intersects(const BOX2< Vec > &aRect) const
Definition box2.h:311
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:41
Methods for painting drawing sheet items.
Definition ds_painter.h:98
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:110
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:46
void SetNormalColor(const COLOR4D &aColor)
Definition ds_painter.h:66
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:205
BOX2D GetViewport() const
Return the current viewport visible area rectangle.
Definition view.cpp:598
GAL * GetGAL() const
Return the GAL this view is using to draw graphical primitives.
Definition view.h:211
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:535
PAINTER * GetPainter() const
Return the painter object used by the view for drawing #VIEW_ITEMS.
Definition view.h:229
Describe the page size and margins of a paper page on which to eventually print or plot.
Definition page_info.h:79
Container for project specific data.
Definition project.h:66
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:41
std::vector< TEXT_VAR_REF_KEY > ExtractTextVarReferences(const wxString &aSource)
Lex-scan aSource and return every ${...} reference that appears, without resolving.
Definition common.cpp:435
The common library.
@ LAYER_PAGE_LIMITS
Color for drawing the page extents (visibility stored in PCBNEW_SETTINGS::m_ShowPageLimits)
Definition layer_ids.h:329
@ LAYER_DRAWINGSHEET
Sheet frame and title block.
Definition layer_ids.h:278
@ LAYER_SELECT_OVERLAY
Selected items overlay.
Definition layer_ids.h:280
@ LAYER_BRIGHTENED
Definition layer_ids.h:493
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:33
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:76
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687
VECTOR2< double > VECTOR2D
Definition vector2d.h:686