progressbar is now work

This commit is contained in:
ingvar1995 2016-03-21 21:53:02 +03:00
parent 2131fa3868
commit bfbc82a9a3
3 changed files with 20 additions and 14 deletions

View File

@ -29,7 +29,7 @@ class FileTransfer(QtCore.QObject):
self.state = TOX_FILE_TRANSFER_STATE['RUNNING']
self._file_number = file_number
self._creation_time = time()
self._size = size
self._size = float(size)
self._done = 0
self._state_changed = StateSignal()
@ -53,7 +53,7 @@ class FileTransfer(QtCore.QObject):
def send_control(self, control):
if self._tox.file_control(self._friend_number, self._file_number, control):
self.state = control
self._state_changed.signal.emit(self.state, self._done / self._size)
self._state_changed.signal.emit(self.state, self._done / self._size if self._size else 0)
def get_file_id(self):
return self._tox.file_get_file_id(self._friend_number, self._file_number)
@ -79,13 +79,16 @@ class SendTransfer(FileTransfer):
if size:
self._file.seek(position)
data = self._file.read(size)
try:
self._tox.file_send_chunk(self._friend_number, self._file_number, position, data)
except:
print self._friend_number, self._file_number, position, data
self._done += size
self._state_changed.signal.emit(self.state, self._done / self._size)
else:
self._file.close()
self.state = TOX_FILE_TRANSFER_STATE['FINISHED']
self._state_changed.signal.emit(self.state, self._done / self._size)
self._state_changed.signal.emit(self.state, 1)
class SendAvatar(SendTransfer):
@ -115,7 +118,7 @@ class ReceiveTransfer(FileTransfer):
if data is None:
self._file.close()
self.state = TOX_FILE_TRANSFER_STATE['FINISHED']
self._state_changed.signal.emit(self.state, self._done / self._size)
self._state_changed.signal.emit(self.state, self._done / self._size if self._size else 0)
else:
data = ''.join(chr(x) for x in data)
if self._file_size < position:
@ -124,9 +127,10 @@ class ReceiveTransfer(FileTransfer):
self._file.seek(position)
self._file.write(data)
self._file.flush()
if position + len(data) > self._file_size:
self._file_size = position + len(data)
self._done += len(data)
l = len(data)
if position + l > self._file_size:
self._file_size = position + l
self._done += l
self._state_changed.signal.emit(self.state, self._done / self._size)

View File

@ -200,7 +200,7 @@ class FileTransferItem(QtGui.QListWidget):
icon = QtGui.QIcon(pixmap)
self.accept.setIcon(icon)
self.accept.setIconSize(QtCore.QSize(50, 50))
self.accept.clicked.connect(lambda: self.accept_transfer(friend_number, file_number))
self.accept.clicked.connect(lambda: self.accept_transfer(friend_number, file_number, size))
self.accept.setVisible(is_incoming_transfer)
self.pb = QtGui.QProgressBar(self)
@ -231,16 +231,18 @@ class FileTransferItem(QtGui.QListWidget):
self.accept.setVisible(False)
self.pb.setVisible(False)
def accept_transfer(self, friend_number, file_number):
def accept_transfer(self, friend_number, file_number, size):
directory = QtGui.QFileDialog.getExistingDirectory()
if directory:
pr = profile.Profile.get_instance()
pr.accept_transfer(self, directory + '/' + self.saved_name, friend_number, file_number)
pr.accept_transfer(self, directory + '/' + self.saved_name, friend_number, file_number, size)
self.accept.setVisible(False)
@QtCore.Slot(int, float)
def update(self, state, progress):
self.pb.setValue(int(progress * 100))
#self.pb.repaint()
print state, progress
if state == TOX_FILE_TRANSFER_STATE['CANCELED']:
self.setStyleSheet('QListWidget { background-color: red; }')
self.cancel.setVisible(False)

View File

@ -729,8 +729,8 @@ class Profile(Contact, Singleton):
tr.cancel()
del self._file_transfers[(friend_number, file_number)]
def accept_transfer(self, item, path, friend_number, file_number):
rt = ReceiveTransfer(path, self._tox, friend_number, file_number)
def accept_transfer(self, item, path, friend_number, file_number, size):
rt = ReceiveTransfer(path, self._tox, friend_number, size, file_number)
self._file_transfers[(friend_number, file_number)] = rt
self._tox.file_control(friend_number, file_number, TOX_FILE_CONTROL['RESUME'])
rt.set_state_changed_handler(item.update)
@ -742,7 +742,7 @@ class Profile(Contact, Singleton):
:param file_number: file number
:param size: size of avatar or 0 (default avatar)
"""
ra = ReceiveAvatar(self._tox, friend_number, file_number, size)
ra = ReceiveAvatar(self._tox, friend_number, size, file_number)
if ra.state != TOX_FILE_TRANSFER_STATE['CANCELED']:
self._file_transfers[(friend_number, file_number)] = ra
else: