Skip to content
Permalink
Browse files

UX fixes for working with numeric formats in table editor

  • Loading branch information
nyalldawson committed Jan 13, 2020
1 parent 024b321 commit 2b5598d8911854a6d9d38ca90938dec034257e5a
@@ -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();
@@ -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 );
@@ -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;
}
@@ -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" ) );
@@ -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 );
@@ -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 ) );
@@ -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 );
@@ -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();
@@ -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;
@@ -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;

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

/**
@@ -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;
@@ -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.
*
@@ -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() );
}


0 comments on commit 2b5598d

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