Skip to content

Commit

Permalink
Use some new hooks in the gui_conf
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiesel committed Mar 26, 2020
1 parent 786cc85 commit 790ba8e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 107 deletions.
171 changes: 69 additions & 102 deletions src/gui/gui_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from aqt import mw
from aqt.deckconf import DeckConf
from aqt.forms import dconf
from aqt.gui_hooks import deck_conf_did_load_config, deck_conf_will_save_config, deck_conf_did_setup_ui_form

from anki.hooks import wrap

Expand All @@ -13,100 +13,102 @@
remove_setting, rename_setting,
)

def setup_reward_tab(self, Dialog):
def setup_reward_tab(dconf) -> None:
"""Add an option tab for Straight Reward at Review section on Deckconf dialog."""
f = dconf.form

w = QtWidgets.QWidget()
self.gridLayout_straight = QtWidgets.QGridLayout()
self.gridLayout_straight.setColumnStretch(1, 5)
f.gridLayout_straight = QtWidgets.QGridLayout()
f.gridLayout_straight.setColumnStretch(1, 5)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)

##### STRAIGHT LENGTH
self.straightLengthLabel = QtWidgets.QLabel(w)
self.straightLengthLabel.setText(_('Begin at straight of length'))
self.gridLayout_straight.addWidget(self.straightLengthLabel, 1, 0, 1, 1)
f.straightLengthLabel = QtWidgets.QLabel(w)
f.straightLengthLabel.setText(_('Begin at straight of length'))
f.gridLayout_straight.addWidget(f.straightLengthLabel, 1, 0, 1, 1)

self.straightLengthSpinBox = QtWidgets.QSpinBox(w)
self.straightLengthSpinBox.setMinimum(0)
self.straightLengthSpinBox.setMaximum(100)
self.gridLayout_straight.addWidget(self.straightLengthSpinBox, 1, 1, 1, 2)
f.straightLengthSpinBox = QtWidgets.QSpinBox(w)
f.straightLengthSpinBox.setMinimum(0)
f.straightLengthSpinBox.setMaximum(100)
f.gridLayout_straight.addWidget(f.straightLengthSpinBox, 1, 1, 1, 2)

##### ENABLE NOTIFICATIONS
self.straightEnableNotificationsLabel = QtWidgets.QLabel(w)
self.straightEnableNotificationsLabel.setText(_('Enable notifications'))
self.gridLayout_straight.addWidget(self.straightEnableNotificationsLabel, 2, 0, 1, 1)
f.straightEnableNotificationsLabel = QtWidgets.QLabel(w)
f.straightEnableNotificationsLabel.setText(_('Enable notifications'))
f.gridLayout_straight.addWidget(f.straightEnableNotificationsLabel, 2, 0, 1, 1)

self.straightEnableNotificationsCheckBox = QtWidgets.QCheckBox(w)
self.gridLayout_straight.addWidget(self.straightEnableNotificationsCheckBox, 2, 1, 1, 2)
f.straightEnableNotificationsCheckBox = QtWidgets.QCheckBox(w)
f.gridLayout_straight.addWidget(f.straightEnableNotificationsCheckBox, 2, 1, 1, 2)

##### BASE EASE
self.straightBaseEaseLabel = QtWidgets.QLabel(w)
self.straightBaseEaseLabel.setText(_('Base ease reward'))
self.gridLayout_straight.addWidget(self.straightBaseEaseLabel, 3, 0, 1, 1)
f.straightBaseEaseLabel = QtWidgets.QLabel(w)
f.straightBaseEaseLabel.setText(_('Base ease reward'))
f.gridLayout_straight.addWidget(f.straightBaseEaseLabel, 3, 0, 1, 1)

self.straightBaseEaseSpinBox = QtWidgets.QSpinBox(w)
self.straightBaseEaseSpinBox.setSuffix('%')
self.straightBaseEaseSpinBox.setMinimum(0)
self.straightBaseEaseSpinBox.setMaximum(999)
self.gridLayout_straight.addWidget(self.straightBaseEaseSpinBox, 3, 1, 1, 2)
f.straightBaseEaseSpinBox = QtWidgets.QSpinBox(w)
f.straightBaseEaseSpinBox.setSuffix('%')
f.straightBaseEaseSpinBox.setMinimum(0)
f.straightBaseEaseSpinBox.setMaximum(999)
f.gridLayout_straight.addWidget(f.straightBaseEaseSpinBox, 3, 1, 1, 2)

##### STEP EASE
self.straightStepEaseLabel = QtWidgets.QLabel(w)
self.straightStepEaseLabel.setText(_('Step ease reward'))
self.gridLayout_straight.addWidget(self.straightStepEaseLabel, 4, 0, 1, 1)
f.straightStepEaseLabel = QtWidgets.QLabel(w)
f.straightStepEaseLabel.setText(_('Step ease reward'))
f.gridLayout_straight.addWidget(f.straightStepEaseLabel, 4, 0, 1, 1)

self.straightStepEaseSpinBox = QtWidgets.QSpinBox(w)
self.straightStepEaseSpinBox.setSuffix('%')
self.straightStepEaseSpinBox.setMinimum(0)
self.straightStepEaseSpinBox.setMaximum(999)
self.gridLayout_straight.addWidget(self.straightStepEaseSpinBox, 4, 1, 1, 2)
f.straightStepEaseSpinBox = QtWidgets.QSpinBox(w)
f.straightStepEaseSpinBox.setSuffix('%')
f.straightStepEaseSpinBox.setMinimum(0)
f.straightStepEaseSpinBox.setMaximum(999)
f.gridLayout_straight.addWidget(f.straightStepEaseSpinBox, 4, 1, 1, 2)

##### START EASE
self.straightStartEaseLabel = QtWidgets.QLabel(w)
self.straightStartEaseLabel.setText(_('Start at ease'))
self.gridLayout_straight.addWidget(self.straightStartEaseLabel, 5, 0, 1, 1)
f.straightStartEaseLabel = QtWidgets.QLabel(w)
f.straightStartEaseLabel.setText(_('Start at ease'))
f.gridLayout_straight.addWidget(f.straightStartEaseLabel, 5, 0, 1, 1)

self.straightStartEaseSpinBox = QtWidgets.QSpinBox(w)
self.straightStartEaseSpinBox.setSuffix('%')
self.straightStartEaseSpinBox.setMinimum(130)
self.straightStartEaseSpinBox.setMaximum(999)
self.gridLayout_straight.addWidget(self.straightStartEaseSpinBox, 5, 1, 1, 2)
f.straightStartEaseSpinBox = QtWidgets.QSpinBox(w)
f.straightStartEaseSpinBox.setSuffix('%')
f.straightStartEaseSpinBox.setMinimum(130)
f.straightStartEaseSpinBox.setMaximum(999)
f.gridLayout_straight.addWidget(f.straightStartEaseSpinBox, 5, 1, 1, 2)

##### STOP EASE
self.straightStopEaseLabel = QtWidgets.QLabel(w)
self.straightStopEaseLabel.setText(_('Stop at ease'))
self.gridLayout_straight.addWidget(self.straightStopEaseLabel, 6, 0, 1, 1)
f.straightStopEaseLabel = QtWidgets.QLabel(w)
f.straightStopEaseLabel.setText(_('Stop at ease'))
f.gridLayout_straight.addWidget(f.straightStopEaseLabel, 6, 0, 1, 1)

self.straightStopEaseSpinBox = QtWidgets.QSpinBox(w)
self.straightStopEaseSpinBox.setSuffix('%')
self.straightStopEaseSpinBox.setMinimum(130)
self.straightStopEaseSpinBox.setMaximum(999)
self.gridLayout_straight.addWidget(self.straightStopEaseSpinBox, 6, 1, 1, 2)
f.straightStopEaseSpinBox = QtWidgets.QSpinBox(w)
f.straightStopEaseSpinBox.setSuffix('%')
f.straightStopEaseSpinBox.setMinimum(130)
f.straightStopEaseSpinBox.setMaximum(999)
f.gridLayout_straight.addWidget(f.straightStopEaseSpinBox, 6, 1, 1, 2)

##### VERTICAL SPACER
verticalSpacer = QtWidgets.QSpacerItem(20, 150, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
self.gridLayout_straight.addItem(verticalSpacer)
f.gridLayout_straight.addItem(verticalSpacer)

w.setLayout(self.gridLayout_straight)
self.tabWidget.insertTab(2, w, 'Straight Rewards')
w.setLayout(f.gridLayout_straight)
f.tabWidget.insertTab(2, w, 'Straight Rewards')

def load_reward_tab(self):
def load_reward_tab(dconf, deck, config):
"""Get the option for Straight Reward."""
straight_conf = get_setting(mw.col, self.conf['name'])
straight_conf = get_setting(mw.col, config['name'])

f = self.form
f = dconf.form
f.straightLengthSpinBox.setValue(straight_conf.straight_length)
f.straightEnableNotificationsCheckBox.setChecked(straight_conf.enable_notifications)
f.straightBaseEaseSpinBox.setValue(straight_conf.base_ease)
f.straightStepEaseSpinBox.setValue(straight_conf.step_ease)
f.straightStartEaseSpinBox.setValue(straight_conf.start_ease)
f.straightStopEaseSpinBox.setValue(straight_conf.stop_ease)

def save_reward_tab(self):
def save_reward_tab(dconf, deck, config):
"""Save the option for Straight Reward."""
f = self.form
f = dconf.form

result = StraightSetting(
self.conf['name'],
config['name'],
f.straightLengthSpinBox.value(),
f.straightEnableNotificationsCheckBox.isChecked(),
f.straightBaseEaseSpinBox.value(),
Expand All @@ -117,17 +119,7 @@ def save_reward_tab(self):

write_setting(mw.col, result)

def restore_reward_tab(self):
straight_conf = get_default_setting(self.conf['name'])

f = self.form
f.straightLengthSpinBox.setValue(straight_conf.straight_length)
f.straightBaseEaseSpinBox.setValue(straight_conf.base_ease)
f.straightStepEaseSpinBox.setValue(straight_conf.step_ease)
f.straightStartEaseSpinBox.setValue(straight_conf.start_ease)
f.straightStopEaseSpinBox.setValue(straight_conf.stop_ease)

def remove_straight_setting(self, _old):
def update_straight_setting(self, _old):
save_names = {c['name'] for c in mw.col.decks.allConf()}

_old(self)
Expand All @@ -136,41 +128,16 @@ def remove_straight_setting(self, _old):

name_for_deletion = save_names.difference(new_names)

if len(name_for_deletion) == 1:
remove_setting(mw.col, name_for_deletion.pop())
if len(name_for_deletion) != 1:
return
# user renamed conf to old name, or reuses name

# otherwise user renamed conf to old name, or reuses name

# unused
def rename_straight_setting(self, _old):
save_names = {c['name'] for c in mw.col.decks.allConf()}

_old(self)

new_names = {c['name'] for c in mw.col.decks.allConf()}

old_name = save_names.difference(new_names)
new_name = new_names.difference(save_names)

if len(old_name) == 1 and len(new_name) == 1:
rename_setting(
mw.col,
old_name.pop(),
new_name.pop(),
)

# otherwise user renamed conf to old name, or reuses name
remove_setting(mw.col, name_for_deletion.pop())

def init_conf():
dconf.Ui_Dialog.setupUi = wrap(dconf.Ui_Dialog.setupUi, setup_reward_tab)

DeckConf.onRestore = wrap(DeckConf.onRestore, restore_reward_tab)
DeckConf.loadConf = wrap(DeckConf.loadConf, load_reward_tab)
DeckConf.saveConf = wrap(DeckConf.saveConf, save_reward_tab, 'before')

DeckConf.remGroup = wrap(DeckConf.remGroup, remove_straight_setting, 'around')
DeckConf.renameGroup = wrap(DeckConf.renameGroup, remove_straight_setting, 'around')
deck_conf_did_setup_ui_form.append(setup_reward_tab)
deck_conf_did_load_config.append(load_reward_tab)
deck_conf_will_save_config.append(save_reward_tab)

# gui_hooks.deck_conf_did_setup_ui_form.append(setup_reward_tab)
# gui_hooks.deck_conf_did_load_config.append(load_reward_tab)
# gui_hooks.deck_conf_will_save_config.append(save_reward_tab)
DeckConf.remGroup = wrap(DeckConf.remGroup, update_straight_setting, 'around')
DeckConf.renameGroup = wrap(DeckConf.renameGroup, update_straight_setting, 'around')
10 changes: 5 additions & 5 deletions src/lib/sync_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def check_cids(col, newrev_cids) -> List[Tuple[int, int]]:
for cid in newrev_cids:
did = col.decks.for_card_ids([cid])

# some cards will do not have decks associated with them,
# and in this case we don't know what reward parameters to use, so ignore
# some cards do not have decks associated with them,
# and in this case we don't know which straight settings to use, so ignore
if not did:
break

Expand Down Expand Up @@ -85,11 +85,11 @@ def after_sync(col) -> None:

result = check_cids(col, newrev_cids)

filtered = [f"cid:{r[0]} easeplus:{r[1]}" for r in result if r[1] != 0]
filtered_length = len(filtered)
filtered_logs = [f"cid:{r[0]} easeplus:{r[1]}" for r in result if r[1] != 0]
filtered_length = len(filtered_logs)

if filtered_length > 0:
log_sync(col.crt, filtered)
log_sync(col.crt, filtered_logs)
display_sync_info(filtered_length)

oldids.clear()
Expand Down

0 comments on commit 790ba8e

Please sign in to comment.