Give file items widgets, so they can soon contain working delete buttons

This commit is contained in:
Micah Lee 2018-02-04 20:09:51 -08:00
parent ea7420e0cf
commit 52d080d2a3
No known key found for this signature in database
GPG key ID: 403C2657CD994F73

View file

@ -38,6 +38,8 @@ class FileList(QtWidgets.QListWidget):
self.setMinimumHeight(200) self.setMinimumHeight(200)
self.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection) self.setSelectionMode(QtWidgets.QAbstractItemView.ExtendedSelection)
self.filenames = []
class DropHereLabel(QtWidgets.QLabel): class DropHereLabel(QtWidgets.QLabel):
""" """
When there are no files or folders in the FileList yet, display the When there are no files or folders in the FileList yet, display the
@ -85,9 +87,6 @@ class FileList(QtWidgets.QListWidget):
self.drop_count = DropCountLabel(self) self.drop_count = DropCountLabel(self)
self.resizeEvent(None) self.resizeEvent(None)
self.filenames = []
self.update()
def update(self): def update(self):
""" """
Update the GUI elements based on the current state. Update the GUI elements based on the current state.
@ -108,6 +107,13 @@ class FileList(QtWidgets.QListWidget):
self.drop_here_image.setGeometry(0, 0, self.width(), self.height() - offset) self.drop_here_image.setGeometry(0, 0, self.width(), self.height() - offset)
self.drop_here_text.setGeometry(0, offset, self.width(), self.height() - offset) self.drop_here_text.setGeometry(0, offset, self.width(), self.height() - offset)
# Add and delete an empty item, to force all items to get redrawn
# This is ugly, but the only way I could figure out how to proceed
item = QtWidgets.QListWidgetItem('fake item')
self.addItem(item)
self.takeItem(self.row(item))
self.update()
def dragEnterEvent(self, event): def dragEnterEvent(self, event):
""" """
dragEnterEvent for dragging files and directories into the widget. dragEnterEvent for dragging files and directories into the widget.
@ -183,11 +189,22 @@ class FileList(QtWidgets.QListWidget):
else: else:
size = common.human_readable_filesize(common.dir_size(filename)) size = common.human_readable_filesize(common.dir_size(filename))
item_name = '{0:s} ({1:s})'.format(basename, size) item_name = '{0:s} ({1:s})'.format(basename, size)
# Create a new item
item = QtWidgets.QListWidgetItem(item_name) item = QtWidgets.QListWidgetItem(item_name)
item.setToolTip(size) item.setToolTip(size)
item.setIcon(icon) item.setIcon(icon)
# Create an item widget to display on the item
item_button = QtWidgets.QPushButton('x')
item_widget_layout = QtWidgets.QHBoxLayout()
item_widget_layout.addStretch()
item_widget_layout.addWidget(item_button)
item_widget = QtWidgets.QWidget()
item_widget.setLayout(item_widget_layout)
self.addItem(item) self.addItem(item)
self.setItemWidget(item, item_widget)
self.files_updated.emit() self.files_updated.emit()