Skip to content
Permalink
Browse files

Unit tests, fixes for table foreground/background color

  • Loading branch information
nyalldawson committed Jan 13, 2020
1 parent 77850ad commit 9835ab06ea3728df462490a7f7bcf2fdfa6197e4
Showing with 145 additions and 5 deletions.
  1. +21 −5 src/gui/tableeditor/qgstableeditorwidget.cpp
  2. +124 −0 tests/src/gui/testqgstableeditor.cpp
@@ -549,42 +549,58 @@ void QgsTableEditorWidget::clearSelectedCells()
void QgsTableEditorWidget::setSelectionForegroundColor( const QColor &color )
{
const QModelIndexList selection = selectedIndexes();
bool changed = false;
mBlockSignals++;
for ( const QModelIndex &index : selection )
{
if ( QTableWidgetItem *i = item( index.row(), index.column() ) )
{
i->setData( Qt::ForegroundRole, color.isValid() ? color : QVariant() );
if ( i->data( Qt::ForegroundRole ).value< QColor >() != color )
{
i->setData( Qt::ForegroundRole, color.isValid() ? color : QVariant() );
changed = true;
}
}
else
{
QTableWidgetItem *newItem = new QTableWidgetItem();
newItem->setData( Qt::ForegroundRole, color.isValid() ? color : QVariant() );
setItem( index.row(), index.column(), newItem );
changed = true;
}
}
if ( !mBlockSignals )
mBlockSignals--;
if ( changed && !mBlockSignals )
emit tableChanged();
}

void QgsTableEditorWidget::setSelectionBackgroundColor( const QColor &color )
{
const QModelIndexList selection = selectedIndexes();
bool changed = false;
mBlockSignals++;
for ( const QModelIndex &index : selection )
{
if ( QTableWidgetItem *i = item( index.row(), index.column() ) )
{
i->setData( Qt::BackgroundRole, color.isValid() ? color : QVariant() );
i->setData( PresetBackgroundColorRole, color.isValid() ? color : QVariant() );
if ( i->data( PresetBackgroundColorRole ).value< QColor >() != color )
{
i->setData( Qt::BackgroundRole, color.isValid() ? color : QVariant() );
i->setData( PresetBackgroundColorRole, color.isValid() ? color : QVariant() );
changed = true;
}
}
else
{
QTableWidgetItem *newItem = new QTableWidgetItem();
newItem->setData( Qt::BackgroundRole, color.isValid() ? color : QVariant() );
newItem->setData( PresetBackgroundColorRole, color.isValid() ? color : QVariant() );
setItem( index.row(), index.column(), newItem );
changed = true;
}
}
if ( !mBlockSignals )
mBlockSignals--;
if ( changed && !mBlockSignals )
emit tableChanged();
}

@@ -37,6 +37,8 @@ class TestQgsTableEditor: public QObject
void selectRows();
void selectColumns();
void clearSelected();
void foregroundColor();
void backgroundColor();

private:

@@ -710,6 +712,128 @@ void TestQgsTableEditor::clearSelected()

}

void TestQgsTableEditor::foregroundColor()
{
QgsTableEditorWidget w;
QVERIFY( w.tableContents().isEmpty() );

QSignalSpy spy( &w, &QgsTableEditorWidget::tableChanged );
QgsTableCell c3;
c3.setContent( 87 );
std::unique_ptr< QgsCurrencyNumericFormat > format = qgis::make_unique< QgsCurrencyNumericFormat >();
format->setNumberDecimalPlaces( 2 );
format->setPrefix( QStringLiteral( "$" ) );
c3.setNumericFormat( format.release() );
QgsTableCell c2( 76 );
c2.setBackgroundColor( QColor( 255, 0, 0 ) );
c2.setForegroundColor( QColor( 0, 255, 0 ) );
w.setTableContents( QgsTableContents() << ( QgsTableRow() << QgsTableCell( QStringLiteral( "Jet" ) ) << c2 << c3 << QgsTableCell( QStringLiteral( "Jet3" ) ) ) );
QCOMPARE( spy.count(), 1 );

QCOMPARE( w.tableContents().size(), 1 );
QCOMPARE( w.tableContents().at( 0 ).size(), 4 );
QCOMPARE( w.tableContents().at( 0 ).at( 0 ).content().toString(), QStringLiteral( "Jet" ) );
QVERIFY( !w.tableContents().at( 0 ).at( 0 ).backgroundColor().isValid() );
QVERIFY( !w.tableContents().at( 0 ).at( 0 ).foregroundColor().isValid() );
QVERIFY( !w.tableContents().at( 0 ).at( 0 ).numericFormat() );
QCOMPARE( w.tableContents().at( 0 ).at( 1 ).content().toString(), QStringLiteral( "76" ) );
QCOMPARE( w.tableContents().at( 0 ).at( 1 ).backgroundColor(), QColor( 255, 0, 0 ) );
QCOMPARE( w.tableContents().at( 0 ).at( 1 ).foregroundColor(), QColor( 0, 255, 0 ) );
QVERIFY( !w.tableContents().at( 0 ).at( 1 ).numericFormat() );
QCOMPARE( w.tableContents().at( 0 ).at( 2 ).content().toString(), QStringLiteral( "87" ) );
QVERIFY( !w.tableContents().at( 0 ).at( 2 ).backgroundColor().isValid() );
QVERIFY( !w.tableContents().at( 0 ).at( 2 ).foregroundColor().isValid() );
QVERIFY( w.tableContents().at( 0 ).at( 2 ).numericFormat() );
QCOMPARE( w.tableContents().at( 0 ).at( 2 ).numericFormat()->id(), QStringLiteral( "currency" ) );
QCOMPARE( w.tableContents().at( 0 ).at( 3 ).content().toString(), QStringLiteral( "Jet3" ) );
QVERIFY( !w.tableContents().at( 0 ).at( 3 ).backgroundColor().isValid() );
QVERIFY( !w.tableContents().at( 0 ).at( 3 ).foregroundColor().isValid() );
QVERIFY( !w.tableContents().at( 0 ).at( 3 ).numericFormat() );

w.selectionModel()->clearSelection();
w.setSelectionForegroundColor( QColor( 255, 255, 0 ) );
QCOMPARE( spy.count(), 1 );

w.selectionModel()->select( w.model()->index( 0, 0 ), QItemSelectionModel::ClearAndSelect );
QVERIFY( !w.selectionForegroundColor().isValid() );
w.selectionModel()->select( w.model()->index( 0, 1 ), QItemSelectionModel::Select );
QVERIFY( !w.selectionForegroundColor().isValid() );
w.selectionModel()->select( w.model()->index( 0, 1 ), QItemSelectionModel::ClearAndSelect );
QCOMPARE( w.selectionForegroundColor(), QColor( 0, 255, 0 ) );
w.selectionModel()->select( w.model()->index( 0, 0 ), QItemSelectionModel::Select );
QVERIFY( !w.selectionForegroundColor().isValid() );
w.setSelectionForegroundColor( QColor( 255, 255, 0 ) );
QCOMPARE( spy.count(), 2 );
QCOMPARE( w.selectionForegroundColor(), QColor( 255, 255, 0 ) );
QCOMPARE( w.tableContents().at( 0 ).at( 0 ).foregroundColor(), QColor( 255, 255, 0 ) );
QCOMPARE( w.tableContents().at( 0 ).at( 1 ).foregroundColor(), QColor( 255, 255, 0 ) );
QVERIFY( !w.tableContents().at( 0 ).at( 2 ).foregroundColor().isValid() );
QVERIFY( !w.tableContents().at( 0 ).at( 3 ).foregroundColor().isValid() );
w.selectionModel()->select( w.model()->index( 0, 3 ), QItemSelectionModel::Select );
QVERIFY( !w.selectionForegroundColor().isValid() );
}

void TestQgsTableEditor::backgroundColor()
{
QgsTableEditorWidget w;
QVERIFY( w.tableContents().isEmpty() );

QSignalSpy spy( &w, &QgsTableEditorWidget::tableChanged );
QgsTableCell c3;
c3.setContent( 87 );
std::unique_ptr< QgsCurrencyNumericFormat > format = qgis::make_unique< QgsCurrencyNumericFormat >();
format->setNumberDecimalPlaces( 2 );
format->setPrefix( QStringLiteral( "$" ) );
c3.setNumericFormat( format.release() );
QgsTableCell c2( 76 );
c2.setBackgroundColor( QColor( 255, 0, 0 ) );
c2.setForegroundColor( QColor( 0, 255, 0 ) );
w.setTableContents( QgsTableContents() << ( QgsTableRow() << QgsTableCell( QStringLiteral( "Jet" ) ) << c2 << c3 << QgsTableCell( QStringLiteral( "Jet3" ) ) ) );
QCOMPARE( spy.count(), 1 );

QCOMPARE( w.tableContents().size(), 1 );
QCOMPARE( w.tableContents().at( 0 ).size(), 4 );
QCOMPARE( w.tableContents().at( 0 ).at( 0 ).content().toString(), QStringLiteral( "Jet" ) );
QVERIFY( !w.tableContents().at( 0 ).at( 0 ).backgroundColor().isValid() );
QVERIFY( !w.tableContents().at( 0 ).at( 0 ).foregroundColor().isValid() );
QVERIFY( !w.tableContents().at( 0 ).at( 0 ).numericFormat() );
QCOMPARE( w.tableContents().at( 0 ).at( 1 ).content().toString(), QStringLiteral( "76" ) );
QCOMPARE( w.tableContents().at( 0 ).at( 1 ).backgroundColor(), QColor( 255, 0, 0 ) );
QCOMPARE( w.tableContents().at( 0 ).at( 1 ).foregroundColor(), QColor( 0, 255, 0 ) );
QVERIFY( !w.tableContents().at( 0 ).at( 1 ).numericFormat() );
QCOMPARE( w.tableContents().at( 0 ).at( 2 ).content().toString(), QStringLiteral( "87" ) );
QVERIFY( !w.tableContents().at( 0 ).at( 2 ).backgroundColor().isValid() );
QVERIFY( !w.tableContents().at( 0 ).at( 2 ).foregroundColor().isValid() );
QVERIFY( w.tableContents().at( 0 ).at( 2 ).numericFormat() );
QCOMPARE( w.tableContents().at( 0 ).at( 2 ).numericFormat()->id(), QStringLiteral( "currency" ) );
QCOMPARE( w.tableContents().at( 0 ).at( 3 ).content().toString(), QStringLiteral( "Jet3" ) );
QVERIFY( !w.tableContents().at( 0 ).at( 3 ).backgroundColor().isValid() );
QVERIFY( !w.tableContents().at( 0 ).at( 3 ).foregroundColor().isValid() );
QVERIFY( !w.tableContents().at( 0 ).at( 3 ).numericFormat() );

w.selectionModel()->clearSelection();
w.setSelectionBackgroundColor( QColor( 255, 255, 0 ) );
QCOMPARE( spy.count(), 1 );

w.selectionModel()->select( w.model()->index( 0, 0 ), QItemSelectionModel::ClearAndSelect );
QVERIFY( !w.selectionBackgroundColor().isValid() );
w.selectionModel()->select( w.model()->index( 0, 1 ), QItemSelectionModel::Select );
QVERIFY( !w.selectionBackgroundColor().isValid() );
w.selectionModel()->select( w.model()->index( 0, 1 ), QItemSelectionModel::ClearAndSelect );
QCOMPARE( w.selectionBackgroundColor(), QColor( 255, 0, 0 ) );
w.selectionModel()->select( w.model()->index( 0, 0 ), QItemSelectionModel::Select );
QVERIFY( !w.selectionBackgroundColor().isValid() );
w.setSelectionBackgroundColor( QColor( 255, 255, 0 ) );
QCOMPARE( spy.count(), 2 );
QCOMPARE( w.selectionBackgroundColor(), QColor( 255, 255, 0 ) );
QCOMPARE( w.tableContents().at( 0 ).at( 0 ).backgroundColor(), QColor( 255, 255, 0 ) );
QCOMPARE( w.tableContents().at( 0 ).at( 1 ).backgroundColor(), QColor( 255, 255, 0 ) );
QVERIFY( !w.tableContents().at( 0 ).at( 2 ).backgroundColor().isValid() );
QVERIFY( !w.tableContents().at( 0 ).at( 3 ).backgroundColor().isValid() );
w.selectionModel()->select( w.model()->index( 0, 3 ), QItemSelectionModel::Select );
QVERIFY( !w.selectionBackgroundColor().isValid() );
}


QGSTEST_MAIN( TestQgsTableEditor )
#include "testqgstableeditor.moc"

0 comments on commit 9835ab0

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