-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqtgui.py
153 lines (140 loc) · 5.13 KB
/
qtgui.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
149
150
151
152
153
# -*- coding: utf-8 -*-
"""
Qt GUI for Robobackup
This file is part of Robobackup.
Copyright 2015 Siegfried Schoefer
Robobackup 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 3 of the License, or
(at your option) any later version.
Robobackup 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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from PyQt5 import QtWidgets, uic, QtCore
from PyQt5.QtCore import pyqtSlot
from PyQt5.Qt import QPixmap
from PyQt5.QtWidgets import QMessageBox
from logtools import Severity, logbook
import os
class BackupGuiQt(QtWidgets.QWidget):
"""
The main widget of the robobackup GUI made with Qt.
"""
def __init__(self, method, *args, **kwargs):
super(BackupGuiQt, self).__init__(*args, **kwargs)
self._method_ = method
self._gui_ = uic.loadUi(os.path.join(os.path.dirname(\
os.path.relpath(__file__)), "ui", "robobackup-gui.ui"), \
self)
self.__set_image("ASK.png")
self._gui_.btnStart.setText(_("Start Backup"))
self._gui_.btnClose.setText(_("Close"))
self.__set_status("")
self._gui_.clockLabel.setText(_("Elapsed time:"))
self._gui_.logLabel.setText(_("Log:"))
self._gui_.btnStart.clicked.connect(self.__clk)
self._gui_.btnClose.clicked.connect(self.__cls)
self._timer_ = QtCore.QTimer()
self._timer_.timeout.connect(self.__show_time)
self._timer_.start(1000)
self._time_ = QtCore.QTime()
@pyqtSlot()
def __clk(self):
"""
This method starts the backup. It is a slot which is connected
to the signal "click" of the button "btnStart".
"""
self._gui_.btnStart.setEnabled(False)
self._gui_.btnClose.setEnabled(False)
self._time_.start()
try:
self._method_()
except:
if __debug__:
logbook.exception("")
self.__set_backup_failure()
QMessageBox.critical(self, _("Critical"), \
_("Robobackup failed. Contact your admin."), \
QMessageBox.Ok)
finally:
self._timer_.timeout.disconnect(self.__show_time)
self._gui_.btnClose.setEnabled(True)
@pyqtSlot()
def __cls(self):
"""
This method is used to close the GUI. It is a slot which is
connected to the signal "click" of the button "btnClose".
"""
self.close() # pylint: disable=maybe-no-member
@pyqtSlot()
def __show_time(self):
"""
The digital timer gets incremented.
"""
milliseconds = self._time_.elapsed()
seconds, milliseconds = divmod(milliseconds, 1000)
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
digitalclock = "{hh:02d}:{mm:02d}:{ss:02d}".format(hh=hours, \
mm=minutes, ss=seconds)
self._gui_.lcd.display(digitalclock)
def update(self):
"""
This method will be called, if logs are changing.
"""
color = logbook.get_severity()
self._gui_.log.setText(logbook.get_string())
if color is Severity.green:
self.__set_backup_success()
elif color is Severity.orange:
self.__set_backup_check()
elif color is Severity.red:
self.__set_backup_failure()
else:
raise RuntimeError(_("Color does not match an action."))
def __set_image(self, img):
"""
This method is used to set memorizable images to illustrate
the status of the programm.
"""
self._gui_.statusPicture.setPixmap(QPixmap(os.path.join \
(os.path.dirname(os.path.relpath(__file__)), "resources",\
img)))
def __set_status(self, status):
"""
This method is used to set the text of a short label indicating
the status of robobackup.
"""
self._gui_.errorLabel.setText(status)
def __set_color(self, color):
"""
This method is used to set the colour of a field.
It can be used to signal a critical error with red.
"""
self._gui_.errorLabel.setStyleSheet(\
"QLabel { background-color: " + color +"}")
def __set_backup_success(self):
"""
Set everything green.
"""
self.__set_image("SUCCESS.png")
self.__set_status(_("Success"))
self.__set_color("green")
def __set_backup_check(self):
"""
Set everything orange.
"""
self.__set_image("CHECK.png")
self.__set_status(_("Check backup"))
self.__set_color("orange")
def __set_backup_failure(self):
"""
Set everything red.
"""
self.__set_image("FAIL.png")
self.__set_status(_("Failure"))
self.__set_color("red")