-
Notifications
You must be signed in to change notification settings - Fork 13
/
loaderrordialog.py
89 lines (72 loc) · 3.02 KB
/
loaderrordialog.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
"""
/***************************************************************************
* *
* 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.QtCore import qDebug, Qt
from PyQt5.QtWidgets import QApplication, QDialog, QFileDialog, QMessageBox
from qgis.core import QgsProject
from . import utils
from .ui_loaderrordialog import Ui_LoadError
class LoadErrorDialog(QDialog, Ui_LoadError):
def __init__(self, filepath):
QDialog.__init__(self)
self.setupUi(self)
self.lblError.setText("File '%s' not found." % filepath)
QApplication.setOverrideCursor(Qt.ArrowCursor)
self.pushButtonBrowse.clicked.connect(self.showBrowserDialog)
def clear(self):
self.lineEditImagePath.setText("")
def showBrowserDialog(self):
bDir, found = QgsProject.instance().readEntry(
utils.SETTINGS_KEY, utils.SETTING_BROWSER_RASTER_DIR, None
)
if not found or not os.path.isdir(bDir):
bDir = os.path.expanduser("~")
qDebug(bDir.encode())
filepath, _ = QFileDialog.getOpenFileName(
self, "Select image", bDir, "Images (*.png *.bmp *.jpg *.tif *.tiff *.pdf)"
)
self.lineEditImagePath.setText(filepath)
if filepath:
bDir, _ = os.path.split(filepath)
QgsProject.instance().writeEntry(
utils.SETTINGS_KEY, utils.SETTING_BROWSER_RASTER_DIR, bDir
)
def done(self, ack):
QApplication.restoreOverrideCursor()
super(LoadErrorDialog, self).done(ack)
def accept(self):
result, message, details = self.validate()
if result:
self.done(QDialog.Accepted)
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