forked from enricofer/changeDataSource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
changeDataSource_dialog.py
103 lines (90 loc) · 4.1 KB
/
changeDataSource_dialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
"""
/***************************************************************************
changeDataSourceDialog
A QGIS plugin
right click on layer tree to change layer datasource
-------------------
begin : 2015-09-29
git sha : $Format:%H$
copyright : (C) 2015 by enrico ferreguti
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
from __future__ import absolute_import
import os
from qgis.PyQt import QtGui, uic, QtWidgets
from qgis.PyQt.QtCore import pyqtSignal
from qgis.core import QgsBrowserModel, QgsMimeDataUtils
from .changeDataSource_dialog_base import Ui_changeDataSourceDialogBase
from .browsedatasource import Ui_dataSourceBrowser
class changeDataSourceDialog(QtWidgets.QDialog, Ui_changeDataSourceDialogBase):
def __init__(self, parent=None):
"""Constructor."""
super(changeDataSourceDialog, self).__init__(parent)
#QtWidgets.QDialog.__init__(self)
# Set up the user interface from Designer.
# After setupUI you can access any designer object by doing
# self.<objectname>, and you can use autoconnect slots - see
# http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
# #widgets-and-dialogs-with-auto-connect
self.setupUi(self)
closedDialog = pyqtSignal()
def closeEvent(self,evnt):
self.closedDialog.emit()
class dataSourceBrowser(QtWidgets.QDialog, Ui_dataSourceBrowser):
def __init__(self, parent=None):
"""Constructor."""
super(dataSourceBrowser, self).__init__(parent)
#QtWidgets.QDialog.__init__(self)
# Set up the user interface from Designer.
# After setupUI you can access any designer object by doing
# self.<objectname>, and you can use autoconnect slots - see
# http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
# #widgets-and-dialogs-with-auto-connect
self.setupUi(self)
self.browserModel = QgsBrowserModel()
self.browserModel.initialize()
self.dataSourceTree.setModel(self.browserModel)
self.dataSourceTree.doubleClicked.connect(self.getUriFromBrowser)
self.dataSourceTree.header().hide()
self.hide()
self.buttonBox.accepted.connect(self.acceptedAction)
self.buttonBox.rejected.connect(self.rejectedAction)
self.acceptedFlag = None
def getUriFromBrowser(self,index):
uriItem = self.browserModel.dataItem(index)
uri_list = QgsMimeDataUtils.decodeUriList(self.browserModel.mimeData([index]))
try:
#print uri_list[0].providerKey,uri_list[0].uri
self.result = (uri_list[0].layerType,uri_list[0].providerKey,uri_list[0].uri)
self.close()
self.acceptedFlag = True
except:
#print "NO VALID URI"
self.result = (None,None,None)
def acceptedAction(self):
self.getUriFromBrowser(self.dataSourceTree.currentIndex())
self.close()
self.acceptedFlag = True
def rejectedAction(self):
self.close()
self.acceptedFlag = None
@staticmethod
def uri(title=""):
dialog = dataSourceBrowser()
dialog.setWindowTitle(title)
result = dialog.exec_()
dialog.show()
if dialog.acceptedFlag:
return (dialog.result)
else:
return (None,None,None)