Debian Codegraph @

Server/Web/HtmlTagLayoutItem.cpp

raw symbols ◀ ▶ SCIP
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 case ObjectTypeCounter:
75 identifier = "c_";
76 break;
77
78 default:
79 identifier = "unknown_";
80 return;
81 }
82 identifier += to_string(layout->GetID());
83 imageDiv.AddId(identifier);
84 imageDiv.AddClass("layout_item");
85 imageDiv.AddAttribute("style", "left:" + to_string(layoutPosX) + "px;top:" + to_string(layoutPosY) + "px;");
86 imageDiv.AddAttribute("draggable", "true");
87 imageDiv.AddAttribute("ondragstart", "drag(event);");
88
89 string menuPosition = "left:" + to_string(layoutPosX + 5) + "px;top:" + to_string(layoutPosY + 30) + "px;";
90 onClickMenuDiv.AddAttribute("style", menuPosition);
91 contextMenuDiv.AddAttribute("style", menuPosition);
92 }
93
94 void HtmlTagLayoutItem::FinishInit()
95 {
96 const DataModel::LayoutItem::LayoutItemSize trackHeight = layout->GetHeight();
97 const DataModel::LayoutItem::LayoutItemSize trackWidth = layout->GetWidth();
98
99 int translate = 0;
100 if (trackHeight > DataModel::LayoutItem::Height1 || trackWidth > DataModel::LayoutItem::Width1)
101 {
102 DataModel::LayoutItem::LayoutRotation rotation = layout->GetRotation();
103 if (rotation == DataModel::LayoutItem::Rotation90 || rotation == DataModel::LayoutItem::Rotation270)
104 {
105 translate = (((trackHeight - trackWidth) * EdgeLength) / 2);
106 }
107 if (rotation == DataModel::LayoutItem::Rotation90)
108 {
109 translate = -translate;
110 }
111 }
112
113 const string layoutHeight = to_string(EdgeLength * trackHeight);
114 const string layoutWidth = to_string(EdgeLength * trackWidth);
115 const string translateText = to_string(translate);
116 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>"));
117
118 if (onClickMenuContentDiv.ChildCount())
119 {
120 static const string OnClick("onclick");
121 if (!imageDiv.IsAttributeSet(OnClick))
122 {
123 imageDiv.AddAttribute(OnClick, "return showOnClickMenu(event, '" + identifier + "');");
124 }
125 onClickMenuContentDiv.AddClass("contextentries");
126 onClickMenuDiv.AddChildTag(onClickMenuContentDiv);
127 onClickMenuDiv.AddClass("contextmenu");
128 onClickMenuDiv.AddId(identifier + "_onclick");
129 AddChildTag(onClickMenuDiv);
130 }
131 if (contextMenuContentDiv.ChildCount())
132 {
133 imageDiv.AddAttribute("oncontextmenu", "return showContextMenu(event, '" + identifier + "');");
134 contextMenuContentDiv.AddClass("contextentries");
135 contextMenuDiv.AddChildTag(contextMenuContentDiv);
136 contextMenuDiv.AddClass("contextmenu");
137 contextMenuDiv.AddId(identifier + "_context");
138 AddChildTag(contextMenuDiv);
139 }
140 AddChildTag(imageDiv);
141 }
142
143 void HtmlTagLayoutItem::AddMenuTitle(HtmlTag& menu,
144 const string& text)
145 {
146 HtmlTag li("li");
147 li.AddClass("contexttitle");
148 li.AddContent(text);
149 menu.AddChildTag(li);
150 }
151
152 void HtmlTagLayoutItem::AddMenuEntry(HtmlTag& menu,
153 const string& text,
154 const string& onClick,
155 const string& className)
156 {
157 HtmlTag li("li");
158 li.AddClass("contextentry");
159 li.AddContent(text);
160 if (onClick.length() > 0)
161 {
162 li.AddAttribute("onClick", onClick);
163 }
164 if (className.length() > 0)
165 {
166 li.AddClass(className);
167 }
168 menu.AddChildTag(li);
169 }
170 }} // namespace Server::Web