aboutsummaryrefslogtreecommitdiffstats
path: root/CategoryCardWidget.py
blob: 27a7993f967e9deb3b1c75930048268aa2b158e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Soundboard Engine - customizable soundboard
# Copyright (C) 2026  Vidhu Kant Sharma <vidhukant@vidhukant.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

import random

from PySide6.QtCore import Signal
from PySide6.QtGui import QPixmap, Qt
from PySide6.QtWidgets import (
    QFrame,
    QHBoxLayout,
    QLabel,
    QPushButton,
    QVBoxLayout,
)

from Soundboard import SoundboardCategory, SoundboardSound


class CategoryCardWidget(QFrame):
    list_clicked = Signal(SoundboardCategory)
    play_requested = Signal(SoundboardSound)
    CARD_HEIGHT = 150
    CARD_WIDTH = 400

    def __init__(self, category: SoundboardCategory, parent=None):
        super().__init__(parent)

        self.category = category

        self.setFixedSize(self.CARD_WIDTH, self.CARD_HEIGHT)
        self.setStyleSheet("""
            CategoryCardWidget {
                border: 1px solid palette(shadow);
                background-color: palette(window);
            }
        """)

        layout = QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)

        # category image
        self.image_label = QLabel()
        self.image_label.setFixedHeight(self.CARD_HEIGHT)
        pixmap = QPixmap(category.image_path).scaled(
            self.image_label.width(),
            self.image_label.height(),
            Qt.AspectRatioMode.KeepAspectRatio,
            Qt.TransformationMode.SmoothTransformation,
        )
        self.image_label.setPixmap(pixmap)
        self.image_label.setAlignment(Qt.AlignmentFlag.AlignLeft)
        layout.addWidget(self.image_label, alignment=Qt.AlignmentFlag.AlignLeft)

        content_layout = QVBoxLayout()
        content_layout.setContentsMargins(20, 0, 20, 0)
        content_layout.setSpacing(6)
        content_layout.addStretch()

        self.name_label = QLabel(category.name)
        self.name_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        name_font = self.name_label.font()
        name_font.setBold(True)
        name_font.setPointSize(18)
        self.name_label.setFont(name_font)
        content_layout.addWidget(self.name_label)

        self.sounds_count_label = QLabel(f"{len(category.sounds)} sounds")
        self.sounds_count_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        content_layout.addWidget(self.sounds_count_label)

        btn_layout = QHBoxLayout()
        btn_layout.setSpacing(6)

        self.btn_list = QPushButton("All Sounds")
        self.btn_list.clicked.connect(lambda: self.list_clicked.emit(self.category))
        btn_layout.addWidget(self.btn_list)

        self.btn_random = QPushButton("Play Random")
        self.btn_random.clicked.connect(self.play_random)
        btn_layout.addWidget(self.btn_random)

        content_layout.addLayout(btn_layout)
        content_layout.addStretch()

        layout.addLayout(content_layout)

    def play_random(self):
        self.play_requested.emit(random.choice(self.category.sounds))