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.

262 lines
9.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The 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 <pcb_edit_frame.h>
  24. #include <font/font.h>
  25. #include <widgets/msgpanel.h>
  26. #include <string_utils.h>
  27. #include <pcb_table.h>
  28. #include <pcb_tablecell.h>
  29. PCB_TABLECELL::PCB_TABLECELL( BOARD_ITEM* aParent ) :
  30. PCB_TEXTBOX( aParent, PCB_TABLECELL_T ),
  31. m_colSpan( 1 ),
  32. m_rowSpan( 1 )
  33. {
  34. if( IsBackLayer( aParent->GetLayer() ) )
  35. SetMirrored( true );
  36. SetRectangleHeight( std::numeric_limits<int>::max() / 2 );
  37. SetRectangleWidth( std::numeric_limits<int>::max() / 2 );
  38. }
  39. void PCB_TABLECELL::swapData( BOARD_ITEM* aImage )
  40. {
  41. wxASSERT( aImage->Type() == PCB_TABLECELL_T );
  42. std::swap( *((PCB_TABLECELL*) this), *((PCB_TABLECELL*) aImage) );
  43. }
  44. wxString PCB_TABLECELL::GetItemDescription( UNITS_PROVIDER* aUnitsProvider, bool aFull ) const
  45. {
  46. return wxString::Format( _( "Table Cell %s" ), GetAddr() );
  47. }
  48. int PCB_TABLECELL::GetRow() const
  49. {
  50. const PCB_TABLE* table = static_cast<const PCB_TABLE*>( GetParent() );
  51. for( int row = 0; row < table->GetRowCount(); ++row )
  52. {
  53. for( int col = 0; col < table->GetColCount(); ++col )
  54. {
  55. if( table->GetCell( row, col ) == this )
  56. return row;
  57. }
  58. }
  59. return -1;
  60. }
  61. int PCB_TABLECELL::GetColumn() const
  62. {
  63. const PCB_TABLE* table = static_cast<const PCB_TABLE*>( GetParent() );
  64. for( int row = 0; row < table->GetRowCount(); ++row )
  65. {
  66. for( int col = 0; col < table->GetColCount(); ++col )
  67. {
  68. if( table->GetCell( row, col ) == this )
  69. return col;
  70. }
  71. }
  72. return -1;
  73. }
  74. wxString PCB_TABLECELL::GetAddr() const
  75. {
  76. return wxString::Format( wxT( "%c%d" ),
  77. 'A' + GetColumn() % 26,
  78. GetRow() + 1 );
  79. }
  80. int PCB_TABLECELL::GetColumnWidth() const
  81. {
  82. return static_cast<PCB_TABLE*>( GetParent() )->GetColWidth( GetColumn() );
  83. }
  84. void PCB_TABLECELL::SetColumnWidth( int aWidth )
  85. {
  86. PCB_TABLE* table = static_cast<PCB_TABLE*>( GetParent() );
  87. table->SetColWidth( GetColumn(), aWidth );
  88. table->Normalize();
  89. }
  90. int PCB_TABLECELL::GetRowHeight() const
  91. {
  92. return static_cast<PCB_TABLE*>( GetParent() )->GetRowHeight( GetRow() );
  93. }
  94. void PCB_TABLECELL::SetRowHeight( int aHeight )
  95. {
  96. PCB_TABLE* table = static_cast<PCB_TABLE*>( GetParent() );
  97. table->SetRowHeight( GetRow(), aHeight );
  98. table->Normalize();
  99. }
  100. void PCB_TABLECELL::GetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
  101. {
  102. aList.emplace_back( _( "Table Cell" ), GetAddr() );
  103. // Don't use GetShownText() here; we want to show the user the variable references
  104. aList.emplace_back( _( "Text" ), KIUI::EllipsizeStatusText( aFrame, GetText() ) );
  105. if( aFrame->GetName() == PCB_EDIT_FRAME_NAME && IsLocked() )
  106. aList.emplace_back( _( "Status" ), _( "Locked" ) );
  107. aList.emplace_back( _( "Layer" ), GetLayerName() );
  108. aList.emplace_back( _( "Mirror" ), IsMirrored() ? _( "Yes" ) : _( "No" ) );
  109. aList.emplace_back( _( "Cell Width" ),
  110. aFrame->MessageTextFromValue( std::abs( GetEnd().x - GetStart().x ) ) );
  111. aList.emplace_back( _( "Cell Height" ),
  112. aFrame->MessageTextFromValue( std::abs( GetEnd().y - GetStart().y ) ) );
  113. aList.emplace_back( _( "Font" ), GetFont() ? GetFont()->GetName() : _( "Default" ) );
  114. if( GetTextThickness() )
  115. aList.emplace_back( _( "Text Thickness" ), aFrame->MessageTextFromValue( GetEffectiveTextPenWidth() ) );
  116. else
  117. aList.emplace_back( _( "Text Thickness" ), _( "Auto" ) );
  118. aList.emplace_back( _( "Text Width" ), aFrame->MessageTextFromValue( GetTextWidth() ) );
  119. aList.emplace_back( _( "Text Height" ), aFrame->MessageTextFromValue( GetTextHeight() ) );
  120. }
  121. double PCB_TABLECELL::Similarity( const BOARD_ITEM& aBoardItem ) const
  122. {
  123. if( aBoardItem.Type() != Type() )
  124. return 0.0;
  125. const PCB_TABLECELL& other = static_cast<const PCB_TABLECELL&>( aBoardItem );
  126. double similarity = 1.0;
  127. if( m_colSpan != other.m_colSpan )
  128. similarity *= 0.9;
  129. if( m_rowSpan != other.m_rowSpan )
  130. similarity *= 0.9;
  131. similarity *= PCB_TEXTBOX::Similarity( other );
  132. return similarity;
  133. }
  134. bool PCB_TABLECELL::operator==( const BOARD_ITEM& aBoardItem ) const
  135. {
  136. if( aBoardItem.Type() != Type() )
  137. return false;
  138. const PCB_TABLECELL& other = static_cast<const PCB_TABLECELL&>( aBoardItem );
  139. return *this == other;
  140. }
  141. bool PCB_TABLECELL::operator==( const PCB_TABLECELL& aOther ) const
  142. {
  143. return m_colSpan == aOther.m_colSpan
  144. && m_rowSpan == aOther.m_rowSpan
  145. && PCB_TEXTBOX::operator==( aOther );
  146. }
  147. static struct PCB_TABLECELL_DESC
  148. {
  149. PCB_TABLECELL_DESC()
  150. {
  151. PROPERTY_MANAGER& propMgr = PROPERTY_MANAGER::Instance();
  152. REGISTER_TYPE( PCB_TABLECELL );
  153. propMgr.AddTypeCast( new TYPE_CAST<PCB_TABLECELL, BOARD_ITEM> );
  154. propMgr.AddTypeCast( new TYPE_CAST<PCB_TABLECELL, BOARD_CONNECTED_ITEM> );
  155. propMgr.AddTypeCast( new TYPE_CAST<PCB_TABLECELL, PCB_TEXTBOX> );
  156. propMgr.AddTypeCast( new TYPE_CAST<PCB_TABLECELL, PCB_SHAPE> );
  157. propMgr.AddTypeCast( new TYPE_CAST<PCB_TABLECELL, EDA_SHAPE> );
  158. propMgr.AddTypeCast( new TYPE_CAST<PCB_TABLECELL, EDA_TEXT> );
  159. propMgr.InheritsAfter( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( BOARD_ITEM ) );
  160. propMgr.InheritsAfter( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( BOARD_CONNECTED_ITEM ) );
  161. propMgr.InheritsAfter( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( PCB_TEXTBOX ) );
  162. propMgr.InheritsAfter( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( PCB_SHAPE ) );
  163. propMgr.InheritsAfter( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_SHAPE ) );
  164. propMgr.InheritsAfter( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_TEXT ) );
  165. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( BOARD_ITEM ), _HKI( "Position X" ) );
  166. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( BOARD_ITEM ), _HKI( "Position Y" ) );
  167. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( PCB_SHAPE ), _HKI( "Layer" ) );
  168. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( PCB_SHAPE ), _HKI( "Soldermask" ) );
  169. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( PCB_SHAPE ), _HKI( "Soldermask Margin Override" ) );
  170. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( BOARD_CONNECTED_ITEM ), _HKI( "Net" ) );
  171. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( PCB_TEXTBOX ), _HKI( "Border" ) );
  172. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( PCB_TEXTBOX ), _HKI( "Border Style" ) );
  173. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( PCB_TEXTBOX ), _HKI( "Border Width" ) );
  174. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "Start X" ) );
  175. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "Start Y" ) );
  176. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "End X" ) );
  177. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "End Y" ) );
  178. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "Shape" ) );
  179. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "Width" ) );
  180. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "Height" ) );
  181. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "Line Width" ) );
  182. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "Line Style" ) );
  183. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_SHAPE ), _HKI( "Line Color" ) );
  184. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_TEXT ), _HKI( "Width" ) );
  185. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_TEXT ), _HKI( "Height" ) );
  186. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_TEXT ), _HKI( "Thickness" ) );
  187. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_TEXT ), _HKI( "Orientation" ) );
  188. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_TEXT ), _HKI( "Hyperlink" ) );
  189. propMgr.Mask( TYPE_HASH( PCB_TABLECELL ), TYPE_HASH( EDA_TEXT ), _HKI( "Color" ) );
  190. const wxString tableProps = _( "Table" );
  191. propMgr.AddProperty( new PROPERTY<PCB_TABLECELL, int>( _HKI( "Column Width" ),
  192. &PCB_TABLECELL::SetColumnWidth, &PCB_TABLECELL::GetColumnWidth,
  193. PROPERTY_DISPLAY::PT_SIZE ),
  194. tableProps );
  195. propMgr.AddProperty( new PROPERTY<PCB_TABLECELL, int>( _HKI( "Row Height" ),
  196. &PCB_TABLECELL::SetRowHeight, &PCB_TABLECELL::GetRowHeight,
  197. PROPERTY_DISPLAY::PT_SIZE ),
  198. tableProps );
  199. }
  200. } _PCB_TABLECELL_DESC;