Use ActionSelector in Loft panel and expose to Python by UiLoader
This commit is contained in:
@@ -341,6 +341,10 @@ Application::Application(bool GUIenabled)
|
||||
"workbenches.");
|
||||
Py::Module(module).setAttr(std::string("ActiveDocument"),Py::None());
|
||||
|
||||
UiLoaderPy::init_type();
|
||||
Base::Interpreter().addType(UiLoaderPy::type_object(),
|
||||
module,"UiLoader");
|
||||
|
||||
//insert Selection module
|
||||
PyObject* pSelectionModule = Py_InitModule3("Selection", SelectionSingleton::Methods,
|
||||
"Selection module");
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
#include "PreCompiled.h"
|
||||
|
||||
#include <CXX/Objects.hxx>
|
||||
#include <App/Application.h>
|
||||
#include <Base/Console.h>
|
||||
#include <Base/Exception.h>
|
||||
@@ -194,6 +195,79 @@ QWidget* UiLoader::createWidget(const QString & className, QWidget * parent,
|
||||
|
||||
// ----------------------------------------------------
|
||||
|
||||
PyObject *UiLoaderPy::PyMake(struct _typeobject *type, PyObject * args, PyObject * kwds)
|
||||
{
|
||||
if (!PyArg_ParseTuple(args, ""))
|
||||
return 0;
|
||||
return new UiLoaderPy();
|
||||
}
|
||||
|
||||
void UiLoaderPy::init_type()
|
||||
{
|
||||
behaviors().name("UiLoader");
|
||||
behaviors().doc("UiLoader to create widgets");
|
||||
behaviors().type_object()->tp_new = &PyMake;
|
||||
// you must have overwritten the virtual functions
|
||||
behaviors().supportRepr();
|
||||
behaviors().supportGetattr();
|
||||
behaviors().supportSetattr();
|
||||
add_varargs_method("createWidget",&UiLoaderPy::createWidget,"createWidget()");
|
||||
}
|
||||
|
||||
UiLoaderPy::UiLoaderPy()
|
||||
{
|
||||
}
|
||||
|
||||
UiLoaderPy::~UiLoaderPy()
|
||||
{
|
||||
}
|
||||
|
||||
Py::Object UiLoaderPy::repr()
|
||||
{
|
||||
std::string s;
|
||||
std::ostringstream s_out;
|
||||
s_out << "Ui loader";
|
||||
return Py::String(s_out.str());
|
||||
}
|
||||
|
||||
Py::Object UiLoaderPy::createWidget(const Py::Tuple& args)
|
||||
{
|
||||
Py::Module sipmod(PyImport_AddModule((char*)"sip"));
|
||||
Py::Module qtmod(PyImport_ImportModule((char*)"PyQt4.Qt"));
|
||||
|
||||
// 1st argument
|
||||
std::string className = (std::string)Py::String(args[0]);
|
||||
|
||||
// 2nd argument
|
||||
QWidget* parent = 0;
|
||||
if (args.size() > 1) {
|
||||
Py::Callable func = sipmod.getDict().getItem("unwrapinstance");
|
||||
Py::Tuple arguments(1);
|
||||
arguments[0] = args[1]; //PyQt pointer
|
||||
Py::Object result = func.apply(arguments);
|
||||
void* ptr = PyLong_AsVoidPtr(result.ptr());
|
||||
QObject* object = reinterpret_cast<QObject*>(ptr);
|
||||
if (object)
|
||||
parent = qobject_cast<QWidget*>(object);
|
||||
}
|
||||
|
||||
// 3rd argument
|
||||
std::string objectName;
|
||||
if (args.size() > 2) {
|
||||
objectName = (std::string)Py::String(args[2]);
|
||||
}
|
||||
|
||||
QWidget* widget = loader.createWidget(QString::fromAscii(className.c_str()), parent,
|
||||
QString::fromAscii(objectName.c_str()));
|
||||
Py::Callable func = sipmod.getDict().getItem("wrapinstance");
|
||||
Py::Tuple arguments(2);
|
||||
arguments[0] = Py::asObject(PyLong_FromVoidPtr(widget));
|
||||
arguments[1] = qtmod.getDict().getItem("QWidget");
|
||||
return func.apply(arguments);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------
|
||||
|
||||
WidgetFactorySupplier* WidgetFactorySupplier::_pcSingleton = 0L;
|
||||
|
||||
WidgetFactorySupplier & WidgetFactorySupplier::instance()
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include "DlgPreferencesImp.h"
|
||||
#include "DlgCustomizeImp.h"
|
||||
#include "PropertyPage.h"
|
||||
#include <CXX/Extensions.hxx>
|
||||
|
||||
namespace Gui {
|
||||
namespace Dialog{
|
||||
@@ -85,13 +86,33 @@ public:
|
||||
* Fore more details see the documentation to QWidgetFactory.
|
||||
*/
|
||||
QWidget* createWidget(const QString & className, QWidget * parent=0,
|
||||
const QString& name =QString());
|
||||
const QString& name = QString());
|
||||
private:
|
||||
QStringList cw;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
class UiLoaderPy : public Py::PythonExtension<UiLoaderPy>
|
||||
{
|
||||
public:
|
||||
static void init_type(void); // announce properties and methods
|
||||
|
||||
UiLoaderPy();
|
||||
~UiLoaderPy();
|
||||
|
||||
Py::Object repr();
|
||||
Py::Object createWidget(const Py::Tuple&);
|
||||
|
||||
private:
|
||||
static PyObject *PyMake(struct _typeobject *, PyObject *, PyObject *);
|
||||
|
||||
private:
|
||||
UiLoader loader;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The WidgetProducer class is a value-based template class that provides
|
||||
* the ability to create widgets dynamically.
|
||||
|
||||
@@ -110,6 +110,7 @@ ActionSelector::ActionSelector(QWidget* parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
addButton = new QPushButton(this);
|
||||
addButton->setObjectName(QLatin1String("addButton"));
|
||||
addButton->setMinimumSize(QSize(30, 30));
|
||||
QIcon icon;
|
||||
icon.addFile(QString::fromUtf8(":/icons/button_right.xpm"), QSize(), QIcon::Normal, QIcon::Off);
|
||||
@@ -123,6 +124,7 @@ ActionSelector::ActionSelector(QWidget* parent)
|
||||
gridLayout->addItem(spacerItem1, 0, 1, 1, 1);
|
||||
|
||||
removeButton = new QPushButton(this);
|
||||
removeButton->setObjectName(QLatin1String("removeButton"));
|
||||
removeButton->setMinimumSize(QSize(30, 30));
|
||||
QIcon icon1;
|
||||
icon1.addFile(QString::fromUtf8(":/icons/button_left.xpm"), QSize(), QIcon::Normal, QIcon::Off);
|
||||
@@ -133,6 +135,7 @@ ActionSelector::ActionSelector(QWidget* parent)
|
||||
gridLayout->addWidget(removeButton, 2, 1, 1, 1);
|
||||
|
||||
upButton = new QPushButton(this);
|
||||
upButton->setObjectName(QLatin1String("upButton"));
|
||||
upButton->setMinimumSize(QSize(30, 30));
|
||||
QIcon icon3;
|
||||
icon3.addFile(QString::fromUtf8(":/icons/button_up.xpm"), QSize(), QIcon::Normal, QIcon::Off);
|
||||
@@ -141,6 +144,7 @@ ActionSelector::ActionSelector(QWidget* parent)
|
||||
gridLayout->addWidget(upButton, 3, 1, 1, 1);
|
||||
|
||||
downButton = new QPushButton(this);
|
||||
downButton->setObjectName(QLatin1String("downButton"));
|
||||
downButton->setMinimumSize(QSize(30, 30));
|
||||
QIcon icon2;
|
||||
icon2.addFile(QString::fromUtf8(":/icons/button_down.xpm"), QSize(), QIcon::Normal, QIcon::Off);
|
||||
@@ -155,6 +159,7 @@ ActionSelector::ActionSelector(QWidget* parent)
|
||||
vboxLayout->addWidget(labelAvailable);
|
||||
|
||||
availableWidget = new QTreeWidget(this);
|
||||
availableWidget->setObjectName(QLatin1String("availableTreeWidget"));
|
||||
availableWidget->setRootIsDecorated(false);
|
||||
availableWidget->setHeaderLabels(QStringList() << QString());
|
||||
availableWidget->header()->hide();
|
||||
@@ -168,6 +173,7 @@ ActionSelector::ActionSelector(QWidget* parent)
|
||||
vboxLayout1->addWidget(labelSelected);
|
||||
|
||||
selectedWidget = new QTreeWidget(this);
|
||||
selectedWidget->setObjectName(QLatin1String("selectedTreeWidget"));
|
||||
selectedWidget->setRootIsDecorated(false);
|
||||
selectedWidget->setHeaderLabels(QStringList() << QString());
|
||||
selectedWidget->header()->hide();
|
||||
@@ -192,17 +198,39 @@ ActionSelector::ActionSelector(QWidget* parent)
|
||||
this, SLOT(onItemDoubleClicked(QTreeWidgetItem*,int)) );
|
||||
connect(selectedWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
|
||||
this, SLOT(onItemDoubleClicked(QTreeWidgetItem*,int)) );
|
||||
connect(availableWidget, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
|
||||
this, SLOT(onItemChanged(QTreeWidgetItem *,int)) );
|
||||
connect(selectedWidget, SIGNAL(itemChanged(QTreeWidgetItem*,int)),
|
||||
this, SLOT(onItemChanged(QTreeWidgetItem *,int)) );
|
||||
connect(availableWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem *)),
|
||||
this, SLOT(onCurrentItemChanged(QTreeWidgetItem *,QTreeWidgetItem *)) );
|
||||
connect(selectedWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem *)),
|
||||
this, SLOT(onCurrentItemChanged(QTreeWidgetItem *,QTreeWidgetItem *)) );
|
||||
retranslateUi();
|
||||
setButtonsEnabled();
|
||||
}
|
||||
|
||||
ActionSelector::~ActionSelector()
|
||||
{
|
||||
}
|
||||
|
||||
void ActionSelector::setSelectedLabel(const QString& label)
|
||||
{
|
||||
labelSelected->setText(label);
|
||||
}
|
||||
|
||||
QString ActionSelector::selectedLabel() const
|
||||
{
|
||||
return labelSelected->text();
|
||||
}
|
||||
|
||||
void ActionSelector::setAvailableLabel(const QString& label)
|
||||
{
|
||||
labelAvailable->setText(label);
|
||||
}
|
||||
|
||||
QString ActionSelector::availableLabel() const
|
||||
{
|
||||
return labelAvailable->text();
|
||||
}
|
||||
|
||||
|
||||
void ActionSelector::retranslateUi()
|
||||
{
|
||||
labelAvailable->setText(QApplication::translate("Gui::ActionSelector", "Available:", 0, QApplication::UnicodeUTF8));
|
||||
@@ -254,7 +282,7 @@ void ActionSelector::setButtonsEnabled()
|
||||
selectedWidget->indexOfTopLevelItem(selectedWidget->currentItem()) < selectedWidget->topLevelItemCount() - 1);
|
||||
}
|
||||
|
||||
void ActionSelector::onItemChanged(QTreeWidgetItem * /*item*/, int /*column*/)
|
||||
void ActionSelector::onCurrentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)
|
||||
{
|
||||
setButtonsEnabled();
|
||||
}
|
||||
|
||||
@@ -75,6 +75,10 @@ public:
|
||||
{ return availableWidget; }
|
||||
QTreeWidget* selectedTreeWidget() const
|
||||
{ return selectedWidget; }
|
||||
void setSelectedLabel(const QString&);
|
||||
QString selectedLabel() const;
|
||||
void setAvailableLabel(const QString&);
|
||||
QString availableLabel() const;
|
||||
|
||||
private:
|
||||
void keyPressEvent(QKeyEvent *);
|
||||
@@ -87,7 +91,7 @@ private Q_SLOTS:
|
||||
void on_removeButton_clicked();
|
||||
void on_upButton_clicked();
|
||||
void on_downButton_clicked();
|
||||
void onItemChanged(QTreeWidgetItem * item, int column);
|
||||
void onCurrentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*);
|
||||
void onItemDoubleClicked(QTreeWidgetItem * item, int column);
|
||||
|
||||
private:
|
||||
|
||||
@@ -91,6 +91,7 @@ WidgetFactorySupplier::WidgetFactorySupplier()
|
||||
new WidgetProducer<Gui::PrefColorButton>;
|
||||
new WidgetProducer<Gui::CommandIconView>;
|
||||
new WidgetProducer<Gui::AccelLineEdit>;
|
||||
new WidgetProducer<Gui::ActionSelector>;
|
||||
new WidgetProducer<Gui::ColorButton>;
|
||||
new WidgetProducer<Gui::UrlLabel>;
|
||||
new WidgetProducer<Gui::FileChooser>;
|
||||
|
||||
Reference in New Issue
Block a user