Spreadsheet: implement SheetViewPy that acts as sub-class of MDIViewPy

This commit is contained in:
wmayer
2021-11-13 20:40:32 +01:00
parent 48f4277477
commit d398ee7f50
5 changed files with 132 additions and 8 deletions

View File

@@ -121,6 +121,7 @@ PyMOD_INIT_FUNC(SpreadsheetGui)
SpreadsheetGui::ViewProviderSheetPython::init();
SpreadsheetGui::Workbench::init();
SpreadsheetGui::SheetView::init();
SpreadsheetGui::SheetViewPy::init_type();
// register preference page
new Gui::PrefPageProducer<SpreadsheetGui::DlgSettingsImp> ("Spreadsheet");

View File

@@ -48,6 +48,7 @@
#include <Gui/ExpressionCompleter.h>
#include <LineEdit.h>
#include <Mod/Spreadsheet/App/Sheet.h>
#include <Mod/Spreadsheet/App/SheetPy.h>
#include <Mod/Spreadsheet/App/Utils.h>
#include "qtcolorpicker.h"
@@ -447,7 +448,7 @@ void SpreadsheetGui::SheetView::setCurrentIndex(App::CellAddress cell) const
PyObject *SheetView::getPyObject()
{
if (!pythonObject)
pythonObject = new SpreadsheetViewPy(this);
pythonObject = new SheetViewPy(this);
Py_INCREF(pythonObject);
return pythonObject;
@@ -458,4 +459,76 @@ void SheetView::deleteSelf()
Gui::MDIView::deleteSelf();
}
// ----------------------------------------------------------
void SheetViewPy::init_type()
{
behaviors().name("SheetViewPy");
behaviors().doc("Python binding class for the Sheet view class");
// you must have overwritten the virtual functions
behaviors().supportRepr();
behaviors().supportGetattr();
behaviors().supportSetattr();
add_varargs_method("getSheet", &SheetViewPy::getSheet, "getSheet()");
behaviors().readyType();
}
SheetViewPy::SheetViewPy(SheetView *mdi)
: base(mdi)
{
}
SheetViewPy::~SheetViewPy()
{
}
Py::Object SheetViewPy::repr()
{
std::ostringstream s_out;
if (!getSheetViewPtr())
throw Py::RuntimeError("Cannot print representation of deleted object");
s_out << "SheetView";
return Py::String(s_out.str());
}
// Since with PyCXX it's not possible to make a sub-class of MDIViewPy
// a trick is to use MDIViewPy as class member and override getattr() to
// join the attributes of both classes. This way all methods of MDIViewPy
// appear for SheetViewPy, too.
Py::Object SheetViewPy::getattr(const char * attr)
{
if (!getSheetViewPtr())
throw Py::RuntimeError("Cannot print representation of deleted object");
std::string name( attr );
if (name == "__dict__" || name == "__class__") {
Py::Dict dict_self(BaseType::getattr("__dict__"));
Py::Dict dict_base(base.getattr("__dict__"));
for (auto it : dict_base) {
dict_self.setItem(it.first, it.second);
}
return dict_self;
}
try {
return BaseType::getattr(attr);
}
catch (Py::AttributeError& e) {
e.clear();
return base.getattr(attr);
}
}
SheetView* SheetViewPy::getSheetViewPtr()
{
return qobject_cast<SheetView*>(base.getMDIViewPtr());
}
Py::Object SheetViewPy::getSheet(const Py::Tuple& args)
{
if (!PyArg_ParseTuple(args.ptr(), ""))
throw Py::Exception();
return Py::asObject(new Spreadsheet::SheetPy(getSheetViewPtr()->getSheet()));
}
#include "moc_SpreadsheetView.cpp"

View File

@@ -24,6 +24,7 @@
#define SpreadsheetView_H
#include <Gui/MDIView.h>
#include <Gui/MDIViewPy.h>
#include <QHeaderView>
#include "SheetModel.h"
#include <Mod/Spreadsheet/App/Sheet.h>
@@ -117,6 +118,25 @@ protected:
std::map<int, int> newRowSizes;
};
class SheetViewPy : public Py::PythonExtension<SheetViewPy>
{
public:
using BaseType = Py::PythonExtension<SheetViewPy>;
static void init_type();
SheetViewPy(SheetView *mdi);
~SheetViewPy();
Py::Object repr();
Py::Object getattr(const char *);
Py::Object getSheet(const Py::Tuple&);
SheetView* getSheetViewPtr();
protected:
Gui::MDIViewPy base;
};
} // namespace SpreadsheetModGui
#endif // SpreadsheetView_H