Skip to content

Commit

Permalink
[layouts] Improve UI for formatting manual table cells
Browse files Browse the repository at this point in the history
Instead of the confusing duplicate settings for foreground color
vs cell text color, remove the foreground color button and only
expose this color setting via the cell text color.
  • Loading branch information
nyalldawson committed Sep 22, 2020
1 parent 8e9bf8b commit 8dd75cc
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 291 deletions.
Expand Up @@ -76,7 +76,7 @@ Returns ``True`` if the current selection has a mix of numeric formats.
.. seealso:: :py:func:`selectionNumericFormat`
%End

QColor selectionForegroundColor();
QColor selectionForegroundColor() /Deprecated/;
%Docstring
Returns the foreground color for the currently selected cells.

Expand All @@ -86,6 +86,9 @@ invalid color will be returned.
.. seealso:: :py:func:`setSelectionForegroundColor`

.. seealso:: :py:func:`selectionBackgroundColor`

.. deprecated::
use selectionTextFormat() instead.
%End

QColor selectionBackgroundColor();
Expand Down Expand Up @@ -278,13 +281,16 @@ current selected cells.
Clears the contents of the currently selected cells.
%End

void setSelectionForegroundColor( const QColor &color );
void setSelectionForegroundColor( const QColor &color ) /Deprecated/;
%Docstring
Sets the foreground color for the currently selected cells.

.. seealso:: :py:func:`selectionForegroundColor`

.. seealso:: :py:func:`setSelectionBackgroundColor`

.. deprecated::
Use setSelectionTextFormat() instead.
%End

void setSelectionBackgroundColor( const QColor &color );
Expand Down
2 changes: 0 additions & 2 deletions src/gui/tableeditor/qgstableeditordialog.cpp
Expand Up @@ -77,7 +77,6 @@ QgsTableEditorDialog::QgsTableEditorDialog( QWidget *parent )

mPropertiesDock->setFeatures( QDockWidget::NoDockWidgetFeatures );

connect( mFormattingWidget, &QgsTableEditorFormattingWidget::foregroundColorChanged, mTableWidget, &QgsTableEditorWidget::setSelectionForegroundColor );
connect( mFormattingWidget, &QgsTableEditorFormattingWidget::backgroundColorChanged, mTableWidget, &QgsTableEditorWidget::setSelectionBackgroundColor );

connect( mFormattingWidget, &QgsTableEditorFormattingWidget::horizontalAlignmentChanged, mTableWidget, &QgsTableEditorWidget::setSelectionHorizontalAlignment );
Expand All @@ -98,7 +97,6 @@ QgsTableEditorDialog::QgsTableEditorDialog( QWidget *parent )

connect( mTableWidget, &QgsTableEditorWidget::activeCellChanged, this, [ = ]
{
mFormattingWidget->setForegroundColor( mTableWidget->selectionForegroundColor() );
mFormattingWidget->setBackgroundColor( mTableWidget->selectionBackgroundColor() );
mFormattingWidget->setNumericFormat( mTableWidget->selectionNumericFormat(), mTableWidget->hasMixedSelectionNumericFormat() );
mFormattingWidget->setRowHeight( mTableWidget->selectionRowHeight() );
Expand Down
21 changes: 0 additions & 21 deletions src/gui/tableeditor/qgstableeditorformattingwidget.cpp
Expand Up @@ -30,10 +30,6 @@ QgsTableEditorFormattingWidget::QgsTableEditorFormattingWidget( QWidget *parent
mFontButton->setShowNullFormat( true );
mFontButton->setNoFormatString( tr( "Clear Formatting" ) );

mTextColorButton->setAllowOpacity( true );
mTextColorButton->setColorDialogTitle( tr( "Text Color" ) );
mTextColorButton->setDefaultColor( QColor( 0, 0, 0 ) );
mTextColorButton->setShowNull( true );
mBackgroundColorButton->setAllowOpacity( true );
mBackgroundColorButton->setColorDialogTitle( tr( "Text Color" ) );
mBackgroundColorButton->setDefaultColor( QColor( 255, 255, 255 ) );
Expand All @@ -45,16 +41,6 @@ QgsTableEditorFormattingWidget::QgsTableEditorFormattingWidget( QWidget *parent
mRowHeightSpinBox->setClearValue( 0, tr( "Automatic" ) );
mColumnWidthSpinBox->setClearValue( 0, tr( "Automatic" ) );

connect( mTextColorButton, &QgsColorButton::colorChanged, this, [ = ]
{
if ( !mBlockSignals )
emit foregroundColorChanged( mTextColorButton->color() );
} );
connect( mTextColorButton, &QgsColorButton::cleared, this, [ = ]
{
if ( !mBlockSignals )
emit foregroundColorChanged( QColor() );
} );
connect( mBackgroundColorButton, &QgsColorButton::colorChanged, this, [ = ]
{
if ( !mBlockSignals )
Expand Down Expand Up @@ -161,13 +147,6 @@ QgsTextFormat QgsTableEditorFormattingWidget::textFormat() const
return mFontButton->textFormat();
}

void QgsTableEditorFormattingWidget::setForegroundColor( const QColor &color )
{
mBlockSignals++;
mTextColorButton->setColor( color );
mBlockSignals--;
}

void QgsTableEditorFormattingWidget::setBackgroundColor( const QColor &color )
{
mBlockSignals++;
Expand Down
15 changes: 0 additions & 15 deletions src/gui/tableeditor/qgstableeditorformattingwidget.h
Expand Up @@ -67,14 +67,6 @@ class GUI_EXPORT QgsTableEditorFormattingWidget : public QgsPanelWidget, public
*/
QgsTextFormat textFormat() const;

/**
* Sets the cell foreground \a color to show in the widget.
*
* \see foregroundColorChanged()
* \see setBackgroundColor()
*/
void setForegroundColor( const QColor &color );

/**
* Sets the cell background \a color to show in the widget.
*
Expand Down Expand Up @@ -154,13 +146,6 @@ class GUI_EXPORT QgsTableEditorFormattingWidget : public QgsPanelWidget, public

signals:

/**
* Emitted whenever the cell foreground \a color is changed in the widget.
*
* \see setForegroundColor()
*/
void foregroundColorChanged( const QColor &color );

/**
* Emitted whenever the cell background \a color is changed in the widget.
*
Expand Down
32 changes: 11 additions & 21 deletions src/gui/tableeditor/qgstableeditorwidget.cpp
Expand Up @@ -324,7 +324,7 @@ void QgsTableEditorWidget::setTableContents( const QgsTableContents &contents )
item->setData( CellContent, col.content() ); // can't use EditRole, because Qt. (https://bugreports.qt.io/browse/QTBUG-11549)
item->setData( Qt::BackgroundRole, col.backgroundColor().isValid() ? col.backgroundColor() : QColor( 255, 255, 255 ) );
item->setData( PresetBackgroundColorRole, col.backgroundColor().isValid() ? col.backgroundColor() : QVariant() );
item->setData( Qt::ForegroundRole, col.foregroundColor().isValid() ? col.foregroundColor() : QVariant() );
item->setData( Qt::ForegroundRole, col.textFormat().isValid() ? col.textFormat().color() : QVariant() );
item->setData( TextFormat, QVariant::fromValue( col.textFormat() ) );
item->setData( HorizontalAlignment, static_cast< int >( col.horizontalAlignment() ) );
item->setData( VerticalAlignment, static_cast< int >( col.verticalAlignment() ) );
Expand Down Expand Up @@ -372,7 +372,6 @@ QgsTableContents QgsTableEditorWidget::tableContents() const
{
cell.setContent( i->data( CellProperty ).value< QgsProperty >().isActive() ? i->data( CellProperty ) : i->data( CellContent ) );
cell.setBackgroundColor( i->data( PresetBackgroundColorRole ).value< QColor >() );
cell.setForegroundColor( i->data( Qt::ForegroundRole ).value< QColor >() );
cell.setTextFormat( i->data( TextFormat ).value< QgsTextFormat >() );
cell.setHorizontalAlignment( static_cast< Qt::Alignment >( i->data( HorizontalAlignment ).toInt() ) );
cell.setVerticalAlignment( static_cast< Qt::Alignment >( i->data( VerticalAlignment ).toInt() ) );
Expand Down Expand Up @@ -494,25 +493,8 @@ bool QgsTableEditorWidget::hasMixedSelectionNumericFormat()

QColor QgsTableEditorWidget::selectionForegroundColor()
{
QColor c;
bool first = true;
const QModelIndexList selection = selectedIndexes();
for ( const QModelIndex &index : selection )
{
QColor indexColor = model()->data( index, Qt::ForegroundRole ).isValid() ? model()->data( index, Qt::ForegroundRole ).value< QColor >() : QColor();
if ( first )
{
c = indexColor;
first = false;
}
else if ( indexColor == c )
continue;
else
{
return QColor();
}
}
return c;
const QgsTextFormat f = selectionTextFormat();
return f.isValid() ? f.color() : QColor();
}

QColor QgsTableEditorWidget::selectionBackgroundColor()
Expand Down Expand Up @@ -963,13 +945,19 @@ void QgsTableEditorWidget::setSelectionForegroundColor( const QColor &color )
if ( i->data( Qt::ForegroundRole ).value< QColor >() != color )
{
i->setData( Qt::ForegroundRole, color.isValid() ? color : QVariant() );
QgsTextFormat f = i->data( TextFormat ).value< QgsTextFormat >();
f.setColor( color );
i->setData( TextFormat, QVariant::fromValue( f ) );
changed = true;
}
}
else
{
QTableWidgetItem *newItem = new QTableWidgetItem();
newItem->setData( Qt::ForegroundRole, color.isValid() ? color : QVariant() );
QgsTextFormat f;
f.setColor( color );
newItem->setData( TextFormat, QVariant::fromValue( f ) );
setItem( index.row(), index.column(), newItem );
changed = true;
}
Expand Down Expand Up @@ -1138,12 +1126,14 @@ void QgsTableEditorWidget::setSelectionTextFormat( const QgsTextFormat &format )
if ( QTableWidgetItem *i = item( index.row(), index.column() ) )
{
i->setData( TextFormat, QVariant::fromValue( format ) );
i->setData( Qt::ForegroundRole, format.color() );
changed = true;
}
else
{
QTableWidgetItem *newItem = new QTableWidgetItem();
newItem->setData( TextFormat, QVariant::fromValue( format ) );
newItem->setData( Qt::ForegroundRole, format.color() );
setItem( index.row(), index.column(), newItem );
changed = true;
}
Expand Down
8 changes: 6 additions & 2 deletions src/gui/tableeditor/qgstableeditorwidget.h
Expand Up @@ -169,8 +169,10 @@ class GUI_EXPORT QgsTableEditorWidget : public QTableWidget
*
* \see setSelectionForegroundColor()
* \see selectionBackgroundColor()
*
* \deprecated use selectionTextFormat() instead.
*/
QColor selectionForegroundColor();
Q_DECL_DEPRECATED QColor selectionForegroundColor() SIP_DEPRECATED;

/**
* Returns the background color for the currently selected cells.
Expand Down Expand Up @@ -364,8 +366,10 @@ class GUI_EXPORT QgsTableEditorWidget : public QTableWidget
*
* \see selectionForegroundColor()
* \see setSelectionBackgroundColor()
*
* \deprecated Use setSelectionTextFormat() instead.
*/
void setSelectionForegroundColor( const QColor &color );
Q_DECL_DEPRECATED void setSelectionForegroundColor( const QColor &color ) SIP_DEPRECATED;

/**
* Sets the background color for the currently selected cells.
Expand Down

0 comments on commit 8dd75cc

Please sign in to comment.