+ use Quantity type as QVariant, add quantity property to InputField and implement a PySide converter

This commit is contained in:
wmayer
2014-04-01 15:43:40 +02:00
parent 3b645362e6
commit a8fe58d93b
4 changed files with 72 additions and 10 deletions

View File

@@ -31,6 +31,8 @@
#include "SpinBox.h"
#include "FileDialog.h"
Q_DECLARE_METATYPE(Base::Quantity)
namespace Gui {
@@ -47,24 +49,25 @@ class GuiExport InputField : public QLineEdit
{
Q_OBJECT
Q_PROPERTY( QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath )
Q_PROPERTY(QByteArray prefPath READ paramGrpPath WRITE setParamGrpPath )
Q_PROPERTY(double singleStep READ singleStep WRITE setSingleStep )
Q_PROPERTY(double maximum READ maximum WRITE setMaximum )
Q_PROPERTY(double minimum READ minimum WRITE setMinimum )
Q_PROPERTY(int historySize READ historySize WRITE setHistorySize )
Q_PROPERTY(QString unit READ getUnitText WRITE setUnitText )
Q_PROPERTY(Base::Quantity quantity READ getQuantity WRITE setValue )
public:
InputField ( QWidget * parent = 0 );
virtual ~InputField();
/// sets the field with a quantity
/// set the field with a quantity
void setValue(const Base::Quantity&);
/// get the actual value
/// get the current value
Base::Quantity getQuantity(void)const{return this->actQuantity;}
/** sets the Unit this field working with.
* After seting the Unit the field will only acceppt
/** sets the Unit this field is working with.
* After setting the Unit the field will only accept
* user input with this unit type. Or if the user input
* a value without unit, this one will be added to the resulting
* Quantity.
@@ -98,9 +101,9 @@ public:
/** @name history and default management */
//@{
/// the param group path where the widget write and read the dafault values
/// the param group path where the widget writes and reads the default values
QByteArray paramGrpPath () const;
/// set the param group path where the widget write and read the dafault values
/// set the param group path where the widget writes and reads the default values
void setParamGrpPath ( const QByteArray& name );
/// push a new value to the history, if no string given the actual text of the input field is used.
void pushToHistory(const QString &valueq = QString());
@@ -114,14 +117,14 @@ public:
Q_SIGNALS:
/** gets emitted if the user has entered a VALID input
* Valid means the user inputted string obays all restrictions
* Valid means the user inputted string obeys all restrictions
* like: minimum, maximum and/or the right Unit (if specified).
* If you want the unfiltered/unvalidated input use textChanged(const QString&)
* instead:
*/
void valueChanged(const Base::Quantity&);
/** gets emitted if the user has entered a VALID input
* Valid means the user inputted string obays all restrictions
* Valid means the user inputted string obeys all restrictions
* like: minimum, maximum and/or the right Unit (if specified).
* If you want the unfiltered/unvalidated input use textChanged(const QString&)
* instead:

View File

@@ -135,6 +135,7 @@ void Gui::SoFCDB::init()
qRegisterMetaType<Base::Vector3f>("Base::Vector3f");
qRegisterMetaType<Base::Vector3d>("Base::Vector3d");
qRegisterMetaType<Base::Quantity>("Base::Quantity");
init_done = TRUE;
}

View File

@@ -59,6 +59,8 @@ PyTypeObject** SbkPySide_QtGuiTypes=NULL;
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/Interpreter.h>
#include <Base/Quantity.h>
#include <Base/QuantityPy.h>
#include "WidgetFactory.h"
@@ -68,9 +70,65 @@ PyTypeObject** SbkPySide_QtGuiTypes=NULL;
using namespace Gui;
#if defined (HAVE_SHIBOKEN) && defined(HAVE_PYSIDE)
namespace Shiboken {
template<> struct Converter<Base::Quantity>
{
static inline bool checkType(PyObject* pyObj) {
return PyObject_TypeCheck(pyObj, &(Base::QuantityPy::Type));
}
static inline bool isConvertible(PyObject* pyObj) {
return PyObject_TypeCheck(pyObj, &(Base::QuantityPy::Type));
}
static inline PyObject* toPython(void* cppobj) {
return toPython(*reinterpret_cast<Base::Quantity*>(cppobj));
}
static inline PyObject* toPython(Base::Quantity cpx) {
return new Base::QuantityPy(new Base::Quantity(cpx));
}
static inline Base::Quantity toCpp(PyObject* pyobj) {
Base::Quantity q = *static_cast<Base::QuantityPy*>(pyobj)->getQuantityPtr();
return q;
}
};
}
PyObject* toPythonFuncQuantity(const void* cpp)
{
return Shiboken::Converter<Base::Quantity>::toPython(const_cast<void*>(cpp));
}
void toCppPointerConvFuncQuantity(PyObject*,void*)
{
}
PythonToCppFunc toCppPointerCheckFuncQuantity(PyObject* obj)
{
if (Shiboken::Converter<Base::Quantity>::isConvertible(obj))
return toCppPointerConvFuncQuantity;
else
return 0;
}
void registerTypes()
{
SbkConverter* convert = Shiboken::Conversions::createConverter(&Base::QuantityPy::Type, toPythonFuncQuantity);
Shiboken::Conversions::setPythonToCppPointerFunctions(convert, toCppPointerConvFuncQuantity, toCppPointerCheckFuncQuantity);
Shiboken::Conversions::registerConverterName(convert, "Base::Quantity");
}
#endif
// --------------------------------------------------------
PythonWrapper::PythonWrapper()
{
#if defined (HAVE_SHIBOKEN) && defined(HAVE_PYSIDE)
static bool init = false;
if (!init) {
init = true;
registerTypes();
}
#endif
}
bool PythonWrapper::toCString(const Py::Object& pyobject, std::string& str)

View File

@@ -36,12 +36,12 @@
#include <Base/UnitsApi.h>
#include <App/PropertyStandard.h>
#include <Gui/Widgets.h>
#include <Gui/InputField.h>
Q_DECLARE_METATYPE(Base::Vector3f)
Q_DECLARE_METATYPE(Base::Vector3d)
Q_DECLARE_METATYPE(Base::Matrix4D)
Q_DECLARE_METATYPE(Base::Placement)
Q_DECLARE_METATYPE(Base::Quantity)
namespace Gui {
namespace Dialog { class TaskPlacement; }