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.
Expand Up @@ -63,6 +63,15 @@ If the selected cells have a mix of different formats then ``None``
will be returned.

.. 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

QColor selectionForegroundColor();
Expand Down
16 changes: 16 additions & 0 deletions src/gui/numericformats/qgsnumericformatwidget.cpp
Expand Up @@ -311,6 +311,20 @@ QgsPercentageNumericFormatWidget::QgsPercentageNumericFormatWidget( const QgsNum
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 )
{
mFormat->setNumberDecimalPlaces( value );
Expand All @@ -334,7 +348,9 @@ void QgsPercentageNumericFormatWidget::setFormat( QgsNumericFormat *format )

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

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

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

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

mFormatNumbersCheckBox->setTristate( false );

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

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

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

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

return mNumericFormat->clone();
Expand All @@ -85,4 +90,13 @@ void QgsTableEditorFormattingWidget::setBackgroundColor( const QColor &color )
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;
11 changes: 11 additions & 0 deletions src/gui/tableeditor/qgstableeditorformattingwidget.h
Expand Up @@ -52,6 +52,8 @@ class GUI_EXPORT QgsTableEditorFormattingWidget : public QgsPanelWidget, private
* if no numeric format is set.
*
* The caller takes ownership of the returned object.
*
* \see setNumericFormat()
*/
QgsNumericFormat *numericFormat() SIP_TRANSFERBACK;

Expand All @@ -71,6 +73,15 @@ class GUI_EXPORT QgsTableEditorFormattingWidget : public QgsPanelWidget, private
*/
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:

/**
Expand Down
30 changes: 30 additions & 0 deletions src/gui/tableeditor/qgstableeditorwidget.cpp
Expand Up @@ -390,6 +390,36 @@ QgsNumericFormat *QgsTableEditorWidget::selectionNumericFormat()
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 c;
Expand Down
8 changes: 8 additions & 0 deletions src/gui/tableeditor/qgstableeditorwidget.h
Expand Up @@ -75,9 +75,17 @@ class GUI_EXPORT QgsTableEditorWidget : public QTableWidget
* will be returned.
*
* \see setSelectionNumericFormat()
* \see hasMixedSelectionNumericFormat()
*/
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.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/src/gui/testqgstableeditor.cpp
Expand Up @@ -876,25 +876,32 @@ void TestQgsTableEditor::numericFormat()

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

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


Expand Down

0 comments on commit 2b5598d

Please sign in to comment.