Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #2173 from cedi4155476/dbmanager-geopackage_2
DBManager: Drag&Drop support for GeoPackage layers
  • Loading branch information
pka committed Jun 29, 2015
2 parents 355fc91 + 4420b3b commit 8b2bbcd
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 25 deletions.
67 changes: 45 additions & 22 deletions python/plugins/db_manager/db_model.py
Expand Up @@ -21,7 +21,7 @@
"""

from PyQt4.QtCore import Qt, QObject, SIGNAL, qDebug, QByteArray, QMimeData, QDataStream, QIODevice, QFileInfo, \
QAbstractItemModel, QModelIndex
QAbstractItemModel, QModelIndex, QSettings
from PyQt4.QtGui import QApplication, QIcon, QMessageBox

from .db_plugins import supportedDbTypes, createDbPlugin
Expand Down Expand Up @@ -359,6 +359,10 @@ def flags(self, index):

if index.column() == 0:
item = index.internalPointer()

if not isinstance(item, SchemaItem) and not isinstance(item, TableItem):
flags |= Qt.ItemIsDropEnabled

if isinstance(item, SchemaItem) or isinstance(item, TableItem):
flags |= Qt.ItemIsEditable

Expand Down Expand Up @@ -509,31 +513,50 @@ def dropMimeData(self, data, action, row, column, parent):
added = 0

if data.hasUrls():
for u in data.urls():
filename = u.toLocalFile()
if filename == "":
continue

if qgis.core.QgsRasterLayer.isValidRasterFileName(filename):
layerType = 'raster'
providerKey = 'gdal'
else:
layerType = 'vector'
providerKey = 'ogr'

layerName = QFileInfo(filename).completeBaseName()

if self.importLayer(layerType, providerKey, layerName, filename, parent):
added += 1

if data.hasFormat(self.QGIS_URI_MIME):
for uri in qgis.core.QgsMimeDataUtils.decodeUriList(data):
if self.importLayer(uri.layerType, uri.providerKey, uri.name, uri.uri, parent):
added += 1
if row == -1 and column == -1:
for u in data.urls():
filename = u.toLocalFile()
if self.addConnection(filename, parent):
added += 1
else:
for u in data.urls():
filename = u.toLocalFile()
if filename == "":
continue

if qgis.core.QgsRasterLayer.isValidRasterFileName(filename):
layerType = 'raster'
providerKey = 'gdal'
else:
layerType = 'vector'
providerKey = 'ogr'

layerName = QFileInfo(filename).completeBaseName()

if self.importLayer(layerType, providerKey, layerName, filename, parent):
added += 1

if data.hasFormat(self.QGIS_URI_MIME):
for uri in qgis.core.QgsMimeDataUtils.decodeUriList(data):
if self.importLayer(uri.layerType, uri.providerKey, uri.name, uri.uri, parent):
added += 1

return added > 0


def addConnection(self, filename, index):
file = filename.split("/")[-1]
if filename == "":
return False
s = QSettings()
connKey = index.internalPointer().getItemData().connectionSettingsKey()
conn = index.internalPointer().getItemData().connectionSettingsFileKey()
s.beginGroup("/%s/%s" % (connKey, file))
s.setValue(conn, filename)
self.treeView.setCurrentIndex(index)
self.treeView.mainWindow.refreshActionSlot()
return True

def importLayer(self, layerType, providerKey, layerName, uriString, parent):
if not self.isImportVectorAvail:
return False
Expand Down
8 changes: 8 additions & 0 deletions python/plugins/db_manager/db_plugins/plugin.py
Expand Up @@ -132,6 +132,11 @@ def connectionSettingsKey(self):
# return the key used to store the connections in settings
pass

@classmethod
def connectionSettingsFileKey(self):
# return the filekey for the settings
pass

@classmethod
def connections(self):
# get the list of connections
Expand Down Expand Up @@ -632,6 +637,9 @@ def uri(self):

def mimeUri(self):
layerType = "raster" if self.type == Table.RasterType else "vector"
if self.database().connector.isgpkg():
url = str(self.database().connector._connectionInfo() + "|layername=" + self.name)
return u"%s:%s:%s:%s" % (layerType, "ogr", self.name, url)
return u"%s:%s:%s:%s" % (layerType, self.database().dbplugin().providerName(), self.name, self.uri().uri())

def toMapLayer(self):
Expand Down
4 changes: 4 additions & 0 deletions python/plugins/db_manager/db_plugins/postgis/plugin.py
Expand Up @@ -62,6 +62,10 @@ def providerName(self):
def connectionSettingsKey(self):
return '/PostgreSQL/connections'

@classmethod
def connectionSettingsFileKey(self):
return "database"

def databasesFactory(self, connection, uri):
return PGDatabase(connection, uri)

Expand Down
9 changes: 9 additions & 0 deletions python/plugins/db_manager/db_plugins/spatialite/connector.py
Expand Up @@ -125,6 +125,15 @@ def hasTableColumnEditingSupport(self):
def hasCreateSpatialViewSupport(self):
return True

def isgpkg(self):
info = float(self.getInfo()[0][:-2])
if info < 4.2:
result = self.uri().database()[-5:] == ".gpkg"
else:
sql = u"SELECT HasGeoPackage()"
result = self._execute(None, sql).fetchone()[0] == 1
return result

def fieldTypes(self):
return [
"integer", "bigint", "smallint", # integers
Expand Down
6 changes: 5 additions & 1 deletion python/plugins/db_manager/db_plugins/spatialite/plugin.py
Expand Up @@ -51,7 +51,7 @@ def typeName(self):

@classmethod
def typeNameString(self):
return 'SpatiaLite'
return 'SpatiaLite/Geopackage'

@classmethod
def providerName(self):
Expand All @@ -61,6 +61,10 @@ def providerName(self):
def connectionSettingsKey(self):
return '/SpatiaLite/connections'

@classmethod
def connectionSettingsFileKey(self):
return "sqlitepath"

def databasesFactory(self, connection, uri):
return SLDatabase(connection, uri)

Expand Down
31 changes: 29 additions & 2 deletions python/plugins/db_manager/db_tree.py
Expand Up @@ -20,8 +20,8 @@
***************************************************************************/
"""

from PyQt4.QtCore import SIGNAL, SLOT
from PyQt4.QtGui import QWidget, QTreeView, QMenu, QLabel
from PyQt4.QtCore import SIGNAL, SLOT, QSettings, Qt
from PyQt4.QtGui import QWidget, QTreeView, QMenu, QLabel, QFileDialog

from qgis.core import QgsMapLayerRegistry, QgsMessageLog
from qgis.gui import QgsMessageBar, QgsMessageBarItem
Expand Down Expand Up @@ -90,6 +90,12 @@ def currentTable(self):
return item
return None

def openConnection(self):
index = self.selectedIndexes()[0]
if index:
if index.data() != "PostGIS":
filename = QFileDialog.getOpenFileName(self, "Open File")
self.model().addConnection(filename, index)

def itemChanged(self, index):
self.setCurrentIndex(index)
Expand Down Expand Up @@ -123,6 +129,10 @@ def contextMenuEvent(self, ev):

elif isinstance(item, DBPlugin) and item.database() is not None:
menu.addAction(self.tr("Re-connect"), self.reconnect)
menu.addAction(self.tr("Delete"), self.delActionSlot)

elif not index.parent().data():
menu.addAction(self.tr("New Connection..."), self.openConnection)

if not menu.isEmpty():
menu.exec_(ev.globalPos())
Expand All @@ -135,6 +145,23 @@ def rename(self):
if isinstance(item, (Table, Schema)):
self.edit(index)

def delActionSlot(self):
db = self.currentDatabase()
path = db.uri().database()
connkey = db.connection().connectionSettingsKey()
self.deletedb(path, connkey)

index = self.currentIndex().parent()
self.setCurrentIndex(index)
self.mainWindow.refreshActionSlot()

def deletedb(self, path, conn):
paths = path.split("/")
path = paths[-1]
s = QSettings()
s.beginGroup("/%s/%s" % (conn, path))
s.remove("")

def delete(self):
item = self.currentItem()
if isinstance(item, (Table, Schema)):
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions python/plugins/db_manager/resources.qrc
Expand Up @@ -27,5 +27,6 @@
<file alias="create_table">icons/toolbar/action_new_table.png</file>
<file alias="refresh">icons/toolbar/action_refresh.png</file>
<file alias="sql_window">icons/toolbar/action_sql_window.png</file>
<file alias="delete">icons/toolbar/action_delete.png</file>
</qresource>
</RCC>

0 comments on commit 8b2bbcd

Please sign in to comment.