Skip to content

Commit

Permalink
Unit tests and fixes for table numeric format setting/retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 14, 2020
1 parent 31cdf13 commit 024b321
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 3 deletions.
13 changes: 13 additions & 0 deletions python/gui/auto_generated/tableeditor/qgstableeditorwidget.sip.in
Expand Up @@ -50,6 +50,19 @@ Returns the current contents of the editor widget table.
Sets the numeric ``format`` to use for the currently selected cells.

Ownership of ``format`` is transferred to the widget.

.. seealso:: :py:func:`selectionNumericFormat`
%End

QgsNumericFormat *selectionNumericFormat();
%Docstring
Returns the numeric format used for the currently selected cells, or
``None`` if the selection has no numeric format set.

If the selected cells have a mix of different formats then ``None``
will be returned.

.. seealso:: :py:func:`setSelectionNumericFormat`
%End

QColor selectionForegroundColor();
Expand Down
54 changes: 51 additions & 3 deletions src/gui/tableeditor/qgstableeditorwidget.cpp
Expand Up @@ -325,6 +325,8 @@ QgsTableContents QgsTableEditorWidget::tableContents() const

void QgsTableEditorWidget::setSelectionNumericFormat( QgsNumericFormat *format )
{
bool changed = false;
mBlockSignals++;
std::unique_ptr< QgsNumericFormat > newFormat( format );
const QModelIndexList selection = selectedIndexes();
for ( const QModelIndex &index : selection )
Expand All @@ -335,13 +337,59 @@ void QgsTableEditorWidget::setSelectionNumericFormat( QgsNumericFormat *format )
i = new QTableWidgetItem();
setItem( index.row(), index.column(), i );
}
delete mNumericFormats.value( i );
mNumericFormats.insert( i, newFormat ? newFormat->clone() : nullptr );
if ( !mNumericFormats.value( i ) && newFormat )
{
changed = true;
mNumericFormats.insert( i, newFormat->clone() );
}
else if ( mNumericFormats.value( i ) && !newFormat )
{
changed = true;
delete mNumericFormats.value( i );
mNumericFormats.remove( i );
}
else if ( newFormat && *newFormat != *mNumericFormats.value( i ) )
{
changed = true;
delete mNumericFormats.value( i );
mNumericFormats.insert( i, newFormat->clone() );
}
}
if ( !mBlockSignals )
mBlockSignals--;
if ( changed && !mBlockSignals )
emit tableChanged();
}

QgsNumericFormat *QgsTableEditorWidget::selectionNumericFormat()
{
QgsNumericFormat *f = nullptr;
bool first = true;
const QModelIndexList selection = selectedIndexes();
for ( const QModelIndex &index : selection )
{
if ( QTableWidgetItem *i = item( index.row(), index.column() ) )
{
if ( first )
{
f = mNumericFormats.value( i );
first = false;
}
else if ( ( !f && !mNumericFormats.value( i ) )
|| ( f && mNumericFormats.value( i ) && *f == *mNumericFormats.value( i ) ) )
continue;
else
{
return nullptr;
}
}
else
{
return nullptr;
}
}
return f;
}

QColor QgsTableEditorWidget::selectionForegroundColor()
{
QColor c;
Expand Down
13 changes: 13 additions & 0 deletions src/gui/tableeditor/qgstableeditorwidget.h
Expand Up @@ -62,9 +62,22 @@ class GUI_EXPORT QgsTableEditorWidget : public QTableWidget
* Sets the numeric \a format to use for the currently selected cells.
*
* Ownership of \a format is transferred to the widget.
*
* \see selectionNumericFormat()
*/
void setSelectionNumericFormat( QgsNumericFormat *format SIP_TRANSFER );

/**
* Returns the numeric format used for the currently selected cells, or
* NULLPTR if the selection has no numeric format set.
*
* If the selected cells have a mix of different formats then NULLPTR
* will be returned.
*
* \see setSelectionNumericFormat()
*/
QgsNumericFormat *selectionNumericFormat();

/**
* Returns the foreground color for the currently selected cells.
*
Expand Down
63 changes: 63 additions & 0 deletions tests/src/gui/testqgstableeditor.cpp
Expand Up @@ -18,6 +18,7 @@

#include "qgstableeditorwidget.h"
#include "qgscurrencynumericformat.h"
#include "qgsbearingnumericformat.h"

class TestQgsTableEditor: public QObject
{
Expand All @@ -39,6 +40,7 @@ class TestQgsTableEditor: public QObject
void clearSelected();
void foregroundColor();
void backgroundColor();
void numericFormat();

private:

Expand Down Expand Up @@ -834,6 +836,67 @@ void TestQgsTableEditor::backgroundColor()
QVERIFY( !w.selectionBackgroundColor().isValid() );
}

void TestQgsTableEditor::numericFormat()
{
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( "$" ) );
QgsTableCell c2( 76 );
c2.setNumericFormat( format.release() );
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( 1 ).numericFormat()->id(), QStringLiteral( "currency" ) );
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( 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.setSelectionNumericFormat( new QgsBearingNumericFormat() );
QCOMPARE( spy.count(), 1 );

w.selectionModel()->select( w.model()->index( 0, 0 ), QItemSelectionModel::ClearAndSelect );
QVERIFY( !w.selectionNumericFormat() );
w.selectionModel()->select( w.model()->index( 0, 1 ), QItemSelectionModel::Select );
QVERIFY( !w.selectionNumericFormat() );
w.selectionModel()->select( w.model()->index( 0, 1 ), QItemSelectionModel::ClearAndSelect );
QCOMPARE( w.selectionNumericFormat()->id(), QStringLiteral( "currency" ) );
w.selectionModel()->select( w.model()->index( 0, 0 ), QItemSelectionModel::Select );
QVERIFY( !w.selectionNumericFormat() );
w.setSelectionNumericFormat( new QgsBearingNumericFormat() );
QCOMPARE( spy.count(), 2 );
QCOMPARE( w.selectionNumericFormat()->id(), QStringLiteral( "bearing" ) );
QCOMPARE( w.tableContents().at( 0 ).at( 0 ).numericFormat()->id(), QStringLiteral( "bearing" ) );
QCOMPARE( w.tableContents().at( 0 ).at( 1 ).numericFormat()->id(), QStringLiteral( "bearing" ) );
QVERIFY( !w.tableContents().at( 0 ).at( 2 ).numericFormat() );
QVERIFY( !w.tableContents().at( 0 ).at( 3 ).numericFormat() );
w.selectionModel()->select( w.model()->index( 0, 3 ), QItemSelectionModel::Select );
QVERIFY( !w.selectionNumericFormat() );
}


QGSTEST_MAIN( TestQgsTableEditor )
#include "testqgstableeditor.moc"

0 comments on commit 024b321

Please sign in to comment.