QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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
18#include "qgsmessagebar.h"
19#include "qgsgui.h"
20#include "qgsdockwidget.h"
21#include "qgspanelwidgetstack.h"
23#include "qgssettings.h"
24#include "qgsmaplayer.h"
25
26#include <QClipboard>
27#include <QMessageBox>
28
30 : QMainWindow( parent )
31{
32 setupUi( this );
33 setWindowTitle( tr( "Table Designer" ) );
34
35 setAttribute( Qt::WA_DeleteOnClose );
36 setDockOptions( dockOptions() | QMainWindow::GroupedDragging );
37
39
40 QGridLayout *viewLayout = new QGridLayout();
41 viewLayout->setSpacing( 0 );
42 viewLayout->setContentsMargins( 0, 0, 0, 0 );
43 centralWidget()->layout()->setSpacing( 0 );
44 centralWidget()->layout()->setContentsMargins( 0, 0, 0, 0 );
45
46 mMessageBar = new QgsMessageBar( centralWidget() );
47 mMessageBar->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed );
48 static_cast< QGridLayout * >( centralWidget()->layout() )->addWidget( mMessageBar, 0, 0, 1, 1, Qt::AlignTop );
49
50 mTableWidget = new QgsTableEditorWidget();
51 mTableWidget->setContentsMargins( 0, 0, 0, 0 );
52 viewLayout->addWidget( mTableWidget, 0, 0 );
53 mViewFrame->setLayout( viewLayout );
54 mViewFrame->setContentsMargins( 0, 0, 0, 0 );
55
56 mTableWidget->setFocus();
57 mTableWidget->setTableContents( QgsTableContents() << ( QgsTableRow() << QgsTableCell() ) );
58
59 connect( mTableWidget, &QgsTableEditorWidget::tableChanged, this, [ = ]
60 {
61 if ( !mBlockSignals )
62 emit tableChanged();
63 } );
64
65 const int minDockWidth( fontMetrics().boundingRect( QStringLiteral( "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ) ).width() );
66
67 mPropertiesDock = new QgsDockWidget( tr( "Cell Contents" ), this );
68 mPropertiesDock->setObjectName( QStringLiteral( "FormattingDock" ) );
69 mPropertiesStack = new QgsPanelWidgetStack();
70 mPropertiesDock->setWidget( mPropertiesStack );
71 mPropertiesDock->setMinimumWidth( minDockWidth );
72
73 mFormattingWidget = new QgsTableEditorFormattingWidget();
74 mFormattingWidget->setDockMode( true );
75 mPropertiesStack->setMainPanel( mFormattingWidget );
76
77 mPropertiesDock->setFeatures( QDockWidget::NoDockWidgetFeatures );
78
80
84
85 connect( mFormattingWidget, &QgsTableEditorFormattingWidget::textFormatChanged, this, [ = ]
86 {
87 mTableWidget->setSelectionTextFormat( mFormattingWidget->textFormat() );
88 } );
89
90 connect( mFormattingWidget, &QgsTableEditorFormattingWidget::numberFormatChanged, this, [ = ]
91 {
92 mTableWidget->setSelectionNumericFormat( mFormattingWidget->numericFormat() );
93 } );
96
97 connect( mTableWidget, &QgsTableEditorWidget::activeCellChanged, this, [ = ]
98 {
99 mFormattingWidget->setBackgroundColor( mTableWidget->selectionBackgroundColor() );
100 mFormattingWidget->setNumericFormat( mTableWidget->selectionNumericFormat(), mTableWidget->hasMixedSelectionNumericFormat() );
101 mFormattingWidget->setRowHeight( mTableWidget->selectionRowHeight() );
102 mFormattingWidget->setColumnWidth( mTableWidget->selectionColumnWidth() );
103 mFormattingWidget->setTextFormat( mTableWidget->selectionTextFormat() );
104 mFormattingWidget->setHorizontalAlignment( mTableWidget->selectionHorizontalAlignment() );
105 mFormattingWidget->setVerticalAlignment( mTableWidget->selectionVerticalAlignment() );
106 mFormattingWidget->setCellProperty( mTableWidget->selectionCellProperty() );
107
108 updateActionsFromSelection();
109
110 mFormattingWidget->setEnabled( !mTableWidget->isHeaderCellSelected() );
111 } );
112 updateActionsFromSelection();
113
114 addDockWidget( Qt::RightDockWidgetArea, mPropertiesDock );
115
116 mActionImportFromClipboard->setEnabled( !QApplication::clipboard()->text().isEmpty() );
117 connect( QApplication::clipboard(), &QClipboard::dataChanged, this, [ = ]() { mActionImportFromClipboard->setEnabled( !QApplication::clipboard()->text().isEmpty() ); } );
118
119 connect( mActionImportFromClipboard, &QAction::triggered, this, &QgsTableEditorDialog::setTableContentsFromClipboard );
120 connect( mActionClose, &QAction::triggered, this, &QMainWindow::close );
121 connect( mActionInsertRowsAbove, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::insertRowsAbove );
122 connect( mActionInsertRowsBelow, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::insertRowsBelow );
123 connect( mActionInsertColumnsBefore, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::insertColumnsBefore );
124 connect( mActionInsertColumnsAfter, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::insertColumnsAfter );
125 connect( mActionMergeSelected, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::mergeSelectedCells );
126 connect( mActionSplitSelected, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::splitSelectedCells );
127 connect( mActionDeleteRows, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::deleteRows );
128 connect( mActionDeleteColumns, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::deleteColumns );
129 connect( mActionSelectRow, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::expandRowSelection );
130 connect( mActionSelectColumn, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::expandColumnSelection );
131 connect( mActionSelectAll, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::selectAll );
132 connect( mActionClear, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::clearSelectedCells );
133 connect( mActionIncludeHeader, &QAction::toggled, this, [ = ]( bool checked )
134 {
135 mTableWidget->setIncludeTableHeader( checked );
136 emit includeHeaderChanged( checked );
137 } );
138
139 // restore the toolbar and dock widgets positions using Qt settings API
140 const QgsSettings settings;
141
142 const QByteArray state = settings.value( QStringLiteral( "LayoutDesigner/tableEditorState" ), QByteArray(), QgsSettings::App ).toByteArray();
143 if ( !state.isEmpty() && !restoreState( state ) )
144 {
145 QgsDebugError( QStringLiteral( "restore of table editor dialog UI state failed" ) );
146 }
147}
148
150{
151 QgsSettings settings;
152 // store the toolbar/dock widget settings using Qt settings API
153 settings.setValue( QStringLiteral( "LayoutDesigner/tableEditorState" ), saveState(), QgsSettings::App );
154}
155
157{
158 return mLayer.data();
159}
160
162{
163 if ( layer != mLayer )
164 {
165 mLayer = layer;
166 mFormattingWidget->setLayer( layer );
167 }
168}
169
170
172{
173 if ( QApplication::clipboard()->text().isEmpty() )
174 return false;
175
176 if ( QMessageBox::question( this, tr( "Import Content From Clipboard" ),
177 tr( "Importing content from clipboard will overwrite current table content. Are you sure?" ) ) != QMessageBox::Yes )
178 return false;
179
180 QgsTableContents contents;
181 const QStringList lines = QApplication::clipboard()->text().split( '\n' );
182 for ( const QString &line : lines )
183 {
184 if ( !line.isEmpty() )
185 {
186 QgsTableRow row;
187 const QStringList cells = line.split( '\t' );
188 for ( const QString &text : cells )
189 {
190 const QgsTableCell cell( text );
191 row << cell;
192 }
193 contents << row;
194 }
195 }
196
197 if ( !contents.isEmpty() )
198 {
199 setTableContents( contents );
200 emit tableChanged();
201 return true;
202 }
203
204 return false;
205}
206
208{
209 mBlockSignals = true;
210 mTableWidget->setTableContents( contents );
211 mTableWidget->resizeRowsToContents();
212 mTableWidget->resizeColumnsToContents();
213 mBlockSignals = false;
214}
215
217{
218 return mTableWidget->tableContents();
219}
220
222{
223 return mTableWidget->tableRowHeight( row );
224}
225
227{
228 return mTableWidget->tableColumnWidth( column );
229}
230
231void QgsTableEditorDialog::setTableRowHeight( int row, double height )
232{
233 mTableWidget->setTableRowHeight( row, height );
234}
235
236void QgsTableEditorDialog::setTableColumnWidth( int column, double width )
237{
238 mTableWidget->setTableColumnWidth( column, width );
239}
240
242{
243 return mActionIncludeHeader->isChecked();
244}
245
247{
248 mActionIncludeHeader->setChecked( included );
249}
250
252{
253 return mTableWidget->tableHeaders();
254}
255
256void QgsTableEditorDialog::setTableHeaders( const QVariantList &headers )
257{
258 mTableWidget->setTableHeaders( headers );
259}
260
265
266void QgsTableEditorDialog::updateActionsFromSelection()
267{
268 const int rowCount = mTableWidget->rowsAssociatedWithSelection().size();
269 const int columnCount = mTableWidget->columnsAssociatedWithSelection().size();
270
271 mActionInsertRowsAbove->setEnabled( rowCount > 0 );
272 mActionInsertRowsBelow->setEnabled( rowCount > 0 );
273 mActionDeleteRows->setEnabled( rowCount > 0 );
274 mActionSelectRow->setEnabled( rowCount > 0 );
275 if ( rowCount == 0 )
276 {
277 mActionInsertRowsAbove->setText( tr( "Rows Above" ) );
278 mActionInsertRowsBelow->setText( tr( "Rows Below" ) );
279 mActionDeleteRows->setText( tr( "Delete Rows" ) );
280 mActionSelectRow->setText( tr( "Select Rows" ) );
281 }
282 else if ( rowCount == 1 )
283 {
284 mActionInsertRowsAbove->setText( tr( "Row Above" ) );
285 mActionInsertRowsBelow->setText( tr( "Row Below" ) );
286 mActionDeleteRows->setText( tr( "Delete Row" ) );
287 mActionSelectRow->setText( tr( "Select Row" ) );
288 }
289 else
290 {
291 mActionInsertRowsAbove->setText( tr( "%n Row(s) Above", nullptr, rowCount ) );
292 mActionInsertRowsBelow->setText( tr( "%n Row(s) Below", nullptr, rowCount ) );
293 mActionDeleteRows->setText( tr( "Delete %n Row(s)", nullptr, rowCount ) );
294 mActionSelectRow->setText( tr( "Select %n Row(s)", nullptr, rowCount ) );
295 }
296
297 mActionInsertColumnsBefore->setEnabled( columnCount > 0 );
298 mActionInsertColumnsAfter->setEnabled( columnCount > 0 );
299 mActionDeleteColumns->setEnabled( columnCount > 0 );
300 mActionSelectColumn->setEnabled( columnCount > 0 );
301 if ( columnCount == 0 )
302 {
303 mActionInsertColumnsBefore->setText( tr( "Columns Before" ) );
304 mActionInsertColumnsAfter->setText( tr( "Columns After" ) );
305 mActionDeleteColumns->setText( tr( "Delete Columns" ) );
306 mActionSelectColumn->setText( tr( "Select Columns" ) );
307 }
308 else if ( columnCount == 1 )
309 {
310 mActionInsertColumnsBefore->setText( tr( "Column Before" ) );
311 mActionInsertColumnsAfter->setText( tr( "Column After" ) );
312 mActionDeleteColumns->setText( tr( "Delete Column" ) );
313 mActionSelectColumn->setText( tr( "Select Column" ) );
314 }
315 else
316 {
317 mActionInsertColumnsBefore->setText( tr( "%n Column(s) Before", nullptr, columnCount ) );
318 mActionInsertColumnsAfter->setText( tr( "%n Column(s) After", nullptr, columnCount ) );
319 mActionDeleteColumns->setText( tr( "Delete %n Column(s)", nullptr, columnCount ) );
320 mActionSelectColumn->setText( tr( "Select %n Column(s)", nullptr, columnCount ) );
321 }
322
323 mActionMergeSelected->setEnabled( mTableWidget->canMergeSelection() );
324 mActionSplitSelected->setEnabled( mTableWidget->canSplitSelection() );
325}
326
327#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:208
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.