135 lines
4.7 KiB
Python
135 lines
4.7 KiB
Python
from PyQt6.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout,
|
|
QComboBox, QLabel)
|
|
from PyQt6.QtCore import pyqtSignal
|
|
import requests
|
|
from typing import List, Dict
|
|
import os
|
|
from dotenv import load_dotenv
|
|
|
|
# Загружаем переменные окружения
|
|
load_dotenv()
|
|
|
|
class Sampler:
|
|
def __init__(self, name: str, title: str = None):
|
|
self.name = name
|
|
self.title = title or name
|
|
|
|
class SamplerManager(QWidget):
|
|
sampler_changed = pyqtSignal(str)
|
|
|
|
def __init__(self, api_url, parent=None):
|
|
super().__init__(parent)
|
|
self.api_url = api_url
|
|
self.setup_ui()
|
|
self.load_samplers()
|
|
|
|
def setup_ui(self):
|
|
self.setStyleSheet("""
|
|
QWidget {
|
|
background-color: transparent;
|
|
}
|
|
QLabel {
|
|
color: #d4d4d4;
|
|
font-size: 13px;
|
|
font-weight: 500;
|
|
margin-right: 8px;
|
|
}
|
|
QComboBox {
|
|
background-color: #252526;
|
|
color: #d4d4d4;
|
|
border: 1px solid #3e3e3e;
|
|
border-radius: 6px;
|
|
padding: 6px 12px;
|
|
min-width: 180px;
|
|
font-size: 13px;
|
|
}
|
|
QComboBox:hover {
|
|
border-color: #565656;
|
|
}
|
|
QComboBox:focus {
|
|
border-color: #007acc;
|
|
}
|
|
QComboBox::drop-down {
|
|
border: none;
|
|
width: 20px;
|
|
}
|
|
QComboBox::down-arrow {
|
|
image: url(data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTYiIGhlaWdodD0iMTYiIHZpZXdCb3g9IjAgMCAxNiAxNiIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTggMTFMNCBTTDEyIDUiIHN0cm9rZT0iI2Q0ZDRkNCIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiLz4KPC9zdmc+);
|
|
}
|
|
QComboBox QAbstractItemView {
|
|
background-color: #252526;
|
|
color: #d4d4d4;
|
|
border: 1px solid #3e3e3e;
|
|
selection-background-color: #094771;
|
|
outline: none;
|
|
}
|
|
""")
|
|
|
|
layout = QHBoxLayout(self)
|
|
layout.setContentsMargins(0, 0, 0, 0)
|
|
layout.setSpacing(8)
|
|
|
|
# Метка
|
|
label = QLabel("Сэмплер:")
|
|
layout.addWidget(label)
|
|
|
|
# Выпадающий список сэмплеров
|
|
self.sampler_combo = QComboBox()
|
|
self.sampler_combo.setFixedHeight(32)
|
|
self.sampler_combo.currentTextChanged.connect(self.on_sampler_changed)
|
|
layout.addWidget(self.sampler_combo)
|
|
|
|
layout.addStretch()
|
|
|
|
def load_samplers(self):
|
|
"""Загрузка списка сэмплеров с сервера"""
|
|
try:
|
|
response = requests.get(f"{self.api_url}/sdapi/v1/samplers")
|
|
if response.status_code == 200:
|
|
samplers_data = response.json()
|
|
self.samplers = [
|
|
Sampler(sampler['name'], sampler.get('title'))
|
|
for sampler in samplers_data
|
|
]
|
|
|
|
# По умолчанию выбираем Euler a
|
|
self.current_sampler = next(
|
|
(sampler for sampler in self.samplers if sampler.name == "Euler a"),
|
|
self.samplers[0] if self.samplers else None
|
|
)
|
|
except Exception as e:
|
|
print(f"Ошибка загрузки сэмплеров: {str(e)}")
|
|
self.samplers = []
|
|
self.current_sampler = None
|
|
|
|
def on_sampler_changed(self, sampler_name):
|
|
"""Обработка смены сэмплера"""
|
|
self.sampler_changed.emit(sampler_name)
|
|
|
|
def update_api_url(self, new_url):
|
|
"""Обновить URL API"""
|
|
self.api_url = new_url
|
|
self.load_samplers()
|
|
|
|
def get_samplers(self):
|
|
"""Возвращает список доступных сэмплеров"""
|
|
return self.samplers
|
|
|
|
def get_sampler_names(self):
|
|
"""Возвращает список имен сэмплеров"""
|
|
return [sampler.name for sampler in self.samplers]
|
|
|
|
def get_current_sampler(self):
|
|
"""Возвращает текущий сэмплер"""
|
|
return self.current_sampler
|
|
|
|
def set_current_sampler(self, sampler_name: str) -> bool:
|
|
"""Установка текущего сэмплера"""
|
|
sampler = next(
|
|
(s for s in self.samplers if s.name == sampler_name),
|
|
None
|
|
)
|
|
if sampler:
|
|
self.current_sampler = sampler
|
|
return True
|
|
return False |