Skip to content

Commit 6ac41b4

Browse files
committed
preview
1 parent 7fd8732 commit 6ac41b4

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

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

Lines changed: 2 additions & 0 deletions
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 reinitWidget();
47+
4648
void setQmlCode( const QString &qmlCode );
4749

4850
public slots:

src/app/qgsattributesformproperties.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgsattributesforminitcode.h"
2020
#include "qgisapp.h"
2121
#include "qgsfieldcombobox.h"
22+
#include "qgsqmlwidgetwrapper.h"
2223

2324
QgsAttributesFormProperties::QgsAttributesFormProperties( QgsVectorLayer *layer, QWidget *parent )
2425
: QWidget( parent )
@@ -1105,10 +1106,14 @@ void DnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int column )
11051106
case QgsAttributesFormProperties::DnDTreeItemData::QmlWidget:
11061107
{
11071108
QDialog dlg;
1108-
dlg.resize( 600, 400 );
11091109
dlg.setWindowTitle( tr( "Configure QML Widget" ) );
1110-
QFormLayout *layout = new QFormLayout() ;
1111-
dlg.setLayout( layout );
1110+
1111+
QVBoxLayout *mainLayout = new QVBoxLayout();
1112+
QHBoxLayout *qmlLayout = new QHBoxLayout();
1113+
QFormLayout *layout = new QFormLayout();
1114+
mainLayout->addLayout( qmlLayout );
1115+
qmlLayout->addLayout( layout );
1116+
dlg.setLayout( mainLayout );
11121117
layout->addWidget( baseWidget );
11131118

11141119
//widget title
@@ -1166,20 +1171,31 @@ void DnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int column )
11661171
} );
11671172

11681173

1174+
QgsQmlWidgetWrapper *qmlWrapper = new QgsQmlWidgetWrapper( mLayer, nullptr, this );
1175+
qmlWrapper->setQmlCode( qmlCode->toPlainText() );
1176+
connect( qmlCode, &QPlainTextEdit::textChanged, this, [ = ]
1177+
{
1178+
qmlWrapper->setQmlCode( qmlCode->toPlainText() );
1179+
qmlWrapper->reinitWidget();
1180+
} );
1181+
1182+
11691183
layout->addRow( tr( "Title" ), title );
11701184
QGroupBox *qmlCodeBox = new QGroupBox( tr( "QML Code" ) );
11711185
qmlCodeBox->setLayout( new QGridLayout );
11721186
qmlCodeBox->layout()->addWidget( qmlObjectTemplate );
11731187
qmlCodeBox->layout()->addWidget( attributeFieldCombo );
11741188
qmlCodeBox->layout()->addWidget( qmlCode );
11751189
layout->addRow( qmlCodeBox );
1190+
qmlLayout->addWidget( qmlWrapper->widget() );
11761191

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

11791194
connect( buttonBox, &QDialogButtonBox::accepted, &dlg, &QDialog::accept );
11801195
connect( buttonBox, &QDialogButtonBox::rejected, &dlg, &QDialog::reject );
11811196

1182-
dlg.layout()->addWidget( buttonBox );
1197+
mainLayout->addWidget( buttonBox );
1198+
11831199

11841200
if ( dlg.exec() )
11851201
{

src/gui/editorwidgets/qgsqmlwidgetwrapper.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <QtQuickWidgets/QQuickWidget>
1919
#include <QQuickWidget>
2020
#include <QQmlContext>
21+
#include <QQmlEngine>
2122

2223
QgsQmlWidgetWrapper::QgsQmlWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
2324
: QgsWidgetWrapper( layer, editor, parent )
@@ -54,6 +55,25 @@ void QgsQmlWidgetWrapper::initWidget( QWidget *editor )
5455
mQmlFile.close();
5556
}
5657

58+
59+
void QgsQmlWidgetWrapper::reinitWidget( )
60+
{
61+
if ( !mWidget )
62+
return;
63+
64+
if ( !mQmlFile.open() )
65+
{
66+
QgsMessageLog::logMessage( tr( "Failed to open temporary QML file" ) );
67+
return;
68+
}
69+
70+
mWidget->engine()->clearComponentCache();
71+
mWidget->setSource( QUrl::fromLocalFile( mQmlFile.fileName() ) );
72+
73+
mQmlFile.close();
74+
}
75+
76+
5777
void QgsQmlWidgetWrapper::setQmlCode( const QString &qmlCode )
5878
{
5979
if ( !mQmlFile.open() )
@@ -62,6 +82,7 @@ void QgsQmlWidgetWrapper::setQmlCode( const QString &qmlCode )
6282
return;
6383
}
6484

85+
mQmlFile.resize( 0 );
6586
mQmlFile.write( qmlCode.toUtf8() );
6687

6788
mQmlFile.close();

src/gui/editorwidgets/qgsqmlwidgetwrapper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class GUI_EXPORT QgsQmlWidgetWrapper : public QgsWidgetWrapper
3535

3636
void initWidget( QWidget *editor ) override;
3737

38+
void reinitWidget();
39+
3840
void setQmlCode( const QString &qmlCode );
3941

4042
public slots:

0 commit comments

Comments
 (0)