ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
JsonRPCPlugin.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include <QtGui>
9 
10 // local
11 #include "JsonRPCPlugin.h"
12 
13 // CV_DB_LIB
14 #include <ecvDisplayTools.h>
15 #include <ecvGenericPointCloud.h>
16 
17 // CV_IO_LIB
18 #include <FileIOFilter.h>
19 
20 // ECV plugins
21 #include <ecvMainAppInterface.h>
22 
23 JsonRPCPlugin::JsonRPCPlugin(QObject* parent)
24  : QObject(parent),
25  ccStdPluginInterface(":/CC/plugin/JsonRPCPlugin/info.json") {
26  CVLog::PrintDebug("JsonRPCPlugin::JsonRPCPlugin");
27 
28  connect(&rpc_server, &JsonRPCServer::execute, this,
30 }
31 
32 QList<QAction*> JsonRPCPlugin::getActions() {
33  CVLog::PrintDebug("JsonRPCPlugin::getActions");
34 
35  // default action (if it has not been already created, this is the moment to
36  // do it)
37  if (!m_action) {
38  m_action = new QAction(getName(), this);
39  m_action->setToolTip(getDescription());
40  m_action->setIcon(getIcon());
41  m_action->setCheckable(true);
42  m_action->setChecked(false);
43  m_action->setEnabled(true);
44 
45  // Connect appropriate signal
46  connect(m_action, &QAction::triggered, this, &JsonRPCPlugin::triggered);
47  }
48 
49  return {m_action};
50 }
51 
52 void JsonRPCPlugin::triggered(bool checked) {
53  qDebug() << "JsonRPCPlugin::triggered " << checked;
54 
55  if (checked) {
56  rpc_server.listen(6001);
57  } else {
58  rpc_server.close();
59  }
60 }
61 
63  QMap<QString, QVariant> params) {
64  qDebug() << method << params;
65  if (m_app == nullptr) {
66  return JsonRPCResult();
67  }
68 
70  bool need_redraw = false;
71  if (method == "open") {
72  QString filename = params["filename"].toString();
73  // code copied from MainWindow::addToDB()
74  // to use the same 'global shift' for multiple files
75  CCVector3d loadCoordinatesShift(0, 0, 0);
76  bool loadCoordinatesTransEnabled = false;
77 
79  {
80  parameters.alwaysDisplayLoadDialog = true;
81  parameters.shiftHandlingMode =
83  parameters.coordinatesShift = &loadCoordinatesShift;
84  parameters.coordinatesShiftEnabled = &loadCoordinatesTransEnabled;
85  parameters.parentWidget = m_app->getActiveWindow();
86  }
87 
88  if (params.contains("silent")) {
89  parameters.alwaysDisplayLoadDialog = false;
90  }
93  filename, parameters, res, params["filter"].toString());
94 
95  if (newGroup) {
96  // disable the normals on all loaded clouds!
97  ccHObject::Container clouds;
98  newGroup->filterChildren(clouds, true, CV_TYPES::POINT_CLOUD);
99  for (ccHObject* cloud : clouds) {
100  if (cloud) {
101  static_cast<ccGenericPointCloud*>(cloud)->showNormals(
102  false);
103  }
104  }
105 
106  // apply matrix if possible
107  QList<QVariant> transformation = params["transformation"].toList();
108  if (transformation.size() == 4 * 4) {
109  std::vector<double> values(4 * 4);
110  bool success;
111  for (unsigned i = 0; i < 4 * 4; ++i) {
112  double d = transformation[i].toDouble(&success);
113  qDebug() << transformation[i].toString()
114  << transformation[i].toDouble();
115  if (!success) {
116  break;
117  }
118  values[((i % 4) * 4) + (i / 4)] = d;
119  }
120  if (success) {
121  ccGLMatrix mat = ccGLMatrix(values.data());
122  qDebug() << "apply matrix: " << mat.toString();
123  newGroup->setGLTransformation(mat);
125  }
126  }
127 
128  m_app->addToDB(newGroup);
129  need_redraw = true;
131  } else {
132  result = JsonRPCResult::error(1, "cancelled by user");
133  }
134  } else if (method == "clear") {
135  // remove everything below root
136  auto root = m_app->dbRootObject();
137  ccHObject* child;
138  while ((child = root->getChild(0)) != nullptr) {
139  m_app->removeFromDB(child, true);
140  }
141  need_redraw = true;
143  }
144 
145  // redraw
146  if (need_redraw) {
147  QWidget* win = m_app->getActiveWindow();
148  if (win) {
150  }
151  }
152 
153  return result;
154 }
std::string filename
CC_FILE_ERROR
Typical I/O filter errors.
Definition: FileIOFilter.h:20
@ CC_FERR_NO_ERROR
Definition: FileIOFilter.h:21
cmdLineReadable * params[]
core::Tensor result
Definition: VtkUtils.cpp:76
static bool PrintDebug(const char *format,...)
Same as Print, but works only in Debug mode.
Definition: CVLog.cpp:153
static ccHObject * LoadFromFile(const QString &filename, LoadParameters &parameters, Shared filter, CC_FILE_ERROR &result)
Loads one or more entities from a file with a known filter.
JsonRPCResult execute(QString method, QMap< QString, QVariant > params)
void triggered(bool checked)
JsonRPCServer rpc_server
Definition: JsonRPCPlugin.h:66
JsonRPCResult execute(QString method, QMap< QString, QVariant > params)
void listen(unsigned int port)
virtual QString getName() const override
Returns (short) name (for menu entry, etc.)
virtual QString getDescription() const override
Returns long name/description (for tooltip, etc.)
virtual QIcon getIcon() const override
Returns icon.
virtual void setGLTransformation(const ccGLMatrix &trans)
Associates entity with a GL transformation (rotation + translation)
QString toString(int precision=12, QChar separator=' ') const
Returns matrix as a string.
Float version of ccGLMatrixTpl.
Definition: ecvGLMatrix.h:19
A 3D cloud interface with associated features (color, normals, octree, etc.)
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
void applyGLTransformation_recursive(const ccGLMatrix *trans=nullptr)
Applies the active OpenGL transformation to the entity (recursive)
unsigned filterChildren(Container &filteredChildren, bool recursive=false, CV_CLASS_ENUM filter=CV_TYPES::OBJECT, bool strict=false) const
Collects the children corresponding to a certain pattern.
std::vector< ccHObject * > Container
Standard instances container (for children, etc.)
Definition: ecvHObject.h:337
ccHObject * getChild(unsigned childPos) const
Returns the ith child.
Definition: ecvHObject.h:325
Standard ECV plugin interface.
ecvMainAppInterface * m_app
Main application interface.
static void RedrawDisplay(bool only2D=false, bool forceRedraw=true)
virtual ccHObject * dbRootObject()=0
Returns DB root (as a ccHObject)
virtual QWidget * getActiveWindow()=0
virtual void addToDB(ccHObject *obj, bool updateZoom=false, bool autoExpandDBTree=true, bool checkDimensions=false, bool autoRedraw=true)=0
virtual void removeFromDB(ccHObject *obj, bool autoDelete=true)=0
Removes an entity from main db tree.
@ POINT_CLOUD
Definition: CVTypes.h:104
std::string toString(T x)
Definition: Common.h:80
Generic loading parameters.
Definition: FileIOFilter.h:51
CCVector3d * coordinatesShift
If applicable, applied shift on load (optional)
Definition: FileIOFilter.h:71
ecvGlobalShiftManager::Mode shiftHandlingMode
How to handle big coordinates.
Definition: FileIOFilter.h:64
QWidget * parentWidget
Parent widget (if any)
Definition: FileIOFilter.h:78
bool * coordinatesShiftEnabled
Whether shift on load has been applied after loading (optional)
Definition: FileIOFilter.h:69
static JsonRPCResult success(QVariant value)
Definition: jsonrpcserver.h:29
static JsonRPCResult error(int code, QString message)
Definition: jsonrpcserver.h:19