Skip to content
Permalink
Browse files

mNofColumns in value relation widget

mNofColumns defining the number of columns when "allowMulti" is checked.

The widged is now a tableWidget instead of listWidget.
ListWidged stuff is removed - including in valuerelationsearchwidget because unused.

Tests are changed from list to table as well.
  • Loading branch information
signedav committed Feb 21, 2018
1 parent a7ca742 commit 471e67f1fb635fb8b6486eb02c42d8934df016e7
@@ -27,6 +27,10 @@ QgsValueRelationConfigDlg::QgsValueRelationConfigDlg( QgsVectorLayer *vl, int fi
connect( mLayerName, &QgsMapLayerComboBox::layerChanged, mValueColumn, &QgsFieldComboBox::setLayer );
connect( mEditExpression, &QAbstractButton::clicked, this, &QgsValueRelationConfigDlg::editExpression );

mNofColumns->setMinimum( 1 );
mNofColumns->setMaximum( 10 );
mNofColumns->setValue( 1 );

connect( mLayerName, &QgsMapLayerComboBox::layerChanged, this, &QgsEditorConfigWidget::changed );
connect( mKeyColumn, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
connect( mValueColumn, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsEditorConfigWidget::changed );
@@ -35,6 +39,14 @@ QgsValueRelationConfigDlg::QgsValueRelationConfigDlg( QgsVectorLayer *vl, int fi
connect( mOrderByValue, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
connect( mFilterExpression, &QTextEdit::textChanged, this, &QgsEditorConfigWidget::changed );
connect( mUseCompleter, &QAbstractButton::toggled, this, &QgsEditorConfigWidget::changed );
connect( mAllowMulti, &QAbstractButton::toggled, this, [ = ]( bool checked )
{
label_nofColumns->setEnabled( checked );
mNofColumns->setEnabled( checked );
}
);

connect( mNofColumns, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsEditorConfigWidget::changed );
}

QVariantMap QgsValueRelationConfigDlg::config()
@@ -45,6 +57,7 @@ QVariantMap QgsValueRelationConfigDlg::config()
cfg.insert( QStringLiteral( "Key" ), mKeyColumn->currentField() );
cfg.insert( QStringLiteral( "Value" ), mValueColumn->currentField() );
cfg.insert( QStringLiteral( "AllowMulti" ), mAllowMulti->isChecked() );
cfg.insert( QStringLiteral( "NofColumns" ), mNofColumns->value() );
cfg.insert( QStringLiteral( "AllowNull" ), mAllowNull->isChecked() );
cfg.insert( QStringLiteral( "OrderByValue" ), mOrderByValue->isChecked() );
cfg.insert( QStringLiteral( "FilterExpression" ), mFilterExpression->toPlainText() );
@@ -60,6 +73,12 @@ void QgsValueRelationConfigDlg::setConfig( const QVariantMap &config )
mKeyColumn->setField( config.value( QStringLiteral( "Key" ) ).toString() );
mValueColumn->setField( config.value( QStringLiteral( "Value" ) ).toString() );
mAllowMulti->setChecked( config.value( QStringLiteral( "AllowMulti" ) ).toBool() );
mNofColumns->setValue( config.value( QStringLiteral( "NofColumns" ), 1 ).toInt() );
if ( !mAllowMulti->isChecked() )
{
label_nofColumns->setEnabled( false );
mNofColumns->setEnabled( false );
}
mAllowNull->setChecked( config.value( QStringLiteral( "AllowNull" ) ).toBool() );
mOrderByValue->setChecked( config.value( QStringLiteral( "OrderByValue" ) ).toBool() );
mFilterExpression->setPlainText( config.value( QStringLiteral( "FilterExpression" ) ).toString() );
@@ -54,19 +54,6 @@ QVariant QgsValueRelationSearchWidgetWrapper::value() const
}
}

if ( mListWidget )
{
QStringList selection;
for ( int i = 0; i < mListWidget->count(); ++i )
{
QListWidgetItem *item = mListWidget->item( i );
if ( item->checkState() == Qt::Checked )
selection << item->data( Qt::UserRole ).toString();
}

v = selection.join( QStringLiteral( "," ) ).prepend( '{' ).append( '}' );
}

if ( mLineEdit )
{
Q_FOREACH ( const QgsValueRelationFieldFormatter::ValueRelationItem &i, mCache )
@@ -141,10 +128,6 @@ void QgsValueRelationSearchWidgetWrapper::clearWidget()
{
mComboBox->setCurrentIndex( 0 );
}
if ( mListWidget )
{
mListWidget->clearSelection();
}
if ( mLineEdit )
{
mLineEdit->setText( QString() );
@@ -157,10 +140,6 @@ void QgsValueRelationSearchWidgetWrapper::setEnabled( bool enabled )
{
mComboBox->setEnabled( enabled );
}
if ( mListWidget )
{
mListWidget->setEnabled( enabled );
}
if ( mLineEdit )
{
mLineEdit->setEnabled( enabled );
@@ -231,7 +210,6 @@ void QgsValueRelationSearchWidgetWrapper::initWidget( QWidget *editor )
mCache = QgsValueRelationFieldFormatter::createCache( config() );

mComboBox = qobject_cast<QComboBox *>( editor );
mListWidget = qobject_cast<QListWidget *>( editor );
mLineEdit = qobject_cast<QLineEdit *>( editor );

if ( mComboBox )
@@ -249,18 +227,6 @@ void QgsValueRelationSearchWidgetWrapper::initWidget( QWidget *editor )

connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsValueRelationSearchWidgetWrapper::onValueChanged );
}
else if ( mListWidget )
{
Q_FOREACH ( const QgsValueRelationFieldFormatter::ValueRelationItem &element, mCache )
{
QListWidgetItem *item = nullptr;
item = new QListWidgetItem( element.value );
item->setData( Qt::UserRole, element.key );

mListWidget->addItem( item );
}
connect( mListWidget, &QListWidget::itemChanged, this, &QgsValueRelationSearchWidgetWrapper::onValueChanged );
}
else if ( mLineEdit )
{
QStringList values;
@@ -21,7 +21,6 @@
#include "qgsvaluerelationfieldformatter.h"

#include <QComboBox>
#include <QListWidget>
#include <QLineEdit>
#include "qgis_gui.h"

@@ -65,7 +64,6 @@ class GUI_EXPORT QgsValueRelationSearchWidgetWrapper : public QgsSearchWidgetWra

private:
QComboBox *mComboBox = nullptr;
QListWidget *mListWidget = nullptr;
QLineEdit *mLineEdit = nullptr;

QgsValueRelationFieldFormatter::ValueRelationCache mCache;
@@ -23,6 +23,7 @@
#include "qgsfilterlineedit.h"
#include "qgsfeatureiterator.h"
#include "qgsvaluerelationfieldformatter.h"
#include "qgslogger.h"

#include <QStringListModel>
#include <QCompleter>
@@ -46,16 +47,21 @@ QVariant QgsValueRelationWidgetWrapper::value() const
}
}

if ( mListWidget )
if ( mTableWidget )
{
QStringList selection;
for ( int i = 0; i < mListWidget->count(); ++i )
for ( int j = 0; j < mTableWidget->rowCount(); j++ )
{
QListWidgetItem *item = mListWidget->item( i );
if ( item->checkState() == Qt::Checked )
selection << item->data( Qt::UserRole ).toString();
for ( int i = 0; i < config( QStringLiteral( "NofColumns" ) ).toInt(); ++i )
{
QTableWidgetItem *item = mTableWidget->item( j, i );
if ( item )
{
if ( item->checkState() == Qt::Checked )
selection << item->data( Qt::UserRole ).toString();
}
}
}

v = selection.join( QStringLiteral( "," ) ).prepend( '{' ).append( '}' );
}

@@ -78,7 +84,7 @@ QWidget *QgsValueRelationWidgetWrapper::createWidget( QWidget *parent )
{
if ( config( QStringLiteral( "AllowMulti" ) ).toBool() )
{
return new QListWidget( parent );
return new QTableWidget( parent );
}
else if ( config( QStringLiteral( "UseCompleter" ) ).toBool() )
{
@@ -94,7 +100,7 @@ void QgsValueRelationWidgetWrapper::initWidget( QWidget *editor )
mCache = QgsValueRelationFieldFormatter::createCache( config() );

mComboBox = qobject_cast<QComboBox *>( editor );
mListWidget = qobject_cast<QListWidget *>( editor );
mTableWidget = qobject_cast<QTableWidget *>( editor );

This comment has been minimized.

Copy link
@3nids

3nids Dec 5, 2018

Member

@signedav
this breaks existing custom UI files with list widgets.
I am not sure this is worth a fix as it would bring some more code.
https://issues.qgis.org/issues/20714

mLineEdit = qobject_cast<QLineEdit *>( editor );

if ( mComboBox )
@@ -112,17 +118,39 @@ void QgsValueRelationWidgetWrapper::initWidget( QWidget *editor )
connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ) );
}
else if ( mListWidget )
else if ( mTableWidget )
{
Q_FOREACH ( const QgsValueRelationFieldFormatter::ValueRelationItem &element, mCache )
mTableWidget->horizontalHeader()->setResizeMode( QHeaderView::Stretch );
mTableWidget->horizontalHeader()->setVisible( false );
mTableWidget->verticalHeader()->setResizeMode( QHeaderView::Stretch );
mTableWidget->verticalHeader()->setVisible( false );
mTableWidget->setShowGrid( false );
mTableWidget->setEditTriggers( QAbstractItemView::NoEditTriggers );
mTableWidget->setSelectionMode( QAbstractItemView::NoSelection );
if ( mCache.size() > 0 )
mTableWidget->setRowCount( ( mCache.size() + config( QStringLiteral( "NofColumns" ) ).toInt() - 1 ) / config( QStringLiteral( "NofColumns" ) ).toInt() );
else
mTableWidget->setRowCount( 1 );
if ( config( QStringLiteral( "NofColumns" ) ).toInt() > 0 )
mTableWidget->setColumnCount( config( QStringLiteral( "NofColumns" ) ).toInt() );
else
mTableWidget->setColumnCount( 1 );

int row = 0, column = 0;
for ( const QgsValueRelationFieldFormatter::ValueRelationItem &element : qgis::as_const( mCache ) )
{
QListWidgetItem *item = nullptr;
item = new QListWidgetItem( element.value );
if ( column == config( QStringLiteral( "NofColumns" ) ).toInt() )
{
row++;
column = 0;
}
QTableWidgetItem *item = nullptr;
item = new QTableWidgetItem( element.value );
item->setData( Qt::UserRole, element.key );

mListWidget->addItem( item );
mTableWidget->setItem( row, column, item );
column++;
}
connect( mListWidget, &QListWidget::itemChanged, this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ) );
connect( mTableWidget, &QTableWidget::itemChanged, this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ) );
}
else if ( mLineEdit )
{
@@ -144,23 +172,29 @@ void QgsValueRelationWidgetWrapper::initWidget( QWidget *editor )

bool QgsValueRelationWidgetWrapper::valid() const
{
return mListWidget || mLineEdit || mComboBox;
return mTableWidget || mLineEdit || mComboBox;
}

void QgsValueRelationWidgetWrapper::setValue( const QVariant &value )
{
if ( mListWidget )
if ( mTableWidget )
{
QStringList checkList;
if ( value.type() == QVariant::StringList )
checkList = value.toStringList();
else if ( value.type() == QVariant::String )
checkList = value.toString().remove( QChar( '{' ) ).remove( QChar( '}' ) ).split( ',' );

for ( int i = 0; i < mListWidget->count(); ++i )
for ( int j = 0; j < mTableWidget->rowCount(); j++ )
{
QListWidgetItem *item = mListWidget->item( i );
item->setCheckState( checkList.contains( item->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
for ( int i = 0; i < config( QStringLiteral( "NofColumns" ) ).toInt() ; ++i )
{
QTableWidgetItem *item = mTableWidget->item( j, i );
if ( item )
{
item->setCheckState( checkList.contains( item->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
}
}
}
}
else if ( mComboBox )
@@ -182,14 +216,17 @@ void QgsValueRelationWidgetWrapper::setValue( const QVariant &value )

void QgsValueRelationWidgetWrapper::showIndeterminateState()
{
if ( mListWidget )
if ( mTableWidget )
{
mListWidget->blockSignals( true );
for ( int i = 0; i < mListWidget->count(); ++i )
mTableWidget->blockSignals( true );
for ( int j = 0; j < mTableWidget->rowCount(); j++ )
{
mListWidget->item( i )->setCheckState( Qt::PartiallyChecked );
for ( int i = 0; i < config( QStringLiteral( "NofColumns" ) ).toInt(); ++i )
{
mTableWidget->item( j, i )->setCheckState( Qt::PartiallyChecked );
}
}
mListWidget->blockSignals( false );
mTableWidget->blockSignals( false );
}
else if ( mComboBox )
{
@@ -208,16 +245,21 @@ void QgsValueRelationWidgetWrapper::setEnabled( bool enabled )

mEnabled = enabled;

if ( mListWidget )
if ( mTableWidget )
{
for ( int i = 0; i < mListWidget->count(); ++i )
for ( int j = 0; j < mTableWidget->rowCount(); j++ )
{
QListWidgetItem *item = mListWidget->item( i );

if ( enabled )
item->setFlags( item->flags() | Qt::ItemIsEnabled );
else
item->setFlags( item->flags() & ~Qt::ItemIsEnabled );
for ( int i = 0; i < mTableWidget->columnCount(); ++i )
{
QTableWidgetItem *item = mTableWidget->item( j, i );
if ( item )
{
if ( enabled )
item->setFlags( item->flags() | Qt::ItemIsEnabled );
else
item->setFlags( item->flags() & ~Qt::ItemIsEnabled );
}
}
}
}
else
@@ -20,7 +20,8 @@
#include "qgsvaluerelationfieldformatter.h"

#include <QComboBox>
#include <QListWidget>
#include <QTableWidget>
#include <QHeaderView>
#include <QLineEdit>
#include "qgis_gui.h"

@@ -72,7 +73,7 @@ class GUI_EXPORT QgsValueRelationWidgetWrapper : public QgsEditorWidgetWrapper

private:
QComboBox *mComboBox = nullptr;
QListWidget *mListWidget = nullptr;
QTableWidget *mTableWidget = nullptr;
QLineEdit *mLineEdit = nullptr;

QgsValueRelationFieldFormatter::ValueRelationCache mCache;

0 comments on commit 471e67f

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