Skip to content

Commit

Permalink
UX fixes for working with numeric formats in table editor
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 14, 2020
1 parent 024b321 commit 2b5598d
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 5 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ If the selected cells have a mix of different formats then ``None``
will be returned. will be returned.


.. seealso:: :py:func:`setSelectionNumericFormat` .. seealso:: :py:func:`setSelectionNumericFormat`

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

bool hasMixedSelectionNumericFormat();
%Docstring
Returns ``True`` if the current selection has a mix of numeric formats.

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


QColor selectionForegroundColor(); QColor selectionForegroundColor();
Expand Down
16 changes: 16 additions & 0 deletions src/gui/numericformats/qgsnumericformatwidget.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -311,6 +311,20 @@ QgsPercentageNumericFormatWidget::QgsPercentageNumericFormatWidget( const QgsNum
emit changed(); emit changed();
} ); } );


connect( mShowPlusCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
{
mFormat->setShowPlusSign( checked );
if ( !mBlockSignals )
emit changed();
} );

connect( mShowThousandsCheckBox, &QCheckBox::toggled, this, [ = ]( bool checked )
{
mFormat->setShowThousandsSeparator( checked );
if ( !mBlockSignals )
emit changed();
} );

connect( mDecimalsSpinBox, qgis::overload<int>::of( &QSpinBox::valueChanged ), this, [ = ]( int value ) connect( mDecimalsSpinBox, qgis::overload<int>::of( &QSpinBox::valueChanged ), this, [ = ]( int value )
{ {
mFormat->setNumberDecimalPlaces( value ); mFormat->setNumberDecimalPlaces( value );
Expand All @@ -334,7 +348,9 @@ void QgsPercentageNumericFormatWidget::setFormat( QgsNumericFormat *format )


mBlockSignals = true; mBlockSignals = true;
mDecimalsSpinBox->setValue( mFormat->numberDecimalPlaces() ); mDecimalsSpinBox->setValue( mFormat->numberDecimalPlaces() );
mShowPlusCheckBox->setChecked( mFormat->showPlusSign() );
mShowTrailingZerosCheckBox->setChecked( mFormat->showTrailingZeros() ); mShowTrailingZerosCheckBox->setChecked( mFormat->showTrailingZeros() );
mShowThousandsCheckBox->setChecked( mFormat->showThousandsSeparator() );
mScalingComboBox->setCurrentIndex( mScalingComboBox->findData( static_cast< int >( mFormat->inputValues() ) ) ); mScalingComboBox->setCurrentIndex( mScalingComboBox->findData( static_cast< int >( mFormat->inputValues() ) ) );
mBlockSignals = false; mBlockSignals = false;
} }
Expand Down
3 changes: 2 additions & 1 deletion src/gui/tableeditor/qgstableeditordialog.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ QgsTableEditorDialog::QgsTableEditorDialog( QWidget *parent )
emit tableChanged(); emit tableChanged();
} ); } );


int minDockWidth( fontMetrics().width( QStringLiteral( "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ) ) ); int minDockWidth( fontMetrics().boundingRect( QStringLiteral( "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ) ).width() );


mPropertiesDock = new QgsDockWidget( tr( "Formatting" ), this ); mPropertiesDock = new QgsDockWidget( tr( "Formatting" ), this );
mPropertiesDock->setObjectName( QStringLiteral( "FormattingDock" ) ); mPropertiesDock->setObjectName( QStringLiteral( "FormattingDock" ) );
Expand All @@ -93,6 +93,7 @@ QgsTableEditorDialog::QgsTableEditorDialog( QWidget *parent )
{ {
mFormattingWidget->setForegroundColor( mTableWidget->selectionForegroundColor() ); mFormattingWidget->setForegroundColor( mTableWidget->selectionForegroundColor() );
mFormattingWidget->setBackgroundColor( mTableWidget->selectionBackgroundColor() ); mFormattingWidget->setBackgroundColor( mTableWidget->selectionBackgroundColor() );
mFormattingWidget->setNumericFormat( mTableWidget->selectionNumericFormat(), mTableWidget->hasMixedSelectionNumericFormat() );
} ); } );


addDockWidget( Qt::RightDockWidgetArea, mPropertiesDock ); addDockWidget( Qt::RightDockWidgetArea, mPropertiesDock );
Expand Down
22 changes: 18 additions & 4 deletions src/gui/tableeditor/qgstableeditorformattingwidget.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ QgsTableEditorFormattingWidget::QgsTableEditorFormattingWidget( QWidget *parent
setupUi( this ); setupUi( this );
setPanelTitle( tr( "Formatting" ) ); setPanelTitle( tr( "Formatting" ) );


mFormatNumbersCheckBox->setTristate( false );

mTextColorButton->setAllowOpacity( true ); mTextColorButton->setAllowOpacity( true );
mTextColorButton->setColorDialogTitle( tr( "Text Color" ) ); mTextColorButton->setColorDialogTitle( tr( "Text Color" ) );
mTextColorButton->setDefaultColor( QColor( 0, 0, 0 ) ); mTextColorButton->setDefaultColor( QColor( 0, 0, 0 ) );
Expand All @@ -42,10 +44,13 @@ QgsTableEditorFormattingWidget::QgsTableEditorFormattingWidget( QWidget *parent
emit backgroundColorChanged( mBackgroundColorButton->color() ); emit backgroundColorChanged( mBackgroundColorButton->color() );
} ); } );


connect( mFormatNumbersCheckBox, &QCheckBox::toggled, this, [ = ]( bool active ) connect( mFormatNumbersCheckBox, &QCheckBox::stateChanged, this, [ = ]( int state )
{ {
mCustomizeFormatButton->setEnabled( active ); mCustomizeFormatButton->setEnabled( state == Qt::Checked );
emit numberFormatChanged(); if ( state != Qt::PartiallyChecked )
mFormatNumbersCheckBox->setTristate( false );
if ( !mBlockSignals )
emit numberFormatChanged();
} ); } );


mCustomizeFormatButton->setEnabled( false ); mCustomizeFormatButton->setEnabled( false );
Expand All @@ -65,7 +70,7 @@ QgsTableEditorFormattingWidget::QgsTableEditorFormattingWidget( QWidget *parent


QgsNumericFormat *QgsTableEditorFormattingWidget::numericFormat() QgsNumericFormat *QgsTableEditorFormattingWidget::numericFormat()
{ {
if ( !mNumericFormat || !mFormatNumbersCheckBox->isChecked() ) if ( !mNumericFormat || mFormatNumbersCheckBox->checkState() != Qt::Checked )
return nullptr; return nullptr;


return mNumericFormat->clone(); return mNumericFormat->clone();
Expand All @@ -85,4 +90,13 @@ void QgsTableEditorFormattingWidget::setBackgroundColor( const QColor &color )
mBlockSignals = false; mBlockSignals = false;
} }


void QgsTableEditorFormattingWidget::setNumericFormat( QgsNumericFormat *format, bool isMixedFormat )
{
mNumericFormat.reset( format ? format->clone() : nullptr );
mBlockSignals = true;
mFormatNumbersCheckBox->setTristate( isMixedFormat );
mFormatNumbersCheckBox->setCheckState( isMixedFormat ? Qt::PartiallyChecked : ( mNumericFormat.get() ? Qt::Checked : Qt::Unchecked ) );
mBlockSignals = false;
}

QgsTableEditorFormattingWidget::~QgsTableEditorFormattingWidget() = default; QgsTableEditorFormattingWidget::~QgsTableEditorFormattingWidget() = default;
11 changes: 11 additions & 0 deletions src/gui/tableeditor/qgstableeditorformattingwidget.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class GUI_EXPORT QgsTableEditorFormattingWidget : public QgsPanelWidget, private
* if no numeric format is set. * if no numeric format is set.
* *
* The caller takes ownership of the returned object. * The caller takes ownership of the returned object.
*
* \see setNumericFormat()
*/ */
QgsNumericFormat *numericFormat() SIP_TRANSFERBACK; QgsNumericFormat *numericFormat() SIP_TRANSFERBACK;


Expand All @@ -71,6 +73,15 @@ class GUI_EXPORT QgsTableEditorFormattingWidget : public QgsPanelWidget, private
*/ */
void setBackgroundColor( const QColor &color ); void setBackgroundColor( const QColor &color );


/**
* Sets the numeric \a format to show in the widget, or NULLPTR if no numeric format is set.
*
* if \a isMixedFormat is TRUE then the widget will be set to indicate a "mixed format" setting.
*
* \see numericFormat()
*/
void setNumericFormat( QgsNumericFormat *format, bool isMixedFormat );

signals: signals:


/** /**
Expand Down
30 changes: 30 additions & 0 deletions src/gui/tableeditor/qgstableeditorwidget.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -390,6 +390,36 @@ QgsNumericFormat *QgsTableEditorWidget::selectionNumericFormat()
return f; return f;
} }


bool QgsTableEditorWidget::hasMixedSelectionNumericFormat()
{
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 true;
}
}
else if ( f )
{
return true;
}
}
return false;
}

QColor QgsTableEditorWidget::selectionForegroundColor() QColor QgsTableEditorWidget::selectionForegroundColor()
{ {
QColor c; QColor c;
Expand Down
8 changes: 8 additions & 0 deletions src/gui/tableeditor/qgstableeditorwidget.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -75,9 +75,17 @@ class GUI_EXPORT QgsTableEditorWidget : public QTableWidget
* will be returned. * will be returned.
* *
* \see setSelectionNumericFormat() * \see setSelectionNumericFormat()
* \see hasMixedSelectionNumericFormat()
*/ */
QgsNumericFormat *selectionNumericFormat(); QgsNumericFormat *selectionNumericFormat();


/**
* Returns TRUE if the current selection has a mix of numeric formats.
*
* \see selectionNumericFormat()
*/
bool hasMixedSelectionNumericFormat();

/** /**
* Returns the foreground color for the currently selected cells. * Returns the foreground color for the currently selected cells.
* *
Expand Down
7 changes: 7 additions & 0 deletions tests/src/gui/testqgstableeditor.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -876,25 +876,32 @@ void TestQgsTableEditor::numericFormat()


w.selectionModel()->clearSelection(); w.selectionModel()->clearSelection();
w.setSelectionNumericFormat( new QgsBearingNumericFormat() ); w.setSelectionNumericFormat( new QgsBearingNumericFormat() );
QVERIFY( !w.hasMixedSelectionNumericFormat() );
QCOMPARE( spy.count(), 1 ); QCOMPARE( spy.count(), 1 );


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




Expand Down

0 comments on commit 2b5598d

Please sign in to comment.