2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
2021-03-10 09:20:37 +01:00
|
|
|
|
2016-04-05 03:02:01 +02:00
|
|
|
import anki.lang
|
2012-12-21 08:51:59 +01:00
|
|
|
import aqt
|
2021-03-10 09:20:37 +01:00
|
|
|
from anki.consts import newCardSchedulingLabels
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt import AnkiQt
|
2020-12-22 04:01:06 +01:00
|
|
|
from aqt.profiles import RecordingDriver, VideoDriver
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2021-03-26 05:21:04 +01:00
|
|
|
from aqt.utils import HelpPage, disable_help_button, openHelp, showInfo, showWarning, tr
|
2019-12-20 10:19:03 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
class Preferences(QDialog):
|
2021-02-01 14:28:21 +01:00
|
|
|
def __init__(self, mw: AnkiQt) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.__init__(self, mw, Qt.Window)
|
|
|
|
self.mw = mw
|
|
|
|
self.prof = self.mw.pm.profile
|
|
|
|
self.form = aqt.forms.preferences.Ui_Preferences()
|
|
|
|
self.form.setupUi(self)
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(self)
|
2014-06-03 09:24:47 +02:00
|
|
|
self.form.buttonBox.button(QDialogButtonBox.Help).setAutoDefault(False)
|
|
|
|
self.form.buttonBox.button(QDialogButtonBox.Close).setAutoDefault(False)
|
2021-01-25 14:45:47 +01:00
|
|
|
qconnect(
|
|
|
|
self.form.buttonBox.helpRequested, lambda: openHelp(HelpPage.PREFERENCES)
|
|
|
|
)
|
2017-09-10 07:15:12 +02:00
|
|
|
self.silentlyClose = True
|
2021-03-10 09:20:37 +01:00
|
|
|
self.setup_collection()
|
|
|
|
self.setup_profile()
|
|
|
|
self.setup_global()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.show()
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def accept(self) -> None:
|
2015-04-26 12:28:32 +02:00
|
|
|
# avoid exception if main window is already closed
|
|
|
|
if not self.mw.col:
|
|
|
|
return
|
2021-03-10 09:20:37 +01:00
|
|
|
self.update_collection()
|
|
|
|
self.update_profile()
|
|
|
|
self.update_global()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw.pm.save()
|
|
|
|
self.mw.reset()
|
|
|
|
self.done(0)
|
2017-09-10 07:15:12 +02:00
|
|
|
aqt.dialogs.markClosed("Preferences")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def reject(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.accept()
|
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
# Preferences stored in the collection
|
2016-04-05 03:02:01 +02:00
|
|
|
######################################################################
|
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
def setup_collection(self) -> None:
|
|
|
|
self.prefs = self.mw.col.get_preferences()
|
2016-04-05 03:02:01 +02:00
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
form = self.form
|
|
|
|
|
|
|
|
scheduling = self.prefs.scheduling
|
|
|
|
form.lrnCutoff.setValue(int(scheduling.learn_ahead_secs / 60.0))
|
|
|
|
form.newSpread.addItems(list(newCardSchedulingLabels(self.mw.col).values()))
|
|
|
|
form.newSpread.setCurrentIndex(scheduling.new_review_mix)
|
|
|
|
form.dayLearnFirst.setChecked(scheduling.day_learn_first)
|
|
|
|
form.dayOffset.setValue(scheduling.rollover)
|
|
|
|
if scheduling.scheduler_version < 2:
|
|
|
|
form.dayLearnFirst.setVisible(False)
|
|
|
|
form.legacy_timezone.setVisible(False)
|
2020-03-23 07:08:06 +01:00
|
|
|
else:
|
2021-03-10 09:20:37 +01:00
|
|
|
form.legacy_timezone.setChecked(not scheduling.new_timezone)
|
|
|
|
|
|
|
|
reviewing = self.prefs.reviewing
|
|
|
|
form.timeLimit.setValue(int(reviewing.time_limit_secs / 60.0))
|
|
|
|
form.showEstimates.setChecked(reviewing.show_intervals_on_buttons)
|
|
|
|
form.showProgress.setChecked(reviewing.show_remaining_due_counts)
|
|
|
|
form.showPlayButtons.setChecked(not reviewing.hide_audio_play_buttons)
|
|
|
|
form.interrupt_audio.setChecked(reviewing.interrupt_audio_when_answering)
|
|
|
|
|
|
|
|
editing = self.prefs.editing
|
|
|
|
form.useCurrent.setCurrentIndex(
|
|
|
|
0 if editing.adding_defaults_to_current_deck else 1
|
2020-11-17 08:42:43 +01:00
|
|
|
)
|
2021-03-10 09:20:37 +01:00
|
|
|
form.paste_strips_formatting.setChecked(editing.paste_strips_formatting)
|
|
|
|
form.pastePNG.setChecked(editing.paste_images_as_png)
|
|
|
|
|
|
|
|
def update_collection(self) -> None:
|
|
|
|
form = self.form
|
|
|
|
|
|
|
|
scheduling = self.prefs.scheduling
|
|
|
|
scheduling.new_review_mix = form.newSpread.currentIndex()
|
|
|
|
scheduling.learn_ahead_secs = form.lrnCutoff.value() * 60
|
|
|
|
scheduling.day_learn_first = form.dayLearnFirst.isChecked()
|
|
|
|
scheduling.rollover = form.dayOffset.value()
|
|
|
|
scheduling.new_timezone = not form.legacy_timezone.isChecked()
|
|
|
|
|
|
|
|
reviewing = self.prefs.reviewing
|
|
|
|
reviewing.show_remaining_due_counts = form.showProgress.isChecked()
|
|
|
|
reviewing.show_intervals_on_buttons = form.showEstimates.isChecked()
|
|
|
|
reviewing.time_limit_secs = form.timeLimit.value() * 60
|
|
|
|
reviewing.hide_audio_play_buttons = not self.form.showPlayButtons.isChecked()
|
|
|
|
reviewing.interrupt_audio_when_answering = self.form.interrupt_audio.isChecked()
|
|
|
|
|
|
|
|
editing = self.prefs.editing
|
|
|
|
editing.adding_defaults_to_current_deck = not form.useCurrent.currentIndex()
|
|
|
|
editing.paste_images_as_png = self.form.pastePNG.isChecked()
|
|
|
|
editing.paste_strips_formatting = self.form.paste_strips_formatting.isChecked()
|
2016-04-05 03:02:01 +02:00
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
self.mw.col.set_preferences(self.prefs)
|
|
|
|
self.mw.apply_collection_options()
|
2020-05-11 10:35:15 +02:00
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
# Preferences stored in the profile
|
|
|
|
######################################################################
|
2020-05-11 10:35:15 +02:00
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
def setup_profile(self) -> None:
|
|
|
|
"Setup options stored in the user profile."
|
|
|
|
self.setup_recording_driver()
|
|
|
|
self.setup_network()
|
|
|
|
self.setup_backup()
|
2020-05-11 10:35:15 +02:00
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
def update_profile(self) -> None:
|
|
|
|
self.update_recording_driver()
|
|
|
|
self.update_network()
|
|
|
|
self.update_backup()
|
2020-05-11 10:35:15 +02:00
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
# Profile: recording driver
|
|
|
|
######################################################################
|
2018-01-20 06:32:52 +01:00
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
def setup_recording_driver(self) -> None:
|
|
|
|
self._recording_drivers = [
|
|
|
|
RecordingDriver.QtAudioInput,
|
|
|
|
RecordingDriver.PyAudio,
|
2020-12-22 04:01:06 +01:00
|
|
|
]
|
2021-03-10 09:20:37 +01:00
|
|
|
# The plan is to phase out PyAudio soon, so will hold off on
|
|
|
|
# making this string translatable for now.
|
|
|
|
self.form.recording_driver.addItems(
|
|
|
|
[
|
|
|
|
f"Voice recording driver: {driver.value}"
|
|
|
|
for driver in self._recording_drivers
|
|
|
|
]
|
|
|
|
)
|
|
|
|
self.form.recording_driver.setCurrentIndex(
|
|
|
|
self._recording_drivers.index(self.mw.pm.recording_driver())
|
2020-12-22 04:01:06 +01:00
|
|
|
)
|
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
def update_recording_driver(self) -> None:
|
|
|
|
new_audio_driver = self._recording_drivers[
|
|
|
|
self.form.recording_driver.currentIndex()
|
|
|
|
]
|
|
|
|
if self.mw.pm.recording_driver() != new_audio_driver:
|
|
|
|
self.mw.pm.set_recording_driver(new_audio_driver)
|
|
|
|
if new_audio_driver == RecordingDriver.PyAudio:
|
|
|
|
showInfo(
|
|
|
|
"""\
|
|
|
|
The PyAudio driver will likely be removed in a future update. If you find it works better \
|
|
|
|
for you than the default driver, please let us know on the Anki forums."""
|
|
|
|
)
|
2020-05-11 10:35:15 +02:00
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
# Profile: network
|
2012-12-21 08:51:59 +01:00
|
|
|
######################################################################
|
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
def setup_network(self) -> None:
|
2021-03-26 04:48:26 +01:00
|
|
|
self.form.media_log.setText(tr.sync_media_log_button())
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.form.media_log.clicked, self.on_media_log)
|
2019-12-23 01:34:10 +01:00
|
|
|
self.form.syncOnProgramOpen.setChecked(self.prof["autoSync"])
|
|
|
|
self.form.syncMedia.setChecked(self.prof["syncMedia"])
|
2020-07-01 03:35:24 +02:00
|
|
|
self.form.autoSyncMedia.setChecked(self.mw.pm.auto_sync_media_minutes() != 0)
|
2019-12-23 01:34:10 +01:00
|
|
|
if not self.prof["syncKey"]:
|
2021-03-10 09:20:37 +01:00
|
|
|
self._hide_sync_auth_settings()
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2019-12-23 01:34:10 +01:00
|
|
|
self.form.syncUser.setText(self.prof.get("syncUser", ""))
|
2021-03-10 09:20:37 +01:00
|
|
|
qconnect(self.form.syncDeauth.clicked, self.sync_logout)
|
2021-03-26 04:48:26 +01:00
|
|
|
self.form.syncDeauth.setText(tr.sync_log_out_button())
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def on_media_log(self) -> None:
|
2020-02-27 03:22:24 +01:00
|
|
|
self.mw.media_syncer.show_sync_log()
|
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
def _hide_sync_auth_settings(self) -> None:
|
2013-04-15 06:46:07 +02:00
|
|
|
self.form.syncDeauth.setVisible(False)
|
2013-10-03 23:09:36 +02:00
|
|
|
self.form.syncUser.setText("")
|
2019-12-23 01:34:10 +01:00
|
|
|
self.form.syncLabel.setText(
|
2021-03-26 04:48:26 +01:00
|
|
|
tr.preferences_synchronizationnot_currently_enabled_click_the_sync()
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
def sync_logout(self) -> None:
|
2020-02-15 08:48:35 +01:00
|
|
|
if self.mw.media_syncer.is_syncing():
|
|
|
|
showWarning("Can't log out while sync in progress.")
|
|
|
|
return
|
2019-12-23 01:34:10 +01:00
|
|
|
self.prof["syncKey"] = None
|
2020-02-15 08:48:35 +01:00
|
|
|
self.mw.col.media.force_resync()
|
2021-03-10 09:20:37 +01:00
|
|
|
self._hide_sync_auth_settings()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
def update_network(self) -> None:
|
2019-12-23 01:34:10 +01:00
|
|
|
self.prof["autoSync"] = self.form.syncOnProgramOpen.isChecked()
|
|
|
|
self.prof["syncMedia"] = self.form.syncMedia.isChecked()
|
2020-07-01 06:01:24 +02:00
|
|
|
self.mw.pm.set_auto_sync_media_minutes(
|
|
|
|
self.form.autoSyncMedia.isChecked() and 15 or 0
|
|
|
|
)
|
2013-05-14 08:23:50 +02:00
|
|
|
if self.form.fullSync.isChecked():
|
2014-11-03 08:28:12 +01:00
|
|
|
self.mw.col.modSchema(check=False)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
# Profile: backup
|
2012-12-21 08:51:59 +01:00
|
|
|
######################################################################
|
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
def setup_backup(self) -> None:
|
2019-12-23 01:34:10 +01:00
|
|
|
self.form.numBackups.setValue(self.prof["numBackups"])
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
def update_backup(self) -> None:
|
2019-12-23 01:34:10 +01:00
|
|
|
self.prof["numBackups"] = self.form.numBackups.value()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
# Global preferences
|
2012-12-21 08:51:59 +01:00
|
|
|
######################################################################
|
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
def setup_global(self) -> None:
|
|
|
|
"Setup options global to all profiles."
|
2020-12-15 14:09:19 +01:00
|
|
|
self.form.uiScale.setValue(int(self.mw.pm.uiScale() * 100))
|
2020-01-23 06:08:10 +01:00
|
|
|
self.form.nightMode.setChecked(self.mw.pm.night_mode())
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
self.setup_language()
|
|
|
|
self.setup_video_driver()
|
|
|
|
|
|
|
|
self.setupOptions()
|
|
|
|
|
|
|
|
def update_global(self) -> None:
|
2020-01-23 06:08:10 +01:00
|
|
|
restart_required = False
|
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
self.update_video_driver()
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
newScale = self.form.uiScale.value() / 100
|
2019-12-19 00:58:16 +01:00
|
|
|
if newScale != self.mw.pm.uiScale():
|
|
|
|
self.mw.pm.setUiScale(newScale)
|
2020-01-23 06:08:10 +01:00
|
|
|
restart_required = True
|
|
|
|
|
2020-04-15 13:37:16 +02:00
|
|
|
if self.mw.pm.night_mode() != self.form.nightMode.isChecked():
|
|
|
|
self.mw.pm.set_night_mode(not self.mw.pm.night_mode())
|
|
|
|
restart_required = True
|
2020-01-23 06:08:10 +01:00
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
if restart_required:
|
2021-03-26 04:48:26 +01:00
|
|
|
showInfo(tr.preferences_changes_will_take_effect_when_you())
|
2020-12-21 06:04:22 +01:00
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
self.updateOptions()
|
|
|
|
|
|
|
|
# legacy - one of Henrik's add-ons is currently wrapping them
|
|
|
|
|
|
|
|
def setupOptions(self) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def updateOptions(self) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Global: language
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def setup_language(self) -> None:
|
|
|
|
f = self.form
|
|
|
|
f.lang.addItems([x[0] for x in anki.lang.langs])
|
|
|
|
f.lang.setCurrentIndex(self.current_lang_index())
|
|
|
|
qconnect(f.lang.currentIndexChanged, self.on_language_index_changed)
|
|
|
|
|
|
|
|
def current_lang_index(self) -> int:
|
|
|
|
codes = [x[1] for x in anki.lang.langs]
|
|
|
|
lang = anki.lang.currentLang
|
|
|
|
if lang in anki.lang.compatMap:
|
|
|
|
lang = anki.lang.compatMap[lang]
|
|
|
|
else:
|
|
|
|
lang = lang.replace("-", "_")
|
|
|
|
try:
|
|
|
|
return codes.index(lang)
|
|
|
|
except:
|
|
|
|
return codes.index("en_US")
|
|
|
|
|
|
|
|
def on_language_index_changed(self, idx: int) -> None:
|
|
|
|
code = anki.lang.langs[idx][1]
|
|
|
|
self.mw.pm.setLang(code)
|
2021-03-26 04:48:26 +01:00
|
|
|
showInfo(tr.preferences_please_restart_anki_to_complete_language(), parent=self)
|
2021-03-10 09:20:37 +01:00
|
|
|
|
|
|
|
# Global: video driver
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def setup_video_driver(self) -> None:
|
|
|
|
self.video_drivers = VideoDriver.all_for_platform()
|
|
|
|
names = [
|
2021-03-26 05:21:04 +01:00
|
|
|
tr.preferences_video_driver(driver=video_driver_name_for_platform(d))
|
2021-03-10 09:20:37 +01:00
|
|
|
for d in self.video_drivers
|
2020-12-21 06:04:22 +01:00
|
|
|
]
|
2021-03-10 09:20:37 +01:00
|
|
|
self.form.video_driver.addItems(names)
|
|
|
|
self.form.video_driver.setCurrentIndex(
|
|
|
|
self.video_drivers.index(self.mw.pm.video_driver())
|
|
|
|
)
|
2020-02-02 23:32:07 +01:00
|
|
|
|
2021-03-10 09:20:37 +01:00
|
|
|
def update_video_driver(self) -> None:
|
|
|
|
new_driver = self.video_drivers[self.form.video_driver.currentIndex()]
|
|
|
|
if new_driver != self.mw.pm.video_driver():
|
|
|
|
self.mw.pm.set_video_driver(new_driver)
|
2021-03-26 04:48:26 +01:00
|
|
|
showInfo(tr.preferences_changes_will_take_effect_when_you())
|
2021-03-10 09:20:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
def video_driver_name_for_platform(driver: VideoDriver) -> str:
|
|
|
|
if driver == VideoDriver.ANGLE:
|
2021-03-26 04:48:26 +01:00
|
|
|
return tr.preferences_video_driver_angle()
|
2021-03-10 09:20:37 +01:00
|
|
|
elif driver == VideoDriver.Software:
|
|
|
|
if isMac:
|
2021-03-26 04:48:26 +01:00
|
|
|
return tr.preferences_video_driver_software_mac()
|
2021-03-10 09:20:37 +01:00
|
|
|
else:
|
2021-03-26 04:48:26 +01:00
|
|
|
return tr.preferences_video_driver_software_other()
|
2021-03-10 09:20:37 +01:00
|
|
|
else:
|
|
|
|
if isMac:
|
2021-03-26 04:48:26 +01:00
|
|
|
return tr.preferences_video_driver_opengl_mac()
|
2021-03-10 09:20:37 +01:00
|
|
|
else:
|
2021-03-26 04:48:26 +01:00
|
|
|
return tr.preferences_video_driver_opengl_other()
|