Skip to content

Commit

Permalink
preview
Browse files Browse the repository at this point in the history
  • Loading branch information
signedav committed Sep 5, 2018
1 parent 7fd8732 commit 6ac41b4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
Expand Up @@ -43,6 +43,8 @@ the Free Software Foundation; either version 2 of the License, or *
virtual void initWidget( QWidget *editor );


void reinitWidget();

void setQmlCode( const QString &qmlCode );

public slots:
Expand Down
24 changes: 20 additions & 4 deletions src/app/qgsattributesformproperties.cpp
Expand Up @@ -19,6 +19,7 @@
#include "qgsattributesforminitcode.h"
#include "qgisapp.h"
#include "qgsfieldcombobox.h"
#include "qgsqmlwidgetwrapper.h"

QgsAttributesFormProperties::QgsAttributesFormProperties( QgsVectorLayer *layer, QWidget *parent )
: QWidget( parent )
Expand Down Expand Up @@ -1105,10 +1106,14 @@ void DnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int column )
case QgsAttributesFormProperties::DnDTreeItemData::QmlWidget:
{
QDialog dlg;
dlg.resize( 600, 400 );
dlg.setWindowTitle( tr( "Configure QML Widget" ) );
QFormLayout *layout = new QFormLayout() ;
dlg.setLayout( layout );

QVBoxLayout *mainLayout = new QVBoxLayout();
QHBoxLayout *qmlLayout = new QHBoxLayout();
QFormLayout *layout = new QFormLayout();
mainLayout->addLayout( qmlLayout );
qmlLayout->addLayout( layout );
dlg.setLayout( mainLayout );
layout->addWidget( baseWidget );

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


QgsQmlWidgetWrapper *qmlWrapper = new QgsQmlWidgetWrapper( mLayer, nullptr, this );
qmlWrapper->setQmlCode( qmlCode->toPlainText() );
connect( qmlCode, &QPlainTextEdit::textChanged, this, [ = ]
{
qmlWrapper->setQmlCode( qmlCode->toPlainText() );
qmlWrapper->reinitWidget();
} );


layout->addRow( tr( "Title" ), title );
QGroupBox *qmlCodeBox = new QGroupBox( tr( "QML Code" ) );
qmlCodeBox->setLayout( new QGridLayout );
qmlCodeBox->layout()->addWidget( qmlObjectTemplate );
qmlCodeBox->layout()->addWidget( attributeFieldCombo );
qmlCodeBox->layout()->addWidget( qmlCode );
layout->addRow( qmlCodeBox );
qmlLayout->addWidget( qmlWrapper->widget() );

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

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

dlg.layout()->addWidget( buttonBox );
mainLayout->addWidget( buttonBox );


if ( dlg.exec() )
{
Expand Down
21 changes: 21 additions & 0 deletions src/gui/editorwidgets/qgsqmlwidgetwrapper.cpp
Expand Up @@ -18,6 +18,7 @@
#include <QtQuickWidgets/QQuickWidget>
#include <QQuickWidget>
#include <QQmlContext>
#include <QQmlEngine>

QgsQmlWidgetWrapper::QgsQmlWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent )
: QgsWidgetWrapper( layer, editor, parent )
Expand Down Expand Up @@ -54,6 +55,25 @@ void QgsQmlWidgetWrapper::initWidget( QWidget *editor )
mQmlFile.close();
}


void QgsQmlWidgetWrapper::reinitWidget( )
{
if ( !mWidget )
return;

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

mWidget->engine()->clearComponentCache();
mWidget->setSource( QUrl::fromLocalFile( mQmlFile.fileName() ) );

mQmlFile.close();
}


void QgsQmlWidgetWrapper::setQmlCode( const QString &qmlCode )
{
if ( !mQmlFile.open() )
Expand All @@ -62,6 +82,7 @@ void QgsQmlWidgetWrapper::setQmlCode( const QString &qmlCode )
return;
}

mQmlFile.resize( 0 );
mQmlFile.write( qmlCode.toUtf8() );

mQmlFile.close();
Expand Down
2 changes: 2 additions & 0 deletions src/gui/editorwidgets/qgsqmlwidgetwrapper.h
Expand Up @@ -35,6 +35,8 @@ class GUI_EXPORT QgsQmlWidgetWrapper : public QgsWidgetWrapper

void initWidget( QWidget *editor ) override;

void reinitWidget();

void setQmlCode( const QString &qmlCode );

public slots:
Expand Down

0 comments on commit 6ac41b4

Please sign in to comment.