From 5a54d5e07ded98651384d2f5f0087f35f351d069 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Tue, 2 Feb 2021 15:47:51 +1000 Subject: [PATCH] fix: slowdowns after import; hard to read popup QTextEdit() will pin the CPU at 100% for seconds to minutes when fed a large string to display - work around it by switching to QPlainTextEdit(). Also strip HTML before showing the user - easier to read, and less text to display. And turn off word wrap, as it makes it easier to skim, and further reduces the work the widget needs to do. https://forums.ankiweb.net/t/big-issue-where-anki-gets-slow-when-you-import-this-deck/7050 --- pylib/anki/importing/anki2.py | 6 ++++-- qt/aqt/importing.py | 2 +- qt/aqt/utils.py | 34 +++++++++++++++++++++------------- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/pylib/anki/importing/anki2.py b/pylib/anki/importing/anki2.py index 6eb6a9c59..b6d8f2de1 100644 --- a/pylib/anki/importing/anki2.py +++ b/pylib/anki/importing/anki2.py @@ -10,7 +10,7 @@ from anki.consts import * from anki.decks import DeckManager from anki.importing.base import Importer from anki.rsbackend import TR -from anki.utils import intTime, joinFields, splitFields +from anki.utils import intTime, joinFields, splitFields, stripHTMLMedia GUID = 1 MID = 2 @@ -71,7 +71,9 @@ class Anki2Importer(Importer): ###################################################################### def _logNoteRow(self, action: str, noteRow: List[str]) -> None: - self.log.append("[%s] %s" % (action, noteRow[6].replace("\x1f", ", "))) + self.log.append( + "[%s] %s" % (action, stripHTMLMedia(noteRow[6].replace("\x1f", ", "))) + ) def _importNotes(self) -> None: # build guid -> (id,mod,mid) hash & map of existing note ids diff --git a/qt/aqt/importing.py b/qt/aqt/importing.py index 4116673e5..8737366e2 100644 --- a/qt/aqt/importing.py +++ b/qt/aqt/importing.py @@ -391,7 +391,7 @@ def importFile(mw, file): if "\n" not in log: tooltip(log) else: - showText(log) + showText(log, plain_text_edit=True) mw.reset() diff --git a/qt/aqt/utils.py b/qt/aqt/utils.py index aa8f52a3a..7c38a3641 100644 --- a/qt/aqt/utils.py +++ b/qt/aqt/utils.py @@ -7,7 +7,7 @@ import os import re import subprocess import sys -from typing import TYPE_CHECKING, Any, List, Optional, Union +from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union import anki import aqt @@ -113,24 +113,31 @@ def showInfo( def showText( - txt, - parent=None, - type="text", - run=True, - geomKey=None, - minWidth=500, - minHeight=400, - title="Anki", - copyBtn=False, -): + txt: str, + parent: Optional[QWidget] = None, + type: str = "text", + run: bool = True, + geomKey: Optional[str] = None, + minWidth: int = 500, + minHeight: int = 400, + title: str = "Anki", + copyBtn: bool = False, + plain_text_edit: bool = False, +) -> Optional[Tuple[QDialog, QDialogButtonBox]]: if not parent: parent = aqt.mw.app.activeWindow() or aqt.mw diag = QDialog(parent) diag.setWindowTitle(title) layout = QVBoxLayout(diag) diag.setLayout(layout) - text = QTextBrowser() - text.setOpenExternalLinks(True) + if plain_text_edit: + # used by the importer + text = QPlainTextEdit() + text.setReadOnly(True) + text.setWordWrapMode(QTextOption.NoWrap) + else: + text = QTextBrowser() + text.setOpenExternalLinks(True) if type == "text": text.setPlainText(txt) else: @@ -165,6 +172,7 @@ def showText( restoreGeom(diag, geomKey) if run: diag.exec_() + return None else: return diag, box