Skip to content

Commit

Permalink
[processing] Rework model input reorder approach to use QStandardItem…
Browse files Browse the repository at this point in the history
…Model

Allows for drag and drop reorder of inputs, hopefully resolves issues on
some qt versions
  • Loading branch information
nyalldawson committed Apr 22, 2020
1 parent 74d7eca commit 888819c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
34 changes: 25 additions & 9 deletions src/gui/processing/models/qgsmodelinputreorderwidget.cpp
Expand Up @@ -16,24 +16,30 @@
#include "qgsmodelinputreorderwidget.h"
#include "qgsgui.h"
#include <QDialogButtonBox>
#include <QStringListModel>
#include <QStandardItemModel>
///@cond NOT_STABLE

QgsModelInputReorderWidget::QgsModelInputReorderWidget( QWidget *parent )
: QWidget( parent )
{
setupUi( this );

mModel = new QStringListModel( this );
mModel = new QStandardItemModel( 0, 1, this );
mInputsList->setModel( mModel );

mInputsList->setDropIndicatorShown( true );
mInputsList->setDragDropOverwriteMode( false );
mInputsList->setDragEnabled( true );
mInputsList->setDragDropMode( QAbstractItemView::InternalMove );

connect( mButtonUp, &QPushButton::clicked, this, [ = ]
{
int currentRow = mInputsList->currentIndex().row() - 1;
if ( currentRow == -1 )
int currentRow = mInputsList->currentIndex().row();
if ( currentRow == 0 )
return;

mModel->moveRow( QModelIndex(), currentRow, QModelIndex(), currentRow + 2 );
mModel->insertRow( currentRow - 1, mModel->takeRow( currentRow ) );
mInputsList->setCurrentIndex( mModel->index( currentRow - 1, 0 ) );
} );

connect( mButtonDown, &QPushButton::clicked, this, [ = ]
Expand All @@ -42,7 +48,8 @@ QgsModelInputReorderWidget::QgsModelInputReorderWidget( QWidget *parent )
if ( currentRow == mModel->rowCount() - 1 )
return;

mModel->moveRow( QModelIndex(), currentRow, QModelIndex(), currentRow + 2 );
mModel->insertRow( currentRow + 1, mModel->takeRow( currentRow ) );
mInputsList->setCurrentIndex( mModel->index( currentRow + 1, 0 ) );
} );

}
Expand All @@ -51,14 +58,23 @@ void QgsModelInputReorderWidget::setInputs( const QList<QgsProcessingModelParame
{
mParameters = inputs;
QStringList res;
mModel->clear();
for ( const QgsProcessingModelParameter &param : inputs )
res << param.description();
mModel->setStringList( res );
{
QStandardItem *item = new QStandardItem( param.description() );
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled );
mModel->appendRow( item );
}
}

QStringList QgsModelInputReorderWidget::inputOrder() const
{
const QStringList order = mModel->stringList();
QStringList order;
order.reserve( mModel->rowCount( ) );
for ( int row = 0; row < mModel->rowCount(); ++row )
{
order << mModel->data( mModel->index( row, 0 ) ).toString();
}
QStringList res;
for ( const QString &description : order )
{
Expand Down
4 changes: 2 additions & 2 deletions src/gui/processing/models/qgsmodelinputreorderwidget.h
Expand Up @@ -24,7 +24,7 @@
#include "processing/models/qgsprocessingmodelparameter.h"
#include <QDialog>

class QStringListModel;
class QStandardItemModel;

///@cond PRIVATE

Expand Down Expand Up @@ -58,7 +58,7 @@ class GUI_EXPORT QgsModelInputReorderWidget : public QWidget, private Ui::QgsMod
private:

QList< QgsProcessingModelParameter > mParameters;
QStringListModel *mModel = nullptr;
QStandardItemModel *mModel = nullptr;
};


Expand Down

0 comments on commit 888819c

Please sign in to comment.