Skip to content

Commit

Permalink
[sensorthings] only allow expansion removal from end of table
Browse files Browse the repository at this point in the history
Otherwise we can end up with inconsistent/invalid expansions

Fixes #59532
  • Loading branch information
nyalldawson committed Feb 3, 2025
1 parent 664abaa commit 3cca51b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
49 changes: 39 additions & 10 deletions src/gui/providers/sensorthings/qgssensorthingssourcewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ QgsSensorThingsSourceWidget::QgsSensorThingsSourceWidget( QWidget *parent )
connect( mExpansionsTable, &QTableView::clicked, this, [this]( const QModelIndex &index ) {
if ( index.column() == QgsSensorThingsExpansionsModel::Column::Actions )
{
mExpansionsModel->removeRows( index.row(), 1 );
// only the bottom expansion (or empty rows) can be removed - otherwise we end up with inconsistent expansions!
if ( mExpansionsModel->canRemoveRow( index.row() ) )
{
mExpansionsModel->removeRows( index.row(), 1 );
}
}
} );

Expand Down Expand Up @@ -722,6 +726,25 @@ bool QgsSensorThingsExpansionsModel::insertRows( int position, int rows, const Q
return true;
}

bool QgsSensorThingsExpansionsModel::canRemoveRow( int row ) const
{
if ( row >= mExpansions.size() )
return true;

for ( int i = mExpansions.size() - 1; i >= 0; --i )
{
if ( row == i && mExpansions.at( i ).isValid() )
return true;

// when we hit the first valid expansion from the end of the list, then
// any earlier rows CANNOT be removed
if ( mExpansions.at( i ).isValid() )
return false;
}

return false;
}

bool QgsSensorThingsExpansionsModel::removeRows( int position, int rows, const QModelIndex &parent )
{
Q_UNUSED( parent )
Expand Down Expand Up @@ -957,19 +980,25 @@ void QgsSensorThingsRemoveExpansionDelegate::paint( QPainter *painter, const QSt
{
QStyledItemDelegate::paint( painter, option, index );

if ( index == mHoveredIndex )
if ( const QgsSensorThingsExpansionsModel *model = qobject_cast< const QgsSensorThingsExpansionsModel * >( index.model() ) )
{
QStyleOptionButton buttonOption;
buttonOption.initFrom( option.widget );
buttonOption.rect = option.rect;
if ( model->canRemoveRow( index.row() ) )
{
if ( index == mHoveredIndex )
{
QStyleOptionButton buttonOption;
buttonOption.initFrom( option.widget );
buttonOption.rect = option.rect;

option.widget->style()->drawControl( QStyle::CE_PushButton, &buttonOption, painter );
}
option.widget->style()->drawControl( QStyle::CE_PushButton, &buttonOption, painter );
}

const QIcon icon = QgsApplication::getThemeIcon( "/mIconClearItem.svg" );
const QRect iconRect( option.rect.left() + ( option.rect.width() - 16 ) / 2, option.rect.top() + ( option.rect.height() - 16 ) / 2, 16, 16 );
const QIcon icon = QgsApplication::getThemeIcon( "/mIconClearItem.svg" );
const QRect iconRect( option.rect.left() + ( option.rect.width() - 16 ) / 2, option.rect.top() + ( option.rect.height() - 16 ) / 2, 16, 16 );

icon.paint( painter, iconRect );
icon.paint( painter, iconRect );
}
}
}

void QgsSensorThingsRemoveExpansionDelegate::setHoveredIndex( const QModelIndex &index )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class QgsSensorThingsExpansionsModel : public QAbstractItemModel
QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
bool insertRows( int position, int rows, const QModelIndex &parent = QModelIndex() ) override;
bool canRemoveRow( int row ) const;
bool removeRows( int position, int rows, const QModelIndex &parent = QModelIndex() ) override;

void setExpansions( const QList<QgsSensorThingsExpansionDefinition> &expansions );
Expand Down

0 comments on commit 3cca51b

Please sign in to comment.