Skip to content

Commit 08544fb

Browse files
committed
[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.
1 parent 3b892c9 commit 08544fb

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

src/gui/qgsbrowserdockwidget_p.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
#include "qgsgui.h"
4242
#include "qgsnative.h"
4343
#include "qgsmaptoolpan.h"
44+
#include "qgsvectorlayercache.h"
45+
#include "qgsattributetablemodel.h"
46+
#include "qgsattributetablefiltermodel.h"
4447
#include <QDesktopServices>
4548

4649
#include <QDragEnterEvent>
@@ -133,6 +136,11 @@ QgsBrowserLayerProperties::QgsBrowserLayerProperties( QWidget *parent )
133136
mMapCanvas->freeze( false );
134137
mMapCanvas->refresh();
135138
}
139+
else if ( mTabWidget->currentWidget() == mAttributesTab )
140+
{
141+
if ( ! mAttributeTableFilterModel )
142+
loadAttributeTable();
143+
}
136144
} );
137145
}
138146

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

213+
mAttributeTable->setModel( nullptr );
214+
if ( mAttributeTableFilterModel )
215+
{
216+
// Cleanup
217+
mAttributeTableFilterModel->deleteLater();
218+
mAttributeTableFilterModel = nullptr;
219+
}
205220
if ( mLayer && mLayer->isValid() )
206221
{
207222
bool ok = false;
@@ -212,6 +227,12 @@ void QgsBrowserLayerProperties::setItem( QgsDataItem *item )
212227
mMapCanvas->setDestinationCrs( mLayer->crs() );
213228
mMapCanvas->setLayers( QList< QgsMapLayer * >() << mLayer.get() );
214229
mMapCanvas->zoomToFullExtent();
230+
231+
if ( mAttributesTab && mLayer->type() != QgsMapLayer::VectorLayer )
232+
{
233+
mTabWidget->removeTab( mTabWidget->indexOf( mAttributesTab ) );
234+
mAttributesTab = nullptr;
235+
}
215236
}
216237

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

271+
void QgsBrowserLayerProperties::loadAttributeTable()
272+
{
273+
if ( !mLayer || !mLayer->isValid() || mLayer->type() != QgsMapLayer::VectorLayer )
274+
return;
275+
276+
// Initialize the cache
277+
QgsVectorLayerCache *layerCache = new QgsVectorLayerCache( qobject_cast< QgsVectorLayer * >( mLayer.get() ), 1000, this );
278+
layerCache->setCacheGeometry( false );
279+
QgsAttributeTableModel *tableModel = new QgsAttributeTableModel( layerCache, this );
280+
mAttributeTableFilterModel = new QgsAttributeTableFilterModel( nullptr, tableModel, this );
281+
tableModel->setRequest( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setLimit( 100 ) );
282+
layerCache->setParent( tableModel );
283+
tableModel->setParent( mAttributeTableFilterModel );
284+
285+
mAttributeTable->setModel( mAttributeTableFilterModel );
286+
tableModel->loadLayer();
287+
QFont font = mAttributeTable->font();
288+
int fontSize = font.pointSize();
289+
#ifdef Q_OS_WIN
290+
fontSize = std::max( fontSize - 1, 8 ); // bit less on windows, due to poor rendering of small point sizes
291+
#else
292+
fontSize = std::max( fontSize - 2, 6 );
293+
#endif
294+
font.setPointSize( fontSize );
295+
mAttributeTable->setFont( font );
296+
297+
// we can safely do this expensive operation here (unlike in the main attribute table), because at most we have only 100 rows...
298+
mAttributeTable->resizeColumnsToContents();
299+
mAttributeTable->resizeRowsToContents();
300+
mAttributeTable->verticalHeader()->setVisible( false ); // maximize valuable table space
301+
mAttributeTable->setAlternatingRowColors( true );
302+
}
303+
250304
QgsBrowserDirectoryProperties::QgsBrowserDirectoryProperties( QWidget *parent )
251305
: QgsBrowserPropertiesWidget( parent )
252306

src/gui/qgsbrowserdockwidget_p.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ class QgsBrowserLayerProperties : public QgsBrowserPropertiesWidget, private Ui:
128128
void urlClicked( const QUrl &url );
129129

130130
private:
131+
132+
void loadAttributeTable();
133+
131134
std::unique_ptr<QgsMapLayer> mLayer;
135+
QgsAttributeTableFilterModel *mAttributeTableFilterModel = nullptr;
132136

133137
};
134138

src/ui/qgsbrowserlayerpropertiesbase.ui

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,29 @@
7171
<number>0</number>
7272
</property>
7373
<item>
74-
<widget class="QgsMapCanvas" name="mMapCanvas" native="true"/>
74+
<widget class="QgsMapCanvas" name="mMapCanvas"/>
75+
</item>
76+
</layout>
77+
</widget>
78+
<widget class="QWidget" name="mAttributesTab">
79+
<attribute name="title">
80+
<string>Attributes</string>
81+
</attribute>
82+
<layout class="QHBoxLayout" name="horizontalLayout_2">
83+
<property name="leftMargin">
84+
<number>0</number>
85+
</property>
86+
<property name="topMargin">
87+
<number>0</number>
88+
</property>
89+
<property name="rightMargin">
90+
<number>0</number>
91+
</property>
92+
<property name="bottomMargin">
93+
<number>0</number>
94+
</property>
95+
<item>
96+
<widget class="QgsAttributeTableView" name="mAttributeTable"/>
7597
</item>
7698
</layout>
7799
</widget>
@@ -92,10 +114,15 @@
92114
<customwidgets>
93115
<customwidget>
94116
<class>QgsMapCanvas</class>
95-
<extends>QWidget</extends>
117+
<extends>QGraphicsView</extends>
96118
<header>qgsmapcanvas.h</header>
97119
<container>1</container>
98120
</customwidget>
121+
<customwidget>
122+
<class>QgsAttributeTableView</class>
123+
<extends>QTableView</extends>
124+
<header>qgsattributetableview.h</header>
125+
</customwidget>
99126
</customwidgets>
100127
<resources/>
101128
<connections/>

0 commit comments

Comments
 (0)