-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
149 lines (126 loc) · 4.66 KB
/
util.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# -*- coding: utf-8 -*-
#***********************************************************************
#
# Image Analysis
# ----------------------------------------------------------------------
# Utility functions and base classes
#
# Vitor Hirota (vitor.hirota [at] gmail.com), INPE 2013
#
# This source 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.
#
# This code is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# A copy of the GNU General Public License is available on the World
# Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also
# obtain it by writing to the Free Software Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA.
#
#***********************************************************************
from PyQt4 import QtCore, QtGui
# from qgis.core import *
from qgis.core import QgsMapLayer, QgsMessageLog, QgsVectorLayer
from qgis.gui import QgsMessageBar
def error_handler(run_fn):
def wrapped(self, *args, **kwargs):
try:
self.log.emit('worker: run started')
run_fn(self, *args, **kwargs)
except:
import traceback
self.error.emit(traceback.format_exc())
error_msg = 'Failed. Please check the logs for details.'
self.finished.emit(False, error_msg)
else:
self.log.emit('worker: completed successfully')
self.finished.emit(True, self.output)
return wrapped
class Task(QtCore.QObject):
def __init__(self, parent, *args):
self.parent = parent
self.valid = True
self.worker = None
self.setup(*args)
def is_valid(self):
return self.valid
def run(self):
# move worker to a new thread
self.thread = QtCore.QThread()
self.worker.moveToThread(self.thread)
self.thread.started.connect(self.worker.run)
# setup signals
self.worker.log.connect(self.parent.log)
self.worker.status.connect(self.status)
self.worker.progress.connect(self.progress)
self.worker.error.connect(self.error)
self.worker.finished.connect(self.finish)
# fire thread
try:
self.thread.start()
self.thread.exec_()
except:
import traceback
self.parent.log(traceback.format_exc())
def clear_message_bar(self, msg, level=QgsMessageBar.INFO, duration=-1):
mb = self.parent.iface.messageBar()
mb.popWidget(self.parent.messageBar)
mb.pushMessage(self.parent.action, msg, level=level, duration=duration)
def post_run(self):
self.completed = 'Task completed.'
def status(self, msg=''):
self.parent.log(msg)
self.parent.messageBar.setText(msg)
def progress(self, value):
self.parent.progressBar.setValue(value)
pb = self.parent.messageBar.findChildren(QtGui.QProgressBar)[0]
pb.setValue(value)
def error(self, msg):
self.parent.log(msg, level='crit')
self.worker.kill()
def kill(self):
self.parent.log('worker: killing')
self.worker.kill()
self.parent.ok_btn.setEnabled(True)
self.progress(0)
def finish(self, success, output):
self.progress(0)
# post run
if success:
self.parent.log('post run')
self.post_run(output)
self.clear_message_bar(self.completed, duration=10)
else:
self.clear_message_bar(output, level=QgsMessageBar.CRITICAL,)
# update gui
self.parent.ok_btn.setEnabled(True)
# clean up task, worker and thread
self.worker.deleteLater()
self.thread.quit()
self.thread.wait()
self.thread.deleteLater()
# self.deleteLater()
class Worker(QtCore.QObject):
log = QtCore.pyqtSignal(str)
status = QtCore.pyqtSignal(str)
progress = QtCore.pyqtSignal(int)
error = QtCore.pyqtSignal(str)
# killed = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal(bool, str)
def __init__(self):
QtCore.QObject.__init__(self)
self.abort = False
self.output = None
def setup(self, *args, **kwargs):
pass
def run(self):
pass
def kill(self):
self.abort = True
def calculate_progress(self, step, total_steps, offset, weight):
self.progress.emit(offset + step * weight / total_steps)