Skip to content
Permalink
Browse files

Auto select new report sections after adding them

  • Loading branch information
nyalldawson committed Dec 31, 2017
1 parent 69a225a commit 43aff9b2e3a0b75d61f7e84b4a958566f276241d
@@ -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()
@@ -14,6 +14,7 @@
***************************************************************************/

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

#ifdef ENABLE_MODELTEST
#include "modeltest.h"
@@ -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 );
@@ -57,6 +57,8 @@ class QgsReportSectionModel : public QAbstractItemModel
*/
QgsAbstractReportSection *sectionForIndex( const QModelIndex &index ) const;

QModelIndex indexForSection( QgsAbstractReportSection *section ) const;

private:
QgsReport *mReport = nullptr;
};

0 comments on commit 43aff9b

Please sign in to comment.
You can’t perform that action at this time.