Expose the selected cells to Python

This commit is contained in:
Jose Luis Cercos-Pita
2021-11-12 14:39:30 +01:00
committed by Chris Hennes
parent 96512c1e01
commit 16bbe12336
6 changed files with 124 additions and 0 deletions

View File

@@ -118,6 +118,7 @@ PyMOD_INIT_FUNC(SpreadsheetGui)
CreateSpreadsheetCommands();
SpreadsheetGui::ViewProviderSheet::init();
SpreadsheetGui::ViewProviderSheetPython::init();
SpreadsheetGui::Workbench::init();
SpreadsheetGui::SheetView::init();

View File

@@ -9,10 +9,12 @@ include_directories(
)
generate_from_xml(SpreadsheetViewPy)
generate_from_xml(ViewProviderSpreadsheetPy)
# The XML files
set(SpreadsheetGui_XML_SRCS
SpreadsheetViewPy.xml
ViewProviderSpreadsheetPy.xml
)
set(SpreadsheetGui_LIBS
@@ -71,6 +73,7 @@ SET(SpreadsheetGui_SRCS
LineEdit.cpp
ViewProviderSpreadsheet.cpp
ViewProviderSpreadsheet.h
ViewProviderSpreadsheetPyImp.cpp
Resources/Spreadsheet.qrc
SpreadsheetView.cpp
SpreadsheetView.h

View File

@@ -36,6 +36,7 @@
#include "ViewProviderSpreadsheet.h"
#include "SpreadsheetView.h"
#include "ViewProviderSpreadsheetPy.h"
#include <Mod/Spreadsheet/App/Sheet.h>
#include <App/Range.h>
@@ -181,3 +182,23 @@ void ViewProviderSheet::updateData(const App::Property* prop)
if (view)
view->updateCell(prop);
}
PyObject *ViewProviderSheet::getPyObject()
{
if (PythonObject.is(Py::_None())){
// ref counter is set to 1
PythonObject = Py::Object(new ViewProviderSpreadsheetPy(this), true);
}
return Py::new_reference_to(PythonObject);
}
// Python feature -----------------------------------------------------------------------
namespace Gui {
/// @cond DOXERR
PROPERTY_SOURCE_TEMPLATE(SpreadsheetGui::ViewProviderSheetPython, SpreadsheetGui::ViewProviderSheet)
/// @endcond
// explicit template instantiation
template class SpreadsheetGuiExport ViewProviderPythonFeatureT<ViewProviderSheet>;
}

View File

@@ -26,6 +26,7 @@
#define SPREADSHEET_ViewProviderImagePlane_H
#include <Gui/ViewProviderDocumentObject.h>
#include <Gui/ViewProviderPythonFeature.h>
#include <QPointer>
namespace Spreadsheet {
@@ -67,13 +68,21 @@ public:
virtual Gui::MDIView *getMDIView() const override;
inline SheetView* getView() const { return view; }
PyObject *getPyObject();
protected:
SheetView* showSpreadsheetView();
void updateData(const App::Property *prop) override;
private:
QPointer<SheetView> view;
Py::Object PythonObject;
};
typedef Gui::ViewProviderPythonFeatureT<ViewProviderSheet> ViewProviderSheetPython;
} //namespace Spreadsheet

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
<PythonExport
Father="ViewProviderDocumentObjectPy"
Name="ViewProviderSpreadsheetPy"
Twin="ViewProviderSheet"
TwinPointer="ViewProviderSheet"
Include="Mod/Spreadsheet/Gui/ViewProviderSpreadsheet.h"
Namespace="SpreadsheetGui"
FatherInclude="Gui/ViewProviderDocumentObjectPy.h"
FatherNamespace="Gui"
Constructor="false"
Delete="false">
<Documentation>
<Author Licence="LGPL" Name="Jose Luis Cercos Pita" EMail="jlcercos@gmail.com" />
<UserDocu>ViewProviderSheet class</UserDocu>
</Documentation>
<Methode Name="selectedRanges">
<Documentation>
<UserDocu>returns a list with the selected ranges of cells</UserDocu>
</Documentation>
</Methode>
<Methode Name="selectedCells">
<Documentation>
<UserDocu>returns a list with the selected cells</UserDocu>
</Documentation>
</Methode>
</PythonExport>
</GenerateModel>

View File

@@ -0,0 +1,61 @@
#include "PreCompiled.h"
#include "ViewProviderSpreadsheetPy.h"
#include "ViewProviderSpreadsheetPy.cpp"
#include "SpreadsheetView.h"
using namespace SpreadsheetGui;
#if PY_MAJOR_VERSION >= 2
#define PyString_FromString PyUnicode_FromString
#endif
// returns a string which represents the object e.g. when printed in python
std::string ViewProviderSpreadsheetPy::representation(void) const
{
return std::string("<ViewProviderSpreadsheet object>");
}
PyObject* ViewProviderSpreadsheetPy::selectedRanges(PyObject* /*obj*/)
{
ViewProviderSheet* vp = this->getViewProviderSheetPtr();
SheetView *sheetView = vp->getView();
std::vector<App::Range> ranges = sheetView->selectedRanges();
PyObject *out = PyList_New(0);
std::vector<App::Range>::const_iterator i = ranges.begin();
for (; i != ranges.end(); ++i)
{
PyObject *py_str = PyString_FromString(i->rangeString().c_str());
PyList_Append(out, py_str);
}
return out;
}
PyObject* ViewProviderSpreadsheetPy::selectedCells(PyObject* /*obj*/)
{
ViewProviderSheet* vp = this->getViewProviderSheetPtr();
SheetView *sheetView = vp->getView();
QModelIndexList cells = sheetView->selectedIndexes();
PyObject *out = PyList_New(0);
for (auto it : cells) {
PyObject *py_str = PyString_FromString(
App::CellAddress(it.row(), it.column()).toString().c_str());
PyList_Append(out, py_str);
}
return out;
}
PyObject *ViewProviderSpreadsheetPy::getCustomAttributes(const char* /*attr*/) const
{
return 0;
}
int ViewProviderSpreadsheetPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
{
return 0;
}