Skip to content

Commit

Permalink
Sort layouts in layout manager
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 5, 2018
1 parent 19b0581 commit 3e12ec9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/app/layout/qgslayoutmanagerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ QgsLayoutManagerDialog::QgsLayoutManagerDialog( QWidget *parent, Qt::WindowFlags

mModel = new QgsLayoutManagerModel( QgsProject::instance()->layoutManager(),
this );
mLayoutListView->setModel( mModel );
mProxyModel = new QgsLayoutManagerProxyModel( mLayoutListView );
mProxyModel->setSourceModel( mModel );
mLayoutListView->setModel( mProxyModel );

connect( mButtonBox, &QDialogButtonBox::rejected, this, &QWidget::close );
connect( mLayoutListView->selectionModel(), &QItemSelectionModel::selectionChanged,
Expand Down Expand Up @@ -386,7 +388,7 @@ void QgsLayoutManagerDialog::removeClicked()
// Find the layouts that need to be deleted
for ( const QModelIndex &index : layoutItems )
{
QgsMasterLayoutInterface *l = mModel->layoutFromIndex( index );
QgsMasterLayoutInterface *l = mModel->layoutFromIndex( mProxyModel->mapToSource( index ) );
if ( l )
{
layoutList << l;
Expand All @@ -405,7 +407,7 @@ void QgsLayoutManagerDialog::showClicked()
const QModelIndexList layoutItems = mLayoutListView->selectionModel()->selectedRows();
for ( const QModelIndex &index : layoutItems )
{
if ( QgsMasterLayoutInterface *l = mModel->layoutFromIndex( index ) )
if ( QgsMasterLayoutInterface *l = mModel->layoutFromIndex( mProxyModel->mapToSource( index ) ) )
{
QgisApp::instance()->openLayoutDesignerDialog( l );
}
Expand All @@ -419,7 +421,7 @@ void QgsLayoutManagerDialog::duplicateClicked()
return;
}

QgsMasterLayoutInterface *currentLayout = mModel->layoutFromIndex( mLayoutListView->selectionModel()->selectedRows().at( 0 ) );
QgsMasterLayoutInterface *currentLayout = mModel->layoutFromIndex( mProxyModel->mapToSource( mLayoutListView->selectionModel()->selectedRows().at( 0 ) ) );
if ( !currentLayout )
return;
QString currentTitle = currentLayout->name();
Expand Down Expand Up @@ -459,7 +461,7 @@ void QgsLayoutManagerDialog::renameClicked()
return;
}

QgsMasterLayoutInterface *currentLayout = mModel->layoutFromIndex( mLayoutListView->selectionModel()->selectedRows().at( 0 ) );
QgsMasterLayoutInterface *currentLayout = mModel->layoutFromIndex( mProxyModel->mapToSource( mLayoutListView->selectionModel()->selectedRows().at( 0 ) ) );
if ( !currentLayout )
return;

Expand All @@ -474,7 +476,7 @@ void QgsLayoutManagerDialog::renameClicked()

void QgsLayoutManagerDialog::itemDoubleClicked( const QModelIndex &index )
{
if ( QgsMasterLayoutInterface *l = mModel->layoutFromIndex( index ) )
if ( QgsMasterLayoutInterface *l = mModel->layoutFromIndex( mProxyModel->mapToSource( index ) ) )
{
QgisApp::instance()->openLayoutDesignerDialog( l );
}
Expand Down Expand Up @@ -630,3 +632,11 @@ void QgsLayoutManagerModel::layoutRenamed( QgsMasterLayoutInterface *layout, con
QModelIndex index = createIndex( row, 0 );
emit dataChanged( index, index, QVector<int>() << Qt::DisplayRole );
}

QgsLayoutManagerProxyModel::QgsLayoutManagerProxyModel( QObject *parent )
: QSortFilterProxyModel( parent )
{
setDynamicSortFilter( true );
sort( 0 );
setSortCaseSensitivity( Qt::CaseInsensitive );
}
12 changes: 12 additions & 0 deletions src/app/layout/qgslayoutmanagerdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define QGSLAYOUTMANAGERDIALOG_H

#include <QItemDelegate>
#include <QSortFilterProxyModel>

#include "ui_qgslayoutmanagerbase.h"

Expand Down Expand Up @@ -55,6 +56,16 @@ class QgsLayoutManagerModel : public QAbstractListModel
QgsLayoutManager *mLayoutManager = nullptr;
};

class QgsLayoutManagerProxyModel : public QSortFilterProxyModel
{
Q_OBJECT

public:

explicit QgsLayoutManagerProxyModel( QObject *parent );

};

/**
* A dialog that allows management of layouts within a project.
*/
Expand Down Expand Up @@ -94,6 +105,7 @@ class QgsLayoutManagerDialog: public QDialog, private Ui::QgsLayoutManagerBase
QPushButton *mDuplicateButton = nullptr;
QPushButton *mCreateReportButton = nullptr;
QgsLayoutManagerModel *mModel = nullptr;
QgsLayoutManagerProxyModel *mProxyModel = nullptr;

#ifdef Q_OS_MAC
void showEvent( QShowEvent *event );
Expand Down

0 comments on commit 3e12ec9

Please sign in to comment.