Skip to content
Permalink
Browse files

use a checkable combo box for fields configuration flags in source fi…

…elds properties tab
  • Loading branch information
3nids committed Sep 14, 2020
1 parent 4fd940a commit d273f78c23b2db986b1b9d6b1cfba73194f0bbd6
@@ -123,6 +123,13 @@ Toggles the item check state
%End

QgsCheckableItemModel *model() const;
%Docstring
Returns the custom item model which handles checking the items

.. seealso:: :py:class:`QgsCheckableItemModel`

.. versionadded:: 3.16
%End

virtual void hidePopup();

@@ -77,8 +77,9 @@ void QgsCheckBoxDelegate::paint( QPainter *painter, const QStyleOptionViewItem &
QgsCheckableComboBox::QgsCheckableComboBox( QWidget *parent )
: QComboBox( parent )
, mSeparator( QStringLiteral( ", " ) )
, mModel( new QgsCheckableItemModel( this ) )
{
setModel( new QgsCheckableItemModel( this ) );
setModel( mModel );
setItemDelegate( new QgsCheckBoxDelegate( this ) );

QLineEdit *lineEdit = new QLineEdit( this );
@@ -164,7 +164,7 @@ class GUI_EXPORT QgsCheckableComboBox : public QComboBox
void setDefaultText( const QString &text );

/**
* Adds an item to the combobox with the given \a text, check \a state
* Adds an item to the combobox with the given \a text, check \a state (stored in the Qt::CheckStateRole)
* and containing the specified \a userData (stored in the Qt::UserRole).
* The item is appended to the list of existing items.
* \since QGIS 3.16
@@ -209,6 +209,13 @@ class GUI_EXPORT QgsCheckableComboBox : public QComboBox
*/
void toggleItemCheckState( int index );

/**
* Returns the custom item model which handles checking the items
* \see QgsCheckableItemModel
* \since QGIS 3.16
*/
QgsCheckableItemModel *model() const {return mModel;}

/**
* Hides the list of items in the combobox if it is currently
* visible and resets the internal state.
@@ -268,6 +275,8 @@ class GUI_EXPORT QgsCheckableComboBox : public QComboBox
QString mSeparator;
QString mDefaultText;

QgsCheckableItemModel *mModel = nullptr;

bool mSkipHide = false;

QMenu *mContextMenu = nullptr;
@@ -15,6 +15,7 @@
***************************************************************************/

#include "qgsaddattrdialog.h"
#include "qgscheckablecombobox.h"
#include "qgssourcefieldsproperties.h"
#include "qgsvectorlayer.h"
#include "qgsproject.h"
@@ -272,20 +273,20 @@ void QgsSourceFieldsProperties::setRow( int row, int idx, const QgsField &field
else
mFieldsList->item( row, AttrNameCol )->setFlags( mFieldsList->item( row, AttrNameCol )->flags() & ~Qt::ItemIsEditable );

//published WMS/WFS attributes
QTableWidgetItem *wmsAttrItem = new QTableWidgetItem();
wmsAttrItem->setCheckState( mLayer->excludeAttributesWms().contains( field.name() ) ? Qt::Unchecked : Qt::Checked );
wmsAttrItem->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable );
mFieldsList->setItem( row, AttrWMSCol, wmsAttrItem );
QTableWidgetItem *wfsAttrItem = new QTableWidgetItem();
wfsAttrItem->setCheckState( mLayer->excludeAttributesWfs().contains( field.name() ) ? Qt::Unchecked : Qt::Checked );
wfsAttrItem->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable );
mFieldsList->setItem( row, AttrWFSCol, wfsAttrItem );
QTableWidgetItem *searchableAttrItem = new QTableWidgetItem();
searchableAttrItem->setCheckState( mLayer->fieldConfigurationFlags( idx ).testFlag( QgsField::ConfigurationFlag::Searchable ) ? Qt::Checked : Qt::Unchecked );
searchableAttrItem->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable );
mFieldsList->setItem( row, AttrSearchableCol, searchableAttrItem );
// Flags
QgsCheckableComboBox *cb = new QgsCheckableComboBox( mFieldsList );
const QMap<QgsField::ConfigurationFlag, QString> flagList = qgsEnumMap<QgsField::ConfigurationFlag>();
QMap<QgsField::ConfigurationFlag, QString>::const_iterator flagIt;
for ( flagIt = flagList.constBegin(); flagIt != flagList.constEnd(); ++flagIt )
{
if ( flagIt.key() == QgsField::ConfigurationFlag::None || flagIt.key() == QgsField::ConfigurationFlag::DefaultFlags )
continue;

cb->addItemWithCheckState( flagIt.value(),
mLayer->fieldConfigurationFlags( idx ).testFlag( flagIt.key() ) ? Qt::Unchecked : Qt::Checked,
QVariant::fromValue( flagIt.key() ) );
}
mFieldsList->setCellWidget( row, AttrSearchableCol, cb );
}

bool QgsSourceFieldsProperties::addAttribute( const QgsField &field )
@@ -307,34 +308,25 @@ bool QgsSourceFieldsProperties::addAttribute( const QgsField &field )

void QgsSourceFieldsProperties::apply()
{
QSet<QString> excludeAttributesWMS, excludeAttributesWFS;

for ( int i = 0; i < mFieldsList->rowCount(); i++ )
{
if ( mFieldsList->item( i, AttrWMSCol )->checkState() == Qt::Unchecked )
{
excludeAttributesWMS.insert( mFieldsList->item( i, AttrNameCol )->text() );
}
if ( mFieldsList->item( i, AttrWFSCol )->checkState() == Qt::Unchecked )
{
excludeAttributesWFS.insert( mFieldsList->item( i, AttrNameCol )->text() );
}

int idx = mFieldsList->item( i, AttrIdCol )->data( Qt::DisplayRole ).toInt();
QgsField::ConfigurationFlags flags = mLayer->fieldConfigurationFlags( idx );
if ( mFieldsList->item( i, AttrSearchableCol )->checkState() == Qt::Checked )
{
flags.setFlag( QgsField::ConfigurationFlag::Searchable, true );
}
else

QgsCheckableComboBox *cb = qobject_cast<QgsCheckableComboBox *>( mFieldsList->cellWidget( i, AttrSearchableCol ) );
if ( cb )
{
flags.setFlag( QgsField::ConfigurationFlag::Searchable, false );
QgsCheckableItemModel *model = cb->model();
for ( int r = 0; r < model->rowCount(); ++r )
{
QModelIndex index = model->index( r, 0 );
QgsField::ConfigurationFlag flag = model->data( index, Qt::UserRole ).value<QgsField::ConfigurationFlag>();
bool active = model->data( index, Qt::CheckStateRole ).value<Qt::CheckState>() == Qt::Checked ? true : false;
flags.setFlag( flag, active );
}
mLayer->setFieldConfigurationFlags( idx, flags );
}
mLayer->setFieldConfigurationFlags( idx, flags );
}

mLayer->setExcludeAttributesWms( excludeAttributesWMS );
mLayer->setExcludeAttributesWfs( excludeAttributesWFS );
}

//SLOTS

0 comments on commit d273f78

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