Skip to content

Commit

Permalink
Attacher: Py: GUI resources interface
Browse files Browse the repository at this point in the history
Routines to get UI strings for attacher: mode names, mode tooltips,
ref.type names
  • Loading branch information
DeepSOIC committed May 13, 2016
1 parent 8b4f121 commit 76c0a81
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/Mod/Part/Gui/AppPartGui.cpp
Expand Up @@ -28,6 +28,7 @@

#include <Mod/Part/App/PropertyTopoShape.h>

#include "AttacherTexts.h"
#include "SoBrepFaceSet.h"
#include "SoBrepEdgeSet.h"
#include "SoBrepPointSet.h"
Expand Down Expand Up @@ -117,9 +118,15 @@ PyMODINIT_FUNC initPartGui()
return;
}

(void)PartGui::initModule();
PyObject* partGuiModule = PartGui::initModule();
Base::Console().Log("Loading GUI of Part module... done\n");

PyObject* pAttachEngineTextsModule = Py_InitModule3("AttachEngineResources", AttacherGui::AttacherGuiPy::Methods,
"AttachEngine Gui resources");
Py_INCREF(pAttachEngineTextsModule);
PyModule_AddObject(partGuiModule, "AttachEngineResources", pAttachEngineTextsModule);


PartGui::SoBrepFaceSet ::initClass();
PartGui::SoBrepEdgeSet ::initClass();
PartGui::SoBrepPointSet ::initClass();
Expand Down
64 changes: 63 additions & 1 deletion src/Mod/Part/Gui/AttacherTexts.cpp
Expand Up @@ -25,6 +25,8 @@
# include <QApplication>
#endif
#include "AttacherTexts.h"
#include <Base/PyObjectBase.h>
#include <Base/Console.h>

using namespace Attacher;

Expand Down Expand Up @@ -262,7 +264,7 @@ TextSet getUIStrings(Base::Type attacherType, eMapMode mmode)
}
}

assert(false && "No user-friendly string defined for this attachment mode.");
Base::Console().Warning("No user-friendly string defined for this attachment mode and attacher type: %s %s \n",AttachEngine::getModeName(mmode).c_str(), attacherType.getName());
return TwoStrings(QString::fromStdString(AttachEngine::getModeName(mmode)), QString());
}

Expand Down Expand Up @@ -325,4 +327,64 @@ QStringList getRefListForMode(AttachEngine &attacher, eMapMode mmode)
return strlist;
}


// --------------------Py interface---------------------

PyObject* AttacherGuiPy::sGetModeStrings(PyObject * /*self*/, PyObject *args, PyObject * /*kwd*/)
{
int modeIndex = 0;
char* attacherType;
if (!PyArg_ParseTuple(args, "si", &attacherType, &modeIndex))
return NULL;

try {
Base::Type t = Base::Type::fromName(attacherType);
if (! t.isDerivedFrom(AttachEngine::getClassTypeId())){
std::stringstream ss;
ss << "Object of this type is not derived from AttachEngine: ";
ss << attacherType;
throw Py::TypeError(ss.str());
}
TextSet strs = getUIStrings(t,eMapMode(modeIndex));
Py::List result;
for(QString &s : strs){
QByteArray ba_utf8 = s.toUtf8();
result.append(Py::String(ba_utf8.data(), "utf-8"));
}

return Py::new_reference_to(result);
} catch (const Py::Exception&) {
return 0;
} catch (const Base::Exception& e) {
PyErr_SetString(Base::BaseExceptionFreeCADError, e.what());
return 0;
}
}

PyObject* AttacherGuiPy::sGetRefTypeUserFriendlyName(PyObject * /*self*/, PyObject *args, PyObject * /*kwd*/)
{
int refTypeIndex = 0;
if (!PyArg_ParseTuple(args, "i", &refTypeIndex))
return NULL;

try {
QByteArray ba_utf8 = getShapeTypeText(eRefType(refTypeIndex)).toUtf8();
return Py::new_reference_to(Py::String(ba_utf8.data(), "utf-8"));
} catch (const Py::Exception&) {
return 0;
} catch (const Base::Exception& e) {
PyErr_SetString(Base::BaseExceptionFreeCADError, e.what());
return 0;
}
}


PyMethodDef AttacherGuiPy::Methods[] = {
{"getModeStrings", (PyCFunction) AttacherGuiPy::sGetModeStrings, 1,
"getModeStrings(attacher_type, mode_index) - gets mode user-friendly name and brief description."},
{"getRefTypeUserFriendlyName", (PyCFunction) AttacherGuiPy::sGetRefTypeUserFriendlyName, 1,
"getRefTypeUserFriendlyName(type_index) - gets user-friendly name of AttachEngine's shape type."},
};


} //namespace AttacherGui
12 changes: 11 additions & 1 deletion src/Mod/Part/Gui/AttacherTexts.h
Expand Up @@ -33,8 +33,10 @@
#include <vector>
#include <QString>
#include <QStringList>
#include <CXX/Objects.hxx>
#include <Mod/Part/App/Attacher.h>


namespace AttacherGui {

typedef std::vector<QString> TextSet;
Expand All @@ -54,6 +56,14 @@ QString PartGuiExport getShapeTypeText(Attacher::eRefType type);

QStringList PartGuiExport getRefListForMode(Attacher::AttachEngine &attacher, Attacher::eMapMode mmode);

}

// Python interface
class PartGuiExport AttacherGuiPy{
public:
static PyMethodDef Methods[];
static PyObject* sGetModeStrings(PyObject * /*self*/, PyObject *args, PyObject * /*kwd*/);
static PyObject* sGetRefTypeUserFriendlyName(PyObject * /*self*/, PyObject *args, PyObject * /*kwd*/);
};

}
#endif

0 comments on commit 76c0a81

Please sign in to comment.