Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processing] Port enum widget wrapper to new API
Fixes: - enum parameters set to "allow multiple" only allow a single value selection when used in modeler - optional enum parameters cannot be set to no value when used outside of modeler Fixes #20406
- Loading branch information
1 parent
160ca69
commit cccf974
Showing
14 changed files
with
1,371 additions
and
4 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
python/gui/auto_generated/processing/qgsprocessingmultipleselectiondialog.sip.in
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,88 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/gui/processing/qgsprocessingmultipleselectiondialog.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
|
||
|
||
|
||
|
||
class QgsProcessingMultipleSelectionDialog : QDialog | ||
{ | ||
%Docstring | ||
Dialog for configuration of a matrix (fixed table) parameter. | ||
|
||
.. note:: | ||
|
||
Not stable API | ||
|
||
.. versionadded:: 3.6 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgsprocessingmultipleselectiondialog.h" | ||
%End | ||
public: | ||
|
||
QgsProcessingMultipleSelectionDialog( const QVariantList &availableOptions = QVariantList(), | ||
const QVariantList &selectedOptions = QVariantList(), | ||
QWidget *parent /TransferThis/ = 0, Qt::WindowFlags flags = 0 ); | ||
%Docstring | ||
Constructor for QgsProcessingMultipleSelectionDialog. | ||
|
||
The ``availableOptions`` list specifies the list of standard known options for the parameter, | ||
whilst the ``selectedOptions`` list specifies which options should be initially selected. | ||
|
||
The ``selectedOptions`` list may contain extra options which are not present in ``availableOptions``, | ||
in which case they will be also added as existing options within the dialog. | ||
%End | ||
|
||
|
||
void setValueFormatter( SIP_PYCALLABLE ); | ||
%Docstring | ||
Sets a callback function to use when encountering an invalid geometry and | ||
%End | ||
%MethodCode | ||
|
||
Py_BEGIN_ALLOW_THREADS | ||
|
||
sipCpp->setValueFormatter( [a0]( const QVariant &v )->QString | ||
{ | ||
QString res; | ||
SIP_BLOCK_THREADS | ||
PyObject *s = sipCallMethod( NULL, a0, "D", &v, sipType_QVariant, NULL ); | ||
int state; | ||
int sipIsError = 0; | ||
QString *t1 = reinterpret_cast<QString *>( sipConvertToType( s, sipType_QString, 0, SIP_NOT_NONE, &state, &sipIsError ) ); | ||
if ( sipIsError == 0 ) | ||
{ | ||
res = QString( *t1 ); | ||
} | ||
sipReleaseType( t1, sipType_QString, state ); | ||
SIP_UNBLOCK_THREADS | ||
return res; | ||
} ); | ||
|
||
Py_END_ALLOW_THREADS | ||
%End | ||
|
||
|
||
QVariantList selectedOptions() const; | ||
%Docstring | ||
Returns the ordered list of selected options. | ||
%End | ||
|
||
}; | ||
|
||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/gui/processing/qgsprocessingmultipleselectiondialog.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ |
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
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
153 changes: 153 additions & 0 deletions
153
src/gui/processing/qgsprocessingmultipleselectiondialog.cpp
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,153 @@ | ||
/*************************************************************************** | ||
qgsprocessingmultipleselectiondialog.cpp | ||
------------------------------------ | ||
Date : February 2019 | ||
Copyright : (C) 2019 Nyall Dawson | ||
Email : nyall dot dawson at gmail dot 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 "qgsprocessingmultipleselectiondialog.h" | ||
#include "qgsgui.h" | ||
#include <QStandardItemModel> | ||
#include <QStandardItem> | ||
#include <QPushButton> | ||
#include <QLineEdit> | ||
#include <QToolButton> | ||
|
||
///@cond NOT_STABLE | ||
|
||
QgsProcessingMultipleSelectionDialog::QgsProcessingMultipleSelectionDialog( const QVariantList &availableOptions, | ||
const QVariantList &selectedOptions, | ||
QWidget *parent, Qt::WindowFlags flags ) | ||
: QDialog( parent, flags ) | ||
, mValueFormatter( []( const QVariant & v )->QString { return v.toString(); } ) | ||
{ | ||
setupUi( this ); | ||
|
||
QgsGui::enableAutoGeometryRestore( this ); | ||
|
||
mSelectionList->setSelectionBehavior( QAbstractItemView::SelectRows ); | ||
mSelectionList->setSelectionMode( QAbstractItemView::ExtendedSelection ); | ||
mSelectionList->setDragDropMode( QAbstractItemView::InternalMove ); | ||
|
||
mButtonSelectAll = new QPushButton( tr( "Select All" ) ); | ||
mButtonBox->addButton( mButtonSelectAll, QDialogButtonBox::ActionRole ); | ||
|
||
mButtonClearSelection = new QPushButton( tr( "Clear Selection" ) ); | ||
mButtonBox->addButton( mButtonClearSelection, QDialogButtonBox::ActionRole ); | ||
|
||
mButtonToggleSelection = new QPushButton( tr( "Toggle Selection" ) ); | ||
mButtonBox->addButton( mButtonToggleSelection, QDialogButtonBox::ActionRole ); | ||
|
||
connect( mButtonSelectAll, &QPushButton::clicked, this, [ = ] { selectAll( true ); } ); | ||
connect( mButtonClearSelection, &QPushButton::clicked, this, [ = ] { selectAll( false ); } ); | ||
connect( mButtonToggleSelection, &QPushButton::clicked, this, &QgsProcessingMultipleSelectionDialog::toggleSelection ); | ||
|
||
populateList( availableOptions, selectedOptions ); | ||
} | ||
|
||
void QgsProcessingMultipleSelectionDialog::setValueFormatter( const std::function<QString( const QVariant & )> &formatter ) | ||
{ | ||
mValueFormatter = formatter; | ||
// update item text using new formatter | ||
for ( int i = 0; i < mModel->rowCount(); ++i ) | ||
{ | ||
mModel->item( i )->setText( mValueFormatter( mModel->item( i )->data( Qt::UserRole ) ) ); | ||
} | ||
} | ||
|
||
QVariantList QgsProcessingMultipleSelectionDialog::selectedOptions() const | ||
{ | ||
QVariantList options; | ||
options.reserve( mModel->rowCount() ); | ||
for ( int i = 0; i < mModel->rowCount(); ++i ) | ||
{ | ||
if ( mModel->item( i )->checkState() == Qt::Checked ) | ||
options << mModel->item( i )->data( Qt::UserRole ); | ||
} | ||
return options; | ||
} | ||
|
||
void QgsProcessingMultipleSelectionDialog::selectAll( const bool checked ) | ||
{ | ||
const QList<QStandardItem *> items = currentItems(); | ||
for ( QStandardItem *item : items ) | ||
{ | ||
item->setCheckState( checked ? Qt::Checked : Qt::Unchecked ); | ||
} | ||
} | ||
|
||
void QgsProcessingMultipleSelectionDialog::toggleSelection() | ||
{ | ||
const QList<QStandardItem *> items = currentItems(); | ||
for ( QStandardItem *item : items ) | ||
{ | ||
item->setCheckState( item->checkState() == Qt::Unchecked ? Qt::Checked : Qt::Unchecked ); | ||
} | ||
} | ||
|
||
QList<QStandardItem *> QgsProcessingMultipleSelectionDialog::currentItems() | ||
{ | ||
QList<QStandardItem *> items; | ||
const QModelIndexList selection = mSelectionList->selectionModel()->selectedIndexes(); | ||
if ( selection.size() > 1 ) | ||
{ | ||
items.reserve( selection.size() ); | ||
for ( const QModelIndex &index : selection ) | ||
{ | ||
items << mModel->itemFromIndex( index ); | ||
} | ||
} | ||
else | ||
{ | ||
items.reserve( mModel->rowCount() ); | ||
for ( int i = 0; i < mModel->rowCount(); ++i ) | ||
{ | ||
items << mModel->item( i ); | ||
} | ||
} | ||
return items; | ||
} | ||
|
||
void QgsProcessingMultipleSelectionDialog::populateList( const QVariantList &availableOptions, const QVariantList &selectedOptions ) | ||
{ | ||
mModel = new QStandardItemModel( this ); | ||
|
||
QVariantList remainingOptions = availableOptions; | ||
|
||
// we add selected options first, keeping the existing order of options | ||
for ( const QVariant &option : selectedOptions ) | ||
{ | ||
// if isinstance(t, QgsProcessingModelChildParameterSource): | ||
// item = QStandardItem(t.staticValue()) | ||
// else: | ||
std::unique_ptr< QStandardItem > item = qgis::make_unique< QStandardItem >( mValueFormatter( option ) ); | ||
item->setData( option, Qt::UserRole ); | ||
item->setCheckState( Qt::Checked ); | ||
item->setCheckable( true ); | ||
item->setDropEnabled( false ); | ||
mModel->appendRow( item.release() ); | ||
remainingOptions.removeAll( option ); | ||
} | ||
|
||
for ( const QVariant &option : qgis::as_const( remainingOptions ) ) | ||
{ | ||
std::unique_ptr< QStandardItem > item = qgis::make_unique< QStandardItem >( mValueFormatter( option ) ); | ||
item->setData( option, Qt::UserRole ); | ||
item->setCheckState( Qt::Unchecked ); | ||
item->setCheckable( true ); | ||
item->setDropEnabled( false ); | ||
mModel->appendRow( item.release() ); | ||
} | ||
|
||
mSelectionList->setModel( mModel ); | ||
} | ||
|
||
///@endcond |
Oops, something went wrong.