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-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 
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  std::deque<SCH_ITEM*>& strokeItems ) :
57  DIALOG_EDIT_LINE_STYLE_BASE( aParent ),
58  m_frame( aParent ),
59  m_strokeItems( strokeItems ),
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_stroke_item = m_strokeItems.front();
81 
82  if( std::all_of( m_strokeItems.begin() + 1, m_strokeItems.end(),
83  [&]( const SCH_ITEM* r )
84  {
85  return r->GetPenWidth() == first_stroke_item->GetPenWidth();
86  } ) )
87  {
88  m_width.SetValue( first_stroke_item->GetPenWidth() );
89  }
90  else
91  {
93  }
94 
95  if( std::all_of( m_strokeItems.begin() + 1, m_strokeItems.end(),
96  [&]( const SCH_ITEM* r )
97  {
98  return r->GetStroke().GetColor() == first_stroke_item->GetStroke().GetColor();
99  } ) )
100  {
101  setColor( first_stroke_item->GetStroke().GetColor() );
102  }
103  else
104  {
105  setColor( COLOR4D::UNSPECIFIED );
106  }
107 
108  if( std::all_of( m_strokeItems.begin() + 1, m_strokeItems.end(),
109  [&]( const SCH_ITEM* r )
110  {
111  return r->GetStroke().GetType() == first_stroke_item->GetStroke().GetType();
112  } ) )
113  {
114  int style = static_cast<int>( first_stroke_item->GetStroke().GetType() );
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( 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( 0 );
179  setColor( COLOR4D::UNSPECIFIED );
180 
181  SCH_ITEM* item = dynamic_cast<SCH_ITEM*>( m_strokeItems.front() );
182 
183  wxCHECK( item, /* void */ );
184 
185  auto typeIt = lineTypeNames.find( item->GetStroke().GetType() );
186  wxCHECK_RET( typeIt != lineTypeNames.end(),
187  "Default line type not found line dialogs line type lookup map" );
188 
189  m_typeCombo->SetSelection( static_cast<int>( typeIt->first ) );
190  Refresh();
191 }
192 
193 
195 {
196  m_selectedColor = aColor;
197 
198  if( aColor == COLOR4D::UNSPECIFIED )
199  {
200  COLOR4D defaultColor = Pgm().GetSettingsManager().GetColorSettings()->GetColor(
201  m_strokeItems.front()->GetLayer() );
202  updateColorButton( defaultColor );
203  }
204  else
206 }
207 
208 
210 {
211  PICKED_ITEMS_LIST pickedItems;
212  STROKE_PARAMS stroke;
213 
214  for( SCH_ITEM* strokeItem : m_strokeItems )
215  pickedItems.PushItem( ITEM_PICKER( m_frame->GetScreen(), strokeItem, UR_CHANGED ) );
216 
217  m_frame->SaveCopyInUndoList( pickedItems, UR_CHANGED, false );
218 
219  for( auto& strokeItem : m_strokeItems )
220  {
221  if( !m_width.IsIndeterminate() )
222  {
223  stroke = strokeItem->GetStroke();
224  stroke.SetWidth( m_width.GetValue() );
225  strokeItem->SetStroke( stroke );
226  }
227 
228  if( m_typeCombo->GetSelection() != wxNOT_FOUND )
229  {
230  stroke = strokeItem->GetStroke();
231  int selection = m_typeCombo->GetSelection();
232  wxCHECK_MSG( selection < (int)lineTypeNames.size(), false,
233  "Selected line type index exceeds size of line type lookup map" );
234 
235  auto it = lineTypeNames.begin();
236  std::advance( it, selection );
237 
238  stroke.SetType( it->first );
239  strokeItem->SetStroke( stroke );
240  }
241 
242  stroke = strokeItem->GetStroke();
243  stroke.SetColor( m_selectedColor );
244  strokeItem->SetStroke( stroke );
245 
246  m_frame->RefreshItem( strokeItem );
247  }
248 
249  m_frame->GetCanvas()->Refresh();
250  m_frame->OnModify();
251 
252  return true;
253 }
virtual STROKE_PARAMS GetStroke() const
Definition: sch_item.h:467
void SetWidth(int aWidth)
Definition: sch_item.h:170
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
const BITMAP_OPAQUE stroke_dot_xpm[1]
Definition: stroke_dot.cpp:29
PNG memory record (file in memory).
Definition: bitmap_def.h:29
DIALOG_EDIT_LINE_STYLE(SCH_EDIT_FRAME *aParent, std::deque< SCH_ITEM * > &strokeItems)
const std::map< PLOT_DASH_TYPE, struct lineTypeStruct > lineTypeNames
void updateColorButton(COLOR4D &aColor)
void SetType(PLOT_DASH_TYPE aType)
Definition: sch_item.h:173
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...
std::deque< SCH_ITEM * > m_strokeItems
void PushItem(const ITEM_PICKER &aItem)
Function PushItem pushes aItem to the top of the list.
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:114
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
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
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
const BITMAP_OPAQUE question_mark_xpm[1]
const BITMAP_OPAQUE stroke_dashdot_xpm[1]
PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
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
see class PGM_BASE
void RefreshItem(EDA_ITEM *aItem, bool isAddOrDelete=false)
Mark an item for refresh.
Simple container to manage line stroke parameters.
Definition: sch_item.h:153
#define _(s)
Definition: 3d_actions.cpp:33
void SaveCopyInUndoList(SCH_SCREEN *aScreen, SCH_ITEM *aItemToCopy, UNDO_REDO_T aTypeCommand, bool aAppend, const wxPoint &aTransformPoint=wxPoint(0, 0))
Create a copy of the current schematic item, and put it in the undo list.
virtual void SetValue(int aValue)
Function SetValue Sets new value (in Internal Units) for the text field, taking care of units convers...
#define INDETERMINATE_ACTION
KIGFX::COLOR4D GetColor()
PLOT_DASH_TYPE GetType() const
Definition: sch_item.h:172
virtual long long int GetValue()
Function GetValue Returns the current value in Internal Units.
void SetColor(const COLOR4D &aColor)
Definition: sch_item.h:176
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...
Class DIALOG_EDIT_LINE_STYLE_BASE.
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:187
COLOR4D is the color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:99