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-16 13:40:37 +01:00
|
|
|
|
2020-02-11 23:28:21 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-12-11 07:20:00 +01:00
|
|
|
import html
|
2019-12-20 10:19:03 +01:00
|
|
|
import time
|
2021-03-20 12:03:26 +01:00
|
|
|
from dataclasses import dataclass
|
2019-12-20 10:19:03 +01:00
|
|
|
from operator import itemgetter
|
2021-03-20 12:03:26 +01:00
|
|
|
from typing import (
|
|
|
|
Any,
|
|
|
|
Callable,
|
|
|
|
Dict,
|
|
|
|
Generator,
|
|
|
|
List,
|
|
|
|
Optional,
|
|
|
|
Sequence,
|
|
|
|
Tuple,
|
|
|
|
Union,
|
|
|
|
cast,
|
|
|
|
)
|
2019-12-19 12:11:12 +01:00
|
|
|
|
2020-03-29 21:10:30 +02:00
|
|
|
import aqt
|
2019-12-20 10:19:03 +01:00
|
|
|
import aqt.forms
|
2021-03-23 10:36:52 +01:00
|
|
|
from anki.cards import Card, CardID
|
2021-03-20 12:03:26 +01:00
|
|
|
from anki.collection import BrowserRow, Collection, Config, OpChanges, SearchNode
|
2019-12-20 10:19:03 +01:00
|
|
|
from anki.consts import *
|
rework filtered deck screen & search errors
- Filtered deck creation now happens as an atomic operation, and is
undoable.
- The logic for initial search text, normalizing searches and so on
has been pushed into the backend.
- Use protobuf to pass the filtered deck to the updated dialog, so
we don't need to deal with untyped JSON.
- Change the "revise your search?" prompt to be a simple info box -
user has access to cancel and build buttons, and doesn't need a separate
prompt. Tweak the wording so the 'show excluded' button should be more
obvious.
- Filtered decks have a time appended to them instead of a number,
primarily because it's easier to implement. No objections going back to
the old behaviour if someone wants to contribute a clean patch.
The standard de-duplication will happen if two decks are created in the
same minute with the same name.
- Tweak the default sort order, and start with two searches. The UI
will still hide the second search by default, but by starting with two,
the frontend doesn't need logic for creating the starting text.
- Search errors now have their own error type, instead of using
InvalidInput, as that was intended mainly for bad API calls. The markdown
conversion is done when the error is converted from the backend, allowing
errors to printed as a string without any special handling by the calling
code.
TODO: when building a new filtered deck, update_active() is clobbering
the undo log when the overview is refreshed
2021-03-24 12:52:48 +01:00
|
|
|
from anki.errors import NotFoundError
|
2020-11-18 04:48:23 +01:00
|
|
|
from anki.lang import without_unicode_isolation
|
2020-01-17 01:17:33 +01:00
|
|
|
from anki.models import NoteType
|
2021-03-25 13:50:31 +01:00
|
|
|
from anki.notes import NoteID
|
2020-06-15 06:14:18 +02:00
|
|
|
from anki.stats import CardStats
|
2021-03-18 03:06:45 +01:00
|
|
|
from anki.tags import MARKED_TAG
|
2021-03-20 12:03:26 +01:00
|
|
|
from anki.utils import ids2str, isMac, isWin
|
2021-02-05 09:50:01 +01:00
|
|
|
from aqt import AnkiQt, colors, gui_hooks
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
from aqt.card_ops import set_card_deck, set_card_flag
|
2020-01-15 22:41:23 +01:00
|
|
|
from aqt.editor import Editor
|
2020-02-10 04:15:19 +01:00
|
|
|
from aqt.exporting import ExportDialog
|
2021-03-19 07:55:10 +01:00
|
|
|
from aqt.find_and_replace import FindAndReplaceDialog
|
2020-08-16 18:49:51 +02:00
|
|
|
from aqt.main import ResetReason
|
2021-03-19 10:15:17 +01:00
|
|
|
from aqt.note_ops import remove_notes
|
2020-07-26 21:16:06 +02:00
|
|
|
from aqt.previewer import BrowserPreviewer as PreviewDialog
|
|
|
|
from aqt.previewer import Previewer
|
2013-05-03 10:52:46 +02:00
|
|
|
from aqt.qt import *
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
from aqt.scheduling_ops import (
|
|
|
|
forget_cards,
|
2021-03-18 02:46:11 +01:00
|
|
|
reposition_new_cards_dialog,
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
set_due_date_dialog,
|
|
|
|
suspend_cards,
|
|
|
|
unsuspend_cards,
|
|
|
|
)
|
2021-03-17 05:51:59 +01:00
|
|
|
from aqt.sidebar import SidebarTreeView
|
2021-03-19 10:15:17 +01:00
|
|
|
from aqt.tag_ops import add_tags, clear_unused_tags, remove_tags_for_notes
|
2020-01-23 06:08:10 +01:00
|
|
|
from aqt.theme import theme_manager
|
2019-12-23 01:34:10 +01:00
|
|
|
from aqt.utils import (
|
2020-11-17 08:42:43 +01:00
|
|
|
TR,
|
2021-01-25 14:45:47 +01:00
|
|
|
HelpPage,
|
2021-03-17 05:51:59 +01:00
|
|
|
KeyboardModifiersPressed,
|
2019-12-23 01:34:10 +01:00
|
|
|
askUser,
|
2021-03-14 13:08:37 +01:00
|
|
|
current_top_level_widget,
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button,
|
2021-03-16 13:40:37 +01:00
|
|
|
ensure_editor_saved,
|
|
|
|
ensure_editor_saved_on_trigger,
|
2019-12-23 01:34:10 +01:00
|
|
|
getTag,
|
|
|
|
openHelp,
|
|
|
|
qtMenuShortcutWorkaround,
|
2020-06-05 03:39:53 +02:00
|
|
|
restore_combo_history,
|
2020-06-08 20:54:20 +02:00
|
|
|
restore_combo_index_for_session,
|
2019-12-23 01:34:10 +01:00
|
|
|
restoreGeom,
|
|
|
|
restoreHeader,
|
|
|
|
restoreSplitter,
|
|
|
|
restoreState,
|
2020-06-05 03:39:53 +02:00
|
|
|
save_combo_history,
|
2020-06-08 20:54:20 +02:00
|
|
|
save_combo_index_for_session,
|
2019-12-23 01:34:10 +01:00
|
|
|
saveGeom,
|
|
|
|
saveHeader,
|
|
|
|
saveSplitter,
|
|
|
|
saveState,
|
|
|
|
shortcut,
|
|
|
|
showInfo,
|
rework filtered deck screen & search errors
- Filtered deck creation now happens as an atomic operation, and is
undoable.
- The logic for initial search text, normalizing searches and so on
has been pushed into the backend.
- Use protobuf to pass the filtered deck to the updated dialog, so
we don't need to deal with untyped JSON.
- Change the "revise your search?" prompt to be a simple info box -
user has access to cancel and build buttons, and doesn't need a separate
prompt. Tweak the wording so the 'show excluded' button should be more
obvious.
- Filtered decks have a time appended to them instead of a number,
primarily because it's easier to implement. No objections going back to
the old behaviour if someone wants to contribute a clean patch.
The standard de-duplication will happen if two decks are created in the
same minute with the same name.
- Tweak the default sort order, and start with two searches. The UI
will still hide the second search by default, but by starting with two,
the frontend doesn't need logic for creating the starting text.
- Search errors now have their own error type, instead of using
InvalidInput, as that was intended mainly for bad API calls. The markdown
conversion is done when the error is converted from the backend, allowing
errors to printed as a string without any special handling by the calling
code.
TODO: when building a new filtered deck, update_active() is clobbering
the undo log when the overview is refreshed
2021-03-24 12:52:48 +01:00
|
|
|
showWarning,
|
2019-12-23 01:34:10 +01:00
|
|
|
tooltip,
|
2020-02-17 05:41:01 +01:00
|
|
|
tr,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
from aqt.webview import AnkiWebView
|
|
|
|
|
2020-02-08 23:59:29 +01:00
|
|
|
|
2020-02-11 23:28:21 +01:00
|
|
|
@dataclass
|
|
|
|
class FindDupesDialog:
|
|
|
|
dialog: QDialog
|
|
|
|
browser: Browser
|
2020-02-08 23:59:29 +01:00
|
|
|
|
|
|
|
|
2020-03-21 07:38:46 +01:00
|
|
|
@dataclass
|
|
|
|
class SearchContext:
|
|
|
|
search: str
|
2020-08-11 22:30:03 +02:00
|
|
|
browser: Browser
|
2020-03-21 08:38:09 +01:00
|
|
|
order: Union[bool, str] = True
|
2020-03-21 07:38:46 +01:00
|
|
|
# if set, provided card ids will be used instead of the regular search
|
2021-03-23 10:36:52 +01:00
|
|
|
card_ids: Optional[Sequence[CardID]] = None
|
2020-03-21 07:38:46 +01:00
|
|
|
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Data model
|
|
|
|
##########################################################################
|
|
|
|
|
2021-03-20 12:03:26 +01:00
|
|
|
|
2021-03-18 15:06:54 +01:00
|
|
|
@dataclass
|
|
|
|
class Cell:
|
2021-03-20 12:03:26 +01:00
|
|
|
text: str
|
|
|
|
is_rtl: bool
|
2021-03-18 15:06:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
class CellRow:
|
2021-03-20 12:03:26 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
cells: Generator[Tuple[str, bool], None, None],
|
|
|
|
color: BrowserRow.Color.V,
|
|
|
|
font_name: str,
|
|
|
|
font_size: int,
|
|
|
|
) -> None:
|
|
|
|
self.refreshed_at: float = time.time()
|
|
|
|
self.cells: Tuple[Cell, ...] = tuple(Cell(*cell) for cell in cells)
|
|
|
|
self.color: Optional[Tuple[str, str]] = backend_color_to_aqt_color(color)
|
|
|
|
self.font_name: str = font_name or "arial"
|
|
|
|
self.font_size: int = font_size if font_size > 0 else 12
|
2021-03-18 15:06:54 +01:00
|
|
|
|
|
|
|
def is_stale(self, threshold: float) -> bool:
|
|
|
|
return self.refreshed_at < threshold
|
|
|
|
|
2021-03-20 12:03:26 +01:00
|
|
|
@staticmethod
|
|
|
|
def generic(length: int, cell_text: str) -> CellRow:
|
|
|
|
return CellRow(
|
|
|
|
((cell_text, False) for cell in range(length)),
|
|
|
|
BrowserRow.COLOR_DEFAULT,
|
|
|
|
"arial",
|
|
|
|
12,
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def placeholder(length: int) -> CellRow:
|
|
|
|
return CellRow.generic(length, "...")
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def deleted(length: int) -> CellRow:
|
|
|
|
return CellRow.generic(length, tr(TR.BROWSING_ROW_DELETED))
|
|
|
|
|
|
|
|
|
|
|
|
def backend_color_to_aqt_color(color: BrowserRow.Color.V) -> Optional[Tuple[str, str]]:
|
|
|
|
if color == BrowserRow.COLOR_MARKED:
|
|
|
|
return colors.MARKED_BG
|
|
|
|
if color == BrowserRow.COLOR_SUSPENDED:
|
|
|
|
return colors.SUSPENDED_BG
|
|
|
|
if color == BrowserRow.COLOR_FLAG_RED:
|
|
|
|
return colors.FLAG1_BG
|
|
|
|
if color == BrowserRow.COLOR_FLAG_ORANGE:
|
|
|
|
return colors.FLAG2_BG
|
|
|
|
if color == BrowserRow.COLOR_FLAG_GREEN:
|
|
|
|
return colors.FLAG3_BG
|
|
|
|
if color == BrowserRow.COLOR_FLAG_BLUE:
|
|
|
|
return colors.FLAG4_BG
|
|
|
|
return None
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
class DataModel(QAbstractTableModel):
|
2021-02-01 00:39:55 +01:00
|
|
|
def __init__(self, browser: Browser) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
QAbstractTableModel.__init__(self)
|
|
|
|
self.browser = browser
|
|
|
|
self.col = browser.col
|
|
|
|
self.sortKey = None
|
2021-03-23 10:13:52 +01:00
|
|
|
self.activeCols: List[str] = self.col.get_config(
|
2019-12-23 01:34:10 +01:00
|
|
|
"activeCols", ["noteFld", "template", "cardDue", "deck"]
|
|
|
|
)
|
2021-03-23 10:36:52 +01:00
|
|
|
self.cards: Sequence[CardID] = []
|
2021-03-20 12:03:26 +01:00
|
|
|
self._rows: Dict[int, CellRow] = {}
|
2021-03-18 15:06:54 +01:00
|
|
|
self._last_refresh = 0.0
|
|
|
|
# serve stale content to avoid hitting the DB?
|
clear_unused_tags and browser redraw improvements
- clear_unused_tags() is now undoable, and returns the number of removed
notes
- add a new mw.query_op() helper for immutable queries
- decouple "freeze/unfreeze ui state" hooks from the "interface update
required" hook, so that the former is fired even on error, and can be
made re-entrant
- use a 'block_updates' flag in Python, instead of setUpdatesEnabled(),
as the latter has the side-effect of preventing child windows like
tooltips from appearing, and forces a full redrawn when updates are
enabled again. The new behaviour leads to the card list blanking out
when a long-running op is running, but in the future if we cache the
cell values we can just display them from the cache instead.
- we were indiscriminately saving the note with saveNow(), due to the
call to saveTags(). Changed so that it only saves when the tags field
is focused.
- drain the "on_done" queue on main before launching a new background
task, to lower the chances of something in on_done making a small query
to the DB and hanging until a long op finishes
- the duplicate check in the editor was executed after the webview loads,
leading to it hanging until the sidebar finishes loading. Run it at
set_note() time instead, so that the editor loads first.
- don't throw an error when a long-running op started with with_progress()
finishes after the window it was launched from has closed
- don't throw an error when the browser is closed before the sidebar
has finished loading
2021-03-17 12:27:42 +01:00
|
|
|
self.block_updates = False
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-23 10:36:52 +01:00
|
|
|
def get_id(self, index: QModelIndex) -> CardID:
|
2021-03-20 12:03:26 +01:00
|
|
|
return self.cards[index.row()]
|
2021-03-18 15:06:54 +01:00
|
|
|
|
|
|
|
def get_cell(self, index: QModelIndex) -> Cell:
|
2021-03-20 12:03:26 +01:00
|
|
|
return self.get_row(index).cells[index.column()]
|
2021-03-18 15:06:54 +01:00
|
|
|
|
2021-03-20 12:03:26 +01:00
|
|
|
def get_row(self, index: QModelIndex) -> CellRow:
|
|
|
|
cid = self.get_id(index)
|
|
|
|
if row := self._rows.get(cid):
|
|
|
|
if not self.block_updates and row.is_stale(self._last_refresh):
|
2021-03-18 15:06:54 +01:00
|
|
|
# need to refresh
|
2021-03-20 12:03:26 +01:00
|
|
|
self._rows[cid] = self._fetch_row_from_backend(cid)
|
|
|
|
return self._rows[cid]
|
|
|
|
# return row, even if it's stale
|
|
|
|
return row
|
|
|
|
if self.block_updates:
|
|
|
|
# blank row until we unblock
|
|
|
|
return CellRow.placeholder(len(self.activeCols))
|
|
|
|
# missing row, need to build
|
|
|
|
self._rows[cid] = self._fetch_row_from_backend(cid)
|
|
|
|
return self._rows[cid]
|
|
|
|
|
2021-03-23 10:36:52 +01:00
|
|
|
def _fetch_row_from_backend(self, cid: CardID) -> CellRow:
|
2021-03-20 12:03:26 +01:00
|
|
|
try:
|
2021-03-23 10:13:52 +01:00
|
|
|
row = CellRow(*self.col.browser_row_for_card(cid))
|
2021-03-23 10:04:01 +01:00
|
|
|
except NotFoundError:
|
2021-03-20 12:03:26 +01:00
|
|
|
return CellRow.deleted(len(self.activeCols))
|
2021-03-23 10:04:01 +01:00
|
|
|
except Exception as e:
|
|
|
|
return CellRow.generic(len(self.activeCols), str(e))
|
2021-03-18 15:06:54 +01:00
|
|
|
|
2021-03-23 10:13:52 +01:00
|
|
|
gui_hooks.browser_did_fetch_row(cid, row, self.activeCols)
|
|
|
|
return row
|
|
|
|
|
2021-03-21 18:44:31 +01:00
|
|
|
def getCard(self, index: QModelIndex) -> Optional[Card]:
|
|
|
|
"""Try to return the indicated, possibly deleted card."""
|
|
|
|
try:
|
|
|
|
return self.col.getCard(self.get_id(index))
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Model interface
|
|
|
|
######################################################################
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def rowCount(self, parent: QModelIndex = QModelIndex()) -> int:
|
2017-02-23 07:21:00 +01:00
|
|
|
if parent and parent.isValid():
|
2017-02-19 05:30:35 +01:00
|
|
|
return 0
|
2012-12-21 08:51:59 +01:00
|
|
|
return len(self.cards)
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def columnCount(self, parent: QModelIndex = QModelIndex()) -> int:
|
2017-02-23 07:21:00 +01:00
|
|
|
if parent and parent.isValid():
|
2017-02-19 05:30:35 +01:00
|
|
|
return 0
|
2012-12-21 08:51:59 +01:00
|
|
|
return len(self.activeCols)
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def data(self, index: QModelIndex = QModelIndex(), role: int = 0) -> Any:
|
2012-12-21 08:51:59 +01:00
|
|
|
if not index.isValid():
|
2021-03-22 09:31:07 +01:00
|
|
|
return QVariant()
|
2012-12-21 08:51:59 +01:00
|
|
|
if role == Qt.FontRole:
|
2021-03-20 12:03:26 +01:00
|
|
|
if self.activeCols[index.column()] not in ("question", "answer", "noteFld"):
|
2021-03-22 09:31:07 +01:00
|
|
|
return QVariant()
|
2021-03-20 12:03:26 +01:00
|
|
|
qfont = QFont()
|
|
|
|
row = self.get_row(index)
|
|
|
|
qfont.setFamily(row.font_name)
|
|
|
|
qfont.setPixelSize(row.font_size)
|
|
|
|
return qfont
|
2021-03-22 09:31:07 +01:00
|
|
|
if role == Qt.TextAlignmentRole:
|
2020-07-26 01:25:39 +02:00
|
|
|
align: Union[Qt.AlignmentFlag, int] = Qt.AlignVCenter
|
2019-12-23 01:34:10 +01:00
|
|
|
if self.activeCols[index.column()] not in (
|
|
|
|
"question",
|
|
|
|
"answer",
|
|
|
|
"template",
|
|
|
|
"deck",
|
|
|
|
"noteFld",
|
|
|
|
"note",
|
2020-04-01 05:29:42 +02:00
|
|
|
"noteTags",
|
2019-12-23 01:34:10 +01:00
|
|
|
):
|
2012-12-21 08:51:59 +01:00
|
|
|
align |= Qt.AlignHCenter
|
|
|
|
return align
|
2021-03-22 09:31:07 +01:00
|
|
|
if role in (Qt.DisplayRole, Qt.ToolTipRole):
|
2021-03-18 15:06:54 +01:00
|
|
|
return self.get_cell(index).text
|
2021-03-22 09:31:07 +01:00
|
|
|
return QVariant()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def headerData(
|
|
|
|
self, section: int, orientation: Qt.Orientation, role: int = 0
|
|
|
|
) -> Optional[str]:
|
2012-12-21 08:51:59 +01:00
|
|
|
if orientation == Qt.Vertical:
|
2021-02-01 00:39:55 +01:00
|
|
|
return None
|
2012-12-21 08:51:59 +01:00
|
|
|
elif role == Qt.DisplayRole and section < len(self.activeCols):
|
2021-03-20 12:03:26 +01:00
|
|
|
type = self.activeCols[section]
|
2014-01-13 11:07:34 +01:00
|
|
|
txt = None
|
2012-12-21 08:51:59 +01:00
|
|
|
for stype, name in self.browser.columns:
|
|
|
|
if type == stype:
|
|
|
|
txt = name
|
|
|
|
break
|
2019-12-12 00:53:42 +01:00
|
|
|
# give the user a hint an invalid column was added by an add-on
|
2014-01-13 11:07:34 +01:00
|
|
|
if not txt:
|
2020-11-17 08:42:43 +01:00
|
|
|
txt = tr(TR.BROWSING_ADDON)
|
2012-12-21 08:51:59 +01:00
|
|
|
return txt
|
|
|
|
else:
|
2021-02-01 00:39:55 +01:00
|
|
|
return None
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def flags(self, index: QModelIndex) -> Qt.ItemFlags:
|
|
|
|
return cast(Qt.ItemFlags, Qt.ItemIsEnabled | Qt.ItemIsSelectable)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Filtering
|
|
|
|
######################################################################
|
|
|
|
|
2020-03-21 07:38:46 +01:00
|
|
|
def search(self, txt: str) -> None:
|
2016-07-14 12:23:44 +02:00
|
|
|
self.beginReset()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.cards = []
|
2017-02-02 12:00:58 +01:00
|
|
|
try:
|
2020-08-11 22:30:03 +02:00
|
|
|
ctx = SearchContext(search=txt, browser=self.browser)
|
2020-03-21 07:38:46 +01:00
|
|
|
gui_hooks.browser_will_search(ctx)
|
|
|
|
if ctx.card_ids is None:
|
2020-05-25 08:54:57 +02:00
|
|
|
ctx.card_ids = self.col.find_cards(ctx.search, order=ctx.order)
|
2020-03-21 07:38:46 +01:00
|
|
|
gui_hooks.browser_did_search(ctx)
|
|
|
|
self.cards = ctx.card_ids
|
2021-01-29 23:05:51 +01:00
|
|
|
except Exception as err:
|
|
|
|
raise err
|
2020-03-20 09:33:35 +01:00
|
|
|
finally:
|
|
|
|
self.endReset()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-16 07:39:41 +01:00
|
|
|
def redraw_cells(self) -> None:
|
|
|
|
"Update cell contents, without changing search count/columns/sorting."
|
|
|
|
if not self.cards:
|
|
|
|
return
|
|
|
|
top_left = self.index(0, 0)
|
2021-03-16 09:30:54 +01:00
|
|
|
bottom_right = self.index(len(self.cards) - 1, len(self.activeCols) - 1)
|
2021-03-18 15:06:54 +01:00
|
|
|
self._last_refresh = time.time()
|
2021-03-16 09:30:54 +01:00
|
|
|
self.dataChanged.emit(top_left, bottom_right) # type: ignore
|
2021-03-16 07:39:41 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def reset(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.beginReset()
|
|
|
|
self.endReset()
|
|
|
|
|
2016-07-14 12:23:44 +02:00
|
|
|
# caller must have called editor.saveNow() before calling this or .reset()
|
2021-02-01 00:39:55 +01:00
|
|
|
def beginReset(self) -> None:
|
2021-03-16 07:39:41 +01:00
|
|
|
self.browser.editor.set_note(None, hide=False)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.browser.mw.progress.start()
|
|
|
|
self.saveSelection()
|
|
|
|
self.beginResetModel()
|
2021-03-20 12:03:26 +01:00
|
|
|
self._rows = {}
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def endReset(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.endResetModel()
|
|
|
|
self.restoreSelection()
|
|
|
|
self.browser.mw.progress.finish()
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def reverse(self) -> None:
|
2021-03-16 13:40:37 +01:00
|
|
|
self.browser.editor.call_after_note_saved(self._reverse)
|
2016-07-14 12:23:44 +02:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def _reverse(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.beginReset()
|
2020-03-21 07:57:33 +01:00
|
|
|
self.cards = list(reversed(self.cards))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.endReset()
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def saveSelection(self) -> None:
|
2021-03-18 02:46:11 +01:00
|
|
|
cards = self.browser.selected_cards()
|
2021-02-11 00:37:38 +01:00
|
|
|
self.selectedCards = {id: True for id in cards}
|
2019-12-23 01:34:10 +01:00
|
|
|
if getattr(self.browser, "card", None):
|
2012-12-21 08:51:59 +01:00
|
|
|
self.focusedCard = self.browser.card.id
|
|
|
|
else:
|
|
|
|
self.focusedCard = None
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def restoreSelection(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
if not self.cards:
|
|
|
|
return
|
|
|
|
sm = self.browser.form.tableView.selectionModel()
|
|
|
|
sm.clear()
|
|
|
|
# restore selection
|
|
|
|
items = QItemSelection()
|
|
|
|
count = 0
|
|
|
|
firstIdx = None
|
|
|
|
focusedIdx = None
|
|
|
|
for row, id in enumerate(self.cards):
|
|
|
|
# if the id matches the focused card, note the index
|
|
|
|
if self.focusedCard == id:
|
|
|
|
focusedIdx = self.index(row, 0)
|
|
|
|
items.select(focusedIdx, focusedIdx)
|
|
|
|
self.focusedCard = None
|
|
|
|
# if the card was previously selected, select again
|
|
|
|
if id in self.selectedCards:
|
|
|
|
count += 1
|
|
|
|
idx = self.index(row, 0)
|
|
|
|
items.select(idx, idx)
|
|
|
|
# note down the first card of the selection, in case we don't
|
|
|
|
# have a focused card
|
|
|
|
if not firstIdx:
|
|
|
|
firstIdx = idx
|
|
|
|
# focus previously focused or first in selection
|
|
|
|
idx = focusedIdx or firstIdx
|
|
|
|
tv = self.browser.form.tableView
|
|
|
|
if idx:
|
2020-04-11 07:54:52 +02:00
|
|
|
row = idx.row()
|
|
|
|
pos = tv.rowViewportPosition(row)
|
|
|
|
visible = pos >= 0 and pos < tv.viewport().height()
|
|
|
|
tv.selectRow(row)
|
|
|
|
|
2020-04-01 03:01:20 +02:00
|
|
|
# we save and then restore the horizontal scroll position because
|
|
|
|
# scrollTo() also scrolls horizontally which is confusing
|
2020-04-11 07:54:52 +02:00
|
|
|
if not visible:
|
|
|
|
h = tv.horizontalScrollBar().value()
|
|
|
|
tv.scrollTo(idx, tv.PositionAtCenter)
|
|
|
|
tv.horizontalScrollBar().setValue(h)
|
2012-12-21 08:51:59 +01:00
|
|
|
if count < 500:
|
|
|
|
# discard large selections; they're too slow
|
2019-12-23 01:34:10 +01:00
|
|
|
sm.select(
|
|
|
|
items, QItemSelectionModel.SelectCurrent | QItemSelectionModel.Rows
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
|
|
|
tv.selectRow(0)
|
|
|
|
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
def op_executed(self, op: OpChanges, focused: bool) -> None:
|
2021-03-18 15:06:54 +01:00
|
|
|
print("op executed")
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
if op.card or op.note or op.deck or op.notetype:
|
2021-03-20 12:03:26 +01:00
|
|
|
self._rows = {}
|
2021-03-14 13:08:37 +01:00
|
|
|
if focused:
|
2021-03-16 07:39:41 +01:00
|
|
|
self.redraw_cells()
|
2021-03-18 15:06:54 +01:00
|
|
|
|
|
|
|
def begin_blocking(self) -> None:
|
|
|
|
self.block_updates = True
|
|
|
|
|
|
|
|
def end_blocking(self) -> None:
|
|
|
|
self.block_updates = False
|
|
|
|
self.redraw_cells()
|
2021-03-13 14:59:32 +01:00
|
|
|
|
2018-08-08 02:39:54 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Line painter
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
class StatusDelegate(QItemDelegate):
|
2021-02-01 00:39:55 +01:00
|
|
|
def __init__(self, browser: Browser, model: DataModel) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
QItemDelegate.__init__(self, browser)
|
2013-01-28 22:45:29 +01:00
|
|
|
self.browser = browser
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model = model
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def paint(
|
|
|
|
self, painter: QPainter, option: QStyleOptionViewItem, index: QModelIndex
|
|
|
|
) -> None:
|
2021-03-20 12:03:26 +01:00
|
|
|
if self.model.get_cell(index).is_rtl:
|
2018-08-08 02:39:54 +02:00
|
|
|
option.direction = Qt.RightToLeft
|
2021-03-20 12:03:26 +01:00
|
|
|
if row_color := self.model.get_row(index).color:
|
|
|
|
brush = QBrush(theme_manager.qcolor(row_color))
|
2012-12-21 08:51:59 +01:00
|
|
|
painter.save()
|
|
|
|
painter.fillRect(option.rect, brush)
|
|
|
|
painter.restore()
|
|
|
|
return QItemDelegate.paint(self, painter, option, index)
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Browser window
|
|
|
|
######################################################################
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
class Browser(QMainWindow):
|
2019-12-20 08:55:19 +01:00
|
|
|
model: DataModel
|
|
|
|
mw: AnkiQt
|
2020-05-20 09:56:52 +02:00
|
|
|
col: Collection
|
2020-01-15 22:41:23 +01:00
|
|
|
editor: Optional[Editor]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 11:54:28 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
mw: AnkiQt,
|
|
|
|
card: Optional[Card] = None,
|
2021-02-11 10:57:19 +01:00
|
|
|
search: Optional[Tuple[Union[str, SearchNode]]] = None,
|
2021-02-01 11:54:28 +01:00
|
|
|
) -> None:
|
2021-02-02 09:48:55 +01:00
|
|
|
"""
|
|
|
|
card : try to search for its note and select it
|
|
|
|
search: set and perform search; caller must ensure validity
|
|
|
|
"""
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
QMainWindow.__init__(self, None, Qt.Window)
|
|
|
|
self.mw = mw
|
|
|
|
self.col = self.mw.col
|
|
|
|
self.lastFilter = ""
|
2020-07-26 01:25:39 +02:00
|
|
|
self.focusTo: Optional[int] = None
|
|
|
|
self._previewer: Optional[Previewer] = None
|
2016-07-14 12:23:44 +02:00
|
|
|
self._closeEventHasCleanedUp = False
|
2012-12-21 08:51:59 +01:00
|
|
|
self.form = aqt.forms.browser.Ui_Dialog()
|
|
|
|
self.form.setupUi(self)
|
2017-08-13 11:11:40 +02:00
|
|
|
self.setupSidebar()
|
2012-12-21 08:51:59 +01:00
|
|
|
restoreGeom(self, "editor", 0)
|
|
|
|
restoreState(self, "editor")
|
|
|
|
restoreSplitter(self.form.splitter, "editor3")
|
|
|
|
self.form.splitter.setChildrenCollapsible(False)
|
2020-01-15 22:41:23 +01:00
|
|
|
self.card: Optional[Card] = None
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setupColumns()
|
|
|
|
self.setupTable()
|
|
|
|
self.setupMenus()
|
|
|
|
self.setupHeaders()
|
|
|
|
self.setupHooks()
|
|
|
|
self.setupEditor()
|
|
|
|
self.updateFont()
|
|
|
|
self.onUndoState(self.mw.form.actionUndo.isEnabled())
|
2021-02-01 11:54:28 +01:00
|
|
|
self.setupSearch(card, search)
|
2020-02-29 17:02:51 +01:00
|
|
|
gui_hooks.browser_will_show(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.show()
|
|
|
|
|
2021-03-18 01:54:02 +01:00
|
|
|
def on_backend_will_block(self) -> None:
|
2021-03-13 14:59:32 +01:00
|
|
|
# make sure the card list doesn't try to refresh itself during the operation,
|
|
|
|
# as that will block the UI
|
2021-03-18 15:06:54 +01:00
|
|
|
self.model.begin_blocking()
|
clear_unused_tags and browser redraw improvements
- clear_unused_tags() is now undoable, and returns the number of removed
notes
- add a new mw.query_op() helper for immutable queries
- decouple "freeze/unfreeze ui state" hooks from the "interface update
required" hook, so that the former is fired even on error, and can be
made re-entrant
- use a 'block_updates' flag in Python, instead of setUpdatesEnabled(),
as the latter has the side-effect of preventing child windows like
tooltips from appearing, and forces a full redrawn when updates are
enabled again. The new behaviour leads to the card list blanking out
when a long-running op is running, but in the future if we cache the
cell values we can just display them from the cache instead.
- we were indiscriminately saving the note with saveNow(), due to the
call to saveTags(). Changed so that it only saves when the tags field
is focused.
- drain the "on_done" queue on main before launching a new background
task, to lower the chances of something in on_done making a small query
to the DB and hanging until a long op finishes
- the duplicate check in the editor was executed after the webview loads,
leading to it hanging until the sidebar finishes loading. Run it at
set_note() time instead, so that the editor loads first.
- don't throw an error when a long-running op started with with_progress()
finishes after the window it was launched from has closed
- don't throw an error when the browser is closed before the sidebar
has finished loading
2021-03-17 12:27:42 +01:00
|
|
|
|
2021-03-18 01:54:02 +01:00
|
|
|
def on_backend_did_block(self) -> None:
|
2021-03-18 15:06:54 +01:00
|
|
|
self.model.end_blocking()
|
2021-03-12 08:53:14 +01:00
|
|
|
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
def on_operation_did_execute(self, changes: OpChanges) -> None:
|
2021-03-16 09:30:54 +01:00
|
|
|
focused = current_top_level_widget() == self
|
|
|
|
self.model.op_executed(changes, focused)
|
2021-03-16 10:21:18 +01:00
|
|
|
self.sidebar.op_executed(changes, focused)
|
2021-03-16 09:30:54 +01:00
|
|
|
if changes.note or changes.notetype:
|
|
|
|
if not self.editor.is_updating_note():
|
2021-03-18 15:06:54 +01:00
|
|
|
# fixme: this will leave the splitter shown, but with no current
|
|
|
|
# note being edited
|
2021-03-16 09:30:54 +01:00
|
|
|
note = self.editor.note
|
|
|
|
if note:
|
2021-03-18 15:06:54 +01:00
|
|
|
try:
|
|
|
|
note.load()
|
|
|
|
except NotFoundError:
|
|
|
|
self.editor.set_note(None)
|
|
|
|
return
|
2021-03-16 09:30:54 +01:00
|
|
|
self.editor.set_note(note)
|
|
|
|
|
|
|
|
self._renderPreview()
|
2021-03-14 13:08:37 +01:00
|
|
|
|
|
|
|
def on_focus_change(self, new: Optional[QWidget], old: Optional[QWidget]) -> None:
|
|
|
|
if current_top_level_widget() == self:
|
2021-03-16 09:30:54 +01:00
|
|
|
self.setUpdatesEnabled(True)
|
2021-03-18 15:06:54 +01:00
|
|
|
self.model.redraw_cells()
|
2021-03-16 10:21:18 +01:00
|
|
|
self.sidebar.refresh_if_needed()
|
2021-03-12 08:53:14 +01:00
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def setupMenus(self) -> None:
|
2019-03-04 07:01:10 +01:00
|
|
|
# pylint: disable=unnecessary-lambda
|
2012-12-21 08:51:59 +01:00
|
|
|
# actions
|
2016-05-31 10:51:40 +02:00
|
|
|
f = self.form
|
2017-08-11 06:40:51 +02:00
|
|
|
# edit
|
2021-03-12 08:56:13 +01:00
|
|
|
qconnect(f.actionUndo.triggered, self.undo)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(f.actionInvertSelection.triggered, self.invertSelection)
|
|
|
|
qconnect(f.actionSelectNotes.triggered, self.selectNotes)
|
2017-08-11 06:40:51 +02:00
|
|
|
if not isMac:
|
|
|
|
f.actionClose.setVisible(False)
|
2021-02-01 12:09:37 +01:00
|
|
|
qconnect(f.actionCreateFilteredDeck.triggered, self.createFilteredDeck)
|
2021-02-02 09:29:09 +01:00
|
|
|
f.actionCreateFilteredDeck.setShortcuts(["Ctrl+G", "Ctrl+Alt+G"])
|
2017-08-11 06:40:51 +02:00
|
|
|
# notes
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(f.actionAdd.triggered, self.mw.onAddCard)
|
2021-03-05 11:47:51 +01:00
|
|
|
qconnect(f.actionAdd_Tags.triggered, lambda: self.add_tags_to_selected_notes())
|
|
|
|
qconnect(
|
|
|
|
f.actionRemove_Tags.triggered,
|
|
|
|
lambda: self.remove_tags_from_selected_notes(),
|
|
|
|
)
|
clear_unused_tags and browser redraw improvements
- clear_unused_tags() is now undoable, and returns the number of removed
notes
- add a new mw.query_op() helper for immutable queries
- decouple "freeze/unfreeze ui state" hooks from the "interface update
required" hook, so that the former is fired even on error, and can be
made re-entrant
- use a 'block_updates' flag in Python, instead of setUpdatesEnabled(),
as the latter has the side-effect of preventing child windows like
tooltips from appearing, and forces a full redrawn when updates are
enabled again. The new behaviour leads to the card list blanking out
when a long-running op is running, but in the future if we cache the
cell values we can just display them from the cache instead.
- we were indiscriminately saving the note with saveNow(), due to the
call to saveTags(). Changed so that it only saves when the tags field
is focused.
- drain the "on_done" queue on main before launching a new background
task, to lower the chances of something in on_done making a small query
to the DB and hanging until a long op finishes
- the duplicate check in the editor was executed after the webview loads,
leading to it hanging until the sidebar finishes loading. Run it at
set_note() time instead, so that the editor loads first.
- don't throw an error when a long-running op started with with_progress()
finishes after the window it was launched from has closed
- don't throw an error when the browser is closed before the sidebar
has finished loading
2021-03-17 12:27:42 +01:00
|
|
|
qconnect(f.actionClear_Unused_Tags.triggered, self.clear_unused_tags)
|
2021-03-18 03:06:45 +01:00
|
|
|
qconnect(
|
|
|
|
f.actionToggle_Mark.triggered, lambda: self.toggle_mark_of_selected_notes()
|
|
|
|
)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(f.actionChangeModel.triggered, self.onChangeModel)
|
|
|
|
qconnect(f.actionFindDuplicates.triggered, self.onFindDupes)
|
|
|
|
qconnect(f.actionFindReplace.triggered, self.onFindReplace)
|
|
|
|
qconnect(f.actionManage_Note_Types.triggered, self.mw.onNoteTypes)
|
2021-03-12 08:53:14 +01:00
|
|
|
qconnect(f.actionDelete.triggered, self.delete_selected_notes)
|
2017-08-11 06:40:51 +02:00
|
|
|
# cards
|
2021-03-12 08:53:14 +01:00
|
|
|
qconnect(f.actionChange_Deck.triggered, self.set_deck_of_selected_cards)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(f.action_Info.triggered, self.showCardInfo)
|
|
|
|
qconnect(f.actionReposition.triggered, self.reposition)
|
Rework reschedule tool
The old rescheduling dialog's two options have been split into two
separate menu items, "Forget", and "Set Due Date"
For cards that are not review cards, "Set Due Date" behaves like the
old reschedule option, changing the cards into a review card, and
and setting both the interval and due date to the provided number of
days.
When "Set Due Date" is applied to a review card, it no longer resets
the card's interval. Instead, it looks at how much the provided number
of days will change the original interval, and adjusts the interval by
that amount, so that cards that are answered earlier receive a smaller
next interval, and cards that are answered after a longer delay receive
a bonus.
For example, imagine a card was answered on day 5, and given an interval
of 10 days, so it has a due date of day 15.
- if on day 10 the due date is changed to day 12 (today+2), the card
is being scheduled 3 days earlier than it was supposed to be, so the
interval will be adjusted to 7 days.
- and if on day 10 the due date is changed to day 20, the interval will
be changed from 10 days to 15 days.
There is no separate option to reset the interval of a review card, but
it can be accomplished by forgetting the card(s), and then setting the
desired due date.
Other notes:
- Added the action to the review screen as well.
- Set the shortcut to Ctrl+Shift+D, and changed the existing Delete
Tags shortcut to Ctrl+Alt+Shift+A.
2021-02-07 11:58:16 +01:00
|
|
|
qconnect(f.action_set_due_date.triggered, self.set_due_date)
|
|
|
|
qconnect(f.action_forget.triggered, self.forget_cards)
|
2021-03-04 13:14:35 +01:00
|
|
|
qconnect(f.actionToggle_Suspend.triggered, self.suspend_selected_cards)
|
2021-03-18 03:06:45 +01:00
|
|
|
qconnect(f.actionRed_Flag.triggered, lambda: self.set_flag_of_selected_cards(1))
|
|
|
|
qconnect(
|
|
|
|
f.actionOrange_Flag.triggered, lambda: self.set_flag_of_selected_cards(2)
|
|
|
|
)
|
|
|
|
qconnect(
|
|
|
|
f.actionGreen_Flag.triggered, lambda: self.set_flag_of_selected_cards(3)
|
|
|
|
)
|
|
|
|
qconnect(
|
|
|
|
f.actionBlue_Flag.triggered, lambda: self.set_flag_of_selected_cards(4)
|
|
|
|
)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(f.actionExport.triggered, lambda: self._on_export_notes())
|
2012-12-21 08:51:59 +01:00
|
|
|
# jumps
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(f.actionPreviousCard.triggered, self.onPreviousCard)
|
|
|
|
qconnect(f.actionNextCard.triggered, self.onNextCard)
|
|
|
|
qconnect(f.actionFirstCard.triggered, self.onFirstCard)
|
|
|
|
qconnect(f.actionLastCard.triggered, self.onLastCard)
|
|
|
|
qconnect(f.actionFind.triggered, self.onFind)
|
|
|
|
qconnect(f.actionNote.triggered, self.onNote)
|
|
|
|
qconnect(f.actionSidebar.triggered, self.focusSidebar)
|
|
|
|
qconnect(f.actionCardList.triggered, self.onCardList)
|
2012-12-21 08:51:59 +01:00
|
|
|
# help
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(f.actionGuide.triggered, self.onHelp)
|
2012-12-21 08:51:59 +01:00
|
|
|
# keyboard shortcut for shift+home/end
|
|
|
|
self.pgUpCut = QShortcut(QKeySequence("Shift+Home"), self)
|
2020-01-15 22:41:23 +01:00
|
|
|
qconnect(self.pgUpCut.activated, self.onFirstCard)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.pgDownCut = QShortcut(QKeySequence("Shift+End"), self)
|
2020-01-15 22:41:23 +01:00
|
|
|
qconnect(self.pgDownCut.activated, self.onLastCard)
|
2012-12-21 08:51:59 +01:00
|
|
|
# add-on hook
|
2020-01-15 08:18:11 +01:00
|
|
|
gui_hooks.browser_menus_did_init(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw.maybeHideAccelerators(self)
|
|
|
|
|
2017-12-14 05:49:51 +01:00
|
|
|
# context menu
|
|
|
|
self.form.tableView.setContextMenuPolicy(Qt.CustomContextMenu)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.form.tableView.customContextMenuRequested, self.onContextMenu)
|
2017-12-14 05:49:51 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onContextMenu(self, _point: QPoint) -> None:
|
2017-12-14 05:49:51 +01:00
|
|
|
m = QMenu()
|
|
|
|
for act in self.form.menu_Cards.actions():
|
|
|
|
m.addAction(act)
|
|
|
|
m.addSeparator()
|
|
|
|
for act in self.form.menu_Notes.actions():
|
|
|
|
m.addAction(act)
|
2020-01-15 22:53:12 +01:00
|
|
|
gui_hooks.browser_will_show_context_menu(self, m)
|
2019-02-05 05:37:07 +01:00
|
|
|
qtMenuShortcutWorkaround(m)
|
2017-12-14 05:49:51 +01:00
|
|
|
m.exec_(QCursor.pos())
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def updateFont(self) -> None:
|
2013-05-23 07:12:04 +02:00
|
|
|
# we can't choose different line heights efficiently, so we need
|
|
|
|
# to pick a line height big enough for any card template
|
|
|
|
curmax = 16
|
|
|
|
for m in self.col.models.all():
|
2019-12-23 01:34:10 +01:00
|
|
|
for t in m["tmpls"]:
|
2013-05-23 07:12:04 +02:00
|
|
|
bsize = t.get("bsize", 0)
|
|
|
|
if bsize > curmax:
|
|
|
|
curmax = bsize
|
2019-12-23 01:34:10 +01:00
|
|
|
self.form.tableView.verticalHeader().setDefaultSectionSize(curmax + 6)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def closeEvent(self, evt: QCloseEvent) -> None:
|
2017-08-16 04:45:33 +02:00
|
|
|
if self._closeEventHasCleanedUp:
|
2016-07-14 12:23:44 +02:00
|
|
|
evt.accept()
|
2017-08-16 04:45:33 +02:00
|
|
|
return
|
2021-03-16 13:40:37 +01:00
|
|
|
self.editor.call_after_note_saved(self._closeWindow)
|
2017-08-16 04:45:33 +02:00
|
|
|
evt.ignore()
|
2016-07-14 12:23:44 +02:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def _closeWindow(self) -> None:
|
2020-04-10 10:42:28 +02:00
|
|
|
self._cleanup_preview()
|
2017-08-16 04:45:33 +02:00
|
|
|
self.editor.cleanup()
|
2012-12-21 08:51:59 +01:00
|
|
|
saveSplitter(self.form.splitter, "editor3")
|
|
|
|
saveGeom(self, "editor")
|
|
|
|
saveState(self, "editor")
|
2014-03-14 07:29:48 +01:00
|
|
|
saveHeader(self.form.tableView.horizontalHeader(), "editor")
|
2012-12-21 08:51:59 +01:00
|
|
|
self.teardownHooks()
|
|
|
|
self.mw.maybeReset()
|
2017-08-16 04:45:33 +02:00
|
|
|
aqt.dialogs.markClosed("Browser")
|
2016-07-14 12:23:44 +02:00
|
|
|
self._closeEventHasCleanedUp = True
|
2021-03-05 04:07:52 +01:00
|
|
|
self.mw.deferred_delete_and_garbage_collect(self)
|
2017-08-16 04:45:33 +02:00
|
|
|
self.close()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
@ensure_editor_saved
|
2021-02-01 00:39:55 +01:00
|
|
|
def closeWithCallback(self, onsuccess: Callable) -> None:
|
2021-03-16 13:40:37 +01:00
|
|
|
self._closeWindow()
|
|
|
|
onsuccess()
|
2013-04-11 12:23:32 +02:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def keyPressEvent(self, evt: QKeyEvent) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
if evt.key() == Qt.Key_Escape:
|
|
|
|
self.close()
|
2017-08-13 11:11:40 +02:00
|
|
|
else:
|
|
|
|
super().keyPressEvent(evt)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def setupColumns(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.columns = [
|
2020-11-17 08:42:43 +01:00
|
|
|
("question", tr(TR.BROWSING_QUESTION)),
|
|
|
|
("answer", tr(TR.BROWSING_ANSWER)),
|
|
|
|
("template", tr(TR.BROWSING_CARD)),
|
|
|
|
("deck", tr(TR.DECKS_DECK)),
|
|
|
|
("noteFld", tr(TR.BROWSING_SORT_FIELD)),
|
|
|
|
("noteCrt", tr(TR.BROWSING_CREATED)),
|
2020-06-29 10:34:03 +02:00
|
|
|
("noteMod", tr(TR.SEARCH_NOTE_MODIFIED)),
|
|
|
|
("cardMod", tr(TR.SEARCH_CARD_MODIFIED)),
|
2020-02-27 03:25:19 +01:00
|
|
|
("cardDue", tr(TR.STATISTICS_DUE_DATE)),
|
2020-11-17 08:42:43 +01:00
|
|
|
("cardIvl", tr(TR.BROWSING_INTERVAL)),
|
|
|
|
("cardEase", tr(TR.BROWSING_EASE)),
|
|
|
|
("cardReps", tr(TR.SCHEDULING_REVIEWS)),
|
|
|
|
("cardLapses", tr(TR.SCHEDULING_LAPSES)),
|
|
|
|
("noteTags", tr(TR.EDITING_TAGS)),
|
|
|
|
("note", tr(TR.BROWSING_NOTE)),
|
2012-12-21 08:51:59 +01:00
|
|
|
]
|
|
|
|
self.columns.sort(key=itemgetter(1))
|
|
|
|
|
2021-02-01 11:54:28 +01:00
|
|
|
def reopen(
|
|
|
|
self,
|
|
|
|
_mw: AnkiQt,
|
|
|
|
card: Optional[Card] = None,
|
2021-02-11 10:57:19 +01:00
|
|
|
search: Optional[Tuple[Union[str, SearchNode]]] = None,
|
2021-02-01 11:54:28 +01:00
|
|
|
) -> None:
|
|
|
|
if search is not None:
|
|
|
|
self.search_for_terms(*search)
|
|
|
|
self.form.searchEdit.setFocus()
|
|
|
|
elif card:
|
|
|
|
self.show_single_card(card)
|
|
|
|
self.form.searchEdit.setFocus()
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Searching
|
|
|
|
######################################################################
|
|
|
|
|
2021-02-01 11:54:28 +01:00
|
|
|
def setupSearch(
|
|
|
|
self,
|
|
|
|
card: Optional[Card] = None,
|
2021-02-11 10:57:19 +01:00
|
|
|
search: Optional[Tuple[Union[str, SearchNode]]] = None,
|
2021-02-01 23:33:41 +01:00
|
|
|
) -> None:
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.form.searchEdit.lineEdit().returnPressed, self.onSearchActivated)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.form.searchEdit.setCompleter(None)
|
2021-01-29 21:07:42 +01:00
|
|
|
self.form.searchEdit.lineEdit().setPlaceholderText(
|
|
|
|
tr(TR.BROWSING_SEARCH_BAR_HINT)
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2021-01-30 10:51:31 +01:00
|
|
|
self.form.searchEdit.addItems(self.mw.pm.profile["searchHistory"])
|
2021-02-01 11:54:28 +01:00
|
|
|
if search is not None:
|
|
|
|
self.search_for_terms(*search)
|
|
|
|
elif card:
|
|
|
|
self.show_single_card(card)
|
|
|
|
else:
|
|
|
|
self.search_for(
|
2021-02-11 10:57:19 +01:00
|
|
|
self.col.build_search_string(SearchNode(deck="current")), ""
|
2021-02-01 11:54:28 +01:00
|
|
|
)
|
2016-07-14 12:23:44 +02:00
|
|
|
self.form.searchEdit.setFocus()
|
|
|
|
|
|
|
|
# search triggered by user
|
2021-03-16 13:40:37 +01:00
|
|
|
@ensure_editor_saved
|
2021-02-01 00:39:55 +01:00
|
|
|
def onSearchActivated(self) -> None:
|
2021-01-29 21:07:42 +01:00
|
|
|
text = self.form.searchEdit.lineEdit().text()
|
2021-01-30 10:51:31 +01:00
|
|
|
try:
|
|
|
|
normed = self.col.build_search_string(text)
|
rework filtered deck screen & search errors
- Filtered deck creation now happens as an atomic operation, and is
undoable.
- The logic for initial search text, normalizing searches and so on
has been pushed into the backend.
- Use protobuf to pass the filtered deck to the updated dialog, so
we don't need to deal with untyped JSON.
- Change the "revise your search?" prompt to be a simple info box -
user has access to cancel and build buttons, and doesn't need a separate
prompt. Tweak the wording so the 'show excluded' button should be more
obvious.
- Filtered decks have a time appended to them instead of a number,
primarily because it's easier to implement. No objections going back to
the old behaviour if someone wants to contribute a clean patch.
The standard de-duplication will happen if two decks are created in the
same minute with the same name.
- Tweak the default sort order, and start with two searches. The UI
will still hide the second search by default, but by starting with two,
the frontend doesn't need logic for creating the starting text.
- Search errors now have their own error type, instead of using
InvalidInput, as that was intended mainly for bad API calls. The markdown
conversion is done when the error is converted from the backend, allowing
errors to printed as a string without any special handling by the calling
code.
TODO: when building a new filtered deck, update_active() is clobbering
the undo log when the overview is refreshed
2021-03-24 12:52:48 +01:00
|
|
|
except Exception as err:
|
|
|
|
showWarning(str(err))
|
2021-01-30 10:51:31 +01:00
|
|
|
else:
|
|
|
|
self.search_for(normed)
|
2021-01-29 23:05:51 +01:00
|
|
|
self.update_history()
|
2018-11-12 03:43:54 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def search_for(self, search: str, prompt: Optional[str] = None) -> None:
|
2021-01-30 11:05:48 +01:00
|
|
|
"""Keep track of search string so that we reuse identical search when
|
|
|
|
refreshing, rather than whatever is currently in the search field.
|
|
|
|
Optionally set the search bar to a different text than the actual search.
|
|
|
|
"""
|
2016-07-14 12:23:44 +02:00
|
|
|
|
2020-10-19 20:37:17 +02:00
|
|
|
self._lastSearchTxt = search
|
2021-01-30 10:51:31 +01:00
|
|
|
prompt = search if prompt == None else prompt
|
|
|
|
self.form.searchEdit.lineEdit().setText(prompt)
|
2016-07-14 12:23:44 +02:00
|
|
|
self.search()
|
|
|
|
|
2021-02-09 01:50:39 +01:00
|
|
|
def current_search(self) -> str:
|
|
|
|
return self.form.searchEdit.lineEdit().text()
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def search(self) -> None:
|
2021-01-30 10:51:31 +01:00
|
|
|
"""Search triggered programmatically. Caller must have saved note first."""
|
2016-07-14 12:23:44 +02:00
|
|
|
|
2021-01-29 23:05:51 +01:00
|
|
|
try:
|
|
|
|
self.model.search(self._lastSearchTxt)
|
2021-01-30 10:51:31 +01:00
|
|
|
except Exception as err:
|
rework filtered deck screen & search errors
- Filtered deck creation now happens as an atomic operation, and is
undoable.
- The logic for initial search text, normalizing searches and so on
has been pushed into the backend.
- Use protobuf to pass the filtered deck to the updated dialog, so
we don't need to deal with untyped JSON.
- Change the "revise your search?" prompt to be a simple info box -
user has access to cancel and build buttons, and doesn't need a separate
prompt. Tweak the wording so the 'show excluded' button should be more
obvious.
- Filtered decks have a time appended to them instead of a number,
primarily because it's easier to implement. No objections going back to
the old behaviour if someone wants to contribute a clean patch.
The standard de-duplication will happen if two decks are created in the
same minute with the same name.
- Tweak the default sort order, and start with two searches. The UI
will still hide the second search by default, but by starting with two,
the frontend doesn't need logic for creating the starting text.
- Search errors now have their own error type, instead of using
InvalidInput, as that was intended mainly for bad API calls. The markdown
conversion is done when the error is converted from the backend, allowing
errors to printed as a string without any special handling by the calling
code.
TODO: when building a new filtered deck, update_active() is clobbering
the undo log when the overview is refreshed
2021-03-24 12:52:48 +01:00
|
|
|
showWarning(str(err))
|
2012-12-21 08:51:59 +01:00
|
|
|
if not self.model.cards:
|
|
|
|
# no row change will fire
|
2021-03-17 01:13:26 +01:00
|
|
|
self.onRowChanged(None, None)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def update_history(self) -> None:
|
2019-12-23 01:34:10 +01:00
|
|
|
sh = self.mw.pm.profile["searchHistory"]
|
2021-01-29 23:05:51 +01:00
|
|
|
if self._lastSearchTxt in sh:
|
|
|
|
sh.remove(self._lastSearchTxt)
|
|
|
|
sh.insert(0, self._lastSearchTxt)
|
2012-12-21 08:51:59 +01:00
|
|
|
sh = sh[:30]
|
|
|
|
self.form.searchEdit.clear()
|
|
|
|
self.form.searchEdit.addItems(sh)
|
2019-12-23 01:34:10 +01:00
|
|
|
self.mw.pm.profile["searchHistory"] = sh
|
2020-12-22 11:08:47 +01:00
|
|
|
|
2021-01-30 11:24:33 +01:00
|
|
|
def updateTitle(self) -> int:
|
2012-12-21 08:51:59 +01:00
|
|
|
selected = len(self.form.tableView.selectionModel().selectedRows())
|
|
|
|
cur = len(self.model.cards)
|
2019-12-23 01:34:10 +01:00
|
|
|
self.setWindowTitle(
|
2020-11-18 04:40:21 +01:00
|
|
|
without_unicode_isolation(
|
|
|
|
tr(TR.BROWSING_WINDOW_TITLE, total=cur, selected=selected)
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
return selected
|
|
|
|
|
2021-02-11 10:57:19 +01:00
|
|
|
def search_for_terms(self, *search_terms: Union[str, SearchNode]) -> None:
|
2021-02-01 11:54:28 +01:00
|
|
|
search = self.col.build_search_string(*search_terms)
|
|
|
|
self.form.searchEdit.setEditText(search)
|
|
|
|
self.onSearchActivated()
|
2021-01-30 11:05:48 +01:00
|
|
|
|
2021-02-01 23:46:56 +01:00
|
|
|
def show_single_card(self, card: Card) -> None:
|
2021-02-01 11:54:28 +01:00
|
|
|
if card.nid:
|
2021-01-30 10:51:31 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def on_show_single_card() -> None:
|
2021-01-30 10:51:31 +01:00
|
|
|
self.card = card
|
2021-02-11 10:57:19 +01:00
|
|
|
search = self.col.build_search_string(SearchNode(nid=card.nid))
|
2021-01-30 10:51:31 +01:00
|
|
|
search = gui_hooks.default_search(search, card)
|
|
|
|
self.search_for(search, "")
|
|
|
|
self.focusCid(card.id)
|
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
self.editor.call_after_note_saved(on_show_single_card)
|
2021-01-29 21:07:42 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onReset(self) -> None:
|
2021-01-23 10:59:12 +01:00
|
|
|
self.sidebar.refresh()
|
experiment with preserving search when resetting
Up until now, we've been forcing a new search whenever reset is called.
The primary reason was that the card list display routines did not expect
a card or note to have been removed. By updating the model to show
"(deleted)" when a card or note is missing, we no longer have to repeat
the search.
This has a few advantages:
- Searches, especially complex ones, can be slow to execute. When we
perform them after every operation like a delete, it can make Anki feel
sluggish.
- The fact that notes have been deleted becomes more obvious - some users
found it easy to miss the "deleted" pop-up in the past.
This change does not just affect deletions, as many other operations
trigger a reset as well. In the past, when using 'set due date' in the
review screen for example, it caused an ugly flicker in the browser screen,
and could be slow when the current search couldn't be quickly redone.
The disadvantage of this approach is that the displayed content may
not reflect the specified search, which has the potential to be confusing.
But if that turns out to be a problem, it could be (partly) alleviated by
displaying a refresh button next to the search bar when the search may
need to be refreshed.
Feedback welcome!
2021-03-13 05:52:44 +01:00
|
|
|
self.model.reset()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Table view & editor
|
|
|
|
######################################################################
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def setupTable(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model = DataModel(self)
|
|
|
|
self.form.tableView.setSortingEnabled(True)
|
|
|
|
self.form.tableView.setModel(self.model)
|
|
|
|
self.form.tableView.selectionModel()
|
|
|
|
self.form.tableView.setItemDelegate(StatusDelegate(self, self.model))
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(
|
|
|
|
self.form.tableView.selectionModel().selectionChanged, self.onRowChanged
|
|
|
|
)
|
2020-03-20 09:34:03 +01:00
|
|
|
self.form.tableView.setWordWrap(False)
|
2020-01-23 06:08:10 +01:00
|
|
|
if not theme_manager.night_mode:
|
|
|
|
self.form.tableView.setStyleSheet(
|
|
|
|
"QTableView{ selection-background-color: rgba(150, 150, 150, 50); "
|
|
|
|
"selection-color: black; }"
|
|
|
|
)
|
2020-02-02 03:01:27 +01:00
|
|
|
elif theme_manager.macos_dark_mode():
|
2021-02-05 09:50:01 +01:00
|
|
|
grid = colors.FRAME_BG
|
2020-02-02 04:09:02 +01:00
|
|
|
self.form.tableView.setStyleSheet(
|
|
|
|
f"""
|
2020-02-02 03:01:27 +01:00
|
|
|
QTableView {{ gridline-color: {grid} }}
|
2020-02-02 04:09:02 +01:00
|
|
|
"""
|
|
|
|
)
|
2018-09-24 09:29:19 +02:00
|
|
|
self.singleCard = False
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def setupEditor(self) -> None:
|
|
|
|
def add_preview_button(leftbuttons: List[str], editor: Editor) -> None:
|
2021-01-10 00:00:19 +01:00
|
|
|
preview_shortcut = "Ctrl+Shift+P"
|
2021-01-10 01:44:08 +01:00
|
|
|
leftbuttons.insert(
|
|
|
|
0,
|
|
|
|
editor.addButton(
|
|
|
|
None,
|
|
|
|
"preview",
|
|
|
|
lambda _editor: self.onTogglePreview(),
|
|
|
|
tr(
|
|
|
|
TR.BROWSING_PREVIEW_SELECTED_CARD,
|
|
|
|
val=shortcut(preview_shortcut),
|
|
|
|
),
|
|
|
|
tr(TR.ACTIONS_PREVIEW),
|
|
|
|
id="previewButton",
|
|
|
|
keys=preview_shortcut,
|
|
|
|
disables=False,
|
|
|
|
rightside=False,
|
|
|
|
toggleable=True,
|
|
|
|
),
|
|
|
|
)
|
2021-01-09 22:34:53 +01:00
|
|
|
|
|
|
|
gui_hooks.editor_did_init_left_buttons.append(add_preview_button)
|
2019-12-23 01:34:10 +01:00
|
|
|
self.editor = aqt.editor.Editor(self.mw, self.form.fieldsArea, self)
|
2021-01-09 22:34:53 +01:00
|
|
|
gui_hooks.editor_did_init_left_buttons.remove(add_preview_button)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
@ensure_editor_saved
|
2021-03-17 05:51:59 +01:00
|
|
|
def onRowChanged(
|
|
|
|
self, current: Optional[QItemSelection], previous: Optional[QItemSelection]
|
|
|
|
) -> None:
|
2021-03-16 13:40:37 +01:00
|
|
|
"""Update current note and hide/show editor."""
|
2020-04-02 11:44:51 +02:00
|
|
|
if self._closeEventHasCleanedUp:
|
|
|
|
return
|
2012-12-21 08:51:59 +01:00
|
|
|
update = self.updateTitle()
|
|
|
|
show = self.model.cards and update == 1
|
2017-08-31 12:38:12 +02:00
|
|
|
idx = self.form.tableView.selectionModel().currentIndex()
|
|
|
|
if idx.isValid():
|
2021-03-21 18:44:31 +01:00
|
|
|
self.card = self.model.getCard(idx)
|
experiment with preserving search when resetting
Up until now, we've been forcing a new search whenever reset is called.
The primary reason was that the card list display routines did not expect
a card or note to have been removed. By updating the model to show
"(deleted)" when a card or note is missing, we no longer have to repeat
the search.
This has a few advantages:
- Searches, especially complex ones, can be slow to execute. When we
perform them after every operation like a delete, it can make Anki feel
sluggish.
- The fact that notes have been deleted becomes more obvious - some users
found it easy to miss the "deleted" pop-up in the past.
This change does not just affect deletions, as many other operations
trigger a reset as well. In the past, when using 'set due date' in the
review screen for example, it caused an ugly flicker in the browser screen,
and could be slow when the current search couldn't be quickly redone.
The disadvantage of this approach is that the displayed content may
not reflect the specified search, which has the potential to be confusing.
But if that turns out to be a problem, it could be (partly) alleviated by
displaying a refresh button next to the search bar when the search may
need to be refreshed.
Feedback welcome!
2021-03-13 05:52:44 +01:00
|
|
|
show = show and self.card is not None
|
|
|
|
self.form.splitter.widget(1).setVisible(bool(show))
|
2017-08-31 12:38:12 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
if not show:
|
2021-03-16 07:39:41 +01:00
|
|
|
self.editor.set_note(None)
|
2013-05-10 08:02:58 +02:00
|
|
|
self.singleCard = False
|
2020-09-24 06:05:16 +02:00
|
|
|
self._renderPreview()
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2021-03-16 07:39:41 +01:00
|
|
|
self.editor.set_note(self.card.note(reload=True), focusTo=self.focusTo)
|
2017-08-05 07:15:19 +02:00
|
|
|
self.focusTo = None
|
2020-03-04 17:41:26 +01:00
|
|
|
self.editor.card = self.card
|
2013-05-10 08:02:58 +02:00
|
|
|
self.singleCard = True
|
2021-03-18 03:06:45 +01:00
|
|
|
self._update_flags_menu()
|
2020-01-15 08:45:35 +01:00
|
|
|
gui_hooks.browser_did_change_row(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def currentRow(self) -> int:
|
2013-05-03 10:52:46 +02:00
|
|
|
idx = self.form.tableView.selectionModel().currentIndex()
|
|
|
|
return idx.row()
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Headers & sorting
|
|
|
|
######################################################################
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def setupHeaders(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
vh = self.form.tableView.verticalHeader()
|
|
|
|
hh = self.form.tableView.horizontalHeader()
|
|
|
|
if not isWin:
|
|
|
|
vh.hide()
|
|
|
|
hh.show()
|
2014-03-14 07:29:48 +01:00
|
|
|
restoreHeader(hh, "editor")
|
2012-12-21 08:51:59 +01:00
|
|
|
hh.setHighlightSections(False)
|
|
|
|
hh.setMinimumSectionSize(50)
|
2016-05-31 10:51:40 +02:00
|
|
|
hh.setSectionsMovable(True)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setColumnSizes()
|
|
|
|
hh.setContextMenuPolicy(Qt.CustomContextMenu)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(hh.customContextMenuRequested, self.onHeaderContext)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setSortIndicator()
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(hh.sortIndicatorChanged, self.onSortChanged)
|
|
|
|
qconnect(hh.sectionMoved, self.onColumnMoved)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
@ensure_editor_saved
|
2021-02-01 00:39:55 +01:00
|
|
|
def onSortChanged(self, idx: int, ord: int) -> None:
|
2021-03-16 13:40:37 +01:00
|
|
|
ord = bool(ord)
|
2012-12-21 08:51:59 +01:00
|
|
|
type = self.model.activeCols[idx]
|
2020-03-20 22:55:53 +01:00
|
|
|
noSort = ("question", "answer")
|
2012-12-21 08:51:59 +01:00
|
|
|
if type in noSort:
|
2020-11-17 08:42:43 +01:00
|
|
|
showInfo(tr(TR.BROWSING_SORTING_ON_THIS_COLUMN_IS_NOT))
|
2019-12-23 01:34:10 +01:00
|
|
|
type = self.col.conf["sortType"]
|
|
|
|
if self.col.conf["sortType"] != type:
|
|
|
|
self.col.conf["sortType"] = type
|
2012-12-21 08:51:59 +01:00
|
|
|
# default to descending for non-text fields
|
|
|
|
if type == "noteFld":
|
|
|
|
ord = not ord
|
2021-02-08 05:10:05 +01:00
|
|
|
self.col.set_config_bool(Config.Bool.BROWSER_SORT_BACKWARDS, ord)
|
2020-03-20 06:44:35 +01:00
|
|
|
self.col.save()
|
2016-07-14 12:23:44 +02:00
|
|
|
self.search()
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2021-02-08 05:10:05 +01:00
|
|
|
if self.col.get_config_bool(Config.Bool.BROWSER_SORT_BACKWARDS) != ord:
|
|
|
|
self.col.set_config_bool(Config.Bool.BROWSER_SORT_BACKWARDS, ord)
|
2020-03-20 06:44:35 +01:00
|
|
|
self.col.save()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model.reverse()
|
|
|
|
self.setSortIndicator()
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def setSortIndicator(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
hh = self.form.tableView.horizontalHeader()
|
2019-12-23 01:34:10 +01:00
|
|
|
type = self.col.conf["sortType"]
|
2012-12-21 08:51:59 +01:00
|
|
|
if type not in self.model.activeCols:
|
|
|
|
hh.setSortIndicatorShown(False)
|
|
|
|
return
|
|
|
|
idx = self.model.activeCols.index(type)
|
2021-02-08 05:10:05 +01:00
|
|
|
if self.col.get_config_bool(Config.Bool.BROWSER_SORT_BACKWARDS):
|
2012-12-21 08:51:59 +01:00
|
|
|
ord = Qt.DescendingOrder
|
|
|
|
else:
|
|
|
|
ord = Qt.AscendingOrder
|
|
|
|
hh.blockSignals(True)
|
|
|
|
hh.setSortIndicator(idx, ord)
|
|
|
|
hh.blockSignals(False)
|
|
|
|
hh.setSortIndicatorShown(True)
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onHeaderContext(self, pos: QPoint) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
gpos = self.form.tableView.mapToGlobal(pos)
|
|
|
|
m = QMenu()
|
|
|
|
for type, name in self.columns:
|
|
|
|
a = m.addAction(name)
|
|
|
|
a.setCheckable(True)
|
|
|
|
a.setChecked(type in self.model.activeCols)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(a.toggled, lambda b, t=type: self.toggleField(t))
|
2020-03-27 23:06:22 +01:00
|
|
|
gui_hooks.browser_header_will_show_context_menu(self, m)
|
2012-12-21 08:51:59 +01:00
|
|
|
m.exec_(gpos)
|
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
@ensure_editor_saved_on_trigger
|
2021-02-01 00:39:55 +01:00
|
|
|
def toggleField(self, type: str) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model.beginReset()
|
|
|
|
if type in self.model.activeCols:
|
|
|
|
if len(self.model.activeCols) < 2:
|
2018-07-25 11:52:21 +02:00
|
|
|
self.model.endReset()
|
2021-02-01 00:39:55 +01:00
|
|
|
showInfo(tr(TR.BROWSING_YOU_MUST_HAVE_AT_LEAST_ONE))
|
|
|
|
return
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model.activeCols.remove(type)
|
2019-12-23 01:34:10 +01:00
|
|
|
adding = False
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
|
|
|
self.model.activeCols.append(type)
|
2019-12-23 01:34:10 +01:00
|
|
|
adding = True
|
2020-06-02 05:39:15 +02:00
|
|
|
self.col.conf["activeCols"] = self.model.activeCols
|
2012-12-21 08:51:59 +01:00
|
|
|
# sorted field may have been hidden
|
|
|
|
self.setSortIndicator()
|
|
|
|
self.setColumnSizes()
|
|
|
|
self.model.endReset()
|
2013-10-04 01:43:46 +02:00
|
|
|
# if we added a column, scroll to it
|
|
|
|
if adding:
|
|
|
|
row = self.currentRow()
|
|
|
|
idx = self.model.index(row, len(self.model.activeCols) - 1)
|
|
|
|
self.form.tableView.scrollTo(idx)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def setColumnSizes(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
hh = self.form.tableView.horizontalHeader()
|
2016-05-31 10:51:40 +02:00
|
|
|
hh.setSectionResizeMode(QHeaderView.Interactive)
|
2019-12-23 01:34:10 +01:00
|
|
|
hh.setSectionResizeMode(
|
|
|
|
hh.logicalIndex(len(self.model.activeCols) - 1), QHeaderView.Stretch
|
|
|
|
)
|
2013-11-25 20:57:39 +01:00
|
|
|
# this must be set post-resize or it doesn't work
|
|
|
|
hh.setCascadingSectionResizes(False)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onColumnMoved(self, *args: Any) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setColumnSizes()
|
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
def setupSidebar(self) -> None:
|
2020-11-17 08:42:43 +01:00
|
|
|
dw = self.sidebarDockWidget = QDockWidget(tr(TR.BROWSING_SIDEBAR), self)
|
2017-08-13 11:11:40 +02:00
|
|
|
dw.setFeatures(QDockWidget.DockWidgetClosable)
|
|
|
|
dw.setObjectName("Sidebar")
|
|
|
|
dw.setAllowedAreas(Qt.LeftDockWidgetArea)
|
2020-10-10 03:42:49 +02:00
|
|
|
|
2021-01-23 10:59:12 +01:00
|
|
|
self.sidebar = SidebarTreeView(self)
|
|
|
|
self.sidebarTree = self.sidebar # legacy alias
|
|
|
|
dw.setWidget(self.sidebar)
|
2021-01-26 23:35:40 +01:00
|
|
|
qconnect(
|
2021-02-05 05:32:56 +01:00
|
|
|
self.form.actionSidebarFilter.triggered,
|
2021-01-26 23:35:40 +01:00
|
|
|
self.focusSidebarSearchBar,
|
|
|
|
)
|
2021-02-26 13:04:30 +01:00
|
|
|
grid = QGridLayout()
|
2021-03-17 05:51:59 +01:00
|
|
|
grid.addWidget(self.sidebar.searchBar, 0, 0)
|
|
|
|
grid.addWidget(self.sidebar.toolbar, 0, 1)
|
2021-02-26 13:04:30 +01:00
|
|
|
grid.addWidget(self.sidebar, 1, 0, 1, 2)
|
|
|
|
grid.setContentsMargins(0, 0, 0, 0)
|
|
|
|
grid.setSpacing(0)
|
2021-01-26 23:35:40 +01:00
|
|
|
w = QWidget()
|
2021-02-26 13:04:30 +01:00
|
|
|
w.setLayout(grid)
|
2021-01-26 23:35:40 +01:00
|
|
|
dw.setWidget(w)
|
2017-08-15 06:54:13 +02:00
|
|
|
self.sidebarDockWidget.setFloating(False)
|
2021-01-23 10:59:12 +01:00
|
|
|
|
2017-08-15 07:19:22 +02:00
|
|
|
self.sidebarDockWidget.setTitleBarWidget(QWidget())
|
2017-08-14 08:57:43 +02:00
|
|
|
self.addDockWidget(Qt.LeftDockWidgetArea, dw)
|
2017-08-13 11:11:40 +02:00
|
|
|
|
2021-01-23 10:59:12 +01:00
|
|
|
# schedule sidebar to refresh after browser window has loaded, so the
|
|
|
|
# UI is more responsive
|
|
|
|
self.mw.progress.timer(10, self.sidebar.refresh, False)
|
2017-08-13 11:11:40 +02:00
|
|
|
|
2021-01-26 23:35:40 +01:00
|
|
|
def showSidebar(self) -> None:
|
2020-10-24 10:47:25 +02:00
|
|
|
# workaround for PyQt focus bug
|
|
|
|
self.editor.hideCompleters()
|
2017-08-13 11:11:40 +02:00
|
|
|
self.sidebarDockWidget.setVisible(True)
|
2021-01-26 23:35:40 +01:00
|
|
|
|
|
|
|
def focusSidebar(self) -> None:
|
|
|
|
self.showSidebar()
|
2021-01-23 10:59:12 +01:00
|
|
|
self.sidebar.setFocus()
|
2017-08-13 11:11:40 +02:00
|
|
|
|
2021-01-26 23:35:40 +01:00
|
|
|
def focusSidebarSearchBar(self) -> None:
|
|
|
|
self.showSidebar()
|
|
|
|
self.sidebar.searchBar.setFocus()
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def toggle_sidebar(self) -> None:
|
2021-01-23 10:59:12 +01:00
|
|
|
want_visible = not self.sidebarDockWidget.isVisible()
|
|
|
|
self.sidebarDockWidget.setVisible(want_visible)
|
|
|
|
if want_visible:
|
|
|
|
self.sidebar.refresh()
|
2020-02-15 21:03:15 +01:00
|
|
|
|
2021-01-23 10:59:12 +01:00
|
|
|
# legacy
|
2021-02-09 01:50:39 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def setFilter(self, *terms: str) -> None:
|
2021-02-09 01:50:39 +01:00
|
|
|
self.sidebar.update_search(*terms)
|
2021-01-23 10:59:12 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Info
|
|
|
|
######################################################################
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def showCardInfo(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
if not self.card:
|
|
|
|
return
|
2020-06-15 06:14:18 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
info, cs = self._cardInfoData()
|
|
|
|
reps = self._revlogData(cs)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-02-12 22:12:45 +01:00
|
|
|
card_info_dialog = CardInfoDialog(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
l = QVBoxLayout()
|
2019-12-23 01:34:10 +01:00
|
|
|
l.setContentsMargins(0, 0, 0, 0)
|
2020-02-12 21:03:11 +01:00
|
|
|
w = AnkiWebView(title="browser card info")
|
2012-12-21 08:51:59 +01:00
|
|
|
l.addWidget(w)
|
2020-02-12 22:12:45 +01:00
|
|
|
w.stdHtml(info + "<p>" + reps, context=card_info_dialog)
|
2012-12-21 08:51:59 +01:00
|
|
|
bb = QDialogButtonBox(QDialogButtonBox.Close)
|
|
|
|
l.addWidget(bb)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(bb.rejected, card_info_dialog.reject)
|
2020-02-12 22:12:45 +01:00
|
|
|
card_info_dialog.setLayout(l)
|
|
|
|
card_info_dialog.setWindowModality(Qt.WindowModal)
|
|
|
|
card_info_dialog.resize(500, 400)
|
2020-02-18 01:59:24 +01:00
|
|
|
restoreGeom(card_info_dialog, "revlog")
|
2020-02-12 22:12:45 +01:00
|
|
|
card_info_dialog.show()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-06-15 06:14:18 +02:00
|
|
|
def _cardInfoData(self) -> Tuple[str, CardStats]:
|
2012-12-21 08:51:59 +01:00
|
|
|
cs = CardStats(self.col, self.card)
|
2020-06-15 06:14:18 +02:00
|
|
|
rep = cs.report(include_revlog=True)
|
2012-12-21 08:51:59 +01:00
|
|
|
return rep, cs
|
|
|
|
|
2020-06-15 06:14:18 +02:00
|
|
|
# legacy - revlog used to be generated here, and some add-ons
|
|
|
|
# wrapped this function
|
|
|
|
|
|
|
|
def _revlogData(self, cs: CardStats) -> str:
|
|
|
|
return ""
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Menu helpers
|
|
|
|
######################################################################
|
|
|
|
|
2021-03-23 10:36:52 +01:00
|
|
|
def selected_cards(self) -> List[CardID]:
|
2019-12-23 01:34:10 +01:00
|
|
|
return [
|
|
|
|
self.model.cards[idx.row()]
|
|
|
|
for idx in self.form.tableView.selectionModel().selectedRows()
|
|
|
|
]
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-25 13:50:31 +01:00
|
|
|
def selected_notes(self) -> List[NoteID]:
|
2019-12-23 01:34:10 +01:00
|
|
|
return self.col.db.list(
|
|
|
|
"""
|
2012-12-21 08:51:59 +01:00
|
|
|
select distinct nid from cards
|
2019-12-23 01:34:10 +01:00
|
|
|
where id in %s"""
|
|
|
|
% ids2str(
|
|
|
|
[
|
|
|
|
self.model.cards[idx.row()]
|
|
|
|
for idx in self.form.tableView.selectionModel().selectedRows()
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-23 10:36:52 +01:00
|
|
|
def selectedNotesAsCards(self) -> List[CardID]:
|
2012-12-21 08:51:59 +01:00
|
|
|
return self.col.db.list(
|
2019-12-23 01:34:10 +01:00
|
|
|
"select id from cards where nid in (%s)"
|
2021-03-18 02:46:11 +01:00
|
|
|
% ",".join([str(s) for s in self.selected_notes()])
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-25 13:50:31 +01:00
|
|
|
def oneModelNotes(self) -> List[NoteID]:
|
2021-03-18 02:46:11 +01:00
|
|
|
sf = self.selected_notes()
|
2012-12-21 08:51:59 +01:00
|
|
|
if not sf:
|
2021-02-01 00:39:55 +01:00
|
|
|
return []
|
2019-12-23 01:34:10 +01:00
|
|
|
mods = self.col.db.scalar(
|
|
|
|
"""
|
2012-12-21 08:51:59 +01:00
|
|
|
select count(distinct mid) from notes
|
2019-12-23 01:34:10 +01:00
|
|
|
where id in %s"""
|
|
|
|
% ids2str(sf)
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
if mods > 1:
|
2020-11-17 08:42:43 +01:00
|
|
|
showInfo(tr(TR.BROWSING_PLEASE_SELECT_CARDS_FROM_ONLY_ONE))
|
2021-02-01 00:39:55 +01:00
|
|
|
return []
|
2012-12-21 08:51:59 +01:00
|
|
|
return sf
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onHelp(self) -> None:
|
2021-01-25 14:45:47 +01:00
|
|
|
openHelp(HelpPage.BROWSING)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-18 02:46:11 +01:00
|
|
|
# legacy
|
|
|
|
|
|
|
|
selectedCards = selected_cards
|
|
|
|
selectedNotes = selected_notes
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Misc menu options
|
|
|
|
######################################################################
|
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
@ensure_editor_saved_on_trigger
|
2021-02-01 00:39:55 +01:00
|
|
|
def onChangeModel(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
nids = self.oneModelNotes()
|
|
|
|
if nids:
|
|
|
|
ChangeModel(self, nids)
|
|
|
|
|
2021-02-01 23:46:56 +01:00
|
|
|
def createFilteredDeck(self) -> None:
|
2021-02-01 08:50:19 +01:00
|
|
|
search = self.form.searchEdit.lineEdit().text()
|
2021-03-17 05:51:59 +01:00
|
|
|
if self.mw.col.schedVer() != 1 and KeyboardModifiersPressed().alt:
|
2021-03-24 04:17:12 +01:00
|
|
|
aqt.dialogs.open("FilteredDeckConfigDialog", self.mw, search_2=search)
|
2021-02-02 09:29:09 +01:00
|
|
|
else:
|
2021-03-24 04:17:12 +01:00
|
|
|
aqt.dialogs.open("FilteredDeckConfigDialog", self.mw, search=search)
|
2021-02-01 19:10:05 +01:00
|
|
|
|
2013-05-03 10:52:46 +02:00
|
|
|
# Preview
|
|
|
|
######################################################################
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onTogglePreview(self) -> None:
|
2020-03-29 21:10:30 +02:00
|
|
|
if self._previewer:
|
2020-04-02 09:35:51 +02:00
|
|
|
self._previewer.close()
|
2020-05-19 23:46:50 +02:00
|
|
|
self._on_preview_closed()
|
2013-05-03 10:52:46 +02:00
|
|
|
else:
|
2020-04-08 08:19:59 +02:00
|
|
|
self._previewer = PreviewDialog(self, self.mw, self._on_preview_closed)
|
2020-04-02 09:35:51 +02:00
|
|
|
self._previewer.open()
|
2017-08-08 08:28:53 +02:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def _renderPreview(self) -> None:
|
2020-03-29 21:10:30 +02:00
|
|
|
if self._previewer:
|
2021-01-10 02:01:24 +01:00
|
|
|
if self.singleCard:
|
|
|
|
self._previewer.render_card()
|
|
|
|
else:
|
|
|
|
self.onTogglePreview()
|
2017-08-08 08:28:53 +02:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def _cleanup_preview(self) -> None:
|
2020-03-29 21:10:30 +02:00
|
|
|
if self._previewer:
|
2020-04-02 17:34:53 +02:00
|
|
|
self._previewer.cancel_timer()
|
2020-04-10 10:42:28 +02:00
|
|
|
self._previewer.close()
|
2019-02-26 02:18:32 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def _on_preview_closed(self) -> None:
|
2021-01-10 01:21:52 +01:00
|
|
|
if self.editor.web:
|
|
|
|
self.editor.web.eval("$('#previewButton').removeClass('highlighted')")
|
2020-04-08 08:19:59 +02:00
|
|
|
self._previewer = None
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Card deletion
|
|
|
|
######################################################################
|
|
|
|
|
2021-03-12 08:53:14 +01:00
|
|
|
def delete_selected_notes(self) -> None:
|
|
|
|
# ensure deletion is not accidentally triggered when the user is focused
|
|
|
|
# in the editing screen or search bar
|
2018-03-01 04:31:52 +01:00
|
|
|
focus = self.focusWidget()
|
|
|
|
if focus != self.form.tableView:
|
|
|
|
return
|
2016-07-14 12:23:44 +02:00
|
|
|
|
2021-03-12 08:53:14 +01:00
|
|
|
# nothing selected?
|
2021-03-18 02:46:11 +01:00
|
|
|
nids = self.selected_notes()
|
2012-12-21 08:51:59 +01:00
|
|
|
if not nids:
|
|
|
|
return
|
2021-03-12 08:53:14 +01:00
|
|
|
|
experiment with preserving search when resetting
Up until now, we've been forcing a new search whenever reset is called.
The primary reason was that the card list display routines did not expect
a card or note to have been removed. By updating the model to show
"(deleted)" when a card or note is missing, we no longer have to repeat
the search.
This has a few advantages:
- Searches, especially complex ones, can be slow to execute. When we
perform them after every operation like a delete, it can make Anki feel
sluggish.
- The fact that notes have been deleted becomes more obvious - some users
found it easy to miss the "deleted" pop-up in the past.
This change does not just affect deletions, as many other operations
trigger a reset as well. In the past, when using 'set due date' in the
review screen for example, it caused an ugly flicker in the browser screen,
and could be slow when the current search couldn't be quickly redone.
The disadvantage of this approach is that the displayed content may
not reflect the specified search, which has the potential to be confusing.
But if that turns out to be a problem, it could be (partly) alleviated by
displaying a refresh button next to the search bar when the search may
need to be refreshed.
Feedback welcome!
2021-03-13 05:52:44 +01:00
|
|
|
# select the next card if there is one
|
|
|
|
self._onNextCard()
|
2021-03-12 08:53:14 +01:00
|
|
|
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
remove_notes(
|
|
|
|
mw=self.mw,
|
|
|
|
note_ids=nids,
|
more reset refactoring
'card modified' covers the common case where we need to rebuild the
study queue, but is also set when changing the card flags. We want to
avoid a queue rebuild in that case, as it causes UI flicker, and may
result in a different card being shown. Note marking doesn't trigger
a queue build, but still causes flicker, and may return the user back
to the front side when they were looking at the answer.
I still think entity-based change tracking is the simplest in the
common case, but to solve the above, I've introduced an enum describing
the last operation that was taken. This currently is not trying to list
out all possible operations, and just describes the ones we want to
special-case.
Other changes:
- Fire the old 'state_did_reset' hook after an operation is performed,
so legacy code can refresh itself after an operation is performed.
- Fire the new `operation_did_execute` hook when mw.reset() is called,
so that as the UI is updated to the use the new hook, it will still
be able to refresh after legacy code calls mw.reset()
- Update the deck browser, overview and review screens to listen to
the new hook, instead of relying on the main window to call moveToState()
- Add a 'set flag' backend action, so we can distinguish it from a
normal card update.
- Drop the separate added/modified entries in the change list in
favour of a single entry per entity.
- Add typing to mw.state
- Tweak perform_op()
- Convert a few more actions to use perform_op()
2021-03-14 10:54:15 +01:00
|
|
|
success=lambda _: tooltip(tr(TR.BROWSING_NOTE_DELETED, count=len(nids))),
|
|
|
|
)
|
2021-03-12 08:53:14 +01:00
|
|
|
|
|
|
|
# legacy
|
|
|
|
|
|
|
|
deleteNotes = delete_selected_notes
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Deck change
|
|
|
|
######################################################################
|
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
@ensure_editor_saved_on_trigger
|
2021-03-12 08:53:14 +01:00
|
|
|
def set_deck_of_selected_cards(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
from aqt.studydeck import StudyDeck
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2021-03-18 02:46:11 +01:00
|
|
|
cids = self.selected_cards()
|
2013-02-20 07:25:59 +01:00
|
|
|
if not cids:
|
|
|
|
return
|
2021-03-12 08:53:14 +01:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
did = self.mw.col.db.scalar("select did from cards where id = ?", cids[0])
|
|
|
|
current = self.mw.col.decks.get(did)["name"]
|
2012-12-21 08:51:59 +01:00
|
|
|
ret = StudyDeck(
|
2019-12-23 01:34:10 +01:00
|
|
|
self.mw,
|
|
|
|
current=current,
|
2020-11-17 08:42:43 +01:00
|
|
|
accept=tr(TR.BROWSING_MOVE_CARDS),
|
|
|
|
title=tr(TR.BROWSING_CHANGE_DECK),
|
2021-01-25 14:45:47 +01:00
|
|
|
help=HelpPage.BROWSING,
|
2019-12-23 01:34:10 +01:00
|
|
|
parent=self,
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
if not ret.name:
|
|
|
|
return
|
|
|
|
did = self.col.decks.id(ret.name)
|
2021-03-12 07:27:40 +01:00
|
|
|
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
set_card_deck(mw=self.mw, card_ids=cids, deck_id=did)
|
2021-03-12 08:53:14 +01:00
|
|
|
|
|
|
|
# legacy
|
|
|
|
|
|
|
|
setDeck = set_deck_of_selected_cards
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Tags
|
|
|
|
######################################################################
|
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
@ensure_editor_saved_on_trigger
|
2021-03-05 11:47:51 +01:00
|
|
|
def add_tags_to_selected_notes(
|
2021-02-01 00:39:55 +01:00
|
|
|
self,
|
|
|
|
tags: Optional[str] = None,
|
|
|
|
) -> None:
|
2021-03-05 11:47:51 +01:00
|
|
|
"Shows prompt if tags not provided."
|
2021-03-16 13:40:37 +01:00
|
|
|
if not (
|
clear_unused_tags and browser redraw improvements
- clear_unused_tags() is now undoable, and returns the number of removed
notes
- add a new mw.query_op() helper for immutable queries
- decouple "freeze/unfreeze ui state" hooks from the "interface update
required" hook, so that the former is fired even on error, and can be
made re-entrant
- use a 'block_updates' flag in Python, instead of setUpdatesEnabled(),
as the latter has the side-effect of preventing child windows like
tooltips from appearing, and forces a full redrawn when updates are
enabled again. The new behaviour leads to the card list blanking out
when a long-running op is running, but in the future if we cache the
cell values we can just display them from the cache instead.
- we were indiscriminately saving the note with saveNow(), due to the
call to saveTags(). Changed so that it only saves when the tags field
is focused.
- drain the "on_done" queue on main before launching a new background
task, to lower the chances of something in on_done making a small query
to the DB and hanging until a long op finishes
- the duplicate check in the editor was executed after the webview loads,
leading to it hanging until the sidebar finishes loading. Run it at
set_note() time instead, so that the editor loads first.
- don't throw an error when a long-running op started with with_progress()
finishes after the window it was launched from has closed
- don't throw an error when the browser is closed before the sidebar
has finished loading
2021-03-17 12:27:42 +01:00
|
|
|
tags := tags or self._prompt_for_tags(tr(TR.BROWSING_ENTER_TAGS_TO_ADD))
|
2021-03-16 13:40:37 +01:00
|
|
|
):
|
|
|
|
return
|
2021-03-19 04:37:42 +01:00
|
|
|
add_tags(
|
|
|
|
mw=self.mw,
|
|
|
|
note_ids=self.selected_notes(),
|
|
|
|
space_separated_tags=tags,
|
|
|
|
success=lambda out: tooltip(
|
|
|
|
tr(TR.BROWSING_NOTES_UPDATED, count=out.count), parent=self
|
|
|
|
),
|
|
|
|
)
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
@ensure_editor_saved_on_trigger
|
2021-03-05 11:47:51 +01:00
|
|
|
def remove_tags_from_selected_notes(self, tags: Optional[str] = None) -> None:
|
|
|
|
"Shows prompt if tags not provided."
|
2021-03-16 13:40:37 +01:00
|
|
|
if not (
|
clear_unused_tags and browser redraw improvements
- clear_unused_tags() is now undoable, and returns the number of removed
notes
- add a new mw.query_op() helper for immutable queries
- decouple "freeze/unfreeze ui state" hooks from the "interface update
required" hook, so that the former is fired even on error, and can be
made re-entrant
- use a 'block_updates' flag in Python, instead of setUpdatesEnabled(),
as the latter has the side-effect of preventing child windows like
tooltips from appearing, and forces a full redrawn when updates are
enabled again. The new behaviour leads to the card list blanking out
when a long-running op is running, but in the future if we cache the
cell values we can just display them from the cache instead.
- we were indiscriminately saving the note with saveNow(), due to the
call to saveTags(). Changed so that it only saves when the tags field
is focused.
- drain the "on_done" queue on main before launching a new background
task, to lower the chances of something in on_done making a small query
to the DB and hanging until a long op finishes
- the duplicate check in the editor was executed after the webview loads,
leading to it hanging until the sidebar finishes loading. Run it at
set_note() time instead, so that the editor loads first.
- don't throw an error when a long-running op started with with_progress()
finishes after the window it was launched from has closed
- don't throw an error when the browser is closed before the sidebar
has finished loading
2021-03-17 12:27:42 +01:00
|
|
|
tags := tags or self._prompt_for_tags(tr(TR.BROWSING_ENTER_TAGS_TO_DELETE))
|
2021-03-16 13:40:37 +01:00
|
|
|
):
|
|
|
|
return
|
2021-03-18 12:35:32 +01:00
|
|
|
remove_tags_for_notes(
|
2021-03-19 04:37:42 +01:00
|
|
|
mw=self.mw,
|
|
|
|
note_ids=self.selected_notes(),
|
|
|
|
space_separated_tags=tags,
|
|
|
|
success=lambda out: tooltip(
|
|
|
|
tr(TR.BROWSING_NOTES_UPDATED, count=out.count), parent=self
|
|
|
|
),
|
2021-03-16 13:40:37 +01:00
|
|
|
)
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
|
clear_unused_tags and browser redraw improvements
- clear_unused_tags() is now undoable, and returns the number of removed
notes
- add a new mw.query_op() helper for immutable queries
- decouple "freeze/unfreeze ui state" hooks from the "interface update
required" hook, so that the former is fired even on error, and can be
made re-entrant
- use a 'block_updates' flag in Python, instead of setUpdatesEnabled(),
as the latter has the side-effect of preventing child windows like
tooltips from appearing, and forces a full redrawn when updates are
enabled again. The new behaviour leads to the card list blanking out
when a long-running op is running, but in the future if we cache the
cell values we can just display them from the cache instead.
- we were indiscriminately saving the note with saveNow(), due to the
call to saveTags(). Changed so that it only saves when the tags field
is focused.
- drain the "on_done" queue on main before launching a new background
task, to lower the chances of something in on_done making a small query
to the DB and hanging until a long op finishes
- the duplicate check in the editor was executed after the webview loads,
leading to it hanging until the sidebar finishes loading. Run it at
set_note() time instead, so that the editor loads first.
- don't throw an error when a long-running op started with with_progress()
finishes after the window it was launched from has closed
- don't throw an error when the browser is closed before the sidebar
has finished loading
2021-03-17 12:27:42 +01:00
|
|
|
def _prompt_for_tags(self, prompt: str) -> Optional[str]:
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
(tags, ok) = getTag(self, self.col, prompt)
|
|
|
|
if not ok:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return tags
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
@ensure_editor_saved_on_trigger
|
clear_unused_tags and browser redraw improvements
- clear_unused_tags() is now undoable, and returns the number of removed
notes
- add a new mw.query_op() helper for immutable queries
- decouple "freeze/unfreeze ui state" hooks from the "interface update
required" hook, so that the former is fired even on error, and can be
made re-entrant
- use a 'block_updates' flag in Python, instead of setUpdatesEnabled(),
as the latter has the side-effect of preventing child windows like
tooltips from appearing, and forces a full redrawn when updates are
enabled again. The new behaviour leads to the card list blanking out
when a long-running op is running, but in the future if we cache the
cell values we can just display them from the cache instead.
- we were indiscriminately saving the note with saveNow(), due to the
call to saveTags(). Changed so that it only saves when the tags field
is focused.
- drain the "on_done" queue on main before launching a new background
task, to lower the chances of something in on_done making a small query
to the DB and hanging until a long op finishes
- the duplicate check in the editor was executed after the webview loads,
leading to it hanging until the sidebar finishes loading. Run it at
set_note() time instead, so that the editor loads first.
- don't throw an error when a long-running op started with with_progress()
finishes after the window it was launched from has closed
- don't throw an error when the browser is closed before the sidebar
has finished loading
2021-03-17 12:27:42 +01:00
|
|
|
def clear_unused_tags(self) -> None:
|
|
|
|
clear_unused_tags(mw=self.mw, parent=self)
|
2017-08-11 06:40:51 +02:00
|
|
|
|
2021-03-05 11:47:51 +01:00
|
|
|
addTags = add_tags_to_selected_notes
|
|
|
|
deleteTags = remove_tags_from_selected_notes
|
clear_unused_tags and browser redraw improvements
- clear_unused_tags() is now undoable, and returns the number of removed
notes
- add a new mw.query_op() helper for immutable queries
- decouple "freeze/unfreeze ui state" hooks from the "interface update
required" hook, so that the former is fired even on error, and can be
made re-entrant
- use a 'block_updates' flag in Python, instead of setUpdatesEnabled(),
as the latter has the side-effect of preventing child windows like
tooltips from appearing, and forces a full redrawn when updates are
enabled again. The new behaviour leads to the card list blanking out
when a long-running op is running, but in the future if we cache the
cell values we can just display them from the cache instead.
- we were indiscriminately saving the note with saveNow(), due to the
call to saveTags(). Changed so that it only saves when the tags field
is focused.
- drain the "on_done" queue on main before launching a new background
task, to lower the chances of something in on_done making a small query
to the DB and hanging until a long op finishes
- the duplicate check in the editor was executed after the webview loads,
leading to it hanging until the sidebar finishes loading. Run it at
set_note() time instead, so that the editor loads first.
- don't throw an error when a long-running op started with with_progress()
finishes after the window it was launched from has closed
- don't throw an error when the browser is closed before the sidebar
has finished loading
2021-03-17 12:27:42 +01:00
|
|
|
clearUnusedTags = clear_unused_tags
|
2021-03-05 11:47:51 +01:00
|
|
|
|
2017-08-12 08:08:10 +02:00
|
|
|
# Suspending
|
2012-12-21 08:51:59 +01:00
|
|
|
######################################################################
|
|
|
|
|
2021-03-04 13:14:35 +01:00
|
|
|
def current_card_is_suspended(self) -> bool:
|
2020-03-23 18:16:39 +01:00
|
|
|
return bool(self.card and self.card.queue == QUEUE_TYPE_SUSPENDED)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
@ensure_editor_saved_on_trigger
|
2021-03-04 13:14:35 +01:00
|
|
|
def suspend_selected_cards(self) -> None:
|
|
|
|
want_suspend = not self.current_card_is_suspended()
|
2021-03-18 02:46:11 +01:00
|
|
|
cids = self.selected_cards()
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
|
|
|
|
if want_suspend:
|
|
|
|
suspend_cards(mw=self.mw, card_ids=cids)
|
|
|
|
else:
|
|
|
|
unsuspend_cards(mw=self.mw, card_ids=cids)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-10 04:15:10 +01:00
|
|
|
# Exporting
|
|
|
|
######################################################################
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def _on_export_notes(self) -> None:
|
2020-02-10 04:15:10 +01:00
|
|
|
cids = self.selectedNotesAsCards()
|
|
|
|
if cids:
|
|
|
|
ExportDialog(self.mw, cids=cids)
|
|
|
|
|
2017-08-16 12:30:29 +02:00
|
|
|
# Flags & Marking
|
2017-08-12 08:08:10 +02:00
|
|
|
######################################################################
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-18 03:06:45 +01:00
|
|
|
@ensure_editor_saved
|
|
|
|
def set_flag_of_selected_cards(self, flag: int) -> None:
|
2019-12-06 05:23:54 +01:00
|
|
|
if not self.card:
|
|
|
|
return
|
2020-09-27 04:31:25 +02:00
|
|
|
|
2018-11-12 02:11:53 +01:00
|
|
|
# flag needs toggling off?
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
if flag == self.card.user_flag():
|
|
|
|
flag = 0
|
more reset refactoring
'card modified' covers the common case where we need to rebuild the
study queue, but is also set when changing the card flags. We want to
avoid a queue rebuild in that case, as it causes UI flicker, and may
result in a different card being shown. Note marking doesn't trigger
a queue build, but still causes flicker, and may return the user back
to the front side when they were looking at the answer.
I still think entity-based change tracking is the simplest in the
common case, but to solve the above, I've introduced an enum describing
the last operation that was taken. This currently is not trying to list
out all possible operations, and just describes the ones we want to
special-case.
Other changes:
- Fire the old 'state_did_reset' hook after an operation is performed,
so legacy code can refresh itself after an operation is performed.
- Fire the new `operation_did_execute` hook when mw.reset() is called,
so that as the UI is updated to the use the new hook, it will still
be able to refresh after legacy code calls mw.reset()
- Update the deck browser, overview and review screens to listen to
the new hook, instead of relying on the main window to call moveToState()
- Add a 'set flag' backend action, so we can distinguish it from a
normal card update.
- Drop the separate added/modified entries in the change list in
favour of a single entry per entity.
- Add typing to mw.state
- Tweak perform_op()
- Convert a few more actions to use perform_op()
2021-03-14 10:54:15 +01:00
|
|
|
|
2021-03-18 03:06:45 +01:00
|
|
|
set_card_flag(mw=self.mw, card_ids=self.selected_cards(), flag=flag)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-18 03:06:45 +01:00
|
|
|
def _update_flags_menu(self) -> None:
|
2021-03-06 15:17:17 +01:00
|
|
|
flag = self.card and self.card.user_flag()
|
2018-11-12 03:10:50 +01:00
|
|
|
flag = flag or 0
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
flagActions = [
|
2021-03-18 03:06:45 +01:00
|
|
|
self.form.actionRed_Flag,
|
|
|
|
self.form.actionOrange_Flag,
|
|
|
|
self.form.actionGreen_Flag,
|
|
|
|
self.form.actionBlue_Flag,
|
2019-12-23 01:34:10 +01:00
|
|
|
]
|
2018-11-12 03:10:50 +01:00
|
|
|
|
|
|
|
for c, act in enumerate(flagActions):
|
2019-12-23 01:34:10 +01:00
|
|
|
act.setChecked(flag == c + 1)
|
2019-02-05 05:37:07 +01:00
|
|
|
|
|
|
|
qtMenuShortcutWorkaround(self.form.menuFlag)
|
2018-11-12 03:10:50 +01:00
|
|
|
|
2021-03-18 03:06:45 +01:00
|
|
|
def toggle_mark_of_selected_notes(self) -> None:
|
|
|
|
have_mark = bool(self.card and self.card.note().has_tag(MARKED_TAG))
|
|
|
|
if have_mark:
|
|
|
|
self.remove_tags_from_selected_notes(tags=MARKED_TAG)
|
2017-08-16 12:30:29 +02:00
|
|
|
else:
|
2021-03-18 03:06:45 +01:00
|
|
|
self.add_tags_to_selected_notes(tags=MARKED_TAG)
|
2017-08-16 12:30:29 +02:00
|
|
|
|
2021-03-18 02:46:11 +01:00
|
|
|
# Scheduling
|
2012-12-21 08:51:59 +01:00
|
|
|
######################################################################
|
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
@ensure_editor_saved_on_trigger
|
2021-02-01 00:39:55 +01:00
|
|
|
def reposition(self) -> None:
|
2021-03-18 02:46:11 +01:00
|
|
|
if self.card and self.card.queue != QUEUE_TYPE_NEW:
|
|
|
|
showInfo(tr(TR.BROWSING_ONLY_NEW_CARDS_CAN_BE_REPOSITIONED), parent=self)
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
|
|
|
|
2021-03-18 02:46:11 +01:00
|
|
|
reposition_new_cards_dialog(
|
|
|
|
mw=self.mw, parent=self, card_ids=self.selected_cards()
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
@ensure_editor_saved_on_trigger
|
Rework reschedule tool
The old rescheduling dialog's two options have been split into two
separate menu items, "Forget", and "Set Due Date"
For cards that are not review cards, "Set Due Date" behaves like the
old reschedule option, changing the cards into a review card, and
and setting both the interval and due date to the provided number of
days.
When "Set Due Date" is applied to a review card, it no longer resets
the card's interval. Instead, it looks at how much the provided number
of days will change the original interval, and adjusts the interval by
that amount, so that cards that are answered earlier receive a smaller
next interval, and cards that are answered after a longer delay receive
a bonus.
For example, imagine a card was answered on day 5, and given an interval
of 10 days, so it has a due date of day 15.
- if on day 10 the due date is changed to day 12 (today+2), the card
is being scheduled 3 days earlier than it was supposed to be, so the
interval will be adjusted to 7 days.
- and if on day 10 the due date is changed to day 20, the interval will
be changed from 10 days to 15 days.
There is no separate option to reset the interval of a review card, but
it can be accomplished by forgetting the card(s), and then setting the
desired due date.
Other notes:
- Added the action to the review screen as well.
- Set the shortcut to Ctrl+Shift+D, and changed the existing Delete
Tags shortcut to Ctrl+Alt+Shift+A.
2021-02-07 11:58:16 +01:00
|
|
|
def set_due_date(self) -> None:
|
2021-03-16 13:40:37 +01:00
|
|
|
set_due_date_dialog(
|
|
|
|
mw=self.mw,
|
|
|
|
parent=self,
|
2021-03-18 02:46:11 +01:00
|
|
|
card_ids=self.selected_cards(),
|
2021-03-16 13:40:37 +01:00
|
|
|
config_key=Config.String.SET_DUE_BROWSER,
|
Rework reschedule tool
The old rescheduling dialog's two options have been split into two
separate menu items, "Forget", and "Set Due Date"
For cards that are not review cards, "Set Due Date" behaves like the
old reschedule option, changing the cards into a review card, and
and setting both the interval and due date to the provided number of
days.
When "Set Due Date" is applied to a review card, it no longer resets
the card's interval. Instead, it looks at how much the provided number
of days will change the original interval, and adjusts the interval by
that amount, so that cards that are answered earlier receive a smaller
next interval, and cards that are answered after a longer delay receive
a bonus.
For example, imagine a card was answered on day 5, and given an interval
of 10 days, so it has a due date of day 15.
- if on day 10 the due date is changed to day 12 (today+2), the card
is being scheduled 3 days earlier than it was supposed to be, so the
interval will be adjusted to 7 days.
- and if on day 10 the due date is changed to day 20, the interval will
be changed from 10 days to 15 days.
There is no separate option to reset the interval of a review card, but
it can be accomplished by forgetting the card(s), and then setting the
desired due date.
Other notes:
- Added the action to the review screen as well.
- Set the shortcut to Ctrl+Shift+D, and changed the existing Delete
Tags shortcut to Ctrl+Alt+Shift+A.
2021-02-07 11:58:16 +01:00
|
|
|
)
|
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
@ensure_editor_saved_on_trigger
|
Rework reschedule tool
The old rescheduling dialog's two options have been split into two
separate menu items, "Forget", and "Set Due Date"
For cards that are not review cards, "Set Due Date" behaves like the
old reschedule option, changing the cards into a review card, and
and setting both the interval and due date to the provided number of
days.
When "Set Due Date" is applied to a review card, it no longer resets
the card's interval. Instead, it looks at how much the provided number
of days will change the original interval, and adjusts the interval by
that amount, so that cards that are answered earlier receive a smaller
next interval, and cards that are answered after a longer delay receive
a bonus.
For example, imagine a card was answered on day 5, and given an interval
of 10 days, so it has a due date of day 15.
- if on day 10 the due date is changed to day 12 (today+2), the card
is being scheduled 3 days earlier than it was supposed to be, so the
interval will be adjusted to 7 days.
- and if on day 10 the due date is changed to day 20, the interval will
be changed from 10 days to 15 days.
There is no separate option to reset the interval of a review card, but
it can be accomplished by forgetting the card(s), and then setting the
desired due date.
Other notes:
- Added the action to the review screen as well.
- Set the shortcut to Ctrl+Shift+D, and changed the existing Delete
Tags shortcut to Ctrl+Alt+Shift+A.
2021-02-07 11:58:16 +01:00
|
|
|
def forget_cards(self) -> None:
|
2021-03-16 13:40:37 +01:00
|
|
|
forget_cards(
|
|
|
|
mw=self.mw,
|
|
|
|
parent=self,
|
2021-03-18 02:46:11 +01:00
|
|
|
card_ids=self.selected_cards(),
|
Rework reschedule tool
The old rescheduling dialog's two options have been split into two
separate menu items, "Forget", and "Set Due Date"
For cards that are not review cards, "Set Due Date" behaves like the
old reschedule option, changing the cards into a review card, and
and setting both the interval and due date to the provided number of
days.
When "Set Due Date" is applied to a review card, it no longer resets
the card's interval. Instead, it looks at how much the provided number
of days will change the original interval, and adjusts the interval by
that amount, so that cards that are answered earlier receive a smaller
next interval, and cards that are answered after a longer delay receive
a bonus.
For example, imagine a card was answered on day 5, and given an interval
of 10 days, so it has a due date of day 15.
- if on day 10 the due date is changed to day 12 (today+2), the card
is being scheduled 3 days earlier than it was supposed to be, so the
interval will be adjusted to 7 days.
- and if on day 10 the due date is changed to day 20, the interval will
be changed from 10 days to 15 days.
There is no separate option to reset the interval of a review card, but
it can be accomplished by forgetting the card(s), and then setting the
desired due date.
Other notes:
- Added the action to the review screen as well.
- Set the shortcut to Ctrl+Shift+D, and changed the existing Delete
Tags shortcut to Ctrl+Alt+Shift+A.
2021-02-07 11:58:16 +01:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Edit: selection
|
|
|
|
######################################################################
|
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
@ensure_editor_saved_on_trigger
|
2021-02-01 00:39:55 +01:00
|
|
|
def selectNotes(self) -> None:
|
2021-03-18 02:46:11 +01:00
|
|
|
nids = self.selected_notes()
|
2012-12-21 08:51:59 +01:00
|
|
|
# clear the selection so we don't waste energy preserving it
|
|
|
|
tv = self.form.tableView
|
|
|
|
tv.selectionModel().clear()
|
2020-10-19 20:51:36 +02:00
|
|
|
|
2021-01-30 02:23:32 +01:00
|
|
|
search = self.col.build_search_string(
|
2021-02-11 10:57:19 +01:00
|
|
|
SearchNode(nids=SearchNode.IdList(ids=nids))
|
2021-01-30 02:23:32 +01:00
|
|
|
)
|
2020-10-19 20:51:36 +02:00
|
|
|
self.search_for(search)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
tv.selectAll()
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def invertSelection(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
sm = self.form.tableView.selectionModel()
|
|
|
|
items = sm.selection()
|
|
|
|
self.form.tableView.selectAll()
|
|
|
|
sm.select(items, QItemSelectionModel.Deselect | QItemSelectionModel.Rows)
|
|
|
|
|
2020-01-15 22:53:12 +01:00
|
|
|
# Hooks
|
2012-12-21 08:51:59 +01:00
|
|
|
######################################################################
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def setupHooks(self) -> None:
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.undo_state_did_change.append(self.onUndoState)
|
rework filtered deck screen & search errors
- Filtered deck creation now happens as an atomic operation, and is
undoable.
- The logic for initial search text, normalizing searches and so on
has been pushed into the backend.
- Use protobuf to pass the filtered deck to the updated dialog, so
we don't need to deal with untyped JSON.
- Change the "revise your search?" prompt to be a simple info box -
user has access to cancel and build buttons, and doesn't need a separate
prompt. Tweak the wording so the 'show excluded' button should be more
obvious.
- Filtered decks have a time appended to them instead of a number,
primarily because it's easier to implement. No objections going back to
the old behaviour if someone wants to contribute a clean patch.
The standard de-duplication will happen if two decks are created in the
same minute with the same name.
- Tweak the default sort order, and start with two searches. The UI
will still hide the second search by default, but by starting with two,
the frontend doesn't need logic for creating the starting text.
- Search errors now have their own error type, instead of using
InvalidInput, as that was intended mainly for bad API calls. The markdown
conversion is done when the error is converted from the backend, allowing
errors to printed as a string without any special handling by the calling
code.
TODO: when building a new filtered deck, update_active() is clobbering
the undo log when the overview is refreshed
2021-03-24 12:52:48 +01:00
|
|
|
# fixme: remove this once all items are using `operation_did_execute`
|
2020-05-22 02:47:14 +02:00
|
|
|
gui_hooks.sidebar_should_refresh_notetypes.append(self.on_item_added)
|
2021-03-18 01:54:02 +01:00
|
|
|
gui_hooks.backend_will_block.append(self.on_backend_will_block)
|
|
|
|
gui_hooks.backend_did_block.append(self.on_backend_did_block)
|
2021-03-13 14:59:32 +01:00
|
|
|
gui_hooks.operation_did_execute.append(self.on_operation_did_execute)
|
2021-03-14 13:08:37 +01:00
|
|
|
gui_hooks.focus_did_change.append(self.on_focus_change)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def teardownHooks(self) -> None:
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.undo_state_did_change.remove(self.onUndoState)
|
2020-05-22 02:47:14 +02:00
|
|
|
gui_hooks.sidebar_should_refresh_notetypes.remove(self.on_item_added)
|
2021-03-18 01:54:02 +01:00
|
|
|
gui_hooks.backend_will_block.remove(self.on_backend_will_block)
|
|
|
|
gui_hooks.backend_did_block.remove(self.on_backend_will_block)
|
2021-03-13 14:59:32 +01:00
|
|
|
gui_hooks.operation_did_execute.remove(self.on_operation_did_execute)
|
2021-03-14 13:08:37 +01:00
|
|
|
gui_hooks.focus_did_change.remove(self.on_focus_change)
|
2020-01-15 22:53:12 +01:00
|
|
|
|
2020-05-22 02:47:14 +02:00
|
|
|
def on_item_added(self, item: Any = None) -> None:
|
2021-01-23 10:59:12 +01:00
|
|
|
self.sidebar.refresh()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-12 08:56:13 +01:00
|
|
|
# Undo
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def undo(self) -> None:
|
|
|
|
# need to make sure we don't hang the UI by redrawing the card list
|
|
|
|
# during the long-running op. mw.undo will take care of the progress
|
|
|
|
# dialog
|
|
|
|
self.setUpdatesEnabled(False)
|
|
|
|
self.mw.undo(lambda _: self.setUpdatesEnabled(True))
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onUndoState(self, on: bool) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.form.actionUndo.setEnabled(on)
|
|
|
|
if on:
|
|
|
|
self.form.actionUndo.setText(self.mw.form.actionUndo.text())
|
|
|
|
|
|
|
|
# Edit: replacing
|
|
|
|
######################################################################
|
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
@ensure_editor_saved_on_trigger
|
2021-02-01 00:39:55 +01:00
|
|
|
def onFindReplace(self) -> None:
|
2021-03-18 02:46:11 +01:00
|
|
|
nids = self.selected_notes()
|
2020-05-05 14:14:12 +02:00
|
|
|
if not nids:
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2020-05-05 14:14:12 +02:00
|
|
|
|
2021-03-19 07:55:10 +01:00
|
|
|
FindAndReplaceDialog(self, mw=self.mw, note_ids=nids)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Edit: finding dupes
|
|
|
|
######################################################################
|
|
|
|
|
2021-03-16 13:40:37 +01:00
|
|
|
@ensure_editor_saved
|
2021-02-01 00:39:55 +01:00
|
|
|
def onFindDupes(self) -> None:
|
2021-03-19 07:55:10 +01:00
|
|
|
import anki.find
|
|
|
|
|
2020-02-11 23:28:21 +01:00
|
|
|
d = QDialog(self)
|
2021-03-05 04:07:52 +01:00
|
|
|
self.mw.garbage_collect_on_dialog_finish(d)
|
2012-12-21 08:51:59 +01:00
|
|
|
frm = aqt.forms.finddupes.Ui_Dialog()
|
|
|
|
frm.setupUi(d)
|
|
|
|
restoreGeom(d, "findDupes")
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(d)
|
2020-06-05 03:39:53 +02:00
|
|
|
searchHistory = restore_combo_history(frm.search, "findDupesFind")
|
2020-05-31 22:10:09 +02:00
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
fields = sorted(
|
|
|
|
anki.find.fieldNames(self.col, downcase=False), key=lambda x: x.lower()
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
frm.fields.addItems(fields)
|
2020-06-08 20:54:20 +02:00
|
|
|
restore_combo_index_for_session(frm.fields, fields, "findDupesFields")
|
2013-10-03 17:07:11 +02:00
|
|
|
self._dupesButton = None
|
2020-05-31 23:37:10 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# links
|
2020-02-12 21:03:11 +01:00
|
|
|
frm.webView.title = "find duplicates"
|
2020-02-12 22:00:13 +01:00
|
|
|
web_context = FindDupesDialog(dialog=d, browser=self)
|
|
|
|
frm.webView.set_bridge_command(self.dupeLinkClicked, web_context)
|
|
|
|
frm.webView.stdHtml("", context=web_context)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onFin(code: Any) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
saveGeom(d, "findDupes")
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(d.finished, onFin)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onClick() -> None:
|
2020-06-05 03:39:53 +02:00
|
|
|
search_text = save_combo_history(frm.search, searchHistory, "findDupesFind")
|
2020-06-08 20:54:20 +02:00
|
|
|
save_combo_index_for_session(frm.fields, "findDupesFields")
|
2012-12-21 08:51:59 +01:00
|
|
|
field = fields[frm.fields.currentIndex()]
|
2020-05-31 22:10:09 +02:00
|
|
|
self.duplicatesReport(frm.webView, field, search_text, frm, web_context)
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-11-17 08:42:43 +01:00
|
|
|
search = frm.buttonBox.addButton(
|
|
|
|
tr(TR.ACTIONS_SEARCH), QDialogButtonBox.ActionRole
|
|
|
|
)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(search.clicked, onClick)
|
2012-12-21 08:51:59 +01:00
|
|
|
d.show()
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def duplicatesReport(
|
|
|
|
self,
|
|
|
|
web: AnkiWebView,
|
|
|
|
fname: str,
|
|
|
|
search: str,
|
|
|
|
frm: aqt.forms.finddupes.Ui_Dialog,
|
|
|
|
web_context: FindDupesDialog,
|
|
|
|
) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw.progress.start()
|
2021-01-28 19:49:16 +01:00
|
|
|
try:
|
|
|
|
res = self.mw.col.findDupes(fname, search)
|
rework filtered deck screen & search errors
- Filtered deck creation now happens as an atomic operation, and is
undoable.
- The logic for initial search text, normalizing searches and so on
has been pushed into the backend.
- Use protobuf to pass the filtered deck to the updated dialog, so
we don't need to deal with untyped JSON.
- Change the "revise your search?" prompt to be a simple info box -
user has access to cancel and build buttons, and doesn't need a separate
prompt. Tweak the wording so the 'show excluded' button should be more
obvious.
- Filtered decks have a time appended to them instead of a number,
primarily because it's easier to implement. No objections going back to
the old behaviour if someone wants to contribute a clean patch.
The standard de-duplication will happen if two decks are created in the
same minute with the same name.
- Tweak the default sort order, and start with two searches. The UI
will still hide the second search by default, but by starting with two,
the frontend doesn't need logic for creating the starting text.
- Search errors now have their own error type, instead of using
InvalidInput, as that was intended mainly for bad API calls. The markdown
conversion is done when the error is converted from the backend, allowing
errors to printed as a string without any special handling by the calling
code.
TODO: when building a new filtered deck, update_active() is clobbering
the undo log when the overview is refreshed
2021-03-24 12:52:48 +01:00
|
|
|
except Exception as e:
|
2021-01-28 19:49:16 +01:00
|
|
|
self.mw.progress.finish()
|
rework filtered deck screen & search errors
- Filtered deck creation now happens as an atomic operation, and is
undoable.
- The logic for initial search text, normalizing searches and so on
has been pushed into the backend.
- Use protobuf to pass the filtered deck to the updated dialog, so
we don't need to deal with untyped JSON.
- Change the "revise your search?" prompt to be a simple info box -
user has access to cancel and build buttons, and doesn't need a separate
prompt. Tweak the wording so the 'show excluded' button should be more
obvious.
- Filtered decks have a time appended to them instead of a number,
primarily because it's easier to implement. No objections going back to
the old behaviour if someone wants to contribute a clean patch.
The standard de-duplication will happen if two decks are created in the
same minute with the same name.
- Tweak the default sort order, and start with two searches. The UI
will still hide the second search by default, but by starting with two,
the frontend doesn't need logic for creating the starting text.
- Search errors now have their own error type, instead of using
InvalidInput, as that was intended mainly for bad API calls. The markdown
conversion is done when the error is converted from the backend, allowing
errors to printed as a string without any special handling by the calling
code.
TODO: when building a new filtered deck, update_active() is clobbering
the undo log when the overview is refreshed
2021-03-24 12:52:48 +01:00
|
|
|
showWarning(str(e))
|
2021-01-28 19:49:16 +01:00
|
|
|
return
|
2013-10-03 17:07:11 +02:00
|
|
|
if not self._dupesButton:
|
|
|
|
self._dupesButton = b = frm.buttonBox.addButton(
|
2020-11-17 08:42:43 +01:00
|
|
|
tr(TR.BROWSING_TAG_DUPLICATES), QDialogButtonBox.ActionRole
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(b.clicked, lambda: self._onTagDupes(res))
|
2020-02-11 23:28:33 +01:00
|
|
|
t = ""
|
2012-12-21 08:51:59 +01:00
|
|
|
groups = len(res)
|
|
|
|
notes = sum(len(r[1]) for r in res)
|
2020-11-18 00:22:27 +01:00
|
|
|
part1 = tr(TR.BROWSING_GROUP, count=groups)
|
|
|
|
part2 = tr(TR.BROWSING_NOTE_COUNT, count=notes)
|
2020-11-22 05:57:53 +01:00
|
|
|
t += tr(TR.BROWSING_FOUND_AS_ACROSS_BS, part=part1, whole=part2)
|
2012-12-21 08:51:59 +01:00
|
|
|
t += "<p><ol>"
|
|
|
|
for val, nids in res:
|
2019-12-23 01:34:10 +01:00
|
|
|
t += (
|
|
|
|
"""<li><a href=# onclick="pycmd('%s');return false;">%s</a>: %s</a>"""
|
|
|
|
% (
|
2021-01-30 02:23:32 +01:00
|
|
|
html.escape(
|
|
|
|
self.col.build_search_string(
|
2021-02-11 10:57:19 +01:00
|
|
|
SearchNode(nids=SearchNode.IdList(ids=nids))
|
2021-01-30 02:23:32 +01:00
|
|
|
)
|
|
|
|
),
|
2020-11-18 00:22:27 +01:00
|
|
|
tr(TR.BROWSING_NOTE_COUNT, count=len(nids)),
|
2019-12-23 01:34:10 +01:00
|
|
|
html.escape(val),
|
|
|
|
)
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
t += "</ol>"
|
2020-02-12 22:00:13 +01:00
|
|
|
web.stdHtml(t, context=web_context)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw.progress.finish()
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def _onTagDupes(self, res: List[Any]) -> None:
|
2013-10-03 17:07:11 +02:00
|
|
|
if not res:
|
|
|
|
return
|
|
|
|
self.model.beginReset()
|
2020-11-17 08:42:43 +01:00
|
|
|
self.mw.checkpoint(tr(TR.BROWSING_TAG_DUPLICATES))
|
2013-10-03 17:07:11 +02:00
|
|
|
nids = set()
|
2021-01-08 20:53:27 +01:00
|
|
|
for _, nidlist in res:
|
2013-10-03 17:07:11 +02:00
|
|
|
nids.update(nidlist)
|
2021-03-05 11:47:51 +01:00
|
|
|
self.col.tags.bulk_add(list(nids), tr(TR.BROWSING_DUPLICATE))
|
2013-10-03 17:07:11 +02:00
|
|
|
self.mw.progress.finish()
|
|
|
|
self.model.endReset()
|
2020-08-16 18:49:51 +02:00
|
|
|
self.mw.requireReset(reason=ResetReason.BrowserTagDupes, context=self)
|
2020-11-17 08:42:43 +01:00
|
|
|
tooltip(tr(TR.BROWSING_NOTES_TAGGED))
|
2013-10-03 17:07:11 +02:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def dupeLinkClicked(self, link: str) -> None:
|
2020-10-19 20:51:36 +02:00
|
|
|
self.search_for(link)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.onNote()
|
|
|
|
|
|
|
|
# Jumping
|
|
|
|
######################################################################
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def _moveCur(self, dir: int, idx: QModelIndex = None) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
if not self.model.cards:
|
|
|
|
return
|
|
|
|
tv = self.form.tableView
|
|
|
|
if idx is None:
|
|
|
|
idx = tv.moveCursor(dir, self.mw.app.keyboardModifiers())
|
2017-08-05 07:15:19 +02:00
|
|
|
tv.selectionModel().setCurrentIndex(
|
|
|
|
idx,
|
2019-12-23 01:34:10 +01:00
|
|
|
QItemSelectionModel.Clear
|
|
|
|
| QItemSelectionModel.Select
|
|
|
|
| QItemSelectionModel.Rows,
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onPreviousCard(self) -> None:
|
2017-08-05 07:15:19 +02:00
|
|
|
self.focusTo = self.editor.currentField
|
2021-03-16 13:40:37 +01:00
|
|
|
self.editor.call_after_note_saved(self._onPreviousCard)
|
2016-07-14 12:23:44 +02:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def _onPreviousCard(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self._moveCur(QAbstractItemView.MoveUp)
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onNextCard(self) -> None:
|
2017-08-05 07:15:19 +02:00
|
|
|
self.focusTo = self.editor.currentField
|
2021-03-16 13:40:37 +01:00
|
|
|
self.editor.call_after_note_saved(self._onNextCard)
|
2016-07-14 12:23:44 +02:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def _onNextCard(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self._moveCur(QAbstractItemView.MoveDown)
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onFirstCard(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
sm = self.form.tableView.selectionModel()
|
|
|
|
idx = sm.currentIndex()
|
|
|
|
self._moveCur(None, self.model.index(0, 0))
|
2021-03-17 05:51:59 +01:00
|
|
|
if not KeyboardModifiersPressed().shift:
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
|
|
|
idx2 = sm.currentIndex()
|
|
|
|
item = QItemSelection(idx2, idx)
|
2019-12-23 01:34:10 +01:00
|
|
|
sm.select(item, QItemSelectionModel.SelectCurrent | QItemSelectionModel.Rows)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onLastCard(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
sm = self.form.tableView.selectionModel()
|
|
|
|
idx = sm.currentIndex()
|
2019-12-23 01:34:10 +01:00
|
|
|
self._moveCur(None, self.model.index(len(self.model.cards) - 1, 0))
|
2021-03-17 05:51:59 +01:00
|
|
|
if not KeyboardModifiersPressed().shift:
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
|
|
|
idx2 = sm.currentIndex()
|
|
|
|
item = QItemSelection(idx, idx2)
|
2019-12-23 01:34:10 +01:00
|
|
|
sm.select(item, QItemSelectionModel.SelectCurrent | QItemSelectionModel.Rows)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onFind(self) -> None:
|
2020-10-24 10:47:25 +02:00
|
|
|
# workaround for PyQt focus bug
|
|
|
|
self.editor.hideCompleters()
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
self.form.searchEdit.setFocus()
|
|
|
|
self.form.searchEdit.lineEdit().selectAll()
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onNote(self) -> None:
|
2020-10-24 10:47:25 +02:00
|
|
|
# workaround for PyQt focus bug
|
|
|
|
self.editor.hideCompleters()
|
|
|
|
|
2013-01-29 01:49:04 +01:00
|
|
|
self.editor.web.setFocus()
|
2019-12-06 05:22:49 +01:00
|
|
|
self.editor.loadNote(focusTo=0)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onCardList(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.form.tableView.setFocus()
|
|
|
|
|
2021-03-23 10:36:52 +01:00
|
|
|
def focusCid(self, cid: CardID) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
try:
|
2021-01-30 13:15:46 +01:00
|
|
|
row = list(self.model.cards).index(cid)
|
|
|
|
except ValueError:
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2021-01-30 10:51:31 +01:00
|
|
|
self.form.tableView.clearSelection()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.form.tableView.selectRow(row)
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Change model dialog
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
class ChangeModel(QDialog):
|
2021-03-25 13:50:31 +01:00
|
|
|
def __init__(self, browser: Browser, nids: List[NoteID]) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.__init__(self, browser)
|
|
|
|
self.browser = browser
|
|
|
|
self.nids = nids
|
|
|
|
self.oldModel = browser.card.note().model()
|
|
|
|
self.form = aqt.forms.changemodel.Ui_Dialog()
|
|
|
|
self.form.setupUi(self)
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setWindowModality(Qt.WindowModal)
|
|
|
|
self.setup()
|
|
|
|
restoreGeom(self, "changeModel")
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.state_did_reset.append(self.onReset)
|
2020-01-15 22:53:12 +01:00
|
|
|
gui_hooks.current_note_type_did_change.append(self.on_note_type_change)
|
2021-03-17 05:51:59 +01:00
|
|
|
# ugh - these are set dynamically by rebuildTemplateMap()
|
|
|
|
self.tcombos: List[QComboBox] = []
|
|
|
|
self.fcombos: List[QComboBox] = []
|
2012-12-21 08:51:59 +01:00
|
|
|
self.exec_()
|
|
|
|
|
2020-01-15 22:53:12 +01:00
|
|
|
def on_note_type_change(self, notetype: NoteType) -> None:
|
|
|
|
self.onReset()
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def setup(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
# maps
|
|
|
|
self.flayout = QHBoxLayout()
|
2019-12-23 01:34:10 +01:00
|
|
|
self.flayout.setContentsMargins(0, 0, 0, 0)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.fwidg = None
|
|
|
|
self.form.fieldMap.setLayout(self.flayout)
|
|
|
|
self.tlayout = QHBoxLayout()
|
2019-12-23 01:34:10 +01:00
|
|
|
self.tlayout.setContentsMargins(0, 0, 0, 0)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.twidg = None
|
|
|
|
self.form.templateMap.setLayout(self.tlayout)
|
|
|
|
if self.style().objectName() == "gtk+":
|
|
|
|
# gtk+ requires margins in inner layout
|
|
|
|
self.form.verticalLayout_2.setContentsMargins(0, 11, 0, 0)
|
|
|
|
self.form.verticalLayout_3.setContentsMargins(0, 11, 0, 0)
|
|
|
|
# model chooser
|
|
|
|
import aqt.modelchooser
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
self.oldModel = self.browser.col.models.get(
|
|
|
|
self.browser.col.db.scalar(
|
2019-12-23 01:34:10 +01:00
|
|
|
"select mid from notes where id = ?", self.nids[0]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.form.oldModelLabel.setText(self.oldModel["name"])
|
2012-12-21 08:51:59 +01:00
|
|
|
self.modelChooser = aqt.modelchooser.ModelChooser(
|
2019-12-23 01:34:10 +01:00
|
|
|
self.browser.mw, self.form.modelChooserWidget, label=False
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.modelChooser.models.setFocus()
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.form.buttonBox.helpRequested, self.onHelp)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.modelChanged(self.browser.mw.col.models.current())
|
|
|
|
self.pauseUpdate = False
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onReset(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.modelChanged(self.browser.col.models.current())
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def modelChanged(self, model: Dict[str, Any]) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.targetModel = model
|
|
|
|
self.rebuildTemplateMap()
|
|
|
|
self.rebuildFieldMap()
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def rebuildTemplateMap(
|
|
|
|
self, key: Optional[str] = None, attr: Optional[str] = None
|
|
|
|
) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
if not key:
|
|
|
|
key = "t"
|
|
|
|
attr = "tmpls"
|
|
|
|
map = getattr(self, key + "widg")
|
|
|
|
lay = getattr(self, key + "layout")
|
|
|
|
src = self.oldModel[attr]
|
|
|
|
dst = self.targetModel[attr]
|
|
|
|
if map:
|
|
|
|
lay.removeWidget(map)
|
|
|
|
map.deleteLater()
|
|
|
|
setattr(self, key + "MapWidget", None)
|
|
|
|
map = QWidget()
|
|
|
|
l = QGridLayout()
|
|
|
|
combos = []
|
2020-11-17 08:42:43 +01:00
|
|
|
targets = [x["name"] for x in dst] + [tr(TR.BROWSING_NOTHING)]
|
2012-12-21 08:51:59 +01:00
|
|
|
indices = {}
|
|
|
|
for i, x in enumerate(src):
|
2020-11-17 12:47:47 +01:00
|
|
|
l.addWidget(QLabel(tr(TR.BROWSING_CHANGE_TO, val=x["name"])), i, 0)
|
2012-12-21 08:51:59 +01:00
|
|
|
cb = QComboBox()
|
|
|
|
cb.addItems(targets)
|
2019-12-23 01:34:10 +01:00
|
|
|
idx = min(i, len(targets) - 1)
|
2012-12-21 08:51:59 +01:00
|
|
|
cb.setCurrentIndex(idx)
|
|
|
|
indices[cb] = idx
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(
|
|
|
|
cb.currentIndexChanged,
|
|
|
|
lambda i, cb=cb, key=key: self.onComboChanged(i, cb, key),
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
combos.append(cb)
|
|
|
|
l.addWidget(cb, i, 1)
|
|
|
|
map.setLayout(l)
|
|
|
|
lay.addWidget(map)
|
|
|
|
setattr(self, key + "widg", map)
|
|
|
|
setattr(self, key + "layout", lay)
|
|
|
|
setattr(self, key + "combos", combos)
|
|
|
|
setattr(self, key + "indices", indices)
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def rebuildFieldMap(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
return self.rebuildTemplateMap(key="f", attr="flds")
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onComboChanged(self, i: int, cb: QComboBox, key: str) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
indices = getattr(self, key + "indices")
|
|
|
|
if self.pauseUpdate:
|
|
|
|
indices[cb] = i
|
|
|
|
return
|
|
|
|
combos = getattr(self, key + "combos")
|
|
|
|
if i == cb.count() - 1:
|
|
|
|
# set to 'nothing'
|
|
|
|
return
|
|
|
|
# find another combo with same index
|
|
|
|
for c in combos:
|
|
|
|
if c == cb:
|
|
|
|
continue
|
|
|
|
if c.currentIndex() == i:
|
|
|
|
self.pauseUpdate = True
|
|
|
|
c.setCurrentIndex(indices[cb])
|
|
|
|
self.pauseUpdate = False
|
|
|
|
break
|
|
|
|
indices[cb] = i
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def getTemplateMap(
|
|
|
|
self,
|
|
|
|
old: Optional[List[Dict[str, Any]]] = None,
|
|
|
|
combos: Optional[List[QComboBox]] = None,
|
|
|
|
new: Optional[List[Dict[str, Any]]] = None,
|
|
|
|
) -> Dict[int, Optional[int]]:
|
2012-12-21 08:51:59 +01:00
|
|
|
if not old:
|
2019-12-23 01:34:10 +01:00
|
|
|
old = self.oldModel["tmpls"]
|
2012-12-21 08:51:59 +01:00
|
|
|
combos = self.tcombos
|
2019-12-23 01:34:10 +01:00
|
|
|
new = self.targetModel["tmpls"]
|
2020-07-26 01:25:39 +02:00
|
|
|
template_map: Dict[int, Optional[int]] = {}
|
2012-12-21 08:51:59 +01:00
|
|
|
for i, f in enumerate(old):
|
|
|
|
idx = combos[i].currentIndex()
|
|
|
|
if idx == len(new):
|
|
|
|
# ignore
|
2020-07-26 01:25:39 +02:00
|
|
|
template_map[f["ord"]] = None
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
|
|
|
f2 = new[idx]
|
2020-07-26 01:25:39 +02:00
|
|
|
template_map[f["ord"]] = f2["ord"]
|
|
|
|
return template_map
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def getFieldMap(self) -> Dict[int, Optional[int]]:
|
2012-12-21 08:51:59 +01:00
|
|
|
return self.getTemplateMap(
|
2019-12-23 01:34:10 +01:00
|
|
|
old=self.oldModel["flds"], combos=self.fcombos, new=self.targetModel["flds"]
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def cleanup(self) -> None:
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.state_did_reset.remove(self.onReset)
|
2020-01-15 22:53:12 +01:00
|
|
|
gui_hooks.current_note_type_did_change.remove(self.on_note_type_change)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.modelChooser.cleanup()
|
|
|
|
saveGeom(self, "changeModel")
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def reject(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.cleanup()
|
|
|
|
return QDialog.reject(self)
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def accept(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
# check maps
|
|
|
|
fmap = self.getFieldMap()
|
|
|
|
cmap = self.getTemplateMap()
|
2016-05-12 06:45:35 +02:00
|
|
|
if any(True for c in list(cmap.values()) if c is None):
|
2020-11-18 02:32:22 +01:00
|
|
|
if not askUser(tr(TR.BROWSING_ANY_CARDS_MAPPED_TO_NOTHING_WILL)):
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2020-11-17 08:42:43 +01:00
|
|
|
self.browser.mw.checkpoint(tr(TR.BROWSING_CHANGE_NOTE_TYPE))
|
2012-12-21 08:51:59 +01:00
|
|
|
b = self.browser
|
2016-07-14 13:49:40 +02:00
|
|
|
b.mw.col.modSchema(check=True)
|
|
|
|
b.mw.progress.start()
|
2012-12-21 08:51:59 +01:00
|
|
|
b.model.beginReset()
|
|
|
|
mm = b.mw.col.models
|
|
|
|
mm.change(self.oldModel, self.nids, self.targetModel, fmap, cmap)
|
2016-07-14 12:23:44 +02:00
|
|
|
b.search()
|
2012-12-21 08:51:59 +01:00
|
|
|
b.model.endReset()
|
|
|
|
b.mw.progress.finish()
|
|
|
|
b.mw.reset()
|
|
|
|
self.cleanup()
|
make sure change note type clears up hooks
if an error occurred after QDialog.accept() had been called,
the hook was left lying around and caused errors when reset later
fired
File "aqt\main.py", line 1028, in onCheckDB
File "aqt\main.py", line 516, in reset
File "anki\hooks.py", line 28, in runHook
File "aqt\modelchooser.py", line 47, in onReset
File "aqt\modelchooser.py", line 82, in updateModels
<class 'RuntimeError'>: wrapped C/C++ object of type QPushButton has been deleted
2018-12-15 03:45:17 +01:00
|
|
|
QDialog.accept(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def onHelp(self) -> None:
|
2021-01-25 14:45:47 +01:00
|
|
|
openHelp(HelpPage.BROWSING_OTHER_MENU_ITEMS)
|
2020-02-12 22:12:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Card Info Dialog
|
|
|
|
######################################################################
|
|
|
|
|
2020-02-12 22:15:44 +01:00
|
|
|
|
2020-02-12 22:12:45 +01:00
|
|
|
class CardInfoDialog(QDialog):
|
|
|
|
silentlyClose = True
|
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def __init__(self, browser: Browser) -> None:
|
|
|
|
super().__init__(browser)
|
2020-02-12 22:12:45 +01:00
|
|
|
self.browser = browser
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(self)
|
2020-02-12 22:12:45 +01:00
|
|
|
|
2021-02-01 00:39:55 +01:00
|
|
|
def reject(self) -> None:
|
2020-02-12 22:12:45 +01:00
|
|
|
saveGeom(self, "revlog")
|
|
|
|
return QDialog.reject(self)
|