attributes_viewer.py

Luiz Andrade, 2015-11-11 05:48 AM

Download (3.18 KB)

 
1
# -*- coding: utf-8 -*-
2
"""
3
/***************************************************************************
4
 DsgTools
5
                                 A QGIS plugin
6
 Brazilian Army Cartographic Production Tools
7
                             -------------------
8
        begin                : 2015-11-10
9
        git sha              : $Format:%H$
10
        copyright            : (C) 2015 by Brazilian Army - Geographic Service Bureau
11
        email                : [email protected]
12
 ***************************************************************************/
13

14
/***************************************************************************
15
 *                                                                         *
16
 *   This program is free software; you can redistribute it and/or modify  *
17
 *   it under the terms of the GNU General Public License as published by  *
18
 *   the Free Software Foundation; either version 2 of the License, or     *
19
 *   (at your option) any later version.                                   *
20
 *                                                                         *
21
 ***************************************************************************/
22
"""
23
import os
24

    
25
# Qt imports
26
from PyQt4 import QtGui, uic, QtCore
27
from PyQt4.QtCore import pyqtSlot, pyqtSignal
28
from PyQt4.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel
29
from PyQt4.QtGui import QTableView
30

    
31
# QGIS imports
32
from qgis.core import QgsMapLayer, QgsDataSourceURI, QgsVectorLayerCache
33
from qgis.gui import QgsAttributeDialog, QgsAttributeTableModel, QgsAttributeTableView, QgsAttributeTableFilterModel
34

    
35
FORM_CLASS, _ = uic.loadUiType(os.path.join(
36
    os.path.dirname(__file__), 'attributes_viewer.ui'))
37

    
38
class AttributesViewer(QtGui.QDockWidget, FORM_CLASS):
39
    def __init__(self, iface, parent = None):
40
        """Constructor."""
41
        super(AttributesViewer, self).__init__(parent)
42
        # Set up the user interface from Designer.
43
        # After setupUI you can access any designer object by doing
44
        # self.<objectname>, and you can use autoconnect slots - see
45
        # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
46
        # #widgets-and-dialogs-with-auto-connect
47
        self.setupUi(self)
48
        
49
        self.iface = iface
50
        
51
        self.populateLayers()
52
        
53
    def populateLayers(self):
54
        self.layerCombo.clear()
55
        
56
        self.layerCombo.addItem(self.tr('Select a Layer'))
57
        
58
        layers = self.iface.mapCanvas().layers()
59
        for layer in layers:
60
            if layer.type() == QgsMapLayer.VectorLayer:
61
                self.layerCombo.addItem(layer.name())
62
        
63
    @pyqtSlot(int)
64
    def on_layerCombo_currentIndexChanged(self):
65
        if self.layerCombo.currentIndex() == 0:
66
            return
67
        
68
        currentLayerName = self.layerCombo.currentText()
69
        
70
        currentLayer = None
71
        layers = self.iface.mapCanvas().layers()
72
        for layer in layers:
73
            if layer.name() == currentLayerName:
74
                currentLayer = layer
75
                break
76
            
77
        cache = QgsVectorLayerCache(currentLayer, 10)
78
        model = QgsAttributeTableModel(cache)
79
        model.loadLayer()
80
        
81
        self.tableView.setModel(model)