mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-17 00:04:02 -04:00
Drag&Drop support for GeoPackage layers
This commit is contained in:
parent
16ad2f82d4
commit
4420b3bf99
@ -21,7 +21,7 @@ email : brush.tyler@gmail.com
|
||||
"""
|
||||
|
||||
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
|
||||
@ -359,6 +359,10 @@ class DBModel(QAbstractItemModel):
|
||||
|
||||
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
|
||||
|
||||
@ -509,6 +513,12 @@ class DBModel(QAbstractItemModel):
|
||||
added = 0
|
||||
|
||||
if data.hasUrls():
|
||||
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 == "":
|
||||
@ -534,6 +544,19 @@ class DBModel(QAbstractItemModel):
|
||||
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
|
||||
|
@ -132,6 +132,11 @@ class DBPlugin(QObject):
|
||||
# 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
|
||||
@ -632,6 +637,9 @@ class Table(DbItemObject):
|
||||
|
||||
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):
|
||||
|
@ -62,6 +62,10 @@ class PostGisDBPlugin(DBPlugin):
|
||||
def connectionSettingsKey(self):
|
||||
return '/PostgreSQL/connections'
|
||||
|
||||
@classmethod
|
||||
def connectionSettingsFileKey(self):
|
||||
return "database"
|
||||
|
||||
def databasesFactory(self, connection, uri):
|
||||
return PGDatabase(connection, uri)
|
||||
|
||||
|
@ -125,6 +125,15 @@ class SpatiaLiteDBConnector(DBConnector):
|
||||
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
|
||||
|
@ -51,7 +51,7 @@ class SpatiaLiteDBPlugin(DBPlugin):
|
||||
|
||||
@classmethod
|
||||
def typeNameString(self):
|
||||
return 'SpatiaLite'
|
||||
return 'SpatiaLite/Geopackage'
|
||||
|
||||
@classmethod
|
||||
def providerName(self):
|
||||
@ -61,6 +61,10 @@ class SpatiaLiteDBPlugin(DBPlugin):
|
||||
def connectionSettingsKey(self):
|
||||
return '/SpatiaLite/connections'
|
||||
|
||||
@classmethod
|
||||
def connectionSettingsFileKey(self):
|
||||
return "sqlitepath"
|
||||
|
||||
def databasesFactory(self, connection, uri):
|
||||
return SLDatabase(connection, uri)
|
||||
|
||||
|
@ -20,8 +20,8 @@ email : brush.tyler@gmail.com
|
||||
***************************************************************************/
|
||||
"""
|
||||
|
||||
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
|
||||
@ -90,6 +90,12 @@ class DBTree(QTreeView):
|
||||
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)
|
||||
@ -123,6 +129,10 @@ class DBTree(QTreeView):
|
||||
|
||||
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())
|
||||
@ -135,6 +145,23 @@ class DBTree(QTreeView):
|
||||
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)):
|
||||
|
BIN
python/plugins/db_manager/icons/toolbar/action_delete.png
Normal file
BIN
python/plugins/db_manager/icons/toolbar/action_delete.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 KiB |
@ -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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user