QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgstableeditordialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstableeditordialog.cpp
3 ------------------------
4 begin : January 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgstableeditordialog.cpp"
19#include "qgsmessagebar.h"
20#include "qgsgui.h"
21#include "qgsdockwidget.h"
22#include "qgspanelwidgetstack.h"
24#include "qgssettings.h"
25#include "qgsmaplayer.h"
26
27#include <QClipboard>
28#include <QMessageBox>
29
31 : QMainWindow( parent )
32{
33 setupUi( this );
34 setWindowTitle( tr( "Table Designer" ) );
35
36 setAttribute( Qt::WA_DeleteOnClose );
37 setDockOptions( dockOptions() | QMainWindow::GroupedDragging );
38
40
41 QGridLayout *viewLayout = new QGridLayout();
42 viewLayout->setSpacing( 0 );
43 viewLayout->setContentsMargins( 0, 0, 0, 0 );
44 centralWidget()->layout()->setSpacing( 0 );
45 centralWidget()->layout()->setContentsMargins( 0, 0, 0, 0 );
46
47 mMessageBar = new QgsMessageBar( centralWidget() );
48 mMessageBar->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed );
49 static_cast< QGridLayout * >( centralWidget()->layout() )->addWidget( mMessageBar, 0, 0, 1, 1, Qt::AlignTop );
50
51 mTableWidget = new QgsTableEditorWidget();
52 mTableWidget->setContentsMargins( 0, 0, 0, 0 );
53 viewLayout->addWidget( mTableWidget, 0, 0 );
54 mViewFrame->setLayout( viewLayout );
55 mViewFrame->setContentsMargins( 0, 0, 0, 0 );
56
57 mTableWidget->setFocus();
58 mTableWidget->setTableContents( QgsTableContents() << ( QgsTableRow() << QgsTableCell() ) );
59
60 connect( mTableWidget, &QgsTableEditorWidget::tableChanged, this, [ = ]
61 {
62 if ( !mBlockSignals )
63 emit tableChanged();
64 } );
65
66 const int minDockWidth( fontMetrics().boundingRect( QStringLiteral( "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ) ).width() );
67
68 mPropertiesDock = new QgsDockWidget( tr( "Cell Contents" ), this );
69 mPropertiesDock->setObjectName( QStringLiteral( "FormattingDock" ) );
70 mPropertiesStack = new QgsPanelWidgetStack();
71 mPropertiesDock->setWidget( mPropertiesStack );
72 mPropertiesDock->setMinimumWidth( minDockWidth );
73
74 mFormattingWidget = new QgsTableEditorFormattingWidget();
75 mFormattingWidget->setDockMode( true );
76 mPropertiesStack->setMainPanel( mFormattingWidget );
77
78 mPropertiesDock->setFeatures( QDockWidget::NoDockWidgetFeatures );
79
81
85
86 connect( mFormattingWidget, &QgsTableEditorFormattingWidget::textFormatChanged, this, [ = ]
87 {
88 mTableWidget->setSelectionTextFormat( mFormattingWidget->textFormat() );
89 } );
90
91 connect( mFormattingWidget, &QgsTableEditorFormattingWidget::numberFormatChanged, this, [ = ]
92 {
93 mTableWidget->setSelectionNumericFormat( mFormattingWidget->numericFormat() );
94 } );
97
98 connect( mTableWidget, &QgsTableEditorWidget::activeCellChanged, this, [ = ]
99 {
100 mFormattingWidget->setBackgroundColor( mTableWidget->selectionBackgroundColor() );
101 mFormattingWidget->setNumericFormat( mTableWidget->selectionNumericFormat(), mTableWidget->hasMixedSelectionNumericFormat() );
102 mFormattingWidget->setRowHeight( mTableWidget->selectionRowHeight() );
103 mFormattingWidget->setColumnWidth( mTableWidget->selectionColumnWidth() );
104 mFormattingWidget->setTextFormat( mTableWidget->selectionTextFormat() );
105 mFormattingWidget->setHorizontalAlignment( mTableWidget->selectionHorizontalAlignment() );
106 mFormattingWidget->setVerticalAlignment( mTableWidget->selectionVerticalAlignment() );
107 mFormattingWidget->setCellProperty( mTableWidget->selectionCellProperty() );
108
109 updateActionsFromSelection();
110
111 mFormattingWidget->setEnabled( !mTableWidget->isHeaderCellSelected() );
112 } );
113 updateActionsFromSelection();
114
115 addDockWidget( Qt::RightDockWidgetArea, mPropertiesDock );
116
117 mActionImportFromClipboard->setEnabled( !QApplication::clipboard()->text().isEmpty() );
118 connect( QApplication::clipboard(), &QClipboard::dataChanged, this, [ = ]() { mActionImportFromClipboard->setEnabled( !QApplication::clipboard()->text().isEmpty() ); } );
119
120 connect( mActionImportFromClipboard, &QAction::triggered, this, &QgsTableEditorDialog::setTableContentsFromClipboard );
121 connect( mActionClose, &QAction::triggered, this, &QMainWindow::close );
122 connect( mActionInsertRowsAbove, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::insertRowsAbove );
123 connect( mActionInsertRowsBelow, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::insertRowsBelow );
124 connect( mActionInsertColumnsBefore, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::insertColumnsBefore );
125 connect( mActionInsertColumnsAfter, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::insertColumnsAfter );
126 connect( mActionMergeSelected, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::mergeSelectedCells );
127 connect( mActionSplitSelected, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::splitSelectedCells );
128 connect( mActionDeleteRows, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::deleteRows );
129 connect( mActionDeleteColumns, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::deleteColumns );
130 connect( mActionSelectRow, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::expandRowSelection );
131 connect( mActionSelectColumn, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::expandColumnSelection );
132 connect( mActionSelectAll, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::selectAll );
133 connect( mActionClear, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::clearSelectedCells );
134 connect( mActionIncludeHeader, &QAction::toggled, this, [ = ]( bool checked )
135 {
136 mTableWidget->setIncludeTableHeader( checked );
137 emit includeHeaderChanged( checked );
138 } );
139
140 // restore the toolbar and dock widgets positions using Qt settings API
141 const QgsSettings settings;
142
143 const QByteArray state = settings.value( QStringLiteral( "LayoutDesigner/tableEditorState" ), QByteArray(), QgsSettings::App ).toByteArray();
144 if ( !state.isEmpty() && !restoreState( state ) )
145 {
146 QgsDebugError( QStringLiteral( "restore of table editor dialog UI state failed" ) );
147 }
148}
149
151{
152 QgsSettings settings;
153 // store the toolbar/dock widget settings using Qt settings API
154 settings.setValue( QStringLiteral( "LayoutDesigner/tableEditorState" ), saveState(), QgsSettings::App );
155}
156
158{
159 return mLayer.data();
160}
161
163{
164 if ( layer != mLayer )
165 {
166 mLayer = layer;
167 mFormattingWidget->setLayer( layer );
168 }
169}
170
171
173{
174 if ( QApplication::clipboard()->text().isEmpty() )
175 return false;
176
177 if ( QMessageBox::question( this, tr( "Import Content From Clipboard" ),
178 tr( "Importing content from clipboard will overwrite current table content. Are you sure?" ) ) != QMessageBox::Yes )
179 return false;
180
181 QgsTableContents contents;
182 const QStringList lines = QApplication::clipboard()->text().split( '\n' );
183 for ( const QString &line : lines )
184 {
185 if ( !line.isEmpty() )
186 {
187 QgsTableRow row;
188 const QStringList cells = line.split( '\t' );
189 for ( const QString &text : cells )
190 {
191 const QgsTableCell cell( text );
192 row << cell;
193 }
194 contents << row;
195 }
196 }
197
198 if ( !contents.isEmpty() )
199 {
200 setTableContents( contents );
201 emit tableChanged();
202 return true;
203 }
204
205 return false;
206}
207
209{
210 mBlockSignals = true;
211 mTableWidget->setTableContents( contents );
212 mTableWidget->resizeRowsToContents();
213 mTableWidget->resizeColumnsToContents();
214 mBlockSignals = false;
215}
216
218{
219 return mTableWidget->tableContents();
220}
221
223{
224 return mTableWidget->tableRowHeight( row );
225}
226
228{
229 return mTableWidget->tableColumnWidth( column );
230}
231
232void QgsTableEditorDialog::setTableRowHeight( int row, double height )
233{
234 mTableWidget->setTableRowHeight( row, height );
235}
236
237void QgsTableEditorDialog::setTableColumnWidth( int column, double width )
238{
239 mTableWidget->setTableColumnWidth( column, width );
240}
241
243{
244 return mActionIncludeHeader->isChecked();
245}
246
248{
249 mActionIncludeHeader->setChecked( included );
250}
251
253{
254 return mTableWidget->tableHeaders();
255}
256
257void QgsTableEditorDialog::setTableHeaders( const QVariantList &headers )
258{
259 mTableWidget->setTableHeaders( headers );
260}
261
266
267void QgsTableEditorDialog::updateActionsFromSelection()
268{
269 const int rowCount = mTableWidget->rowsAssociatedWithSelection().size();
270 const int columnCount = mTableWidget->columnsAssociatedWithSelection().size();
271
272 mActionInsertRowsAbove->setEnabled( rowCount > 0 );
273 mActionInsertRowsBelow->setEnabled( rowCount > 0 );
274 mActionDeleteRows->setEnabled( rowCount > 0 );
275 mActionSelectRow->setEnabled( rowCount > 0 );
276 if ( rowCount == 0 )
277 {
278 mActionInsertRowsAbove->setText( tr( "Rows Above" ) );
279 mActionInsertRowsBelow->setText( tr( "Rows Below" ) );
280 mActionDeleteRows->setText( tr( "Delete Rows" ) );
281 mActionSelectRow->setText( tr( "Select Rows" ) );
282 }
283 else if ( rowCount == 1 )
284 {
285 mActionInsertRowsAbove->setText( tr( "Row Above" ) );
286 mActionInsertRowsBelow->setText( tr( "Row Below" ) );
287 mActionDeleteRows->setText( tr( "Delete Row" ) );
288 mActionSelectRow->setText( tr( "Select Row" ) );
289 }
290 else
291 {
292 mActionInsertRowsAbove->setText( tr( "%n Row(s) Above", nullptr, rowCount ) );
293 mActionInsertRowsBelow->setText( tr( "%n Row(s) Below", nullptr, rowCount ) );
294 mActionDeleteRows->setText( tr( "Delete %n Row(s)", nullptr, rowCount ) );
295 mActionSelectRow->setText( tr( "Select %n Row(s)", nullptr, rowCount ) );
296 }
297
298 mActionInsertColumnsBefore->setEnabled( columnCount > 0 );
299 mActionInsertColumnsAfter->setEnabled( columnCount > 0 );
300 mActionDeleteColumns->setEnabled( columnCount > 0 );
301 mActionSelectColumn->setEnabled( columnCount > 0 );
302 if ( columnCount == 0 )
303 {
304 mActionInsertColumnsBefore->setText( tr( "Columns Before" ) );
305 mActionInsertColumnsAfter->setText( tr( "Columns After" ) );
306 mActionDeleteColumns->setText( tr( "Delete Columns" ) );
307 mActionSelectColumn->setText( tr( "Select Columns" ) );
308 }
309 else if ( columnCount == 1 )
310 {
311 mActionInsertColumnsBefore->setText( tr( "Column Before" ) );
312 mActionInsertColumnsAfter->setText( tr( "Column After" ) );
313 mActionDeleteColumns->setText( tr( "Delete Column" ) );
314 mActionSelectColumn->setText( tr( "Select Column" ) );
315 }
316 else
317 {
318 mActionInsertColumnsBefore->setText( tr( "%n Column(s) Before", nullptr, columnCount ) );
319 mActionInsertColumnsAfter->setText( tr( "%n Column(s) After", nullptr, columnCount ) );
320 mActionDeleteColumns->setText( tr( "Delete %n Column(s)", nullptr, columnCount ) );
321 mActionSelectColumn->setText( tr( "Select %n Column(s)", nullptr, columnCount ) );
322 }
323
324 mActionMergeSelected->setEnabled( mTableWidget->canMergeSelection() );
325 mActionSplitSelected->setEnabled( mTableWidget->canSplitSelection() );
326}
327
328#include "qgstableeditordialog.h"
QgsDockWidget subclass with more fine-grained control over how the widget is closed or opened.
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...
Definition qgsgui.cpp:209
Base class for all map layer types.
Definition qgsmaplayer.h:76
A bar for displaying non-blocking messages to the user.
A stack widget to manage panels in the interface.
void setMainPanel(QgsPanelWidget *panel SIP_TRANSFER)
Sets the main panel widget for the stack and selects it for the user.
virtual void setDockMode(bool dockMode)
Set the widget in dock mode which tells the widget to emit panel widgets and not open dialogs.
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
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.
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...
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.
A reusable widget for formatting the contents of a QgsTableCell.
void cellPropertyChanged(const QgsProperty &property)
Emitted when the cell contents property shown in the widget is changed.
void textFormatChanged()
Emitted whenever the text format shown in the widget is changed.
void verticalAlignmentChanged(Qt::Alignment alignment)
Emitted when the vertical alignment shown in the widget is changed.
void rowHeightChanged(double height)
Emitted whenever the row height shown in the widget is changed.
QgsTextFormat textFormat() const
Returns the current text format shown in the widget.
void setVerticalAlignment(Qt::Alignment alignment)
Sets the vertical alignment to show in the widget.
void setTextFormat(const QgsTextFormat &format)
Sets the text format to show in the widget.
void registerExpressionContextGenerator(QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
void setBackgroundColor(const QColor &color)
Sets the cell background color to show in the widget.
void backgroundColorChanged(const QColor &color)
Emitted whenever the cell background color is changed in the widget.
QgsNumericFormat * numericFormat()
Returns the current numeric format shown in the widget, or a nullptr if no numeric format is set.
void setCellProperty(const QgsProperty &property)
Sets the cell content's property to show in the widget.
void numberFormatChanged()
Emitted whenever the numeric format shown in the widget is changed.
void setColumnWidth(double width)
Sets the column width to show in the widget, or 0 for automatic width.
void setHorizontalAlignment(Qt::Alignment alignment)
Sets the horizontal alignment to show in the widget.
void setNumericFormat(QgsNumericFormat *format, bool isMixedFormat)
Sets the numeric format to show in the widget, or nullptr if no numeric format is set.
void horizontalAlignmentChanged(Qt::Alignment alignment)
Emitted when the horizontal alignment shown in the widget is changed.
void setLayer(QgsMapLayer *layer)
Set the layer to be used for in the expression editor context.
void columnWidthChanged(double width)
Emitted whenever the column width shown in the widget is changed.
void setRowHeight(double height)
Sets the row height to show in the widget, or 0 for automatic height.
A reusable widget for editing simple spreadsheet-style tables.
void setSelectionVerticalAlignment(Qt::Alignment alignment)
Sets the vertical alignment for the currently selected cells.
void splitSelectedCells()
Splits (un-merges) selected table cells.
QgsProperty selectionCellProperty()
Returns the QgsProperty used for the contents of the currently selected cells.
QVariantList tableHeaders() const
Returns the table header values.
void setSelectionTextFormat(const QgsTextFormat &format)
Sets the text format for the selected cells.
void setTableHeaders(const QVariantList &headers)
Sets the table headers.
void setSelectionBackgroundColor(const QColor &color)
Sets the background color for the currently selected cells.
void activeCellChanged()
Emitted whenever the active (or selected) cell changes in the widget.
Qt::Alignment selectionHorizontalAlignment()
Returns the horizontal alignment for the currently selected cells.
QgsTextFormat selectionTextFormat()
Returns the text format for the currently selected cells.
void insertColumnsAfter()
Inserts new columns after the current selection.
double selectionRowHeight()
Returns the height (in millimeters) of the rows associated with the current selection,...
void clearSelectedCells()
Clears the contents of the currently selected cells.
bool hasMixedSelectionNumericFormat()
Returns true if the current selection has a mix of numeric formats.
QList< int > rowsAssociatedWithSelection()
Returns a list of the rows associated with the current table selected cells.
QList< int > columnsAssociatedWithSelection()
Returns a list of the columns associated with the current table selected cells.
void deleteRows()
Deletes all rows associated with the current selected cells.
void setSelectionHorizontalAlignment(Qt::Alignment alignment)
Sets the horizontal alignment for the currently selected cells.
void setSelectionRowHeight(double height)
Sets the row height (in millimeters) for the currently selected rows, or 0 for automatic row height.
void insertRowsBelow()
Inserts new rows below the current selection.
double tableRowHeight(int row)
Returns the configured row height for the specified row, or 0 if an automatic height should be used f...
QColor selectionBackgroundColor()
Returns the background color for the currently selected cells.
bool canMergeSelection() const
Returns true if a selection has been made which can be merged.
Qt::Alignment selectionVerticalAlignment()
Returns the horizontal alignment for the currently selected cells.
void insertRowsAbove()
Inserts new rows above the current selection.
void setTableColumnWidth(int column, double width)
Sets the configured column width for the specified column.
void setSelectionCellProperty(const QgsProperty &property)
Sets the cell contents QgsProperty for the currently selected cells.
void setSelectionColumnWidth(double height)
Sets the column width (in millimeters) for the currently selected columns, or 0 for automatic column ...
double selectionColumnWidth()
Returns the width (in millimeters) of the columns associated with the current selection,...
bool canSplitSelection() const
Returns true if a selection has been made which can be split.
void expandRowSelection()
Expands out the selection to include whole rows associated with the current selected cells.
double tableColumnWidth(int column)
Returns the configured column width for the specified column, or 0 if an automatic width should be us...
void setIncludeTableHeader(bool included)
Sets whether the table includes a header row.
QgsNumericFormat * selectionNumericFormat()
Returns the numeric format used for the currently selected cells, or nullptr if the selection has no ...
void setTableRowHeight(int row, double height)
Sets the configured row height for the specified row.
QgsTableContents tableContents() const
Returns the current contents of the editor widget table.
void deleteColumns()
Deletes all columns associated with the current selected cells.
bool isHeaderCellSelected() const
Returns true if any header cells are selected.
void setTableContents(const QgsTableContents &contents)
Sets the contents to show in the editor widget.
void tableChanged()
Emitted whenever the table contents are changed.
void insertColumnsBefore()
Inserts new columns before the current selection.
void mergeSelectedCells()
Merges selected table cells.
void setSelectionNumericFormat(QgsNumericFormat *format)
Sets the numeric format to use for the currently selected cells.
void expandColumnSelection()
Expands out the selection to include whole columns associated with the current selected cells.
#define QgsDebugError(str)
Definition qgslogger.h:38
QVector< QgsTableRow > QgsTableContents
A set of table rows.
QVector< QgsTableCell > QgsTableRow
A row of table cells.