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 );
95 mTableWidget->setSelectionTextFormat( mFormattingWidget->textFormat() );
99 mTableWidget->setSelectionNumericFormat( mFormattingWidget->numericFormat() );
105 mFormattingWidget->setBackgroundColor( mTableWidget->selectionBackgroundColor() );
106 mFormattingWidget->setNumericFormat( mTableWidget->selectionNumericFormat(), mTableWidget->hasMixedSelectionNumericFormat() );
107 mFormattingWidget->setRowHeight( mTableWidget->selectionRowHeight() );
108 mFormattingWidget->setColumnWidth( mTableWidget->selectionColumnWidth() );
109 mFormattingWidget->setTextFormat( mTableWidget->selectionTextFormat() );
110 mFormattingWidget->setHorizontalAlignment( mTableWidget->selectionHorizontalAlignment() );
111 mFormattingWidget->setVerticalAlignment( mTableWidget->selectionVerticalAlignment() );
112 mFormattingWidget->setCellProperty( mTableWidget->selectionCellProperty() );
114 updateActionsFromSelection();
116 mFormattingWidget->setEnabled( !mTableWidget->isHeaderCellSelected() );
118 updateActionsFromSelection();
120 addDockWidget( Qt::RightDockWidgetArea, mPropertiesDock );
122 mActionImportFromClipboard->setEnabled( !QApplication::clipboard()->text().isEmpty() );
123 connect( QApplication::clipboard(), &QClipboard::dataChanged,
this, [
this]() { mActionImportFromClipboard->setEnabled( !QApplication::clipboard()->text().isEmpty() ); } );
126 connect( mActionClose, &QAction::triggered,
this, &QMainWindow::close );
137 connect( mActionSelectAll, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::selectAll );
139 connect( mActionIncludeHeader, &QAction::toggled,
this, [
this](
bool checked ) {
140 mTableWidget->setIncludeTableHeader( checked );
147 const QByteArray state = settings.
value( u
"LayoutDesigner/tableEditorState"_s, QByteArray(),
QgsSettings::App ).toByteArray();
148 if ( !state.isEmpty() && !restoreState( state ) )
150 QgsDebugError( u
"restore of table editor dialog UI state failed"_s );
163 return mLayer.data();
168 if (
layer != mLayer )
171 mFormattingWidget->setLayer(
layer );
182 if ( mTable ==
table )
189 setLayer( layout->reportContext().layer() );
197 const QList<double> rowHeights =
table->rowHeights();
198 for (
const double height : rowHeights )
204 const QList<double> columnWidths =
table->columnWidths();
205 QVariantList headers;
206 headers.reserve( columnWidths.size() );
207 for (
const double width : columnWidths )
210 headers << ( col <
table->headers().count() ?
table->headers().value( col ).heading() : QVariant() );
219 if ( QApplication::clipboard()->text().isEmpty() )
222 if ( QMessageBox::question(
this, tr(
"Import Content From Clipboard" ), tr(
"Importing content from clipboard will overwrite current table content. Are you sure?" ) ) != QMessageBox::Yes )
226 const QStringList lines = QApplication::clipboard()->text().split(
'\n' );
227 for (
const QString &line : lines )
229 if ( !line.isEmpty() )
232 const QStringList cells = line.split(
'\t' );
233 for (
const QString &text : cells )
242 if ( !contents.isEmpty() )
254 mBlockSignals =
true;
255 mTableWidget->setTableContents( contents );
256 mTableWidget->resizeRowsToContents();
257 mTableWidget->resizeColumnsToContents();
258 mBlockSignals =
false;
263 return mTableWidget->tableContents();
268 return mTableWidget->tableRowHeight( row );
273 return mTableWidget->tableColumnWidth( column );
278 mTableWidget->setTableRowHeight( row, height );
283 mTableWidget->setTableColumnWidth( column, width );
288 return mActionIncludeHeader->isChecked();
293 mActionIncludeHeader->setChecked( included );
298 return mTableWidget->tableHeaders();
303 mTableWidget->setTableHeaders( headers );
308 mFormattingWidget->registerExpressionContextGenerator( generator );
311void QgsTableEditorDialog::updateActionsFromSelection()
316 mActionInsertRowsAbove->setEnabled( rowCount > 0 );
317 mActionInsertRowsBelow->setEnabled( rowCount > 0 );
318 mActionDeleteRows->setEnabled( rowCount > 0 );
319 mActionSelectRow->setEnabled( rowCount > 0 );
322 mActionInsertRowsAbove->setText( tr(
"Rows Above" ) );
323 mActionInsertRowsBelow->setText( tr(
"Rows Below" ) );
324 mActionDeleteRows->setText( tr(
"Delete Rows" ) );
325 mActionSelectRow->setText( tr(
"Select Rows" ) );
327 else if ( rowCount == 1 )
329 mActionInsertRowsAbove->setText( tr(
"Row Above" ) );
330 mActionInsertRowsBelow->setText( tr(
"Row Below" ) );
331 mActionDeleteRows->setText( tr(
"Delete Row" ) );
332 mActionSelectRow->setText( tr(
"Select Row" ) );
336 mActionInsertRowsAbove->setText( tr(
"%n Row(s) Above",
nullptr, rowCount ) );
337 mActionInsertRowsBelow->setText( tr(
"%n Row(s) Below",
nullptr, rowCount ) );
338 mActionDeleteRows->setText( tr(
"Delete %n Row(s)",
nullptr, rowCount ) );
339 mActionSelectRow->setText( tr(
"Select %n Row(s)",
nullptr, rowCount ) );
342 mActionInsertColumnsBefore->setEnabled( columnCount > 0 );
343 mActionInsertColumnsAfter->setEnabled( columnCount > 0 );
344 mActionDeleteColumns->setEnabled( columnCount > 0 );
345 mActionSelectColumn->setEnabled( columnCount > 0 );
346 if ( columnCount == 0 )
348 mActionInsertColumnsBefore->setText( tr(
"Columns Before" ) );
349 mActionInsertColumnsAfter->setText( tr(
"Columns After" ) );
350 mActionDeleteColumns->setText( tr(
"Delete Columns" ) );
351 mActionSelectColumn->setText( tr(
"Select Columns" ) );
353 else if ( columnCount == 1 )
355 mActionInsertColumnsBefore->setText( tr(
"Column Before" ) );
356 mActionInsertColumnsAfter->setText( tr(
"Column After" ) );
357 mActionDeleteColumns->setText( tr(
"Delete Column" ) );
358 mActionSelectColumn->setText( tr(
"Select Column" ) );
362 mActionInsertColumnsBefore->setText( tr(
"%n Column(s) Before",
nullptr, columnCount ) );
363 mActionInsertColumnsAfter->setText( tr(
"%n Column(s) After",
nullptr, columnCount ) );
364 mActionDeleteColumns->setText( tr(
"Delete %n Column(s)",
nullptr, columnCount ) );
365 mActionSelectColumn->setText( tr(
"Select %n Column(s)",
nullptr, columnCount ) );
368 mActionMergeSelected->setEnabled( mTableWidget->canMergeSelection() );
369 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.