add color list
This commit is contained in:
142
ui/widgets/color_list_widget.py
Normal file
142
ui/widgets/color_list_widget.py
Normal file
@@ -0,0 +1,142 @@
|
||||
from PySide6.QtWidgets import (
|
||||
QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLineEdit, QListView,
|
||||
QAbstractItemView, QComboBox, QLabel, QStyledItemDelegate, QStyleOptionButton, QStyle,
|
||||
)
|
||||
from PySide6.QtGui import QPixmap, QIcon, QColor, QStandardItemModel, QStandardItem
|
||||
from PySide6.QtCore import Qt, QSortFilterProxyModel, QSize, QRect, Signal, QModelIndex, QEvent
|
||||
import sys
|
||||
|
||||
|
||||
|
||||
def color_icon(color: QColor, size: int = 24) -> QIcon:
|
||||
pixmap = QPixmap(size, size)
|
||||
pixmap.fill(color)
|
||||
return QIcon(pixmap)
|
||||
|
||||
|
||||
class FilterProxy(QSortFilterProxyModel):
|
||||
def __init__(self, parent=None):
|
||||
super().__init__(parent)
|
||||
self.search_text = ""
|
||||
|
||||
def setSearchText(self, text: str):
|
||||
self.search_text = text
|
||||
self.invalidateFilter()
|
||||
|
||||
def filterAcceptsRow(self, source_row, source_parent):
|
||||
if not self.search_text:
|
||||
return True
|
||||
model = self.sourceModel()
|
||||
index = model.index(source_row, 0, source_parent)
|
||||
name = model.data(index, Qt.ItemDataRole.DisplayRole)
|
||||
return self.search_text.lower() in name.lower()
|
||||
|
||||
class DeleteButtonDelegate(QStyledItemDelegate):
|
||||
deleteClicked = Signal(QModelIndex)
|
||||
|
||||
def paint(self, painter, option, index):
|
||||
super().paint(painter, option, index)
|
||||
|
||||
button = QStyleOptionButton()
|
||||
button.rect = QRect(option.rect.right() - 60, option.rect.top(), 60, option.rect.height()) # type: ignore
|
||||
button.text = "Usuń" # type: ignore
|
||||
QApplication.style().drawControl(QStyle.ControlElement.CE_PushButton, button, painter)
|
||||
|
||||
def editorEvent(self, event, model, option, index):
|
||||
if event.type() == QEvent.Type.MouseButtonRelease:
|
||||
button_rect = QRect(option.rect.right() - 60, option.rect.top(), 60, option.rect.height())
|
||||
if button_rect.contains(event.pos()):
|
||||
self.deleteClicked.emit(index)
|
||||
return True
|
||||
return super().editorEvent(event, model, option, index)
|
||||
|
||||
class ColorListWidget(QWidget):
|
||||
def __init__(self, colors: list[dict], parent=None):
|
||||
super().__init__(parent)
|
||||
self.colors = colors
|
||||
|
||||
layout = QVBoxLayout(self)
|
||||
|
||||
self.filter_edit = QLineEdit()
|
||||
self.filter_edit.setPlaceholderText("Szukaj po nazwie koloru...")
|
||||
self.filter_edit.setMinimumHeight(32)
|
||||
self.filter_edit.setStyleSheet("font-size: 12pt;")
|
||||
|
||||
layout.addWidget(self.filter_edit)
|
||||
|
||||
self.model = QStandardItemModel(self)
|
||||
if colors:
|
||||
self.set_colors(colors)
|
||||
# for color in colors:
|
||||
# item = QStandardItem( color['name'])
|
||||
# item.setIcon(color_icon(QColor(color['hex']), 112))
|
||||
|
||||
# self.model.appendRow(item)
|
||||
|
||||
self.proxy_model = FilterProxy(self)
|
||||
self.proxy_model.setSourceModel(self.model)
|
||||
|
||||
self.list_view = QListView()
|
||||
self.list_view.setModel(self.proxy_model)
|
||||
# self.list_view.setModel(self.model)
|
||||
self.list_view.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
|
||||
self.list_view.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
|
||||
self.list_view.setUniformItemSizes(True)
|
||||
self.list_view.setSpacing(2)
|
||||
self.list_view.setIconSize(QSize(36, 36))
|
||||
self.list_view.setStyleSheet("""
|
||||
QListView::icon {
|
||||
margin-right: 12px; /* odstęp między ikoną a tekstem */
|
||||
}
|
||||
""")
|
||||
layout.addWidget(self.list_view)
|
||||
|
||||
delegate = DeleteButtonDelegate(self.list_view)
|
||||
delegate.deleteClicked.connect(self.on_delete_item)
|
||||
self.list_view.setItemDelegate(delegate)
|
||||
|
||||
self.filter_edit.textChanged.connect(self.proxy_model.setSearchText)
|
||||
# self.list_view.selectionModel().selectionChanged.connect(self.on_selection_changed)
|
||||
self.list_view.selectionModel().currentChanged.connect(self.on_current_changed)
|
||||
self.list_view.clicked.connect(self.on_item_clicked)
|
||||
|
||||
def set_colors(self, colors: list[dict]):
|
||||
self.colors = colors
|
||||
self.model.clear()
|
||||
for color in colors:
|
||||
item = QStandardItem( color['name'])
|
||||
item.setIcon(color_icon(QColor(color['color']), 112))
|
||||
self.model.appendRow(item)
|
||||
|
||||
|
||||
def on_current_changed(self, current, previous):
|
||||
if current.isValid():
|
||||
source_index = self.proxy_model.mapToSource(current)
|
||||
item = self.model.itemFromIndex(source_index)
|
||||
print(f"Wybrano kolor: {item.text()}")
|
||||
|
||||
def on_selection_changed(self, selected, deselected):
|
||||
indexes = selected.indexes()
|
||||
if indexes:
|
||||
index = indexes[0]
|
||||
source_index = self.proxy_model.mapToSource(index)
|
||||
item = self.model.itemFromIndex(source_index)
|
||||
line = item.text()
|
||||
print(f"Wybrano kolor: {line}")
|
||||
return line
|
||||
else:
|
||||
line = ""
|
||||
print("Brak zaznaczenia")
|
||||
return line
|
||||
|
||||
def on_item_clicked(self, index):
|
||||
source_index = self.proxy_model.mapToSource(index)
|
||||
item = self.model.itemFromIndex(source_index)
|
||||
if item:
|
||||
line = item.text()
|
||||
print(f"Kliknięto kolor: {line}")
|
||||
return line
|
||||
|
||||
def on_delete_item(self, index):
|
||||
source_index = self.proxy_model.mapToSource(index)
|
||||
self.model.removeRow(source_index.row())
|
||||
Reference in New Issue
Block a user