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 <pgm_base.h>
31 #include <sch_edit_frame.h>
32 
33 const int BUTT_COLOR_MINSIZE_X = 32;
34 const int BUTT_COLOR_MINSIZE_Y = 20;
35 
36 
38 {
39  wxString name;
41 };
42 
43 
44 /*
45  * Conversion map between PLOT_DASH_TYPE values and style names displayed
46  */
47 const std::map<PLOT_DASH_TYPE, struct lineTypeStruct> lineTypeNames = {
48  { PLOT_DASH_TYPE::SOLID, { _( "Solid" ), stroke_solid_xpm } },
49  { PLOT_DASH_TYPE::DASH, { _( "Dashed" ), stroke_dash_xpm } },
50  { PLOT_DASH_TYPE::DOT, { _( "Dotted" ), stroke_dot_xpm } },
51  { PLOT_DASH_TYPE::DASHDOT, { _( "Dash-Dot" ), stroke_dashdot_xpm } },
52 };
53 
54 
56  SCH_EDIT_FRAME* aParent, std::deque<SCH_LINE*>& lines )
57  : DIALOG_EDIT_LINE_STYLE_BASE( aParent ),
58  m_frame( aParent ),
59  m_lines( lines ),
60  m_width( aParent, m_staticTextWidth, m_lineWidth, m_staticWidthUnits, true )
61 {
62  m_sdbSizerApply->SetLabel( _( "Default" ) );
63 
65 
66  for( auto& typeEntry : lineTypeNames )
67  {
68  m_typeCombo->Append( typeEntry.second.name, KiBitmap( typeEntry.second.bitmap ) );
69  }
70 
71  m_sdbSizerOK->SetDefault();
72 
73  // Now all widgets have the size fixed, call FinishDialogSettings
75 }
76 
77 
79 {
80  auto first_line = m_lines.front();
81 
82  if( std::all_of( m_lines.begin() + 1, m_lines.end(),
83  [&]( const SCH_LINE* r )
84  {
85  return r->GetPenSize() == first_line->GetPenSize();
86  } ) )
87  {
88  m_width.SetValue( first_line->GetPenSize() );
89  }
90  else
91  {
93  }
94 
95  if( std::all_of( m_lines.begin() + 1, m_lines.end(),
96  [&]( const SCH_LINE* r )
97  {
98  return r->GetLineColor() == first_line->GetLineColor();
99  } ) )
100  {
101  setColor( first_line->GetLineColor() );
102  }
103  else
104  {
105  setColor( COLOR4D::UNSPECIFIED );
106  }
107 
108  if( std::all_of( m_lines.begin() + 1, m_lines.end(),
109  [&]( const SCH_LINE* r )
110  {
111  return r->GetLineStyle() == first_line->GetLineStyle();
112  } ) )
113  {
114  int style = static_cast<int>( first_line->GetLineStyle() );
115  wxCHECK_MSG( style < (int)lineTypeNames.size(), false,
116  "Line type for first line is not found in the type lookup map" );
117  m_typeCombo->SetSelection( style );
118  }
119  else
120  {
121  m_typeCombo->SetSelection( wxNOT_FOUND );
122  }
123 
124  return true;
125 }
126 
127 
128 void DIALOG_EDIT_LINE_STYLE::onColorButtonClicked( wxCommandEvent& event )
129 {
130  COLOR4D newColor = COLOR4D::UNSPECIFIED;
131  DIALOG_COLOR_PICKER dialog( this, m_selectedColor, false );
132 
133  if( dialog.ShowModal() == wxID_OK )
134  newColor = dialog.GetColor();
135 
136  if( newColor == COLOR4D::UNSPECIFIED || m_selectedColor == newColor )
137  return;
138 
139  setColor( newColor );
140 }
141 
142 
144 {
145  wxMemoryDC iconDC;
146 
147 
148  if( aColor == COLOR4D::UNSPECIFIED )
149  {
150  m_colorButton->SetBitmap( KiBitmap( question_mark_xpm ) );
151  }
152  else
153  {
154  wxBitmap bitmap( std::max( m_colorButton->GetSize().x, BUTT_COLOR_MINSIZE_X ),
155  std::max( m_colorButton->GetSize().y, BUTT_COLOR_MINSIZE_Y ) );
156 
157  iconDC.SelectObject( bitmap );
158  iconDC.SetPen( *wxBLACK_PEN );
159 
160  wxBrush brush( aColor.ToColour() );
161  iconDC.SetBrush( brush );
162 
163  // Paint the full bitmap in aColor:
164  iconDC.SetBackground( brush );
165  iconDC.Clear();
166 
167  m_colorButton->SetBitmap( bitmap );
168  }
169 
170  m_colorButton->Refresh();
171 
172  Refresh( false );
173 }
174 
175 
176 void DIALOG_EDIT_LINE_STYLE::resetDefaults( wxCommandEvent& event )
177 {
178  m_width.SetValue( m_lines.front()->GetDefaultWidth() );
179  setColor( COLOR4D::UNSPECIFIED );
180 
181  auto typeIt = lineTypeNames.find( m_lines.front()->GetDefaultStyle() );
182  wxCHECK_RET( typeIt != lineTypeNames.end(),
183  "Default line type not found line dialogs line type lookup map" );
184 
185  m_typeCombo->SetSelection( static_cast<int>( typeIt->first ) );
186  Refresh();
187 }
188 
189 
191 {
192  m_selectedColor = aColor;
193 
194  if( aColor == COLOR4D::UNSPECIFIED )
195  {
196  COLOR4D defaultColor = Pgm().GetSettingsManager().GetColorSettings()->GetColor(
197  m_lines.front()->GetLayer() );
198  updateColorButton( defaultColor );
199  }
200  else
202 }
203 
204 
206 {
207  for( auto& line : m_lines )
208  {
210 
211  if( !m_width.IsIndeterminate() )
212  {
213  line->SetLineWidth( m_width.GetValue() );
214  }
215 
216  if( m_typeCombo->GetSelection() != wxNOT_FOUND )
217  {
218  int selection = m_typeCombo->GetSelection();
219  wxCHECK_MSG( selection < (int)lineTypeNames.size(), false,
220  "Selected line type index exceeds size of line type lookup map" );
221 
222  auto it = lineTypeNames.begin();
223  std::advance( it, selection );
224 
225  line->SetLineStyle( it->first );
226  }
227 
228  line->SetLineColor( m_selectedColor );
229 
230  m_frame->RefreshItem( line );
231  }
232 
233  m_frame->GetCanvas()->Refresh();
234  m_frame->OnModify();
235 
236  return true;
237 }
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:103
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:80
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
see class PGM_BASE
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:33
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:40