Skip to content

Commit e8e32fa

Browse files
committed
Configuration and display
works in basic - no attributs. no handy configurator.
1 parent f41aaa7 commit e8e32fa

10 files changed

+118
-41
lines changed

python/core/auto_generated/qgsattributeeditorelement.sip.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ An attribute editor widget that will represent arbitrary QML code.
358358
#include "qgsattributeeditorelement.h"
359359
%End
360360
public:
361-
QgsAttributeEditorQmlElement( QgsAttributeEditorElement *parent );
361+
QgsAttributeEditorQmlElement( const QString &name, QgsAttributeEditorElement *parent );
362362

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

python/gui/auto_generated/editorwidgets/qgsqmlwidgetwrapper.sip.in

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ the Free Software Foundation; either version 2 of the License, or *
4343
virtual void initWidget( QWidget *editor );
4444

4545

46+
void setQmlCode( const QString &qmlCode );
47+
4648
public slots:
4749

4850
virtual void setFeature( const QgsFeature &feature );

src/app/qgsattributesformproperties.cpp

+70-13
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,20 @@ QTreeWidgetItem *QgsAttributesFormProperties::loadAttributeEditorTreeItem( QgsAt
456456
{
457457
loadAttributeEditorTreeItem( wdg, newWidget, tree );
458458
}
459+
break;
460+
}
461+
462+
case QgsAttributeEditorElement::AeTypeQmlElement:
463+
{
464+
const QgsAttributeEditorQmlElement *qmlElementEditor = static_cast<const QgsAttributeEditorQmlElement *>( widgetDef );
465+
DnDTreeItemData itemData = DnDTreeItemData( DnDTreeItemData::QmlWidget, widgetDef->name(), widgetDef->name() );
466+
itemData.setShowLabel( widgetDef->showLabel() );
467+
QmlElementEditorConfiguration qmlEdConfig;
468+
qmlEdConfig.qmlCode = qmlElementEditor->qmlCode();
469+
itemData.setQmlElementEditorConfiguration( qmlEdConfig );
470+
newWidget = tree->addItem( parent, itemData );
471+
break;
459472
}
460-
break;
461473
default:
462474
//should not happen
463475
break;
@@ -580,8 +592,43 @@ QgsAttributeEditorElement *QgsAttributesFormProperties::createAttributeEditorWid
580592

581593
case DnDTreeItemData::QmlWidget:
582594
{
583-
QgsAttributeEditorQmlElement *element = new QgsAttributeEditorQmlElement( parent );
584-
element->setQmlCode( "ABC " );
595+
QgsAttributeEditorQmlElement *element = new QgsAttributeEditorQmlElement( item->text( 0 ), parent );
596+
element->setQmlCode( itemData.qmlElementEditorConfiguration().qmlCode );
597+
598+
/*
599+
element->setQmlCode( QStringLiteral(
600+
"import QtQuick 2.0\n"
601+
"\n"
602+
"Rectangle {\n"
603+
" width: 100\n"
604+
" height: 100\n"
605+
" color: \"red\"\n"
606+
"}\n" ) );
607+
608+
element->setQmlCode( QStringLiteral(
609+
"import QtQuick 2.0\n"
610+
"import QtCharts 2.0\n"
611+
"\n"
612+
"ChartView {\n"
613+
" width: 600\n"
614+
" height: 400\n"
615+
"\n"
616+
" PieSeries {\n"
617+
" id: pieSeries\n"
618+
" PieSlice { label: \"outlet 1\"; value: attributes.outlet_1 }\n"
619+
" PieSlice { label: \"outlet 2\"; value: attributes.outlet_2 }\n"
620+
" PieSlice { label: \"outlet 3\"; value: attributes.outlet_3 }\n"
621+
" PieSlice { label: \"outlet 4\"; value: attributes.outlet_4 }\n"
622+
" }\n"
623+
"}\n" ) );
624+
import QtQuick 2.0
625+
626+
Rectangle {
627+
width: 100
628+
height: 100
629+
color: "red"
630+
}
631+
*/
585632

586633
widgetDef = element;
587634
break;
@@ -1087,12 +1134,11 @@ void DnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int column )
10871134
dlg.setLayout( layout );
10881135
layout->addWidget( baseWidget );
10891136

1090-
QCheckBox *showLinkButton = new QCheckBox( tr( "Show link button" ) );
1091-
showLinkButton->setChecked( itemData.relationEditorConfiguration().showLinkButton );
1092-
QCheckBox *showUnlinkButton = new QCheckBox( tr( "Show unlink button" ) );
1093-
showUnlinkButton->setChecked( itemData.relationEditorConfiguration().showUnlinkButton );
1094-
layout->addRow( showLinkButton );
1095-
layout->addRow( showUnlinkButton );
1137+
QPlainTextEdit *qmlCode = new QPlainTextEdit( itemData.qmlElementEditorConfiguration().qmlCode );
1138+
QLineEdit *title = new QLineEdit( itemData.name() );
1139+
1140+
layout->addRow( tr( "Title" ), title );
1141+
layout->addRow( tr( "QML Code" ), qmlCode );
10961142

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

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

11041150
if ( dlg.exec() )
11051151
{
1106-
QgsAttributesFormProperties::RelationEditorConfiguration relEdCfg;
1107-
relEdCfg.showLinkButton = showLinkButton->isChecked();
1108-
relEdCfg.showUnlinkButton = showUnlinkButton->isChecked();
1152+
QgsAttributesFormProperties::QmlElementEditorConfiguration qmlEdCfg;
1153+
qmlEdCfg.qmlCode = qmlCode->toPlainText();
1154+
itemData.setName( title->text() );
1155+
itemData.setQmlElementEditorConfiguration( qmlEdCfg );
11091156
itemData.setShowLabel( showLabelCheckbox->isChecked() );
1110-
itemData.setRelationEditorConfiguration( relEdCfg );
11111157

11121158
item->setData( 0, QgsAttributesFormProperties::DnDTreeRole, itemData );
1159+
item->setText( 0, title->text() );
11131160
}
11141161
}
11151162
break;
@@ -1216,3 +1263,13 @@ void QgsAttributesFormProperties::DnDTreeItemData::setRelationEditorConfiguratio
12161263
mRelationEditorConfiguration = relationEditorConfiguration;
12171264
}
12181265

1266+
QgsAttributesFormProperties::QmlElementEditorConfiguration QgsAttributesFormProperties::DnDTreeItemData::qmlElementEditorConfiguration() const
1267+
{
1268+
return mQmlElementEditorConfiguration;
1269+
}
1270+
1271+
void QgsAttributesFormProperties::DnDTreeItemData::setQmlElementEditorConfiguration( QgsAttributesFormProperties::QmlElementEditorConfiguration qmlElementEditorConfiguration )
1272+
{
1273+
mQmlElementEditorConfiguration = qmlElementEditorConfiguration;
1274+
}
1275+

src/app/qgsattributesformproperties.h

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <QFileDialog>
3030
#include <QHBoxLayout>
3131
#include <QFormLayout>
32+
#include <QPlainTextEdit>
3233

3334
#include "qgsvectorlayer.h"
3435
#include "ui_qgsattributesformproperties.h"
@@ -70,6 +71,11 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
7071
bool showUnlinkButton = true;
7172
};
7273

74+
struct QmlElementEditorConfiguration
75+
{
76+
QString qmlCode;
77+
};
78+
7379
class DnDTreeItemData : public QTreeWidgetItem
7480
{
7581
public:
@@ -116,6 +122,9 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
116122
RelationEditorConfiguration relationEditorConfiguration() const;
117123
void setRelationEditorConfiguration( RelationEditorConfiguration relationEditorConfiguration );
118124

125+
QmlElementEditorConfiguration qmlElementEditorConfiguration() const;
126+
void setQmlElementEditorConfiguration( QmlElementEditorConfiguration qmlElementEditorConfiguration );
127+
119128
private:
120129
Type mType = Field;
121130
QString mName;
@@ -126,6 +135,7 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
126135
bool mShowLabel = true;
127136
QgsOptionalExpression mVisibilityExpression;
128137
RelationEditorConfiguration mRelationEditorConfiguration;
138+
QmlElementEditorConfiguration mQmlElementEditorConfiguration;
129139
};
130140

131141

src/core/qgsattributeeditorelement.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void QgsAttributeEditorRelation::setShowUnlinkButton( bool showUnlinkButton )
153153

154154
QgsAttributeEditorElement *QgsAttributeEditorQmlElement::clone( QgsAttributeEditorElement *parent ) const
155155
{
156-
QgsAttributeEditorQmlElement *element = new QgsAttributeEditorQmlElement( parent );
156+
QgsAttributeEditorQmlElement *element = new QgsAttributeEditorQmlElement( name(), parent );
157157
element->setQmlCode( mQmlCode );
158158

159159
return element;

src/core/qgsattributeeditorelement.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@ class CORE_EXPORT QgsAttributeEditorRelation : public QgsAttributeEditorElement
420420
class CORE_EXPORT QgsAttributeEditorQmlElement : public QgsAttributeEditorElement
421421
{
422422
public:
423-
QgsAttributeEditorQmlElement( QgsAttributeEditorElement *parent )
424-
: QgsAttributeEditorElement( AeTypeQmlElement, "TODO NAME", parent )
423+
QgsAttributeEditorQmlElement( const QString &name, QgsAttributeEditorElement *parent )
424+
: QgsAttributeEditorElement( AeTypeQmlElement, name, parent )
425425
{}
426426

427427
QgsAttributeEditorElement *clone( QgsAttributeEditorElement *parent ) const override SIP_FACTORY;

src/core/qgseditformconfig.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -558,13 +558,13 @@ QgsAttributeEditorElement *QgsEditFormConfig::attributeEditorElementFromDomEleme
558558

559559
newElement = container;
560560
}
561-
else if ( elem.tagName() == QLatin1String( "attributeEditorField" ) )
561+
else if ( elem.tagName() == QStringLiteral( "attributeEditorField" ) )
562562
{
563563
QString name = elem.attribute( QStringLiteral( "name" ) );
564564
int idx = d->mFields.lookupField( name );
565565
newElement = new QgsAttributeEditorField( name, idx, parent );
566566
}
567-
else if ( elem.tagName() == QLatin1String( "attributeEditorRelation" ) )
567+
else if ( elem.tagName() == QStringLiteral( "attributeEditorRelation" ) )
568568
{
569569
// At this time, the relations are not loaded
570570
// So we only grab the id and delegate the rest to onRelationsLoaded()
@@ -573,6 +573,12 @@ QgsAttributeEditorElement *QgsEditFormConfig::attributeEditorElementFromDomEleme
573573
relElement->setShowUnlinkButton( elem.attribute( QStringLiteral( "showUnlinkButton" ), QStringLiteral( "1" ) ).toInt() );
574574
newElement = relElement;
575575
}
576+
else if ( elem.tagName() == QStringLiteral( "attributeEditorQmlElement" ) )
577+
{
578+
QgsAttributeEditorQmlElement *qmlElement = new QgsAttributeEditorQmlElement( elem.attribute( QStringLiteral( "name" ) ), parent );
579+
qmlElement->setQmlCode( elem.text() );
580+
newElement = qmlElement;
581+
}
576582

577583
if ( newElement )
578584
{

src/gui/editorwidgets/qgsqmlwidgetwrapper.cpp

+14-17
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,29 @@ void QgsQmlWidgetWrapper::initWidget( QWidget *editor )
4040
if ( !quickWidget )
4141
return;
4242

43+
4344
if ( !mQmlFile.open() )
4445
{
4546
QgsMessageLog::logMessage( tr( "Failed to open temporary QML file" ) );
4647
return;
4748
}
4849

49-
QString qmlCode = QStringLiteral(
50-
"import QtQuick 2.0"
51-
"import QtCharts 2.0"
52-
""
53-
"ChartView {"
54-
" width: 600"
55-
" height: 400"
56-
""
57-
" PieSeries {"
58-
" id: pieSeries"
59-
" PieSlice { label: \"outlet 1\"; value: attributes.outlet_1 }"
60-
" PieSlice { label: \"outlet 2\"; value: attributes.outlet_2 }"
61-
" PieSlice { label: \"outlet 3\"; value: attributes.outlet_3 }"
62-
" PieSlice { label: \"outlet 4\"; value: attributes.outlet_4 }"
63-
" }"
64-
"}" );
50+
quickWidget->setSource( QUrl::fromLocalFile( mQmlFile.fileName() ) );
51+
52+
mQmlFile.close();
53+
}
54+
55+
void QgsQmlWidgetWrapper::setQmlCode( const QString &qmlCode )
56+
{
57+
if ( !mQmlFile.open() )
58+
{
59+
QgsMessageLog::logMessage( tr( "Failed to open temporary QML file" ) );
60+
return;
61+
}
6562

6663
mQmlFile.write( qmlCode.toUtf8() );
6764

68-
quickWidget->setSource( QUrl::fromLocalFile( mQmlFile.fileName() ) );
65+
mQmlFile.close();
6966
}
7067

7168
void QgsQmlWidgetWrapper::setFeature( const QgsFeature &feature )

src/gui/editorwidgets/qgsqmlwidgetwrapper.h

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgswidgetwrapper.h"
2020
#include "qgis.h"
2121
#include "qgis_gui.h"
22+
#include <QtQuickWidgets/QQuickWidget>
2223

2324

2425
class GUI_EXPORT QgsQmlWidgetWrapper : public QgsWidgetWrapper
@@ -34,12 +35,16 @@ class GUI_EXPORT QgsQmlWidgetWrapper : public QgsWidgetWrapper
3435

3536
void initWidget( QWidget *editor ) override;
3637

38+
void setQmlCode( const QString &qmlCode );
39+
3740
public slots:
3841

3942
void setFeature( const QgsFeature &feature ) override;
4043

4144
private:
4245
QTemporaryFile mQmlFile;
46+
47+
QQuickWidget *mWidget = nullptr;
4348
};
4449

4550
#endif // QGSQMLWIDGETWRAPPER_H

src/gui/qgsattributeform.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -1775,11 +1775,10 @@ QgsAttributeForm::WidgetInfo QgsAttributeForm::createWidgetFromDef( const QgsAtt
17751775
const QgsAttributeEditorQmlElement *elementDef = static_cast<const QgsAttributeEditorQmlElement *>( widgetDef );
17761776

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

1782-
qmlWrapper->setConfig( mLayer->editFormConfig().widgetConfig( "TODO NAME??" ) );
1779+
qmlWrapper->setQmlCode( elementDef->qmlCode() );
1780+
1781+
//qmlWrapper->setConfig( mLayer->editFormConfig().widgetConfig( elementDef->name()) );
17831782
qmlWrapper->setContext( context );
17841783

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

17901789
newWidgetInfo.widget = qmlWrapper->widget();
1791-
newWidgetInfo.labelText = QString();
1790+
newWidgetInfo.labelText = elementDef->name();
17921791
newWidgetInfo.labelOnTop = true;
1792+
newWidgetInfo.showLabel = widgetDef->showLabel();
17931793
break;
17941794
}
17951795

0 commit comments

Comments
 (0)