Skip to content

Commit

Permalink
[FEATURE][layouts] Add import content from clipboard for fixed table …
Browse files Browse the repository at this point in the history
…items
  • Loading branch information
nirvn committed Mar 31, 2020
1 parent 3dfc931 commit ab6fe9d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
10 changes: 10 additions & 0 deletions python/gui/auto_generated/tableeditor/qgstableeditordialog.sip.in
Expand Up @@ -34,6 +34,16 @@ Constructor for QgsTableEditorDialog with the specified ``parent`` widget.
%Docstring
Sets the ``contents`` to show in the editor widget.

.. seealso:: :py:func:`tableContents`
%End


bool setTableContentsFromClipboard();
%Docstring
Parses the clipboard text into contents to show in the editor widget.

:return: ``True`` if the clipboard was successfully parsed

.. seealso:: :py:func:`tableContents`
%End

Expand Down
43 changes: 43 additions & 0 deletions src/gui/tableeditor/qgstableeditordialog.cpp
Expand Up @@ -21,6 +21,9 @@
#include "qgspanelwidgetstack.h"
#include "qgstableeditorformattingwidget.h"

#include <QClipboard>
#include <QMessageBox>

QgsTableEditorDialog::QgsTableEditorDialog( QWidget *parent )
: QMainWindow( parent )
{
Expand Down Expand Up @@ -98,6 +101,10 @@ QgsTableEditorDialog::QgsTableEditorDialog( QWidget *parent )

addDockWidget( Qt::RightDockWidgetArea, mPropertiesDock );

mActionImportFromClipboard->setEnabled( !QApplication::clipboard()->text().isEmpty() );
connect( QApplication::clipboard(), &QClipboard::dataChanged, this, [ = ]() { mActionImportFromClipboard->setEnabled( !QApplication::clipboard()->text().isEmpty() ); } );

connect( mActionImportFromClipboard, &QAction::triggered, this, &QgsTableEditorDialog::setTableContentsFromClipboard );
connect( mActionClose, &QAction::triggered, this, &QMainWindow::close );
connect( mActionInsertRowsAbove, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::insertRowsAbove );
connect( mActionInsertRowsBelow, &QAction::triggered, mTableWidget, &QgsTableEditorWidget::insertRowsBelow );
Expand All @@ -116,6 +123,42 @@ QgsTableEditorDialog::QgsTableEditorDialog( QWidget *parent )
} );
}

bool QgsTableEditorDialog::setTableContentsFromClipboard()
{
if ( QApplication::clipboard()->text().isEmpty() )
return false;

if ( QMessageBox::question( this, tr( "Import Content From Clipboard" ),
tr( "Importing content from clipboard will overwrite current table content. Are you sure?" ) ) != QMessageBox::Yes )
return false;

QgsTableContents contents;
const QStringList lines = QApplication::clipboard()->text().split( '\n' );
for ( const QString &line : lines )
{
if ( !line.isEmpty() )
{
QgsTableRow row;
const QStringList cells = line.split( '\t' );
for ( const QString &text : cells )
{
QgsTableCell cell( text );
row << cell;
}
contents << row;
}
}

if ( !contents.isEmpty() )
{
setTableContents( contents );
emit tableChanged();
return true;
}

return false;
}

void QgsTableEditorDialog::setTableContents( const QgsTableContents &contents )
{
mBlockSignals = true;
Expand Down
9 changes: 9 additions & 0 deletions src/gui/tableeditor/qgstableeditordialog.h
Expand Up @@ -54,6 +54,15 @@ class GUI_EXPORT QgsTableEditorDialog : public QMainWindow, private Ui::QgsTable
*/
void setTableContents( const QgsTableContents &contents );

/**
* Parses the clipboard text into contents to show in the editor widget.
* \returns TRUE if the clipboard was successfully parsed
*
* \see tableContents()
*/

bool setTableContentsFromClipboard();

/**
* Returns the current contents of the editor widget table.
*
Expand Down
7 changes: 7 additions & 0 deletions src/ui/qgstableeditorbase.ui
Expand Up @@ -100,6 +100,8 @@
<property name="title">
<string>File</string>
</property>
<addaction name="mActionImportFromClipboard"/>
<addaction name="separator"/>
<addaction name="mActionClose"/>
</widget>
<addaction name="menuFile"/>
Expand Down Expand Up @@ -161,6 +163,11 @@
<string>Select Column</string>
</property>
</action>
<action name="mActionImportFromClipboard">
<property name="text">
<string>Import Content from Clipboard</string>
</property>
</action>
<action name="mActionClose">
<property name="text">
<string>Close Editor</string>
Expand Down

0 comments on commit ab6fe9d

Please sign in to comment.