KiCad PCB EDA Suite
Loading...
Searching...
No Matches
grid_settings.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) 2023 Mike Williams <[email protected]>
5 * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
22#include <nlohmann/json.hpp>
23#include <wx/translation.h>
24
25#include <units_provider.h>
26
27namespace nlohmann
28{
29template <>
30struct adl_serializer<wxString>
31{
32 static void to_json( json& j, const wxString& s ) { j = s.ToUTF8(); }
33
34 static void from_json( const json& j, wxString& s )
35 {
36 s = wxString::FromUTF8( j.get<std::string>().c_str() );
37 }
38};
39} // namespace nlohmann
40
41wxString GRID::MessageText( EDA_IU_SCALE aScale, EDA_UNITS aUnits, bool aDisplayUnits ) const
42{
43 EDA_DATA_TYPE type = EDA_DATA_TYPE::DISTANCE;
44
46 aScale, aUnits,
47 EDA_UNIT_UTILS::UI::DoubleValueFromString( aScale, EDA_UNITS::MILLIMETRES, x, type ),
48 aDisplayUnits );
50 aScale, aUnits,
51 EDA_UNIT_UTILS::UI::DoubleValueFromString( aScale, EDA_UNITS::MILLIMETRES, y, type ),
52 aDisplayUnits );
53
54 if( xStr == yStr )
55 return xStr;
56
57 return wxString::Format( wxS( "%s x %s" ), xStr, yStr );
58}
59
60wxString GRID::UserUnitsMessageText( UNITS_PROVIDER* aProvider, bool aDisplayUnits ) const
61{
62 return MessageText( aProvider->GetIuScale(), aProvider->GetUserUnits(), aDisplayUnits );
63}
64
65
67{
68 return VECTOR2D{
69 EDA_UNIT_UTILS::UI::DoubleValueFromString( aScale, EDA_UNITS::MILLIMETRES, x ),
70 EDA_UNIT_UTILS::UI::DoubleValueFromString( aScale, EDA_UNITS::MILLIMETRES, y ),
71 };
72}
73
74
75bool GRID::operator==( const GRID& aOther ) const
76{
77 return x == aOther.x && y == aOther.y;
78}
79
80
81bool operator!=( const GRID& lhs, const GRID& rhs )
82{
83 return !( lhs == rhs );
84}
85
86
87bool operator<( const GRID& lhs, const GRID& rhs )
88{
89 return lhs.name < rhs.name;
90}
91
92
93void to_json( nlohmann::json& j, const GRID& g )
94{
95 j = nlohmann::json{
96 { "name", g.name },
97 { "x", g.x },
98 { "y", g.y },
99 };
100}
101
102
103void from_json( const nlohmann::json& j, GRID& g )
104{
105 j.at( "name" ).get_to( g.name );
106 j.at( "x" ).get_to( g.x );
107 j.at( "y" ).get_to( g.y );
108}
const EDA_IU_SCALE & GetIuScale() const
EDA_UNITS GetUserUnits() const
EDA_DATA_TYPE
The type of unit.
Definition: eda_units.h:38
EDA_UNITS
Definition: eda_units.h:46
nlohmann::json json
Definition: gerbview.cpp:46
bool operator<(const GRID &lhs, const GRID &rhs)
void from_json(const nlohmann::json &j, GRID &g)
bool operator!=(const GRID &lhs, const GRID &rhs)
void to_json(nlohmann::json &j, const GRID &g)
KICOMMON_API wxString MessageTextFromValue(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, double aValue, bool aAddUnitsText=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
A helper to convert the double length aValue to a string in inches, millimeters, or unscaled units.
Definition: eda_units.cpp:378
KICOMMON_API double DoubleValueFromString(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
Function DoubleValueFromString converts aTextValue to a double.
Definition: eda_units.cpp:565
Common grid settings, available to every frame.
Definition: grid_settings.h:34
bool operator==(const GRID &aOther) const
wxString UserUnitsMessageText(UNITS_PROVIDER *aProvider, bool aDisplayUnits=true) const
Returns a string representation of the grid in the user's units.
wxString y
Definition: grid_settings.h:53
wxString MessageText(EDA_IU_SCALE aScale, EDA_UNITS aUnits, bool aDisplayUnits=true) const
Returns a string representation of the grid in specified units.
VECTOR2D ToDouble(EDA_IU_SCALE aScale) const
wxString x
Definition: grid_settings.h:52
wxString name
Definition: grid_settings.h:51
static void from_json(const json &j, wxString &s)
static void to_json(json &j, const wxString &s)