From da3e1fad2998b30663bf4ea45d09913f7005bb9f Mon Sep 17 00:00:00 2001 From: xarkes Date: Sun, 8 Jul 2018 10:49:20 +0200 Subject: [PATCH] Cleaned a bit and added a widget example --- src/MainWindow.cpp | 2 +- src/plugins/CutterPlugin.h | 2 +- src/plugins/CutterPythonPlugin.cpp | 10 ++++-- src/plugins/CutterPythonPlugin.h | 2 +- src/plugins/pluginSample.py | 54 ++++++++++++++++++++---------- src/python/cutter_plugin.py | 20 +++++++---- 6 files changed, 61 insertions(+), 29 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 4e03d340..64668c5c 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -278,7 +278,7 @@ void MainWindow::initUI() /* Setup plugins interfaces */ QList plugins = core->getCutterPlugins(); for (auto plugin : plugins) { - CutterDockWidget *pluginDock = plugin->setupInterface(this); + QDockWidget *pluginDock = plugin->setupInterface(this); if (pluginDock) { tabifyDockWidget(dashboardDock, pluginDock); } diff --git a/src/plugins/CutterPlugin.h b/src/plugins/CutterPlugin.h index 2585169a..46d4c1bc 100644 --- a/src/plugins/CutterPlugin.h +++ b/src/plugins/CutterPlugin.h @@ -12,7 +12,7 @@ class CutterPlugin public: virtual ~CutterPlugin() {} virtual void setupPlugin(CutterCore *core) = 0; - virtual CutterDockWidget* setupInterface(MainWindow *main, QAction *action = nullptr) = 0; + virtual QDockWidget *setupInterface(MainWindow *main, QAction *action = nullptr) = 0; QString name; QString description; diff --git a/src/plugins/CutterPythonPlugin.cpp b/src/plugins/CutterPythonPlugin.cpp index a1146790..86761c37 100644 --- a/src/plugins/CutterPythonPlugin.cpp +++ b/src/plugins/CutterPythonPlugin.cpp @@ -93,7 +93,7 @@ QString CutterPythonPlugin::getAttributeFromPython(const char *attribute) return result; } -CutterDockWidget* CutterPythonPlugin::setupInterface(MainWindow *main, QAction *action) +QDockWidget *CutterPythonPlugin::setupInterface(MainWindow *main, QAction *action) { Q_UNUSED(main) Q_UNUSED(action) @@ -116,9 +116,13 @@ CutterDockWidget* CutterPythonPlugin::setupInterface(MainWindow *main, QAction * } auto dockWidget = reinterpret_cast(PyLong_AsLong(pWidget)); - printf("plugin gave me this: %s\n", dockWidget->objectName().toLocal8Bit().constData()); + if (!dockWidget) { + qWarning() << "Cannot instantiate QDockWidget."; + Python()->saveThread(); + return nullptr; + } Python()->saveThread(); - return nullptr; + return dockWidget; } diff --git a/src/plugins/CutterPythonPlugin.h b/src/plugins/CutterPythonPlugin.h index 7f27b714..9a05bb36 100644 --- a/src/plugins/CutterPythonPlugin.h +++ b/src/plugins/CutterPythonPlugin.h @@ -10,7 +10,7 @@ public: CutterPythonPlugin(PyObject* pluginModule); ~CutterPythonPlugin(); void setupPlugin(CutterCore *core); - CutterDockWidget* setupInterface(MainWindow *main, QAction *action); + QDockWidget *setupInterface(MainWindow *main, QAction *action); private: PyObject *pluginModule = nullptr; diff --git a/src/plugins/pluginSample.py b/src/plugins/pluginSample.py index 891f473f..ddf883dd 100644 --- a/src/plugins/pluginSample.py +++ b/src/plugins/pluginSample.py @@ -1,31 +1,51 @@ import cutter from cutter_plugin import CutterPlugin -from PySide2 import QtCore, QtWidgets -import shiboken2 +from PySide2 import QtWidgets +from PySide2.QtCore import QObject, SIGNAL, Qt +from PySide2.QtGui import QFont class CutterSamplePlugin(CutterPlugin): name = "SamplePlugin" description = "A sample plugin written in python." version = "1.0" - author = "xarkes" - - def setupPlugin(self): - self.app = QtCore.QCoreApplication.instance() + author = "xarkes and thestr4ng3r :-P" def setupInterface(self): - print('Creating the dock widget...') - main_window = None - for widget in QtWidgets.QApplication.topLevelWidgets(): - if widget.objectName() == "MainWindow": - main_window = widget - dock_widget = QtWidgets.QDockWidget(main_window) + super().setupInterface() + + # Create dock widget and content widget + dock_widget = QtWidgets.QDockWidget(self.main) dock_widget.setObjectName("FancyDockWidgetFromCoolPlugin") - dock_widget.setWindowTitle('Test Widget') - print(main_window, dock_widget) - ptr = shiboken2.getCppPointer(dock_widget)[0] - print(ptr) - return ptr + dock_widget.setWindowTitle("Test Widget") + content = QtWidgets.QWidget() + dock_widget.setWidget(content) + + # Create layout and label + layout = QtWidgets.QVBoxLayout(dock_widget) + content.setLayout(layout) + self.text = QtWidgets.QLabel(content) + self.text.setSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred) + layout.addWidget(self.text) + + button = QtWidgets.QPushButton(content) + button.setText("Want a fortune?") + button.setSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum) + button.setMaximumHeight(50) + button.setMaximumWidth(200) + layout.addWidget(button) + layout.setAlignment(button, Qt.AlignHCenter) + + # TODO How to access CutterCore::seekChanged? + # QObject.connect(cutter.core() ?, cutter.???) + QObject.connect(button, SIGNAL("clicked()"), self.on_button_clicked) + + return self.makeCppPointer(dock_widget) + + + def on_button_clicked(self): + res = cutter.cmd("?E `fo`") + self.text.setText(res) # Instantiate our plugin diff --git a/src/python/cutter_plugin.py b/src/python/cutter_plugin.py index 9317924d..ce7809f2 100644 --- a/src/python/cutter_plugin.py +++ b/src/python/cutter_plugin.py @@ -1,17 +1,25 @@ -from abc import ABC, abstractmethod +from PySide2 import QtCore, QtWidgets +import shiboken2 -class CutterPlugin(ABC): +class CutterPlugin(object): name = '' description = '' version = '' author = '' - @abstractmethod def setupPlugin(self): - pass + self.app = QtCore.QCoreApplication.instance() - @abstractmethod def setupInterface(self): - pass + for widget in QtWidgets.QApplication.topLevelWidgets(): + if widget.objectName() == "MainWindow": + self.main = widget + break + + def makeCppPointer(self, widget): + ptr = shiboken2.getCppPointer(widget)[0] + return ptr + +