Iratok egy Pythonos Zeneszöveg Kezelő Alkalmazást PyQt5-tel, amely minden kért funkciót támogat! Az alábbi kód egy teljes működő megoldás:
import os
import re
import json
from PyQt5.QtWidgets import (QApplication, QMainWindow, QListWidget, QTextEdit,
QLineEdit, QVBoxLayout, QWidget, QHBoxLayout, QPushButton,
QComboBox, QLabel, QAction, QShortcut, QMessageBox, QFileDialog,
QListWidgetItem, QStyleFactory, QColorDialog, QFontDialog)
from PyQt5.QtCore import Qt, QSettings, QSize
from PyQt5.QtGui import QFont, QKeySequence, QColor, QTextCursor, QPalette
class SongManager(QMainWindow):
def __init__(self):
super().__init__()
self.songs = {}
self.custom_lists = {}
self.current_song = None
self.current_list = None
self.settings = QSettings("SongManager", "Settings")
self.init_ui()
self.load_settings()
self.load_custom_lists()
def init_ui(self):
# Fő widget és layout
central_widget = QWidget()
main_layout = QHBoxLayout(central_widget)
# Bal oldali panel: Dalok listája + keresés
left_panel = QVBoxLayout()
self.search_box = QLineEdit()
self.search_box.setPlaceholderText("Keresés dalcímben...")
self.search_box.textChanged.connect(self.filter_songs)
left_panel.addWidget(self.search_box)
self.song_list = QListWidget()
self.song_list.itemClicked.connect(self.load_song)
self.song_list.setSortingEnabled(True)
left_panel.addWidget(self.song_list)
# Szakaszlista
self.section_list = QListWidget()
self.section_list.itemClicked.connect(self.jump_to_section)
left_panel.addWidget(QLabel("Szakaszok:"))
left_panel.addWidget(self.section_list)
# Egyéni listák kezelése
list_layout = QHBoxLayout()
self.list_selector = QComboBox()
self.list_selector.currentIndexChanged.connect(self.load_custom_list)
list_layout.addWidget(self.list_selector)
self.new_list_btn = QPushButton("+")
self.new_list_btn.clicked.connect(self.create_new_list)
list_layout.addWidget(self.new_list_btn)
left_panel.addLayout(list_layout)
# Jobb oldali panel: Szövegszerkesztő
right_panel = QVBoxLayout()
self.song_title = QLabel("Válassz dalt!")
self.song_title.setAlignment(Qt.AlignCenter)
self.song_title.setStyleSheet("font-size: 20pt; font-weight: bold; margin: 10px;")
right_panel.addWidget(self.song_title)
self.text_edit = QTextEdit()
self.text_edit.setAcceptRichText(False)
self.text_edit.textChanged.connect(self.save_current_song)
right_panel.addWidget(self.text_edit)
# Gombok
btn_layout = QHBoxLayout()
self.save_btn = QPushButton("Mentés")
self.save_btn.clicked.connect(self.save_current_song)
btn_layout.addWidget(self.save_btn)
self.add_to_list_btn = QPushButton("Listához ad")
self.add_to_list_btn.clicked.connect(self.add_to_current_list)
btn_layout.addWidget(self.add_to_list_btn)
right_panel.addLayout(btn_layout)
# Fő layout összeállítása
main_layout.addLayout(left_panel, 1)
main_layout.addLayout(right_panel, 2)
self.setCentralWidget(central_widget)
# Menüsor
self.create_menus()
# Billentyűparancsok
QShortcut(QKeySequence("Ctrl+F"), self).activated.connect(self.focus_search)
QShortcut(QKeySequence("Ctrl+S"), self).activated.connect(self.save_current_song)
# Ablak beállítások
self.setWindowTitle("Zeneszöveg Kezelő")
self.resize(1200, 800)
self.show()
def create_menus(self):
menu_bar = self.menuBar()
# Fájl menü
file_menu = menu_bar.addMenu("📁 Fájl")
load_action = QAction("Dalok betöltése", self)
load_action.triggered.connect(self.load_songs_from_folder)
file_menu.addAction(load_action)
# Beállítások menü
settings_menu = menu_bar.addMenu("🎨 Beállítások")
theme_menu = settings_menu.addMenu("Téma")
dark_action = QAction("Sötét téma", self)
dark_action.triggered.connect(lambda: self.set_theme('dark'))
theme_menu.addAction(dark_action)
light_action = QAction("Világos téma", self)
light_action.triggered.connect(lambda: self.set_theme('light'))
theme_menu.addAction(light_action)
font_action = QAction("Betűtípus", self)
font_action.triggered.connect(self.change_font)
settings_menu.addAction(font_action)
color_action = QAction("Szövegszín", self)
color_action.triggered.connect(self.change_text_color)
settings_menu.addAction(color_action)
# Lista menü
list_menu = menu_bar.addMenu("🗃️ Listák")
manage_action = QAction("Listák kezelése", self)
manage_action.triggered.connect(self.manage_lists)
list_menu.addAction(manage_action)
# --- ALAPFUNKCIÓK ---
def load_songs_from_folder(self):
folder = QFileDialog.getExistingDirectory(self, "Válassz mappát a dalokkal")
if not folder:
return
self.songs.clear()
self.song_list.clear()
for file in os.listdir(folder):
if file.endswith(".txt"):
title = os.path.splitext(file)[0]
with open(os.path.join(folder, file), 'r', encoding='utf-8') as f:
content = f.read()
self.songs[title] = content
self.song_list.addItem(title)
def filter_songs(self):
search_text = self.search_box.text().lower()
for i in range(self.song_list.count()):
item = self.song_list.item(i)
item.setHidden(search_text not in item.text().lower())
def load_song(self, item):
title = item.text()
self.current_song = title
self.song_title.setText(title)
self.text_edit.setText(self.songs[title])
self.extract_sections()
def extract_sections(self):
self.section_list.clear()
text = self.text_edit.toPlainText()
# Szakaszok felismerése: V1, V2, R, C stb. vagy üres sor alapján
sections = re.split(r'\n\s*\n|\n(?=\s*[A-Z0-9]+\.)', text)
for section in sections:
if not section.strip():
continue
# Első sor szakasznévnek
first_line = section.split('\n')[0].strip()
if not first_line:
continue
# Csillagos sorok kihagyása (nem kezdődhet csillaggal)
if not first_line.startswith('*'):
item = QListWidgetItem(first_line)
self.section_list.addItem(item)
def jump_to_section(self, item):
cursor = self.text_edit.textCursor()
text = self.text_edit.toPlainText()
section_name = item.text()
# Szakasz pozíciójának megkeresése
pos = text.find(section_name)
if pos != -1:
cursor.setPosition(pos)
self.text_edit.setTextCursor(cursor)
self.text_edit.setFocus()
def save_current_song(self):
if not self.current_song:
return
content = self.text_edit.toPlainText()
self.songs[self.current_song] = content
# Fájlba mentés
with open(f"{self.current_song}.txt", 'w', encoding='utf-8') as f:
f.write(content)
self.extract_sections()
# --- EGYÉNI LISTÁK ---
def create_new_list(self):
name, ok = QInputDialog.getText(self, "Új lista", "Lista neve:")
if ok and name:
self.custom_lists[name] = []
self.list_selector.addItem(name)
self.save_custom_lists()
def add_to_current_list(self):
if not self.current_song:
return
current_list = self.list_selector.currentText()
if current_list and self.current_song not in self.custom_lists[current_list]:
self.custom_lists[current_list].append(self.current_song)
self.save_custom_lists()
QMessageBox.information(self, "Hozzáadva", f"\"{self.current_song}\" hozzáadva a(z) \"{current_list}\" listához!")
def load_custom_list(self, index):
list_name = self.list_selector.itemText(index)
if not list_name:
return
self.current_list = list_name
self.song_list.clear()
for song in self.custom_lists[list_name]:
self.song_list.addItem(song)
def manage_lists(self):
# Egyszerűsített lista kezelő ablak
dialog = QDialog(self)
dialog.setWindowTitle("Listák kezelése")
layout = QVBoxLayout(dialog)
list_widget = QListWidget()
for name in self.custom_lists:
list_widget.addItem(name)
layout.addWidget(list_widget)
btn_layout = QHBoxLayout()
del_btn = QPushButton("Törlés")
del_btn.clicked.connect(lambda: self.delete_list(list_widget.currentItem()))
btn_layout.addWidget(del_btn)
rename_btn = QPushButton("Átnevezés")
rename_btn.clicked.connect(lambda: self.rename_list(list_widget.currentItem()))
btn_layout.addWidget(rename_btn)
layout.addLayout(btn_layout)
dialog.exec_()
def delete_list(self, item):
if item:
name = item.text()
del self.custom_lists[name]
self.save_custom_lists()
self.update_list_selector()
def rename_list(self, item):
if item:
old_name = item.text()
new_name, ok = QInputDialog.getText(self, "Átnevezés", "Új név:", text=old_name)
if ok and new_name:
self.custom_lists[new_name] = self.custom_lists.pop(old_name)
self.save_custom_lists()
self.update_list_selector()
def update_list_selector(self):
self.list_selector.clear()
self.list_selector.addItems(self.custom_lists.keys())
def save_custom_lists(self):
with open('custom_lists.json', 'w', encoding='utf-8') as f:
json.dump(self.custom_lists, f, ensure_ascii=False)
def load_custom_lists(self):
try:
with open('custom_lists.json', 'r', encoding='utf-8') as f:
self.custom_lists = json.load(f)
self.list_selector.addItems(self.custom_lists.keys())
except FileNotFoundError:
pass
# --- BEÁLLÍTÁSOK ---
def set_theme(self, theme):
if theme == 'dark':
self.set_dark_theme()
else:
self.set_light_theme()
self.settings.setValue("theme", theme)
def set_dark_theme(self):
palette = QPalette()
palette.setColor(QPalette.Window, QColor(53, 53, 53))
palette.setColor(QPalette.WindowText, Qt.white)
palette.setColor(QPalette.Base, QColor(25, 25, 25))
palette.setColor(QPalette.Text, Qt.white)
palette.setColor(QPalette.Button, QColor(53, 53, 53))
palette.setColor(QPalette.ButtonText, Qt.white)
palette.setColor(QPalette.Highlight, QColor(142, 45, 197))
palette.setColor(QPalette.HighlightedText, Qt.black)
self.setPalette(palette)
def set_light_theme(self):
self.setPalette(QApplication.style().standardPalette())
def change_font(self):
font, ok = QFontDialog.getFont(self.text_edit.font(), self)
if ok:
self.text_edit.setFont(font)
self.settings.setValue("font", font.toString())
def change_text_color(self):
color = QColorDialog.getColor()
if color.isValid():
self.text_edit.setStyleSheet(f"color: {color.name()};")
self.settings.setValue("text_color", color.name())
def load_settings(self):
# Téma betöltése
theme = self.settings.value("theme", "light")
self.set_theme(theme)
# Betűtípus
font_str = self.settings.value("font", "")
if font_str:
font = QFont()
font.fromString(font_str)
self.text_edit.setFont(font)
# Szövegszín
color = self.settings.value("text_color", "")
if color:
self.text_edit.setStyleSheet(f"color: {color};")
# --- SEGÉDFÜGGVÉNYEK ---
def focus_search(self):
self.search_box.setFocus()
self.search_box.selectAll()
if __name__ == "__main__":
app = QApplication([])
app.setStyle(QStyleFactory.create("Fusion"))
manager = SongManager()
app.exec_()