35#include "moc_qgstableeditordialog.cpp"
37using namespace Qt::StringLiterals;
40 : QMainWindow( parent )
43 setWindowTitle( tr(
"Table Designer" ) );
45 setAttribute( Qt::WA_DeleteOnClose );
46 setDockOptions( dockOptions() | QMainWindow::GroupedDragging );
50 QGridLayout *viewLayout =
new QGridLayout();
51 viewLayout->setSpacing( 0 );
52 viewLayout->setContentsMargins( 0, 0, 0, 0 );
53 centralWidget()->layout()->setSpacing( 0 );
54 centralWidget()->layout()->setContentsMargins( 0, 0, 0, 0 );
57 mMessageBar->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed );
58 static_cast<QGridLayout *
>( centralWidget()->layout() )->addWidget( mMessageBar, 0, 0, 1, 1, Qt::AlignTop );
61 mTableWidget->setContentsMargins( 0, 0, 0, 0 );
62 viewLayout->addWidget( mTableWidget, 0, 0 );
63 mViewFrame->setLayout( viewLayout );
64 mViewFrame->setContentsMargins( 0, 0, 0, 0 );
66 mTableWidget->setFocus();
74 const int minDockWidth( fontMetrics().boundingRect( u
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"_s ).width() );
76 mPropertiesDock =
new QgsDockWidget( tr(
"Cell Contents" ),
this );
77 mPropertiesDock->setObjectName( u
"FormattingDock"_s );
79 mPropertiesDock->setWidget( mPropertiesStack );
80 mPropertiesDock->setMinimumWidth( minDockWidth );
83 mFormattingWidget->setDockMode(
true );
84 mPropertiesStack->setMainPanel( mFormattingWidget );
86 mPropertiesDock->setFeatures( QDockWidget::NoDockWidgetFeatures );
101 mFormattingWidget->setBackgroundColor( mTableWidget->selectionBackgroundColor() );
102 mFormattingWidget->setNumericFormat( mTableWidget->selectionNumericFormat(), mTableWidget->hasMixedSelectionNumericFormat() );
103 mFormattingWidget->setRowHeight( mTableWidget->selectionRowHeight() );
104 mFormattingWidget->setColumnWidth( mTableWidget->selectionColumnWidth() );
105 mFormattingWidget->setTextFormat( mTableWidget->selectionTextFormat() );
106 mFormattingWidget->setHorizontalAlignment( mTableWidget->selectionHorizontalAlignment() );
107 mFormattingWidget->setVerticalAlignment( mTableWidget->selectionVerticalAlignment() );
108 mFormattingWidget->setCellProperty( mTableWidget->selectionCellProperty() );
110 updateActionsFromSelection();
112 mFormattingWidget->setEnabled( !mTableWidget->isHeaderCellSelected() );
114 updateActionsFromSelection();
116 addDockWidget( Qt::RightDockWidgetArea, mPropertiesDock );
118 mActionImportFromClipboard->setEnabled( !QApplication::clipboard()->text().isEmpty() );
119 connect( QApplication::clipboard(), &QClipboard::dataChanged,
this, [
this]() { mActionImportFromClipboard->setEnabled( !QApplication::clipboard()->text().isEmpty() ); } );
122 connect( mActionClose, &QAction::triggered,
this, &QMainWindow::close );
133 connect( mActionSelectAll, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::selectAll );
135 connect( mActionIncludeHeader, &QAction::toggled,
this, [
this](
bool checked ) {
136 mTableWidget->setIncludeTableHeader( checked );
143 const QByteArray state = settings.
value( u
"LayoutDesigner/tableEditorState"_s, QByteArray(),
QgsSettings::App ).toByteArray();
144 if ( !state.isEmpty() && !restoreState( state ) )
146 QgsDebugError( u
"restore of table editor dialog UI state failed"_s );
159 return mLayer.data();
164 if (
layer != mLayer )
167 mFormattingWidget->setLayer(
layer );
178 if ( mTable ==
table )
185 setLayer( layout->reportContext().layer() );
193 const QList<double> rowHeights =
table->rowHeights();
194 for (
const double height : rowHeights )
200 const QList<double> columnWidths =
table->columnWidths();
201 QVariantList headers;
202 headers.reserve( columnWidths.size() );
203 for (
const double width : columnWidths )
206 headers << ( col <
table->headers().count() ?
table->headers().value( col ).heading() : QVariant() );
215 if ( QApplication::clipboard()->text().isEmpty() )
218 if ( QMessageBox::question(
this, tr(
"Import Content From Clipboard" ), tr(
"Importing content from clipboard will overwrite current table content. Are you sure?" ) ) != QMessageBox::Yes )
222 const QStringList lines = QApplication::clipboard()->text().split(
'\n' );
223 for (
const QString &line : lines )
225 if ( !line.isEmpty() )
228 const QStringList cells = line.split(
'\t' );
229 for (
const QString &text : cells )
238 if ( !contents.isEmpty() )
250 mBlockSignals =
true;
251 mTableWidget->setTableContents( contents );
252 mTableWidget->resizeRowsToContents();
253 mTableWidget->resizeColumnsToContents();
254 mBlockSignals =
false;
259 return mTableWidget->tableContents();
264 return mTableWidget->tableRowHeight( row );
269 return mTableWidget->tableColumnWidth( column );
274 mTableWidget->setTableRowHeight( row, height );
279 mTableWidget->setTableColumnWidth( column, width );
284 return mActionIncludeHeader->isChecked();
289 mActionIncludeHeader->setChecked( included );
294 return mTableWidget->tableHeaders();
299 mTableWidget->setTableHeaders( headers );
304 mFormattingWidget->registerExpressionContextGenerator( generator );
307void QgsTableEditorDialog::updateActionsFromSelection()
312 mActionInsertRowsAbove->setEnabled( rowCount > 0 );
313 mActionInsertRowsBelow->setEnabled( rowCount > 0 );
314 mActionDeleteRows->setEnabled( rowCount > 0 );
315 mActionSelectRow->setEnabled( rowCount > 0 );
318 mActionInsertRowsAbove->setText( tr(
"Rows Above" ) );
319 mActionInsertRowsBelow->setText( tr(
"Rows Below" ) );
320 mActionDeleteRows->setText( tr(
"Delete Rows" ) );
321 mActionSelectRow->setText( tr(
"Select Rows" ) );
323 else if ( rowCount == 1 )
325 mActionInsertRowsAbove->setText( tr(
"Row Above" ) );
326 mActionInsertRowsBelow->setText( tr(
"Row Below" ) );
327 mActionDeleteRows->setText( tr(
"Delete Row" ) );
328 mActionSelectRow->setText( tr(
"Select Row" ) );
332 mActionInsertRowsAbove->setText( tr(
"%n Row(s) Above",
nullptr, rowCount ) );
333 mActionInsertRowsBelow->setText( tr(
"%n Row(s) Below",
nullptr, rowCount ) );
334 mActionDeleteRows->setText( tr(
"Delete %n Row(s)",
nullptr, rowCount ) );
335 mActionSelectRow->setText( tr(
"Select %n Row(s)",
nullptr, rowCount ) );
338 mActionInsertColumnsBefore->setEnabled( columnCount > 0 );
339 mActionInsertColumnsAfter->setEnabled( columnCount > 0 );
340 mActionDeleteColumns->setEnabled( columnCount > 0 );
341 mActionSelectColumn->setEnabled( columnCount > 0 );
342 if ( columnCount == 0 )
344 mActionInsertColumnsBefore->setText( tr(
"Columns Before" ) );
345 mActionInsertColumnsAfter->setText( tr(
"Columns After" ) );
346 mActionDeleteColumns->setText( tr(
"Delete Columns" ) );
347 mActionSelectColumn->setText( tr(
"Select Columns" ) );
349 else if ( columnCount == 1 )
351 mActionInsertColumnsBefore->setText( tr(
"Column Before" ) );
352 mActionInsertColumnsAfter->setText( tr(
"Column After" ) );
353 mActionDeleteColumns->setText( tr(
"Delete Column" ) );
354 mActionSelectColumn->setText( tr(
"Select Column" ) );
358 mActionInsertColumnsBefore->setText( tr(
"%n Column(s) Before",
nullptr, columnCount ) );
359 mActionInsertColumnsAfter->setText( tr(
"%n Column(s) After",
nullptr, columnCount ) );
360 mActionDeleteColumns->setText( tr(
"Delete %n Column(s)",
nullptr, columnCount ) );
361 mActionSelectColumn->setText( tr(
"Select %n Column(s)",
nullptr, columnCount ) );
364 mActionMergeSelected->setEnabled( mTableWidget->canMergeSelection() );
365 mActionSplitSelected->setEnabled( mTableWidget->canSplitSelection() );
Abstract interface for generating an expression context.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
A layout table subclass that displays manually entered (and formatted) content.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Base class for all map layer types.
A bar for displaying non-blocking messages to the user.
Stores settings for use within QGIS.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
Encapsulates the contents and formatting of a single table cell.
void setIncludeTableHeader(bool included)
Sets whether the table includes a header row.
void setTable(QgsLayoutItemManualTable *table)
Sets the table associated with the editor.
QgsTableContents tableContents() const
Returns the current contents of the editor widget table.
void setTableHeaders(const QVariantList &headers)
Sets the table headers.
bool includeTableHeader() const
Returns true if the table includes a header row.
QgsTableEditorDialog(QWidget *parent=nullptr)
Constructor for QgsTableEditorDialog with the specified parent widget.
void includeHeaderChanged(bool included)
Emitted whenever the "include table header" setting is changed.
void setTableColumnWidth(int column, double width)
Sets the configured column width for the specified column.
void tableChanged()
Emitted whenever the table contents are changed.
void setTableRowHeight(int row, double height)
Sets the configured row height for the specified row.
void registerExpressionContextGenerator(QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
bool setTableContentsFromClipboard()
Parses the clipboard text into contents to show in the editor widget.
QVariantList tableHeaders() const
Returns the table header values.
void closeEvent(QCloseEvent *) override
QgsMapLayer * layer() const
Returns the (possibly nullptr) layer associated with the expression editor context.
double tableRowHeight(int row)
Returns the configured row height for the specified row, or 0 if an automatic height should be used f...
double tableColumnWidth(int column)
Returns the configured column width for the specified column, or 0 if an automatic width should be us...
QgsLayoutItemManualTable * table() const
Returns the manual table associated with the editor.
void setLayer(QgsMapLayer *layer)
Sets the layer to be used associated with the expression editor context.
void setTableContents(const QgsTableContents &contents)
Sets the contents to show in the editor widget.
#define QgsDebugError(str)
QVector< QgsTableRow > QgsTableContents
A set of table rows.
QVector< QgsTableCell > QgsTableRow
A row of table cells.