2014-09-10 17:27:45 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vpropertyformwidget.cpp
|
|
|
|
** @author hedgeware <internal(at)hedgeware.net>
|
|
|
|
** @date
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** All rights reserved. This program and the accompanying materials
|
|
|
|
** are made available under the terms of the GNU Lesser General Public License
|
|
|
|
** (LGPL) version 2.1 which accompanies this distribution, and is available at
|
|
|
|
** http://www.gnu.org/licenses/lgpl-2.1.html
|
|
|
|
**
|
|
|
|
** This library is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
** Lesser General Public License for more details.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include "vpropertyformwidget.h"
|
|
|
|
|
|
|
|
#include <QFormLayout>
|
|
|
|
#include "vpropertyformwidget_p.h"
|
|
|
|
|
|
|
|
#include "plugins/vwidgetproperty.h"
|
|
|
|
#include <QEvent>
|
|
|
|
#include <QKeyEvent>
|
|
|
|
#include "vproperty.h"
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
using namespace VPE;
|
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
VPropertyFormWidget::VPropertyFormWidget(const QString &title, const QString &description,
|
|
|
|
const QList<VProperty*>& properties, QWidget *parent)
|
2014-09-10 17:27:45 +02:00
|
|
|
: QGroupBox(title, parent), d_ptr(new VPropertyFormWidgetPrivate(properties))
|
|
|
|
{
|
|
|
|
build();
|
|
|
|
setToolTip(description);
|
|
|
|
setWhatsThis(description);
|
|
|
|
}
|
|
|
|
|
|
|
|
VPropertyFormWidget::VPropertyFormWidget(VProperty *parent_property, QWidget *parent)
|
|
|
|
: QGroupBox(parent), d_ptr(new VPropertyFormWidgetPrivate())
|
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (parent_property)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
d_ptr->Properties = parent_property->getChildren();
|
|
|
|
build();
|
|
|
|
setTitle(parent_property->getName());
|
|
|
|
setToolTip(parent_property->getDescription());
|
|
|
|
setWhatsThis(parent_property->getDescription());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
VPropertyFormWidget::VPropertyFormWidget(VPropertyFormWidgetPrivate *d_pointer, QWidget *parent, const QString &title,
|
|
|
|
const QString &description)
|
2014-09-10 17:27:45 +02:00
|
|
|
: QGroupBox(title, parent), d_ptr(d_pointer)
|
|
|
|
{
|
|
|
|
build();
|
|
|
|
setToolTip(description);
|
|
|
|
setWhatsThis(description);
|
|
|
|
}
|
|
|
|
|
|
|
|
VPropertyFormWidget::~VPropertyFormWidget()
|
|
|
|
{
|
|
|
|
delete d_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VPropertyFormWidget::build()
|
|
|
|
{
|
|
|
|
// Clear the old content, delete old widgets
|
|
|
|
d_ptr->EditorWidgets.clear();
|
2014-09-10 19:57:08 +02:00
|
|
|
if (layout())
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
QLayoutItem *child;
|
2014-09-10 19:57:08 +02:00
|
|
|
while (layout()->count() > 0 && (child = layout()->takeAt(0)) != 0)
|
|
|
|
{
|
|
|
|
if (child->widget())
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
delete child->widget();
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
delete child;
|
|
|
|
}
|
|
|
|
delete layout();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new content
|
2014-09-10 19:57:08 +02:00
|
|
|
if (d_ptr->Properties.isEmpty())
|
|
|
|
{
|
|
|
|
return; //... only if there are properties
|
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
QFormLayout* tmpFormLayout = new QFormLayout(this);
|
|
|
|
setLayout(tmpFormLayout);
|
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
for (int i = 0; i < d_ptr->Properties.count(); ++i)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
// Get the current property
|
|
|
|
VProperty* tmpProperty = d_ptr->Properties.value(i, nullptr);
|
2014-09-10 19:57:08 +02:00
|
|
|
if (!tmpProperty)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
if (tmpProperty->getRowCount() > 0)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
if (tmpProperty->propertyType() == Property::Complex)
|
|
|
|
{
|
|
|
|
buildEditor(tmpProperty, tmpFormLayout, Property::Complex);
|
|
|
|
QWidget *group = new QWidget(this);
|
|
|
|
tmpFormLayout->addRow(group);
|
|
|
|
|
|
|
|
QFormLayout* subFormLayout = new QFormLayout(group);
|
|
|
|
QMargins margins = subFormLayout->contentsMargins();
|
|
|
|
margins.setTop(0);
|
|
|
|
margins.setLeft(14);
|
|
|
|
subFormLayout->setContentsMargins(margins);
|
|
|
|
|
|
|
|
group->setLayout(subFormLayout);
|
|
|
|
QList<VProperty*> children = tmpProperty->getChildren();
|
|
|
|
for (int j = 0; j < children.size(); ++j)
|
|
|
|
{
|
|
|
|
buildEditor(children[j], subFormLayout);
|
|
|
|
connect(children[j], &VProperty::childChanged, tmpProperty, &VProperty::ValueChildChanged,
|
|
|
|
Qt::UniqueConnection);
|
|
|
|
++i;
|
|
|
|
d_ptr->Properties.insert(i, children[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Child properties, the property itself is not being added
|
|
|
|
VPropertyFormWidget* tmpNewFormWidget = new VPropertyFormWidget(tmpProperty, this);
|
|
|
|
tmpFormLayout->addRow(tmpNewFormWidget);
|
|
|
|
d_ptr->EditorWidgets.append(VPropertyFormWidgetPrivate::SEditorWidget(tmpNewFormWidget));
|
|
|
|
tmpNewFormWidget->setCommitBehaviour(d_ptr->UpdateEditors);
|
|
|
|
}
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
|
|
|
else if (tmpProperty->type() == "widget")
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
VWidgetProperty* tmpWidgetProperty = static_cast<VWidgetProperty*>(tmpProperty);
|
|
|
|
tmpFormLayout->addRow(tmpWidgetProperty->getWidget());
|
|
|
|
d_ptr->EditorWidgets.append(VPropertyFormWidgetPrivate::SEditorWidget(tmpWidgetProperty->getWidget()));
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
buildEditor(tmpProperty, tmpFormLayout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VPropertyFormWidget::buildEditor(VProperty* property, QFormLayout* formLayout, Property type)
|
|
|
|
{
|
|
|
|
// Add property (no child properties)
|
|
|
|
// Create the editor (if it doesn't work, create empty widget)
|
|
|
|
QWidget* tmpEditor = property->createEditor(this, QStyleOptionViewItem(), nullptr);
|
2014-09-10 19:57:08 +02:00
|
|
|
if (!tmpEditor)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
tmpEditor = new QWidget(this);
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
// set tooltip and whats this
|
|
|
|
tmpEditor->setToolTip(property->getDescription());
|
|
|
|
tmpEditor->setWhatsThis(property->getDescription());
|
|
|
|
|
|
|
|
// Install event filter
|
|
|
|
tmpEditor->installEventFilter(this);
|
|
|
|
|
|
|
|
// Set the editor data
|
|
|
|
property->setEditorData(tmpEditor);
|
|
|
|
|
|
|
|
// add new row
|
|
|
|
if (type == Property::Complex)
|
|
|
|
{
|
|
|
|
QString name = "<b>"+property->getName()+"</b>";
|
|
|
|
formLayout->addRow(name, tmpEditor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
formLayout->addRow(property->getName(), tmpEditor);
|
|
|
|
}
|
|
|
|
|
|
|
|
d_ptr->EditorWidgets.append(VPropertyFormWidgetPrivate::SEditorWidget(tmpEditor));
|
|
|
|
}
|
|
|
|
|
|
|
|
void VPropertyFormWidget::commitData()
|
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
for (int i = 0; i < d_ptr->Properties.count(); ++i)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
commitData(i);
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void VPropertyFormWidget::loadData()
|
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
for (int i = 0; i < d_ptr->Properties.count(); ++i)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
loadData(i);
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void VPropertyFormWidget::commitData(int row)
|
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (row < 0 || row >= d_ptr->EditorWidgets.count() || row >= d_ptr->Properties.count())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
VPropertyFormWidgetPrivate::SEditorWidget& tmpEditorWidget = d_ptr->EditorWidgets[row];
|
|
|
|
VProperty* tmpProperty = d_ptr->Properties[row];
|
2014-09-10 19:57:08 +02:00
|
|
|
if (tmpEditorWidget.FormWidget)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
tmpEditorWidget.FormWidget->commitData();
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
|
|
|
else if (tmpEditorWidget.Editor && tmpProperty)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
|
|
|
QVariant newValue = tmpProperty->getEditorData(tmpEditorWidget.Editor);
|
|
|
|
QVariant oldValue = tmpProperty->data(VProperty::DPC_Data, Qt::EditRole);
|
|
|
|
if (oldValue != newValue)
|
|
|
|
{
|
|
|
|
VProperty *parent = tmpProperty->getParent();
|
|
|
|
if (parent == nullptr)
|
|
|
|
{
|
|
|
|
tmpProperty->setValue(newValue);
|
|
|
|
emit propertyDataSubmitted(tmpProperty);
|
|
|
|
}
|
|
|
|
else if (parent->propertyType() == Property::Complex)
|
|
|
|
{
|
|
|
|
tmpProperty->UpdateParent(newValue);
|
|
|
|
emit propertyDataSubmitted(parent);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmpProperty->setValue(newValue);
|
|
|
|
emit propertyDataSubmitted(tmpProperty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VPropertyFormWidget::loadData(int row)
|
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (row < 0 || row >= d_ptr->EditorWidgets.count() || row >= d_ptr->Properties.count())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
VPropertyFormWidgetPrivate::SEditorWidget& tmpEditorWidget = d_ptr->EditorWidgets[row];
|
|
|
|
VProperty* tmpProperty = d_ptr->Properties[row];
|
2014-09-10 19:57:08 +02:00
|
|
|
if (tmpEditorWidget.FormWidget)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
tmpEditorWidget.FormWidget->loadData();
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
|
|
|
else if (tmpEditorWidget.Editor && tmpProperty)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
tmpProperty->setEditorData(tmpEditorWidget.Editor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VPropertyFormWidget::setCommitBehaviour(bool auto_commit)
|
|
|
|
{
|
|
|
|
d_ptr->UpdateEditors = auto_commit;
|
|
|
|
|
|
|
|
QList<VPropertyFormWidget*> tmpChildFormWidgets = getChildPropertyFormWidgets();
|
2014-09-10 19:57:08 +02:00
|
|
|
foreach(VPropertyFormWidget* tmpChild, tmpChildFormWidgets)
|
|
|
|
{
|
|
|
|
if (tmpChild)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
tmpChild->setCommitBehaviour(auto_commit);
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<VPropertyFormWidget *> VPropertyFormWidget::getChildPropertyFormWidgets() const
|
|
|
|
{
|
|
|
|
QList<VPropertyFormWidget *> tmpResult;
|
2014-09-10 19:57:08 +02:00
|
|
|
foreach(const VPropertyFormWidgetPrivate::SEditorWidget& tmpEditorWidget, d_ptr->EditorWidgets)
|
|
|
|
{
|
|
|
|
if (tmpEditorWidget.FormWidget)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
tmpResult.append(tmpEditorWidget.FormWidget);
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return tmpResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VPropertyFormWidget::eventFilter(QObject *object, QEvent *event)
|
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (!d_ptr->UpdateEditors)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return false;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
|
|
|
QWidget* editor = qobject_cast<QWidget*>(object);
|
|
|
|
if (!editor)
|
2014-09-10 19:57:08 +02:00
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return false;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
if (event->type() == QEvent::KeyPress)
|
|
|
|
{
|
|
|
|
switch (static_cast<QKeyEvent *>(event)->key())
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
case Qt::Key_Tab:
|
|
|
|
case Qt::Key_Backtab:
|
|
|
|
case Qt::Key_Enter:
|
|
|
|
case Qt::Key_Return:
|
|
|
|
case Qt::Key_Escape:
|
|
|
|
commitData(editor);
|
|
|
|
event->accept();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
|
|
|
else if (event->type() == QEvent::FocusOut || (event->type() == QEvent::Hide && editor->isWindow()))
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
commitData(editor);
|
|
|
|
return false;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
|
|
|
else if (event->type() == QEvent::ShortcutOverride)
|
|
|
|
{
|
|
|
|
if (static_cast<QKeyEvent*>(event)->key() == Qt::Key_Escape)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
commitData(editor);
|
|
|
|
event->accept();
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
|
|
|
else if (event->type() == MyCustomEventType)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
commitData(editor);
|
|
|
|
event->accept();
|
|
|
|
return true;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return QGroupBox::eventFilter(object, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-14 11:16:59 +02:00
|
|
|
void VPropertyFormWidget::commitData(const QWidget *editor)
|
2014-09-10 17:27:45 +02:00
|
|
|
{
|
2014-09-10 19:57:08 +02:00
|
|
|
if (!editor)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
return;
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
|
2014-09-10 19:57:08 +02:00
|
|
|
for (int i = 0; i < d_ptr->EditorWidgets.count(); ++i)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
VPropertyFormWidgetPrivate::SEditorWidget& tmpEditorWidget = d_ptr->EditorWidgets[i];
|
2014-09-10 19:57:08 +02:00
|
|
|
if (tmpEditorWidget.Editor == editor)
|
|
|
|
{
|
2014-09-10 17:27:45 +02:00
|
|
|
commitData(i);
|
2014-09-10 19:57:08 +02:00
|
|
|
}
|
2014-09-10 17:27:45 +02:00
|
|
|
}
|
|
|
|
}
|