Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannQDQ committed Nov 25, 2024
1 parent c61ed98 commit 84d72cc
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ Returns the (possibly ``None``) layer associated with the expression editor cont
Sets the ``layer`` to be used associated with the expression editor context.

.. versionadded:: 3.40
%End

QgsLayoutItemManualTable *table() const;
%Docstring
Returns the manual table associated with the editor.

.. versionadded:: 3.42
%End

void setTable( QgsLayoutItemManualTable *table );
%Docstring
Sets the ``table`` associated with the editor.

.. versionadded:: 3.42
%End

signals:
Expand Down
14 changes: 14 additions & 0 deletions python/gui/auto_generated/tableeditor/qgstableeditordialog.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ Returns the (possibly ``None``) layer associated with the expression editor cont
Sets the ``layer`` to be used associated with the expression editor context.

.. versionadded:: 3.40
%End

QgsLayoutItemManualTable *table() const;
%Docstring
Returns the manual table associated with the editor.

.. versionadded:: 3.42
%End

void setTable( QgsLayoutItemManualTable *table );
%Docstring
Sets the ``table`` associated with the editor.

.. versionadded:: 3.42
%End

signals:
Expand Down
101 changes: 41 additions & 60 deletions src/gui/layout/qgslayoutmanualtablewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include "qgsguiutils.h"
#include "qgslayouttablebackgroundcolorsdialog.h"


QPointer< QgsTableEditorDialog > QgsLayoutManualTableWidget::sEditorDialog = nullptr;

QgsLayoutManualTableWidget::QgsLayoutManualTableWidget( QgsLayoutFrame *frame )
: QgsLayoutItemBaseWidget( nullptr, frame ? qobject_cast< QgsLayoutItemManualTable* >( frame->multiFrame() ) : nullptr )
, mTable( frame ? qobject_cast< QgsLayoutItemManualTable* >( frame->multiFrame() ) : nullptr )
Expand Down Expand Up @@ -151,9 +154,9 @@ bool QgsLayoutManualTableWidget::setNewItem( QgsLayoutItem *item )
{
disconnect( mTable, &QgsLayoutObject::changed, this, &QgsLayoutManualTableWidget::updateGuiElements );
}
if ( mEditorDialog )
if ( sEditorDialog && sEditorDialog->table() != mTable )
{
mEditorDialog->close();
sEditorDialog->close();
}

mTable = qobject_cast< QgsLayoutItemManualTable * >( multiFrame );
Expand All @@ -170,56 +173,51 @@ bool QgsLayoutManualTableWidget::setNewItem( QgsLayoutItem *item )
return true;
}

QgsTableEditorDialog *QgsLayoutManualTableWidget::openTableDesigner( QgsLayoutFrame *frame, QWidget *parent )
void QgsLayoutManualTableWidget::openTableDesigner( QgsLayoutFrame *frame, QWidget *parent )
{
QgsLayoutItemManualTable *table = frame ? qobject_cast<QgsLayoutItemManualTable *> ( frame->multiFrame() ) : nullptr;
if ( !table )
{
return nullptr;
return;
}
QgsTableEditorDialog *editorDialog = new QgsTableEditorDialog( parent );
if ( QgsLayout *layout = table->layout() )

if ( sEditorDialog )
{
editorDialog->setLayer( layout->reportContext().layer() );
// Dialog already exists, and linked to the same table => display it
if ( sEditorDialog->table() == table )
{
// the unholy quadfecta
sEditorDialog->show();
sEditorDialog->raise();
if ( parent )
{
sEditorDialog->setWindowState( parent->windowState() & ~Qt::WindowMinimized );
}
sEditorDialog->activateWindow();
return;
}
// Otherwise, close (and delete) the dialog
sEditorDialog->close();
}
editorDialog->registerExpressionContextGenerator( table );
connect( frame, &QgsLayoutFrame::destroyed, editorDialog, &QMainWindow::close );
auto updateName = [editorDialog, frame] {editorDialog->setWindowTitle( QString( "%1 - %2 " ).arg( editorDialog->tr( "Table Designer" ) ).arg( frame->displayName() ) );};
connect( frame, &QgsLayoutFrame::changed, editorDialog, updateName );
updateName();

if ( parent )
connect( parent, &QWidget::destroyed, editorDialog, &QMainWindow::close );
sEditorDialog = new QgsTableEditorDialog( parent );
sEditorDialog->setTable( table );

editorDialog->setIncludeTableHeader( table->includeTableHeader() );
editorDialog->setTableContents( table->tableContents() );
connect( frame, &QgsLayoutFrame::destroyed, sEditorDialog, &QMainWindow::close );
auto updateName = [frame] {sEditorDialog->setWindowTitle( QString( "%1 - %2 " ).arg( sEditorDialog->tr( "Table Designer" ) ).arg( frame->displayName() ) );};
connect( frame, &QgsLayoutFrame::changed, sEditorDialog, updateName );
updateName();

int row = 0;
const QList< double > rowHeights = table->rowHeights();
for ( const double height : rowHeights )
{
editorDialog->setTableRowHeight( row, height );
row++;
}
int col = 0;
const QList< double > columnWidths = table->columnWidths();
QVariantList headers;
headers.reserve( columnWidths.size() );
for ( const double width : columnWidths )
{
editorDialog->setTableColumnWidth( col, width );
headers << ( col < table->headers().count() ? table->headers().value( col ).heading() : QVariant() );
col++;
}
editorDialog->setTableHeaders( headers );
if ( parent )
connect( parent, &QWidget::destroyed, sEditorDialog, &QMainWindow::close );

connect( editorDialog, &QgsTableEditorDialog::tableChanged, table, [ = ]
connect( sEditorDialog, &QgsTableEditorDialog::tableChanged, table, [ = ]
{
table->beginCommand( tr( "Change Table Contents" ) );
table->setTableContents( editorDialog->tableContents() );
table->setTableContents( sEditorDialog->tableContents() );

const QVariantList headerText = editorDialog->tableHeaders();
if ( editorDialog->includeTableHeader() )
const QVariantList headerText = sEditorDialog->tableHeaders();
if ( sEditorDialog->includeTableHeader() )
{
QgsLayoutTableColumns headers;
for ( const QVariant &h : headerText )
Expand All @@ -234,7 +232,7 @@ QgsTableEditorDialog *QgsLayoutManualTableWidget::openTableDesigner( QgsLayoutFr
rowHeights.reserve( rowCount );
for ( int row = 0; row < rowCount; ++row )
{
rowHeights << editorDialog->tableRowHeight( row );
rowHeights << sEditorDialog->tableRowHeight( row );
}
table->setRowHeights( rowHeights );

Expand All @@ -245,43 +243,26 @@ QgsTableEditorDialog *QgsLayoutManualTableWidget::openTableDesigner( QgsLayoutFr
columnWidths.reserve( columnCount );
for ( int col = 0; col < columnCount; ++col )
{
columnWidths << editorDialog->tableColumnWidth( col );
columnWidths << sEditorDialog->tableColumnWidth( col );
}
table->setColumnWidths( columnWidths );
}

table->endCommand();
} );

connect( editorDialog, &QgsTableEditorDialog::includeHeaderChanged, table, [ = ]( bool included )
connect( sEditorDialog, &QgsTableEditorDialog::includeHeaderChanged, table, [ = ]( bool included )
{
table->beginCommand( tr( "Change Table Header" ) );
table->setIncludeTableHeader( included );
table->endCommand();
} );
editorDialog->show();
return editorDialog;
sEditorDialog->show();
}

void QgsLayoutManualTableWidget::setTableContents()
{
if ( !mTable )
{
return;
}

if ( mEditorDialog )
{
// the unholy quadfecta
mEditorDialog->show();
mEditorDialog->raise();
mEditorDialog->setWindowState( windowState() & ~Qt::WindowMinimized );
mEditorDialog->activateWindow();
}
else
{
mEditorDialog = openTableDesigner( mFrame, this );
}
openTableDesigner( mFrame, this );
}

void QgsLayoutManualTableWidget::mMarginSpinBox_valueChanged( double d )
Expand Down
6 changes: 3 additions & 3 deletions src/gui/layout/qgslayoutmanualtablewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class GUI_EXPORT QgsLayoutManualTableWidget: public QgsLayoutItemBaseWidget, pub
QgsExpressionContext createExpressionContext() const override;

//! Creates and open the table editor dialog
static QgsTableEditorDialog *openTableDesigner( QgsLayoutFrame *frame, QWidget *parent = nullptr );
static void openTableDesigner( QgsLayoutFrame *frame, QWidget *parent = nullptr );


protected:
Expand All @@ -60,11 +60,11 @@ class GUI_EXPORT QgsLayoutManualTableWidget: public QgsLayoutItemBaseWidget, pub
QPointer< QgsLayoutFrame > mFrame;
QgsLayoutItemPropertiesWidget *mItemPropertiesWidget = nullptr;

QPointer< QgsTableEditorDialog > mEditorDialog;

//! Blocks / unblocks the signals of all GUI elements
void blockAllSignals( bool b );

static QPointer< QgsTableEditorDialog > sEditorDialog;

private slots:

void setTableContents();
Expand Down
48 changes: 47 additions & 1 deletion src/gui/tableeditor/qgstableeditordialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@
#include "qgspanelwidgetstack.h"
#include "qgstableeditorformattingwidget.h"
#include "qgssettings.h"
#include "qgsmaplayer.h"
#include "qgsvectorlayer.h"
#include "qgslayout.h"
#include "qgslayoutreportcontext.h"
#include "qgslayoutitemmanualtable.h"
#include "qgslayouttablecolumn.h"

#include <QClipboard>
#include <QMessageBox>
Expand Down Expand Up @@ -168,6 +172,48 @@ void QgsTableEditorDialog::setLayer( QgsMapLayer *layer )
}
}

QgsLayoutItemManualTable *QgsTableEditorDialog::table() const
{
return mTable;
}

void QgsTableEditorDialog::setTable( QgsLayoutItemManualTable *table )
{
if ( mTable == table )
return;

mTable = table;

if ( QgsLayout *layout = table->layout() )
{
setLayer( layout->reportContext().layer() );
}
registerExpressionContextGenerator( table );

setIncludeTableHeader( table->includeTableHeader() );
setTableContents( table->tableContents() );

int row = 0;
const QList< double > rowHeights = table->rowHeights();
for ( const double height : rowHeights )
{
setTableRowHeight( row, height );
row++;
}
int col = 0;
const QList< double > columnWidths = table->columnWidths();
QVariantList headers;
headers.reserve( columnWidths.size() );
for ( const double width : columnWidths )
{
setTableColumnWidth( col, width );
headers << ( col < table->headers().count() ? table->headers().value( col ).heading() : QVariant() );
col++;
}
setTableHeaders( headers );
}



bool QgsTableEditorDialog::setTableContentsFromClipboard()
{
Expand Down
14 changes: 14 additions & 0 deletions src/gui/tableeditor/qgstableeditordialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class QgsPanelWidgetStack;
class QgsTableEditorFormattingWidget;
class QgsExpressionContextGenerator;
class QgsMapLayer;
class QgsLayoutItemManualTable;

/**
* \ingroup gui
Expand Down Expand Up @@ -156,6 +157,18 @@ class GUI_EXPORT QgsTableEditorDialog : public QMainWindow, private Ui::QgsTable
*/
void setLayer( QgsMapLayer *layer );

/**
* Returns the manual table associated with the editor.
* \since QGIS 3.42
*/
QgsLayoutItemManualTable *table() const;

/**
* Sets the \a table associated with the editor.
* \since QGIS 3.42
*/
void setTable( QgsLayoutItemManualTable *table );

signals:

/**
Expand All @@ -180,6 +193,7 @@ class GUI_EXPORT QgsTableEditorDialog : public QMainWindow, private Ui::QgsTable
QgsTableEditorFormattingWidget *mFormattingWidget = nullptr;
bool mBlockSignals = false;
QPointer< QgsMapLayer > mLayer;
QgsLayoutItemManualTable *mTable = nullptr;

void updateActionsFromSelection();
};
Expand Down

0 comments on commit 84d72cc

Please sign in to comment.