ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
PythonPlugin.h
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 #pragma once
9 
10 #include "PythonConfig.h"
11 #include "PythonInterpreter.h"
12 #include "PythonPluginManager.h"
13 #include "ecvStdPluginInterface.h"
14 
15 #include <iostream>
16 #include <map>
17 #include <vector>
18 
19 class QListWidget;
20 class PythonEditor;
21 class PythonRepl;
22 class FileRunner;
23 class PackageManager;
26 
28 class PythonPlugin final : public QObject, public ccStdPluginInterface
29 {
30  Q_OBJECT
32 
33  Q_PLUGIN_METADATA(IID "ecvcorp.cloudviewer.plugin.PythonRuntime" FILE "../info.json")
34 
35  public:
36  explicit PythonPlugin(QObject *parent = nullptr);
37 
38  QList<QAction *> getActions() override;
39 
42  void registerCommands(ccCommandLineInterface *cmd) override;
43 
44  void setMainAppInterface(ecvMainAppInterface *app) override;
45 
46  void stop() override;
47 
48  PythonPlugin(const PythonPlugin &) = delete;
49  PythonPlugin(PythonPlugin &&) = delete;
50  PythonPlugin &operator=(const PythonPlugin &) = delete;
51  PythonPlugin &operator=(PythonPlugin &&) = delete;
52  ~PythonPlugin() noexcept override;
53 
54  private:
55  void finalizeInterpreter();
56  void showRepl();
57  void showEditor() const;
58  void showAboutDialog() const;
59  void showFileRunner() const;
60  void showPackageManager();
61  void showPythonActionLauncher() const;
62  void showSettings() const;
63  static void showDocumentation();
64 
65  // Script list function
66  void addScriptAction();
67  void addScript(QString path);
68  void executeScript(QString path);
69  void removeScript(QString name, QAction *self);
70 
71  void populatePluginSubMenu();
72 
73  void handlePluginActionClicked(bool checked);
74  void handlePythonExecutionStarted();
75  void handlePythonExecutionFinished();
76 
77  PythonConfig m_config{};
78  PythonInterpreter m_interp;
79  PythonPluginManager m_pluginManager;
80 
81  PythonRuntimeSettings *m_settings{nullptr};
82  PythonRepl *m_repl{nullptr};
83  PythonEditor *m_editor{nullptr};
84  FileRunner *m_fileRunner{nullptr};
85  PackageManager *m_packageManager{nullptr};
86  PythonActionLauncher *m_actionLauncher{nullptr};
87 
89  QAction *m_showEditor{nullptr};
90  QAction *m_showRepl{nullptr};
91  QAction *m_showDoc{nullptr};
92  QAction *m_showFileRunner{nullptr};
93  QAction *m_showAboutDialog{nullptr};
94  QAction *m_showPackageManager{nullptr};
95  QAction *m_showActionLauncher{nullptr};
96  QAction *m_showSettings{nullptr};
97 
99  QMenu *m_drawScriptRegister{nullptr};
100  QMenu *m_pluginsMenu{nullptr};
101 
103  std::map<const QAction *, const Runtime::RegisteredPlugin::Action *> m_pluginActions;
104 
106  QAction *m_addScript{nullptr};
107  QMenu *m_removeScript{nullptr};
108  std::map<QString, QAction *> m_scriptList;
109  QStringList m_savedPath;
110  QString m_saveFilePath;
111 };
PythonRuntimeSettings
Definition: PythonRuntimeSettings.h:16
bytes
void * bytes
Definition: TriangleMeshBuffers.cpp:159
PythonPlugin::getActions
QList< QAction * > getActions() override
Get a list of actions for this plugin.
Definition: PythonPlugin.cpp:136
PACKAGE_MANAGER_ICON_PATH
#define PACKAGE_MANAGER_ICON_PATH
Definition: Resources.h:19
Runtime.h
PythonPluginManager::loadPluginsFrom
void loadPluginsFrom(const QStringList &paths)
Definition: PythonPluginManager.cpp:117
SETTINGS_ICON_PATH
#define SETTINGS_ICON_PATH
Definition: Resources.h:23
PythonInterpreter::executionStarted
void executionStarted()
FileRunner
Definition: FileRunner.h:22
PythonPluginCommand::Args::pythonArgv
std::vector< wchar_t * > pythonArgv
Definition: PythonPlugin.cpp:466
format
filament::Texture::InternalFormat format
Definition: FilamentResourceManager.cpp:181
PYPLUGIN_ICON_PATH
#define PYPLUGIN_ICON_PATH
Definition: Resources.h:24
ecvMainAppInterface::getMainWindow
virtual QMainWindow * getMainWindow()=0
Returns main window.
plgError
PluginLogger< CVLog::LOG_ERROR > plgError
Definition: Utilities.h:103
ccCommandLineInterface::error
virtual bool error(const QString &message) const =0
PythonActionLauncher.h
PythonConfig::type
Type type() const
Definition: PythonConfig.h:84
QStringToWcharArray
wchar_t * QStringToWcharArray(const QString &string)
Returns a newly allocated wchar_t array (null terminated) from a QString.
Definition: Utilities.h:106
PythonPluginCommand::process
bool process(ccCommandLineInterface &cmd) override
Main process.
Definition: PythonPlugin.cpp:440
PackageManager.h
PythonPluginManager::loadPluginsFromEntryPoints
void loadPluginsFromEntryPoints()
Definition: PythonPluginManager.cpp:21
PythonPluginManager::plugins
const std::vector< Runtime::RegisteredPlugin > & plugins() const
Returns the currently loaded plugins.
Definition: PythonPluginManager.cpp:16
ccStdPluginInterface::setMainAppInterface
virtual void setMainAppInterface(ecvMainAppInterface *app)
Sets application entry point.
Definition: ecvStdPluginInterface.h:46
PythonInterpreter::finalize
void finalize()
Definition: PythonInterpreter.cpp:298
REPL_ICON_PATH
#define REPL_ICON_PATH
Definition: Resources.h:15
PythonConfig::initBundled
void initBundled()
Definition: PythonConfig.cpp:202
PythonPlugin::stop
void stop() override
Stops the plugin.
Definition: PythonPlugin.cpp:128
PythonPlugin::operator=
PythonPlugin & operator=(const PythonPlugin &)=delete
ecvCommandLineInterface.h
LoadSettings
static std::unique_ptr< QSettings > LoadSettings()
Definition: PythonPlugin.cpp:121
PythonPluginCommand::Args::Args
Args()=default
Runtime::unsetMainAppInterfaceInstance
void unsetMainAppInterfaceInstance() noexcept
Unsets the app interface pointer.
Definition: Runtime.cpp:90
ACTION_LAUNCHER_ICON_PATH
#define ACTION_LAUNCHER_ICON_PATH
Definition: Resources.h:21
size
int size
Definition: FileIOFactory.cpp:130
PythonEditor.h
Runtime::setMainAppInterfaceInstance
void setMainAppInterfaceInstance(ecvMainAppInterface *appInterface) noexcept(false)
Definition: Runtime.cpp:82
AboutDialog.h
ABOUT_ICON_PATH
#define ABOUT_ICON_PATH
Definition: Resources.h:13
PythonInterpreter
Definition: PythonInterpreter.h:32
ecvMainAppInterface
Main application interface (for plugins)
Definition: ecvMainAppInterface.h:24
PackageManager
Definition: PackageManager.h:22
AboutDialog
Definition: AboutDialog.h:14
Runtime::RegisteredPlugin::Action::name
QString name
Name to be displayed in the UI.
Definition: Runtime.h:32
PythonRuntimeSettings::isDefaultPythonEnv
bool isDefaultPythonEnv() const
Definition: PythonRuntimeSettings.cpp:276
cloudViewer::t::geometry::path
static const std::string path
Definition: PointCloud.cpp:59
e
const double * e
Definition: essential_matrix_poly.h:10
ccCommandLineInterface::Command::Shared
QSharedPointer< Command > Shared
Shared type.
Definition: ecvCommandLineInterface.h:132
PythonPluginCommand::Args::parseFrom
bool parseFrom(ccCommandLineInterface &cmd)
Definition: PythonPlugin.cpp:470
PYSCRIPTS_REGISTER_ICON_PATH
#define PYSCRIPTS_REGISTER_ICON_PATH
Definition: Resources.h:26
ccStdPluginInterface::m_app
ecvMainAppInterface * m_app
Main application interface.
Definition: ecvStdPluginInterface.h:85
ecvStdPluginInterface.h
PythonInterpreter::initialize
void initialize(const PythonConfig &config)
Definition: PythonInterpreter.cpp:173
SetIconFromPyObject
static bool SetIconFromPyObject(T *iconReceiver, const py::object &pyIcon)
Definition: PythonPlugin.cpp:618
PythonConfig::validateAndDisplayErrors
bool validateAndDisplayErrors(QWidget *parent=nullptr) const
Definition: PythonConfig.cpp:348
PythonPlugin::~PythonPlugin
~PythonPlugin() noexcept override
Definition: PythonPlugin.cpp:427
PythonInterpreter::executeFunction
void executeFunction(const pybind11::object &function)
Definition: PythonInterpreter.cpp:150
PythonPluginCommand::Args::filepath
QString filepath
Definition: PythonPlugin.cpp:465
LogPythonHome
void LogPythonHome()
Logs the PYTHON_HOME the log console of ACloudViewer.
Definition: Utilities.h:140
DOCUMENTATION_ICON_PATH
#define DOCUMENTATION_ICON_PATH
Definition: Resources.h:16
PythonActionLauncher
Definition: PythonActionLauncher.h:18
PythonConfig.h
PythonConfig
Definition: PythonConfig.h:71
PYSCRIPT_ICON_PATH
#define PYSCRIPT_ICON_PATH
Definition: Resources.h:32
ccCommandLineInterface::Command
Generic command interface.
Definition: ecvCommandLineInterface.h:130
PythonConfig::IsInsideEnvironment
static bool IsInsideEnvironment()
Definition: PythonConfig.cpp:380
PythonPluginCommand::Args::~Args
virtual ~Args()
Definition: PythonPlugin.cpp:488
ccCommandLineInterface::arguments
virtual QStringList & arguments()=0
Returns the list of arguments.
PythonPluginCommand
Definition: PythonPlugin.cpp:433
ccCommandLineInterface::print
virtual void print(const QString &message) const =0
EDITOR_ICON_PATH
#define EDITOR_ICON_PATH
Definition: Resources.h:14
PythonPlugin
"Entry point" of the plugin
Definition: PythonPlugin.h:28
Utilities.h
ADD_PYSCRIPT_ICON_PATH
#define ADD_PYSCRIPT_ICON_PATH
Definition: Resources.h:28
PythonPluginManager::unloadPlugins
void unloadPlugins()
This MUST be called before finalizing the interpreter.
Definition: PythonPluginManager.cpp:187
ccCommandLineInterface
Command line interface.
Definition: ecvCommandLineInterface.h:112
Runtime::RegisteredPlugin::Action::icon
pybind11::object icon
Optional path or (bytes, str) where str is the format.
Definition: Runtime.h:36
PythonConfig::fromContainingEnvironment
static PythonConfig fromContainingEnvironment()
Definition: PythonConfig.cpp:385
PythonPluginCommand::interpreter
PythonInterpreter * interpreter
Definition: PythonPlugin.cpp:497
ccPluginInterface
Standard ECV plugin interface.
Definition: ecvPluginInterface.h:33
LogPythonPath
void LogPythonPath()
Logs the PYTHON_PATH the log console of ACloudViewer.
Definition: Utilities.h:116
Runtime::unsetCmdLineInterfaceInstance
void unsetCmdLineInterfaceInstance() noexcept
Unsets the pointer to the cmdline app interface.
Definition: Runtime.cpp:107
plgPrint
PluginLogger< CVLog::LOG_STANDARD > plgPrint
Definition: Utilities.h:100
PythonInterpreter::executeFile
bool executeFile(const std::string &filePath)
Execution functions (and slots)
Definition: PythonInterpreter.cpp:52
ccCommandLineInterface::Command::Command
Command(const QString &name, const QString &keyword)
Default constructor.
Definition: ecvCommandLineInterface.cpp:225
CVLog::Warning
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
Definition: CVLog.cpp:133
PythonRepl
Homemade REPL (Read Print Eval Loop)
Definition: PythonRepl.h:58
PythonEditor
Definition: PythonEditor.h:50
PythonPluginCommand::PythonPluginCommand
PythonPluginCommand(PythonInterpreter *interpreter_)
Definition: PythonPlugin.cpp:435
plgWarning
PluginLogger< CVLog::LOG_WARNING > plgWarning
Definition: Utilities.h:102
PLUGIN_INFO_PATH
#define PLUGIN_INFO_PATH
Definition: Resources.h:12
PythonPlugin.h
Runtime::RegisteredPlugin::Action
Definition: Runtime.h:23
CreateQIconFromPyObject
static QIcon CreateQIconFromPyObject(const py::object &pyIcon)
Definition: PythonPlugin.cpp:554
PythonPlugin::setMainAppInterface
void setMainAppInterface(ecvMainAppInterface *app) override
Sets application entry point.
Definition: PythonPlugin.cpp:509
REMOVE_PYSCRIPT_ICON_PATH
#define REMOVE_PYSCRIPT_ICON_PATH
Definition: Resources.h:30
PythonPlugin::PythonPlugin
PythonPlugin(QObject *parent=nullptr)
Definition: PythonPlugin.cpp:36
PythonInterpreter::IsInitialized
static bool IsInitialized()
Definition: PythonInterpreter.cpp:293
ccCommandLineInterface::registerCommand
virtual bool registerCommand(Command::Shared command)=0
Registers a new command.
PythonRuntimeSettings::pluginsPaths
QStringList pluginsPaths() const
Definition: PythonRuntimeSettings.cpp:244
PythonPluginManager
Definition: PythonPluginManager.h:16
ccStdPluginInterface
Standard ECV plugin interface.
Definition: ecvStdPluginInterface.h:31
PythonInterpreter.h
Runtime::setCmdLineInterfaceInstance
void setCmdLineInterfaceInstance(ccCommandLineInterface *cmdLine) noexcept
Definition: Runtime.cpp:99
PythonPluginCommand::Args
Definition: PythonPlugin.cpp:463
PythonRepl.h
FileRunner.h
Resources.h
RUNNER_ICON_PATH
#define RUNNER_ICON_PATH
Definition: Resources.h:18
PythonRuntimeSettings::pythonEnvConfig
PythonConfig pythonEnvConfig() const
Definition: PythonRuntimeSettings.cpp:249
PythonInterpreter::executionFinished
void executionFinished()
Runtime::RegisteredPlugin
Definition: Runtime.h:21
PythonPlugin::registerCommands
void registerCommands(ccCommandLineInterface *cmd) override
Definition: PythonPlugin.cpp:500
PythonPluginManager.h
name
std::string name
Definition: FileIOFactory.cpp:129
PythonRuntimeSettings.h