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
|
2016-06-22 06:42:41 +02:00
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-10-28 10:46:45 +02:00
|
|
|
import sys
|
|
|
|
|
|
|
|
if sys.version_info[0] < 3 or sys.version_info[1] < 9:
|
|
|
|
raise Exception("Anki requires Python 3.9+")
|
|
|
|
|
|
|
|
# ensure unicode filenames are supported
|
|
|
|
try:
|
|
|
|
"テスト".encode(sys.getfilesystemencoding())
|
|
|
|
except UnicodeEncodeError as exc:
|
|
|
|
raise Exception("Anki requires a UTF-8 locale.") from exc
|
|
|
|
|
|
|
|
from .package import packaged_build_setup
|
|
|
|
|
|
|
|
packaged_build_setup()
|
|
|
|
|
2021-12-09 07:06:28 +01:00
|
|
|
# syncserver needs to be run before Qt loaded
|
|
|
|
if "--syncserver" in sys.argv:
|
|
|
|
from anki.syncserver import serve
|
|
|
|
|
|
|
|
serve()
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
2019-03-04 07:45:29 +01:00
|
|
|
import argparse
|
2016-05-12 06:45:35 +02:00
|
|
|
import builtins
|
2020-11-25 02:54:41 +01:00
|
|
|
import cProfile
|
2019-12-20 10:19:03 +01:00
|
|
|
import getpass
|
|
|
|
import locale
|
2019-12-23 14:37:27 +01:00
|
|
|
import os
|
2019-12-20 10:19:03 +01:00
|
|
|
import tempfile
|
2019-12-23 14:37:27 +01:00
|
|
|
import traceback
|
2022-04-06 03:34:57 +02:00
|
|
|
from typing import TYPE_CHECKING, Any, Callable, Optional, cast
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
import anki.lang
|
2021-01-31 06:55:08 +01:00
|
|
|
from anki._backend import RustBackend
|
2021-07-11 06:51:25 +02:00
|
|
|
from anki.buildinfo import version as _version
|
|
|
|
from anki.collection import Collection
|
2012-12-21 10:04:26 +01:00
|
|
|
from anki.consts import HELP_SITE
|
2021-11-25 00:06:16 +01:00
|
|
|
from anki.utils import checksum, is_lin, is_mac
|
2021-08-28 20:37:31 +02:00
|
|
|
from aqt import gui_hooks
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2021-10-10 06:27:28 +02:00
|
|
|
from aqt.utils import TR, tr
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2022-04-06 03:34:57 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
import aqt.profiles
|
|
|
|
|
2021-07-11 06:51:25 +02:00
|
|
|
# compat aliases
|
|
|
|
anki.version = _version # type: ignore
|
|
|
|
anki.Collection = Collection # type: ignore
|
|
|
|
|
2020-08-01 03:30:16 +02:00
|
|
|
# we want to be able to print unicode debug info to console without
|
|
|
|
# fear of a traceback on systems with the console set to ASCII
|
2020-08-06 01:05:26 +02:00
|
|
|
try:
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8") # type: ignore
|
|
|
|
sys.stderr.reconfigure(encoding="utf-8") # type: ignore
|
|
|
|
except AttributeError:
|
2022-04-09 08:48:06 +02:00
|
|
|
if is_win:
|
|
|
|
# On Windows without console; add a mock writer. The stderr
|
|
|
|
# writer will be overwritten when ErrorHandler is initialized.
|
|
|
|
sys.stderr = sys.stdout = open(os.devnull, "w", encoding="utf8")
|
2020-08-01 03:30:16 +02:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
appVersion = _version
|
2020-02-29 01:37:46 +01:00
|
|
|
appWebsite = "https://apps.ankiweb.net/"
|
2022-08-31 10:35:53 +02:00
|
|
|
appWebsiteDownloadSection = "https://apps.ankiweb.net/#download"
|
2020-02-29 01:37:46 +01:00
|
|
|
appDonate = "https://apps.ankiweb.net/support/"
|
2020-02-29 12:43:37 +01:00
|
|
|
appShared = "https://ankiweb.net/shared/"
|
2019-12-23 01:34:10 +01:00
|
|
|
appUpdate = "https://ankiweb.net/update/desktop"
|
|
|
|
appHelpSite = HELP_SITE
|
2019-12-19 00:58:16 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
from aqt.main import AnkiQt # isort:skip
|
2021-10-05 01:08:48 +02:00
|
|
|
from aqt.profiles import ProfileManager, VideoDriver # isort:skip
|
2019-12-19 00:58:16 +01:00
|
|
|
|
2020-11-25 02:54:41 +01:00
|
|
|
profiler: Optional[cProfile.Profile] = None
|
2019-12-23 01:34:10 +01:00
|
|
|
mw: Optional[AnkiQt] = None # set on init
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-10-05 00:35:35 +02:00
|
|
|
import aqt.forms
|
2013-10-04 00:37:19 +02:00
|
|
|
|
2017-09-10 07:15:12 +02:00
|
|
|
# Dialog manager
|
2017-08-16 04:45:33 +02:00
|
|
|
##########################################################################
|
2017-09-10 07:15:12 +02:00
|
|
|
# ensures only one copy of the window is open at once, and provides
|
|
|
|
# a way for dialogs to clean up asynchronously when collection closes
|
|
|
|
|
|
|
|
# to integrate a new window:
|
|
|
|
# - add it to _dialogs
|
|
|
|
# - define close behaviour, by either:
|
|
|
|
# -- setting silentlyClose=True to have it close immediately
|
|
|
|
# -- define a closeWithCallback() method
|
|
|
|
# - have the window opened via aqt.dialogs.open(<name>, self)
|
2018-11-28 10:16:23 +01:00
|
|
|
# - have a method reopen(*args), called if the user ask to open the window a second time. Arguments passed are the same than for original opening.
|
2017-09-10 07:15:12 +02:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
# - make preferences modal? cmd+q does wrong thing
|
2017-09-10 07:15:12 +02:00
|
|
|
|
|
|
|
|
2021-03-24 04:17:12 +01:00
|
|
|
from aqt import addcards, addons, browser, editcurrent, filtered_deck # isort:skip
|
2020-02-04 02:41:20 +01:00
|
|
|
from aqt import stats, about, preferences, mediasync # isort:skip
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2017-02-06 23:21:33 +01:00
|
|
|
class DialogManager:
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
_dialogs: dict[str, list] = {
|
2017-09-10 07:15:12 +02:00
|
|
|
"AddCards": [addcards.AddCards, None],
|
2020-12-26 18:07:37 +01:00
|
|
|
"AddonsDialog": [addons.AddonsDialog, None],
|
2017-09-10 07:15:12 +02:00
|
|
|
"Browser": [browser.Browser, None],
|
|
|
|
"EditCurrent": [editcurrent.EditCurrent, None],
|
2021-03-24 04:17:12 +01:00
|
|
|
"FilteredDeckConfigDialog": [filtered_deck.FilteredDeckConfigDialog, None],
|
2017-09-10 07:15:12 +02:00
|
|
|
"DeckStats": [stats.DeckStats, None],
|
2020-06-30 09:08:10 +02:00
|
|
|
"NewDeckStats": [stats.NewDeckStats, None],
|
2017-09-10 07:15:12 +02:00
|
|
|
"About": [about.show, None],
|
|
|
|
"Preferences": [preferences.Preferences, None],
|
2020-02-04 02:48:51 +01:00
|
|
|
"sync_log": [mediasync.MediaSyncDialog, None],
|
2017-09-10 07:15:12 +02:00
|
|
|
}
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 08:50:19 +01:00
|
|
|
def open(self, name: str, *args: Any, **kwargs: Any) -> Any:
|
2012-12-21 08:51:59 +01:00
|
|
|
(creator, instance) = self._dialogs[name]
|
|
|
|
if instance:
|
2021-10-05 05:53:01 +02:00
|
|
|
if instance.windowState() & Qt.WindowState.WindowMinimized:
|
|
|
|
instance.setWindowState(
|
|
|
|
instance.windowState() & ~Qt.WindowState.WindowMinimized
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
instance.activateWindow()
|
|
|
|
instance.raise_()
|
2019-12-23 01:34:10 +01:00
|
|
|
if hasattr(instance, "reopen"):
|
2021-02-01 08:50:19 +01:00
|
|
|
instance.reopen(*args, **kwargs)
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2021-02-01 08:50:19 +01:00
|
|
|
instance = creator(*args, **kwargs)
|
2012-12-21 08:51:59 +01:00
|
|
|
self._dialogs[name][1] = instance
|
2021-08-28 20:37:31 +02:00
|
|
|
gui_hooks.dialog_manager_did_open_dialog(self, name, instance)
|
2021-02-01 08:50:19 +01:00
|
|
|
return instance
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def markClosed(self, name: str) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self._dialogs[name] = [self._dialogs[name][0], None]
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def allClosed(self) -> bool:
|
2017-08-16 04:45:33 +02:00
|
|
|
return not any(x[1] for x in self._dialogs.values())
|
|
|
|
|
2020-03-07 17:29:37 +01:00
|
|
|
def closeAll(self, onsuccess: Callable[[], None]) -> Optional[bool]:
|
2017-08-16 04:45:33 +02:00
|
|
|
# can we close immediately?
|
|
|
|
if self.allClosed():
|
|
|
|
onsuccess()
|
2020-03-07 17:29:37 +01:00
|
|
|
return None
|
2017-08-16 04:45:33 +02:00
|
|
|
|
|
|
|
# ask all windows to close and await a reply
|
|
|
|
for (name, (creator, instance)) in self._dialogs.items():
|
|
|
|
if not instance:
|
|
|
|
continue
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def callback() -> None:
|
2017-08-16 04:45:33 +02:00
|
|
|
if self.allClosed():
|
|
|
|
onsuccess()
|
|
|
|
else:
|
|
|
|
# still waiting for others to close
|
|
|
|
pass
|
|
|
|
|
2017-09-10 07:15:12 +02:00
|
|
|
if getattr(instance, "silentlyClose", False):
|
|
|
|
instance.close()
|
|
|
|
callback()
|
|
|
|
else:
|
|
|
|
instance.closeWithCallback(callback)
|
2017-08-16 04:45:33 +02:00
|
|
|
|
2013-04-11 12:23:32 +02:00
|
|
|
return True
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-03-07 17:35:09 +01:00
|
|
|
def register_dialog(
|
|
|
|
self, name: str, creator: Union[Callable, type], instance: Optional[Any] = None
|
2021-02-02 14:30:53 +01:00
|
|
|
) -> None:
|
2020-03-07 17:35:09 +01:00
|
|
|
"""Allows add-ons to register a custom dialog to be managed by Anki's dialog
|
2020-03-07 17:43:21 +01:00
|
|
|
manager, which ensures that only one copy of the window is open at once,
|
|
|
|
and that the dialog cleans up asynchronously when the collection closes
|
2020-08-31 05:29:28 +02:00
|
|
|
|
2020-03-07 17:43:21 +01:00
|
|
|
Please note that dialogs added in this manner need to define a close behavior
|
|
|
|
by either:
|
2020-08-31 05:29:28 +02:00
|
|
|
|
2020-03-07 17:43:21 +01:00
|
|
|
- setting `dialog.silentlyClose = True` to have it close immediately
|
|
|
|
- define a `dialog.closeWithCallback()` method that is called when closed
|
|
|
|
by the dialog manager
|
2020-08-31 05:29:28 +02:00
|
|
|
|
2020-03-07 17:43:21 +01:00
|
|
|
TODO?: Implement more restrictive type check to ensure these requirements
|
|
|
|
are met
|
2020-08-31 05:29:28 +02:00
|
|
|
|
2020-03-07 17:35:09 +01:00
|
|
|
Arguments:
|
|
|
|
name {str} -- Name/identifier of the dialog in question
|
|
|
|
creator {Union[Callable, type]} -- A class or function to create new
|
|
|
|
dialog instances with
|
2020-08-31 05:29:28 +02:00
|
|
|
|
2020-03-07 17:35:09 +01:00
|
|
|
Keyword Arguments:
|
|
|
|
instance {Optional[Any]} -- An optional existing instance of the dialog
|
|
|
|
(default: {None})
|
|
|
|
"""
|
|
|
|
self._dialogs[name] = [creator, instance]
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
dialogs = DialogManager()
|
|
|
|
|
|
|
|
# Language handling
|
|
|
|
##########################################################################
|
|
|
|
# Qt requires its translator to be installed before any GUI widgets are
|
2020-11-18 04:22:51 +01:00
|
|
|
# loaded, and we need the Qt language to match the i18n language or
|
2012-12-21 08:51:59 +01:00
|
|
|
# translated shortcuts will not work.
|
|
|
|
|
2020-03-14 00:45:00 +01:00
|
|
|
# A reference to the Qt translator needs to be held to prevent it from
|
|
|
|
# being immediately deallocated.
|
2019-12-20 06:07:40 +01:00
|
|
|
_qtrans: Optional[QTranslator] = None
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-03-14 00:45:00 +01:00
|
|
|
def setupLangAndBackend(
|
2020-11-21 03:16:26 +01:00
|
|
|
pm: ProfileManager,
|
|
|
|
app: QApplication,
|
|
|
|
force: Optional[str] = None,
|
|
|
|
firstTime: bool = False,
|
2020-03-14 00:45:00 +01:00
|
|
|
) -> RustBackend:
|
2020-01-02 10:43:19 +01:00
|
|
|
global _qtrans
|
2012-12-21 08:51:59 +01:00
|
|
|
try:
|
2019-12-23 01:34:10 +01:00
|
|
|
locale.setlocale(locale.LC_ALL, "")
|
2012-12-21 08:51:59 +01:00
|
|
|
except:
|
|
|
|
pass
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-02-16 06:14:08 +01:00
|
|
|
# add _ and ngettext globals used by legacy code
|
2021-02-02 14:30:53 +01:00
|
|
|
def fn__(arg) -> None: # type: ignore
|
2019-03-04 03:08:48 +01:00
|
|
|
print("".join(traceback.format_stack()[-2]))
|
2020-11-18 02:53:33 +01:00
|
|
|
print("_ global will break in the future; please see anki/lang.py")
|
|
|
|
return arg
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2021-02-02 14:30:53 +01:00
|
|
|
def fn_ngettext(a, b, c) -> None: # type: ignore
|
2019-03-07 09:34:22 +01:00
|
|
|
print("".join(traceback.format_stack()[-2]))
|
2020-11-18 02:53:33 +01:00
|
|
|
print("ngettext global will break in the future; please see anki/lang.py")
|
|
|
|
return b
|
2019-03-07 09:34:22 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
builtins.__dict__["_"] = fn__
|
|
|
|
builtins.__dict__["ngettext"] = fn_ngettext
|
2020-02-16 06:14:08 +01:00
|
|
|
|
|
|
|
# get lang and normalize into ja/zh-CN form
|
2020-11-21 03:16:26 +01:00
|
|
|
if firstTime:
|
|
|
|
lang = pm.meta["defaultLang"]
|
|
|
|
else:
|
|
|
|
lang = force or pm.meta["defaultLang"]
|
2020-02-16 06:14:08 +01:00
|
|
|
lang = anki.lang.lang_to_disk_lang(lang)
|
|
|
|
|
2022-12-13 01:51:13 +01:00
|
|
|
# set active language
|
|
|
|
anki.lang.set_lang(lang)
|
2020-02-16 06:14:08 +01:00
|
|
|
|
|
|
|
# switch direction for RTL languages
|
2022-12-13 01:34:34 +01:00
|
|
|
if anki.lang.is_rtl(lang):
|
2021-10-05 05:53:01 +02:00
|
|
|
app.setLayoutDirection(Qt.LayoutDirection.RightToLeft)
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2021-10-05 05:53:01 +02:00
|
|
|
app.setLayoutDirection(Qt.LayoutDirection.LeftToRight)
|
2020-02-16 06:14:08 +01:00
|
|
|
|
|
|
|
# load qt translations
|
2012-12-21 08:51:59 +01:00
|
|
|
_qtrans = QTranslator()
|
2021-02-04 11:28:25 +01:00
|
|
|
|
2021-11-25 00:06:16 +01:00
|
|
|
if is_mac and getattr(sys, "frozen", False):
|
2021-10-28 10:46:45 +02:00
|
|
|
qt_dir = os.path.join(sys.prefix, "../Resources/qt_translations")
|
2021-02-04 11:28:25 +01:00
|
|
|
else:
|
2021-10-05 05:53:01 +02:00
|
|
|
if qtmajor == 5:
|
|
|
|
qt_dir = QLibraryInfo.location(QLibraryInfo.TranslationsPath) # type: ignore
|
|
|
|
else:
|
|
|
|
qt_dir = QLibraryInfo.path(QLibraryInfo.LibraryPath.TranslationsPath)
|
2020-02-16 06:14:08 +01:00
|
|
|
qt_lang = lang.replace("-", "_")
|
2021-02-11 01:09:06 +01:00
|
|
|
if _qtrans.load(f"qtbase_{qt_lang}", qt_dir):
|
2012-12-21 08:51:59 +01:00
|
|
|
app.installTranslator(_qtrans)
|
|
|
|
|
2020-03-14 00:45:00 +01:00
|
|
|
return anki.lang.current_i18n
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# App initialisation
|
|
|
|
##########################################################################
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
class AnkiApp(QApplication):
|
|
|
|
|
|
|
|
# Single instance support on Win32/Linux
|
|
|
|
##################################################
|
|
|
|
|
2016-05-31 10:51:40 +02:00
|
|
|
appMsg = pyqtSignal(str)
|
|
|
|
|
2021-02-11 01:09:06 +01:00
|
|
|
KEY = f"anki{checksum(getpass.getuser())}"
|
2018-10-28 05:17:16 +01:00
|
|
|
TMOUT = 30000
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def __init__(self, argv: list[str]) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
QApplication.__init__(self, argv)
|
Redesign Qt widgets with stylesheets (#2050)
* Remove --medium-border variable
* Implement color palette using Sass maps
I hand-picked the gray tones, the other colors are from the Tailwind CSS v3 palette.
Significant changes:
- light theme is brighter
- dark theme is darker
- borders are softer
I also deleted some platform- and night-mode-specific code.
* Use custom colors for note view switch
* Use same placeholder color for all inputs
* Skew color palette for more dark values
by removing gray[3], which wasn't used anywhere. Slight adjustments were made to the darker tones.
* Adjust frame- window- and border colors
* Give deck browser entries --frame-bg as background color
* Define styling for QComboBox and QLineEdit globally
* Experiment with CSS filter for inline-colors
Inside darker inputs, some colors like dark blue will be hard to read, so we could try to improve text-color contrast with global adjustments depending on the theme.
* Use different map structure for _vars.scss
after @hgiesel's idea: https://github.com/ankitects/anki/pull/2016#discussion_r947087871
* Move custom QLineEdit styles out of searchbar.py
* Merge branch 'main' into color-palette
* Revert QComboBox stylesheet override
* Align gray color palette more with macOS
* Adjust light theme
* Add custom styling for Qt controls
* Use --slightly-grey-text for options tab color
* Replace gray tones with more neutral values
* Improve categorization of global colors
by renaming almost all of them and sorting them into separate maps.
* Saturate highlight-bg in light theme
* Tweak gray tones
* Adjust box-shadow of EditingArea to make fields look inset
* Add Sass functions to access color palette and semantic variables
in response to https://github.com/ankitects/anki/pull/2016#issuecomment-1220571076
* Showcase use of access functions in several locations
@hgiesel in buttons.scss I access the color palette directly. Is this what you meant by "... keep it local to the component, and possibly make it global at a later time ..."?
* Fix focus box shadow transition and remove default shadow for a cleaner look
I couldn't quite get the inset look the way I wanted, because inset box-shadows do not respect the border radius, therefore causing aliasing.
* Tweak light theme border and shadow colors
* Add functions and colors to base_lib
* Add vars_lib as dependency to base_lib and button_mixins_lib
* Improve uses of default-themed variables
* Use old --frame-bg color and use darker tone for canvas-default
* Return CSS var by default and add palette-of function for raw value
* Showcase use of palette-of function
The #{...} syntax is required only because the use cases are CSS var definitions. In other cases a simple palette-of(keyword, theme) would suffice.
* Light theme: decrease brightness of canvas-default and adjust fg-default
* Use canvas-inset variable for switch knob
* Adjust light theme
* Add back box-shadow to EditingArea
* Light theme: darken background and flatten transition
also set hue and saturation of gray-8 to 0 (like all the other grays).
* Reduce flag colors to single default value
* Tweak card/note accent colors
* Experiment with inset look for fields again
Is this too dark in night mode? It's the same color used for all other text inputs.
* Dark theme: make border-default one shade darker
* Tweak inset shadow color
* Dark theme: make border-faint darker than canvas-default
meaning two shades darker than it currently was.
* Fix PlainTextInput not expanding
* Dark theme: use less saturated flag colors
* Adjust gray tones
* Create stylesheet overrides for various Qt widgets
Including QPushButton, QComboBox, QSpinBox, QLineEdit, QListWidget, QTabWidget, QTreeWidget, QToolTip, QTableView, QScrollBar and sub-widgets.
* Make webview scrollbar look identical to Qt one
* Add blue colors for primary buttons
* Tweak disabled state of SpinBox button
* Apply styles to all platforms
mainly so people like @hgiesel can easily test the widget style overrides, but maybe you actually prefer them over the native ones, who knows :)
* Tweak webview button borders
* Add type annotations to eventFilter
* Adjust padding of QComboBox and its drop-down arrow
* Use isinstance for comparison
* Remove reimport of Any
* Revert "Merge branch 'redesign-test' into custom-qt-controls"
This reverts commit ff36297456b693a0d4b4b69f5f487ac1a01c1861, reversing
changes made to 6bb45355d143aa081d2d643933bd02ddc43206de.
* Add missing copyright header
* Left-align QTabWidget headers
* Exclude macOS from stylesheet overrides
* Fix failure to start on macOS (dae)
* Use standard macOS theme in dark mode (dae)
I believe this was originally behind a feature flag because the user
had to use a hack to get it to work
(https://forums.ankiweb.net/t/title-bar-dark-mode-fix-broken/1189),
and it did not work correctly when the system theme was changed.
Since the introduction of libankihelper and the app automatically
updating as the system theme changes, these issues no longer seem to
exist, and switching between light and dark appears to work consistently.
Pushed into this PR because it addresses the background color issue
mentioned in code review.
Closes #2054
2022-09-08 12:44:38 +02:00
|
|
|
self.installEventFilter(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
self._argv = argv
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def secondInstance(self) -> bool:
|
2013-10-04 00:37:19 +02:00
|
|
|
# we accept only one command line argument. if it's missing, send
|
|
|
|
# a blank screen to just raise the existing window
|
|
|
|
opts, args = parseArgs(self._argv)
|
|
|
|
buf = "raise"
|
|
|
|
if args and args[0]:
|
|
|
|
buf = os.path.abspath(args[0])
|
|
|
|
if self.sendMsg(buf):
|
2016-05-12 06:45:35 +02:00
|
|
|
print("Already running; reusing existing instance.")
|
2013-10-04 00:37:19 +02:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
# send failed, so we're the first instance or the
|
|
|
|
# previous instance died
|
2012-12-21 08:51:59 +01:00
|
|
|
QLocalServer.removeServer(self.KEY)
|
|
|
|
self._srv = QLocalServer(self)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self._srv.newConnection, self.onRecv)
|
2012-12-21 08:51:59 +01:00
|
|
|
self._srv.listen(self.KEY)
|
2013-10-04 00:37:19 +02:00
|
|
|
return False
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-02 14:30:53 +01:00
|
|
|
def sendMsg(self, txt: str) -> bool:
|
2012-12-21 08:51:59 +01:00
|
|
|
sock = QLocalSocket(self)
|
2021-10-05 05:53:01 +02:00
|
|
|
sock.connectToServer(self.KEY, QIODevice.OpenModeFlag.WriteOnly)
|
2012-12-21 08:51:59 +01:00
|
|
|
if not sock.waitForConnected(self.TMOUT):
|
2013-10-04 00:37:19 +02:00
|
|
|
# first instance or previous instance dead
|
|
|
|
return False
|
2016-05-31 10:51:40 +02:00
|
|
|
sock.write(txt.encode("utf8"))
|
2012-12-21 08:51:59 +01:00
|
|
|
if not sock.waitForBytesWritten(self.TMOUT):
|
2014-08-26 08:25:22 +02:00
|
|
|
# existing instance running but hung
|
2019-12-23 01:34:10 +01:00
|
|
|
QMessageBox.warning(
|
|
|
|
None,
|
2021-03-26 04:48:26 +01:00
|
|
|
tr.qt_misc_anki_is_running(),
|
|
|
|
tr.qt_misc_if_instance_is_not_responding(),
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2018-10-28 05:17:16 +01:00
|
|
|
|
|
|
|
sys.exit(1)
|
2012-12-21 08:51:59 +01:00
|
|
|
sock.disconnectFromServer()
|
2013-10-04 00:37:19 +02:00
|
|
|
return True
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def onRecv(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
sock = self._srv.nextPendingConnection()
|
|
|
|
if not sock.waitForReadyRead(self.TMOUT):
|
|
|
|
sys.stderr.write(sock.errorString())
|
|
|
|
return
|
2021-03-17 05:51:59 +01:00
|
|
|
path = bytes(cast(bytes, sock.readAll())).decode("utf8")
|
2020-08-02 02:16:54 +02:00
|
|
|
self.appMsg.emit(path) # type: ignore
|
2012-12-21 08:51:59 +01:00
|
|
|
sock.disconnectFromServer()
|
|
|
|
|
|
|
|
# OS X file/url handler
|
|
|
|
##################################################
|
|
|
|
|
2021-02-02 14:30:53 +01:00
|
|
|
def event(self, evt: QEvent) -> bool:
|
2021-10-05 05:53:01 +02:00
|
|
|
if evt.type() == QEvent.Type.FileOpen:
|
2020-08-02 02:16:54 +02:00
|
|
|
self.appMsg.emit(evt.file() or "raise") # type: ignore
|
2012-12-21 08:51:59 +01:00
|
|
|
return True
|
|
|
|
return QApplication.event(self, evt)
|
|
|
|
|
Redesign Qt widgets with stylesheets (#2050)
* Remove --medium-border variable
* Implement color palette using Sass maps
I hand-picked the gray tones, the other colors are from the Tailwind CSS v3 palette.
Significant changes:
- light theme is brighter
- dark theme is darker
- borders are softer
I also deleted some platform- and night-mode-specific code.
* Use custom colors for note view switch
* Use same placeholder color for all inputs
* Skew color palette for more dark values
by removing gray[3], which wasn't used anywhere. Slight adjustments were made to the darker tones.
* Adjust frame- window- and border colors
* Give deck browser entries --frame-bg as background color
* Define styling for QComboBox and QLineEdit globally
* Experiment with CSS filter for inline-colors
Inside darker inputs, some colors like dark blue will be hard to read, so we could try to improve text-color contrast with global adjustments depending on the theme.
* Use different map structure for _vars.scss
after @hgiesel's idea: https://github.com/ankitects/anki/pull/2016#discussion_r947087871
* Move custom QLineEdit styles out of searchbar.py
* Merge branch 'main' into color-palette
* Revert QComboBox stylesheet override
* Align gray color palette more with macOS
* Adjust light theme
* Add custom styling for Qt controls
* Use --slightly-grey-text for options tab color
* Replace gray tones with more neutral values
* Improve categorization of global colors
by renaming almost all of them and sorting them into separate maps.
* Saturate highlight-bg in light theme
* Tweak gray tones
* Adjust box-shadow of EditingArea to make fields look inset
* Add Sass functions to access color palette and semantic variables
in response to https://github.com/ankitects/anki/pull/2016#issuecomment-1220571076
* Showcase use of access functions in several locations
@hgiesel in buttons.scss I access the color palette directly. Is this what you meant by "... keep it local to the component, and possibly make it global at a later time ..."?
* Fix focus box shadow transition and remove default shadow for a cleaner look
I couldn't quite get the inset look the way I wanted, because inset box-shadows do not respect the border radius, therefore causing aliasing.
* Tweak light theme border and shadow colors
* Add functions and colors to base_lib
* Add vars_lib as dependency to base_lib and button_mixins_lib
* Improve uses of default-themed variables
* Use old --frame-bg color and use darker tone for canvas-default
* Return CSS var by default and add palette-of function for raw value
* Showcase use of palette-of function
The #{...} syntax is required only because the use cases are CSS var definitions. In other cases a simple palette-of(keyword, theme) would suffice.
* Light theme: decrease brightness of canvas-default and adjust fg-default
* Use canvas-inset variable for switch knob
* Adjust light theme
* Add back box-shadow to EditingArea
* Light theme: darken background and flatten transition
also set hue and saturation of gray-8 to 0 (like all the other grays).
* Reduce flag colors to single default value
* Tweak card/note accent colors
* Experiment with inset look for fields again
Is this too dark in night mode? It's the same color used for all other text inputs.
* Dark theme: make border-default one shade darker
* Tweak inset shadow color
* Dark theme: make border-faint darker than canvas-default
meaning two shades darker than it currently was.
* Fix PlainTextInput not expanding
* Dark theme: use less saturated flag colors
* Adjust gray tones
* Create stylesheet overrides for various Qt widgets
Including QPushButton, QComboBox, QSpinBox, QLineEdit, QListWidget, QTabWidget, QTreeWidget, QToolTip, QTableView, QScrollBar and sub-widgets.
* Make webview scrollbar look identical to Qt one
* Add blue colors for primary buttons
* Tweak disabled state of SpinBox button
* Apply styles to all platforms
mainly so people like @hgiesel can easily test the widget style overrides, but maybe you actually prefer them over the native ones, who knows :)
* Tweak webview button borders
* Add type annotations to eventFilter
* Adjust padding of QComboBox and its drop-down arrow
* Use isinstance for comparison
* Remove reimport of Any
* Revert "Merge branch 'redesign-test' into custom-qt-controls"
This reverts commit ff36297456b693a0d4b4b69f5f487ac1a01c1861, reversing
changes made to 6bb45355d143aa081d2d643933bd02ddc43206de.
* Add missing copyright header
* Left-align QTabWidget headers
* Exclude macOS from stylesheet overrides
* Fix failure to start on macOS (dae)
* Use standard macOS theme in dark mode (dae)
I believe this was originally behind a feature flag because the user
had to use a hack to get it to work
(https://forums.ankiweb.net/t/title-bar-dark-mode-fix-broken/1189),
and it did not work correctly when the system theme was changed.
Since the introduction of libankihelper and the app automatically
updating as the system theme changes, these issues no longer seem to
exist, and switching between light and dark appears to work consistently.
Pushed into this PR because it addresses the background color issue
mentioned in code review.
Closes #2054
2022-09-08 12:44:38 +02:00
|
|
|
# Global cursor: pointer for Qt buttons
|
|
|
|
##################################################
|
|
|
|
|
|
|
|
def eventFilter(self, src: Any, evt: QEvent) -> bool:
|
2022-10-12 06:29:06 +02:00
|
|
|
pointer_classes = (
|
|
|
|
QPushButton,
|
|
|
|
QCheckBox,
|
|
|
|
QRadioButton,
|
|
|
|
QMenu,
|
|
|
|
# classes with PyQt5 compatibility proxy
|
|
|
|
without_qt5_compat_wrapper(QToolButton),
|
|
|
|
without_qt5_compat_wrapper(QTabBar),
|
|
|
|
)
|
|
|
|
if evt.type() in [QEvent.Type.Enter, QEvent.Type.HoverEnter]:
|
|
|
|
if (isinstance(src, pointer_classes) and src.isEnabled()) or (
|
|
|
|
isinstance(src, without_qt5_compat_wrapper(QComboBox))
|
|
|
|
and not src.isEditable()
|
2022-09-20 08:34:15 +02:00
|
|
|
):
|
Redesign Qt widgets with stylesheets (#2050)
* Remove --medium-border variable
* Implement color palette using Sass maps
I hand-picked the gray tones, the other colors are from the Tailwind CSS v3 palette.
Significant changes:
- light theme is brighter
- dark theme is darker
- borders are softer
I also deleted some platform- and night-mode-specific code.
* Use custom colors for note view switch
* Use same placeholder color for all inputs
* Skew color palette for more dark values
by removing gray[3], which wasn't used anywhere. Slight adjustments were made to the darker tones.
* Adjust frame- window- and border colors
* Give deck browser entries --frame-bg as background color
* Define styling for QComboBox and QLineEdit globally
* Experiment with CSS filter for inline-colors
Inside darker inputs, some colors like dark blue will be hard to read, so we could try to improve text-color contrast with global adjustments depending on the theme.
* Use different map structure for _vars.scss
after @hgiesel's idea: https://github.com/ankitects/anki/pull/2016#discussion_r947087871
* Move custom QLineEdit styles out of searchbar.py
* Merge branch 'main' into color-palette
* Revert QComboBox stylesheet override
* Align gray color palette more with macOS
* Adjust light theme
* Add custom styling for Qt controls
* Use --slightly-grey-text for options tab color
* Replace gray tones with more neutral values
* Improve categorization of global colors
by renaming almost all of them and sorting them into separate maps.
* Saturate highlight-bg in light theme
* Tweak gray tones
* Adjust box-shadow of EditingArea to make fields look inset
* Add Sass functions to access color palette and semantic variables
in response to https://github.com/ankitects/anki/pull/2016#issuecomment-1220571076
* Showcase use of access functions in several locations
@hgiesel in buttons.scss I access the color palette directly. Is this what you meant by "... keep it local to the component, and possibly make it global at a later time ..."?
* Fix focus box shadow transition and remove default shadow for a cleaner look
I couldn't quite get the inset look the way I wanted, because inset box-shadows do not respect the border radius, therefore causing aliasing.
* Tweak light theme border and shadow colors
* Add functions and colors to base_lib
* Add vars_lib as dependency to base_lib and button_mixins_lib
* Improve uses of default-themed variables
* Use old --frame-bg color and use darker tone for canvas-default
* Return CSS var by default and add palette-of function for raw value
* Showcase use of palette-of function
The #{...} syntax is required only because the use cases are CSS var definitions. In other cases a simple palette-of(keyword, theme) would suffice.
* Light theme: decrease brightness of canvas-default and adjust fg-default
* Use canvas-inset variable for switch knob
* Adjust light theme
* Add back box-shadow to EditingArea
* Light theme: darken background and flatten transition
also set hue and saturation of gray-8 to 0 (like all the other grays).
* Reduce flag colors to single default value
* Tweak card/note accent colors
* Experiment with inset look for fields again
Is this too dark in night mode? It's the same color used for all other text inputs.
* Dark theme: make border-default one shade darker
* Tweak inset shadow color
* Dark theme: make border-faint darker than canvas-default
meaning two shades darker than it currently was.
* Fix PlainTextInput not expanding
* Dark theme: use less saturated flag colors
* Adjust gray tones
* Create stylesheet overrides for various Qt widgets
Including QPushButton, QComboBox, QSpinBox, QLineEdit, QListWidget, QTabWidget, QTreeWidget, QToolTip, QTableView, QScrollBar and sub-widgets.
* Make webview scrollbar look identical to Qt one
* Add blue colors for primary buttons
* Tweak disabled state of SpinBox button
* Apply styles to all platforms
mainly so people like @hgiesel can easily test the widget style overrides, but maybe you actually prefer them over the native ones, who knows :)
* Tweak webview button borders
* Add type annotations to eventFilter
* Adjust padding of QComboBox and its drop-down arrow
* Use isinstance for comparison
* Remove reimport of Any
* Revert "Merge branch 'redesign-test' into custom-qt-controls"
This reverts commit ff36297456b693a0d4b4b69f5f487ac1a01c1861, reversing
changes made to 6bb45355d143aa081d2d643933bd02ddc43206de.
* Add missing copyright header
* Left-align QTabWidget headers
* Exclude macOS from stylesheet overrides
* Fix failure to start on macOS (dae)
* Use standard macOS theme in dark mode (dae)
I believe this was originally behind a feature flag because the user
had to use a hack to get it to work
(https://forums.ankiweb.net/t/title-bar-dark-mode-fix-broken/1189),
and it did not work correctly when the system theme was changed.
Since the introduction of libankihelper and the app automatically
updating as the system theme changes, these issues no longer seem to
exist, and switching between light and dark appears to work consistently.
Pushed into this PR because it addresses the background color issue
mentioned in code review.
Closes #2054
2022-09-08 12:44:38 +02:00
|
|
|
self.setOverrideCursor(QCursor(Qt.CursorShape.PointingHandCursor))
|
|
|
|
else:
|
|
|
|
self.restoreOverrideCursor()
|
|
|
|
return False
|
|
|
|
|
2022-09-20 08:34:15 +02:00
|
|
|
elif evt.type() in [QEvent.Type.HoverLeave, QEvent.Type.Leave] or isinstance(
|
|
|
|
evt, QCloseEvent
|
|
|
|
):
|
Redesign Qt widgets with stylesheets (#2050)
* Remove --medium-border variable
* Implement color palette using Sass maps
I hand-picked the gray tones, the other colors are from the Tailwind CSS v3 palette.
Significant changes:
- light theme is brighter
- dark theme is darker
- borders are softer
I also deleted some platform- and night-mode-specific code.
* Use custom colors for note view switch
* Use same placeholder color for all inputs
* Skew color palette for more dark values
by removing gray[3], which wasn't used anywhere. Slight adjustments were made to the darker tones.
* Adjust frame- window- and border colors
* Give deck browser entries --frame-bg as background color
* Define styling for QComboBox and QLineEdit globally
* Experiment with CSS filter for inline-colors
Inside darker inputs, some colors like dark blue will be hard to read, so we could try to improve text-color contrast with global adjustments depending on the theme.
* Use different map structure for _vars.scss
after @hgiesel's idea: https://github.com/ankitects/anki/pull/2016#discussion_r947087871
* Move custom QLineEdit styles out of searchbar.py
* Merge branch 'main' into color-palette
* Revert QComboBox stylesheet override
* Align gray color palette more with macOS
* Adjust light theme
* Add custom styling for Qt controls
* Use --slightly-grey-text for options tab color
* Replace gray tones with more neutral values
* Improve categorization of global colors
by renaming almost all of them and sorting them into separate maps.
* Saturate highlight-bg in light theme
* Tweak gray tones
* Adjust box-shadow of EditingArea to make fields look inset
* Add Sass functions to access color palette and semantic variables
in response to https://github.com/ankitects/anki/pull/2016#issuecomment-1220571076
* Showcase use of access functions in several locations
@hgiesel in buttons.scss I access the color palette directly. Is this what you meant by "... keep it local to the component, and possibly make it global at a later time ..."?
* Fix focus box shadow transition and remove default shadow for a cleaner look
I couldn't quite get the inset look the way I wanted, because inset box-shadows do not respect the border radius, therefore causing aliasing.
* Tweak light theme border and shadow colors
* Add functions and colors to base_lib
* Add vars_lib as dependency to base_lib and button_mixins_lib
* Improve uses of default-themed variables
* Use old --frame-bg color and use darker tone for canvas-default
* Return CSS var by default and add palette-of function for raw value
* Showcase use of palette-of function
The #{...} syntax is required only because the use cases are CSS var definitions. In other cases a simple palette-of(keyword, theme) would suffice.
* Light theme: decrease brightness of canvas-default and adjust fg-default
* Use canvas-inset variable for switch knob
* Adjust light theme
* Add back box-shadow to EditingArea
* Light theme: darken background and flatten transition
also set hue and saturation of gray-8 to 0 (like all the other grays).
* Reduce flag colors to single default value
* Tweak card/note accent colors
* Experiment with inset look for fields again
Is this too dark in night mode? It's the same color used for all other text inputs.
* Dark theme: make border-default one shade darker
* Tweak inset shadow color
* Dark theme: make border-faint darker than canvas-default
meaning two shades darker than it currently was.
* Fix PlainTextInput not expanding
* Dark theme: use less saturated flag colors
* Adjust gray tones
* Create stylesheet overrides for various Qt widgets
Including QPushButton, QComboBox, QSpinBox, QLineEdit, QListWidget, QTabWidget, QTreeWidget, QToolTip, QTableView, QScrollBar and sub-widgets.
* Make webview scrollbar look identical to Qt one
* Add blue colors for primary buttons
* Tweak disabled state of SpinBox button
* Apply styles to all platforms
mainly so people like @hgiesel can easily test the widget style overrides, but maybe you actually prefer them over the native ones, who knows :)
* Tweak webview button borders
* Add type annotations to eventFilter
* Adjust padding of QComboBox and its drop-down arrow
* Use isinstance for comparison
* Remove reimport of Any
* Revert "Merge branch 'redesign-test' into custom-qt-controls"
This reverts commit ff36297456b693a0d4b4b69f5f487ac1a01c1861, reversing
changes made to 6bb45355d143aa081d2d643933bd02ddc43206de.
* Add missing copyright header
* Left-align QTabWidget headers
* Exclude macOS from stylesheet overrides
* Fix failure to start on macOS (dae)
* Use standard macOS theme in dark mode (dae)
I believe this was originally behind a feature flag because the user
had to use a hack to get it to work
(https://forums.ankiweb.net/t/title-bar-dark-mode-fix-broken/1189),
and it did not work correctly when the system theme was changed.
Since the introduction of libankihelper and the app automatically
updating as the system theme changes, these issues no longer seem to
exist, and switching between light and dark appears to work consistently.
Pushed into this PR because it addresses the background color issue
mentioned in code review.
Closes #2054
2022-09-08 12:44:38 +02:00
|
|
|
self.restoreOverrideCursor()
|
|
|
|
return False
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def parseArgs(argv: list[str]) -> tuple[argparse.Namespace, list[str]]:
|
2012-12-21 08:51:59 +01:00
|
|
|
"Returns (opts, args)."
|
2013-04-16 12:54:23 +02:00
|
|
|
# py2app fails to strip this in some instances, then anki dies
|
|
|
|
# as there's no such profile
|
2021-11-25 00:06:16 +01:00
|
|
|
if is_mac and len(argv) > 1 and argv[1].startswith("-psn"):
|
2013-04-16 12:54:23 +02:00
|
|
|
argv = [argv[0]]
|
2021-02-11 01:09:06 +01:00
|
|
|
parser = argparse.ArgumentParser(description=f"Anki {appVersion}")
|
2020-01-03 17:57:33 +01:00
|
|
|
parser.usage = "%(prog)s [OPTIONS] [file to import/add-on to install]"
|
2019-03-04 07:45:29 +01:00
|
|
|
parser.add_argument("-b", "--base", help="path to base folder", default="")
|
|
|
|
parser.add_argument("-p", "--profile", help="profile name to load", default="")
|
|
|
|
parser.add_argument("-l", "--lang", help="interface language (en, de, etc)")
|
2020-10-12 21:57:49 +02:00
|
|
|
parser.add_argument(
|
2021-01-11 05:11:18 +01:00
|
|
|
"-v", "--version", help="print the Anki version and exit", action="store_true"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--safemode", help="disable add-ons and automatic syncing", action="store_true"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--syncserver",
|
|
|
|
help="skip GUI and start a local sync server",
|
|
|
|
action="store_true",
|
2020-10-12 21:57:49 +02:00
|
|
|
)
|
2019-03-04 07:45:29 +01:00
|
|
|
return parser.parse_known_args(argv[1:])
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2021-02-02 14:30:53 +01:00
|
|
|
def setupGL(pm: aqt.profiles.ProfileManager) -> None:
|
2020-12-22 04:01:06 +01:00
|
|
|
driver = pm.video_driver()
|
2018-08-08 15:48:25 +02:00
|
|
|
|
|
|
|
# work around pyqt loading wrong GL library
|
2021-11-25 00:06:16 +01:00
|
|
|
if is_lin:
|
2018-08-08 15:48:25 +02:00
|
|
|
import ctypes
|
2019-12-23 01:34:10 +01:00
|
|
|
|
|
|
|
ctypes.CDLL("libGL.so.1", ctypes.RTLD_GLOBAL)
|
2018-08-08 15:48:25 +02:00
|
|
|
|
|
|
|
# catch opengl errors
|
2021-02-02 14:30:53 +01:00
|
|
|
def msgHandler(category: Any, ctx: Any, msg: Any) -> None:
|
2021-10-05 05:53:01 +02:00
|
|
|
if category == QtMsgType.QtDebugMsg:
|
2020-05-31 20:50:57 +02:00
|
|
|
category = "debug"
|
2021-10-05 05:53:01 +02:00
|
|
|
elif category == QtMsgType.QtInfoMsg:
|
2020-05-31 20:50:57 +02:00
|
|
|
category = "info"
|
2021-10-05 05:53:01 +02:00
|
|
|
elif category == QtMsgType.QtWarningMsg:
|
2020-05-31 20:50:57 +02:00
|
|
|
category = "warning"
|
2021-10-05 05:53:01 +02:00
|
|
|
elif category == QtMsgType.QtCriticalMsg:
|
2020-05-31 20:50:57 +02:00
|
|
|
category = "critical"
|
2021-10-05 05:53:01 +02:00
|
|
|
elif category == QtMsgType.QtDebugMsg:
|
2020-05-31 20:50:57 +02:00
|
|
|
category = "debug"
|
2021-10-05 05:53:01 +02:00
|
|
|
elif category == QtMsgType.QtFatalMsg:
|
2020-05-31 20:50:57 +02:00
|
|
|
category = "fatal"
|
|
|
|
else:
|
|
|
|
category = "unknown"
|
2020-05-31 19:40:05 +02:00
|
|
|
context = ""
|
|
|
|
if ctx.file:
|
|
|
|
context += f"{ctx.file}:"
|
|
|
|
if ctx.line:
|
|
|
|
context += f"{ctx.line},"
|
|
|
|
if ctx.function:
|
|
|
|
context += f"{ctx.function}"
|
|
|
|
if context:
|
|
|
|
context = f"'{context}'"
|
2022-04-06 03:34:57 +02:00
|
|
|
if (
|
|
|
|
"Failed to create OpenGL context" in msg
|
|
|
|
# Based on the message Qt6 shows to the user; have not tested whether
|
|
|
|
# we can actually capture this or not.
|
|
|
|
or "Failed to initialize graphics backend" in msg
|
|
|
|
):
|
2019-12-23 01:34:10 +01:00
|
|
|
QMessageBox.critical(
|
|
|
|
None,
|
2021-03-26 04:48:26 +01:00
|
|
|
tr.qt_misc_error(),
|
2021-03-26 05:38:15 +01:00
|
|
|
tr.qt_misc_error_loading_graphics_driver(
|
2020-12-22 04:01:06 +01:00
|
|
|
mode=driver.value,
|
|
|
|
context=context,
|
2020-11-21 03:16:26 +01:00
|
|
|
),
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2020-12-22 04:01:06 +01:00
|
|
|
pm.set_video_driver(driver.next())
|
2018-08-08 15:48:25 +02:00
|
|
|
return
|
|
|
|
else:
|
2020-05-31 20:50:57 +02:00
|
|
|
print(f"Qt {category}: {msg} {context}")
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2018-08-08 15:48:25 +02:00
|
|
|
qInstallMessageHandler(msgHandler)
|
|
|
|
|
2020-12-22 04:01:06 +01:00
|
|
|
if driver == VideoDriver.OpenGL:
|
2022-04-03 11:57:30 +02:00
|
|
|
# Leaving QT_OPENGL unset appears to sometimes produce different results
|
|
|
|
# to explicitly setting it to 'auto'; the former seems to be more compatible.
|
2020-12-22 04:01:06 +01:00
|
|
|
pass
|
2018-08-08 15:48:25 +02:00
|
|
|
else:
|
2021-11-25 00:06:16 +01:00
|
|
|
if is_win:
|
2022-04-03 11:57:30 +02:00
|
|
|
# on Windows, this appears to be sufficient on Qt5/Qt6.
|
|
|
|
# On Qt6, ANGLE is excluded by the enum.
|
2020-12-22 04:01:06 +01:00
|
|
|
os.environ["QT_OPENGL"] = driver.value
|
2021-11-25 00:06:16 +01:00
|
|
|
elif is_mac:
|
2021-10-05 05:53:01 +02:00
|
|
|
QCoreApplication.setAttribute(Qt.ApplicationAttribute.AA_UseSoftwareOpenGL)
|
2021-11-25 00:06:16 +01:00
|
|
|
elif is_lin:
|
2022-04-03 11:57:30 +02:00
|
|
|
# Qt5 only
|
2020-12-22 04:01:06 +01:00
|
|
|
os.environ["QT_XCB_FORCE_SOFTWARE_OPENGL"] = "1"
|
2022-04-03 11:57:30 +02:00
|
|
|
# Required on Qt6
|
|
|
|
if "QTWEBENGINE_CHROMIUM_FLAGS" not in os.environ:
|
|
|
|
os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = "--disable-gpu"
|
2018-08-08 15:48:25 +02:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-05-05 08:28:49 +02:00
|
|
|
PROFILE_CODE = os.environ.get("ANKI_PROFILE_CODE")
|
2020-05-04 00:47:03 +02:00
|
|
|
|
2020-05-05 08:28:49 +02:00
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def write_profile_results() -> None:
|
2020-08-02 02:16:54 +02:00
|
|
|
|
2020-05-05 08:28:49 +02:00
|
|
|
profiler.disable()
|
2021-03-22 00:43:48 +01:00
|
|
|
profile = os.path.join(os.environ.get("BUILD_WORKSPACE_DIRECTORY", ""), "anki.prof")
|
|
|
|
profiler.dump_stats(profile)
|
2020-05-16 04:53:01 +02:00
|
|
|
profiler.dump_stats("anki.prof")
|
|
|
|
print("profile stats written to anki.prof")
|
2020-12-17 03:03:24 +01:00
|
|
|
print("use 'bazel run qt:profile' to explore")
|
2020-05-04 00:47:03 +02:00
|
|
|
|
|
|
|
|
2021-02-01 14:28:21 +01:00
|
|
|
def run() -> None:
|
2021-10-28 10:46:45 +02:00
|
|
|
print("Preparing to run...")
|
2014-07-07 03:41:56 +02:00
|
|
|
try:
|
|
|
|
_run()
|
2016-05-12 06:45:35 +02:00
|
|
|
except Exception as e:
|
2018-09-24 08:24:11 +02:00
|
|
|
traceback.print_exc()
|
2019-12-23 01:34:10 +01:00
|
|
|
QMessageBox.critical(
|
|
|
|
None,
|
|
|
|
"Startup Error",
|
2021-02-11 01:09:06 +01:00
|
|
|
f"Please notify support of this error:\n\n{traceback.format_exc()}",
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
|
|
|
|
2014-07-07 03:41:56 +02:00
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def _run(argv: Optional[list[str]] = None, exec: bool = True) -> Optional[AnkiApp]:
|
2017-09-23 17:01:29 +02:00
|
|
|
"""Start AnkiQt application or reuse an existing instance if one exists.
|
|
|
|
|
|
|
|
If the function is invoked with exec=False, the AnkiQt will not enter
|
|
|
|
the main event loop - instead the application object will be returned.
|
|
|
|
|
|
|
|
The 'exec' and 'argv' arguments will be useful for testing purposes.
|
|
|
|
|
|
|
|
If no 'argv' is supplied then 'sys.argv' will be used.
|
|
|
|
"""
|
2012-12-21 08:51:59 +01:00
|
|
|
global mw
|
2020-05-05 04:29:48 +02:00
|
|
|
global profiler
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2017-09-21 05:02:39 +02:00
|
|
|
if argv is None:
|
|
|
|
argv = sys.argv
|
|
|
|
|
2013-02-21 20:51:06 +01:00
|
|
|
# parse args
|
2017-09-21 05:02:39 +02:00
|
|
|
opts, args = parseArgs(argv)
|
2013-02-21 20:51:06 +01:00
|
|
|
|
2020-06-19 18:11:04 +02:00
|
|
|
if opts.version:
|
2021-01-11 05:11:18 +01:00
|
|
|
print(f"Anki {appVersion}")
|
2021-02-01 14:28:21 +01:00
|
|
|
return None
|
2020-06-19 18:11:04 +02:00
|
|
|
|
2020-05-05 08:28:49 +02:00
|
|
|
if PROFILE_CODE:
|
2020-05-04 00:47:03 +02:00
|
|
|
|
2020-05-05 04:15:23 +02:00
|
|
|
profiler = cProfile.Profile()
|
|
|
|
profiler.enable()
|
2020-05-04 00:47:03 +02:00
|
|
|
|
2021-09-14 01:53:23 +02:00
|
|
|
if (
|
|
|
|
getattr(sys, "frozen", False)
|
2022-04-20 11:41:41 +02:00
|
|
|
and (os.getenv("QT_QPA_PLATFORM") == "wayland" or os.getenv("WAYLAND_DISPLAY"))
|
2021-09-14 01:53:23 +02:00
|
|
|
and not os.getenv("ANKI_WAYLAND")
|
|
|
|
):
|
|
|
|
# users need to opt in to wayland support, given the issues it has
|
2022-04-03 08:50:00 +02:00
|
|
|
print("Wayland support is disabled by default due to bugs:")
|
|
|
|
print("https://github.com/ankitects/anki/issues/1767")
|
2021-09-20 13:42:16 +02:00
|
|
|
print("You can force it on with an env var: ANKI_WAYLAND=1")
|
2021-09-14 01:53:23 +02:00
|
|
|
os.environ["QT_QPA_PLATFORM"] = "xcb"
|
|
|
|
|
2018-08-08 15:48:25 +02:00
|
|
|
# profile manager
|
2022-12-24 01:44:40 +01:00
|
|
|
i18n_setup = False
|
2019-12-24 11:33:39 +01:00
|
|
|
pm = None
|
|
|
|
try:
|
2022-12-24 01:44:40 +01:00
|
|
|
base_folder = ProfileManager.get_created_base_folder(opts.base)
|
|
|
|
Collection.initialize_backend_logging(str(base_folder / "anki.log"))
|
|
|
|
|
|
|
|
# default to specified/system language before getting user's preference so that we can localize some more strings
|
|
|
|
lang = anki.lang.get_def_lang(opts.lang)
|
|
|
|
anki.lang.set_lang(lang[1])
|
|
|
|
i18n_setup = True
|
|
|
|
|
|
|
|
pm = ProfileManager(base_folder)
|
2019-12-24 11:33:39 +01:00
|
|
|
pmLoadResult = pm.setupMeta()
|
|
|
|
except:
|
|
|
|
# will handle below
|
2020-01-14 05:56:28 +01:00
|
|
|
traceback.print_exc()
|
|
|
|
pm = None
|
2018-07-28 08:38:22 +02:00
|
|
|
|
2019-12-24 11:33:39 +01:00
|
|
|
if pm:
|
|
|
|
# gl workarounds
|
|
|
|
setupGL(pm)
|
|
|
|
# apply user-provided scale factor
|
|
|
|
os.environ["QT_SCALE_FACTOR"] = str(pm.uiScale())
|
2016-08-01 04:16:06 +02:00
|
|
|
|
2017-11-27 02:01:15 +01:00
|
|
|
# opt in to full hidpi support?
|
2021-10-05 05:53:01 +02:00
|
|
|
if not os.environ.get("ANKI_NOHIGHDPI") and qtmajor == 5:
|
|
|
|
QCoreApplication.setAttribute(Qt.ApplicationAttribute.AA_EnableHighDpiScaling) # type: ignore
|
|
|
|
QCoreApplication.setAttribute(Qt.ApplicationAttribute.AA_UseHighDpiPixmaps) # type: ignore
|
2019-12-17 09:43:32 +01:00
|
|
|
os.environ["QT_ENABLE_HIGHDPI_SCALING"] = "1"
|
|
|
|
os.environ["QT_SCALE_FACTOR_ROUNDING_POLICY"] = "PassThrough"
|
|
|
|
|
2019-08-16 22:35:39 +02:00
|
|
|
# Opt into software rendering. Useful for buggy systems.
|
|
|
|
if os.environ.get("ANKI_SOFTWAREOPENGL"):
|
2021-10-05 05:53:01 +02:00
|
|
|
QCoreApplication.setAttribute(Qt.ApplicationAttribute.AA_UseSoftwareOpenGL)
|
2019-08-16 22:35:39 +02:00
|
|
|
|
2021-12-10 08:52:08 +01:00
|
|
|
# fix an issue on Windows, where Ctrl+Alt shortcuts are triggered by AltGr,
|
|
|
|
# preventing users from typing things like "@" through AltGr+Q on a German
|
|
|
|
# keyboard.
|
|
|
|
if is_win and "QT_QPA_PLATFORM" not in os.environ:
|
2020-07-24 03:57:37 +02:00
|
|
|
os.environ["QT_QPA_PLATFORM"] = "windows:altgr"
|
2020-07-24 02:32:50 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# create the app
|
|
|
|
QCoreApplication.setApplicationName("Anki")
|
2019-07-18 23:14:34 +02:00
|
|
|
QGuiApplication.setDesktopFileName("anki.desktop")
|
2019-07-20 13:10:59 +02:00
|
|
|
app = AnkiApp(argv)
|
2012-12-21 08:51:59 +01:00
|
|
|
if app.secondInstance():
|
|
|
|
# we've signaled the primary instance, so we should close
|
2021-02-01 14:28:21 +01:00
|
|
|
return None
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-24 11:33:39 +01:00
|
|
|
if not pm:
|
2022-12-24 01:44:40 +01:00
|
|
|
if i18n_setup:
|
|
|
|
QMessageBox.critical(
|
|
|
|
None,
|
|
|
|
tr.qt_misc_error(),
|
|
|
|
tr.profiles_could_not_create_data_folder(),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
QMessageBox.critical(None, "Startup Failed", "Unable to create data folder")
|
2021-02-01 14:28:21 +01:00
|
|
|
return None
|
2019-12-24 11:33:39 +01:00
|
|
|
|
2013-04-15 06:46:07 +02:00
|
|
|
# disable icons on mac; this must be done before window created
|
2021-11-25 00:06:16 +01:00
|
|
|
if is_mac:
|
2021-10-05 05:53:01 +02:00
|
|
|
app.setAttribute(Qt.ApplicationAttribute.AA_DontShowIconsInMenus)
|
2013-04-15 06:46:07 +02:00
|
|
|
|
2019-12-17 08:59:19 +01:00
|
|
|
# disable help button in title bar on qt versions that support it
|
2021-11-25 00:06:16 +01:00
|
|
|
if is_win and qtmajor == 5 and qtminor >= 10:
|
2021-10-24 14:09:43 +02:00
|
|
|
QApplication.setAttribute(Qt.AA_DisableWindowContextHelpButton) # type: ignore
|
2019-12-17 08:59:19 +01:00
|
|
|
|
2018-10-11 07:49:04 +02:00
|
|
|
# proxy configured?
|
2020-08-31 04:05:36 +02:00
|
|
|
from urllib.request import getproxies, proxy_bypass
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-05-31 23:18:13 +02:00
|
|
|
disable_proxies = False
|
|
|
|
try:
|
|
|
|
if "http" in getproxies():
|
|
|
|
# if it's not set up to bypass localhost, we'll
|
|
|
|
# need to disable proxies in the webviews
|
|
|
|
if not proxy_bypass("127.0.0.1"):
|
|
|
|
disable_proxies = True
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
# proxy_bypass can't handle unicode in hostnames; assume we need
|
|
|
|
# to disable proxies
|
|
|
|
disable_proxies = True
|
|
|
|
|
|
|
|
if disable_proxies:
|
|
|
|
print("webview proxy use disabled")
|
|
|
|
proxy = QNetworkProxy()
|
2021-10-05 05:53:01 +02:00
|
|
|
proxy.setType(QNetworkProxy.ProxyType.NoProxy)
|
2020-05-31 23:18:13 +02:00
|
|
|
QNetworkProxy.setApplicationProxy(proxy)
|
2018-10-11 07:49:04 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# we must have a usable temp dir
|
|
|
|
try:
|
|
|
|
tempfile.gettempdir()
|
|
|
|
except:
|
|
|
|
QMessageBox.critical(
|
2019-12-23 01:34:10 +01:00
|
|
|
None,
|
2021-03-26 04:48:26 +01:00
|
|
|
tr.qt_misc_error(),
|
|
|
|
tr.qt_misc_no_temp_folder(),
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2021-02-01 14:28:21 +01:00
|
|
|
return None
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-10-05 06:44:07 +02:00
|
|
|
# make image resources available
|
|
|
|
from aqt.utils import aqt_data_folder
|
|
|
|
|
|
|
|
QDir.addSearchPath("icons", os.path.join(aqt_data_folder(), "qt", "icons"))
|
|
|
|
|
2019-12-24 11:23:36 +01:00
|
|
|
if pmLoadResult.firstTime:
|
2020-11-21 03:16:26 +01:00
|
|
|
pm.setDefaultLang(lang[0])
|
2019-12-24 11:23:36 +01:00
|
|
|
|
2019-12-19 00:38:36 +01:00
|
|
|
if pmLoadResult.loadError:
|
|
|
|
QMessageBox.warning(
|
2019-12-23 01:34:10 +01:00
|
|
|
None,
|
2021-03-26 04:48:26 +01:00
|
|
|
tr.profiles_prefs_corrupt_title(),
|
|
|
|
tr.profiles_prefs_file_is_corrupt(),
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2018-08-08 15:48:25 +02:00
|
|
|
|
|
|
|
if opts.profile:
|
|
|
|
pm.openProfile(opts.profile)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-03-14 00:45:00 +01:00
|
|
|
# i18n & backend
|
2020-11-21 03:16:26 +01:00
|
|
|
backend = setupLangAndBackend(pm, app, opts.lang, pmLoadResult.firstTime)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-12-22 04:01:06 +01:00
|
|
|
driver = pm.video_driver()
|
2021-11-25 00:06:16 +01:00
|
|
|
if is_lin and driver == VideoDriver.OpenGL:
|
2018-12-18 10:29:34 +01:00
|
|
|
from aqt.utils import gfxDriverIsBroken
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2018-12-18 10:29:34 +01:00
|
|
|
if gfxDriverIsBroken():
|
2020-12-22 04:01:06 +01:00
|
|
|
pm.set_video_driver(driver.next())
|
2019-12-23 01:34:10 +01:00
|
|
|
QMessageBox.critical(
|
|
|
|
None,
|
2021-03-26 04:48:26 +01:00
|
|
|
tr.qt_misc_error(),
|
|
|
|
tr.qt_misc_incompatible_video_driver(),
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2018-12-18 10:29:34 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# load the main window
|
|
|
|
import aqt.main
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-03-14 00:45:00 +01:00
|
|
|
mw = aqt.main.AnkiQt(app, pm, backend, opts, args)
|
2017-09-21 05:02:39 +02:00
|
|
|
if exec:
|
2021-10-28 10:46:45 +02:00
|
|
|
print("Starting main loop...")
|
2017-09-21 05:02:39 +02:00
|
|
|
app.exec()
|
|
|
|
else:
|
|
|
|
return app
|
2020-05-04 00:47:03 +02:00
|
|
|
|
2020-05-05 08:28:49 +02:00
|
|
|
if PROFILE_CODE:
|
2020-05-16 04:53:01 +02:00
|
|
|
write_profile_results()
|
2021-02-01 14:28:21 +01:00
|
|
|
|
|
|
|
return None
|