KiCad PCB EDA Suite
dialog_line_wire_bus_properties.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-2021 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>
30 #include <sch_edit_frame.h>
31 #include <widgets/color_swatch.h>
32 
33 
35 {
36  wxString name;
37  const BITMAPS bitmap;
38 };
39 
40 
41 /*
42  * Conversion map between PLOT_DASH_TYPE values and style names displayed
43  */
44 const std::map<PLOT_DASH_TYPE, struct lineTypeStruct> lineTypeNames = {
45  { PLOT_DASH_TYPE::SOLID, { _( "Solid" ), BITMAPS::stroke_solid } },
46  { PLOT_DASH_TYPE::DASH, { _( "Dashed" ), BITMAPS::stroke_dash } },
47  { PLOT_DASH_TYPE::DOT, { _( "Dotted" ), BITMAPS::stroke_dot } },
48  { PLOT_DASH_TYPE::DASHDOT, { _( "Dash-Dot" ), BITMAPS::stroke_dashdot } },
49 };
50 
51 
52 #define DEFAULT_STYLE _( "Default" )
53 #define INDETERMINATE_STYLE _( "Leave unchanged" )
54 
55 
57  std::deque<SCH_ITEM*>& aItems ) :
59  m_frame( aParent ),
60  m_strokeItems( aItems ),
61  m_width( aParent, m_staticTextWidth, m_lineWidth, m_staticWidthUnits, true )
62 {
63  m_sdbSizerApply->SetLabel( _( "Default" ) );
64 
65  m_colorSwatch->SetDefaultColor( COLOR4D::UNSPECIFIED );
66 
67  m_helpLabel1->SetFont( KIUI::GetInfoFont( this ).Italic() );
68  m_helpLabel2->SetFont( KIUI::GetInfoFont( this ).Italic() );
69 
71 
72  for( const std::pair<const PLOT_DASH_TYPE, lineTypeStruct>& typeEntry : lineTypeNames )
73  m_typeCombo->Append( typeEntry.second.name, KiBitmap( typeEntry.second.bitmap ) );
74 
75  m_typeCombo->Append( DEFAULT_STYLE );
76 
77  m_sdbSizerOK->SetDefault();
78 
79  // Now all widgets have the size fixed, call FinishDialogSettings
81 }
82 
83 
85 {
86  SCH_ITEM* first_stroke_item = m_strokeItems.front();
87 
88  if( std::all_of( m_strokeItems.begin() + 1, m_strokeItems.end(),
89  [&]( const SCH_ITEM* r )
90  {
91  return r->GetPenWidth() == first_stroke_item->GetPenWidth();
92  } ) )
93  {
94  m_width.SetValue( first_stroke_item->GetStroke().GetWidth() );
95  }
96  else
97  {
99  }
100 
101  if( std::all_of( m_strokeItems.begin() + 1, m_strokeItems.end(),
102  [&]( const SCH_ITEM* r )
103  {
104  return r->GetStroke().GetColor() == first_stroke_item->GetStroke().GetColor();
105  } ) )
106  {
107  m_colorSwatch->SetSwatchColor( first_stroke_item->GetStroke().GetColor(), false );
108  }
109  else
110  {
111  m_colorSwatch->SetSwatchColor( COLOR4D::UNSPECIFIED, false );
112  }
113 
114  if( std::all_of( m_strokeItems.begin() + 1, m_strokeItems.end(),
115  [&]( const SCH_ITEM* r )
116  {
117  return r->GetStroke().GetPlotStyle() == first_stroke_item->GetStroke().GetPlotStyle();
118  } ) )
119  {
120  int style = static_cast<int>( first_stroke_item->GetStroke().GetPlotStyle() );
121 
122  if( style == -1 )
123  m_typeCombo->SetStringSelection( DEFAULT_STYLE );
124  else if( style < (int) lineTypeNames.size() )
125  m_typeCombo->SetSelection( style );
126  else
127  wxFAIL_MSG( "Line type not found in the type lookup map" );
128  }
129  else
130  {
131  m_typeCombo->Append( INDETERMINATE_STYLE );
132  m_typeCombo->SetStringSelection( INDETERMINATE_STYLE );
133  }
134 
135  return true;
136 }
137 
138 
140 {
141  m_width.SetValue( 0 );
142  m_colorSwatch->SetSwatchColor( COLOR4D::UNSPECIFIED, false );
143 
144  m_typeCombo->SetStringSelection( DEFAULT_STYLE );
145 
146  Refresh();
147 }
148 
149 
151 {
152  PICKED_ITEMS_LIST pickedItems;
153  STROKE_PARAMS stroke;
154 
155  for( SCH_ITEM* strokeItem : m_strokeItems )
156  pickedItems.PushItem( ITEM_PICKER( m_frame->GetScreen(), strokeItem, UNDO_REDO::CHANGED ) );
157 
158  m_frame->SaveCopyInUndoList( pickedItems, UNDO_REDO::CHANGED, false );
159 
160  for( SCH_ITEM* strokeItem : m_strokeItems )
161  {
162  stroke = strokeItem->GetStroke();
163 
164  if( !m_width.IsIndeterminate() )
165  stroke.SetWidth( m_width.GetValue() );
166 
167  auto it = lineTypeNames.begin();
168  std::advance( it, m_typeCombo->GetSelection() );
169 
170  if( it == lineTypeNames.end() )
172  else
173  stroke.SetPlotStyle( it->first );
174 
176 
177  strokeItem->SetStroke( stroke );
178  m_frame->UpdateItem( strokeItem );
179  }
180 
181  m_frame->GetCanvas()->Refresh();
182  m_frame->OnModify();
183 
184  return true;
185 }
void SetSwatchColor(const KIGFX::COLOR4D &aColor, bool aSendEvent)
Set the current swatch color directly.
virtual STROKE_PARAMS GetStroke() const
Definition: sch_item.h:460
void SetWidth(int aWidth)
Definition: sch_item.h:164
void UpdateItem(EDA_ITEM *aItem, bool isAddOrDelete=false)
Mark an item for refresh.
KIGFX::COLOR4D GetSwatchColor() const
DIALOG_LINE_WIRE_BUS_PROPERTIES(SCH_EDIT_FRAME *aParent, std::deque< SCH_ITEM * > &aItems)
const std::map< PLOT_DASH_TYPE, struct lineTypeStruct > lineTypeNames
wxFont GetInfoFont(wxWindow *aWindow)
Definition: ui_common.cpp:141
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:167
Class DIALOG_LINE_WIRE_BUS_PROPERTIES_BASE.
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:97
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...
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
PLOT_DASH_TYPE GetPlotStyle() const
Definition: sch_item.h:166
#define _(s)
void SetDefaultColor(const KIGFX::COLOR4D &aColor)
Sets the color that will be chosen with the "Reset to Default" button in the chooser.
int GetWidth() const
Definition: sch_item.h:163
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:105
void resetDefaults(wxCommandEvent &event) override
virtual int GetPenWidth() const
Definition: sch_item.h:285
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
COLOR4D GetColor() const
Definition: sch_item.h:169
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:151
virtual void SetValue(int aValue)
Set new value (in Internal Units) for the text field, taking care of units conversion.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
Update the board display after modifying it by a python script (note: it is automatically called by a...
virtual long long int GetValue()
Return the current value in Internal Units.
void SetColor(const COLOR4D &aColor)
Definition: sch_item.h:170
#define INDETERMINATE_STYLE
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag of the current screen and u...
#define INDETERMINATE_ACTION
Definition: base_units.h:48
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition: sch_item.h:193