Skip to content

Commit

Permalink
Configuration and display
Browse files Browse the repository at this point in the history
works in basic - no attributs. no handy configurator.
  • Loading branch information
signedav committed Sep 5, 2018
1 parent f41aaa7 commit e8e32fa
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ An attribute editor widget that will represent arbitrary QML code.
#include "qgsattributeeditorelement.h"
%End
public:
QgsAttributeEditorQmlElement( QgsAttributeEditorElement *parent );
QgsAttributeEditorQmlElement( const QString &name, QgsAttributeEditorElement *parent );

virtual QgsAttributeEditorElement *clone( QgsAttributeEditorElement *parent ) const /Factory/;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ the Free Software Foundation; either version 2 of the License, or *
virtual void initWidget( QWidget *editor );


void setQmlCode( const QString &qmlCode );

public slots:

virtual void setFeature( const QgsFeature &feature );
Expand Down
83 changes: 70 additions & 13 deletions src/app/qgsattributesformproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,20 @@ QTreeWidgetItem *QgsAttributesFormProperties::loadAttributeEditorTreeItem( QgsAt
{
loadAttributeEditorTreeItem( wdg, newWidget, tree );
}
break;
}

case QgsAttributeEditorElement::AeTypeQmlElement:
{
const QgsAttributeEditorQmlElement *qmlElementEditor = static_cast<const QgsAttributeEditorQmlElement *>( widgetDef );
DnDTreeItemData itemData = DnDTreeItemData( DnDTreeItemData::QmlWidget, widgetDef->name(), widgetDef->name() );
itemData.setShowLabel( widgetDef->showLabel() );
QmlElementEditorConfiguration qmlEdConfig;
qmlEdConfig.qmlCode = qmlElementEditor->qmlCode();
itemData.setQmlElementEditorConfiguration( qmlEdConfig );
newWidget = tree->addItem( parent, itemData );
break;
}
break;
default:
//should not happen
break;
Expand Down Expand Up @@ -580,8 +592,43 @@ QgsAttributeEditorElement *QgsAttributesFormProperties::createAttributeEditorWid

case DnDTreeItemData::QmlWidget:
{
QgsAttributeEditorQmlElement *element = new QgsAttributeEditorQmlElement( parent );
element->setQmlCode( "ABC " );
QgsAttributeEditorQmlElement *element = new QgsAttributeEditorQmlElement( item->text( 0 ), parent );
element->setQmlCode( itemData.qmlElementEditorConfiguration().qmlCode );

/*
element->setQmlCode( QStringLiteral(
"import QtQuick 2.0\n"
"\n"
"Rectangle {\n"
" width: 100\n"
" height: 100\n"
" color: \"red\"\n"
"}\n" ) );
element->setQmlCode( QStringLiteral(
"import QtQuick 2.0\n"
"import QtCharts 2.0\n"
"\n"
"ChartView {\n"
" width: 600\n"
" height: 400\n"
"\n"
" PieSeries {\n"
" id: pieSeries\n"
" PieSlice { label: \"outlet 1\"; value: attributes.outlet_1 }\n"
" PieSlice { label: \"outlet 2\"; value: attributes.outlet_2 }\n"
" PieSlice { label: \"outlet 3\"; value: attributes.outlet_3 }\n"
" PieSlice { label: \"outlet 4\"; value: attributes.outlet_4 }\n"
" }\n"
"}\n" ) );
import QtQuick 2.0
Rectangle {
width: 100
height: 100
color: "red"
}
*/

widgetDef = element;
break;
Expand Down Expand Up @@ -1087,12 +1134,11 @@ void DnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int column )
dlg.setLayout( layout );
layout->addWidget( baseWidget );

QCheckBox *showLinkButton = new QCheckBox( tr( "Show link button" ) );
showLinkButton->setChecked( itemData.relationEditorConfiguration().showLinkButton );
QCheckBox *showUnlinkButton = new QCheckBox( tr( "Show unlink button" ) );
showUnlinkButton->setChecked( itemData.relationEditorConfiguration().showUnlinkButton );
layout->addRow( showLinkButton );
layout->addRow( showUnlinkButton );
QPlainTextEdit *qmlCode = new QPlainTextEdit( itemData.qmlElementEditorConfiguration().qmlCode );
QLineEdit *title = new QLineEdit( itemData.name() );

layout->addRow( tr( "Title" ), title );
layout->addRow( tr( "QML Code" ), qmlCode );

QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel );

Expand All @@ -1103,13 +1149,14 @@ void DnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int column )

if ( dlg.exec() )
{
QgsAttributesFormProperties::RelationEditorConfiguration relEdCfg;
relEdCfg.showLinkButton = showLinkButton->isChecked();
relEdCfg.showUnlinkButton = showUnlinkButton->isChecked();
QgsAttributesFormProperties::QmlElementEditorConfiguration qmlEdCfg;
qmlEdCfg.qmlCode = qmlCode->toPlainText();
itemData.setName( title->text() );
itemData.setQmlElementEditorConfiguration( qmlEdCfg );
itemData.setShowLabel( showLabelCheckbox->isChecked() );
itemData.setRelationEditorConfiguration( relEdCfg );

item->setData( 0, QgsAttributesFormProperties::DnDTreeRole, itemData );
item->setText( 0, title->text() );
}
}
break;
Expand Down Expand Up @@ -1216,3 +1263,13 @@ void QgsAttributesFormProperties::DnDTreeItemData::setRelationEditorConfiguratio
mRelationEditorConfiguration = relationEditorConfiguration;
}

QgsAttributesFormProperties::QmlElementEditorConfiguration QgsAttributesFormProperties::DnDTreeItemData::qmlElementEditorConfiguration() const
{
return mQmlElementEditorConfiguration;
}

void QgsAttributesFormProperties::DnDTreeItemData::setQmlElementEditorConfiguration( QgsAttributesFormProperties::QmlElementEditorConfiguration qmlElementEditorConfiguration )
{
mQmlElementEditorConfiguration = qmlElementEditorConfiguration;
}

10 changes: 10 additions & 0 deletions src/app/qgsattributesformproperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <QFileDialog>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QPlainTextEdit>

#include "qgsvectorlayer.h"
#include "ui_qgsattributesformproperties.h"
Expand Down Expand Up @@ -70,6 +71,11 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
bool showUnlinkButton = true;
};

struct QmlElementEditorConfiguration
{
QString qmlCode;
};

class DnDTreeItemData : public QTreeWidgetItem
{
public:
Expand Down Expand Up @@ -116,6 +122,9 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
RelationEditorConfiguration relationEditorConfiguration() const;
void setRelationEditorConfiguration( RelationEditorConfiguration relationEditorConfiguration );

QmlElementEditorConfiguration qmlElementEditorConfiguration() const;
void setQmlElementEditorConfiguration( QmlElementEditorConfiguration qmlElementEditorConfiguration );

private:
Type mType = Field;
QString mName;
Expand All @@ -126,6 +135,7 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
bool mShowLabel = true;
QgsOptionalExpression mVisibilityExpression;
RelationEditorConfiguration mRelationEditorConfiguration;
QmlElementEditorConfiguration mQmlElementEditorConfiguration;
};


Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsattributeeditorelement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void QgsAttributeEditorRelation::setShowUnlinkButton( bool showUnlinkButton )

QgsAttributeEditorElement *QgsAttributeEditorQmlElement::clone( QgsAttributeEditorElement *parent ) const
{
QgsAttributeEditorQmlElement *element = new QgsAttributeEditorQmlElement( parent );
QgsAttributeEditorQmlElement *element = new QgsAttributeEditorQmlElement( name(), parent );
element->setQmlCode( mQmlCode );

return element;
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsattributeeditorelement.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ class CORE_EXPORT QgsAttributeEditorRelation : public QgsAttributeEditorElement
class CORE_EXPORT QgsAttributeEditorQmlElement : public QgsAttributeEditorElement
{
public:
QgsAttributeEditorQmlElement( QgsAttributeEditorElement *parent )
: QgsAttributeEditorElement( AeTypeQmlElement, "TODO NAME", parent )
QgsAttributeEditorQmlElement( const QString &name, QgsAttributeEditorElement *parent )
: QgsAttributeEditorElement( AeTypeQmlElement, name, parent )
{}

QgsAttributeEditorElement *clone( QgsAttributeEditorElement *parent ) const override SIP_FACTORY;
Expand Down
10 changes: 8 additions & 2 deletions src/core/qgseditformconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,13 +558,13 @@ QgsAttributeEditorElement *QgsEditFormConfig::attributeEditorElementFromDomEleme

newElement = container;
}
else if ( elem.tagName() == QLatin1String( "attributeEditorField" ) )
else if ( elem.tagName() == QStringLiteral( "attributeEditorField" ) )
{
QString name = elem.attribute( QStringLiteral( "name" ) );
int idx = d->mFields.lookupField( name );
newElement = new QgsAttributeEditorField( name, idx, parent );
}
else if ( elem.tagName() == QLatin1String( "attributeEditorRelation" ) )
else if ( elem.tagName() == QStringLiteral( "attributeEditorRelation" ) )
{
// At this time, the relations are not loaded
// So we only grab the id and delegate the rest to onRelationsLoaded()
Expand All @@ -573,6 +573,12 @@ QgsAttributeEditorElement *QgsEditFormConfig::attributeEditorElementFromDomEleme
relElement->setShowUnlinkButton( elem.attribute( QStringLiteral( "showUnlinkButton" ), QStringLiteral( "1" ) ).toInt() );
newElement = relElement;
}
else if ( elem.tagName() == QStringLiteral( "attributeEditorQmlElement" ) )
{
QgsAttributeEditorQmlElement *qmlElement = new QgsAttributeEditorQmlElement( elem.attribute( QStringLiteral( "name" ) ), parent );
qmlElement->setQmlCode( elem.text() );
newElement = qmlElement;
}

if ( newElement )
{
Expand Down
31 changes: 14 additions & 17 deletions src/gui/editorwidgets/qgsqmlwidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,29 @@ void QgsQmlWidgetWrapper::initWidget( QWidget *editor )
if ( !quickWidget )
return;


if ( !mQmlFile.open() )
{
QgsMessageLog::logMessage( tr( "Failed to open temporary QML file" ) );
return;
}

QString qmlCode = QStringLiteral(
"import QtQuick 2.0"
"import QtCharts 2.0"
""
"ChartView {"
" width: 600"
" height: 400"
""
" PieSeries {"
" id: pieSeries"
" PieSlice { label: \"outlet 1\"; value: attributes.outlet_1 }"
" PieSlice { label: \"outlet 2\"; value: attributes.outlet_2 }"
" PieSlice { label: \"outlet 3\"; value: attributes.outlet_3 }"
" PieSlice { label: \"outlet 4\"; value: attributes.outlet_4 }"
" }"
"}" );
quickWidget->setSource( QUrl::fromLocalFile( mQmlFile.fileName() ) );

mQmlFile.close();
}

void QgsQmlWidgetWrapper::setQmlCode( const QString &qmlCode )
{
if ( !mQmlFile.open() )
{
QgsMessageLog::logMessage( tr( "Failed to open temporary QML file" ) );
return;
}

mQmlFile.write( qmlCode.toUtf8() );

quickWidget->setSource( QUrl::fromLocalFile( mQmlFile.fileName() ) );
mQmlFile.close();
}

void QgsQmlWidgetWrapper::setFeature( const QgsFeature &feature )
Expand Down
5 changes: 5 additions & 0 deletions src/gui/editorwidgets/qgsqmlwidgetwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "qgswidgetwrapper.h"
#include "qgis.h"
#include "qgis_gui.h"
#include <QtQuickWidgets/QQuickWidget>


class GUI_EXPORT QgsQmlWidgetWrapper : public QgsWidgetWrapper
Expand All @@ -34,12 +35,16 @@ class GUI_EXPORT QgsQmlWidgetWrapper : public QgsWidgetWrapper

void initWidget( QWidget *editor ) override;

void setQmlCode( const QString &qmlCode );

public slots:

void setFeature( const QgsFeature &feature ) override;

private:
QTemporaryFile mQmlFile;

QQuickWidget *mWidget = nullptr;
};

#endif // QGSQMLWIDGETWRAPPER_H
10 changes: 5 additions & 5 deletions src/gui/qgsattributeform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1775,11 +1775,10 @@ QgsAttributeForm::WidgetInfo QgsAttributeForm::createWidgetFromDef( const QgsAtt
const QgsAttributeEditorQmlElement *elementDef = static_cast<const QgsAttributeEditorQmlElement *>( widgetDef );

QgsQmlWidgetWrapper *qmlWrapper = new QgsQmlWidgetWrapper( mLayer, nullptr, this );
QTemporaryFile *qmlFile = new QTemporaryFile();
qmlFile->write( elementDef->qmlCode().toUtf8() );
qmlFile->setParent( qmlWrapper );

qmlWrapper->setConfig( mLayer->editFormConfig().widgetConfig( "TODO NAME??" ) );
qmlWrapper->setQmlCode( elementDef->qmlCode() );

//qmlWrapper->setConfig( mLayer->editFormConfig().widgetConfig( elementDef->name()) );
qmlWrapper->setContext( context );

// QgsAttributeFormRelationEditorWidget *formWidget = new QgsAttributeFormRelationEditorWidget( rww, this );
Expand All @@ -1788,8 +1787,9 @@ QgsAttributeForm::WidgetInfo QgsAttributeForm::createWidgetFromDef( const QgsAtt
// mFormWidgets.append( formWidget );

newWidgetInfo.widget = qmlWrapper->widget();
newWidgetInfo.labelText = QString();
newWidgetInfo.labelText = elementDef->name();
newWidgetInfo.labelOnTop = true;
newWidgetInfo.showLabel = widgetDef->showLabel();
break;
}

Expand Down

0 comments on commit e8e32fa

Please sign in to comment.