Skip to content

Commit

Permalink
[browser] Restore attribute preview from 2.x standalone browser
Browse files Browse the repository at this point in the history
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 14, 2018
1 parent 3b892c9 commit 08544fb
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
54 changes: 54 additions & 0 deletions src/gui/qgsbrowserdockwidget_p.cpp
Expand Up @@ -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>
Expand Down Expand Up @@ -133,6 +136,11 @@ QgsBrowserLayerProperties::QgsBrowserLayerProperties( QWidget *parent )
mMapCanvas->freeze( false );
mMapCanvas->refresh();
}
else if ( mTabWidget->currentWidget() == mAttributesTab )
{
if ( ! mAttributeTableFilterModel )
loadAttributeTable();
}
} );
}

Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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 )

Expand Down
4 changes: 4 additions & 0 deletions src/gui/qgsbrowserdockwidget_p.h
Expand Up @@ -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;

};

Expand Down
31 changes: 29 additions & 2 deletions src/ui/qgsbrowserlayerpropertiesbase.ui
Expand Up @@ -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>
Expand All @@ -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/>
Expand Down

0 comments on commit 08544fb

Please sign in to comment.