-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
executable file
·104 lines (82 loc) · 2.63 KB
/
__init__.py
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
103
104
from anki.hooks import wrap
from aqt.sound import (
av_player,
MpvManager,
OnDoneCallback,
AVTag,
SoundOrVideoTag,
)
from aqt import AnkiQt
from aqt.qt import (
QAction,
QHBoxLayout,
QVBoxLayout,
QDialog,
QDialogButtonBox,
QSlider,
QLabel,
Qt,
)
from aqt import mw
from aqt.utils import qconnect
from anki import hooks
from aqt import gui_hooks
import os
config = mw.addonManager.getConfig(__name__)
class Dialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Audio volume")
self.volume = config["volume"]
self.layout = QVBoxLayout()
self.layout1 = QHBoxLayout()
self.layout2 = QHBoxLayout()
QBtn = (
QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel
)
self.buttonBox = QDialogButtonBox(QBtn)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
self.slider = QSlider(Qt.Orientation.Horizontal)
self.slider.setRange(0, 100)
self.slider.setSingleStep(5)
self.slider.setValue(self.volume)
self.slider.valueChanged.connect(self.value_changed)
self.label = QLabel(f"{self.volume}%")
self.layout1.addWidget(self.slider)
self.layout1.addWidget(self.label)
self.layout2.addWidget(self.buttonBox)
self.layout.addLayout(self.layout1)
self.layout.addLayout(self.layout2)
self.setLayout(self.layout)
def accept(self):
config["volume"] = self.volume
mw.addonManager.writeConfig(__name__, config)
self.close()
def value_changed(self, v):
self.volume = v
self.label.setText(f"{self.volume}%")
self.slider.setValue(self.volume)
def testFunction() -> None:
dlg = Dialog()
dlg.exec()
action = QAction("Audio volume", mw)
qconnect(action.triggered, testFunction)
mw.form.menuTools.addAction(action)
class ModifiedMpvManager(MpvManager):
def play(self, tag: AVTag, on_done: OnDoneCallback) -> None:
assert isinstance(tag, SoundOrVideoTag)
self._on_done = on_done
filename = hooks.media_file_filter(tag.filename)
path = os.path.join(self.media_folder, filename)
self.command(
"loadfile",
path,
"replace",
0,
f"volume={config['volume']}," + "pause=no,",
)
gui_hooks.av_player_did_begin_playing(self, tag)
def add_player(_):
av_player.players.append(ModifiedMpvManager(mw.pm.base, mw.col.media.dir()))
AnkiQt.setup_sound = wrap(AnkiQt.setup_sound, add_player, "after")