KiCad PCB EDA Suite
Loading...
Searching...
No Matches
title_block.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) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <richio.h>
26#include <common.h>
27#include <title_block.h>
28#include <core/kicad_algo.h>
29
30void TITLE_BLOCK::Format( OUTPUTFORMATTER* aFormatter ) const
31{
32 // Don't write the title block information if there is nothing to write.
33 bool isempty = true;
34 for( unsigned idx = 0; idx < m_tbTexts.GetCount(); idx++ )
35 {
36 if( ! m_tbTexts[idx].IsEmpty() )
37 {
38 isempty = false;
39 break;
40 }
41 }
42
43 if( !isempty )
44 {
45 aFormatter->Print( "(title_block" );
46
47 if( !GetTitle().IsEmpty() )
48 aFormatter->Print( "(title %s)", aFormatter->Quotew( GetTitle() ).c_str() );
49
50 if( !GetDate().IsEmpty() )
51 aFormatter->Print( "(date %s)", aFormatter->Quotew( GetDate() ).c_str() );
52
53 if( !GetRevision().IsEmpty() )
54 aFormatter->Print( "(rev %s)", aFormatter->Quotew( GetRevision() ).c_str() );
55
56 if( !GetCompany().IsEmpty() )
57 aFormatter->Print( "(company %s)", aFormatter->Quotew( GetCompany() ).c_str() );
58
59 for( int ii = 0; ii < 9; ii++ )
60 {
61 if( !GetComment(ii).IsEmpty() )
62 {
63 aFormatter->Print( "(comment %d %s)",
64 ii+1,
65 aFormatter->Quotew( GetComment(ii) ).c_str() );
66 }
67 }
68
69 aFormatter->Print( ")" );
70 }
71}
72
73void TITLE_BLOCK::GetContextualTextVars( wxArrayString* aVars )
74{
75 if( !alg::contains( *aVars, wxT( "ISSUE_DATE" ) ) )
76 {
77 aVars->push_back( wxT( "ISSUE_DATE" ) );
78 aVars->push_back( wxT( "CURRENT_DATE" ) );
79 aVars->push_back( wxT( "REVISION" ) );
80 aVars->push_back( wxT( "TITLE" ) );
81 aVars->push_back( wxT( "COMPANY" ) );
82 aVars->push_back( wxT( "COMMENT1" ) );
83 aVars->push_back( wxT( "COMMENT2" ) );
84 aVars->push_back( wxT( "COMMENT3" ) );
85 aVars->push_back( wxT( "COMMENT4" ) );
86 aVars->push_back( wxT( "COMMENT5" ) );
87 aVars->push_back( wxT( "COMMENT6" ) );
88 aVars->push_back( wxT( "COMMENT7" ) );
89 aVars->push_back( wxT( "COMMENT8" ) );
90 aVars->push_back( wxT( "COMMENT9" ) );
91 }
92}
93
94
95bool TITLE_BLOCK::TextVarResolver( wxString* aToken, const PROJECT* aProject, int aFlags ) const
96{
97 bool tokenUpdated = false;
98 wxString originalToken = *aToken;
99
100 auto getCurrentDate =
101 []() -> wxString
102 {
103 // We can choose different formats. Should probably be kept in sync with ISSUE_DATE
104 // formatting in DIALOG_PAGES_SETTINGS.
105 //
106 // return wxDateTime::Now().Format( wxLocale::GetInfo( wxLOCALE_SHORT_DATE_FMT ) );
107 // return wxDateTime::Now().Format( wxLocale::GetInfo( wxLOCALE_LONG_DATE_FMT ) );
108 // return wxDateTime::Now().Format( wxT("%Y-%b-%d") );
109 return wxDateTime::Now().FormatISODate();
110 };
111
112 if( aToken->IsSameAs( wxT( "ISSUE_DATE" ) ) )
113 {
114 *aToken = GetDate();
115 tokenUpdated = true;
116 }
117 else if( aToken->IsSameAs( wxT( "CURRENT_DATE" ) ) )
118 {
119 *aToken = getCurrentDate();
120 tokenUpdated = true;
121 }
122 else if( aToken->IsSameAs( wxT( "REVISION" ) ) )
123 {
124 *aToken = GetRevision();
125 tokenUpdated = true;
126 }
127 else if( aToken->IsSameAs( wxT( "TITLE" ) ) )
128 {
129 *aToken = GetTitle();
130 tokenUpdated = true;
131 }
132 else if( aToken->IsSameAs( wxT( "COMPANY" ) ) )
133 {
134 *aToken = GetCompany();
135 tokenUpdated = true;
136 }
137 else if( aToken->Left( aToken->Len() - 1 ).IsSameAs( wxT( "COMMENT" ) ) )
138 {
139 wxChar c = aToken->Last();
140
141 switch( c )
142 {
143 case '1':
144 case '2':
145 case '3':
146 case '4':
147 case '5':
148 case '6':
149 case '7':
150 case '8':
151 case '9':
152 *aToken = GetComment( c - '1' );
153 tokenUpdated = true;
154 }
155 }
156
157 if( tokenUpdated )
158 {
159 if( aToken->IsSameAs( wxT( "CURRENT_DATE" ) ) )
160 *aToken = getCurrentDate();
161 else if( aProject )
162 *aToken = ExpandTextVars( *aToken, aProject, aFlags );
163
164 // This is the default fallback, so don't claim we resolved it
165 if( *aToken == wxT( "${" ) + originalToken + wxT( "}" ) )
166 return false;
167
168 return true;
169 }
170
171 return false;
172}
173
174
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:322
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:543
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:458
Container for project specific data.
Definition: project.h:64
const wxString & GetCompany() const
Definition: title_block.h:96
const wxString & GetRevision() const
Definition: title_block.h:86
bool TextVarResolver(wxString *aToken, const PROJECT *aProject, int aFlags=0) const
Definition: title_block.cpp:95
const wxString & GetDate() const
Definition: title_block.h:76
const wxString & GetComment(int aIdx) const
Definition: title_block.h:107
wxArrayString m_tbTexts
Definition: title_block.h:130
const wxString & GetTitle() const
Definition: title_block.h:63
virtual void Format(OUTPUTFORMATTER *aFormatter) const
Output the object to aFormatter in s-expression form.
Definition: title_block.cpp:30
static void GetContextualTextVars(wxArrayString *aVars)
Definition: title_block.cpp:73
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject, int aFlags)
Definition: common.cpp:59
The common library.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100