2012-04-16 13:19:40 +02:00
# -*- coding: utf-8 -*-
"""
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Name : DB Manager
2013-06-09 18:28:52 +02:00
Description : Database manager plugin for QGIS
2012-04-16 13:19:40 +02:00
Date : May 23 , 2011
copyright : ( C ) 2011 by Giuseppe Sucameli
email : brush . tyler @gmail.com
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* This program is free software ; you can redistribute it and / or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation ; either version 2 of the License , or *
* ( at your option ) any later version . *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
"""
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
from PyQt4 . QtCore import SIGNAL , SLOT
from PyQt4 . QtGui import QWidget , QTreeView , QMenu , QLabel
2012-04-16 13:19:40 +02:00
2014-10-28 23:38:15 +01:00
from qgis . core import QgsMapLayerRegistry , QgsMessageLog
from qgis . gui import QgsMessageBar , QgsMessageBarItem
2012-04-16 13:19:40 +02:00
from . db_model import DBModel
from . db_plugins . plugin import DBPlugin , Schema , Table
class DBTree ( QTreeView ) :
2012-12-24 11:48:08 +02:00
def __init__ ( self , mainWindow ) :
QTreeView . __init__ ( self , mainWindow )
self . mainWindow = mainWindow
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
self . setModel ( DBModel ( self ) )
self . setHeaderHidden ( True )
self . setEditTriggers ( QTreeView . EditKeyPressed | QTreeView . SelectedClicked )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
self . setDragEnabled ( True )
self . setAcceptDrops ( True )
self . setDropIndicatorShown ( True )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
self . connect ( self . selectionModel ( ) , SIGNAL ( " currentChanged(const QModelIndex&, const QModelIndex&) " ) , self . currentItemChanged )
self . connect ( self , SIGNAL ( " expanded(const QModelIndex&) " ) , self . itemChanged )
self . connect ( self , SIGNAL ( " collapsed(const QModelIndex&) " ) , self . itemChanged )
self . connect ( self . model ( ) , SIGNAL ( " dataChanged(const QModelIndex&, const QModelIndex&) " ) , self . modelDataChanged )
self . connect ( self . model ( ) , SIGNAL ( " notPopulated " ) , self . collapse )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
def refreshItem ( self , item = None ) :
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
if item is None :
2012-12-24 11:48:08 +02:00
item = self . currentItem ( )
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
if item is None : return
2012-12-24 11:48:08 +02:00
self . model ( ) . refreshItem ( item )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
def showSystemTables ( self , show ) :
pass
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
def currentItem ( self ) :
indexes = self . selectedIndexes ( )
if len ( indexes ) < = 0 :
return
return self . model ( ) . getItem ( indexes [ 0 ] )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
def currentDatabase ( self ) :
item = self . currentItem ( )
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
if item is None : return
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
if isinstance ( item , ( DBPlugin , Schema , Table ) ) :
return item . database ( )
return None
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
def currentSchema ( self ) :
item = self . currentItem ( )
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
if item is None : return
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
if isinstance ( item , ( Schema , Table ) ) :
return item . schema ( )
return None
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
def currentTable ( self ) :
item = self . currentItem ( )
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
if item is None : return
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
if isinstance ( item , Table ) :
return item
return None
2012-12-10 00:12:07 +01:00
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
def itemChanged ( self , index ) :
self . setCurrentIndex ( index )
self . emit ( SIGNAL ( ' selectedItemChanged ' ) , self . currentItem ( ) )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
def modelDataChanged ( self , indexFrom , indexTo ) :
self . itemChanged ( indexTo )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
def currentItemChanged ( self , current , previous ) :
self . itemChanged ( current )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
def contextMenuEvent ( self , ev ) :
index = self . indexAt ( ev . pos ( ) )
if not index . isValid ( ) :
return
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
if index != self . currentIndex ( ) :
self . itemChanged ( index )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
item = self . currentItem ( )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
menu = QMenu ( self )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
if isinstance ( item , ( Table , Schema ) ) :
2013-06-09 18:28:52 +02:00
menu . addAction ( self . tr ( " Rename " ) , self . rename )
menu . addAction ( self . tr ( " Delete " ) , self . delete )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
if isinstance ( item , Table ) :
menu . addSeparator ( )
2013-06-09 20:37:53 +02:00
menu . addAction ( self . tr ( " Add to canvas " ) , self . addLayer )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
elif isinstance ( item , DBPlugin ) and item . database ( ) is not None :
2013-06-09 18:28:52 +02:00
menu . addAction ( self . tr ( " Re-connect " ) , self . reconnect )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
if not menu . isEmpty ( ) :
menu . exec_ ( ev . globalPos ( ) )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
menu . deleteLater ( )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
def rename ( self ) :
index = self . currentIndex ( )
item = self . model ( ) . getItem ( index )
if isinstance ( item , ( Table , Schema ) ) :
self . edit ( index )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
def delete ( self ) :
item = self . currentItem ( )
if isinstance ( item , ( Table , Schema ) ) :
self . mainWindow . invokeCallback ( item . database ( ) . deleteActionSlot )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
def addLayer ( self ) :
table = self . currentTable ( )
if table is not None :
2014-10-28 23:38:15 +01:00
layer = table . toMapLayer ( )
layers = QgsMapLayerRegistry . instance ( ) . addMapLayers ( [ layer ] )
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
if len ( layers ) != 1 :
2014-10-28 23:38:15 +01:00
QgsMessageLog . instance ( ) . logMessage ( self . tr ( " % 1 is an invalid layer - not loaded " ) . replace ( " % 1 " , layer . publicSource ( ) ) )
msgLabel = QLabel ( self . tr ( " % 1 is an invalid layer and cannot be loaded. Please check the <a href= \" #messageLog \" >message log</a> for further info. " ) . replace ( " % 1 " , layer . publicSource ( ) ) , self . mainWindow . infoBar )
msgLabel . setWordWrap ( True )
self . connect ( msgLabel , SIGNAL ( " linkActivated( QString ) " ) ,
self . mainWindow . iface . mainWindow ( ) . findChild ( QWidget , " MessageLog " ) , SLOT ( " show() " ) )
self . connect ( msgLabel , SIGNAL ( " linkActivated( QString ) " ) ,
self . mainWindow . iface . mainWindow ( ) , SLOT ( " raise() " ) )
self . mainWindow . infoBar . pushItem ( QgsMessageBarItem ( msgLabel , QgsMessageBar . WARNING ) )
2012-04-16 13:19:40 +02:00
2012-12-24 11:48:08 +02:00
def reconnect ( self ) :
db = self . currentDatabase ( )
if db is not None :
self . mainWindow . invokeCallback ( db . reconnectActionSlot )