Skip to content

Commit

Permalink
[feature][needs-docs] HTML form widget
Browse files Browse the repository at this point in the history
Shameless clone of QML widget, with some webview quirks.

Funded by ARPA Piemonte
  • Loading branch information
elpaso committed Mar 23, 2019
1 parent 78b5678 commit b165258
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 5 deletions.
45 changes: 44 additions & 1 deletion python/core/auto_generated/qgsattributeeditorelement.sip.in
Expand Up @@ -48,7 +48,8 @@ layer.
AeTypeField,
AeTypeRelation,
AeTypeInvalid,
AeTypeQmlElement
AeTypeQmlElement,
AeTypeHtmlElement
};

QgsAttributeEditorElement( AttributeEditorType type, const QString &name, QgsAttributeEditorElement *parent = 0 );
Expand Down Expand Up @@ -401,6 +402,48 @@ The QML code that will be represented within this widget.

};


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.

.. versionadded:: 3.4
%End

void setHtmlCode( const QString &htmlCode );
%Docstring
The HTML code that will be represented within this widget.

@param htmlCode
%End

};


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

class QgsWidgetWrapper : QObject
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions python/gui/gui_auto.sip
Expand Up @@ -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
Expand Down
136 changes: 134 additions & 2 deletions src/app/qgsattributesformproperties.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -139,14 +141,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 );
}

Expand Down Expand Up @@ -478,6 +483,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..." ) );
Expand Down Expand Up @@ -519,6 +537,12 @@ void QgsAttributesFormProperties::onAttributeSelectionChanged()
mAttributeTypeDialog->setVisible( false );
break;
}
case DnDTreeItemData::HtmlWidget:
{
mAttributeRelationEdit->setVisible( false );
mAttributeTypeDialog->setVisible( false );
break;
}

}
}
Expand Down Expand Up @@ -609,6 +633,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() );
Expand Down Expand Up @@ -861,6 +894,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 );
Expand Down Expand Up @@ -941,6 +978,11 @@ bool DnDTree::dropMimeData( QTreeWidgetItem *parent, int index, const QMimeData
{
onItemDoubleClicked( newItem, 0 );
}

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

Expand Down Expand Up @@ -1281,6 +1323,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;
Expand Down Expand Up @@ -1393,6 +1514,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;
Expand Down
12 changes: 11 additions & 1 deletion src/app/qgsattributesformproperties.h
Expand Up @@ -74,6 +74,11 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
QString qmlCode;
};

struct HtmlElementEditorConfiguration
{
QString htmlCode;
};

class DnDTreeItemData : public QTreeWidgetItem
{
public:
Expand All @@ -82,7 +87,8 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
Field,
Relation,
Container,
QmlWidget
QmlWidget,
HtmlWidget
};

//do we need that
Expand Down Expand Up @@ -124,6 +130,9 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
QmlElementEditorConfiguration qmlElementEditorConfiguration() const;
void setQmlElementEditorConfiguration( QmlElementEditorConfiguration qmlElementEditorConfiguration );

HtmlElementEditorConfiguration htmlElementEditorConfiguration() const;
void setHtmlElementEditorConfiguration( HtmlElementEditorConfiguration htmlElementEditorConfiguration );

QColor backgroundColor() const;
void setBackgroundColor( const QColor &backgroundColor );

Expand All @@ -138,6 +147,7 @@ class APP_EXPORT QgsAttributesFormProperties : public QWidget, private Ui_QgsAtt
QgsOptionalExpression mVisibilityExpression;
RelationEditorConfiguration mRelationEditorConfiguration;
QmlElementEditorConfiguration mQmlElementEditorConfiguration;
HtmlElementEditorConfiguration mHtmlElementEditorConfiguration;
QColor mBackgroundColor;
};

Expand Down
30 changes: 30 additions & 0 deletions src/core/qgsattributeeditorelement.cpp
Expand Up @@ -189,3 +189,33 @@ QString QgsAttributeEditorQmlElement::typeIdentifier() const
{
return QStringLiteral( "attributeEditorQmlElement" );
}

QgsAttributeEditorElement *QgsAttributeEditorHtmlElement::clone( QgsAttributeEditorElement *parent ) const
{
QgsAttributeEditorHtmlElement *element = new QgsAttributeEditorHtmlElement( name(), parent );
element->setHtmlCode( mHtmlCode );

return element;
}

QString QgsAttributeEditorHtmlElement::htmlCode() const
{
return mHtmlCode;
}

void QgsAttributeEditorHtmlElement::setHtmlCode( const QString &htmlCode )
{
mHtmlCode = htmlCode;
}

void QgsAttributeEditorHtmlElement::saveConfiguration( QDomElement &elem ) const
{
QDomText codeElem = elem.ownerDocument().createTextNode( mHtmlCode );
elem.appendChild( codeElem );
}

QString QgsAttributeEditorHtmlElement::typeIdentifier() const
{
return QStringLiteral( "attributeEditorHtmlElement" );
}

0 comments on commit b165258

Please sign in to comment.