| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
from PyQt5 import QtCore, QtGui, QtWidgets |
| 22 |
import time |
| 23 |
|
| 24 |
from breezy import ui |
| 25 |
from breezy.plugins.qbrz.lib.i18n import gettext |
| 26 |
|
| 27 |
def ui_current_widget(f): |
| 28 |
def decorate(*args, **kargs): |
| 29 |
if isinstance(ui.ui_factory, QUIFactory): |
| 30 |
ui.ui_factory.current_widget_stack.append(args[0]) |
| 31 |
try: |
| 32 |
r = f(*args, **kargs) |
| 33 |
finally: |
| 34 |
del ui.ui_factory.current_widget_stack[-1] |
| 35 |
return r |
| 36 |
else: |
| 37 |
return f(*args, **kargs) |
| 38 |
return decorate |
| 39 |
|
| 40 |
def quifactory(): |
| 41 |
if isinstance(ui.ui_factory, QUIFactory): |
| 42 |
return ui.ui_factory |
| 43 |
return None |
| 44 |
|
| 45 |
def current_throbber(): |
| 46 |
ui = quifactory() |
| 47 |
if ui: |
| 48 |
return ui.throbber() |
| 49 |
return None |
| 50 |
|
| 51 |
class QUIFactory(ui.UIFactory): |
| 52 |
|
| 53 |
def __init__(self): |
| 54 |
super(QUIFactory, self).__init__() |
| 55 |
self.current_widget_stack = [] |
| 56 |
|
| 57 |
self._transport_update_time = 0 |
| 58 |
self._total_byte_count = 0 |
| 59 |
self._bytes_since_update = 0 |
| 60 |
self._transport_rate = None |
| 61 |
|
| 62 |
def current_widget(self): |
| 63 |
if self.current_widget_stack: |
| 64 |
return self.current_widget_stack[-1] |
| 65 |
return None |
| 66 |
|
| 67 |
def throbber(self): |
| 68 |
current_widget = self.current_widget() |
| 69 |
if current_widget and getattr(current_widget, 'throbber', None) is not None: |
| 70 |
return current_widget.throbber |
| 71 |
return None |
| 72 |
|
| 73 |
def report_transport_activity(self, transport, byte_count, direction): |
| 74 |
"""Called by transports as they do IO. |
| 75 |
|
| 76 |
This may update a progress bar, spinner, or similar display. |
| 77 |
By default it does nothing. |
| 78 |
""" |
| 79 |
|
| 80 |
self._total_byte_count += byte_count |
| 81 |
self._bytes_since_update += byte_count |
| 82 |
|
| 83 |
throbber = self.throbber() |
| 84 |
if throbber: |
| 85 |
now = time.time() |
| 86 |
if self._transport_update_time is None: |
| 87 |
self._transport_update_time = now |
| 88 |
elif now >= (self._transport_update_time + 0.2): |
| 89 |
|
| 90 |
|
| 91 |
self._transport_rate = self._bytes_since_update / (now - self._transport_update_time) |
| 92 |
self._transport_update_time = now |
| 93 |
self._bytes_since_update = 0 |
| 94 |
|
| 95 |
if self._transport_rate: |
| 96 |
msg = ("%6dkB @ %4dkB/s" % |
| 97 |
(self._total_byte_count>>10, int(self._transport_rate)>>10,)) |
| 98 |
else: |
| 99 |
msg = ("%6dkB @ " % |
| 100 |
(self._total_byte_count>>10,)) |
| 101 |
|
| 102 |
throbber.transport.setText(msg) |
| 103 |
|
| 104 |
QtCore.QCoreApplication.processEvents() |
| 105 |
|
| 106 |
def get_password(self, prompt='', **kwargs): |
| 107 |
password, ok = QtWidgets.QInputDialog.getText(self.current_widget(), |
| 108 |
gettext("Enter Password"), |
| 109 |
(prompt % kwargs), |
| 110 |
QtWidgets.QLineEdit.Password) |
| 111 |
|
| 112 |
if ok: |
| 113 |
return str(password) |
| 114 |
else: |
| 115 |
raise KeyboardInterrupt() |
| 116 |
|
| 117 |
def get_username(self, prompt='', **kwargs): |
| 118 |
username, ok = QtWidgets.QInputDialog.getText(self.current_widget(), |
| 119 |
gettext("Enter Username"), |
| 120 |
(prompt % kwargs)) |
| 121 |
|
| 122 |
if ok: |
| 123 |
return str(username) |
| 124 |
else: |
| 125 |
raise KeyboardInterrupt() |
| 126 |
|
| 127 |
def get_boolean(self, prompt): |
| 128 |
button = QtWidgets.QMessageBox.question( |
| 129 |
self.current_widget(), "Bazaar", prompt, |
| 130 |
QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No) |
| 131 |
|
| 132 |
return button == QtWidgets.QMessageBox.Yes |
| 133 |
|
| 134 |
def clear_term(self): |
| 135 |
"""Prepare the terminal for output. |
| 136 |
|
| 137 |
This will, for example, clear text progress bars, and leave the |
| 138 |
cursor at the leftmost position.""" |
| 139 |
pass |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
if __name__ == "__main__": |
| 146 |
application = QtWidgets.QApplication([]) |
| 147 |
ui_factory = QUIFactory() |
| 148 |
print(ui_factory.get_username("Enter password 123")) |
| 149 |
|