From bfbc82a9a3731c1b91ca3195f190187df69bf611 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Mon, 21 Mar 2016 21:53:02 +0300 Subject: [PATCH] progressbar is now work --- src/file_transfers.py | 20 ++++++++++++-------- src/list_items.py | 8 +++++--- src/profile.py | 6 +++--- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/file_transfers.py b/src/file_transfers.py index 491979b..78de97d 100644 --- a/src/file_transfers.py +++ b/src/file_transfers.py @@ -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) - self._tox.file_send_chunk(self._friend_number, self._file_number, position, data) + 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) diff --git a/src/list_items.py b/src/list_items.py index 43a46ab..4108358 100644 --- a/src/list_items.py +++ b/src/list_items.py @@ -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) diff --git a/src/profile.py b/src/profile.py index 58801b3..38ae0c4 100644 --- a/src/profile.py +++ b/src/profile.py @@ -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: