Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Expose text format override options to table editor widget
  • Loading branch information
nyalldawson committed Jul 12, 2020
1 parent c962eaa commit b584f93
Show file tree
Hide file tree
Showing 11 changed files with 344 additions and 8 deletions.
Expand Up @@ -524,6 +524,7 @@ Returns a pixmap preview for a text ``format``.

};


/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
Expand Up @@ -76,6 +76,8 @@ Returns ``True`` if the current selection has a mix of numeric formats.
.. seealso:: :py:func:`selectionNumericFormat`
%End

bool hasMixedSelectionHasTextFormat();

QColor selectionForegroundColor();
%Docstring
Returns the foreground color for the currently selected cells.
Expand All @@ -100,6 +102,9 @@ invalid color will be returned.
.. seealso:: :py:func:`selectionForegroundColor`
%End

QgsTextFormat selectionTextFormat();
bool selectionHasTextFormat();

double selectionRowHeight();
%Docstring
Returns the height (in millimeters) of the rows associated with the current selection,
Expand Down Expand Up @@ -257,6 +262,9 @@ Sets the background color for the currently selected cells.
.. seealso:: :py:func:`setSelectionForegroundColor`
%End

void setSelectionTextFormat( const QgsTextFormat &format );
void setSelectionHasTextFormat( bool hasFormat );

void setSelectionRowHeight( double height );
%Docstring
Sets the row ``height`` (in millimeters) for the currently selected rows, or 0 for automatic row height.
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsapplication.cpp
Expand Up @@ -235,6 +235,7 @@ void QgsApplication::init( QString profileFolder )
qRegisterMetaTypeStreamOperators<QgsProcessingModelChildParameterSource>( "QgsProcessingModelChildParameterSource" );
qRegisterMetaType<QgsRemappingSinkDefinition>( "QgsRemappingSinkDefinition" );
qRegisterMetaType<QgsProcessingModelChildDependency>( "QgsProcessingModelChildDependency" );
qRegisterMetaType<QgsTextFormat>( "QgsTextFormat" );
QMetaType::registerComparators<QgsProcessingModelChildDependency>();
QMetaType::registerEqualsComparator<QgsProcessingFeatureSourceDefinition>();
QMetaType::registerEqualsComparator<QgsProperty>();
Expand Down
2 changes: 2 additions & 0 deletions src/core/textrenderer/qgstextformat.h
Expand Up @@ -486,4 +486,6 @@ class CORE_EXPORT QgsTextFormat

};

Q_DECLARE_METATYPE( QgsTextFormat )

#endif // QGSTEXTFORMAT_H
11 changes: 11 additions & 0 deletions src/gui/tableeditor/qgstableeditordialog.cpp
Expand Up @@ -78,6 +78,16 @@ QgsTableEditorDialog::QgsTableEditorDialog( QWidget *parent )

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

connect( mFormattingWidget, &QgsTableEditorFormattingWidget::textFormatChanged, this, [ = ]
{
mTableWidget->setSelectionTextFormat( mFormattingWidget->textFormat() );
} );
connect( mFormattingWidget, &QgsTableEditorFormattingWidget::hasTextFormatChanged, this, [ = ]
{
mTableWidget->setSelectionHasTextFormat( mFormattingWidget->textFormatSet() );
} );

connect( mFormattingWidget, &QgsTableEditorFormattingWidget::numberFormatChanged, this, [ = ]
{
mTableWidget->setSelectionNumericFormat( mFormattingWidget->numericFormat() );
Expand All @@ -92,6 +102,7 @@ QgsTableEditorDialog::QgsTableEditorDialog( QWidget *parent )
mFormattingWidget->setNumericFormat( mTableWidget->selectionNumericFormat(), mTableWidget->hasMixedSelectionNumericFormat() );
mFormattingWidget->setRowHeight( mTableWidget->selectionRowHeight() );
mFormattingWidget->setColumnWidth( mTableWidget->selectionColumnWidth() );
mFormattingWidget->setTextFormat( mTableWidget->selectionTextFormat(), mTableWidget->selectionHasTextFormat(), mTableWidget->hasMixedSelectionHasTextFormat() );

updateActionNamesFromSelection();

Expand Down
35 changes: 35 additions & 0 deletions src/gui/tableeditor/qgstableeditorformattingwidget.cpp
Expand Up @@ -25,6 +25,7 @@ QgsTableEditorFormattingWidget::QgsTableEditorFormattingWidget( QWidget *parent
setPanelTitle( tr( "Formatting" ) );

mFormatNumbersCheckBox->setTristate( false );
mTextFormatCheckBox->setTristate( false );

mTextColorButton->setAllowOpacity( true );
mTextColorButton->setColorDialogTitle( tr( "Text Color" ) );
Expand Down Expand Up @@ -68,6 +69,21 @@ QgsTableEditorFormattingWidget::QgsTableEditorFormattingWidget( QWidget *parent
emit numberFormatChanged();
} );

connect( mTextFormatCheckBox, &QCheckBox::stateChanged, this, [ = ]( int state )
{
mFontButton->setEnabled( state == Qt::Checked );
if ( state != Qt::PartiallyChecked )
mTextFormatCheckBox->setTristate( false );
if ( !mBlockSignals )
emit hasTextFormatChanged();
} );

connect( mFontButton, &QgsFontButton::changed, this, [ = ]
{
if ( !mBlockSignals )
emit textFormatChanged();
} );

mCustomizeFormatButton->setEnabled( false );
connect( mCustomizeFormatButton, &QPushButton::clicked, this, [ = ]
{
Expand Down Expand Up @@ -114,6 +130,16 @@ QgsNumericFormat *QgsTableEditorFormattingWidget::numericFormat()
return mNumericFormat->clone();
}

QgsTextFormat QgsTableEditorFormattingWidget::textFormat() const
{
return mFontButton->textFormat();
}

bool QgsTableEditorFormattingWidget::textFormatSet() const
{
return mTextFormatCheckBox->checkState() == Qt::Checked;
}

void QgsTableEditorFormattingWidget::setForegroundColor( const QColor &color )
{
mBlockSignals++;
Expand All @@ -137,6 +163,15 @@ void QgsTableEditorFormattingWidget::setNumericFormat( QgsNumericFormat *format,
mBlockSignals--;
}

void QgsTableEditorFormattingWidget::setTextFormat( const QgsTextFormat &format, bool isSet, bool isMixedFormat )
{
mBlockSignals++;
mTextFormatCheckBox->setTristate( isMixedFormat );
mTextFormatCheckBox->setCheckState( isMixedFormat ? Qt::PartiallyChecked : ( isSet ? Qt::Checked : Qt::Unchecked ) );
mFontButton->setTextFormat( format );
mBlockSignals--;
}

void QgsTableEditorFormattingWidget::setRowHeight( double height )
{
mBlockSignals++;
Expand Down
41 changes: 41 additions & 0 deletions src/gui/tableeditor/qgstableeditorformattingwidget.h
Expand Up @@ -57,6 +57,23 @@ class GUI_EXPORT QgsTableEditorFormattingWidget : public QgsPanelWidget, private
*/
QgsNumericFormat *numericFormat() SIP_TRANSFERBACK;

/**
* Returns the current text format shown in the widget.
*
* \see setTextFormat()
* \see textFormatSet()
* \since QGIS 3.16
*/
QgsTextFormat textFormat() const;

/**
* Returns TRUE if a text format is set in the widget.
*
* \see textFormat()
* \since QGIS 3.16
*/
bool textFormatSet() const;

/**
* Sets the cell foreground \a color to show in the widget.
*
Expand All @@ -82,6 +99,16 @@ class GUI_EXPORT QgsTableEditorFormattingWidget : public QgsPanelWidget, private
*/
void setNumericFormat( QgsNumericFormat *format, bool isMixedFormat );

/**
* Sets the text \a format to show in the widget.
*
* if \a isMixedFormat is TRUE then the widget will be set to indicate a "mixed format" setting.
*
* \see textFormat()
* \since QGIS 3.16
*/
void setTextFormat( const QgsTextFormat &format, bool isSet, bool isMixedFormat );

/**
* Sets the row \a height to show in the widget, or 0 for automatic height.
*
Expand Down Expand Up @@ -119,6 +146,20 @@ class GUI_EXPORT QgsTableEditorFormattingWidget : public QgsPanelWidget, private
*/
void numberFormatChanged();

/**
* Emitted whenever the text format shown in the widget is changed.
*
* \since QGIS 3.16
*/
void textFormatChanged();

/**
* Emitted whenever the apply text format option in the widget is changed.
*
* \since QGIS 3.16
*/
void hasTextFormatChanged();

/**
* Emitted whenever the row \a height shown in the widget is changed.
*/
Expand Down
105 changes: 105 additions & 0 deletions src/gui/tableeditor/qgstableeditorwidget.cpp
Expand Up @@ -325,6 +325,8 @@ void QgsTableEditorWidget::setTableContents( const QgsTableContents &contents )
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( HasTextFormat, col.hasTextFormat() );
item->setData( TextFormat, QVariant::fromValue( col.textFormat() ) );
if ( col.numericFormat() )
{
mNumericFormats.insert( item, col.numericFormat()->clone() );
Expand Down Expand Up @@ -365,6 +367,8 @@ QgsTableContents QgsTableEditorWidget::tableContents() const
cell.setContent( i->data( CellContent ) );
cell.setBackgroundColor( i->data( PresetBackgroundColorRole ).value< QColor >() );
cell.setForegroundColor( i->data( Qt::ForegroundRole ).value< QColor >() );
cell.setHasTextFormat( i->data( HasTextFormat ).toBool() );
cell.setTextFormat( i->data( TextFormat ).value< QgsTextFormat >() );

if ( mNumericFormats.value( i ) )
{
Expand Down Expand Up @@ -481,6 +485,29 @@ bool QgsTableEditorWidget::hasMixedSelectionNumericFormat()
return false;
}

bool QgsTableEditorWidget::hasMixedSelectionHasTextFormat()
{
bool res = false;
bool first = true;
const QModelIndexList selection = selectedIndexes();
for ( const QModelIndex &index : selection )
{
bool hasFormat = model()->data( index, HasTextFormat ).isValid() ? model()->data( index, HasTextFormat ).toBool() : false;
if ( first )
{
res = hasFormat;
first = false;
}
else if ( hasFormat == res )
continue;
else
{
return true;
}
}
return false;
}

QColor QgsTableEditorWidget::selectionForegroundColor()
{
QColor c;
Expand Down Expand Up @@ -527,6 +554,28 @@ QColor QgsTableEditorWidget::selectionBackgroundColor()
return c;
}

QgsTextFormat QgsTableEditorWidget::selectionTextFormat()
{
const QModelIndexList selection = selectedIndexes();
for ( const QModelIndex &index : selection )
{
if ( model()->data( index, TextFormat ).isValid() )
return model()->data( index, TextFormat ).value< QgsTextFormat >();
}
return QgsTextFormat();
}

bool QgsTableEditorWidget::selectionHasTextFormat()
{
const QModelIndexList selection = selectedIndexes();
for ( const QModelIndex &index : selection )
{
if ( model()->data( index, HasTextFormat ).isValid() )
return model()->data( index, HasTextFormat ).toBool();
}
return false;
}

double QgsTableEditorWidget::selectionRowHeight()
{
double height = 0;
Expand Down Expand Up @@ -908,6 +957,62 @@ void QgsTableEditorWidget::setSelectionBackgroundColor( const QColor &color )
emit tableChanged();
}

void QgsTableEditorWidget::setSelectionTextFormat( const QgsTextFormat &format )
{
const QModelIndexList selection = selectedIndexes();
bool changed = false;
mBlockSignals++;
for ( const QModelIndex &index : selection )
{
if ( index.row() == 0 && mIncludeHeader )
continue;

if ( QTableWidgetItem *i = item( index.row(), index.column() ) )
{
i->setData( TextFormat, QVariant::fromValue( format ) );
changed = true;
}
else
{
QTableWidgetItem *newItem = new QTableWidgetItem();
newItem->setData( TextFormat, QVariant::fromValue( format ) );
setItem( index.row(), index.column(), newItem );
changed = true;
}
}
mBlockSignals--;
if ( changed && !mBlockSignals )
emit tableChanged();
}

void QgsTableEditorWidget::setSelectionHasTextFormat( bool hasFormat )
{
const QModelIndexList selection = selectedIndexes();
bool changed = false;
mBlockSignals++;
for ( const QModelIndex &index : selection )
{
if ( index.row() == 0 && mIncludeHeader )
continue;

if ( QTableWidgetItem *i = item( index.row(), index.column() ) )
{
i->setData( HasTextFormat, hasFormat );
changed = true;
}
else
{
QTableWidgetItem *newItem = new QTableWidgetItem();
newItem->setData( HasTextFormat, hasFormat );
setItem( index.row(), index.column(), newItem );
changed = true;
}
}
mBlockSignals--;
if ( changed && !mBlockSignals )
emit tableChanged();
}

void QgsTableEditorWidget::setSelectionRowHeight( double height )
{
bool changed = false;
Expand Down

0 comments on commit b584f93

Please sign in to comment.