Debian Codegraph @

DataModel/Switch.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #include <map>
22
23 #include "DataModel/Switch.h"
24 #include "Utils/Utils.h"
25
26 using std::map;
27 using std::stringstream;
28 using std::string;
29
30 namespace DataModel
31 {
32 std::string Switch::Serialize() const
33 {
34 string str = "objectType=Switch;";
35 str += AccessoryBase::Serialize();
36 str += ";";
37 str += LayoutItem::Serialize();
38 str += ";";
39 str += LockableItem::Serialize();
40 return str;
41 }
42
43 bool Switch::Deserialize(const std::string& serialized)
44 {
45 map<string,string> arguments;
46 ParseArguments(serialized, arguments);
47 string objectType = Utils::Utils::GetStringMapEntry(arguments, "objectType");
48 if (objectType.compare("Switch") != 0)
49 {
50 return false;
51 }
52
53 AccessoryBase::Deserialize(arguments);
54 LayoutItem::Deserialize(arguments);
55 LockableItem::Deserialize(arguments);
56 SetSizeFromType();
57 SetVisible(VisibleYes);
58 return true;
59 }
60
61 void Switch::SetAccessoryState(const AccessoryState state)
62 {
63 AccessoryState checkedState = state;
64 if (GetAccessoryType() != SwitchTypeThreeWay && state == SwitchStateThird)
65 {
66 checkedState = SwitchStateTurnout;
67 }
68 AccessoryBase::SetAccessoryState(checkedState);
69 }
70
71 bool Switch::UsesAddress(Address address) const
72 {
73 switch(GetAccessoryType())
74 {
75 case SwitchTypeThreeWay:
76 return (GetAddress() == address)
77 || (GetAddress() + 1 == address);
78
79 case SwitchTypeLeft:
80 case SwitchTypeRight:
81 case SwitchTypeMaerklinLeft:
82 case SwitchTypeMaerklinRight:
83 default:
84 return (GetAddress() == address);
85 }
86 }
87
88 std::map<DataModel::AccessoryState, Languages::TextSelector> Switch::GetStateOptions() const
89 {
90 std::map<DataModel::AccessoryState,Languages::TextSelector> out;
91 out[DataModel::SwitchStateStraight] = Languages::TextStraight;
92 switch(GetAccessoryType())
93 {
94 case DataModel::SwitchTypeThreeWay:
95 out[DataModel::SwitchStateTurnout] = Languages::TextLeft;
96 out[DataModel::SwitchStateThird] = Languages::TextRight;
97 break;
98
99 default:
100 out[DataModel::SwitchStateTurnout] = Languages::TextTurnout;
101 break;
102 }
103 return out;
104 }
105
106 AccessoryState Switch::CalculateInvertedSwitchState(const Address address, const AccessoryState state) const
107 {
108 if (!GetInverted())
109 {
110 if ((GetAddress() + 1) == address && state == SwitchStateTurnout)
111 {
112 return SwitchStateThird;
113 }
114 return state;
115 }
116
117 switch(state)
118 {
119 case SwitchStateTurnout:
120 case SwitchStateThird:
121 return SwitchStateStraight;
122
123 case SwitchStateStraight:
124 return GetAddress() == address ? SwitchStateTurnout : SwitchStateThird;
125
126 default:
127 return state;
128 }
129 }
130
131 DataModel::LayoutItem::LayoutItemSize Switch::CalculateHeightFromType(AccessoryType type)
132 {
133 switch (type)
134 {
135 case SwitchTypeMaerklinLeft:
136 case SwitchTypeMaerklinRight:
137 return 2;
138
139 default:
140 return Height1;
141 }
142 }
143
144 Switch& Switch::operator=(const Hardware::AccessoryCacheEntry& accessory)
145 {
146 SetControlID(accessory.GetControlID());
147 SetName(accessory.GetName());
148 SetAddress(accessory.GetAddress());
149 SetProtocol(accessory.GetProtocol());
150 SetMatchKey(accessory.GetMatchKey());
151 return *this;
152 }
153 } // namespace DataModel