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.

183 lines
5.6 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 Seth Hillbrand <hillbrand@ucdavis.edu>
  5. * Copyright (C) 2014-2020 KiCad Developers, see AUTHORS.txt for contributors.
  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 <bitmaps.h>
  25. #include <sch_line.h>
  26. #include <dialog_edit_line_style.h>
  27. #include <dialogs/dialog_color_picker.h>
  28. #include <pgm_base.h>
  29. #include <settings/settings_manager.h>
  30. #include <sch_edit_frame.h>
  31. #include <widgets/color_swatch.h>
  32. struct lineTypeStruct
  33. {
  34. wxString name;
  35. const BITMAP_OPAQUE* bitmap;
  36. };
  37. /*
  38. * Conversion map between PLOT_DASH_TYPE values and style names displayed
  39. */
  40. const std::map<PLOT_DASH_TYPE, struct lineTypeStruct> lineTypeNames = {
  41. { PLOT_DASH_TYPE::SOLID, { _( "Solid" ), stroke_solid_xpm } },
  42. { PLOT_DASH_TYPE::DASH, { _( "Dashed" ), stroke_dash_xpm } },
  43. { PLOT_DASH_TYPE::DOT, { _( "Dotted" ), stroke_dot_xpm } },
  44. { PLOT_DASH_TYPE::DASHDOT, { _( "Dash-Dot" ), stroke_dashdot_xpm } },
  45. };
  46. #define DEFAULT_STYLE _( "Default" )
  47. #define INDETERMINATE_STYLE _( "Leave unchanged" )
  48. DIALOG_EDIT_LINE_STYLE::DIALOG_EDIT_LINE_STYLE( SCH_EDIT_FRAME* aParent,
  49. std::deque<SCH_ITEM*>& strokeItems ) :
  50. DIALOG_EDIT_LINE_STYLE_BASE( aParent ),
  51. m_frame( aParent ),
  52. m_strokeItems( strokeItems ),
  53. m_width( aParent, m_staticTextWidth, m_lineWidth, m_staticWidthUnits, true )
  54. {
  55. m_sdbSizerApply->SetLabel( _( "Default" ) );
  56. m_colorSwatch->SetDefaultColor( COLOR4D::UNSPECIFIED );
  57. SetInitialFocus( m_lineWidth );
  58. for( auto& typeEntry : lineTypeNames )
  59. m_typeCombo->Append( typeEntry.second.name, KiBitmap( typeEntry.second.bitmap ) );
  60. m_typeCombo->Append( DEFAULT_STYLE );
  61. m_sdbSizerOK->SetDefault();
  62. // Now all widgets have the size fixed, call FinishDialogSettings
  63. FinishDialogSettings();
  64. }
  65. bool DIALOG_EDIT_LINE_STYLE::TransferDataToWindow()
  66. {
  67. auto first_stroke_item = m_strokeItems.front();
  68. if( std::all_of( m_strokeItems.begin() + 1, m_strokeItems.end(),
  69. [&]( const SCH_ITEM* r )
  70. {
  71. return r->GetPenWidth() == first_stroke_item->GetPenWidth();
  72. } ) )
  73. {
  74. m_width.SetValue( first_stroke_item->GetPenWidth() );
  75. }
  76. else
  77. {
  78. m_width.SetValue( INDETERMINATE_ACTION );
  79. }
  80. if( std::all_of( m_strokeItems.begin() + 1, m_strokeItems.end(),
  81. [&]( const SCH_ITEM* r )
  82. {
  83. return r->GetStroke().GetColor() == first_stroke_item->GetStroke().GetColor();
  84. } ) )
  85. {
  86. m_colorSwatch->SetSwatchColor( first_stroke_item->GetStroke().GetColor(), false );
  87. }
  88. else
  89. {
  90. m_colorSwatch->SetSwatchColor( COLOR4D::UNSPECIFIED, false );
  91. }
  92. if( std::all_of( m_strokeItems.begin() + 1, m_strokeItems.end(),
  93. [&]( const SCH_ITEM* r )
  94. {
  95. return r->GetStroke().GetPlotStyle() == first_stroke_item->GetStroke().GetPlotStyle();
  96. } ) )
  97. {
  98. int style = static_cast<int>( first_stroke_item->GetStroke().GetPlotStyle() );
  99. if( style == -1 )
  100. m_typeCombo->SetStringSelection( DEFAULT_STYLE );
  101. else if( style < (int) lineTypeNames.size() )
  102. m_typeCombo->SetSelection( style );
  103. else
  104. wxFAIL_MSG( "Line type not found in the type lookup map" );
  105. }
  106. else
  107. {
  108. m_typeCombo->Append( INDETERMINATE_STYLE );
  109. m_typeCombo->SetStringSelection( INDETERMINATE_STYLE );
  110. }
  111. return true;
  112. }
  113. void DIALOG_EDIT_LINE_STYLE::resetDefaults( wxCommandEvent& event )
  114. {
  115. m_width.SetValue( 0 );
  116. m_colorSwatch->SetSwatchColor( COLOR4D::UNSPECIFIED, false );
  117. m_typeCombo->SetStringSelection( DEFAULT_STYLE );
  118. Refresh();
  119. }
  120. bool DIALOG_EDIT_LINE_STYLE::TransferDataFromWindow()
  121. {
  122. PICKED_ITEMS_LIST pickedItems;
  123. STROKE_PARAMS stroke;
  124. for( SCH_ITEM* strokeItem : m_strokeItems )
  125. pickedItems.PushItem( ITEM_PICKER( m_frame->GetScreen(), strokeItem, UNDO_REDO::CHANGED ) );
  126. m_frame->SaveCopyInUndoList( pickedItems, UNDO_REDO::CHANGED, false );
  127. for( SCH_ITEM* strokeItem : m_strokeItems )
  128. {
  129. stroke = strokeItem->GetStroke();
  130. if( !m_width.IsIndeterminate() )
  131. stroke.SetWidth( m_width.GetValue() );
  132. auto it = lineTypeNames.begin();
  133. std::advance( it, m_typeCombo->GetSelection() );
  134. if( it == lineTypeNames.end() )
  135. stroke.SetPlotStyle( PLOT_DASH_TYPE::DEFAULT );
  136. else
  137. stroke.SetPlotStyle( it->first );
  138. stroke.SetColor( m_colorSwatch->GetSwatchColor() );
  139. strokeItem->SetStroke( stroke );
  140. m_frame->UpdateItem( strokeItem );
  141. }
  142. m_frame->GetCanvas()->Refresh();
  143. m_frame->OnModify();
  144. return true;
  145. }