Debian Codegraph @

Server/Web/HtmlTagLayoutItem.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 <string>
22
23 #include "Server/Web/HtmlTagLayoutItem.h"
24
25 using std::string;
26 using std::to_string;
27
28 namespace Server { namespace Web
29 {
30 const string HtmlTagLayoutItem::EdgeLengthString = std::to_string(HtmlTagLayoutItem::EdgeLength);
31
32 HtmlTagLayoutItem::HtmlTagLayoutItem(const DataModel::LayoutItem* layout,
33 const DataModel::LayoutItem::LayoutPosition posX,
34 const DataModel::LayoutItem::LayoutPosition posY)
35 : layout(layout),
36 imageDiv("div"),
37 onClickMenuDiv("div"),
38 onClickMenuContentDiv("ul"),
39 contextMenuDiv("div"),
40 contextMenuContentDiv("ul"),
41 layoutPosX(posX * EdgeLength),
42 layoutPosY(posY * EdgeLength)
43 {
44 switch (layout->GetObjectType())
45 {
46 case ObjectTypeAccessory:
47 identifier = "a_";
48 break;
49
50 case ObjectTypeFeedback:
51 identifier = "f_";
52 break;
53
54 case ObjectTypeRoute:
55 identifier = "r_";
56 break;
57
58 case ObjectTypeTrack:
59 identifier = "t_";
60 break;
61
62 case ObjectTypeSignal:
63 identifier = "si_";
64 break;
65
66 case ObjectTypeSwitch:
67 identifier = "sw_";
68 break;
69
70 case ObjectTypeText:
71 identifier = "tx_";
72 break;
73
74 default:
75 identifier = "unknown_";
76 return;
77 }
78 identifier += to_string(layout->GetID());
79 imageDiv.AddId(identifier);
80 imageDiv.AddClass("layout_item");
81 imageDiv.AddAttribute("style", "left:" + to_string(layoutPosX) + "px;top:" + to_string(layoutPosY) + "px;");
82 imageDiv.AddAttribute("draggable", "true");
83 imageDiv.AddAttribute("ondragstart", "drag(event);");
84
85 string menuPosition = "left:" + to_string(layoutPosX + 5) + "px;top:" + to_string(layoutPosY + 30) + "px;";
86 onClickMenuDiv.AddAttribute("style", menuPosition);
87 contextMenuDiv.AddAttribute("style", menuPosition);
88 }
89
90 void HtmlTagLayoutItem::FinishInit()
91 {
92 const DataModel::LayoutItem::LayoutItemSize trackHeight = layout->GetHeight();
93 const DataModel::LayoutItem::LayoutItemSize trackWidth = layout->GetWidth();
94
95 int translate = 0;
96 if (trackHeight > DataModel::LayoutItem::Height1 || trackWidth > DataModel::LayoutItem::Width1)
97 {
98 DataModel::LayoutItem::LayoutRotation rotation = layout->GetRotation();
99 if (rotation == DataModel::LayoutItem::Rotation90 || rotation == DataModel::LayoutItem::Rotation270)
100 {
101 translate = (((trackHeight - trackWidth) * EdgeLength) / 2);
102 }
103 if (rotation == DataModel::LayoutItem::Rotation90)
104 {
105 translate = -translate;
106 }
107 }
108
109 const string layoutHeight = to_string(EdgeLength * trackHeight);
110 const string layoutWidth = to_string(EdgeLength * trackWidth);
111 const string translateText = to_string(translate);
112 imageDiv.AddChildTag(HtmlTag().AddContent("<svg width=\"" + layoutWidth + "\" height=\"" + layoutHeight + "\" id=\"" + identifier + "_img\" style=\"transform:rotate(" + DataModel::LayoutItem::Rotation(layout->GetRotation()) + "deg) translate(" + translateText + "px," + translateText + "px);\">" + image + "</svg>"));
113
114 if (onClickMenuContentDiv.ChildCount())
115 {
116 static const string OnClick("onclick");
117 if (!imageDiv.IsAttributeSet(OnClick))
118 {
119 imageDiv.AddAttribute(OnClick, "return showOnClickMenu(event, '" + identifier + "');");
120 }
121 onClickMenuContentDiv.AddClass("contextentries");
122 onClickMenuDiv.AddChildTag(onClickMenuContentDiv);
123 onClickMenuDiv.AddClass("contextmenu");
124 onClickMenuDiv.AddId(identifier + "_onclick");
125 AddChildTag(onClickMenuDiv);
126 }
127 if (contextMenuContentDiv.ChildCount())
128 {
129 imageDiv.AddAttribute("oncontextmenu", "return showContextMenu(event, '" + identifier + "');");
130 contextMenuContentDiv.AddClass("contextentries");
131 contextMenuDiv.AddChildTag(contextMenuContentDiv);
132 contextMenuDiv.AddClass("contextmenu");
133 contextMenuDiv.AddId(identifier + "_context");
134 AddChildTag(contextMenuDiv);
135 }
136 AddChildTag(imageDiv);
137 }
138
139 void HtmlTagLayoutItem::AddMenuTitle(HtmlTag& menu,
140 const string& text)
141 {
142 HtmlTag li("li");
143 li.AddClass("contexttitle");
144 li.AddContent(text);
145 menu.AddChildTag(li);
146 }
147
148 void HtmlTagLayoutItem::AddMenuEntry(HtmlTag& menu,
149 const string& text,
150 const string& onClick,
151 const string& className)
152 {
153 HtmlTag li("li");
154 li.AddClass("contextentry");
155 li.AddContent(text);
156 if (onClick.length() > 0)
157 {
158 li.AddAttribute("onClick", onClick);
159 }
160 if (className.length() > 0)
161 {
162 li.AddClass(className);
163 }
164 menu.AddChildTag(li);
165 }
166 }} // namespace Server::Web