def add_file(self, filename): """ Add a file or directory to this widget. """ if filename not in self.filenames: if not os.access(filename, os.R_OK): Alert(strings._("not_a_readable_file", True).format(filename)) return self.filenames.append(filename) fileinfo = QtCore.QFileInfo(filename) basename = os.path.basename(filename.rstrip('/')) ip = QtWidgets.QFileIconProvider() icon = ip.icon(fileinfo) if os.path.isfile(filename): size = common.human_readable_filesize(fileinfo.size()) else: size = common.human_readable_filesize( common.dir_size(filename)) item_name = '{0:s} ({1:s})'.format(basename, size) item = QtWidgets.QListWidgetItem(item_name) item.setToolTip(size) item.setIcon(icon) self.addItem(item) self.files_updated.emit()
def add_file(self, filename): """ Add a file or directory to this widget. """ if filename not in self.filenames: if not os.access(filename, os.R_OK): Alert(strings._("not_a_readable_file", True).format(filename)) return self.filenames.append(filename) # Re-sort the list internally self.filenames.sort() fileinfo = QtCore.QFileInfo(filename) basename = os.path.basename(filename.rstrip('/')) ip = QtWidgets.QFileIconProvider() icon = ip.icon(fileinfo) if os.path.isfile(filename): size = common.human_readable_filesize(fileinfo.size()) else: size = common.human_readable_filesize(common.dir_size(filename)) item_name = '{0:s} ({1:s})'.format(basename, size) item = QtWidgets.QListWidgetItem(item_name) item.setToolTip(size) item.setIcon(icon) self.addItem(item) self.files_updated.emit()
def update_primary_action(self): # Show or hide primary action layout file_count = self.file_selection.file_list.count() if file_count > 0: self.primary_action.show() self.info_widget.show() # Update the file count in the info label total_size_bytes = 0 for index in range(self.file_selection.file_list.count()): item = self.file_selection.file_list.item(index) total_size_bytes += item.size_bytes total_size_readable = common.human_readable_filesize(total_size_bytes) if file_count > 1: self.info_label.setText(strings._('gui_file_info', True).format(file_count, total_size_readable)) else: self.info_label.setText(strings._('gui_file_info_single', True).format(file_count, total_size_readable)) else: self.primary_action.hide() self.info_widget.hide() # Resize window self.adjustSize()
def update_primary_action(self): # Show or hide primary action layout file_count = self.file_selection.file_list.count() if file_count > 0: self.primary_action.show() self.info_widget.show() # Update the file count in the info label total_size_bytes = 0 for index in range(self.file_selection.file_list.count()): item = self.file_selection.file_list.item(index) total_size_bytes += item.size_bytes total_size_readable = common.human_readable_filesize( total_size_bytes) if file_count > 1: self.info_label.setText( strings._('gui_file_info', True).format(file_count, total_size_readable)) else: self.info_label.setText( strings._('gui_file_info_single', True).format(file_count, total_size_readable)) else: self.primary_action.hide() self.info_widget.hide() # Resize window self.adjustSize()
def update(self, downloaded_bytes): self.downloaded_bytes = downloaded_bytes self.progress_bar.setValue(downloaded_bytes) if downloaded_bytes == self.progress_bar.total_bytes: pb_fmt = strings._('gui_download_progress_complete').format( common.format_seconds(time.time() - self.started)) else: elapsed = time.time() - self.started if elapsed < 10: # Wait a couple of seconds for the download rate to stabilize. # This prevents a "Windows copy dialog"-esque experience at # the beginning of the download. pb_fmt = strings._('gui_download_progress_starting').format( common.human_readable_filesize(downloaded_bytes)) else: pb_fmt = strings._('gui_download_progress_eta').format( common.human_readable_filesize(downloaded_bytes), self.estimated_time_remaining) self.progress_bar.setFormat(pb_fmt)
def test_human_readable_filesize(self, test_input, expected): assert common.human_readable_filesize(test_input) == expected
def add_file(self, filename): """ Add a file or directory to this widget. """ filenames = [] for index in range(self.count()): filenames.append(self.item(index).filename) if filename not in filenames: if not os.access(filename, os.R_OK): Alert(strings._("not_a_readable_file", True).format(filename)) return fileinfo = QtCore.QFileInfo(filename) ip = QtWidgets.QFileIconProvider() icon = ip.icon(fileinfo) if os.path.isfile(filename): size_bytes = fileinfo.size() size_readable = common.human_readable_filesize(size_bytes) else: size_bytes = common.dir_size(filename) size_readable = common.human_readable_filesize(size_bytes) # Create a new item item = QtWidgets.QListWidgetItem() item.setIcon(icon) item.size_bytes = size_bytes # Item's filename attribute and size labels item.filename = filename item_size = QtWidgets.QLabel(size_readable) item_size.setStyleSheet( 'QLabel { color: #666666; font-size: 11px; }') item.basename = os.path.basename(filename.rstrip('/')) # Use the basename as the method with which to sort the list metrics = QtGui.QFontMetrics(item.font()) elided = metrics.elidedText(item.basename, QtCore.Qt.ElideRight, self.sizeHint().width()) item.setData(QtCore.Qt.DisplayRole, elided) # Item's delete button def delete_item(): itemrow = self.row(item) self.takeItem(itemrow) self.files_updated.emit() item.item_button = QtWidgets.QPushButton() item.item_button.setDefault(False) item.item_button.setFlat(True) item.item_button.setIcon( QtGui.QIcon( common.get_resource_path('images/file_delete.png'))) item.item_button.clicked.connect(delete_item) item.item_button.setSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) # Item info widget, with a white background item_info_layout = QtWidgets.QHBoxLayout() item_info_layout.addWidget(item_size) item_info_layout.addWidget(item.item_button) item_info = QtWidgets.QWidget() item_info.setObjectName('item-info') item_info.setLayout(item_info_layout) # Create the item's widget and layouts item_hlayout = QtWidgets.QHBoxLayout() item_hlayout.addStretch() item_hlayout.addWidget(item_info) widget = QtWidgets.QWidget() widget.setLayout(item_hlayout) item.setSizeHint(widget.sizeHint()) self.addItem(item) self.setItemWidget(item, widget) self.files_updated.emit()
def add_file(self, filename): """ Add a file or directory to this widget. """ filenames = [] for index in range(self.count()): filenames.append(self.item(index).filename) if filename not in filenames: if not os.access(filename, os.R_OK): Alert(strings._("not_a_readable_file", True).format(filename)) return fileinfo = QtCore.QFileInfo(filename) ip = QtWidgets.QFileIconProvider() icon = ip.icon(fileinfo) if os.path.isfile(filename): size_bytes = fileinfo.size() size_readable = common.human_readable_filesize(size_bytes) else: size_bytes = common.dir_size(filename) size_readable = common.human_readable_filesize(size_bytes) # Create a new item item = QtWidgets.QListWidgetItem() item.setIcon(icon) item.size_bytes = size_bytes # Item's filename attribute and size labels item.filename = filename item_size = QtWidgets.QLabel(size_readable) item_size.setStyleSheet('QLabel { color: #666666; font-size: 11px; }') item.basename = os.path.basename(filename.rstrip('/')) # Use the basename as the method with which to sort the list metrics = QtGui.QFontMetrics(item.font()) elided = metrics.elidedText(item.basename, QtCore.Qt.ElideRight, self.sizeHint().width()) item.setData(QtCore.Qt.DisplayRole, elided) # Item's delete button def delete_item(): itemrow = self.row(item) self.takeItem(itemrow) self.files_updated.emit() item.item_button = QtWidgets.QPushButton() item.item_button.setDefault(False) item.item_button.setFlat(True) item.item_button.setIcon( QtGui.QIcon(common.get_resource_path('images/file_delete.png')) ) item.item_button.clicked.connect(delete_item) item.item_button.setSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) # Item info widget, with a white background item_info_layout = QtWidgets.QHBoxLayout() item_info_layout.addWidget(item_size) item_info_layout.addWidget(item.item_button) item_info = QtWidgets.QWidget() item_info.setObjectName('item-info') item_info.setLayout(item_info_layout) # Create the item's widget and layouts item_hlayout = QtWidgets.QHBoxLayout() item_hlayout.addStretch() item_hlayout.addWidget(item_info) widget = QtWidgets.QWidget() widget.setLayout(item_hlayout) item.setSizeHint(widget.sizeHint()) self.addItem(item) self.setItemWidget(item, widget) self.files_updated.emit()