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 #include <widgets/color_swatch.h>
33 
34 
36 {
37  wxString name;
38  const BITMAPS bitmap;
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" ), BITMAPS::stroke_solid } },
47  { PLOT_DASH_TYPE::DASH, { _( "Dashed" ), BITMAPS::stroke_dash } },
48  { PLOT_DASH_TYPE::DOT, { _( "Dotted" ), BITMAPS::stroke_dot } },
49  { PLOT_DASH_TYPE::DASHDOT, { _( "Dash-Dot" ), BITMAPS::stroke_dashdot } },
50 };
51 
52 
53 #define DEFAULT_STYLE _( "Default" )
54 #define INDETERMINATE_STYLE _( "Leave unchanged" )
55 
56 
58  std::deque<SCH_ITEM*>& strokeItems ) :
59  DIALOG_EDIT_LINE_STYLE_BASE( aParent ),
60  m_frame( aParent ),
61  m_strokeItems( strokeItems ),
62  m_width( aParent, m_staticTextWidth, m_lineWidth, m_staticWidthUnits, true )
63 {
64  m_sdbSizerApply->SetLabel( _( "Default" ) );
65 
66  m_colorSwatch->SetDefaultColor( COLOR4D::UNSPECIFIED );
67 
69 
70  for( auto& typeEntry : lineTypeNames )
71  m_typeCombo->Append( typeEntry.second.name, KiBitmap( typeEntry.second.bitmap ) );
72 
73  m_typeCombo->Append( DEFAULT_STYLE );
74 
75  m_sdbSizerOK->SetDefault();
76 
77  // Now all widgets have the size fixed, call FinishDialogSettings
79 }
80 
81 
83 {
84  auto first_stroke_item = m_strokeItems.front();
85 
86  if( std::all_of( m_strokeItems.begin() + 1, m_strokeItems.end(),
87  [&]( const SCH_ITEM* r )
88  {
89  return r->GetPenWidth() == first_stroke_item->GetPenWidth();
90  } ) )
91  {
92  m_width.SetValue( first_stroke_item->GetPenWidth() );
93  }
94  else
95  {
97  }
98 
99  if( std::all_of( m_strokeItems.begin() + 1, m_strokeItems.end(),
100  [&]( const SCH_ITEM* r )
101  {
102  return r->GetStroke().GetColor() == first_stroke_item->GetStroke().GetColor();
103  } ) )
104  {
105  m_colorSwatch->SetSwatchColor( first_stroke_item->GetStroke().GetColor(), false );
106  }
107  else
108  {
109  m_colorSwatch->SetSwatchColor( COLOR4D::UNSPECIFIED, false );
110  }
111 
112  if( std::all_of( m_strokeItems.begin() + 1, m_strokeItems.end(),
113  [&]( const SCH_ITEM* r )
114  {
115  return r->GetStroke().GetPlotStyle() == first_stroke_item->GetStroke().GetPlotStyle();
116  } ) )
117  {
118  int style = static_cast<int>( first_stroke_item->GetStroke().GetPlotStyle() );
119 
120  if( style == -1 )
121  m_typeCombo->SetStringSelection( DEFAULT_STYLE );
122  else if( style < (int) lineTypeNames.size() )
123  m_typeCombo->SetSelection( style );
124  else
125  wxFAIL_MSG( "Line type not found in the type lookup map" );
126  }
127  else
128  {
129  m_typeCombo->Append( INDETERMINATE_STYLE );
130  m_typeCombo->SetStringSelection( INDETERMINATE_STYLE );
131  }
132 
133  return true;
134 }
135 
136 
137 void DIALOG_EDIT_LINE_STYLE::resetDefaults( wxCommandEvent& event )
138 {
139  m_width.SetValue( 0 );
140  m_colorSwatch->SetSwatchColor( COLOR4D::UNSPECIFIED, false );
141 
142  m_typeCombo->SetStringSelection( DEFAULT_STYLE );
143 
144  Refresh();
145 }
146 
147 
149 {
150  PICKED_ITEMS_LIST pickedItems;
151  STROKE_PARAMS stroke;
152 
153  for( SCH_ITEM* strokeItem : m_strokeItems )
154  pickedItems.PushItem( ITEM_PICKER( m_frame->GetScreen(), strokeItem, UNDO_REDO::CHANGED ) );
155 
156  m_frame->SaveCopyInUndoList( pickedItems, UNDO_REDO::CHANGED, false );
157 
158  for( SCH_ITEM* strokeItem : m_strokeItems )
159  {
160  stroke = strokeItem->GetStroke();
161 
162  if( !m_width.IsIndeterminate() )
163  stroke.SetWidth( m_width.GetValue() );
164 
165  auto it = lineTypeNames.begin();
166  std::advance( it, m_typeCombo->GetSelection() );
167 
168  if( it == lineTypeNames.end() )
170  else
171  stroke.SetPlotStyle( it->first );
172 
174 
175  strokeItem->SetStroke( stroke );
176  m_frame->UpdateItem( strokeItem );
177  }
178 
179  m_frame->GetCanvas()->Refresh();
180  m_frame->OnModify();
181 
182  return true;
183 }
void SetWidth(int aWidth)
Definition: sch_item.h:168
void SetSwatchColor(KIGFX::COLOR4D aColor, bool aSendEvent)
Set the current swatch color directly.
void UpdateItem(EDA_ITEM *aItem, bool isAddOrDelete=false)
Mark an item for refresh.
KIGFX::COLOR4D GetSwatchColor() const
DIALOG_EDIT_LINE_STYLE(SCH_EDIT_FRAME *aParent, std::deque< SCH_ITEM * > &strokeItems)
#define INDETERMINATE_STYLE
void SetDefaultColor(KIGFX::COLOR4D aColor)
Sets the color that will be chosen with the "Reset to Default" button in the chooser.
const std::map< PLOT_DASH_TYPE, struct lineTypeStruct > lineTypeNames
std::deque< SCH_ITEM * > m_strokeItems
void PushItem(const ITEM_PICKER &aItem)
Push aItem to the top of the list.
Schematic editor (Eeschema) main window.
void SetPlotStyle(PLOT_DASH_TYPE aPlotStyle)
Definition: sch_item.h:171
bool IsIndeterminate() const
Return true if the control holds the indeterminate value (for instance, if it represents a multiple s...
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:98
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
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
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
A holder to handle information on schematic or board items.
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:104
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 finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
see class PGM_BASE
BITMAPS
A list of all bitmap identifiers.
Definition: bitmaps_list.h:32
void SaveCopyInUndoList(SCH_SCREEN *aScreen, SCH_ITEM *aItemToCopy, UNDO_REDO aTypeCommand, bool aAppend)
Create a copy of the current schematic item, and put it in the undo list.
Simple container to manage line stroke parameters.
Definition: sch_item.h:155
#define DEFAULT_STYLE
#define _(s)
Definition: 3d_actions.cpp:33
virtual void SetValue(int aValue)
Set new value (in Internal Units) for the text field, taking care of units conversion.
virtual long long int GetValue()
Return the current value in Internal Units.
void SetColor(const COLOR4D &aColor)
Definition: sch_item.h:174
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.
#define INDETERMINATE_ACTION
Definition: base_units.h:49
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:197