You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.5 KiB

  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. #include <richio.h>
  25. #include <common.h>
  26. #include <title_block.h>
  27. #include <core/kicad_algo.h>
  28. void TITLE_BLOCK::Format( OUTPUTFORMATTER* aFormatter, int aNestLevel, int aControlBits ) const
  29. {
  30. // Don't write the title block information if there is nothing to write.
  31. bool isempty = true;
  32. for( unsigned idx = 0; idx < m_tbTexts.GetCount(); idx++ )
  33. {
  34. if( ! m_tbTexts[idx].IsEmpty() )
  35. {
  36. isempty = false;
  37. break;
  38. }
  39. }
  40. if( !isempty )
  41. {
  42. aFormatter->Print( aNestLevel, "(title_block\n" );
  43. if( !GetTitle().IsEmpty() )
  44. aFormatter->Print( aNestLevel+1, "(title %s)\n",
  45. aFormatter->Quotew( GetTitle() ).c_str() );
  46. if( !GetDate().IsEmpty() )
  47. aFormatter->Print( aNestLevel+1, "(date %s)\n",
  48. aFormatter->Quotew( GetDate() ).c_str() );
  49. if( !GetRevision().IsEmpty() )
  50. aFormatter->Print( aNestLevel+1, "(rev %s)\n",
  51. aFormatter->Quotew( GetRevision() ).c_str() );
  52. if( !GetCompany().IsEmpty() )
  53. aFormatter->Print( aNestLevel+1, "(company %s)\n",
  54. aFormatter->Quotew( GetCompany() ).c_str() );
  55. for( int ii = 0; ii < 9; ii++ )
  56. {
  57. if( !GetComment(ii).IsEmpty() )
  58. aFormatter->Print( aNestLevel+1, "(comment %d %s)\n", ii+1,
  59. aFormatter->Quotew( GetComment(ii) ).c_str() );
  60. }
  61. aFormatter->Print( aNestLevel, ")\n\n" );
  62. }
  63. }
  64. void TITLE_BLOCK::GetContextualTextVars( wxArrayString* aVars )
  65. {
  66. if( !alg::contains( *aVars, wxT( "ISSUE_DATE" ) ) )
  67. {
  68. aVars->push_back( wxT( "ISSUE_DATE" ) );
  69. aVars->push_back( wxT( "CURRENT_DATE" ) );
  70. aVars->push_back( wxT( "REVISION" ) );
  71. aVars->push_back( wxT( "TITLE" ) );
  72. aVars->push_back( wxT( "COMPANY" ) );
  73. aVars->push_back( wxT( "COMMENT1" ) );
  74. aVars->push_back( wxT( "COMMENT2" ) );
  75. aVars->push_back( wxT( "COMMENT3" ) );
  76. aVars->push_back( wxT( "COMMENT4" ) );
  77. aVars->push_back( wxT( "COMMENT5" ) );
  78. aVars->push_back( wxT( "COMMENT6" ) );
  79. aVars->push_back( wxT( "COMMENT7" ) );
  80. aVars->push_back( wxT( "COMMENT8" ) );
  81. aVars->push_back( wxT( "COMMENT9" ) );
  82. }
  83. }
  84. bool TITLE_BLOCK::TextVarResolver( wxString* aToken, const PROJECT* aProject ) const
  85. {
  86. bool tokenUpdated = false;
  87. wxString originalToken = *aToken;
  88. auto getCurrentDate =
  89. []() -> wxString
  90. {
  91. // We can choose different formats. Should probably be kept in sync with ISSUE_DATE
  92. // formatting in DIALOG_PAGES_SETTINGS.
  93. //
  94. // return wxDateTime::Now().Format( wxLocale::GetInfo( wxLOCALE_SHORT_DATE_FMT ) );
  95. // return wxDateTime::Now().Format( wxLocale::GetInfo( wxLOCALE_LONG_DATE_FMT ) );
  96. // return wxDateTime::Now().Format( wxT("%Y-%b-%d") );
  97. return wxDateTime::Now().FormatISODate();
  98. };
  99. if( aToken->IsSameAs( wxT( "ISSUE_DATE" ) ) )
  100. {
  101. *aToken = GetDate();
  102. tokenUpdated = true;
  103. }
  104. else if( aToken->IsSameAs( wxT( "CURRENT_DATE" ) ) )
  105. {
  106. *aToken = getCurrentDate();
  107. tokenUpdated = true;
  108. }
  109. else if( aToken->IsSameAs( wxT( "REVISION" ) ) )
  110. {
  111. *aToken = GetRevision();
  112. tokenUpdated = true;
  113. }
  114. else if( aToken->IsSameAs( wxT( "TITLE" ) ) )
  115. {
  116. *aToken = GetTitle();
  117. tokenUpdated = true;
  118. }
  119. else if( aToken->IsSameAs( wxT( "COMPANY" ) ) )
  120. {
  121. *aToken = GetCompany();
  122. tokenUpdated = true;
  123. }
  124. else if( aToken->Left( aToken->Len() - 1 ).IsSameAs( wxT( "COMMENT" ) ) )
  125. {
  126. wxChar c = aToken->Last();
  127. switch( c )
  128. {
  129. case '1':
  130. case '2':
  131. case '3':
  132. case '4':
  133. case '5':
  134. case '6':
  135. case '7':
  136. case '8':
  137. case '9':
  138. *aToken = GetComment( c - '1' );
  139. tokenUpdated = true;
  140. }
  141. }
  142. if( tokenUpdated )
  143. {
  144. if( aToken->IsSameAs( wxT( "CURRENT_DATE" ) ) )
  145. *aToken = getCurrentDate();
  146. else if( aProject )
  147. *aToken = ExpandTextVars( *aToken, aProject );
  148. // This is the default fallback, so don't claim we resolved it
  149. if( *aToken == wxT( "${" ) + originalToken + wxT( "}" ) )
  150. return false;
  151. return true;
  152. }
  153. return false;
  154. }