Cleaned a bit and added a widget example

This commit is contained in:
xarkes 2018-07-08 10:49:20 +02:00 committed by Florian Märkl
parent 74a201ecba
commit da3e1fad29
6 changed files with 61 additions and 29 deletions

View File

@ -278,7 +278,7 @@ void MainWindow::initUI()
/* Setup plugins interfaces */ /* Setup plugins interfaces */
QList<CutterPlugin *> plugins = core->getCutterPlugins(); QList<CutterPlugin *> plugins = core->getCutterPlugins();
for (auto plugin : plugins) { for (auto plugin : plugins) {
CutterDockWidget *pluginDock = plugin->setupInterface(this); QDockWidget *pluginDock = plugin->setupInterface(this);
if (pluginDock) { if (pluginDock) {
tabifyDockWidget(dashboardDock, pluginDock); tabifyDockWidget(dashboardDock, pluginDock);
} }

View File

@ -12,7 +12,7 @@ class CutterPlugin
public: public:
virtual ~CutterPlugin() {} virtual ~CutterPlugin() {}
virtual void setupPlugin(CutterCore *core) = 0; 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 name;
QString description; QString description;

View File

@ -93,7 +93,7 @@ QString CutterPythonPlugin::getAttributeFromPython(const char *attribute)
return result; return result;
} }
CutterDockWidget* CutterPythonPlugin::setupInterface(MainWindow *main, QAction *action) QDockWidget *CutterPythonPlugin::setupInterface(MainWindow *main, QAction *action)
{ {
Q_UNUSED(main) Q_UNUSED(main)
Q_UNUSED(action) Q_UNUSED(action)
@ -116,9 +116,13 @@ CutterDockWidget* CutterPythonPlugin::setupInterface(MainWindow *main, QAction *
} }
auto dockWidget = reinterpret_cast<QDockWidget *>(PyLong_AsLong(pWidget)); auto dockWidget = reinterpret_cast<QDockWidget *>(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(); Python()->saveThread();
return nullptr; return dockWidget;
} }

View File

@ -10,7 +10,7 @@ public:
CutterPythonPlugin(PyObject* pluginModule); CutterPythonPlugin(PyObject* pluginModule);
~CutterPythonPlugin(); ~CutterPythonPlugin();
void setupPlugin(CutterCore *core); void setupPlugin(CutterCore *core);
CutterDockWidget* setupInterface(MainWindow *main, QAction *action); QDockWidget *setupInterface(MainWindow *main, QAction *action);
private: private:
PyObject *pluginModule = nullptr; PyObject *pluginModule = nullptr;

View File

@ -1,31 +1,51 @@
import cutter import cutter
from cutter_plugin import CutterPlugin from cutter_plugin import CutterPlugin
from PySide2 import QtCore, QtWidgets from PySide2 import QtWidgets
import shiboken2 from PySide2.QtCore import QObject, SIGNAL, Qt
from PySide2.QtGui import QFont
class CutterSamplePlugin(CutterPlugin): class CutterSamplePlugin(CutterPlugin):
name = "SamplePlugin" name = "SamplePlugin"
description = "A sample plugin written in python." description = "A sample plugin written in python."
version = "1.0" version = "1.0"
author = "xarkes" author = "xarkes and thestr4ng3r :-P"
def setupPlugin(self):
self.app = QtCore.QCoreApplication.instance()
def setupInterface(self): def setupInterface(self):
print('Creating the dock widget...') super().setupInterface()
main_window = None
for widget in QtWidgets.QApplication.topLevelWidgets(): # Create dock widget and content widget
if widget.objectName() == "MainWindow": dock_widget = QtWidgets.QDockWidget(self.main)
main_window = widget
dock_widget = QtWidgets.QDockWidget(main_window)
dock_widget.setObjectName("FancyDockWidgetFromCoolPlugin") dock_widget.setObjectName("FancyDockWidgetFromCoolPlugin")
dock_widget.setWindowTitle('Test Widget') dock_widget.setWindowTitle("Test Widget")
print(main_window, dock_widget) content = QtWidgets.QWidget()
ptr = shiboken2.getCppPointer(dock_widget)[0] dock_widget.setWidget(content)
print(ptr)
return ptr # 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 # Instantiate our plugin

View File

@ -1,17 +1,25 @@
from abc import ABC, abstractmethod from PySide2 import QtCore, QtWidgets
import shiboken2
class CutterPlugin(ABC): class CutterPlugin(object):
name = '' name = ''
description = '' description = ''
version = '' version = ''
author = '' author = ''
@abstractmethod
def setupPlugin(self): def setupPlugin(self):
pass self.app = QtCore.QCoreApplication.instance()
@abstractmethod
def setupInterface(self): 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