Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Add widget to edit QVariantMap
- Loading branch information
Patrick Valsecchi
committed
Sep 8, 2016
1 parent
3d6e7c8
commit e533f0c
Showing
15 changed files
with
786 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** \ingroup gui | ||
* Widget allowing to edit a QVariantMap, using a table. | ||
* @note added in QGIS 3.0 | ||
*/ | ||
class QgsKeyValueWidget : public QWidget | ||
{ | ||
%TypeHeaderCode | ||
#include "qgskeyvaluewidget.h" | ||
%End | ||
public: | ||
/** | ||
* Constructor. | ||
*/ | ||
explicit QgsKeyValueWidget( QWidget* parent /TransferThis/ = 0 ); | ||
|
||
/** | ||
* Set the initial value of the widget. | ||
*/ | ||
void setMap( const QVariantMap& map ); | ||
|
||
/** | ||
* Get the edit value. | ||
* @return the QVariantMap | ||
*/ | ||
QVariantMap map() const; | ||
|
||
signals: | ||
/** | ||
* Emitted each time a key or a value is changed. | ||
*/ | ||
void valueChanged(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/*************************************************************************** | ||
qgskeyvaluewidgetfactory.cpp | ||
-------------------------------------- | ||
Date : 08.2016 | ||
Copyright : (C) 2016 Patrick Valsecchi | ||
Email : patrick.valsecchi@camptocamp.com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgskeyvaluewidgetfactory.h" | ||
#include "qgskeyvaluewidgetwrapper.h" | ||
#include "qgsdummyconfigdlg.h" | ||
#include "qgsfield.h" | ||
#include "qgsvectorlayer.h" | ||
|
||
#include <QVariant> | ||
#include <QSettings> | ||
|
||
QgsKeyValueWidgetFactory::QgsKeyValueWidgetFactory( const QString& name ): | ||
QgsEditorWidgetFactory( name ) | ||
{ | ||
} | ||
|
||
QgsEditorWidgetWrapper* QgsKeyValueWidgetFactory::create( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent ) const | ||
{ | ||
return new QgsKeyValueWidgetWrapper( vl, fieldIdx, editor, parent ); | ||
} | ||
|
||
QgsEditorConfigWidget* QgsKeyValueWidgetFactory::configWidget( QgsVectorLayer *vl, int fieldIdx, QWidget *parent ) const | ||
{ | ||
Q_UNUSED( vl ); | ||
Q_UNUSED( fieldIdx ); | ||
Q_UNUSED( parent ); | ||
return new QgsDummyConfigDlg( vl, fieldIdx, parent, QObject::tr( "Key/Value field" ) ); | ||
} | ||
|
||
QgsEditorWidgetConfig QgsKeyValueWidgetFactory::readConfig( const QDomElement &configElement, QgsVectorLayer *layer, int fieldIdx ) | ||
{ | ||
Q_UNUSED( configElement ); | ||
Q_UNUSED( layer ); | ||
Q_UNUSED( fieldIdx ); | ||
return QgsEditorWidgetConfig(); | ||
} | ||
|
||
void QgsKeyValueWidgetFactory::writeConfig( const QgsEditorWidgetConfig& config, QDomElement& configElement, QDomDocument& doc, const QgsVectorLayer* layer, int fieldIdx ) | ||
{ | ||
Q_UNUSED( config ); | ||
Q_UNUSED( configElement ); | ||
Q_UNUSED( doc ); | ||
Q_UNUSED( layer ); | ||
Q_UNUSED( fieldIdx ); | ||
} | ||
|
||
QString QgsKeyValueWidgetFactory::representValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const | ||
{ | ||
Q_UNUSED( vl ); | ||
Q_UNUSED( fieldIdx ); | ||
Q_UNUSED( config ); | ||
Q_UNUSED( cache ); | ||
|
||
if ( value.isNull() ) | ||
{ | ||
QSettings settings; | ||
return settings.value( "qgis/nullValue", "NULL" ).toString(); | ||
} | ||
|
||
QString result; | ||
const QVariantMap map = value.toMap(); | ||
for ( QVariantMap::const_iterator i = map.constBegin(); i != map.constEnd(); ++i ) | ||
{ | ||
if ( !result.isEmpty() ) result.append( ", " ); | ||
result.append( i.key() ).append( ": " ).append( i.value().toString() ); | ||
} | ||
return result; | ||
} | ||
|
||
Qt::AlignmentFlag QgsKeyValueWidgetFactory::alignmentFlag( QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config ) const | ||
{ | ||
Q_UNUSED( vl ); | ||
Q_UNUSED( fieldIdx ); | ||
Q_UNUSED( config ); | ||
|
||
return Qt::AlignLeft; | ||
} | ||
|
||
unsigned int QgsKeyValueWidgetFactory::fieldScore( const QgsVectorLayer* vl, int fieldIdx ) const | ||
{ | ||
const QgsField field = vl->fields().field( fieldIdx ); | ||
return field.type() == QVariant::Map ? 20 : 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/*************************************************************************** | ||
qgskeyvaluewidgetfactory.h | ||
-------------------------------------- | ||
Date : 08.2016 | ||
Copyright : (C) 2016 Patrick Valsecchi | ||
Email : patrick.valsecchi@camptocamp.com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSKEYVALUEEDITFACTORY_H | ||
#define QGSKEYVALUEEDITFACTORY_H | ||
|
||
#include "qgseditorwidgetfactory.h" | ||
|
||
/** @ingroup gui | ||
* Factory for widgets for editing a QVariantMap | ||
* @note added in QGIS 3.0 | ||
* @note not available in Python bindings | ||
*/ | ||
class GUI_EXPORT QgsKeyValueWidgetFactory : public QgsEditorWidgetFactory | ||
{ | ||
public: | ||
/** | ||
* Constructor. | ||
*/ | ||
QgsKeyValueWidgetFactory( const QString& name ); | ||
|
||
// QgsEditorWidgetFactory interface | ||
public: | ||
QgsEditorWidgetWrapper* create( QgsVectorLayer *vl, int fieldIdx, QWidget *editor, QWidget *parent ) const override; | ||
//QgsSearchWidgetWrapper* createSearchWidget( QgsVectorLayer* vl, int fieldIdx, QWidget* parent ) const override; | ||
QgsEditorConfigWidget* configWidget( QgsVectorLayer *vl, int fieldIdx, QWidget *parent ) const override; | ||
QgsEditorWidgetConfig readConfig( const QDomElement &configElement, QgsVectorLayer *layer, int fieldIdx ) override; | ||
void writeConfig( const QgsEditorWidgetConfig& config, QDomElement& configElement, QDomDocument& doc, const QgsVectorLayer* layer, int fieldIdx ) override; | ||
QString representValue( QgsVectorLayer* vl, int fieldIdx, const QgsEditorWidgetConfig& config, const QVariant& cache, const QVariant& value ) const override; | ||
Qt::AlignmentFlag alignmentFlag( QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config ) const override; | ||
unsigned int fieldScore( const QgsVectorLayer* vl, int fieldIdx ) const override; | ||
}; | ||
|
||
#endif // QGSKEYVALUEEDITFACTORY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/*************************************************************************** | ||
qgskeyvaluewidgetwrapper.cpp | ||
-------------------------------------- | ||
Date : 08.2016 | ||
Copyright : (C) 2016 Patrick Valsecchi | ||
Email : patrick.valsecchi@camptocamp.com | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgskeyvaluewidgetwrapper.h" | ||
#include "qgskeyvaluewidget.h" | ||
#include "qgsattributeform.h" | ||
|
||
QgsKeyValueWidgetWrapper::QgsKeyValueWidgetWrapper( QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent ): | ||
QgsEditorWidgetWrapper( vl, fieldIdx, editor, parent ), mWidget( nullptr ) | ||
{ | ||
} | ||
|
||
QVariant QgsKeyValueWidgetWrapper::value() const | ||
{ | ||
if ( !mWidget ) return QVariant( QVariant::Map ); | ||
return mWidget->map(); | ||
} | ||
|
||
void QgsKeyValueWidgetWrapper::showIndeterminateState() | ||
{ | ||
mWidget->setMap( QVariantMap() ); | ||
} | ||
|
||
static bool isInTable( const QWidget* parent ) | ||
{ | ||
if ( !parent ) return false; | ||
if ( qobject_cast<const QTableView*>( parent ) ) return true; | ||
return isInTable( parent->parentWidget() ); | ||
} | ||
|
||
QWidget* QgsKeyValueWidgetWrapper::createWidget( QWidget* parent ) | ||
{ | ||
if ( isInTable( parent ) ) | ||
{ | ||
// if to be put in a table, draw a border and set a decent size | ||
QFrame* ret = new QFrame( parent ); | ||
ret->setFrameShape( QFrame::StyledPanel ); | ||
QHBoxLayout* layout = new QHBoxLayout( ret ); | ||
layout->addWidget( new QgsKeyValueWidget( ret ) ); | ||
ret->setMinimumSize( QSize( 320, 110 ) ); | ||
return ret; | ||
} | ||
else | ||
{ | ||
return new QgsKeyValueWidget( parent ); | ||
} | ||
} | ||
|
||
void QgsKeyValueWidgetWrapper::initWidget( QWidget* editor ) | ||
{ | ||
mWidget = qobject_cast<QgsKeyValueWidget*>( editor ); | ||
if ( !mWidget ) | ||
{ | ||
mWidget = editor->findChild<QgsKeyValueWidget*>(); | ||
} | ||
|
||
connect( mWidget, SIGNAL( valueChanged( const QVariant& ) ), this, SIGNAL( valueChanged( const QVariant& ) ) ); | ||
} | ||
|
||
bool QgsKeyValueWidgetWrapper::valid() const | ||
{ | ||
return true; | ||
} | ||
|
||
void QgsKeyValueWidgetWrapper::setValue( const QVariant& value ) | ||
{ | ||
mWidget->setMap( value.toMap() ); | ||
} | ||
|
||
void QgsKeyValueWidgetWrapper::updateConstraintWidgetStatus( bool /*constraintValid*/ ) | ||
{ | ||
// Nothing | ||
} |
Oops, something went wrong.