ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
PythonActionLauncher.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 "PythonActionLauncher.h"
9 #include "PythonInterpreter.h"
10 #include "Resources.h"
11 #include <ui_ActionLauncher.h>
12 
13 #include <QIcon>
14 #include <QVBoxLayout>
15 
16 #undef slots
17 #include "PythonPluginManager.h"
19 
22 class PluginListModel final : public QAbstractListModel
23 {
24  Q_OBJECT
25 
26  public:
28  : QAbstractListModel(parent), plugin(plugin)
29  {
30  }
31 
32  int rowCount(const QModelIndex &parent) const override
33  {
34  Q_ASSERT(plugin != nullptr);
35  return static_cast<int>(plugin->actions.size());
36  };
37 
38  QVariant data(const QModelIndex &index, int role) const override
39  {
40  Q_ASSERT(plugin != nullptr);
41  if (role != Qt::DisplayRole || !index.isValid() || index.row() >= plugin->actions.size())
42  {
43  return {};
44  }
45  return plugin->actions[index.row()].name;
46  };
47 
48  void handleDoubleClick(const QModelIndex &index)
49  {
50  if (!index.isValid())
51  {
52  return;
53  }
54 
55  PythonInterpreter *interpreter =
56  static_cast<PythonActionLauncher *>(parent())->m_interpreter;
57  interpreter->executeFunction(plugin->actions[index.row()].target);
58  }
59 
60  private:
61  const Runtime::RegisteredPlugin *plugin{nullptr};
62 };
63 
65  PythonInterpreter *interpreter,
66  QWidget *parent)
67  : QWidget(parent),
68  m_ui(new Ui_ActionLauncher),
69  m_pluginManager(pluginManager),
70  m_interpreter(interpreter)
71 {
72  setWindowTitle("ActionLauncher");
73  m_ui->setupUi(this);
74  connect(
76  connect(
78 
79  setWindowIcon(QIcon(ACTION_LAUNCHER_ICON_PATH));
80 }
81 
83 {
84  clearToolBox();
86  QWidget::showEvent(event);
87 }
88 
90 {
91  m_ui->toolBox->setDisabled(true);
92 }
94 {
95  m_ui->toolBox->setDisabled(false);
96 }
97 
99 {
100  QToolBox *toolBox = m_ui->toolBox;
101  for (int i{toolBox->count() - 1}; i >= 0; --i)
102  {
103  QWidget *widget = toolBox->widget(i);
104  toolBox->removeItem(i);
105  delete widget;
106  }
107 }
108 
110 {
111  QToolBox *toolBox = m_ui->toolBox;
112  for (const Runtime::RegisteredPlugin &plugin : m_pluginManager->plugins())
113  {
114  if (plugin.actions.empty())
115  {
116  continue;
117  }
118 
119  auto *view = new QListView(this);
120  auto *model = new PluginListModel(&plugin, this);
121  connect(view, &QListView::doubleClicked, model, &PluginListModel::handleDoubleClick);
122  view->setModel(model);
123 
124  toolBox->addItem(view, plugin.name);
125  }
126 }
127 
128 #include "PythonActionLauncher.moc"
MouseEvent event
#define ACTION_LAUNCHER_ICON_PATH
Definition: Resources.h:21
int rowCount(const QModelIndex &parent) const override
void handleDoubleClick(const QModelIndex &index)
QVariant data(const QModelIndex &index, int role) const override
PluginListModel(const Runtime::RegisteredPlugin *plugin, PythonActionLauncher *parent)
void showEvent(QShowEvent *event) override
PythonActionLauncher(const PythonPluginManager *pluginManager, PythonInterpreter *interpreter, QWidget *parent=nullptr)
void executeFunction(const pybind11::object &function)
void executionFinished()
const std::vector< Runtime::RegisteredPlugin > & plugins() const
Returns the currently loaded plugins.
std::vector< Action > actions
Definition: Runtime.h:68