KiCad PCB EDA Suite
dialog_edit_line_style.cpp
Go to the documentation of this file.
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 
25 #include <bitmaps.h>
26 #include <sch_line.h>
27 #include <dialog_edit_line_style.h>
29 #include <sch_edit_frame.h>
30 
31 const int BUTT_COLOR_MINSIZE_X = 32;
32 const int BUTT_COLOR_MINSIZE_Y = 20;
33 
34 
36 {
37  wxString name;
39 };
40 
41 
42 /*
43  * Conversion map between PLOT_DASH_TYPE values and style names displayed
44  */
45 const std::map<PLOT_DASH_TYPE, struct lineTypeStruct> lineTypeNames = {
46  { PLOT_DASH_TYPE::SOLID, { _( "Solid" ), stroke_solid_xpm } },
47  { PLOT_DASH_TYPE::DASH, { _( "Dashed" ), stroke_dash_xpm } },
48  { PLOT_DASH_TYPE::DOT, { _( "Dotted" ), stroke_dot_xpm } },
49  { PLOT_DASH_TYPE::DASHDOT, { _( "Dash-Dot" ), stroke_dashdot_xpm } },
50 };
51 
52 
54  SCH_EDIT_FRAME* aParent, std::deque<SCH_LINE*>& lines )
55  : DIALOG_EDIT_LINE_STYLE_BASE( aParent ),
56  m_frame( aParent ),
57  m_lines( lines ),
58  m_width( aParent, m_staticTextWidth, m_lineWidth, m_staticWidthUnits, true )
59 {
60  m_sdbSizerApply->SetLabel( _( "Default" ) );
61 
63 
64  for( auto& typeEntry : lineTypeNames )
65  {
66  m_typeCombo->Append( typeEntry.second.name, KiBitmap( typeEntry.second.bitmap ) );
67  }
68 
69  m_sdbSizerOK->SetDefault();
70 
71  // Now all widgets have the size fixed, call FinishDialogSettings
73 }
74 
75 
77 {
78  auto first_line = m_lines.front();
79 
80  if( std::all_of( m_lines.begin() + 1, m_lines.end(),
81  [&]( const SCH_LINE* r )
82  {
83  return r->GetPenSize() == first_line->GetPenSize();
84  } ) )
85  {
86  m_width.SetValue( first_line->GetPenSize() );
87  }
88  else
89  {
91  }
92 
93  if( std::all_of( m_lines.begin() + 1, m_lines.end(),
94  [&]( const SCH_LINE* r )
95  {
96  return r->GetLineColor() == first_line->GetLineColor();
97  } ) )
98  {
99  setColor( first_line->GetLineColor() );
100  }
101  else
102  {
103  setColor( COLOR4D::UNSPECIFIED );
104  }
105 
106  if( std::all_of( m_lines.begin() + 1, m_lines.end(),
107  [&]( const SCH_LINE* r )
108  {
109  return r->GetLineStyle() == first_line->GetLineStyle();
110  } ) )
111  {
112  int style = static_cast<int>( first_line->GetLineStyle() );
113  wxCHECK_MSG( style < (int)lineTypeNames.size(), false,
114  "Line type for first line is not found in the type lookup map" );
115  m_typeCombo->SetSelection( style );
116  }
117  else
118  {
119  m_typeCombo->SetSelection( wxNOT_FOUND );
120  }
121 
122  return true;
123 }
124 
125 
126 void DIALOG_EDIT_LINE_STYLE::onColorButtonClicked( wxCommandEvent& event )
127 {
128  COLOR4D newColor = COLOR4D::UNSPECIFIED;
129  DIALOG_COLOR_PICKER dialog( this, m_selectedColor, false );
130 
131  if( dialog.ShowModal() == wxID_OK )
132  newColor = dialog.GetColor();
133 
134  if( newColor == COLOR4D::UNSPECIFIED || m_selectedColor == newColor )
135  return;
136 
137  setColor( newColor );
138 }
139 
140 
142 {
143  wxMemoryDC iconDC;
144 
145 
146  if( aColor == COLOR4D::UNSPECIFIED )
147  {
148  m_colorButton->SetBitmap( KiBitmap( question_mark_xpm ) );
149  }
150  else
151  {
152  wxBitmap bitmap( std::max( m_colorButton->GetSize().x, BUTT_COLOR_MINSIZE_X ),
153  std::max( m_colorButton->GetSize().y, BUTT_COLOR_MINSIZE_Y ) );
154 
155  iconDC.SelectObject( bitmap );
156  iconDC.SetPen( *wxBLACK_PEN );
157 
158  wxBrush brush( aColor.ToColour() );
159  iconDC.SetBrush( brush );
160 
161  // Paint the full bitmap in aColor:
162  iconDC.SetBackground( brush );
163  iconDC.Clear();
164 
165  m_colorButton->SetBitmap( bitmap );
166  }
167 
168  m_colorButton->Refresh();
169 
170  Refresh( false );
171 }
172 
173 
174 void DIALOG_EDIT_LINE_STYLE::resetDefaults( wxCommandEvent& event )
175 {
176  m_width.SetValue( m_lines.front()->GetDefaultWidth() );
177  setColor( m_lines.front()->GetDefaultColor() );
178 
179  auto typeIt = lineTypeNames.find( m_lines.front()->GetDefaultStyle() );
180  wxCHECK_RET( typeIt != lineTypeNames.end(),
181  "Default line type not found line dialogs line type lookup map" );
182 
183  m_typeCombo->SetSelection( static_cast<int>( typeIt->first ) );
184  Refresh();
185 }
186 
187 
189 {
190  m_selectedColor = aColor;
192 }
193 
194 
196 {
197  for( auto& line : m_lines )
198  {
200 
201  if( !m_width.IsIndeterminate() )
202  {
203  line->SetLineWidth( m_width.GetValue() );
204  }
205 
206  if( m_typeCombo->GetSelection() != wxNOT_FOUND )
207  {
208  int selection = m_typeCombo->GetSelection();
209  wxCHECK_MSG( selection < (int)lineTypeNames.size(), false,
210  "Selected line type index exceeds size of line type lookup map" );
211 
212  auto it = lineTypeNames.begin();
213  std::advance( it, selection );
214 
215  line->SetLineStyle( it->first );
216  }
217 
218  if( m_selectedColor != COLOR4D::UNSPECIFIED )
219  {
220  line->SetLineColor( m_selectedColor );
221  }
222 
223  m_frame->RefreshItem( line );
224  }
225 
226  m_frame->GetCanvas()->Refresh();
227  m_frame->OnModify();
228 
229  return true;
230 }
const BITMAP_OPAQUE stroke_dot_xpm[1]
Definition: stroke_dot.cpp:29
PNG memory record (file in memory).
Definition: bitmap_def.h:29
const std::map< PLOT_DASH_TYPE, struct lineTypeStruct > lineTypeNames
void updateColorButton(COLOR4D &aColor)
const int BUTT_COLOR_MINSIZE_X
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
Schematic editor (Eeschema) main window.
bool IsIndeterminate() const
Function IsIndeterminate Returns true if the control holds the indeterminate value (for instance,...
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:115
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:79
const int BUTT_COLOR_MINSIZE_Y
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
void resetDefaults(wxCommandEvent &event) override
const BITMAP_OPAQUE * bitmap
#define INDETERMINATE
void setColor(const COLOR4D &aColor)
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
const BITMAP_OPAQUE stroke_dash_xpm[1]
Definition: stroke_dash.cpp:25
void SaveCopyInUndoList(SCH_ITEM *aItemToCopy, UNDO_REDO_T aTypeCommand, bool aAppend=false, const wxPoint &aTransformPoint=wxPoint(0, 0))
Create a copy of the current schematic item, and put it in the undo list.
const BITMAP_OPAQUE question_mark_xpm[1]
const BITMAP_OPAQUE stroke_dashdot_xpm[1]
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=NULL) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
void onColorButtonClicked(wxCommandEvent &aEvent) override
std::deque< SCH_LINE * > m_lines
Segment description base class to describe items which have 2 end points (track, wire,...
Definition: sch_line.h:38
void RefreshItem(EDA_ITEM *aItem, bool isAddOrDelete=false)
Mark an item for refresh.
#define _(s)
Definition: 3d_actions.cpp:31
virtual void SetValue(int aValue)
Function SetValue Sets new value (in Internal Units) for the text field, taking care of units convers...
KIGFX::COLOR4D GetColor()
virtual long long int GetValue()
Function GetValue Returns the current value in Internal Units.
const BITMAP_OPAQUE stroke_solid_xpm[1]
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag of the current screen and u...
DIALOG_EDIT_LINE_STYLE(SCH_EDIT_FRAME *aParent, std::deque< SCH_LINE * > &lines)
Class DIALOG_EDIT_LINE_STYLE_BASE.
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:39