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.

229 lines
6.4 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-2018 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 <sch_edit_frame.h>
  29. const int BUTT_COLOR_MINSIZE_X = 32;
  30. const int BUTT_COLOR_MINSIZE_Y = 20;
  31. struct lineTypeStruct
  32. {
  33. wxString name;
  34. const BITMAP_OPAQUE* bitmap;
  35. };
  36. /*
  37. * Conversion map between PLOT_DASH_TYPE values and style names displayed
  38. */
  39. const std::map<PLOT_DASH_TYPE, struct lineTypeStruct> lineTypeNames = {
  40. { PLOT_DASH_TYPE::SOLID, { _( "Solid" ), stroke_solid_xpm } },
  41. { PLOT_DASH_TYPE::DASH, { _( "Dashed" ), stroke_dash_xpm } },
  42. { PLOT_DASH_TYPE::DOT, { _( "Dotted" ), stroke_dot_xpm } },
  43. { PLOT_DASH_TYPE::DASHDOT, { _( "Dash-Dot" ), stroke_dashdot_xpm } },
  44. };
  45. DIALOG_EDIT_LINE_STYLE::DIALOG_EDIT_LINE_STYLE(
  46. SCH_EDIT_FRAME* aParent, std::deque<SCH_LINE*>& lines )
  47. : DIALOG_EDIT_LINE_STYLE_BASE( aParent ),
  48. m_frame( aParent ),
  49. m_lines( lines ),
  50. m_width( aParent, m_staticTextWidth, m_lineWidth, m_staticWidthUnits, true )
  51. {
  52. m_sdbSizerApply->SetLabel( _( "Default" ) );
  53. SetInitialFocus( m_lineWidth );
  54. for( auto& typeEntry : lineTypeNames )
  55. {
  56. m_typeCombo->Append( typeEntry.second.name, KiBitmap( typeEntry.second.bitmap ) );
  57. }
  58. m_sdbSizerOK->SetDefault();
  59. // Now all widgets have the size fixed, call FinishDialogSettings
  60. FinishDialogSettings();
  61. }
  62. bool DIALOG_EDIT_LINE_STYLE::TransferDataToWindow()
  63. {
  64. auto first_line = m_lines.front();
  65. if( std::all_of( m_lines.begin() + 1, m_lines.end(),
  66. [&]( const SCH_LINE* r )
  67. {
  68. return r->GetPenSize() == first_line->GetPenSize();
  69. } ) )
  70. {
  71. m_width.SetValue( first_line->GetPenSize() );
  72. }
  73. else
  74. {
  75. m_width.SetValue( INDETERMINATE );
  76. }
  77. if( std::all_of( m_lines.begin() + 1, m_lines.end(),
  78. [&]( const SCH_LINE* r )
  79. {
  80. return r->GetLineColor() == first_line->GetLineColor();
  81. } ) )
  82. {
  83. setColor( first_line->GetLineColor() );
  84. }
  85. else
  86. {
  87. setColor( COLOR4D::UNSPECIFIED );
  88. }
  89. if( std::all_of( m_lines.begin() + 1, m_lines.end(),
  90. [&]( const SCH_LINE* r )
  91. {
  92. return r->GetLineStyle() == first_line->GetLineStyle();
  93. } ) )
  94. {
  95. int style = static_cast<int>( first_line->GetLineStyle() );
  96. wxCHECK_MSG( style < (int)lineTypeNames.size(), false,
  97. "Line type for first line is not found in the type lookup map" );
  98. m_typeCombo->SetSelection( style );
  99. }
  100. else
  101. {
  102. m_typeCombo->SetSelection( wxNOT_FOUND );
  103. }
  104. return true;
  105. }
  106. void DIALOG_EDIT_LINE_STYLE::onColorButtonClicked( wxCommandEvent& event )
  107. {
  108. COLOR4D newColor = COLOR4D::UNSPECIFIED;
  109. DIALOG_COLOR_PICKER dialog( this, m_selectedColor, false );
  110. if( dialog.ShowModal() == wxID_OK )
  111. newColor = dialog.GetColor();
  112. if( newColor == COLOR4D::UNSPECIFIED || m_selectedColor == newColor )
  113. return;
  114. setColor( newColor );
  115. }
  116. void DIALOG_EDIT_LINE_STYLE::updateColorButton( COLOR4D& aColor )
  117. {
  118. wxMemoryDC iconDC;
  119. if( aColor == COLOR4D::UNSPECIFIED )
  120. {
  121. m_colorButton->SetBitmap( KiBitmap( question_mark_xpm ) );
  122. }
  123. else
  124. {
  125. wxBitmap bitmap( std::max( m_colorButton->GetSize().x, BUTT_COLOR_MINSIZE_X ),
  126. std::max( m_colorButton->GetSize().y, BUTT_COLOR_MINSIZE_Y ) );
  127. iconDC.SelectObject( bitmap );
  128. iconDC.SetPen( *wxBLACK_PEN );
  129. wxBrush brush( aColor.ToColour() );
  130. iconDC.SetBrush( brush );
  131. // Paint the full bitmap in aColor:
  132. iconDC.SetBackground( brush );
  133. iconDC.Clear();
  134. m_colorButton->SetBitmap( bitmap );
  135. }
  136. m_colorButton->Refresh();
  137. Refresh( false );
  138. }
  139. void DIALOG_EDIT_LINE_STYLE::resetDefaults( wxCommandEvent& event )
  140. {
  141. m_width.SetValue( m_lines.front()->GetDefaultWidth() );
  142. setColor( m_lines.front()->GetDefaultColor() );
  143. auto typeIt = lineTypeNames.find( m_lines.front()->GetDefaultStyle() );
  144. wxCHECK_RET( typeIt != lineTypeNames.end(),
  145. "Default line type not found line dialogs line type lookup map" );
  146. m_typeCombo->SetSelection( static_cast<int>( typeIt->first ) );
  147. Refresh();
  148. }
  149. void DIALOG_EDIT_LINE_STYLE::setColor( const COLOR4D& aColor )
  150. {
  151. m_selectedColor = aColor;
  152. updateColorButton( m_selectedColor );
  153. }
  154. bool DIALOG_EDIT_LINE_STYLE::TransferDataFromWindow()
  155. {
  156. for( auto& line : m_lines )
  157. {
  158. m_frame->SaveCopyInUndoList( line, UR_CHANGED );
  159. if( !m_width.IsIndeterminate() )
  160. {
  161. line->SetLineWidth( m_width.GetValue() );
  162. }
  163. if( m_typeCombo->GetSelection() != wxNOT_FOUND )
  164. {
  165. int selection = m_typeCombo->GetSelection();
  166. wxCHECK_MSG( selection < (int)lineTypeNames.size(), false,
  167. "Selected line type index exceeds size of line type lookup map" );
  168. auto it = lineTypeNames.begin();
  169. std::advance( it, selection );
  170. line->SetLineStyle( it->first );
  171. }
  172. if( m_selectedColor != COLOR4D::UNSPECIFIED )
  173. {
  174. line->SetLineColor( m_selectedColor );
  175. }
  176. m_frame->RefreshItem( line );
  177. }
  178. m_frame->GetCanvas()->Refresh();
  179. m_frame->OnModify();
  180. return true;
  181. }