Navigation Menu

Skip to content

Commit

Permalink
Auto select new report sections after adding them
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 5, 2018
1 parent 69a225a commit 43aff9b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/app/layout/qgsreportorganizerwidget.cpp
Expand Up @@ -79,13 +79,19 @@ void QgsReportOrganizerWidget::setMessageBar( QgsMessageBar *bar )
void QgsReportOrganizerWidget::addLayoutSection()
{
std::unique_ptr< QgsReportSectionLayout > section = qgis::make_unique< QgsReportSectionLayout >();
QgsAbstractReportSection *newSection = section.get();
mSectionModel->addSection( mViewSections->currentIndex(), std::move( section ) );
QModelIndex newIndex = mSectionModel->indexForSection( newSection );
mViewSections->setCurrentIndex( newIndex );
}

void QgsReportOrganizerWidget::addFieldGroupSection()
{
std::unique_ptr< QgsReportSectionFieldGroup > section = qgis::make_unique< QgsReportSectionFieldGroup >();
QgsAbstractReportSection *newSection = section.get();
mSectionModel->addSection( mViewSections->currentIndex(), std::move( section ) );
QModelIndex newIndex = mSectionModel->indexForSection( newSection );
mViewSections->setCurrentIndex( newIndex );
}

void QgsReportOrganizerWidget::removeSection()
Expand Down
24 changes: 24 additions & 0 deletions src/app/layout/qgsreportsectionmodel.cpp
Expand Up @@ -14,6 +14,7 @@
***************************************************************************/

#include "qgsreportsectionmodel.h"
#include "functional"

#ifdef ENABLE_MODELTEST
#include "modeltest.h"
Expand Down Expand Up @@ -187,6 +188,29 @@ QgsAbstractReportSection *QgsReportSectionModel::sectionForIndex( const QModelIn
return static_cast<QgsAbstractReportSection *>( index.internalPointer() );
}

QModelIndex QgsReportSectionModel::indexForSection( QgsAbstractReportSection *section ) const
{
if ( !section )
return QModelIndex();

std::function< QModelIndex( const QModelIndex &parent, QgsAbstractReportSection *section ) > findIndex = [&]( const QModelIndex & parent, QgsAbstractReportSection * section )->QModelIndex
{
for ( int row = 0; row < rowCount( parent ); ++row )
{
QModelIndex current = index( row, 0, parent );
if ( sectionForIndex( current ) == section )
return current;

QModelIndex checkChildren = findIndex( current, section );
if ( checkChildren.isValid() )
return checkChildren;
}
return QModelIndex();
};

return findIndex( QModelIndex(), section );
}

bool QgsReportSectionModel::removeRows( int row, int count, const QModelIndex &parent )
{
QgsAbstractReportSection *parentSection = sectionForIndex( parent );
Expand Down
2 changes: 2 additions & 0 deletions src/app/layout/qgsreportsectionmodel.h
Expand Up @@ -57,6 +57,8 @@ class QgsReportSectionModel : public QAbstractItemModel
*/
QgsAbstractReportSection *sectionForIndex( const QModelIndex &index ) const;

QModelIndex indexForSection( QgsAbstractReportSection *section ) const;

private:
QgsReport *mReport = nullptr;
};
Expand Down

0 comments on commit 43aff9b

Please sign in to comment.