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.

337 lines
8.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018 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 <kiway.h>
  24. #include <kiway_player.h>
  25. #include <text_mod_grid_table.h>
  26. #include <widgets/grid_icon_text_helpers.h>
  27. #include <widgets/grid_combobox.h>
  28. #include "grid_layer_box_helpers.h"
  29. enum
  30. {
  31. MYID_SELECT_FOOTPRINT = 991, // must be within GRID_TRICKS' enum range
  32. MYID_SHOW_DATASHEET
  33. };
  34. wxArrayString g_menuOrientations;
  35. TEXT_MOD_GRID_TABLE::TEXT_MOD_GRID_TABLE( EDA_UNITS_T aUserUnits, PCB_BASE_FRAME* aFrame ) :
  36. m_userUnits( aUserUnits ),
  37. m_frame( aFrame )
  38. {
  39. // Build the column attributes.
  40. m_readOnlyAttr = new wxGridCellAttr;
  41. m_readOnlyAttr->SetReadOnly( true );
  42. m_boolColAttr = new wxGridCellAttr;
  43. m_boolColAttr->SetRenderer( new wxGridCellBoolRenderer() );
  44. m_boolColAttr->SetEditor( new wxGridCellBoolEditor() );
  45. m_boolColAttr->SetAlignment( wxALIGN_CENTER, wxALIGN_BOTTOM );
  46. if( g_menuOrientations.IsEmpty() )
  47. {
  48. g_menuOrientations.push_back( wxT( "0 " ) + GetAbbreviatedUnitsLabel( DEGREES ) );
  49. g_menuOrientations.push_back( wxT( "90 " ) + GetAbbreviatedUnitsLabel( DEGREES ) );
  50. g_menuOrientations.push_back( wxT( "-90 " ) + GetAbbreviatedUnitsLabel( DEGREES ) );
  51. g_menuOrientations.push_back( wxT( "180 " ) + GetAbbreviatedUnitsLabel( DEGREES ) );
  52. }
  53. m_orientationColAttr = new wxGridCellAttr;
  54. m_orientationColAttr->SetEditor( new GRID_CELL_COMBOBOX( g_menuOrientations ) );
  55. m_layerColAttr = new wxGridCellAttr;
  56. m_layerColAttr->SetRenderer( new GRID_CELL_LAYER_RENDERER( m_frame ) );
  57. m_layerColAttr->SetEditor( new GRID_CELL_LAYER_SELECTOR( m_frame, LSET::ForbiddenTextLayers() ) );
  58. }
  59. TEXT_MOD_GRID_TABLE::~TEXT_MOD_GRID_TABLE()
  60. {
  61. m_readOnlyAttr->DecRef();
  62. m_boolColAttr->DecRef();
  63. m_orientationColAttr->DecRef();
  64. m_layerColAttr->DecRef();
  65. }
  66. wxString TEXT_MOD_GRID_TABLE::GetColLabelValue( int aCol )
  67. {
  68. switch( aCol )
  69. {
  70. case TMC_TEXT: return _( "Text Items" );
  71. case TMC_SHOWN: return _( "Show" );
  72. case TMC_WIDTH: return _( "Width" );
  73. case TMC_HEIGHT: return _( "Height" );
  74. case TMC_THICKNESS: return _( "Thickness" );
  75. case TMC_ITALIC: return _( "Italic" );
  76. case TMC_LAYER: return _( "Layer" );
  77. case TMC_ORIENTATION: return _( "Orientation" );
  78. case TMC_UPRIGHT: return _( "Keep Upright" );
  79. case TMC_XOFFSET: return _( "X Offset" );
  80. case TMC_YOFFSET: return _( "Y Offset" );
  81. default: wxFAIL; return wxEmptyString;
  82. }
  83. }
  84. wxString TEXT_MOD_GRID_TABLE::GetRowLabelValue( int aRow )
  85. {
  86. switch( aRow )
  87. {
  88. case 0: return _( "Reference" );
  89. case 1: return _( "Value" );
  90. default: return wxEmptyString;
  91. }
  92. }
  93. bool TEXT_MOD_GRID_TABLE::CanGetValueAs( int aRow, int aCol, const wxString& aTypeName )
  94. {
  95. switch( aCol )
  96. {
  97. case TMC_TEXT:
  98. case TMC_WIDTH:
  99. case TMC_HEIGHT:
  100. case TMC_THICKNESS:
  101. case TMC_ORIENTATION:
  102. case TMC_XOFFSET:
  103. case TMC_YOFFSET:
  104. return aTypeName == wxGRID_VALUE_STRING;
  105. case TMC_SHOWN:
  106. case TMC_ITALIC:
  107. case TMC_UPRIGHT:
  108. return aTypeName == wxGRID_VALUE_BOOL;
  109. case TMC_LAYER:
  110. return aTypeName == wxGRID_VALUE_NUMBER;
  111. default:
  112. wxFAIL;
  113. return false;
  114. }
  115. }
  116. bool TEXT_MOD_GRID_TABLE::CanSetValueAs( int aRow, int aCol, const wxString& aTypeName )
  117. {
  118. return CanGetValueAs( aRow, aCol, aTypeName );
  119. }
  120. wxGridCellAttr* TEXT_MOD_GRID_TABLE::GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind )
  121. {
  122. switch( aCol )
  123. {
  124. case TMC_TEXT:
  125. case TMC_WIDTH:
  126. case TMC_HEIGHT:
  127. case TMC_THICKNESS:
  128. case TMC_XOFFSET:
  129. case TMC_YOFFSET:
  130. return nullptr;
  131. case TMC_SHOWN:
  132. case TMC_ITALIC:
  133. case TMC_UPRIGHT:
  134. m_boolColAttr->IncRef();
  135. return m_boolColAttr;
  136. case TMC_LAYER:
  137. m_layerColAttr->IncRef();
  138. return m_layerColAttr;
  139. case TMC_ORIENTATION:
  140. m_orientationColAttr->IncRef();
  141. return m_orientationColAttr;
  142. default:
  143. wxFAIL;
  144. return nullptr;
  145. }
  146. }
  147. wxString TEXT_MOD_GRID_TABLE::GetValue( int aRow, int aCol )
  148. {
  149. const TEXTE_MODULE& text = this->at( (size_t) aRow );
  150. switch( aCol )
  151. {
  152. case TMC_TEXT:
  153. return text.GetText();
  154. case TMC_WIDTH:
  155. return StringFromValue( m_userUnits, text.GetTextWidth(), true, true );
  156. case TMC_HEIGHT:
  157. return StringFromValue( m_userUnits, text.GetTextHeight(), true, true );
  158. case TMC_THICKNESS:
  159. return StringFromValue( m_userUnits, text.GetThickness(), true, true );
  160. case TMC_LAYER:
  161. return text.GetLayerName();
  162. case TMC_ORIENTATION:
  163. return StringFromValue( DEGREES, (int) NormalizeAnglePos( text.GetTextAngle() ), true );
  164. case TMC_XOFFSET:
  165. return StringFromValue( m_userUnits, text.GetPos0().x, true );
  166. case TMC_YOFFSET:
  167. return StringFromValue( m_userUnits, text.GetPos0().y, true );
  168. default:
  169. // we can't assert here because wxWidgets sometimes calls this without checking
  170. // the column type when trying to see if there's an overflow
  171. return wxT( "bad wxWidgets!" );
  172. }
  173. }
  174. bool TEXT_MOD_GRID_TABLE::GetValueAsBool( int aRow, int aCol )
  175. {
  176. TEXTE_MODULE& text = this->at( (size_t) aRow );
  177. switch( aCol )
  178. {
  179. case TMC_SHOWN: return text.IsVisible();
  180. case TMC_ITALIC: return text.IsItalic();
  181. case TMC_UPRIGHT: return text.IsKeepUpright();
  182. default:
  183. wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a bool value" ), aCol ) );
  184. return false;
  185. }
  186. }
  187. long TEXT_MOD_GRID_TABLE::GetValueAsLong( int aRow, int aCol )
  188. {
  189. TEXTE_MODULE& text = this->at( (size_t) aRow );
  190. switch( aCol )
  191. {
  192. case TMC_LAYER: return text.GetLayer();
  193. default:
  194. wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a long value" ), aCol ) );
  195. return 0;
  196. }
  197. }
  198. void TEXT_MOD_GRID_TABLE::SetValue( int aRow, int aCol, const wxString &aValue )
  199. {
  200. TEXTE_MODULE& text = this->at( (size_t) aRow );
  201. wxPoint pos;
  202. switch( aCol )
  203. {
  204. case TMC_TEXT:
  205. text.SetText( aValue );
  206. break;
  207. case TMC_WIDTH:
  208. text.SetTextWidth( ValueFromString( m_userUnits, aValue, true ) );
  209. break;
  210. case TMC_HEIGHT:
  211. text.SetTextHeight( ValueFromString( m_userUnits, aValue, true ) );
  212. break;
  213. case TMC_THICKNESS:
  214. text.SetThickness( ValueFromString( m_userUnits, aValue, true ) );
  215. break;
  216. case TMC_ORIENTATION:
  217. text.SetTextAngle( DoubleValueFromString( DEGREES, aValue ) );
  218. text.SetDrawCoord();
  219. break;
  220. case TMC_XOFFSET:
  221. case TMC_YOFFSET:
  222. pos = text.GetPos0();
  223. if( aCol == TMC_XOFFSET )
  224. pos.x = ValueFromString( m_userUnits, aValue );
  225. else
  226. pos.y = ValueFromString( m_userUnits, aValue );
  227. text.SetPos0( pos );
  228. text.SetDrawCoord();
  229. break;
  230. default:
  231. wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a string value" ), aCol ) );
  232. break;
  233. }
  234. GetView()->Refresh();
  235. }
  236. void TEXT_MOD_GRID_TABLE::SetValueAsBool( int aRow, int aCol, bool aValue )
  237. {
  238. TEXTE_MODULE& text = this->at( (size_t) aRow );
  239. switch( aCol )
  240. {
  241. case TMC_SHOWN:
  242. text.SetVisible( aValue );
  243. break;
  244. case TMC_ITALIC:
  245. text.SetItalic( aValue );
  246. break;
  247. case TMC_UPRIGHT:text.SetKeepUpright( aValue );
  248. break;
  249. default:
  250. wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a bool value" ), aCol ) );
  251. break;
  252. }
  253. }
  254. void TEXT_MOD_GRID_TABLE::SetValueAsLong( int aRow, int aCol, long aValue )
  255. {
  256. TEXTE_MODULE& text = this->at( (size_t) aRow );
  257. switch( aCol )
  258. {
  259. case TMC_LAYER:
  260. text.SetLayer( ToLAYER_ID( (int) aValue ) );
  261. text.SetMirrored( IsBackLayer( text.GetLayer() ) );
  262. break;
  263. default:
  264. wxFAIL_MSG( wxString::Format( wxT( "column %d doesn't hold a long value" ), aCol ) );
  265. break;
  266. }
  267. }