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, int aNestLevel, int aControlBits ) 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( aNestLevel, "(title_block\n" );
46
47 if( !GetTitle().IsEmpty() )
48 aFormatter->Print( aNestLevel+1, "(title %s)\n",
49 aFormatter->Quotew( GetTitle() ).c_str() );
50
51 if( !GetDate().IsEmpty() )
52 aFormatter->Print( aNestLevel+1, "(date %s)\n",
53 aFormatter->Quotew( GetDate() ).c_str() );
54
55 if( !GetRevision().IsEmpty() )
56 aFormatter->Print( aNestLevel+1, "(rev %s)\n",
57 aFormatter->Quotew( GetRevision() ).c_str() );
58
59 if( !GetCompany().IsEmpty() )
60 aFormatter->Print( aNestLevel+1, "(company %s)\n",
61 aFormatter->Quotew( GetCompany() ).c_str() );
62
63 for( int ii = 0; ii < 9; ii++ )
64 {
65 if( !GetComment(ii).IsEmpty() )
66 aFormatter->Print( aNestLevel+1, "(comment %d %s)\n", ii+1,
67 aFormatter->Quotew( GetComment(ii) ).c_str() );
68 }
69
70 aFormatter->Print( aNestLevel, ")\n\n" );
71 }
72}
73
74void TITLE_BLOCK::GetContextualTextVars( wxArrayString* aVars )
75{
76 if( !alg::contains( *aVars, wxT( "ISSUE_DATE" ) ) )
77 {
78 aVars->push_back( wxT( "ISSUE_DATE" ) );
79 aVars->push_back( wxT( "CURRENT_DATE" ) );
80 aVars->push_back( wxT( "REVISION" ) );
81 aVars->push_back( wxT( "TITLE" ) );
82 aVars->push_back( wxT( "COMPANY" ) );
83 aVars->push_back( wxT( "COMMENT1" ) );
84 aVars->push_back( wxT( "COMMENT2" ) );
85 aVars->push_back( wxT( "COMMENT3" ) );
86 aVars->push_back( wxT( "COMMENT4" ) );
87 aVars->push_back( wxT( "COMMENT5" ) );
88 aVars->push_back( wxT( "COMMENT6" ) );
89 aVars->push_back( wxT( "COMMENT7" ) );
90 aVars->push_back( wxT( "COMMENT8" ) );
91 aVars->push_back( wxT( "COMMENT9" ) );
92 }
93}
94
95
96bool TITLE_BLOCK::TextVarResolver( wxString* aToken, const PROJECT* aProject ) const
97{
98 bool tokenUpdated = false;
99 wxString originalToken = *aToken;
100
101 auto getCurrentDate =
102 []() -> wxString
103 {
104 // We can choose different formats. Should probably be kept in sync with ISSUE_DATE
105 // formatting in DIALOG_PAGES_SETTINGS.
106 //
107 // return wxDateTime::Now().Format( wxLocale::GetInfo( wxLOCALE_SHORT_DATE_FMT ) );
108 // return wxDateTime::Now().Format( wxLocale::GetInfo( wxLOCALE_LONG_DATE_FMT ) );
109 // return wxDateTime::Now().Format( wxT("%Y-%b-%d") );
110 return wxDateTime::Now().FormatISODate();
111 };
112
113 if( aToken->IsSameAs( wxT( "ISSUE_DATE" ) ) )
114 {
115 *aToken = GetDate();
116 tokenUpdated = true;
117 }
118 else if( aToken->IsSameAs( wxT( "CURRENT_DATE" ) ) )
119 {
120 *aToken = getCurrentDate();
121 tokenUpdated = true;
122 }
123 else if( aToken->IsSameAs( wxT( "REVISION" ) ) )
124 {
125 *aToken = GetRevision();
126 tokenUpdated = true;
127 }
128 else if( aToken->IsSameAs( wxT( "TITLE" ) ) )
129 {
130 *aToken = GetTitle();
131 tokenUpdated = true;
132 }
133 else if( aToken->IsSameAs( wxT( "COMPANY" ) ) )
134 {
135 *aToken = GetCompany();
136 tokenUpdated = true;
137 }
138 else if( aToken->Left( aToken->Len() - 1 ).IsSameAs( wxT( "COMMENT" ) ) )
139 {
140 wxChar c = aToken->Last();
141
142 switch( c )
143 {
144 case '1':
145 case '2':
146 case '3':
147 case '4':
148 case '5':
149 case '6':
150 case '7':
151 case '8':
152 case '9':
153 *aToken = GetComment( c - '1' );
154 tokenUpdated = true;
155 }
156 }
157
158 if( tokenUpdated )
159 {
160 if( aToken->IsSameAs( wxT( "CURRENT_DATE" ) ) )
161 *aToken = getCurrentDate();
162 else if( aProject )
163 *aToken = ExpandTextVars( *aToken, aProject );
164
165 // This is the default fallback, so don't claim we resolved it
166 if( *aToken == wxT( "${" ) + originalToken + wxT( "}" ) )
167 return false;
168
169 return true;
170 }
171
172 return false;
173}
174
175
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:526
int PRINTF_FUNC 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:62
const wxString & GetCompany() const
Definition: title_block.h:96
bool TextVarResolver(wxString *aToken, const PROJECT *aProject) const
Definition: title_block.cpp:96
virtual void Format(OUTPUTFORMATTER *aFormatter, int aNestLevel, int aControlBits) const
Output the object to aFormatter in s-expression form.
Definition: title_block.cpp:30
const wxString & GetRevision() const
Definition: title_block.h:86
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:132
const wxString & GetTitle() const
Definition: title_block.h:63
static void GetContextualTextVars(wxArrayString *aVars)
Definition: title_block.cpp:74
wxString ExpandTextVars(const wxString &aSource, const PROJECT *aProject)
Definition: common.cpp:58
The common library.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100