Skip to content

Commit

Permalink
[processing] Use an editable combobox with current project layout names
Browse files Browse the repository at this point in the history
when editing a model which uses the print layout parameter type

Instead of a plain text edit with no hints as to the current project's layouts
  • Loading branch information
nyalldawson committed Jun 29, 2020
1 parent d8337ea commit c923546
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
35 changes: 27 additions & 8 deletions src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp
Expand Up @@ -2510,13 +2510,21 @@ QWidget *QgsProcessingLayoutWidgetWrapper::createWidget()

case QgsProcessingGui::Modeler:
{
mLineEdit = new QLineEdit();
mLineEdit->setToolTip( tr( "Name of an existing print layout" ) );
connect( mLineEdit, &QLineEdit::textChanged, this, [ = ]( const QString & )
mPlainComboBox = new QComboBox();
mPlainComboBox->setEditable( true );
mPlainComboBox->setToolTip( tr( "Name of an existing print layout" ) );
if ( widgetContext().project() )
{
const QList< QgsPrintLayout * > layouts = widgetContext().project()->layoutManager()->printLayouts();
for ( const QgsPrintLayout *layout : layouts )
mPlainComboBox->addItem( layout->name() );
}

connect( mPlainComboBox, &QComboBox::currentTextChanged, this, [ = ]( const QString & )
{
emit widgetValueHasChanged( this );
} );
return mLineEdit;
return mPlainComboBox;
}
}
return nullptr;
Expand All @@ -2536,10 +2544,10 @@ void QgsProcessingLayoutWidgetWrapper::setWidgetValue( const QVariant &value, Qg
mComboBox->setCurrentLayout( nullptr );
}
}
else if ( mLineEdit )
else if ( mPlainComboBox )
{
const QString v = QgsProcessingParameters::parameterAsString( parameterDefinition(), value, context );
mLineEdit->setText( v );
mPlainComboBox->setCurrentText( v );
}
}

Expand All @@ -2550,12 +2558,23 @@ QVariant QgsProcessingLayoutWidgetWrapper::widgetValue() const
const QgsMasterLayoutInterface *l = mComboBox->currentLayout();
return l ? l->name() : QVariant();
}
else if ( mLineEdit )
return mLineEdit->text().isEmpty() ? QVariant() : mLineEdit->text();
else if ( mPlainComboBox )
return mPlainComboBox->currentText().isEmpty() ? QVariant() : mPlainComboBox->currentText();
else
return QVariant();
}

void QgsProcessingLayoutWidgetWrapper::setWidgetContext( const QgsProcessingParameterWidgetContext &context )
{
QgsAbstractProcessingParameterWidgetWrapper::setWidgetContext( context );
if ( mPlainComboBox && context.project() )
{
const QList< QgsPrintLayout * > layouts = widgetContext().project()->layoutManager()->printLayouts();
for ( const QgsPrintLayout *layout : layouts )
mPlainComboBox->addItem( layout->name() );
}
}

QStringList QgsProcessingLayoutWidgetWrapper::compatibleParameterTypes() const
{
return QStringList()
Expand Down
3 changes: 2 additions & 1 deletion src/gui/processing/qgsprocessingwidgetwrapperimpl.h
Expand Up @@ -825,6 +825,7 @@ class GUI_EXPORT QgsProcessingLayoutWidgetWrapper : public QgsAbstractProcessing

void setWidgetValue( const QVariant &value, QgsProcessingContext &context ) override;
QVariant widgetValue() const override;
void setWidgetContext( const QgsProcessingParameterWidgetContext &context ) override;

QStringList compatibleParameterTypes() const override;

Expand All @@ -834,7 +835,7 @@ class GUI_EXPORT QgsProcessingLayoutWidgetWrapper : public QgsAbstractProcessing
private:

QgsLayoutComboBox *mComboBox = nullptr;
QLineEdit *mLineEdit = nullptr;
QComboBox *mPlainComboBox = nullptr;

friend class TestProcessingGui;
};
Expand Down
14 changes: 7 additions & 7 deletions tests/src/gui/testprocessinggui.cpp
Expand Up @@ -4139,7 +4139,7 @@ void TestProcessingGui::testLayoutWrapper()
}
else
{
QCOMPARE( static_cast< QLineEdit * >( wrapper.wrappedWidget() )->text(), QStringLiteral( "l2" ) );
QCOMPARE( static_cast< QComboBox * >( wrapper.wrappedWidget() )->currentText(), QStringLiteral( "l2" ) );
}
wrapper.setWidgetValue( "l1", context );
QCOMPARE( spy.count(), 2 );
Expand All @@ -4151,7 +4151,7 @@ void TestProcessingGui::testLayoutWrapper()
}
else
{
QCOMPARE( static_cast< QLineEdit * >( wrapper.wrappedWidget() )->text(), QStringLiteral( "l1" ) );
QCOMPARE( static_cast< QComboBox * >( wrapper.wrappedWidget() )->currentText(), QStringLiteral( "l1" ) );
}

QLabel *l = wrapper.createWrappedLabel();
Expand All @@ -4174,7 +4174,7 @@ void TestProcessingGui::testLayoutWrapper()
}
else
{
static_cast< QLineEdit * >( wrapper.wrappedWidget() )->setText( QStringLiteral( "aaaa" ) );
static_cast< QComboBox * >( wrapper.wrappedWidget() )->setCurrentText( QStringLiteral( "aaaa" ) );
}
QCOMPARE( spy.count(), 3 );

Expand All @@ -4199,7 +4199,7 @@ void TestProcessingGui::testLayoutWrapper()
}
else
{
QCOMPARE( static_cast< QLineEdit * >( wrapper2.wrappedWidget() )->text(), QStringLiteral( "l2" ) );
QCOMPARE( static_cast< QComboBox * >( wrapper2.wrappedWidget() )->currentText(), QStringLiteral( "l2" ) );
}
wrapper2.setWidgetValue( "l1", context );
QCOMPARE( spy2.count(), 2 );
Expand All @@ -4211,7 +4211,7 @@ void TestProcessingGui::testLayoutWrapper()
}
else
{
QCOMPARE( static_cast< QLineEdit * >( wrapper2.wrappedWidget() )->text(), QStringLiteral( "l1" ) );
QCOMPARE( static_cast< QComboBox * >( wrapper2.wrappedWidget() )->currentText(), QStringLiteral( "l1" ) );
}
wrapper2.setWidgetValue( QVariant(), context );
QCOMPARE( spy2.count(), 3 );
Expand All @@ -4223,14 +4223,14 @@ void TestProcessingGui::testLayoutWrapper()
}
else
{
QVERIFY( static_cast< QLineEdit * >( wrapper2.wrappedWidget() )->text().isEmpty() );
QVERIFY( static_cast< QComboBox * >( wrapper2.wrappedWidget() )->currentText().isEmpty() );
}

// check signal
if ( type != QgsProcessingGui::Modeler )
static_cast< QComboBox * >( wrapper2.wrappedWidget() )->setCurrentIndex( 2 );
else
static_cast< QLineEdit * >( wrapper2.wrappedWidget() )->setText( QStringLiteral( "aaa" ) );
static_cast< QComboBox * >( wrapper2.wrappedWidget() )->setCurrentText( QStringLiteral( "aaa" ) );
QCOMPARE( spy2.count(), 4 );

delete w;
Expand Down

0 comments on commit c923546

Please sign in to comment.