Skip to content
Permalink
Browse files

API backward compatibility fix, code layout and readability, implicit…

… sharing
  • Loading branch information
3nids committed May 6, 2020
1 parent 28c20cf commit 51b0d5a13b578ffac5fb5ab6940efe5cb76e3340
@@ -1466,10 +1466,9 @@ bool QgsCompositionConverter::readTableXml( QgsLayoutItemAttributeTable *layoutI
layoutItem->mColumns.append( column );

// sorting columns are now (QGIS 3.14+) handled in a dedicated list
// copy the display columns if sortByRank > 0 and then, sort them by rank
Q_NOWARN_DEPRECATED_PUSH
for ( const QgsLayoutTableColumn &col : qgis::as_const( layoutItem->mColumns ) )
if ( col.sortByRank() > 0 )
layoutItem->mSortColumns.append( col );
std::copy_if( layoutItem->mColumns.begin(), layoutItem->mColumns.end(), std::back_inserter( layoutItem->mSortColumns ), []( const QgsLayoutTableColumn & col ) {return col.sortByRank() > 0;} );
std::sort( layoutItem->mSortColumns.begin(), layoutItem->mSortColumns.end(), []( const QgsLayoutTableColumn & a, const QgsLayoutTableColumn & b ) {return a.sortByRank() < b.sortByRank();} );
Q_NOWARN_DEPRECATED_POP
}
@@ -367,16 +367,16 @@ void QgsLayoutItemAttributeTable::restoreFieldAliasMap( const QMap<int, QString>
return;
}

for ( QgsLayoutTableColumn &column : mColumns )
for ( int i = 0; i < mColumns.count(); i++ )
{
int attrIdx = source->fields().lookupField( column.attribute() );
int attrIdx = source->fields().lookupField( mColumns[i].attribute() );
if ( map.contains( attrIdx ) )
{
column.setHeading( map.value( attrIdx ) );
mColumns[i].setHeading( map.value( attrIdx ) );
}
else
{
column.setHeading( source->attributeDisplayName( attrIdx ) );
mColumns[i].setHeading( source->attributeDisplayName( attrIdx ) );
}
}
}
@@ -190,10 +190,9 @@ bool QgsLayoutTable::readPropertiesFromElement( const QDomElement &itemElem, con
else
{
// backward compatibility for QGIS < 3.14
// copy the display columns if sortByRank > 0 and then, sort them by rank
Q_NOWARN_DEPRECATED_PUSH
for ( const QgsLayoutTableColumn &col : qgis::as_const( mColumns ) )
if ( col.sortByRank() > 0 )
mSortColumns.append( col );
std::copy_if( mColumns.begin(), mColumns.end(), std::back_inserter( mSortColumns ), []( const QgsLayoutTableColumn & col ) {return col.sortByRank() > 0;} );
std::sort( mSortColumns.begin(), mSortColumns.end(), []( const QgsLayoutTableColumn & a, const QgsLayoutTableColumn & b ) {return a.sortByRank() < b.sortByRank();} );
Q_NOWARN_DEPRECATED_POP
}
@@ -792,17 +791,24 @@ void QgsLayoutTable::setWrapBehavior( QgsLayoutTable::WrapBehavior behavior )
void QgsLayoutTable::setColumns( const QgsLayoutTableColumns &columns )
{
//remove existing columns
mColumns.clear();
mColumns = columns;

mColumns.append( columns );
// backward compatibility
// test if sorting is provided with the columns and call setSortColumns in such case
QgsLayoutTableSortColumns newSortColumns;
Q_NOWARN_DEPRECATED_PUSH
std::copy_if( mColumns.begin(), mColumns.end(), std::back_inserter( newSortColumns ), []( const QgsLayoutTableColumn & col ) {return col.sortByRank() > 0;} );
if ( !newSortColumns.isEmpty() )
{
std::sort( newSortColumns.begin(), newSortColumns.end(), []( const QgsLayoutTableColumn & a, const QgsLayoutTableColumn & b ) {return a.sortByRank() < b.sortByRank();} );
setSortColumns( newSortColumns );
}
Q_NOWARN_DEPRECATED_POP
}

void QgsLayoutTable::setSortColumns( const QgsLayoutTableSortColumns &sortColumns )
{
//remove existing columns
mSortColumns.clear();

mSortColumns.append( sortColumns );
mSortColumns = sortColumns;
}

void QgsLayoutTable::setCellStyle( QgsLayoutTable::CellStyleGroup group, const QgsLayoutTableStyle &style )
@@ -231,38 +231,40 @@ bool QgsLayoutAttributeTableColumnModelBase::setData( const QModelIndex &index,
if ( index.column() > displayedColumns().count() )
return false;

QgsLayoutTableColumn &colToUpdate = columns()[index.row()];

Column col = displayedColumns()[index.column()];
switch ( col )
{
case Attribute:
// also update column's heading, if it hasn't been customized
if ( columns()[index.row()].heading().isEmpty() || ( columns()[index.row()].heading() == columns()[index.row()].attribute() ) )
if ( colToUpdate.heading().isEmpty() || ( colToUpdate.heading() == colToUpdate.attribute() ) )
{
columns()[index.row()].setHeading( value.toString() );
colToUpdate.setHeading( value.toString() );
emit dataChanged( createIndex( index.row(), 1 ), createIndex( index.row(), 1 ) );
}
columns()[index.row()].setAttribute( value.toString() );
colToUpdate.setAttribute( value.toString() );
emit dataChanged( index, index );
return true;

case Heading:
columns()[index.row()].setHeading( value.toString() );
colToUpdate.setHeading( value.toString() );
emit dataChanged( index, index );
return true;

case Alignment:
columns()[index.row()].setHAlignment( Qt::AlignmentFlag( value.toInt() & Qt::AlignHorizontal_Mask ) );
columns()[index.row()].setVAlignment( Qt::AlignmentFlag( value.toInt() & Qt::AlignVertical_Mask ) );
colToUpdate.setHAlignment( Qt::AlignmentFlag( value.toInt() & Qt::AlignHorizontal_Mask ) );
colToUpdate.setVAlignment( Qt::AlignmentFlag( value.toInt() & Qt::AlignVertical_Mask ) );
emit dataChanged( index, index );
return true;

case Width:
columns()[index.row()].setWidth( value.toDouble() );
colToUpdate.setWidth( value.toDouble() );
emit dataChanged( index, index );
return true;

case SortOrder:
mTable->sortColumns()[index.row()].setSortOrder( static_cast< Qt::SortOrder >( value.toInt() ) );
colToUpdate.setSortOrder( static_cast< Qt::SortOrder >( value.toInt() ) );
emit dataChanged( index, index );
return true;
}

0 comments on commit 51b0d5a

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