Skip to content
Permalink
Browse files

Unit test for cell clearing, fix duplicate signals

  • Loading branch information
nyalldawson committed Jan 13, 2020
1 parent e3507ff commit 77850adff64cfd8f3e5bd5b761a5ac12dd42146c
@@ -261,7 +261,7 @@ void QgsTableEditorWidget::keyPressEvent( QKeyEvent *event )

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

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

mBlockSignals = false;
mBlockSignals--;
updateHeaders();
emit tableChanged();
}
@@ -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();
}

@@ -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;

@@ -36,6 +36,7 @@ class TestQgsTableEditor: public QObject
void deleteColumns();
void selectRows();
void selectColumns();
void clearSelected();

private:

@@ -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.
You can’t perform that action at this time.