Skip to content
Permalink
Browse files

Merge pull request #9616 from elpaso/html-widget

HTML form widget
  • Loading branch information
elpaso committed Mar 26, 2019
2 parents bfd28b2 + 0aa326b commit 09fe865094842bf4ea11df5c72f93503161af0a1
@@ -48,7 +48,8 @@ layer.
AeTypeField,
AeTypeRelation,
AeTypeInvalid,
AeTypeQmlElement
AeTypeQmlElement,
AeTypeHtmlElement
};

QgsAttributeEditorElement( AttributeEditorType type, const QString &name, QgsAttributeEditorElement *parent = 0 );
@@ -394,13 +395,51 @@ The QML code that will be represented within this widget.

void setQmlCode( const QString &qmlCode );
%Docstring
Sets the QML code that will be represented within this widget to ``qmlCode``.
%End

};


class QgsAttributeEditorHtmlElement : QgsAttributeEditorElement
{
%Docstring
An attribute editor widget that will represent arbitrary HTML code.

.. versionadded:: 3.10
%End

%TypeHeaderCode
#include "qgsattributeeditorelement.h"
%End
public:

QgsAttributeEditorHtmlElement( const QString &name, QgsAttributeEditorElement *parent );
%Docstring
Creates a new element which can display HTML

:param name: The name of the widget
:param parent: The parent (used as container)
%End

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


QString htmlCode() const;
%Docstring
The QML code that will be represented within this widget.

@param qmlCode
.. versionadded:: 3.4
%End

void setHtmlCode( const QString &htmlCode );
%Docstring
Sets the HTML code that will be represented within this widget to ``htmlCode``.
%End

};


/************************************************************************
* This file has been generated automatically from *
* *
@@ -15,6 +15,7 @@
%ModuleCode
#include "qgsrelationwidgetwrapper.h"
#include "qgsqmlwidgetwrapper.h"
#include "qgshtmlwidgetwrapper.h"
%End

class QgsWidgetWrapper : QObject
@@ -40,6 +41,8 @@ changed status of the widget will be saved.
sipType = sipType_QgsRelationWidgetWrapper;
else if ( qobject_cast<QgsQmlWidgetWrapper *>( sipCpp ) )
sipType = sipType_QgsQmlWidgetWrapper;
else if ( qobject_cast<QgsHtmlWidgetWrapper *>( sipCpp ) )
sipType = sipType_QgsHtmlWidgetWrapper;
else
sipType = 0;
%End
@@ -0,0 +1,65 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/editorwidgets/qgshtmlwidgetwrapper.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/


class QgsHtmlWidgetWrapper : QgsWidgetWrapper
{
%Docstring
Wraps a QQuickWidget to display HTML code

.. versionadded:: 3.4
%End

%TypeHeaderCode
#include "qgshtmlwidgetwrapper.h"
%End
public:

QgsHtmlWidgetWrapper( QgsVectorLayer *layer, QWidget *editor, QWidget *parent );
%Docstring
Create a html widget wrapper

:param layer: The layer on which the feature is
:param editor: An editor widget. Can be ``None`` if one should be autogenerated.
:param parent: A parent widget
%End

virtual bool valid() const;


virtual QWidget *createWidget( QWidget *parent );


virtual void initWidget( QWidget *editor );


void reinitWidget();
%Docstring
Clears the content and makes new initialization
%End

void setHtmlCode( const QString &htmlCode );
%Docstring
Sets the HTML code to ``htmlCode``
%End

public slots:
virtual void setFeature( const QgsFeature &feature );


};



/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/editorwidgets/qgshtmlwidgetwrapper.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -287,6 +287,7 @@
%Include auto_generated/editorwidgets/qgsdatetimesearchwidgetwrapper.sip
%Include auto_generated/editorwidgets/qgsdefaultsearchwidgetwrapper.sip
%Include auto_generated/editorwidgets/qgsdoublespinbox.sip
%Include auto_generated/editorwidgets/qgshtmlwidgetwrapper.sip
%Include auto_generated/editorwidgets/qgsmultiedittoolbutton.sip
%Include auto_generated/editorwidgets/qgsrelationreferencesearchwidgetwrapper.sip
%Include auto_generated/editorwidgets/qgsrelationreferencewidget.sip
@@ -20,8 +20,10 @@
#include "qgisapp.h"
#include "qgsfieldcombobox.h"
#include "qgsqmlwidgetwrapper.h"
#include "qgshtmlwidgetwrapper.h"
#include "qgsapplication.h"
#include "qgscolorbutton.h"
#include "qgscodeeditorhtml.h"

QgsAttributesFormProperties::QgsAttributesFormProperties( QgsVectorLayer *layer, QWidget *parent )
: QWidget( parent )
@@ -140,14 +142,17 @@ void QgsAttributesFormProperties::initAvailableWidgetsTree()
}
catitem->setExpanded( true );

// QML widget
// QML/HTML widget
catItemData = DnDTreeItemData( DnDTreeItemData::Container, QStringLiteral( "Other" ), tr( "Other Widgets" ) );
catitem = mAvailableWidgetsTree->addItem( mAvailableWidgetsTree->invisibleRootItem(), catItemData );

DnDTreeItemData itemData = DnDTreeItemData( DnDTreeItemData::QmlWidget, QStringLiteral( "QmlWidget" ), tr( "QML Widget" ) );
itemData.setShowLabel( true );

mAvailableWidgetsTree->addItem( catitem, itemData );

auto itemDataHtml { DnDTreeItemData( DnDTreeItemData::HtmlWidget, QStringLiteral( "HtmlWidget" ), tr( "HTML Widget" ) ) };
itemDataHtml.setShowLabel( true );
mAvailableWidgetsTree->addItem( catitem, itemDataHtml );
catitem ->setExpanded( true );
}

@@ -479,6 +484,19 @@ QTreeWidgetItem *QgsAttributesFormProperties::loadAttributeEditorTreeItem( QgsAt
newWidget = tree->addItem( parent, itemData );
break;
}

case QgsAttributeEditorElement::AeTypeHtmlElement:
{
const QgsAttributeEditorHtmlElement *htmlElementEditor = static_cast<const QgsAttributeEditorHtmlElement *>( widgetDef );
DnDTreeItemData itemData = DnDTreeItemData( DnDTreeItemData::HtmlWidget, widgetDef->name(), widgetDef->name() );
itemData.setShowLabel( widgetDef->showLabel() );
HtmlElementEditorConfiguration htmlEdConfig;
htmlEdConfig.htmlCode = htmlElementEditor->htmlCode();
itemData.setHtmlElementEditorConfiguration( htmlEdConfig );
newWidget = tree->addItem( parent, itemData );
break;
}

case QgsAttributeEditorElement::AeTypeInvalid:
{
QgsDebugMsg( QStringLiteral( "Not loading invalid attribute editor type..." ) );
@@ -520,6 +538,12 @@ void QgsAttributesFormProperties::onAttributeSelectionChanged()
mAttributeTypeDialog->setVisible( false );
break;
}
case DnDTreeItemData::HtmlWidget:
{
mAttributeRelationEdit->setVisible( false );
mAttributeTypeDialog->setVisible( false );
break;
}

}
}
@@ -620,6 +644,15 @@ QgsAttributeEditorElement *QgsAttributesFormProperties::createAttributeEditorWid
widgetDef = element;
break;
}

case DnDTreeItemData::HtmlWidget:
{
QgsAttributeEditorHtmlElement *element = new QgsAttributeEditorHtmlElement( item->text( 0 ), parent );
element->setHtmlCode( itemData.htmlElementEditorConfiguration().htmlCode );
widgetDef = element;
break;
}

}

widgetDef->setShowLabel( itemData.showLabel() );
@@ -875,6 +908,10 @@ QTreeWidgetItem *DnDTree::addItem( QTreeWidgetItem *parent, QgsAttributesFormPro
case QgsAttributesFormProperties::DnDTreeItemData::QmlWidget:
//no icon for QmlWidget
break;

case QgsAttributesFormProperties::DnDTreeItemData::HtmlWidget:
//no icon for HtmlWidget
break;
}
}
newItem->setData( 0, QgsAttributesFormProperties::DnDTreeRole, data );
@@ -955,6 +992,11 @@ bool DnDTree::dropMimeData( QTreeWidgetItem *parent, int index, const QMimeData
{
onItemDoubleClicked( newItem, 0 );
}

if ( itemElement.type() == QgsAttributesFormProperties::DnDTreeItemData::HtmlWidget )
{
onItemDoubleClicked( newItem, 0 );
}
}
}

@@ -1295,6 +1337,85 @@ void DnDTree::onItemDoubleClicked( QTreeWidgetItem *item, int column )
}
break;

case QgsAttributesFormProperties::DnDTreeItemData::HtmlWidget:
{
QDialog dlg;
dlg.setWindowTitle( tr( "Configure HTML Widget" ) );

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

QLineEdit *title = new QLineEdit( itemData.name() );

//htmlCode
QgsCodeEditorHTML *htmlCode = new QgsCodeEditorHTML( );
htmlCode->setSizePolicy( QSizePolicy::Policy::Expanding, QSizePolicy::Policy::Expanding );
htmlCode->setText( itemData.htmlElementEditorConfiguration().htmlCode );

QgsHtmlWidgetWrapper *htmlWrapper = new QgsHtmlWidgetWrapper( mLayer, nullptr, this );
QgsFeature previewFeature;
mLayer->getFeatures().nextFeature( previewFeature );

//update preview on text change
connect( htmlCode, &QgsCodeEditorHTML::textChanged, this, [ = ]
{
htmlWrapper->setHtmlCode( htmlCode->text( ) );
htmlWrapper->reinitWidget();
htmlWrapper->setFeature( previewFeature );
} );

QgsFieldExpressionWidget *expressionWidget = new QgsFieldExpressionWidget;
expressionWidget->setLayer( mLayer );
QToolButton *addExpressionButton = new QToolButton();
addExpressionButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/symbologyAdd.svg" ) ) );

connect( addExpressionButton, &QAbstractButton::clicked, this, [ = ]
{
htmlCode->insertText( QStringLiteral( "<script>document.write(expression.evaluate(\"%1\"));</script>" ).arg( expressionWidget->expression().replace( '"', QLatin1String( "\\\"" ) ) ) );
} );

layout->addWidget( new QLabel( tr( "Title" ) ) );
layout->addWidget( title );
QGroupBox *expressionWidgetBox = new QGroupBox( tr( "HTML Code" ) );
layout->addWidget( expressionWidgetBox );
expressionWidgetBox->setLayout( new QHBoxLayout );
expressionWidgetBox->layout()->addWidget( expressionWidget );
expressionWidgetBox->layout()->addWidget( addExpressionButton );
layout->addWidget( htmlCode );
QScrollArea *htmlPreviewBox = new QScrollArea();
htmlPreviewBox->setLayout( new QGridLayout );
htmlPreviewBox->setMinimumWidth( 400 );
htmlPreviewBox->layout()->addWidget( htmlWrapper->widget() );
//emit to load preview for the first time
emit htmlCode->textChanged();
htmlLayout->addWidget( htmlPreviewBox );

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

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

mainLayout->addWidget( buttonBox );

if ( dlg.exec() )
{
QgsAttributesFormProperties::HtmlElementEditorConfiguration htmlEdCfg;
htmlEdCfg.htmlCode = htmlCode->text();
itemData.setName( title->text() );
itemData.setHtmlElementEditorConfiguration( htmlEdCfg );
itemData.setShowLabel( showLabelCheckbox->isChecked() );

item->setData( 0, QgsAttributesFormProperties::DnDTreeRole, itemData );
item->setText( 0, title->text() );
}
}
break;

case QgsAttributesFormProperties::DnDTreeItemData::Field:
{
QDialog dlg;
@@ -1407,6 +1528,17 @@ void QgsAttributesFormProperties::DnDTreeItemData::setQmlElementEditorConfigurat
mQmlElementEditorConfiguration = qmlElementEditorConfiguration;
}


QgsAttributesFormProperties::HtmlElementEditorConfiguration QgsAttributesFormProperties::DnDTreeItemData::htmlElementEditorConfiguration() const
{
return mHtmlElementEditorConfiguration;
}

void QgsAttributesFormProperties::DnDTreeItemData::setHtmlElementEditorConfiguration( QgsAttributesFormProperties::HtmlElementEditorConfiguration htmlElementEditorConfiguration )
{
mHtmlElementEditorConfiguration = htmlElementEditorConfiguration;
}

QColor QgsAttributesFormProperties::DnDTreeItemData::backgroundColor() const
{
return mBackgroundColor;

0 comments on commit 09fe865

Please sign in to comment.
You can’t perform that action at this time.