Skip to content
Permalink
Browse files

[FEATURE] Copy statistics panel content to clipboard button

  • Loading branch information
nirvn committed Apr 20, 2018
1 parent eb7f755 commit f63d5ad9acb3d6a8317a20743f937f773bbcf1b1
@@ -12,13 +12,15 @@
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsstatisticalsummarydockwidget.h"
#include "qgsstatisticalsummary.h"
#include "qgsproject.h"

#include "qgisapp.h"
#include "qgsclipboard.h"
#include "qgsmapcanvas.h"
#include "qgsvectorlayer.h"
#include "qgsproject.h"
#include "qgssettings.h"
#include "qgsstatisticalsummarydockwidget.h"
#include "qgsstatisticalsummary.h"
#include "qgsvectorlayer.h"

#include <QTableWidget>
#include <QAction>
@@ -93,6 +95,7 @@ QgsStatisticalSummaryDockWidget::QgsStatisticalSummaryDockWidget( QWidget *paren
connect( mLayerComboBox, &QgsMapLayerComboBox::layerChanged, this, &QgsStatisticalSummaryDockWidget::layerChanged );
connect( mFieldExpressionWidget, static_cast<void ( QgsFieldExpressionWidget::* )( const QString & )>( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsStatisticalSummaryDockWidget::fieldChanged );
connect( mSelectedOnlyCheckBox, &QAbstractButton::toggled, this, &QgsStatisticalSummaryDockWidget::refreshStatistics );
connect( mButtonCopy, &QAbstractButton::clicked, this, &QgsStatisticalSummaryDockWidget::copyStatistics );
connect( mButtonRefresh, &QAbstractButton::clicked, this, &QgsStatisticalSummaryDockWidget::refreshStatistics );
connect( QgsProject::instance(), static_cast<void ( QgsProject::* )( const QStringList & )>( &QgsProject::layersWillBeRemoved ), this, &QgsStatisticalSummaryDockWidget::layersRemoved );

@@ -121,6 +124,34 @@ void QgsStatisticalSummaryDockWidget::fieldChanged()
}
}

void QgsStatisticalSummaryDockWidget::copyStatistics()
{
QStringList rows;
QStringList columns;
for ( int i = 0; i < mStatisticsTable->rowCount(); i++ )
{
for ( int j = 0; j < mStatisticsTable->columnCount(); j++ )
{
QTableWidgetItem *item = mStatisticsTable->item( i, j );
columns += item->text();
}
rows += columns.join( QStringLiteral( "\t" ) );
columns.clear();
}

if ( !rows.isEmpty() )
{
QString text = QString( "%1\t%2\n%3" ).arg( mStatisticsTable->horizontalHeaderItem( 0 )->text(),
mStatisticsTable->horizontalHeaderItem( 1 )->text(),
rows.join( QStringLiteral( "\n" ) ) );
QString html = QString( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/></head><body><table border=\"1\"><tr><td>%1</td></tr></table></body></html>" ).arg( text );
html.replace( QStringLiteral( "\t" ), QStringLiteral( "</td><td>" ) ).replace( QStringLiteral( "\n" ), QStringLiteral( "</td></tr><tr><td>" ) );

QgsClipboard clipboard;
clipboard.setData( QStringLiteral( "text/html" ), html.toUtf8(), text );
}
}

void QgsStatisticalSummaryDockWidget::refreshStatistics()
{
if ( !mLayer || ( mFieldExpressionWidget->isExpression() && !mFieldExpressionWidget->isValidExpression() ) )
@@ -125,6 +125,11 @@ class APP_EXPORT QgsStatisticalSummaryDockWidget : public QgsDockWidget, private

public slots:

/**
* Copy the displayed statistics to the clipboard
*/
void copyStatistics();

/**
* Recalculates the displayed statistics
*/
@@ -102,6 +102,23 @@
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="mButtonCopy">
<property name="toolTip">
<string>Copy Statistics to Clipboard</string>
</property>
<property name="text">
<string>…</string>
</property>
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionEditCopy.svg</normaloff>:/images/themes/default/mActionEditCopy.svg</iconset>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="mButtonRefresh">
<property name="toolTip">

0 comments on commit f63d5ad

Please sign in to comment.
You can’t perform that action at this time.