Skip to content

Commit

Permalink
Fix missing Report top level item in organizer widget
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 5, 2018
1 parent 57cac01 commit 69a225a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
57 changes: 34 additions & 23 deletions src/app/layout/qgsreportsectionmodel.cpp
Expand Up @@ -96,16 +96,20 @@ QVariant QgsReportSectionModel::headerData( int section, Qt::Orientation orienta

int QgsReportSectionModel::rowCount( const QModelIndex &parent ) const
{
QgsAbstractReportSection *parentSection = nullptr;
if ( parent.column() > 0 )
return 0;
if ( !parent.isValid() )
return 1; // report

QgsAbstractReportSection *parentSection = sectionForIndex( parent );
return parentSection ? parentSection->childCount() : 0;
}

bool QgsReportSectionModel::hasChildren( const QModelIndex &parent ) const
{
if ( !parent.isValid() )
parentSection = mReport;
else
parentSection = sectionForIndex( parent );
return true; // root item: its children are top level items

return parentSection->childCount();
QgsAbstractReportSection *parentSection = sectionForIndex( parent );
return parentSection && parentSection->childCount() > 0;
}

int QgsReportSectionModel::columnCount( const QModelIndex & ) const
Expand All @@ -118,32 +122,33 @@ QModelIndex QgsReportSectionModel::index( int row, int column, const QModelIndex
if ( !hasIndex( row, column, parent ) )
return QModelIndex();

QgsAbstractReportSection *parentSection = nullptr;

if ( !parent.isValid() )
parentSection = mReport;
else
parentSection = sectionForIndex( parent );

QgsAbstractReportSection *childSection = parentSection->childSection( row );
if ( childSection )
return createIndex( row, column, childSection );
QgsAbstractReportSection *parentSection = sectionForIndex( parent );
if ( parentSection )
{
QgsAbstractReportSection *item = parentSection->childSections().value( row, nullptr );
return item ? createIndex( row, column, item ) : QModelIndex();
}
else
return QModelIndex();
{
if ( row == 0 )
return createIndex( row, column, nullptr );
else
return QModelIndex();
}
}

QModelIndex QgsReportSectionModel::parent( const QModelIndex &index ) const
{
if ( !index.isValid() )
QgsAbstractReportSection *childSection = sectionForIndex( index );
if ( !childSection )
return QModelIndex();

QgsAbstractReportSection *childSection = sectionForIndex( index );
QgsAbstractReportSection *parentSection = childSection->parentSection();

if ( parentSection == mReport )
if ( !parentSection )
return QModelIndex();

return createIndex( parentSection->row(), 0, parentSection );
else
return createIndex( parentSection->row(), 0, parentSection != mReport ? parentSection : nullptr );
}

bool QgsReportSectionModel::setData( const QModelIndex &index, const QVariant &value, int role )
Expand Down Expand Up @@ -173,6 +178,12 @@ bool QgsReportSectionModel::setData( const QModelIndex &index, const QVariant &v

QgsAbstractReportSection *QgsReportSectionModel::sectionForIndex( const QModelIndex &index ) const
{
if ( !index.isValid() )
return nullptr;

if ( !index.internalPointer() ) // top level item
return mReport; // IMPORTANT - QgsReport uses multiple inheritance, so cannot static cast the void*!

return static_cast<QgsAbstractReportSection *>( index.internalPointer() );
}

Expand Down
1 change: 1 addition & 0 deletions src/app/layout/qgsreportsectionmodel.h
Expand Up @@ -42,6 +42,7 @@ class QgsReportSectionModel : public QAbstractItemModel
QVariant headerData( int section, Qt::Orientation orientation,
int role = Qt::DisplayRole ) const override;
int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
bool hasChildren( const QModelIndex &parent = QModelIndex() ) const override;
int columnCount( const QModelIndex & = QModelIndex() ) const override;

QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
Expand Down

0 comments on commit 69a225a

Please sign in to comment.