forked from gvellut/FreehandRasterGeoreferencer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreehandrastergeoreferencerdialog.py
121 lines (96 loc) · 3.92 KB
/
freehandrastergeoreferencerdialog.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
import os.path
from PyQt5.QtWidgets import QAction, QDialog, QFileDialog, QMenu, QMessageBox
from qgis.core import QgsProject
from . import utils
from .ui_freehandrastergeoreferencer import Ui_FreehandRasterGeoreferencer
class FreehandRasterGeoreferencerDialog(QDialog, Ui_FreehandRasterGeoreferencer):
REPLACE = 2
DUPLICATE = 3
def __init__(self):
QDialog.__init__(self)
self.setupUi(self)
self.configureAdvancedMenu()
self.pushButtonAdd.clicked.connect(self.addNew)
self.pushButtonCancel.clicked.connect(self.reject)
self.pushButtonBrowse.clicked.connect(self.showBrowserDialog)
self.toolButtonAdvanced.clicked.connect(self.showAdvancedMenu)
def clear(self, layer):
self.layer = layer
if layer is None:
imagepath = ""
else:
imagepath = layer.filepath
self.lineEditImagePath.setText(imagepath)
def showBrowserDialog(self):
bDir, found = QgsProject.instance().readEntry(
utils.SETTINGS_KEY, utils.SETTING_BROWSER_RASTER_DIR, None
)
if not found:
bDir = os.path.expanduser("~")
filepath, _ = QFileDialog.getOpenFileName(
self, "Select image", bDir, "Images (*.png *.bmp *.jpg *.tif *.tiff *.pdf)"
)
if filepath:
self.lineEditImagePath.setText(filepath)
bDir, _ = os.path.split(filepath)
QgsProject.instance().writeEntry(
utils.SETTINGS_KEY, utils.SETTING_BROWSER_RASTER_DIR, bDir
)
def configureAdvancedMenu(self):
action1 = QAction("Replace image for selected layer", self)
action2 = QAction("Duplicate selected layer", self)
action1.triggered.connect(self.replaceImage)
action2.triggered.connect(self.duplicateLayer)
menu = QMenu(self)
menu.addAction(action1)
menu.addAction(action2)
self.toolButtonAdvanced.setMenu(menu)
def showAdvancedMenu(self):
self.toolButtonAdvanced.showMenu()
def replaceImage(self):
self.accept(self.REPLACE)
def duplicateLayer(self):
self.accept(self.DUPLICATE, False)
def addNew(self):
self.accept()
def accept(self, retValue=QDialog.Accepted, validate=True):
if not validate:
self.done(retValue)
return
result, message, details = self.validate()
if result:
self.done(retValue)
else:
msgBox = QMessageBox()
msgBox.setWindowTitle("Error")
msgBox.setText(message)
msgBox.setDetailedText(details)
msgBox.setStandardButtons(QMessageBox.Ok)
msgBox.exec_()
def validate(self):
result = True
message = ""
details = ""
self.imagePath = self.lineEditImagePath.text()
_, extension = os.path.splitext(self.imagePath)
extension = extension.lower()
if not os.path.isfile(self.imagePath) or (
extension not in [".jpg", ".bmp", ".png", ".tif", ".tiff", ".pdf"]
):
result = False
if len(details) > 0:
details += "\n"
details += "The path must be an image file"
if not result:
message = "There were errors in the form"
return result, message, details