| 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 |
void 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; |
| 51 |
} |
| 52 |
|
| 53 |
AccessoryBase::Deserialize(arguments); |
| 54 |
LayoutItem::Deserialize(arguments); |
| 55 |
LockableItem::Deserialize(arguments); |
| 56 |
SetSizeFromType(); |
| 57 |
SetVisible(VisibleYes); |
| 58 |
} |
| 59 |
|
| 60 |
void Switch::SetAccessoryState(const AccessoryState state) |
| 61 |
{ |
| 62 |
AccessoryState checkedState = state; |
| 63 |
if (GetAccessoryType() != SwitchTypeThreeWay && state == SwitchStateThird) |
| 64 |
{ |
| 65 |
checkedState = SwitchStateTurnout; |
| 66 |
} |
| 67 |
AccessoryBase::SetAccessoryState(checkedState); |
| 68 |
} |
| 69 |
|
| 70 |
bool Switch::UsesAddress(Address address) const |
| 71 |
{ |
| 72 |
switch(GetAccessoryType()) |
| 73 |
{ |
| 74 |
case SwitchTypeThreeWay: |
| 75 |
return (GetAddress() == address) |
| 76 |
|| (GetAddress() + 1 == address); |
| 77 |
|
| 78 |
case SwitchTypeLeft: |
| 79 |
case SwitchTypeRight: |
| 80 |
case SwitchTypeMaerklinLeft: |
| 81 |
case SwitchTypeMaerklinRight: |
| 82 |
default: |
| 83 |
return (GetAddress() == address); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
std::map<DataModel::AccessoryState, Languages::TextSelector> Switch::GetStateOptions() const |
| 88 |
{ |
| 89 |
std::map<DataModel::AccessoryState,Languages::TextSelector> out; |
| 90 |
out[DataModel::SwitchStateStraight] = Languages::TextStraight; |
| 91 |
switch(GetAccessoryType()) |
| 92 |
{ |
| 93 |
case DataModel::SwitchTypeThreeWay: |
| 94 |
out[DataModel::SwitchStateTurnout] = Languages::TextLeft; |
| 95 |
out[DataModel::SwitchStateThird] = Languages::TextRight; |
| 96 |
break; |
| 97 |
|
| 98 |
default: |
| 99 |
out[DataModel::SwitchStateTurnout] = Languages::TextTurnout; |
| 100 |
break; |
| 101 |
} |
| 102 |
return out; |
| 103 |
} |
| 104 |
|
| 105 |
AccessoryState Switch::CalculateInvertedSwitchState(const Address address, const AccessoryState state) const |
| 106 |
{ |
| 107 |
if (!GetInverted()) |
| 108 |
{ |
| 109 |
if ((GetAddress() + 1) == address && state == SwitchStateTurnout) |
| 110 |
{ |
| 111 |
return SwitchStateThird; |
| 112 |
} |
| 113 |
return state; |
| 114 |
} |
| 115 |
|
| 116 |
switch(state) |
| 117 |
{ |
| 118 |
case SwitchStateTurnout: |
| 119 |
case SwitchStateThird: |
| 120 |
return SwitchStateStraight; |
| 121 |
|
| 122 |
case SwitchStateStraight: |
| 123 |
return GetAddress() == address ? SwitchStateTurnout : SwitchStateThird; |
| 124 |
|
| 125 |
default: |
| 126 |
return state; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
DataModel::LayoutItem::LayoutItemSize Switch::CalculateHeightFromType(AccessoryType type) |
| 131 |
{ |
| 132 |
switch (type) |
| 133 |
{ |
| 134 |
case SwitchTypeMaerklinLeft: |
| 135 |
case SwitchTypeMaerklinRight: |
| 136 |
return 2; |
| 137 |
|
| 138 |
default: |
| 139 |
return Height1; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
Switch& Switch::operator=(const Hardware::AccessoryCacheEntry& accessory) |
| 144 |
{ |
| 145 |
SetControlID(accessory.GetControlID()); |
| 146 |
SetName(accessory.GetName()); |
| 147 |
SetAddress(accessory.GetAddress()); |
| 148 |
SetProtocol(accessory.GetProtocol()); |
| 149 |
SetMatchKey(accessory.GetMatchKey()); |
| 150 |
return *this; |
| 151 |
} |
| 152 |
} // namespace DataModel |