add color list

This commit is contained in:
2025-09-06 19:09:13 +02:00
parent 69a31e153f
commit 1aa65743e9
2 changed files with 165 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ from PySide6.QtCore import Qt, Slot
from PySide6.QtGui import QPalette, QColor from PySide6.QtGui import QPalette, QColor
from ui.widgets.placeholder_widget import PlaceholderWidget from ui.widgets.placeholder_widget import PlaceholderWidget
from ui.widgets.color_list_widget import ColorListWidget
class MainWindow(QMainWindow): class MainWindow(QMainWindow):
def __init__(self): def __init__(self):
@@ -43,7 +44,28 @@ class MainWindow(QMainWindow):
histogram_view.setFixedHeight(200) histogram_view.setFixedHeight(200)
control_layout.addWidget(histogram_view) control_layout.addWidget(histogram_view)
control_layout.addStretch() color_list_widget = ColorListWidget([
{"name": "Red", "color": "#FF0000"},
{"name": "Green", "color": "#00FF00"},
{"name": "Blue", "color": "#0000FF"},
{"name": "Yellow", "color": "#FFFF00"},
{"name": "Cyan", "color": "#00FFFF"},
{"name": "Magenta", "color": "#FF00FF"},
], self.control_widget)
control_layout.addWidget(color_list_widget)
# control_layout.addStretch()
record_button = QPushButton("Nagraj Wideo")
record_button.setMinimumHeight(40)
record_button.setStyleSheet("font-size: 12pt;")
control_layout.addWidget(record_button)
photo_button = QPushButton("Zrób zdjęcie")
photo_button.setMinimumHeight(40)
photo_button.setStyleSheet("font-size: 12pt;")
control_layout.addWidget(photo_button)

View 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())