Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Unit test for cell clearing, fix duplicate signals
  • Loading branch information
nyalldawson committed Jan 14, 2020
1 parent e3507ff commit 77850ad
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/gui/tableeditor/qgstableeditorwidget.cpp
Expand Up @@ -261,7 +261,7 @@ void QgsTableEditorWidget::keyPressEvent( QKeyEvent *event )

void QgsTableEditorWidget::setTableContents( const QgsTableContents &contents )
{
mBlockSignals = true;
mBlockSignals++;
qDeleteAll( mNumericFormats );
mNumericFormats.clear();

Expand All @@ -287,7 +287,7 @@ void QgsTableEditorWidget::setTableContents( const QgsTableContents &contents )
rowNumber++;
}

mBlockSignals = false;
mBlockSignals--;
updateHeaders();
emit tableChanged();
}
Expand Down Expand Up @@ -531,14 +531,18 @@ void QgsTableEditorWidget::expandColumnSelection()
void QgsTableEditorWidget::clearSelectedCells()
{
const QModelIndexList selection = selectedIndexes();
bool changed = false;
mBlockSignals++;
for ( const QModelIndex &index : selection )
{
if ( QTableWidgetItem *i = item( index.row(), index.column() ) )
{
i->setText( QString() );
changed = true;
}
}
if ( !mBlockSignals )
mBlockSignals--;
if ( changed && !mBlockSignals )
emit tableChanged();
}

Expand Down
2 changes: 1 addition & 1 deletion src/gui/tableeditor/qgstableeditorwidget.h
Expand Up @@ -196,7 +196,7 @@ class GUI_EXPORT QgsTableEditorWidget : public QTableWidget
QList< int > collectUniqueRows( const QModelIndexList &list ) const;
QList< int > collectUniqueColumns( const QModelIndexList &list ) const;

bool mBlockSignals = false;
int mBlockSignals = 0;
QHash< QTableWidgetItem *, QgsNumericFormat * > mNumericFormats;
QMenu *mHeaderMenu = nullptr;

Expand Down
47 changes: 47 additions & 0 deletions tests/src/gui/testqgstableeditor.cpp
Expand Up @@ -36,6 +36,7 @@ class TestQgsTableEditor: public QObject
void deleteColumns();
void selectRows();
void selectColumns();
void clearSelected();

private:

Expand Down Expand Up @@ -663,6 +664,52 @@ void TestQgsTableEditor::selectColumns()
QCOMPARE( w.selectionModel()->selectedIndexes().size(), 0 );
}

void TestQgsTableEditor::clearSelected()
{
QgsTableEditorWidget w;
w.setTableContents( QgsTableContents() << ( QgsTableRow() << QgsTableCell( QStringLiteral( "A1" ) ) << QgsTableCell( QStringLiteral( "A2" ) ) << QgsTableCell( QStringLiteral( "A3" ) ) )
<< ( QgsTableRow() << QgsTableCell( QStringLiteral( "B1" ) ) << QgsTableCell( QStringLiteral( "B2" ) ) << QgsTableCell( QStringLiteral( "B3" ) ) )
<< ( QgsTableRow() << QgsTableCell( QStringLiteral( "C1" ) ) << QgsTableCell( QStringLiteral( "C2" ) ) << QgsTableCell( QStringLiteral( "C3" ) ) ) );

QSignalSpy spy( &w, &QgsTableEditorWidget::tableChanged );
w.selectionModel()->clearSelection();
w.clearSelectedCells();
QCOMPARE( spy.count(), 0 );
QCOMPARE( w.tableContents().size(), 3 );
QCOMPARE( w.tableContents().at( 0 ).size(), 3 );
QCOMPARE( w.tableContents().at( 0 ).at( 0 ).content().toString(), QStringLiteral( "A1" ) );
QCOMPARE( w.tableContents().at( 0 ).at( 1 ).content().toString(), QStringLiteral( "A2" ) );
QCOMPARE( w.tableContents().at( 0 ).at( 2 ).content().toString(), QStringLiteral( "A3" ) );
QCOMPARE( w.tableContents().at( 1 ).size(), 3 );
QCOMPARE( w.tableContents().at( 1 ).at( 0 ).content().toString(), QStringLiteral( "B1" ) );
QCOMPARE( w.tableContents().at( 1 ).at( 1 ).content().toString(), QStringLiteral( "B2" ) );
QCOMPARE( w.tableContents().at( 1 ).at( 2 ).content().toString(), QStringLiteral( "B3" ) );
QCOMPARE( w.tableContents().at( 2 ).size(), 3 );
QCOMPARE( w.tableContents().at( 2 ).at( 0 ).content().toString(), QStringLiteral( "C1" ) );
QCOMPARE( w.tableContents().at( 2 ).at( 1 ).content().toString(), QStringLiteral( "C2" ) );
QCOMPARE( w.tableContents().at( 2 ).at( 2 ).content().toString(), QStringLiteral( "C3" ) );

w.selectionModel()->select( w.model()->index( 1, 1 ), QItemSelectionModel::ClearAndSelect );
w.selectionModel()->select( w.model()->index( 1, 2 ), QItemSelectionModel::Select );
w.selectionModel()->select( w.model()->index( 0, 2 ), QItemSelectionModel::Select );
w.clearSelectedCells();
QCOMPARE( spy.count(), 1 );
QCOMPARE( w.tableContents().size(), 3 );
QCOMPARE( w.tableContents().at( 0 ).size(), 3 );
QCOMPARE( w.tableContents().at( 0 ).at( 0 ).content().toString(), QStringLiteral( "A1" ) );
QCOMPARE( w.tableContents().at( 0 ).at( 1 ).content().toString(), QStringLiteral( "A2" ) );
QCOMPARE( w.tableContents().at( 0 ).at( 2 ).content().toString(), QString() );
QCOMPARE( w.tableContents().at( 1 ).size(), 3 );
QCOMPARE( w.tableContents().at( 1 ).at( 0 ).content().toString(), QStringLiteral( "B1" ) );
QCOMPARE( w.tableContents().at( 1 ).at( 1 ).content().toString(), QString() );
QCOMPARE( w.tableContents().at( 1 ).at( 2 ).content().toString(), QString() );
QCOMPARE( w.tableContents().at( 2 ).size(), 3 );
QCOMPARE( w.tableContents().at( 2 ).at( 0 ).content().toString(), QStringLiteral( "C1" ) );
QCOMPARE( w.tableContents().at( 2 ).at( 1 ).content().toString(), QStringLiteral( "C2" ) );
QCOMPARE( w.tableContents().at( 2 ).at( 2 ).content().toString(), QStringLiteral( "C3" ) );

}


QGSTEST_MAIN( TestQgsTableEditor )
#include "testqgstableeditor.moc"

0 comments on commit 77850ad

Please sign in to comment.