34#include "moc_qgstableeditordialog.cpp"
37 : QMainWindow( parent )
40 setWindowTitle( tr(
"Table Designer" ) );
42 setAttribute( Qt::WA_DeleteOnClose );
43 setDockOptions( dockOptions() | QMainWindow::GroupedDragging );
47 QGridLayout *viewLayout =
new QGridLayout();
48 viewLayout->setSpacing( 0 );
49 viewLayout->setContentsMargins( 0, 0, 0, 0 );
50 centralWidget()->layout()->setSpacing( 0 );
51 centralWidget()->layout()->setContentsMargins( 0, 0, 0, 0 );
54 mMessageBar->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed );
55 static_cast<QGridLayout *
>( centralWidget()->layout() )->addWidget( mMessageBar, 0, 0, 1, 1, Qt::AlignTop );
58 mTableWidget->setContentsMargins( 0, 0, 0, 0 );
59 viewLayout->addWidget( mTableWidget, 0, 0 );
60 mViewFrame->setLayout( viewLayout );
61 mViewFrame->setContentsMargins( 0, 0, 0, 0 );
63 mTableWidget->setFocus();
71 const int minDockWidth( fontMetrics().boundingRect( QStringLiteral(
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ) ).width() );
73 mPropertiesDock =
new QgsDockWidget( tr(
"Cell Contents" ),
this );
74 mPropertiesDock->setObjectName( QStringLiteral(
"FormattingDock" ) );
76 mPropertiesDock->setWidget( mPropertiesStack );
77 mPropertiesDock->setMinimumWidth( minDockWidth );
80 mFormattingWidget->setDockMode(
true );
81 mPropertiesStack->setMainPanel( mFormattingWidget );
83 mPropertiesDock->setFeatures( QDockWidget::NoDockWidgetFeatures );
92 mTableWidget->setSelectionTextFormat( mFormattingWidget->textFormat() );
96 mTableWidget->setSelectionNumericFormat( mFormattingWidget->numericFormat() );
102 mFormattingWidget->setBackgroundColor( mTableWidget->selectionBackgroundColor() );
103 mFormattingWidget->setNumericFormat( mTableWidget->selectionNumericFormat(), mTableWidget->hasMixedSelectionNumericFormat() );
104 mFormattingWidget->setRowHeight( mTableWidget->selectionRowHeight() );
105 mFormattingWidget->setColumnWidth( mTableWidget->selectionColumnWidth() );
106 mFormattingWidget->setTextFormat( mTableWidget->selectionTextFormat() );
107 mFormattingWidget->setHorizontalAlignment( mTableWidget->selectionHorizontalAlignment() );
108 mFormattingWidget->setVerticalAlignment( mTableWidget->selectionVerticalAlignment() );
109 mFormattingWidget->setCellProperty( mTableWidget->selectionCellProperty() );
111 updateActionsFromSelection();
113 mFormattingWidget->setEnabled( !mTableWidget->isHeaderCellSelected() );
115 updateActionsFromSelection();
117 addDockWidget( Qt::RightDockWidgetArea, mPropertiesDock );
119 mActionImportFromClipboard->setEnabled( !QApplication::clipboard()->text().isEmpty() );
120 connect( QApplication::clipboard(), &QClipboard::dataChanged,
this, [
this]() { mActionImportFromClipboard->setEnabled( !QApplication::clipboard()->text().isEmpty() ); } );
123 connect( mActionClose, &QAction::triggered,
this, &QMainWindow::close );
134 connect( mActionSelectAll, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::selectAll );
136 connect( mActionIncludeHeader, &QAction::toggled,
this, [
this](
bool checked ) {
137 mTableWidget->setIncludeTableHeader( checked );
144 const QByteArray state = settings.
value( QStringLiteral(
"LayoutDesigner/tableEditorState" ), QByteArray(),
QgsSettings::App ).toByteArray();
145 if ( !state.isEmpty() && !restoreState( state ) )
147 QgsDebugError( QStringLiteral(
"restore of table editor dialog UI state failed" ) );
160 return mLayer.data();
165 if (
layer != mLayer )
168 mFormattingWidget->setLayer(
layer );
179 if ( mTable ==
table )
186 setLayer( layout->reportContext().layer() );
194 const QList<double> rowHeights =
table->rowHeights();
195 for (
const double height : rowHeights )
201 const QList<double> columnWidths =
table->columnWidths();
202 QVariantList headers;
203 headers.reserve( columnWidths.size() );
204 for (
const double width : columnWidths )
207 headers << ( col <
table->headers().count() ?
table->headers().value( col ).heading() : QVariant() );
216 if ( QApplication::clipboard()->text().isEmpty() )
219 if ( QMessageBox::question(
this, tr(
"Import Content From Clipboard" ), tr(
"Importing content from clipboard will overwrite current table content. Are you sure?" ) ) != QMessageBox::Yes )
223 const QStringList lines = QApplication::clipboard()->text().split(
'\n' );
224 for (
const QString &line : lines )
226 if ( !line.isEmpty() )
229 const QStringList cells = line.split(
'\t' );
230 for (
const QString &text : cells )
239 if ( !contents.isEmpty() )
251 mBlockSignals =
true;
252 mTableWidget->setTableContents( contents );
253 mTableWidget->resizeRowsToContents();
254 mTableWidget->resizeColumnsToContents();
255 mBlockSignals =
false;
260 return mTableWidget->tableContents();
265 return mTableWidget->tableRowHeight( row );
270 return mTableWidget->tableColumnWidth( column );
275 mTableWidget->setTableRowHeight( row, height );
280 mTableWidget->setTableColumnWidth( column, width );
285 return mActionIncludeHeader->isChecked();
290 mActionIncludeHeader->setChecked( included );
295 return mTableWidget->tableHeaders();
300 mTableWidget->setTableHeaders( headers );
305 mFormattingWidget->registerExpressionContextGenerator( generator );
308void QgsTableEditorDialog::updateActionsFromSelection()
313 mActionInsertRowsAbove->setEnabled( rowCount > 0 );
314 mActionInsertRowsBelow->setEnabled( rowCount > 0 );
315 mActionDeleteRows->setEnabled( rowCount > 0 );
316 mActionSelectRow->setEnabled( rowCount > 0 );
319 mActionInsertRowsAbove->setText( tr(
"Rows Above" ) );
320 mActionInsertRowsBelow->setText( tr(
"Rows Below" ) );
321 mActionDeleteRows->setText( tr(
"Delete Rows" ) );
322 mActionSelectRow->setText( tr(
"Select Rows" ) );
324 else if ( rowCount == 1 )
326 mActionInsertRowsAbove->setText( tr(
"Row Above" ) );
327 mActionInsertRowsBelow->setText( tr(
"Row Below" ) );
328 mActionDeleteRows->setText( tr(
"Delete Row" ) );
329 mActionSelectRow->setText( tr(
"Select Row" ) );
333 mActionInsertRowsAbove->setText( tr(
"%n Row(s) Above",
nullptr, rowCount ) );
334 mActionInsertRowsBelow->setText( tr(
"%n Row(s) Below",
nullptr, rowCount ) );
335 mActionDeleteRows->setText( tr(
"Delete %n Row(s)",
nullptr, rowCount ) );
336 mActionSelectRow->setText( tr(
"Select %n Row(s)",
nullptr, rowCount ) );
339 mActionInsertColumnsBefore->setEnabled( columnCount > 0 );
340 mActionInsertColumnsAfter->setEnabled( columnCount > 0 );
341 mActionDeleteColumns->setEnabled( columnCount > 0 );
342 mActionSelectColumn->setEnabled( columnCount > 0 );
343 if ( columnCount == 0 )
345 mActionInsertColumnsBefore->setText( tr(
"Columns Before" ) );
346 mActionInsertColumnsAfter->setText( tr(
"Columns After" ) );
347 mActionDeleteColumns->setText( tr(
"Delete Columns" ) );
348 mActionSelectColumn->setText( tr(
"Select Columns" ) );
350 else if ( columnCount == 1 )
352 mActionInsertColumnsBefore->setText( tr(
"Column Before" ) );
353 mActionInsertColumnsAfter->setText( tr(
"Column After" ) );
354 mActionDeleteColumns->setText( tr(
"Delete Column" ) );
355 mActionSelectColumn->setText( tr(
"Select Column" ) );
359 mActionInsertColumnsBefore->setText( tr(
"%n Column(s) Before",
nullptr, columnCount ) );
360 mActionInsertColumnsAfter->setText( tr(
"%n Column(s) After",
nullptr, columnCount ) );
361 mActionDeleteColumns->setText( tr(
"Delete %n Column(s)",
nullptr, columnCount ) );
362 mActionSelectColumn->setText( tr(
"Select %n Column(s)",
nullptr, columnCount ) );
365 mActionMergeSelected->setEnabled( mTableWidget->canMergeSelection() );
366 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.