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.

205 lines
7.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <sch_edit_frame.h>
  24. #include <widgets/msgpanel.h>
  25. #include <string_utils.h>
  26. #include <schematic.h>
  27. #include <sch_table.h>
  28. #include <sch_tablecell.h>
  29. using KIGFX::SCH_RENDER_SETTINGS;
  30. SCH_TABLECELL::SCH_TABLECELL( int aLineWidth, FILL_T aFillType ) :
  31. SCH_TEXTBOX( aLineWidth, aFillType, wxEmptyString, SCH_TABLECELL_T ),
  32. m_colSpan( 1 ),
  33. m_rowSpan( 1 )
  34. {
  35. }
  36. void SCH_TABLECELL::SwapData( SCH_ITEM* aItem )
  37. {
  38. SCH_TEXTBOX::SwapData( aItem );
  39. SCH_TABLECELL* cell = static_cast<SCH_TABLECELL*>( aItem );
  40. std::swap( m_colSpan, cell->m_colSpan );
  41. std::swap( m_rowSpan, cell->m_rowSpan );
  42. }
  43. wxString SCH_TABLECELL::GetItemDescription( UNITS_PROVIDER* aUnitsProvider ) const
  44. {
  45. return wxString::Format( _( "Table Cell %s" ), GetAddr() );
  46. }
  47. int SCH_TABLECELL::GetRow() const
  48. {
  49. const SCH_TABLE* table = static_cast<const SCH_TABLE*>( GetParent() );
  50. for( int row = 0; row < table->GetRowCount(); ++row )
  51. {
  52. for( int col = 0; col < table->GetColCount(); ++col )
  53. {
  54. if( table->GetCell( row, col ) == this )
  55. return row;
  56. }
  57. }
  58. return -1;
  59. }
  60. int SCH_TABLECELL::GetColumn() const
  61. {
  62. const SCH_TABLE* table = static_cast<const SCH_TABLE*>( GetParent() );
  63. for( int row = 0; row < table->GetRowCount(); ++row )
  64. {
  65. for( int col = 0; col < table->GetColCount(); ++col )
  66. {
  67. if( table->GetCell( row, col ) == this )
  68. return col;
  69. }
  70. }
  71. return -1;
  72. }
  73. wxString SCH_TABLECELL::GetAddr() const
  74. {
  75. return wxString::Format( wxT( "%c%d" ),
  76. 'A' + GetColumn() % 26,
  77. GetRow() + 1 );
  78. }
  79. void SCH_TABLECELL::Print( const RENDER_SETTINGS* aSettings, const VECTOR2I& aOffset )
  80. {
  81. if( m_colSpan >= 1 && m_rowSpan >= 1 )
  82. SCH_TEXTBOX::Print( aSettings, aOffset );
  83. }
  84. void SCH_TABLECELL::Plot( PLOTTER* aPlotter, bool aBackground,
  85. const SCH_PLOT_SETTINGS& aPlotSettings ) const
  86. {
  87. if( m_colSpan >= 1 && m_rowSpan >= 1 )
  88. SCH_TEXTBOX::Plot( aPlotter, aBackground, aPlotSettings );
  89. }
  90. void SCH_TABLECELL::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  91. {
  92. aList.emplace_back( _( "Table Cell" ), GetAddr() );
  93. // Don't use GetShownText() here; we want to show the user the variable references
  94. aList.emplace_back( _( "Text" ), KIUI::EllipsizeStatusText( aFrame, GetText() ) );
  95. aList.emplace_back( _( "Cell Width" ),
  96. aFrame->MessageTextFromValue( std::abs( GetEnd().x - GetStart().x ) ) );
  97. aList.emplace_back( _( "Cell Height" ),
  98. aFrame->MessageTextFromValue( std::abs( GetEnd().y - GetStart().y ) ) );
  99. aList.emplace_back( _( "Font" ), GetFont() ? GetFont()->GetName() : _( "Default" ) );
  100. wxString textStyle[] = { _( "Normal" ), _( "Italic" ), _( "Bold" ), _( "Bold Italic" ) };
  101. int style = IsBold() && IsItalic() ? 3 : IsBold() ? 2 : IsItalic() ? 1 : 0;
  102. aList.emplace_back( _( "Style" ), textStyle[style] );
  103. aList.emplace_back( _( "Text Size" ), aFrame->MessageTextFromValue( GetTextWidth() ) );
  104. }
  105. double SCH_TABLECELL::Similarity( const SCH_ITEM& aOtherItem ) const
  106. {
  107. if( aOtherItem.Type() != Type() )
  108. return 0.0;
  109. const SCH_TABLECELL& other = static_cast<const SCH_TABLECELL&>( aOtherItem );
  110. double similarity = 1.0;
  111. if( m_colSpan != other.m_colSpan )
  112. similarity *= 0.9;
  113. if( m_rowSpan != other.m_rowSpan )
  114. similarity *= 0.9;
  115. similarity *= SCH_TEXTBOX::Similarity( other );
  116. return similarity;
  117. }
  118. bool SCH_TABLECELL::operator==( const SCH_ITEM& aOtherItem ) const
  119. {
  120. if( aOtherItem.Type() != Type() )
  121. return false;
  122. const SCH_TABLECELL& other = static_cast<const SCH_TABLECELL&>( aOtherItem );
  123. return m_colSpan == other.m_colSpan
  124. && m_rowSpan == other.m_rowSpan
  125. && SCH_TEXTBOX::operator==( other );
  126. }
  127. static struct SCH_TABLECELL_DESC
  128. {
  129. SCH_TABLECELL_DESC()
  130. {
  131. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  132. REGISTER_TYPE( SCH_TABLECELL );
  133. propMgr.AddTypeCast( new TYPE_CAST<SCH_TABLECELL, SCH_TEXTBOX> );
  134. propMgr.AddTypeCast( new TYPE_CAST<SCH_TABLECELL, SCH_SHAPE> );
  135. propMgr.AddTypeCast( new TYPE_CAST<SCH_TABLECELL, EDA_SHAPE> );
  136. propMgr.AddTypeCast( new TYPE_CAST<SCH_TABLECELL, EDA_TEXT> );
  137. propMgr.InheritsAfter( TYPE_HASH( SCH_TABLECELL ), TYPE_HASH( SCH_TEXTBOX ) );
  138. propMgr.InheritsAfter( TYPE_HASH( SCH_TABLECELL ), TYPE_HASH( SCH_SHAPE ) );
  139. propMgr.InheritsAfter( TYPE_HASH( SCH_TABLECELL ), TYPE_HASH( EDA_SHAPE ) );
  140. propMgr.InheritsAfter( TYPE_HASH( SCH_TABLECELL ), TYPE_HASH( EDA_TEXT ) );
  141. propMgr.Mask( TYPE_HASH( SCH_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "Start X" ) );
  142. propMgr.Mask( TYPE_HASH( SCH_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "Start Y" ) );
  143. propMgr.Mask( TYPE_HASH( SCH_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "End X" ) );
  144. propMgr.Mask( TYPE_HASH( SCH_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "End Y" ) );
  145. propMgr.Mask( TYPE_HASH( SCH_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "Shape" ) );
  146. propMgr.Mask( TYPE_HASH( SCH_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "Line Width" ) );
  147. propMgr.Mask( TYPE_HASH( SCH_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "Line Style" ) );
  148. propMgr.Mask( TYPE_HASH( SCH_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "Line Color" ) );
  149. propMgr.Mask( TYPE_HASH( SCH_TABLECELL ), TYPE_HASH( EDA_TEXT ), _HKI( "Width" ) );
  150. propMgr.Mask( TYPE_HASH( SCH_TABLECELL ), TYPE_HASH( EDA_TEXT ), _HKI( "Height" ) );
  151. propMgr.Mask( TYPE_HASH( SCH_TABLECELL ), TYPE_HASH( EDA_TEXT ), _HKI( "Thickness" ) );
  152. propMgr.Mask( TYPE_HASH( SCH_TABLECELL ), TYPE_HASH( EDA_TEXT ), _HKI( "Orientation" ) );
  153. propMgr.Mask( TYPE_HASH( SCH_TABLECELL ), TYPE_HASH( EDA_TEXT ), _HKI( "Hyperlink" ) );
  154. }
  155. } _SCH_TABLECELL_DESC;