-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
160 lines (123 loc) · 4.7 KB
/
__init__.py
File metadata and controls
160 lines (123 loc) · 4.7 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import os
import json
from aqt import mw
from aqt.qt import QTimer, QDialog, QVBoxLayout, QLabel, QLineEdit, QPushButton, QCheckBox
from aqt.reviewer import Reviewer
from aqt.utils import showInfo
from aqt import gui_hooks
# Config path
def get_config_path():
return os.path.join(mw.pm.profileFolder(), "auto_reveal", "config.json")
default_config = {
"allowed_models": ["Your Note Type Here"],
"allowed_decks": ["Your Deck Here"],
"delay_seconds": 7,
"enabled": True
}
def load_config():
path = get_config_path()
if os.path.exists(path):
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
return default_config.copy()
def save_config(config):
path = get_config_path()
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
json.dump(config, f, ensure_ascii=False, indent=2)
# -- snip --
_config_cache = None
def get_config():
global _config_cache
if _config_cache is None:
_config_cache = load_config()
return _config_cache
# Auto-flip logic
LONGFORM_TAG = "longform"
_original_show_question = Reviewer._showQuestion
timer_ref = None # store last timer so we can cancel it
def patched_show_question(self):
global timer_ref # track the current timer
result = _original_show_question(self)
if not get_config().get("enabled", True):
return result
model_name = self.card.note().model()["name"]
deck_name = mw.col.decks.name(self.card.did)
allowed_models = get_config().get("allowed_models", [])
allowed_decks = get_config().get("allowed_decks", [])
delay = get_config().get("delay_seconds", 7) * 1000 # convert to ms
# cancel previous timer
if timer_ref is not None:
timer_ref.stop()
timer_ref.deleteLater()
timer_ref = None
if model_name not in allowed_models:
return result
if allowed_decks and deck_name not in allowed_decks:
return result
if LONGFORM_TAG in self.card.note().tags:
delay = int(delay * 2)
# create a new one
timer = QTimer()
timer.setSingleShot(True)
def flip_if_same_card():
if self and self.state == "question":
self._showAnswer()
timer.timeout.connect(flip_if_same_card)
timer.start(delay)
timer_ref = timer # track this timer
return result
def reveal_answer_if_safe(reviewer):
if reviewer and reviewer.state == "question":
reviewer._showAnswer()
Reviewer._showQuestion = patched_show_question
# Settings window
class SettingsDialog(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Auto Reveal Settings")
self.setMinimumWidth(400)
layout = QVBoxLayout()
self.label_models = QLabel("Allowed note types (comma-separated):")
layout.addWidget(self.label_models)
self.input_models = QLineEdit()
self.input_models.setText(", ".join(get_config().get("allowed_models", [])))
layout.addWidget(self.input_models)
self.label_deck = QLabel("Allowed deck names (comma-separated):")
layout.addWidget(self.label_deck)
self.input_deck = QLineEdit()
self.input_deck.setText(", ".join(get_config().get("allowed_decks", [])))
layout.addWidget(self.input_deck)
self.label_delay = QLabel("Auto reveal delay (seconds):")
layout.addWidget(self.label_delay)
self.input_delay = QLineEdit()
self.input_delay.setText(str(get_config().get("delay_seconds", 7)))
layout.addWidget(self.input_delay)
self.toggle_enabled = QCheckBox("Enable Auto Reveal Effect")
self.toggle_enabled.setChecked(get_config().get("enabled", True))
layout.addWidget(self.toggle_enabled)
self.save_button = QPushButton("Save Settings")
self.save_button.clicked.connect(self.save)
layout.addWidget(self.save_button)
self.setLayout(layout)
def save(self):
models = [m.strip() for m in self.input_models.text().split(",") if m.strip()]
decks = [d.strip() for d in self.input_deck.text().split(",") if d.strip()]
try:
delay = max(1, int(float(self.input_delay.text().strip())))
except:
delay = 7
config = get_config()
config["allowed_models"] = models
config["allowed_decks"] = decks
config["delay_seconds"] = delay
config["enabled"] = self.toggle_enabled.isChecked()
save_config(config)
global _config_cache
_config_cache = None
self.close()
def open_settings():
dialog = SettingsDialog()
dialog.exec()
action = mw.form.menuTools.addAction("Auto Reveal (Timer) Settings")
action.triggered.connect(open_settings)