KiCad PCB EDA Suite
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-2021 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( "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 ) const
96{
97 bool tokenUpdated = false;
98 wxString originalToken = *aToken;
99
100 if( aToken->IsSameAs( wxT( "ISSUE_DATE" ) ) )
101 {
102 *aToken = GetDate();
103 tokenUpdated = true;
104 }
105 else if( aToken->IsSameAs( wxT( "CURRENT_DATE" ) ) )
106 {
107 // We can choose different formats. Should probably be kept in sync with ISSUE_DATE
108 // formatting in DIALOG_PAGES_SETTINGS.
109 //
110 // *aToken = wxDateTime::Now().Format( wxLocale::GetInfo( wxLOCALE_SHORT_DATE_FMT ) );
111 // *aToken = wxDateTime::Now().Format( wxLocale::GetInfo( wxLOCALE_LONG_DATE_FMT ) );
112 // *aToken = wxDateTime::Now().Format( wxT("%Y-%b-%d") );
113 *aToken = wxDateTime::Now().FormatISODate();
114
115 tokenUpdated = true;
116 }
117 else if( aToken->IsSameAs( wxT( "REVISION" ) ) )
118 {
119 *aToken = GetRevision();
120 tokenUpdated = true;
121 }
122 else if( aToken->IsSameAs( wxT( "TITLE" ) ) )
123 {
124 *aToken = GetTitle();
125 tokenUpdated = true;
126 }
127 else if( aToken->IsSameAs( wxT( "COMPANY" ) ) )
128 {
129 *aToken = GetCompany();
130 tokenUpdated = true;
131 }
132 else if( aToken->Left( aToken->Len() - 1 ).IsSameAs( wxT( "COMMENT" ) ) )
133 {
134 wxChar c = aToken->Last();
135
136 switch( c )
137 {
138 case '1':
139 case '2':
140 case '3':
141 case '4':
142 case '5':
143 case '6':
144 case '7':
145 case '8':
146 case '9':
147 *aToken = GetComment( c - '1' );
148 tokenUpdated = true;
149 }
150 }
151
152 if( tokenUpdated )
153 {
154 *aToken = ExpandTextVars( *aToken, aProject );
155
156 // This is the default fallback, so don't claim we resolved it
157 if( *aToken == wxT( "${" ) + originalToken + wxT( "}" ) )
158 return false;
159
160 return true;
161 }
162
163 return false;
164}
165
166
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:310
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:499
int PRINTF_FUNC Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:431
Container for project specific data.
Definition: project.h:63
const wxString & GetCompany() const
Definition: title_block.h:96
bool TextVarResolver(wxString *aToken, const PROJECT *aProject) const
Definition: title_block.cpp:95
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:57
The common library.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:99