-
Notifications
You must be signed in to change notification settings - Fork 4
/
messagebarcancel.py
123 lines (101 loc) · 4.26 KB
/
messagebarcancel.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Name : MessageBar Cancel
Description : Use to add cancel in messagebar
Date : November, 2017
copyright : (C) 2017 by Luiz Motta
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. *
* *
***************************************************************************/
"""
import os
from PyQt4 import QtCore, QtGui
from qgis import core as QgsCore, gui as QgsGui
class MessageBarCancelProgress(QtCore.QObject):
def __init__(self, pluginName, msgBar, msg, maximum, funcKill, hasProgressFile=False):
def initGui():
self.pb = QtGui.QProgressBar( self.msgBar )
self.pb.setAlignment( QtCore.Qt.AlignLeft )
self.lb = QtGui.QLabel( self.msgBar )
self.tbCancel = QtGui.QToolButton( self.msgBar )
self.tbCancel.setIcon( QgsCore.QgsApplication.getThemeIcon( "/mActionCancelAllEdits.svg" ) )
self.tbCancel.setToolTip( "Cancel download")
self.tbCancel.setText( "Cancel")
self.tbCancel.setToolButtonStyle( QtCore.Qt.ToolButtonTextBesideIcon )
self.widget = self.msgBar.createMessage( pluginName, msg )
widgets = [ self.tbCancel, self.lb, self.pb ]
lyt = self.widget.layout()
for item in widgets:
lyt.addWidget( item )
del widgets[:]
if hasProgressFile:
self.pbFile = QtGui.QProgressBar( self.msgBar )
self.pbFile.setAlignment( QtCore.Qt.AlignLeft )
self.pbFile.setValue( 1 )
lyt.addWidget( self.pbFile )
super(MessageBarCancelProgress, self).__init__()
( self.msgBar, self.maximum ) = ( msgBar, maximum )
self.pb = self.lb = self.widget = self.isCancel = self.pbFile = None
initGui()
self.tbCancel.clicked.connect( self.clickedCancel )
self.pb.destroyed.connect( self.destroyed)
self.msgBar.pushWidget( self.widget, QgsGui.QgsMessageBar.INFO )
self.pb.setValue( 1 )
self.pb.setMaximum( maximum )
self.isCancel = False
self.kill = funcKill
def step(self, value, image=None):
if self.pb is None:
return
self.pb.setValue( value )
self.lb.setText( "%d/%d" % ( value, self.maximum ) )
if not image is None:
self.pbFile.setToolTip( image )
self.pbFile.setFormat( "%p% " + os.path.split( image )[-1] )
@QtCore.pyqtSlot(QtCore.QObject)
def destroyed(self, obj):
self.pb = None
@QtCore.pyqtSlot(bool)
def clickedCancel(self, checked):
if self.pb is None:
return
self.kill()
self.isCancel = True
@QtCore.pyqtSlot(int, int)
def stepFile(self, bytesReceived, bytesTotal):
if self.pb is None:
return
self.pbFile.setMaximum( bytesTotal )
self.pbFile.setValue( bytesReceived )
class MessageBarCancel(QtCore.QObject):
def __init__(self, pluginName, msgBar, msg, funcKill):
def initGui():
self.tbCancel = QtGui.QToolButton( msgBar )
self.tbCancel.setIcon( QgsCore.QgsApplication.getThemeIcon( '/mActionCancelAllEdits.svg' ) )
self.tbCancel.setText( "Cancel")
self.tbCancel.setToolButtonStyle( QtCore.Qt.ToolButtonTextBesideIcon )
self.widget = msgBar.createMessage( pluginName, msg )
lyt = self.widget.layout()
lyt.addWidget( self.tbCancel )
super(MessageBarCancel, self).__init__()
self.widget = self.isCancel = None
initGui()
self.tbCancel.clicked.connect( self.clickedCancel )
msgBar.pushWidget( self.widget, QgsGui.QgsMessageBar.INFO )
self.isCancel = False
self.kill = funcKill
def message(self, msg):
if not self.isCancel:
self.widget.setText( msg )
@QtCore.pyqtSlot(bool)
def clickedCancel(self, checked):
self.kill()
self.isCancel = True