Skip to content
Permalink
Browse files

[browser] Restore attribute preview from 2.x standalone browser

This resurrects an "attributes" tab in the browser layer properties
panel/window, showing a preview of the attributes for a vector
layer.

For performance, we limit the table to show a maximum of the first
100 rows. (There's no way to "page" attribute table requests
currently or load them in the background). But besides, the table
is really only good for a quick preview and in this case it's ok
to only show a subset of records.
  • Loading branch information
nyalldawson committed Nov 13, 2018
1 parent 3b892c9 commit 08544fbbd6d8a81a4a2eec0fdc90f1d5d880eb61
Showing with 87 additions and 2 deletions.
  1. +54 −0 src/gui/qgsbrowserdockwidget_p.cpp
  2. +4 −0 src/gui/qgsbrowserdockwidget_p.h
  3. +29 −2 src/ui/qgsbrowserlayerpropertiesbase.ui
@@ -41,6 +41,9 @@
#include "qgsgui.h"
#include "qgsnative.h"
#include "qgsmaptoolpan.h"
#include "qgsvectorlayercache.h"
#include "qgsattributetablemodel.h"
#include "qgsattributetablefiltermodel.h"
#include <QDesktopServices>

#include <QDragEnterEvent>
@@ -133,6 +136,11 @@ QgsBrowserLayerProperties::QgsBrowserLayerProperties( QWidget *parent )
mMapCanvas->freeze( false );
mMapCanvas->refresh();
}
else if ( mTabWidget->currentWidget() == mAttributesTab )
{
if ( ! mAttributeTableFilterModel )
loadAttributeTable();
}
} );
}

@@ -202,6 +210,13 @@ void QgsBrowserLayerProperties::setItem( QgsDataItem *item )
return;
}

mAttributeTable->setModel( nullptr );
if ( mAttributeTableFilterModel )
{
// Cleanup
mAttributeTableFilterModel->deleteLater();
mAttributeTableFilterModel = nullptr;
}
if ( mLayer && mLayer->isValid() )
{
bool ok = false;
@@ -212,6 +227,12 @@ void QgsBrowserLayerProperties::setItem( QgsDataItem *item )
mMapCanvas->setDestinationCrs( mLayer->crs() );
mMapCanvas->setLayers( QList< QgsMapLayer * >() << mLayer.get() );
mMapCanvas->zoomToFullExtent();

if ( mAttributesTab && mLayer->type() != QgsMapLayer::VectorLayer )
{
mTabWidget->removeTab( mTabWidget->indexOf( mAttributesTab ) );
mAttributesTab = nullptr;
}
}

QString myStyle = QgsApplication::reportStyleSheet();
@@ -247,6 +268,39 @@ void QgsBrowserLayerProperties::urlClicked( const QUrl &url )
QDesktopServices::openUrl( url );
}

void QgsBrowserLayerProperties::loadAttributeTable()
{
if ( !mLayer || !mLayer->isValid() || mLayer->type() != QgsMapLayer::VectorLayer )
return;

// Initialize the cache
QgsVectorLayerCache *layerCache = new QgsVectorLayerCache( qobject_cast< QgsVectorLayer * >( mLayer.get() ), 1000, this );
layerCache->setCacheGeometry( false );
QgsAttributeTableModel *tableModel = new QgsAttributeTableModel( layerCache, this );
mAttributeTableFilterModel = new QgsAttributeTableFilterModel( nullptr, tableModel, this );
tableModel->setRequest( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setLimit( 100 ) );
layerCache->setParent( tableModel );
tableModel->setParent( mAttributeTableFilterModel );

mAttributeTable->setModel( mAttributeTableFilterModel );
tableModel->loadLayer();
QFont font = mAttributeTable->font();
int fontSize = font.pointSize();
#ifdef Q_OS_WIN
fontSize = std::max( fontSize - 1, 8 ); // bit less on windows, due to poor rendering of small point sizes
#else
fontSize = std::max( fontSize - 2, 6 );
#endif
font.setPointSize( fontSize );
mAttributeTable->setFont( font );

// we can safely do this expensive operation here (unlike in the main attribute table), because at most we have only 100 rows...
mAttributeTable->resizeColumnsToContents();
mAttributeTable->resizeRowsToContents();
mAttributeTable->verticalHeader()->setVisible( false ); // maximize valuable table space
mAttributeTable->setAlternatingRowColors( true );
}

QgsBrowserDirectoryProperties::QgsBrowserDirectoryProperties( QWidget *parent )
: QgsBrowserPropertiesWidget( parent )

@@ -128,7 +128,11 @@ class QgsBrowserLayerProperties : public QgsBrowserPropertiesWidget, private Ui:
void urlClicked( const QUrl &url );

private:

void loadAttributeTable();

std::unique_ptr<QgsMapLayer> mLayer;
QgsAttributeTableFilterModel *mAttributeTableFilterModel = nullptr;

};

@@ -71,7 +71,29 @@
<number>0</number>
</property>
<item>
<widget class="QgsMapCanvas" name="mMapCanvas" native="true"/>
<widget class="QgsMapCanvas" name="mMapCanvas"/>
</item>
</layout>
</widget>
<widget class="QWidget" name="mAttributesTab">
<attribute name="title">
<string>Attributes</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QgsAttributeTableView" name="mAttributeTable"/>
</item>
</layout>
</widget>
@@ -92,10 +114,15 @@
<customwidgets>
<customwidget>
<class>QgsMapCanvas</class>
<extends>QWidget</extends>
<extends>QGraphicsView</extends>
<header>qgsmapcanvas.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsAttributeTableView</class>
<extends>QTableView</extends>
<header>qgsattributetableview.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>

0 comments on commit 08544fb

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