Debian Codegraph @

src/Gui/3Dconnexion/navlib/NavlibCmds.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2014-2023 3Dconnexion. *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include "NavlibInterface.h"
24
25 #include <QScrollBar>
26 #include <QString>
27 #include <QBuffer>
28
29 #include <algorithm>
30 #include <iterator>
31
32 #include <SpaceMouse/CCategory.hpp>
33 #include <SpaceMouse/CCommand.hpp>
34 #include <SpaceMouse/CCommandSet.hpp>
35 #include <SpaceMouse/CImage.hpp>
36
37 #include <Gui/Action.h>
38 #include <Gui/Application.h>
39 #include <Gui/Command.h>
40 #include <Gui/MainWindow.h>
41 #include <Gui/View3DInventor.h>
42 #include <Gui/Workbench.h>
43 #include <Gui/WorkbenchManager.h>
44
45 #include <gsl/pointers>
46
47 constexpr uint8_t LCD_ICON_SIZE = 24u;
48
49 NavlibInterface::ParsedData NavlibInterface::parseCommandId(const std::string& commandId) const
50 {
51 ParsedData result{"", "", 0};
52
53 if (commandId.empty())
54 return result;
55
56 auto groupDelimiter = std::find(commandId.cbegin(), commandId.cend(), '|');
57 if (groupDelimiter == commandId.cend())
58 return result;
59
60 result.groupName = std::string(commandId.cbegin(), groupDelimiter);
61 groupDelimiter++;
62
63 auto commandDelimiter = std::find(groupDelimiter, commandId.cend(), '|');
64
65 result.commandName = std::string(groupDelimiter, commandDelimiter);
66 result.actionIndex = -1;
67
68 if (commandDelimiter != commandId.cend()) {
69 QString indexString =
70 QString::fromStdString(std::string(++commandDelimiter, commandId.cend()).c_str());
71
72 result.actionIndex = indexString.toInt();
73 }
74
75 return result;
76 }
77
78 std::string NavlibInterface::getId(const Gui::Command& command, const int32_t parameter) const
79 {
80 std::string name = std::string(command.getName());
81
82 if (parameter != -1) {
83 name.push_back('|');
84 name.append(std::to_string(parameter));
85 }
86
87 std::string groupName = command.getGroupName();
88
89 if (groupName.compare("<string>") == 0 || groupName.empty())
90 groupName = "Others";
91
92 return groupName + "|" + name;
93 }
94
95 TDx::CImage NavlibInterface::getImage(const QAction& qaction, const std::string& id) const
96 {
97 const QIcon iconImg = qaction.icon();
98 const QImage qimage = iconImg.pixmap(QSize(LCD_ICON_SIZE, LCD_ICON_SIZE)).toImage();
99 QByteArray qbyteArray;
100 QBuffer qbuffer(&qbyteArray);
101 qimage.save(&qbuffer, "PNG");
102
103 return TDxImage::FromData(qbyteArray.toStdString(), 0, id.c_str());
104 }
105
106 void NavlibInterface::removeMarkups(std::string& text) const
107 {
108 for (auto textBegin = text.cbegin(); textBegin != text.cend();) {
109 auto markupBegin = std::find(textBegin, text.cend(), '<');
110 if (markupBegin == text.cend())
111 return;
112
113 auto markupEnd = std::find(markupBegin, text.cend(), '>');
114 if (markupEnd == text.cend())
115 return;
116
117 const char enclosingChar = *std::prev(markupEnd);
118 textBegin = text.erase(markupBegin, ++markupEnd);
119
120 if(enclosingChar == 'p')
121 textBegin = text.insert(textBegin, 2, '\n');
122 }
123 }
124
125 TDxCommand NavlibInterface::getCCommand(const Gui::Command& command,
126 const QAction& qAction,
127 const int32_t parameter) const
128 {
129 std::string commandName = qAction.text().toStdString();
130 std::string commandId = getId(command, parameter);
131
132 if (commandName.empty() || commandId.empty())
133 return TDxCommand();
134
135 gsl::not_null<const char*> commandToolTip =
136 command.getToolTipText() ? command.getToolTipText() : "";
137
138 std::string commandDescription =
139 parameter == -1 ? std::string(commandToolTip) : qAction.toolTip().toStdString();
140
141 auto newEnd = std::remove(commandName.begin(), commandName.end(), '&');
142 commandName.erase(newEnd, commandName.end());
143 removeMarkups(commandDescription);
144
145 return TDxCommand(commandId, commandName, commandDescription);
146 }
147
148 long NavlibInterface::SetActiveCommand(std::string commandId)
149 {
150 ParsedData parsedData = parseCommandId(commandId);
151 auto& commandManager = Gui::Application::Instance->commandManager();
152
153 if (parsedData.groupName.compare("Others")) {
154 auto commands = commandManager.getGroupCommands(parsedData.groupName.c_str());
155
156 for (Gui::Command* command : commands) {
157 if (!std::string(command->getName()).compare(parsedData.commandName)) {
158 if (parsedData.actionIndex == -1) {
159 Gui::Action* pAction = command->getAction();
160 if (pAction != nullptr) {
161 pAction->action()->trigger();
162 }
163 }
164 else
165 command->invoke(parsedData.actionIndex);
166 return 0;
167 }
168 }
169 }
170 else
171 commandManager.runCommandByName(parsedData.commandName.c_str());
172
173 return 0;
174 }
175
176 void NavlibInterface::unpackCommands(Gui::Command& command,
177 TDxCategory& category,
178 std::vector<TDx::CImage>& images)
179 {
180 if (command.getAction() == nullptr)
181 return;
182
183 QList<QAction*> pQActions;
184 TDxCategory subCategory;
185 int32_t index = -1;
186 auto actionGroup = qobject_cast<Gui::ActionGroup*>(command.getAction());
187
188 if (actionGroup != nullptr) {
189 pQActions = actionGroup->actions();
190 std::string subCategoryName = actionGroup->text().toStdString();
191 subCategory = TDxCategory(subCategoryName, subCategoryName);
192 index = 0;
193 }
194 else
195 pQActions.push_back(command.getAction()->action());
196
197 for (QAction* pQAction : pQActions) {
198 if (pQAction->isSeparator())
199 continue;
200
201 TDxCommand ccommand = getCCommand(command, *pQAction, index);
202
203 if (ccommand.GetId().empty())
204 continue;
205
206 if (!pQAction->icon().isNull()) {
207 TDxImage commandImage = getImage(*pQAction, ccommand.GetId());
208 images.push_back(commandImage);
209 }
210
211 if (pQActions.size() > 1)
212 subCategory.push_back(std::move(ccommand));
213 else {
214 category.push_back(std::move(ccommand));
215 return;
216 }
217 index++;
218 }
219 category.push_back(std::move(subCategory));
220 }
221
222 void NavlibInterface::exportCommands(const std::string& workbench)
223 {
224 if (errorCode || (workbench.compare(noneWorkbenchStr) == 0))
225 return;
226
227 auto exportedSetItr =
228 std::find(exportedCommandSets.cbegin(), exportedCommandSets.cend(), workbench);
229 if (exportedSetItr != exportedCommandSets.cend()) {
230 PutActiveCommands(workbench);
231 return;
232 }
233
234 std::string shortName(workbench);
235 size_t index = shortName.find(workbenchStr);
236
237 if (index != std::string::npos)
238 shortName.erase(index, workbenchStr.size());
239
240 auto guiCommands = Gui::Application::Instance->commandManager().getAllCommands();
241
242 std::vector<TDxImage> images;
243 std::unordered_map<std::string, TDxCategory> categories;
244
245 for (Gui::Command* command : guiCommands) {
246 std::string groupName(command->getGroupName());
247 std::string groupId(groupName);
248
249 if ((groupName.compare("<string>") == 0) || groupName.empty()) {
250 groupName = "...";
251 groupId = "Others";
252 }
253
254 categories.emplace(groupName, TDxCategory(groupId, groupName));
255 unpackCommands(*command, categories.at(groupName), images);
256 }
257
258 TDxCommandSet commandsSet(workbench, shortName);
259
260 for (auto& itr : categories)
261 commandsSet.push_back(std::move(itr.second));
262
263 CNav3D::AddCommandSet(commandsSet);
264 CNav3D::PutActiveCommands(workbench);
265 CNav3D::AddImages(images);
266 exportedCommandSets.push_back(workbench);
267 }