Skip to content

Commit 8b2bbcd

Browse files
committed
Merge pull request #2173 from cedi4155476/dbmanager-geopackage_2
DBManager: Drag&Drop support for GeoPackage layers
2 parents 355fc91 + 4420b3b commit 8b2bbcd

File tree

8 files changed

+101
-25
lines changed

8 files changed

+101
-25
lines changed

python/plugins/db_manager/db_model.py

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"""
2222

2323
from PyQt4.QtCore import Qt, QObject, SIGNAL, qDebug, QByteArray, QMimeData, QDataStream, QIODevice, QFileInfo, \
24-
QAbstractItemModel, QModelIndex
24+
QAbstractItemModel, QModelIndex, QSettings
2525
from PyQt4.QtGui import QApplication, QIcon, QMessageBox
2626

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

360360
if index.column() == 0:
361361
item = index.internalPointer()
362+
363+
if not isinstance(item, SchemaItem) and not isinstance(item, TableItem):
364+
flags |= Qt.ItemIsDropEnabled
365+
362366
if isinstance(item, SchemaItem) or isinstance(item, TableItem):
363367
flags |= Qt.ItemIsEditable
364368

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

511515
if data.hasUrls():
512-
for u in data.urls():
513-
filename = u.toLocalFile()
514-
if filename == "":
515-
continue
516-
517-
if qgis.core.QgsRasterLayer.isValidRasterFileName(filename):
518-
layerType = 'raster'
519-
providerKey = 'gdal'
520-
else:
521-
layerType = 'vector'
522-
providerKey = 'ogr'
523-
524-
layerName = QFileInfo(filename).completeBaseName()
525-
526-
if self.importLayer(layerType, providerKey, layerName, filename, parent):
527-
added += 1
528-
529-
if data.hasFormat(self.QGIS_URI_MIME):
530-
for uri in qgis.core.QgsMimeDataUtils.decodeUriList(data):
531-
if self.importLayer(uri.layerType, uri.providerKey, uri.name, uri.uri, parent):
532-
added += 1
516+
if row == -1 and column == -1:
517+
for u in data.urls():
518+
filename = u.toLocalFile()
519+
if self.addConnection(filename, parent):
520+
added += 1
521+
else:
522+
for u in data.urls():
523+
filename = u.toLocalFile()
524+
if filename == "":
525+
continue
526+
527+
if qgis.core.QgsRasterLayer.isValidRasterFileName(filename):
528+
layerType = 'raster'
529+
providerKey = 'gdal'
530+
else:
531+
layerType = 'vector'
532+
providerKey = 'ogr'
533+
534+
layerName = QFileInfo(filename).completeBaseName()
535+
536+
if self.importLayer(layerType, providerKey, layerName, filename, parent):
537+
added += 1
538+
539+
if data.hasFormat(self.QGIS_URI_MIME):
540+
for uri in qgis.core.QgsMimeDataUtils.decodeUriList(data):
541+
if self.importLayer(uri.layerType, uri.providerKey, uri.name, uri.uri, parent):
542+
added += 1
533543

534544
return added > 0
535545

536546

547+
def addConnection(self, filename, index):
548+
file = filename.split("/")[-1]
549+
if filename == "":
550+
return False
551+
s = QSettings()
552+
connKey = index.internalPointer().getItemData().connectionSettingsKey()
553+
conn = index.internalPointer().getItemData().connectionSettingsFileKey()
554+
s.beginGroup("/%s/%s" % (connKey, file))
555+
s.setValue(conn, filename)
556+
self.treeView.setCurrentIndex(index)
557+
self.treeView.mainWindow.refreshActionSlot()
558+
return True
559+
537560
def importLayer(self, layerType, providerKey, layerName, uriString, parent):
538561
if not self.isImportVectorAvail:
539562
return False

python/plugins/db_manager/db_plugins/plugin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ def connectionSettingsKey(self):
132132
# return the key used to store the connections in settings
133133
pass
134134

135+
@classmethod
136+
def connectionSettingsFileKey(self):
137+
# return the filekey for the settings
138+
pass
139+
135140
@classmethod
136141
def connections(self):
137142
# get the list of connections
@@ -632,6 +637,9 @@ def uri(self):
632637

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

637645
def toMapLayer(self):

python/plugins/db_manager/db_plugins/postgis/plugin.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ def providerName(self):
6262
def connectionSettingsKey(self):
6363
return '/PostgreSQL/connections'
6464

65+
@classmethod
66+
def connectionSettingsFileKey(self):
67+
return "database"
68+
6569
def databasesFactory(self, connection, uri):
6670
return PGDatabase(connection, uri)
6771

python/plugins/db_manager/db_plugins/spatialite/connector.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ def hasTableColumnEditingSupport(self):
125125
def hasCreateSpatialViewSupport(self):
126126
return True
127127

128+
def isgpkg(self):
129+
info = float(self.getInfo()[0][:-2])
130+
if info < 4.2:
131+
result = self.uri().database()[-5:] == ".gpkg"
132+
else:
133+
sql = u"SELECT HasGeoPackage()"
134+
result = self._execute(None, sql).fetchone()[0] == 1
135+
return result
136+
128137
def fieldTypes(self):
129138
return [
130139
"integer", "bigint", "smallint", # integers

python/plugins/db_manager/db_plugins/spatialite/plugin.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def typeName(self):
5151

5252
@classmethod
5353
def typeNameString(self):
54-
return 'SpatiaLite'
54+
return 'SpatiaLite/Geopackage'
5555

5656
@classmethod
5757
def providerName(self):
@@ -61,6 +61,10 @@ def providerName(self):
6161
def connectionSettingsKey(self):
6262
return '/SpatiaLite/connections'
6363

64+
@classmethod
65+
def connectionSettingsFileKey(self):
66+
return "sqlitepath"
67+
6468
def databasesFactory(self, connection, uri):
6569
return SLDatabase(connection, uri)
6670

python/plugins/db_manager/db_tree.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
***************************************************************************/
2121
"""
2222

23-
from PyQt4.QtCore import SIGNAL, SLOT
24-
from PyQt4.QtGui import QWidget, QTreeView, QMenu, QLabel
23+
from PyQt4.QtCore import SIGNAL, SLOT, QSettings, Qt
24+
from PyQt4.QtGui import QWidget, QTreeView, QMenu, QLabel, QFileDialog
2525

2626
from qgis.core import QgsMapLayerRegistry, QgsMessageLog
2727
from qgis.gui import QgsMessageBar, QgsMessageBarItem
@@ -90,6 +90,12 @@ def currentTable(self):
9090
return item
9191
return None
9292

93+
def openConnection(self):
94+
index = self.selectedIndexes()[0]
95+
if index:
96+
if index.data() != "PostGIS":
97+
filename = QFileDialog.getOpenFileName(self, "Open File")
98+
self.model().addConnection(filename, index)
9399

94100
def itemChanged(self, index):
95101
self.setCurrentIndex(index)
@@ -123,6 +129,10 @@ def contextMenuEvent(self, ev):
123129

124130
elif isinstance(item, DBPlugin) and item.database() is not None:
125131
menu.addAction(self.tr("Re-connect"), self.reconnect)
132+
menu.addAction(self.tr("Delete"), self.delActionSlot)
133+
134+
elif not index.parent().data():
135+
menu.addAction(self.tr("New Connection..."), self.openConnection)
126136

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

148+
def delActionSlot(self):
149+
db = self.currentDatabase()
150+
path = db.uri().database()
151+
connkey = db.connection().connectionSettingsKey()
152+
self.deletedb(path, connkey)
153+
154+
index = self.currentIndex().parent()
155+
self.setCurrentIndex(index)
156+
self.mainWindow.refreshActionSlot()
157+
158+
def deletedb(self, path, conn):
159+
paths = path.split("/")
160+
path = paths[-1]
161+
s = QSettings()
162+
s.beginGroup("/%s/%s" % (conn, path))
163+
s.remove("")
164+
138165
def delete(self):
139166
item = self.currentItem()
140167
if isinstance(item, (Table, Schema)):
Loading

python/plugins/db_manager/resources.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@
2727
<file alias="create_table">icons/toolbar/action_new_table.png</file>
2828
<file alias="refresh">icons/toolbar/action_refresh.png</file>
2929
<file alias="sql_window">icons/toolbar/action_sql_window.png</file>
30+
<file alias="delete">icons/toolbar/action_delete.png</file>
3031
</qresource>
3132
</RCC>

0 commit comments

Comments
 (0)