Skip to content

Commit

Permalink
[feature][layouts] Expose control over cell text alignment for
Browse files Browse the repository at this point in the history
individual table cells in manual text tables

Allows users to set the text horizontal and vertical alignments
on a cell by cell/column/row basis

Sponsored by City of Canning
  • Loading branch information
nyalldawson committed Jul 12, 2020
1 parent 1135e40 commit ff8d070
Show file tree
Hide file tree
Showing 19 changed files with 661 additions and 33 deletions.
Expand Up @@ -143,6 +143,10 @@ Replaces the headers in the table with a specified list of :py:class:`QgsLayoutT

virtual QgsTextFormat textFormatForCell( int row, int column ) const;

virtual Qt::Alignment horizontalAlignmentForCell( int row, int column ) const;

virtual Qt::Alignment verticalAlignmentForCell( int row, int column ) const;


};

Expand Down
18 changes: 18 additions & 0 deletions python/core/auto_generated/layout/qgslayouttable.sip.in
Expand Up @@ -752,6 +752,24 @@ Returns the text format to use for the header cell at the specified ``column``.

.. seealso:: :py:func:`textFormatForCell`

.. versionadded:: 3.16
%End

virtual Qt::Alignment horizontalAlignmentForCell( int row, int column ) const;
%Docstring
Returns the horizontal alignment to use for the cell at the specified ``row`` and ``column``.

.. seealso:: :py:func:`verticalAlignmentForCell`

.. versionadded:: 3.16
%End

virtual Qt::Alignment verticalAlignmentForCell( int row, int column ) const;
%Docstring
Returns the vertical alignment to use for the cell at the specified ``row`` and ``column``.

.. seealso:: :py:func:`horizontalAlignmentForCell`

.. versionadded:: 3.16
%End

Expand Down
44 changes: 44 additions & 0 deletions python/core/auto_generated/qgstablecell.sip.in
Expand Up @@ -142,6 +142,50 @@ Sets the numeric ``format`` used for numbers in the cell, or ``None`` if no spec
Ownership of ``format`` is transferred to the cell.

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

Qt::Alignment horizontalAlignment() const;
%Docstring
Returns the horizontal alignment for text in the cell.

.. seealso:: :py:func:`setHorizontalAlignment`

.. seealso:: :py:func:`verticalAlignment`

.. versionadded:: 3.16
%End

void setHorizontalAlignment( Qt::Alignment alignment );
%Docstring
Sets the horizontal ``alignment`` for text in the cell.

.. seealso:: :py:func:`horizontalAlignment`

.. seealso:: :py:func:`setVerticalAlignment`

.. versionadded:: 3.16
%End

Qt::Alignment verticalAlignment() const;
%Docstring
Returns the vertical alignment for text in the cell.

.. seealso:: :py:func:`setVerticalAlignment`

.. seealso:: :py:func:`horizontalAlignment`

.. versionadded:: 3.16
%End

void setVerticalAlignment( Qt::Alignment alignment );
%Docstring
Sets the vertical ``alignment`` for text in the cell.

.. seealso:: :py:func:`verticalAlignment`

.. seealso:: :py:func:`setHorizontalAlignment`

.. versionadded:: 3.16
%End

QVariantMap properties( const QgsReadWriteContext &context ) const;
Expand Down
69 changes: 69 additions & 0 deletions python/gui/auto_generated/tableeditor/qgstableeditorwidget.sip.in
Expand Up @@ -77,6 +77,11 @@ Returns ``True`` if the current selection has a mix of numeric formats.
%End

bool hasMixedSelectionHasTextFormat();
%Docstring
Returns ``True`` if the current selection has a mix of text format override state.

.. versionadded:: 3.16
%End

QColor selectionForegroundColor();
%Docstring
Expand All @@ -100,10 +105,41 @@ invalid color will be returned.
.. seealso:: :py:func:`setSelectionBackgroundColor`

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

Qt::Alignment selectionHorizontalAlignment();
%Docstring
Returns the horizontal alignment for the currently selected cells.

If the returned value contains both horizontal and vertical alignment flags, then
the selected cells have a mix of different horizontal alignments.

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

Qt::Alignment selectionVerticalAlignment();
%Docstring
Returns the horizontal alignment for the currently selected cells.

If the returned value contains both horizontal and vertical alignment flags, then
the selected cells have a mix of different vertical alignments.

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

QgsTextFormat selectionTextFormat();
%Docstring
Returns the text format for the currently selected cells.

.. versionadded:: 3.16
%End

bool selectionHasTextFormat();
%Docstring
Returns whether the currently selected cells have the text format override option set.

.. versionadded:: 3.16
%End

double selectionRowHeight();
%Docstring
Expand Down Expand Up @@ -260,10 +296,43 @@ Sets the background color for the currently selected cells.
.. seealso:: :py:func:`selectionBackgroundColor`

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

void setSelectionHorizontalAlignment( Qt::Alignment alignment );
%Docstring
Sets the horizontal alignment for the currently selected cells.

.. seealso:: :py:func:`selectionHorizontalAlignment`

.. seealso:: :py:func:`setSelectionVerticalAlignment`

.. versionadded:: 3.16
%End

void setSelectionVerticalAlignment( Qt::Alignment alignment );
%Docstring
Sets the vertical alignment for the currently selected cells.

.. seealso:: :py:func:`selectionVerticalAlignment`

.. seealso:: :py:func:`setSelectionHorizontalAlignment`

.. versionadded:: 3.16
%End

void setSelectionTextFormat( const QgsTextFormat &format );
%Docstring
Sets the text ``format`` for the selected cells.

.. versionadded:: 3.16
%End

void setSelectionHasTextFormat( bool hasFormat );
%Docstring
Sets whether the selected cells have the text format override option set.

.. versionadded:: 3.16
%End

void setSelectionRowHeight( double height );
%Docstring
Expand Down
16 changes: 16 additions & 0 deletions src/core/layout/qgslayoutitemmanualtable.cpp
Expand Up @@ -318,6 +318,22 @@ QgsTextFormat QgsLayoutItemManualTable::textFormatForCell( int row, int column )
return QgsLayoutTable::textFormatForCell( row, column );
}

Qt::Alignment QgsLayoutItemManualTable::horizontalAlignmentForCell( int row, int column ) const
{
if ( row < mContents.size() && column < mContents.at( row ).size() )
return mContents.value( row ).value( column ).horizontalAlignment();

return QgsLayoutTable::horizontalAlignmentForCell( row, column );
}

Qt::Alignment QgsLayoutItemManualTable::verticalAlignmentForCell( int row, int column ) const
{
if ( row < mContents.size() && column < mContents.at( row ).size() )
return mContents.value( row ).value( column ).verticalAlignment();

return QgsLayoutTable::verticalAlignmentForCell( row, column );
}

void QgsLayoutItemManualTable::refreshColumns()
{
// refresh columns
Expand Down
2 changes: 2 additions & 0 deletions src/core/layout/qgslayoutitemmanualtable.h
Expand Up @@ -141,6 +141,8 @@ class CORE_EXPORT QgsLayoutItemManualTable: public QgsLayoutTable
bool calculateMaxRowHeights() override;
QgsTextFormat textFormatForHeader( int column ) const override;
QgsTextFormat textFormatForCell( int row, int column ) const override;
Qt::Alignment horizontalAlignmentForCell( int row, int column ) const override;
Qt::Alignment verticalAlignmentForCell( int row, int column ) const override;

private:

Expand Down
14 changes: 12 additions & 2 deletions src/core/layout/qgslayouttable.cpp
Expand Up @@ -550,8 +550,8 @@ void QgsLayoutTable::render( QgsLayoutItemRenderContext &context, const QRectF &
textCell.top() * context.renderContext().scaleFactor(),
textCell.width() * context.renderContext().scaleFactor(),
textCell.height() * context.renderContext().scaleFactor() ), 0,
QgsTextRenderer::convertQtHAlignment( column.hAlignment() ), str, context.renderContext(), cellFormat, true,
QgsTextRenderer::convertQtVAlignment( column.vAlignment() ) );
QgsTextRenderer::convertQtHAlignment( horizontalAlignmentForCell( row, col ) ), str, context.renderContext(), cellFormat, true,
QgsTextRenderer::convertQtVAlignment( verticalAlignmentForCell( row, col ) ) );
}
p->restore();

Expand Down Expand Up @@ -1534,3 +1534,13 @@ QgsTextFormat QgsLayoutTable::textFormatForHeader( int ) const
return mHeaderTextFormat;
}

Qt::Alignment QgsLayoutTable::horizontalAlignmentForCell( int, int column ) const
{
return mColumns.value( column ).hAlignment();
}

Qt::Alignment QgsLayoutTable::verticalAlignmentForCell( int, int column ) const
{
return mColumns.value( column ).vAlignment();
}

16 changes: 16 additions & 0 deletions src/core/layout/qgslayouttable.h
Expand Up @@ -741,6 +741,22 @@ class CORE_EXPORT QgsLayoutTable: public QgsLayoutMultiFrame
*/
virtual QgsTextFormat textFormatForHeader( int column ) const;

/**
* Returns the horizontal alignment to use for the cell at the specified \a row and \a column.
*
* \see verticalAlignmentForCell()
* \since QGIS 3.16
*/
virtual Qt::Alignment horizontalAlignmentForCell( int row, int column ) const;

/**
* Returns the vertical alignment to use for the cell at the specified \a row and \a column.
*
* \see horizontalAlignmentForCell()
* \since QGIS 3.16
*/
virtual Qt::Alignment verticalAlignmentForCell( int row, int column ) const;

private:

QMap< CellStyleGroup, QString > mCellStyleNames;
Expand Down
30 changes: 30 additions & 0 deletions src/core/qgstablecell.cpp
Expand Up @@ -30,6 +30,8 @@ QgsTableCell::QgsTableCell( const QgsTableCell &other )
, mHasTextFormat( other.mHasTextFormat )
, mTextFormat( other.mTextFormat )
, mFormat( other.mFormat ? other.mFormat->clone() : nullptr )
, mHAlign( other.mHAlign )
, mVAlign( other.mVAlign )
{}

QgsTableCell::~QgsTableCell() = default;
Expand All @@ -42,6 +44,8 @@ QgsTableCell &QgsTableCell::operator=( const QgsTableCell &other )
mHasTextFormat = other.mHasTextFormat;
mTextFormat = other.mTextFormat;
mFormat.reset( other.mFormat ? other.mFormat->clone() : nullptr );
mHAlign = other.mHAlign;
mVAlign = other.mVAlign;
return *this;
}

Expand Down Expand Up @@ -73,6 +77,9 @@ QVariantMap QgsTableCell::properties( const QgsReadWriteContext &context ) const
textDoc.appendChild( textElem );
res.insert( QStringLiteral( "text_format" ), textDoc.toString() );

res.insert( QStringLiteral( "halign" ), static_cast< int >( mHAlign ) );
res.insert( QStringLiteral( "valign" ), static_cast< int >( mVAlign ) );

return res;
}

Expand Down Expand Up @@ -104,4 +111,27 @@ void QgsTableCell::setProperties( const QVariantMap &properties, const QgsReadWr
{
mFormat.reset();
}

mHAlign = static_cast< Qt::Alignment >( properties.value( QStringLiteral( "halign" ), Qt::AlignLeft ).toInt() );
mVAlign = static_cast< Qt::Alignment >( properties.value( QStringLiteral( "valign" ), Qt::AlignVCenter ).toInt() );
}

Qt::Alignment QgsTableCell::horizontalAlignment() const
{
return mHAlign;
}

void QgsTableCell::setHorizontalAlignment( Qt::Alignment alignment )
{
mHAlign = alignment;
}

Qt::Alignment QgsTableCell::verticalAlignment() const
{
return mVAlign;
}

void QgsTableCell::setVerticalAlignment( Qt::Alignment alignment )
{
mVAlign = alignment;
}
40 changes: 40 additions & 0 deletions src/core/qgstablecell.h
Expand Up @@ -149,6 +149,42 @@ class CORE_EXPORT QgsTableCell
*/
void setNumericFormat( QgsNumericFormat *format SIP_TRANSFER );

/**
* Returns the horizontal alignment for text in the cell.
*
* \see setHorizontalAlignment()
* \see verticalAlignment()
* \since QGIS 3.16
*/
Qt::Alignment horizontalAlignment() const;

/**
* Sets the horizontal \a alignment for text in the cell.
*
* \see horizontalAlignment()
* \see setVerticalAlignment()
* \since QGIS 3.16
*/
void setHorizontalAlignment( Qt::Alignment alignment );

/**
* Returns the vertical alignment for text in the cell.
*
* \see setVerticalAlignment()
* \see horizontalAlignment()
* \since QGIS 3.16
*/
Qt::Alignment verticalAlignment() const;

/**
* Sets the vertical \a alignment for text in the cell.
*
* \see verticalAlignment()
* \see setHorizontalAlignment()
* \since QGIS 3.16
*/
void setVerticalAlignment( Qt::Alignment alignment );

/**
* Returns the properties of the cell.
*
Expand Down Expand Up @@ -180,6 +216,10 @@ class CORE_EXPORT QgsTableCell
bool mHasTextFormat = false;
QgsTextFormat mTextFormat;
std::unique_ptr< QgsNumericFormat > mFormat;

Qt::Alignment mHAlign = Qt::AlignLeft;
Qt::Alignment mVAlign = Qt::AlignVCenter;

};

/**
Expand Down
5 changes: 5 additions & 0 deletions src/gui/tableeditor/qgstableeditordialog.cpp
Expand Up @@ -79,6 +79,9 @@ QgsTableEditorDialog::QgsTableEditorDialog( QWidget *parent )
connect( mFormattingWidget, &QgsTableEditorFormattingWidget::foregroundColorChanged, mTableWidget, &QgsTableEditorWidget::setSelectionForegroundColor );
connect( mFormattingWidget, &QgsTableEditorFormattingWidget::backgroundColorChanged, mTableWidget, &QgsTableEditorWidget::setSelectionBackgroundColor );

connect( mFormattingWidget, &QgsTableEditorFormattingWidget::horizontalAlignmentChanged, mTableWidget, &QgsTableEditorWidget::setSelectionHorizontalAlignment );
connect( mFormattingWidget, &QgsTableEditorFormattingWidget::verticalAlignmentChanged, mTableWidget, &QgsTableEditorWidget::setSelectionVerticalAlignment );

connect( mFormattingWidget, &QgsTableEditorFormattingWidget::textFormatChanged, this, [ = ]
{
mTableWidget->setSelectionTextFormat( mFormattingWidget->textFormat() );
Expand All @@ -103,6 +106,8 @@ QgsTableEditorDialog::QgsTableEditorDialog( QWidget *parent )
mFormattingWidget->setRowHeight( mTableWidget->selectionRowHeight() );
mFormattingWidget->setColumnWidth( mTableWidget->selectionColumnWidth() );
mFormattingWidget->setTextFormat( mTableWidget->selectionTextFormat(), mTableWidget->selectionHasTextFormat(), mTableWidget->hasMixedSelectionHasTextFormat() );
mFormattingWidget->setHorizontalAlignment( mTableWidget->selectionHorizontalAlignment() );
mFormattingWidget->setVerticalAlignment( mTableWidget->selectionVerticalAlignment() );

updateActionNamesFromSelection();

Expand Down

0 comments on commit ff8d070

Please sign in to comment.