2012-12-21 08:51:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
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
|
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-01-04 05:13:20 +01:00
|
|
|
from concurrent.futures import Future
|
2020-02-11 23:28:21 +01:00
|
|
|
from dataclasses import dataclass
|
2019-12-20 10:19:03 +01:00
|
|
|
from operator import itemgetter
|
2021-01-25 14:45:47 +01:00
|
|
|
from typing import List, Optional, Sequence, Tuple, 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
|
2020-01-15 22:41:23 +01:00
|
|
|
from anki.cards import Card
|
2021-01-28 11:17:24 +01:00
|
|
|
from anki.collection import Collection, InvalidInput, NamedFilter
|
2019-12-20 10:19:03 +01:00
|
|
|
from anki.consts import *
|
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
|
2020-01-15 22:53:12 +01:00
|
|
|
from anki.notes import Note
|
2020-06-15 06:14:18 +02:00
|
|
|
from anki.stats import CardStats
|
2020-09-03 09:42:46 +02:00
|
|
|
from anki.utils import htmlToTextLine, ids2str, isMac, isWin
|
2020-01-14 23:53:57 +01:00
|
|
|
from aqt import AnkiQt, gui_hooks
|
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
|
2020-08-16 18:49:51 +02:00
|
|
|
from aqt.main import ResetReason
|
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 *
|
2021-01-23 10:59:12 +01:00
|
|
|
from aqt.sidebar import SidebarTreeView
|
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,
|
2019-12-23 01:34:10 +01:00
|
|
|
MenuList,
|
|
|
|
SubMenu,
|
|
|
|
askUser,
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button,
|
2019-12-23 01:34:10 +01:00
|
|
|
getOnlyText,
|
|
|
|
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,
|
2020-06-05 03:39:53 +02:00
|
|
|
restore_is_checked,
|
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,
|
2020-06-05 03:39:53 +02:00
|
|
|
save_is_checked,
|
2019-12-23 01:34:10 +01:00
|
|
|
saveGeom,
|
|
|
|
saveHeader,
|
|
|
|
saveSplitter,
|
|
|
|
saveState,
|
|
|
|
shortcut,
|
2021-01-25 23:19:19 +01:00
|
|
|
show_invalid_search_error,
|
2019-12-23 01:34:10 +01:00
|
|
|
showInfo,
|
|
|
|
showWarning,
|
|
|
|
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
|
|
|
|
|
2021-01-23 10:59:12 +01:00
|
|
|
# legacy add-on support
|
|
|
|
# pylint: disable=unused-import
|
|
|
|
from aqt.sidebar import SidebarItem, SidebarStage # isort: skip
|
|
|
|
|
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
|
|
|
|
card_ids: Optional[Sequence[int]] = None
|
|
|
|
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Data model
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
class DataModel(QAbstractTableModel):
|
2020-02-23 09:47:16 +01:00
|
|
|
def __init__(self, browser: Browser):
|
2012-12-21 08:51:59 +01:00
|
|
|
QAbstractTableModel.__init__(self)
|
|
|
|
self.browser = browser
|
|
|
|
self.col = browser.col
|
|
|
|
self.sortKey = None
|
2020-04-05 13:38:58 +02:00
|
|
|
self.activeCols = self.col.get_config(
|
2019-12-23 01:34:10 +01:00
|
|
|
"activeCols", ["noteFld", "template", "cardDue", "deck"]
|
|
|
|
)
|
2020-03-21 07:38:46 +01:00
|
|
|
self.cards: Sequence[int] = []
|
2020-02-23 09:47:16 +01:00
|
|
|
self.cardObjs: Dict[int, Card] = {}
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-03-17 05:50:25 +01:00
|
|
|
def getCard(self, index: QModelIndex) -> Card:
|
2012-12-21 08:51:59 +01:00
|
|
|
id = self.cards[index.row()]
|
|
|
|
if not id in self.cardObjs:
|
2013-10-30 14:42:32 +01:00
|
|
|
self.cardObjs[id] = self.col.getCard(id)
|
2012-12-21 08:51:59 +01:00
|
|
|
return self.cardObjs[id]
|
|
|
|
|
|
|
|
def refreshNote(self, note):
|
|
|
|
refresh = False
|
|
|
|
for c in note.cards():
|
|
|
|
if c.id in self.cardObjs:
|
|
|
|
del self.cardObjs[c.id]
|
|
|
|
refresh = True
|
|
|
|
if refresh:
|
2020-07-26 01:25:39 +02:00
|
|
|
self.layoutChanged.emit() # type: ignore
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Model interface
|
|
|
|
######################################################################
|
|
|
|
|
2017-02-19 05:30:35 +01:00
|
|
|
def rowCount(self, parent):
|
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)
|
|
|
|
|
2017-02-19 05:30:35 +01:00
|
|
|
def columnCount(self, parent):
|
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)
|
|
|
|
|
|
|
|
def data(self, index, role):
|
|
|
|
if not index.isValid():
|
|
|
|
return
|
|
|
|
if role == Qt.FontRole:
|
2019-12-23 01:34:10 +01:00
|
|
|
if self.activeCols[index.column()] not in ("question", "answer", "noteFld"):
|
2012-12-22 02:10:52 +01:00
|
|
|
return
|
|
|
|
c = self.getCard(index)
|
|
|
|
t = c.template()
|
2017-07-29 08:00:02 +02:00
|
|
|
if not t.get("bfont"):
|
|
|
|
return
|
|
|
|
f = QFont()
|
2020-07-26 01:25:39 +02:00
|
|
|
f.setFamily(cast(str, t.get("bfont", "arial")))
|
|
|
|
f.setPixelSize(cast(int, t.get("bsize", 12)))
|
2012-12-22 02:10:52 +01:00
|
|
|
return f
|
2017-07-29 08:00:02 +02:00
|
|
|
|
2012-12-22 02:10:52 +01:00
|
|
|
elif 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
|
|
|
|
elif role == Qt.DisplayRole or role == Qt.EditRole:
|
|
|
|
return self.columnData(index)
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
def headerData(self, section, orientation, role):
|
|
|
|
if orientation == Qt.Vertical:
|
|
|
|
return
|
|
|
|
elif role == Qt.DisplayRole and section < len(self.activeCols):
|
|
|
|
type = self.columnType(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:
|
|
|
|
return
|
|
|
|
|
|
|
|
def flags(self, index):
|
2019-12-23 01:34:10 +01:00
|
|
|
return Qt.ItemFlag(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 = []
|
2021-01-16 06:37:40 +01:00
|
|
|
exception: Optional[Exception] = None
|
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-12-29 11:18:49 +01:00
|
|
|
ctx.search = self.browser.normalize_search(ctx.search)
|
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
|
2017-02-02 12:00:58 +01:00
|
|
|
except Exception as e:
|
2021-01-16 06:37:40 +01:00
|
|
|
exception = e
|
2020-03-20 09:33:35 +01:00
|
|
|
finally:
|
|
|
|
self.endReset()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-01-16 06:37:40 +01:00
|
|
|
if exception:
|
|
|
|
show_invalid_search_error(exception)
|
2017-02-02 12:00:58 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def reset(self):
|
|
|
|
self.beginReset()
|
|
|
|
self.endReset()
|
|
|
|
|
2016-07-14 12:23:44 +02:00
|
|
|
# caller must have called editor.saveNow() before calling this or .reset()
|
2012-12-21 08:51:59 +01:00
|
|
|
def beginReset(self):
|
|
|
|
self.browser.editor.setNote(None, hide=False)
|
|
|
|
self.browser.mw.progress.start()
|
|
|
|
self.saveSelection()
|
|
|
|
self.beginResetModel()
|
|
|
|
self.cardObjs = {}
|
|
|
|
|
|
|
|
def endReset(self):
|
|
|
|
self.endResetModel()
|
|
|
|
self.restoreSelection()
|
|
|
|
self.browser.mw.progress.finish()
|
|
|
|
|
|
|
|
def reverse(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
self.browser.editor.saveNow(self._reverse)
|
|
|
|
|
|
|
|
def _reverse(self):
|
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()
|
|
|
|
|
|
|
|
def saveSelection(self):
|
|
|
|
cards = self.browser.selectedCards()
|
|
|
|
self.selectedCards = dict([(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
|
|
|
|
|
|
|
|
def restoreSelection(self):
|
|
|
|
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)
|
|
|
|
|
|
|
|
# Column data
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def columnType(self, column):
|
|
|
|
return self.activeCols[column]
|
|
|
|
|
2020-03-19 22:59:59 +01:00
|
|
|
def time_format(self):
|
|
|
|
return "%Y-%m-%d"
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def columnData(self, index):
|
|
|
|
col = index.column()
|
|
|
|
type = self.columnType(col)
|
|
|
|
c = self.getCard(index)
|
|
|
|
if type == "question":
|
|
|
|
return self.question(c)
|
|
|
|
elif type == "answer":
|
|
|
|
return self.answer(c)
|
|
|
|
elif type == "noteFld":
|
|
|
|
f = c.note()
|
2017-03-14 07:48:40 +01:00
|
|
|
return htmlToTextLine(f.fields[self.col.models.sortIdx(f.model())])
|
2012-12-21 08:51:59 +01:00
|
|
|
elif type == "template":
|
2019-12-23 01:34:10 +01:00
|
|
|
t = c.template()["name"]
|
|
|
|
if c.model()["type"] == MODEL_CLOZE:
|
2020-07-26 01:25:39 +02:00
|
|
|
t = f"{t} {c.ord + 1}"
|
2012-12-21 08:51:59 +01:00
|
|
|
return t
|
|
|
|
elif type == "cardDue":
|
|
|
|
# catch invalid dates
|
|
|
|
try:
|
|
|
|
t = self.nextDue(c, index)
|
|
|
|
except:
|
|
|
|
t = ""
|
|
|
|
if c.queue < 0:
|
2020-07-26 01:25:39 +02:00
|
|
|
t = f"({t})"
|
2012-12-21 08:51:59 +01:00
|
|
|
return t
|
|
|
|
elif type == "noteCrt":
|
2020-03-19 22:59:59 +01:00
|
|
|
return time.strftime(self.time_format(), time.localtime(c.note().id / 1000))
|
2012-12-21 08:51:59 +01:00
|
|
|
elif type == "noteMod":
|
2020-03-19 22:59:59 +01:00
|
|
|
return time.strftime(self.time_format(), time.localtime(c.note().mod))
|
2012-12-21 08:51:59 +01:00
|
|
|
elif type == "cardMod":
|
2020-03-19 22:59:59 +01:00
|
|
|
return time.strftime(self.time_format(), time.localtime(c.mod))
|
2012-12-21 08:51:59 +01:00
|
|
|
elif type == "cardReps":
|
|
|
|
return str(c.reps)
|
|
|
|
elif type == "cardLapses":
|
|
|
|
return str(c.lapses)
|
2013-09-03 20:20:20 +02:00
|
|
|
elif type == "noteTags":
|
2013-10-08 00:42:06 +02:00
|
|
|
return " ".join(c.note().tags)
|
2012-12-21 08:51:59 +01:00
|
|
|
elif type == "note":
|
2019-12-23 01:34:10 +01:00
|
|
|
return c.model()["name"]
|
2012-12-21 08:51:59 +01:00
|
|
|
elif type == "cardIvl":
|
2020-03-17 05:50:25 +01:00
|
|
|
if c.type == CARD_TYPE_NEW:
|
2020-11-17 08:42:43 +01:00
|
|
|
return tr(TR.BROWSING_NEW)
|
2020-03-17 05:50:25 +01:00
|
|
|
elif c.type == CARD_TYPE_LRN:
|
2020-11-17 08:42:43 +01:00
|
|
|
return tr(TR.BROWSING_LEARNING)
|
2020-05-24 00:36:50 +02:00
|
|
|
return self.col.format_timespan(c.ivl * 86400)
|
2012-12-21 08:51:59 +01:00
|
|
|
elif type == "cardEase":
|
2020-03-17 05:50:25 +01:00
|
|
|
if c.type == CARD_TYPE_NEW:
|
2020-11-17 08:42:43 +01:00
|
|
|
return tr(TR.BROWSING_NEW)
|
2019-12-23 01:34:10 +01:00
|
|
|
return "%d%%" % (c.factor / 10)
|
2012-12-21 08:51:59 +01:00
|
|
|
elif type == "deck":
|
|
|
|
if c.odid:
|
|
|
|
# in a cram deck
|
|
|
|
return "%s (%s)" % (
|
|
|
|
self.browser.mw.col.decks.name(c.did),
|
2019-12-23 01:34:10 +01:00
|
|
|
self.browser.mw.col.decks.name(c.odid),
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
# normal deck
|
|
|
|
return self.browser.mw.col.decks.name(c.did)
|
|
|
|
|
|
|
|
def question(self, c):
|
2017-03-14 07:48:40 +01:00
|
|
|
return htmlToTextLine(c.q(browser=True))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def answer(self, c):
|
2019-12-23 01:34:10 +01:00
|
|
|
if c.template().get("bafmt"):
|
2012-12-21 08:51:59 +01:00
|
|
|
# they have provided a template, use it verbatim
|
|
|
|
c.q(browser=True)
|
2017-03-14 07:48:40 +01:00
|
|
|
return htmlToTextLine(c.a())
|
2012-12-21 08:51:59 +01:00
|
|
|
# need to strip question from answer
|
|
|
|
q = self.question(c)
|
2017-03-14 07:48:40 +01:00
|
|
|
a = htmlToTextLine(c.a())
|
2012-12-21 08:51:59 +01:00
|
|
|
if a.startswith(q):
|
2019-12-23 01:34:10 +01:00
|
|
|
return a[len(q) :].strip()
|
2012-12-21 08:51:59 +01:00
|
|
|
return a
|
|
|
|
|
|
|
|
def nextDue(self, c, index):
|
|
|
|
if c.odid:
|
2020-11-17 08:42:43 +01:00
|
|
|
return tr(TR.BROWSING_FILTERED)
|
2020-02-02 10:28:04 +01:00
|
|
|
elif c.queue == QUEUE_TYPE_LRN:
|
2012-12-21 08:51:59 +01:00
|
|
|
date = c.due
|
2020-02-02 10:19:45 +01:00
|
|
|
elif c.queue == QUEUE_TYPE_NEW or c.type == CARD_TYPE_NEW:
|
2020-02-27 03:25:19 +01:00
|
|
|
return tr(TR.STATISTICS_DUE_FOR_NEW_CARD, number=c.due)
|
2020-01-31 09:58:03 +01:00
|
|
|
elif c.queue in (QUEUE_TYPE_REV, QUEUE_TYPE_DAY_LEARN_RELEARN) or (
|
|
|
|
c.type == CARD_TYPE_REV and c.queue < 0
|
|
|
|
):
|
2019-12-23 01:34:10 +01:00
|
|
|
date = time.time() + ((c.due - self.col.sched.today) * 86400)
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
|
|
|
return ""
|
2020-03-19 22:59:59 +01:00
|
|
|
return time.strftime(self.time_format(), time.localtime(date))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2018-08-08 02:39:54 +02:00
|
|
|
def isRTL(self, index):
|
|
|
|
col = index.column()
|
|
|
|
type = self.columnType(col)
|
|
|
|
if type != "noteFld":
|
|
|
|
return False
|
|
|
|
|
|
|
|
c = self.getCard(index)
|
|
|
|
nt = c.note().model()
|
2019-12-23 01:34:10 +01:00
|
|
|
return nt["flds"][self.col.models.sortIdx(nt)]["rtl"]
|
|
|
|
|
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):
|
2012-12-21 08:51:59 +01:00
|
|
|
def __init__(self, browser, model):
|
|
|
|
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
|
|
|
|
|
|
|
|
def paint(self, painter, option, index):
|
|
|
|
try:
|
|
|
|
c = self.model.getCard(index)
|
|
|
|
except:
|
|
|
|
# in the the middle of a reset; return nothing so this row is not
|
|
|
|
# rendered until we have a chance to reset the model
|
|
|
|
return
|
2017-08-12 08:08:10 +02:00
|
|
|
|
2018-08-08 02:39:54 +02:00
|
|
|
if self.model.isRTL(index):
|
|
|
|
option.direction = Qt.RightToLeft
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
col = None
|
2018-09-05 08:55:26 +02:00
|
|
|
if c.userFlag() > 0:
|
2020-01-23 06:08:10 +01:00
|
|
|
col = theme_manager.qcolor(f"flag{c.userFlag()}-bg")
|
2017-08-16 12:30:29 +02:00
|
|
|
elif c.note().hasTag("Marked"):
|
2020-01-23 06:08:10 +01:00
|
|
|
col = theme_manager.qcolor("marked-bg")
|
2020-03-17 05:50:25 +01:00
|
|
|
elif c.queue == QUEUE_TYPE_SUSPENDED:
|
2020-01-23 06:08:10 +01:00
|
|
|
col = theme_manager.qcolor("suspended-bg")
|
2012-12-21 08:51:59 +01:00
|
|
|
if col:
|
2020-01-23 06:08:10 +01:00
|
|
|
brush = QBrush(col)
|
2012-12-21 08:51:59 +01:00
|
|
|
painter.save()
|
|
|
|
painter.fillRect(option.rect, brush)
|
|
|
|
painter.restore()
|
2017-08-12 08:08:10 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
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
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
# fixme: respond to reset+edit hooks
|
|
|
|
|
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
|
|
|
|
2020-10-12 09:26:53 +02:00
|
|
|
def __init__(self, mw: AnkiQt) -> None:
|
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())
|
2016-07-14 12:23:44 +02:00
|
|
|
self.setupSearch()
|
2020-02-29 17:02:51 +01:00
|
|
|
gui_hooks.browser_will_show(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.show()
|
|
|
|
|
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
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(f.filter.clicked, self.onFilterButton)
|
2017-08-11 06:40:51 +02:00
|
|
|
# edit
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(f.actionUndo.triggered, self.mw.onUndo)
|
|
|
|
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)
|
|
|
|
# notes
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(f.actionAdd.triggered, self.mw.onAddCard)
|
|
|
|
qconnect(f.actionAdd_Tags.triggered, lambda: self.addTags())
|
|
|
|
qconnect(f.actionRemove_Tags.triggered, lambda: self.deleteTags())
|
|
|
|
qconnect(f.actionClear_Unused_Tags.triggered, self.clearUnusedTags)
|
|
|
|
qconnect(f.actionToggle_Mark.triggered, lambda: self.onMark())
|
|
|
|
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)
|
|
|
|
qconnect(f.actionDelete.triggered, self.deleteNotes)
|
2017-08-11 06:40:51 +02:00
|
|
|
# cards
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(f.actionChange_Deck.triggered, self.setDeck)
|
|
|
|
qconnect(f.action_Info.triggered, self.showCardInfo)
|
|
|
|
qconnect(f.actionReposition.triggered, self.reposition)
|
|
|
|
qconnect(f.actionReschedule.triggered, self.reschedule)
|
|
|
|
qconnect(f.actionToggle_Suspend.triggered, self.onSuspend)
|
|
|
|
qconnect(f.actionRed_Flag.triggered, lambda: self.onSetFlag(1))
|
|
|
|
qconnect(f.actionOrange_Flag.triggered, lambda: self.onSetFlag(2))
|
|
|
|
qconnect(f.actionGreen_Flag.triggered, lambda: self.onSetFlag(3))
|
|
|
|
qconnect(f.actionBlue_Flag.triggered, lambda: self.onSetFlag(4))
|
|
|
|
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.actionTags.triggered, self.onFilterButton)
|
|
|
|
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
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def onContextMenu(self, _point) -> 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())
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def updateFont(self):
|
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
|
|
|
|
|
|
|
def closeEvent(self, evt):
|
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
|
|
|
|
self.editor.saveNow(self._closeWindow)
|
|
|
|
evt.ignore()
|
2016-07-14 12:23:44 +02:00
|
|
|
|
2017-08-16 04:45:33 +02:00
|
|
|
def _closeWindow(self):
|
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
|
2017-08-16 04:45:33 +02:00
|
|
|
self.mw.gcWindow(self)
|
|
|
|
self.close()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2017-08-16 04:45:33 +02:00
|
|
|
def closeWithCallback(self, onsuccess):
|
|
|
|
def callback():
|
|
|
|
self._closeWindow()
|
|
|
|
onsuccess()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2017-08-16 04:45:33 +02:00
|
|
|
self.editor.saveNow(callback)
|
2013-04-11 12:23:32 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def keyPressEvent(self, evt):
|
|
|
|
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
|
|
|
|
|
|
|
def setupColumns(self):
|
|
|
|
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))
|
|
|
|
|
|
|
|
# Searching
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def setupSearch(self):
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.form.searchButton.clicked, self.onSearchActivated)
|
|
|
|
qconnect(self.form.searchEdit.lineEdit().returnPressed, self.onSearchActivated)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.form.searchEdit.setCompleter(None)
|
2020-11-17 08:42:43 +01:00
|
|
|
self._searchPrompt = tr(TR.BROWSING_TYPE_HERE_TO_SEARCH)
|
2021-01-28 13:26:52 +01:00
|
|
|
self._searchPromptFilter = self.col.search_string(name=NamedFilter.CURRENT_DECK)
|
2019-12-23 01:34:10 +01:00
|
|
|
self.form.searchEdit.addItems(
|
|
|
|
[self._searchPrompt] + self.mw.pm.profile["searchHistory"]
|
|
|
|
)
|
2020-10-19 20:57:19 +02:00
|
|
|
self.search_for("is:current", self._searchPrompt)
|
2016-07-14 12:23:44 +02:00
|
|
|
# then replace text for easily showing the deck
|
|
|
|
self.form.searchEdit.lineEdit().selectAll()
|
|
|
|
self.form.searchEdit.setFocus()
|
|
|
|
|
|
|
|
# search triggered by user
|
|
|
|
def onSearchActivated(self):
|
|
|
|
self.editor.saveNow(self._onSearchActivated)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2016-07-14 12:23:44 +02:00
|
|
|
def _onSearchActivated(self):
|
2018-11-12 03:43:54 +01:00
|
|
|
# grab search text and normalize
|
2020-10-19 20:51:36 +02:00
|
|
|
prompt = self.form.searchEdit.lineEdit().text()
|
|
|
|
|
2016-07-14 12:23:44 +02:00
|
|
|
# convert guide text before we save history
|
2021-01-28 13:26:52 +01:00
|
|
|
txt = self._searchPromptFilter if prompt == self._searchPrompt else prompt
|
2020-10-19 21:07:29 +02:00
|
|
|
self.update_history(txt)
|
2016-07-14 12:23:44 +02:00
|
|
|
|
2020-10-19 21:07:29 +02:00
|
|
|
# keep track of search string so that we reuse identical search when
|
|
|
|
# refreshing, rather than whatever is currently in the search field
|
|
|
|
self.search_for(txt)
|
2018-11-12 03:43:54 +01:00
|
|
|
|
2020-10-19 21:07:29 +02:00
|
|
|
def update_history(self, search: str) -> None:
|
2019-12-23 01:34:10 +01:00
|
|
|
sh = self.mw.pm.profile["searchHistory"]
|
2020-10-19 21:07:29 +02:00
|
|
|
if search in sh:
|
|
|
|
sh.remove(search)
|
|
|
|
sh.insert(0, search)
|
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
|
2016-07-14 12:23:44 +02:00
|
|
|
|
2020-10-19 20:37:17 +02:00
|
|
|
def search_for(self, search: str, prompt: Optional[str] = None) -> None:
|
|
|
|
self._lastSearchTxt = search
|
|
|
|
self.form.searchEdit.lineEdit().setText(prompt or search)
|
2016-07-14 12:23:44 +02:00
|
|
|
self.search()
|
|
|
|
|
|
|
|
# search triggered programmatically. caller must have saved note first.
|
2020-01-15 22:41:23 +01:00
|
|
|
def search(self) -> None:
|
2016-07-14 12:23:44 +02:00
|
|
|
if "is:current" in self._lastSearchTxt:
|
|
|
|
# show current card if there is one
|
2020-02-17 10:57:11 +01:00
|
|
|
c = self.card = self.mw.reviewer.card
|
2016-07-14 12:23:44 +02:00
|
|
|
nid = c and c.nid or 0
|
2020-02-17 10:57:11 +01:00
|
|
|
if nid:
|
2021-01-02 19:38:57 +01:00
|
|
|
search = "nid:%d" % nid
|
|
|
|
search = gui_hooks.default_search(search, c)
|
|
|
|
self.model.search(search)
|
2020-02-17 10:57:11 +01:00
|
|
|
self.focusCid(c.id)
|
2016-07-14 12:23:44 +02:00
|
|
|
else:
|
|
|
|
self.model.search(self._lastSearchTxt)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
if not self.model.cards:
|
|
|
|
# no row change will fire
|
2016-07-14 12:23:44 +02:00
|
|
|
self._onRowChanged(None, None)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-12-29 11:18:49 +01:00
|
|
|
def normalize_search(self, search: str) -> str:
|
2021-01-28 11:17:24 +01:00
|
|
|
normed = self.col.search_string(searches=[search])
|
2020-12-22 11:08:47 +01:00
|
|
|
self._lastSearchTxt = normed
|
|
|
|
self.form.searchEdit.lineEdit().setText(normed)
|
|
|
|
return normed
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def updateTitle(self):
|
|
|
|
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
|
|
|
|
|
|
|
|
def onReset(self):
|
2021-01-23 10:59:12 +01:00
|
|
|
self.sidebar.refresh()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.editor.setNote(None)
|
2016-07-14 12:23:44 +02:00
|
|
|
self.search()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Table view & editor
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def setupTable(self):
|
|
|
|
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():
|
|
|
|
grid = theme_manager.str_color("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
|
|
|
|
|
|
|
def setupEditor(self):
|
2021-01-09 22:34:53 +01:00
|
|
|
def add_preview_button(leftbuttons, editor):
|
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
|
|
|
|
|
|
|
def onRowChanged(self, current, previous):
|
|
|
|
"Update current note and hide/show editor."
|
2016-07-14 12:23:44 +02:00
|
|
|
self.editor.saveNow(lambda: self._onRowChanged(current, previous))
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def _onRowChanged(self, current, previous) -> None:
|
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
|
2020-03-23 18:16:39 +01:00
|
|
|
self.form.splitter.widget(1).setVisible(bool(show))
|
2017-08-31 12:38:12 +02:00
|
|
|
idx = self.form.tableView.selectionModel().currentIndex()
|
|
|
|
if idx.isValid():
|
|
|
|
self.card = self.model.getCard(idx)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
if not show:
|
|
|
|
self.editor.setNote(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:
|
2017-08-05 07:15:19 +02:00
|
|
|
self.editor.setNote(self.card.note(reload=True), focusTo=self.focusTo)
|
|
|
|
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
|
2018-11-12 03:10:50 +01:00
|
|
|
self._updateFlagsMenu()
|
2020-01-15 08:45:35 +01:00
|
|
|
gui_hooks.browser_did_change_row(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-01-15 22:53:12 +01:00
|
|
|
def refreshCurrentCard(self, note: Note) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model.refreshNote(note)
|
2020-07-24 03:57:22 +02:00
|
|
|
self._renderPreview()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2018-11-27 08:58:38 +01:00
|
|
|
def onLoadNote(self, editor):
|
|
|
|
self.refreshCurrentCard(editor.note)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def refreshCurrentCardFilter(self, flag, note, fidx):
|
|
|
|
self.refreshCurrentCard(note)
|
|
|
|
return flag
|
|
|
|
|
2013-05-03 10:52:46 +02:00
|
|
|
def currentRow(self):
|
|
|
|
idx = self.form.tableView.selectionModel().currentIndex()
|
|
|
|
return idx.row()
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Headers & sorting
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def setupHeaders(self):
|
|
|
|
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
|
|
|
|
|
|
|
def onSortChanged(self, idx, ord):
|
2019-10-28 04:25:12 +01:00
|
|
|
ord = bool(ord)
|
2016-07-14 12:23:44 +02:00
|
|
|
self.editor.saveNow(lambda: self._onSortChanged(idx, ord))
|
|
|
|
|
|
|
|
def _onSortChanged(self, idx, 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
|
2019-12-23 01:34:10 +01:00
|
|
|
self.col.conf["sortBackwards"] = ord
|
2020-03-20 06:44:35 +01:00
|
|
|
self.col.setMod()
|
|
|
|
self.col.save()
|
2016-07-14 12:23:44 +02:00
|
|
|
self.search()
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2019-12-23 01:34:10 +01:00
|
|
|
if self.col.conf["sortBackwards"] != ord:
|
|
|
|
self.col.conf["sortBackwards"] = ord
|
2020-03-20 06:44:35 +01:00
|
|
|
self.col.setMod()
|
|
|
|
self.col.save()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model.reverse()
|
|
|
|
self.setSortIndicator()
|
|
|
|
|
|
|
|
def setSortIndicator(self):
|
|
|
|
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)
|
2019-12-23 01:34:10 +01:00
|
|
|
if self.col.conf["sortBackwards"]:
|
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)
|
|
|
|
|
|
|
|
def onHeaderContext(self, pos):
|
|
|
|
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)
|
|
|
|
|
|
|
|
def toggleField(self, type):
|
2016-07-14 12:23:44 +02:00
|
|
|
self.editor.saveNow(lambda: self._toggleField(type))
|
|
|
|
|
|
|
|
def _toggleField(self, type):
|
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()
|
2020-11-17 08:42:43 +01:00
|
|
|
return showInfo(tr(TR.BROWSING_YOU_MUST_HAVE_AT_LEAST_ONE))
|
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
|
|
|
|
|
|
|
def setColumnSizes(self):
|
|
|
|
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
|
|
|
|
|
|
|
def onColumnMoved(self, a, b, c):
|
|
|
|
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)
|
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
|
|
|
|
2019-12-19 12:11:12 +01:00
|
|
|
def focusSidebar(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-23 10:59:12 +01:00
|
|
|
self.sidebar.setFocus()
|
2017-08-13 11:11:40 +02:00
|
|
|
|
2021-01-23 10:59:12 +01:00
|
|
|
# legacy
|
2019-12-19 12:11:12 +01:00
|
|
|
def maybeRefreshSidebar(self) -> None:
|
2021-01-23 10:59:12 +01:00
|
|
|
self.sidebar.refresh()
|
2019-12-19 12:11:12 +01:00
|
|
|
|
2021-01-23 10:59:12 +01:00
|
|
|
def toggle_sidebar(self):
|
|
|
|
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
|
|
|
# Filter button and sidebar helpers
|
2012-12-21 08:51:59 +01:00
|
|
|
######################################################################
|
|
|
|
|
2017-08-11 11:06:29 +02:00
|
|
|
def onFilterButton(self):
|
2017-08-15 10:41:36 +02:00
|
|
|
ml = MenuList()
|
2017-08-11 11:06:29 +02:00
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
ml.addChild(self._todayFilters())
|
|
|
|
ml.addChild(self._cardStateFilters())
|
|
|
|
ml.addSeparator()
|
2017-08-12 08:08:10 +02:00
|
|
|
|
2021-01-23 10:59:12 +01:00
|
|
|
toggle_sidebar = QAction(tr(TR.BROWSING_SIDEBAR))
|
|
|
|
qconnect(toggle_sidebar.triggered, self.toggle_sidebar)
|
|
|
|
toggle_sidebar.setCheckable(True)
|
|
|
|
toggle_sidebar.setChecked(self.sidebarDockWidget.isVisible())
|
|
|
|
ml.addChild(toggle_sidebar)
|
2017-08-15 10:41:36 +02:00
|
|
|
ml.addSeparator()
|
2017-08-13 11:11:40 +02:00
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
ml.addChild(self._savedSearches())
|
2017-08-11 11:06:29 +02:00
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
ml.popupOver(self.form.filter)
|
2017-08-11 11:06:29 +02:00
|
|
|
|
2021-01-23 10:59:12 +01:00
|
|
|
def update_search(self, *terms: str):
|
|
|
|
"Modify the current search string based on modified keys, then refresh."
|
2021-01-06 20:25:13 +01:00
|
|
|
try:
|
2021-01-28 11:17:24 +01:00
|
|
|
search = self.col.search_string(searches=list(terms))
|
2021-01-09 10:51:15 +01:00
|
|
|
mods = self.mw.app.keyboardModifiers()
|
|
|
|
if mods & Qt.AltModifier:
|
2021-01-28 11:17:24 +01:00
|
|
|
search = self.col.search_string(negate=True, searches=[search])
|
2016-05-12 06:45:35 +02:00
|
|
|
cur = str(self.form.searchEdit.lineEdit().text())
|
2021-01-06 20:25:13 +01:00
|
|
|
if cur != self._searchPrompt:
|
|
|
|
if mods & Qt.ControlModifier and mods & Qt.ShiftModifier:
|
2021-01-28 11:17:24 +01:00
|
|
|
search = self.col.replace_search_term(cur, search)
|
2021-01-06 20:25:13 +01:00
|
|
|
elif mods & Qt.ControlModifier:
|
2021-01-28 11:17:24 +01:00
|
|
|
search = self.col.search_string(searches=[cur, search])
|
2021-01-06 20:25:13 +01:00
|
|
|
elif mods & Qt.ShiftModifier:
|
2021-01-28 11:17:24 +01:00
|
|
|
search = self.col.search_string(
|
|
|
|
concat_by_or=True, searches=[cur, search]
|
2021-01-06 20:25:13 +01:00
|
|
|
)
|
|
|
|
except InvalidInput as e:
|
2021-01-16 06:37:40 +01:00
|
|
|
show_invalid_search_error(e)
|
2021-01-06 20:25:13 +01:00
|
|
|
else:
|
2021-01-09 10:51:15 +01:00
|
|
|
self.form.searchEdit.lineEdit().setText(search)
|
2021-01-06 20:25:13 +01:00
|
|
|
self.onSearchActivated()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-01-23 10:59:12 +01:00
|
|
|
# legacy
|
|
|
|
def setFilter(self, *terms: str):
|
|
|
|
self.set_filter_then_search(*terms)
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
def _simpleFilters(self, items):
|
|
|
|
ml = MenuList()
|
2017-08-11 11:06:29 +02:00
|
|
|
for row in items:
|
|
|
|
if row is None:
|
2017-08-15 10:41:36 +02:00
|
|
|
ml.addSeparator()
|
2017-08-11 11:06:29 +02:00
|
|
|
else:
|
2021-01-09 10:51:15 +01:00
|
|
|
label, filter_name = row
|
2021-01-23 10:59:12 +01:00
|
|
|
ml.addItem(label, self.sidebar._named_filter(filter_name))
|
2017-08-15 10:41:36 +02:00
|
|
|
return ml
|
|
|
|
|
|
|
|
def _todayFilters(self):
|
2020-11-17 08:42:43 +01:00
|
|
|
subm = SubMenu(tr(TR.BROWSING_TODAY))
|
2019-12-23 01:34:10 +01:00
|
|
|
subm.addChild(
|
|
|
|
self._simpleFilters(
|
|
|
|
(
|
2021-01-09 10:51:15 +01:00
|
|
|
(tr(TR.BROWSING_ADDED_TODAY), NamedFilter.ADDED_TODAY),
|
|
|
|
(tr(TR.BROWSING_STUDIED_TODAY), NamedFilter.STUDIED_TODAY),
|
|
|
|
(tr(TR.BROWSING_AGAIN_TODAY), NamedFilter.AGAIN_TODAY),
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2017-08-15 10:41:36 +02:00
|
|
|
return subm
|
2017-08-11 11:06:29 +02:00
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
def _cardStateFilters(self):
|
2020-11-17 08:42:43 +01:00
|
|
|
subm = SubMenu(tr(TR.BROWSING_CARD_STATE))
|
2019-12-23 01:34:10 +01:00
|
|
|
subm.addChild(
|
|
|
|
self._simpleFilters(
|
|
|
|
(
|
2021-01-09 10:51:15 +01:00
|
|
|
(tr(TR.ACTIONS_NEW), NamedFilter.NEW),
|
|
|
|
(tr(TR.SCHEDULING_LEARNING), NamedFilter.LEARN),
|
|
|
|
(tr(TR.SCHEDULING_REVIEW), NamedFilter.REVIEW),
|
|
|
|
(tr(TR.FILTERING_IS_DUE), NamedFilter.DUE),
|
2019-12-23 01:34:10 +01:00
|
|
|
None,
|
2021-01-09 10:51:15 +01:00
|
|
|
(tr(TR.BROWSING_SUSPENDED), NamedFilter.SUSPENDED),
|
|
|
|
(tr(TR.BROWSING_BURIED), NamedFilter.BURIED),
|
2019-12-23 01:34:10 +01:00
|
|
|
None,
|
2021-01-09 10:51:15 +01:00
|
|
|
(tr(TR.ACTIONS_RED_FLAG), NamedFilter.RED_FLAG),
|
|
|
|
(tr(TR.ACTIONS_ORANGE_FLAG), NamedFilter.ORANGE_FLAG),
|
|
|
|
(tr(TR.ACTIONS_GREEN_FLAG), NamedFilter.GREEN_FLAG),
|
|
|
|
(tr(TR.ACTIONS_BLUE_FLAG), NamedFilter.BLUE_FLAG),
|
|
|
|
(tr(TR.BROWSING_NO_FLAG), NamedFilter.NO_FLAG),
|
|
|
|
(tr(TR.BROWSING_ANY_FLAG), NamedFilter.ANY_FLAG),
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2017-08-15 10:41:36 +02:00
|
|
|
return subm
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
|
|
# Favourites
|
|
|
|
######################################################################
|
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
def _savedSearches(self):
|
|
|
|
ml = MenuList()
|
2017-08-11 11:06:29 +02:00
|
|
|
# make sure exists
|
|
|
|
if "savedFilters" not in self.col.conf:
|
2020-04-05 13:38:58 +02:00
|
|
|
self.col.set_config("savedFilters", {})
|
2017-08-11 11:06:29 +02:00
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
ml.addSeparator()
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
|
|
if self._currentFilterIsSaved():
|
2020-11-17 08:42:43 +01:00
|
|
|
ml.addItem(tr(TR.BROWSING_REMOVE_CURRENT_FILTER), self._onRemoveFilter)
|
2021-01-28 13:31:08 +01:00
|
|
|
elif self._searchPrompt != self.form.searchEdit.lineEdit().text():
|
2020-11-17 08:42:43 +01:00
|
|
|
ml.addItem(tr(TR.BROWSING_SAVE_CURRENT_FILTER), self._onSaveFilter)
|
2017-08-11 11:06:29 +02:00
|
|
|
|
2017-08-15 10:41:36 +02:00
|
|
|
return ml
|
2017-08-11 11:06:29 +02:00
|
|
|
|
2020-05-04 04:26:50 +02:00
|
|
|
def _onSaveFilter(self) -> None:
|
2021-01-07 22:11:21 +01:00
|
|
|
try:
|
2021-01-28 11:17:24 +01:00
|
|
|
filt = self.col.search_string(
|
|
|
|
searches=[self.form.searchEdit.lineEdit().text()]
|
2021-01-07 22:11:21 +01:00
|
|
|
)
|
|
|
|
except InvalidInput as e:
|
2021-01-16 06:37:40 +01:00
|
|
|
show_invalid_search_error(e)
|
2021-01-07 22:11:21 +01:00
|
|
|
else:
|
|
|
|
name = getOnlyText(tr(TR.BROWSING_PLEASE_GIVE_YOUR_FILTER_A_NAME))
|
|
|
|
if not name:
|
|
|
|
return
|
|
|
|
conf = self.col.get_config("savedFilters")
|
|
|
|
conf[name] = filt
|
|
|
|
self.col.set_config("savedFilters", conf)
|
2021-01-23 10:59:12 +01:00
|
|
|
self.sidebar.refresh()
|
2017-08-11 11:06:29 +02:00
|
|
|
|
2021-01-20 01:26:53 +01:00
|
|
|
def _onRemoveFilter(self) -> None:
|
|
|
|
self.removeFilter(self._currentFilterIsSaved())
|
|
|
|
|
|
|
|
def removeFilter(self, name: str) -> None:
|
2020-11-17 12:47:47 +01:00
|
|
|
if not askUser(tr(TR.BROWSING_REMOVE_FROM_YOUR_SAVED_SEARCHES, val=name)):
|
2017-08-11 11:06:29 +02:00
|
|
|
return
|
2021-01-20 01:26:53 +01:00
|
|
|
conf = self.col.get_config("savedFilters")
|
|
|
|
del conf[name]
|
|
|
|
self.col.set_config("savedFilters", conf)
|
2021-01-23 10:59:12 +01:00
|
|
|
self.sidebar.refresh()
|
2021-01-20 01:26:53 +01:00
|
|
|
|
|
|
|
def renameFilter(self, old: str) -> None:
|
|
|
|
conf = self.col.get_config("savedFilters")
|
|
|
|
try:
|
|
|
|
filt = conf[old]
|
|
|
|
except KeyError:
|
|
|
|
return
|
|
|
|
new = getOnlyText(tr(TR.ACTIONS_NEW_NAME), default=old)
|
|
|
|
if new == old or not new:
|
|
|
|
return
|
|
|
|
conf[new] = filt
|
|
|
|
del conf[old]
|
|
|
|
self.col.set_config("savedFilters", conf)
|
2021-01-23 10:59:12 +01:00
|
|
|
self.sidebar.refresh()
|
2017-08-11 11:06:29 +02:00
|
|
|
|
|
|
|
# returns name if found
|
2021-01-20 01:26:53 +01:00
|
|
|
def _currentFilterIsSaved(self) -> Optional[str]:
|
2017-08-11 11:06:29 +02:00
|
|
|
filt = self.form.searchEdit.lineEdit().text()
|
2021-01-07 22:11:21 +01:00
|
|
|
try:
|
2021-01-28 11:17:24 +01:00
|
|
|
filt = self.col.search_string(searches=[filt])
|
2021-01-07 22:11:21 +01:00
|
|
|
except InvalidInput:
|
|
|
|
pass
|
2020-04-05 13:38:58 +02:00
|
|
|
for k, v in self.col.get_config("savedFilters").items():
|
2021-01-07 22:11:21 +01:00
|
|
|
try:
|
2021-01-28 11:17:24 +01:00
|
|
|
v = self.col.search_string(searches=[v])
|
2021-01-07 22:11:21 +01:00
|
|
|
except InvalidInput:
|
|
|
|
pass
|
2017-08-11 11:06:29 +02:00
|
|
|
if filt == v:
|
|
|
|
return k
|
|
|
|
return None
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Info
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def showCardInfo(self):
|
|
|
|
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
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def selectedCards(self):
|
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
|
|
|
|
|
|
|
def selectedNotes(self):
|
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
|
|
|
|
|
|
|
def selectedNotesAsCards(self):
|
|
|
|
return self.col.db.list(
|
2019-12-23 01:34:10 +01:00
|
|
|
"select id from cards where nid in (%s)"
|
|
|
|
% ",".join([str(s) for s in self.selectedNotes()])
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def oneModelNotes(self):
|
|
|
|
sf = self.selectedNotes()
|
|
|
|
if not sf:
|
|
|
|
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))
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
|
|
|
return sf
|
|
|
|
|
|
|
|
def onHelp(self):
|
2021-01-25 14:45:47 +01:00
|
|
|
openHelp(HelpPage.BROWSING)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Misc menu options
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def onChangeModel(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
self.editor.saveNow(self._onChangeModel)
|
|
|
|
|
|
|
|
def _onChangeModel(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
nids = self.oneModelNotes()
|
|
|
|
if nids:
|
|
|
|
ChangeModel(self, nids)
|
|
|
|
|
2013-05-03 10:52:46 +02:00
|
|
|
# Preview
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def onTogglePreview(self):
|
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
|
|
|
|
2020-07-24 03:57:22 +02:00
|
|
|
def _renderPreview(self):
|
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
|
|
|
|
2020-04-10 10:42:28 +02:00
|
|
|
def _cleanup_preview(self):
|
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
|
|
|
|
2020-04-08 08:19:59 +02:00
|
|
|
def _on_preview_closed(self):
|
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
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def deleteNotes(self):
|
2018-03-01 04:31:52 +01:00
|
|
|
focus = self.focusWidget()
|
|
|
|
if focus != self.form.tableView:
|
|
|
|
return
|
|
|
|
self._deleteNotes()
|
2016-07-14 12:23:44 +02:00
|
|
|
|
|
|
|
def _deleteNotes(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
nids = self.selectedNotes()
|
|
|
|
if not nids:
|
|
|
|
return
|
2020-11-17 08:42:43 +01:00
|
|
|
self.mw.checkpoint(tr(TR.BROWSING_DELETE_NOTES))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model.beginReset()
|
2014-08-11 21:32:59 +02:00
|
|
|
# figure out where to place the cursor after the deletion
|
|
|
|
curRow = self.form.tableView.selectionModel().currentIndex().row()
|
2019-12-23 01:34:10 +01:00
|
|
|
selectedRows = [
|
|
|
|
i.row() for i in self.form.tableView.selectionModel().selectedRows()
|
|
|
|
]
|
2014-08-11 21:32:59 +02:00
|
|
|
if min(selectedRows) < curRow < max(selectedRows):
|
|
|
|
# last selection in middle; place one below last selected item
|
|
|
|
move = sum(1 for i in selectedRows if i > curRow)
|
|
|
|
newRow = curRow - move
|
|
|
|
elif max(selectedRows) <= curRow:
|
|
|
|
# last selection at bottom; place one below bottommost selection
|
|
|
|
newRow = max(selectedRows) - len(nids) + 1
|
|
|
|
else:
|
|
|
|
# last selection at top; place one above topmost selection
|
|
|
|
newRow = min(selectedRows) - 1
|
2020-06-04 10:21:04 +02:00
|
|
|
self.col.remove_notes(nids)
|
2016-07-14 12:23:44 +02:00
|
|
|
self.search()
|
2012-12-21 08:51:59 +01:00
|
|
|
if len(self.model.cards):
|
2014-08-11 21:32:59 +02:00
|
|
|
newRow = min(newRow, len(self.model.cards) - 1)
|
|
|
|
newRow = max(newRow, 0)
|
|
|
|
self.model.focusedCard = self.model.cards[newRow]
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model.endReset()
|
2020-08-17 12:07:48 +02:00
|
|
|
self.mw.reset()
|
2020-11-18 00:22:27 +01:00
|
|
|
tooltip(tr(TR.BROWSING_NOTE_DELETED, count=len(nids)))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Deck change
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def setDeck(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
self.editor.saveNow(self._setDeck)
|
|
|
|
|
|
|
|
def _setDeck(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
from aqt.studydeck import StudyDeck
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2013-02-20 07:25:59 +01:00
|
|
|
cids = self.selectedCards()
|
|
|
|
if not cids:
|
|
|
|
return
|
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)
|
|
|
|
deck = self.col.decks.get(did)
|
2019-12-23 01:34:10 +01:00
|
|
|
if deck["dyn"]:
|
2020-11-17 08:42:43 +01:00
|
|
|
showWarning(tr(TR.BROWSING_CARDS_CANT_BE_MANUALLY_MOVED_INTO))
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
|
|
|
self.model.beginReset()
|
2020-11-17 08:42:43 +01:00
|
|
|
self.mw.checkpoint(tr(TR.BROWSING_CHANGE_DECK))
|
2020-09-03 09:42:46 +02:00
|
|
|
self.col.set_deck(cids, did)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model.endReset()
|
2020-08-16 18:49:51 +02:00
|
|
|
self.mw.requireReset(reason=ResetReason.BrowserSetDeck, context=self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Tags
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def addTags(self, tags=None, label=None, prompt=None, func=None):
|
2016-07-14 12:23:44 +02:00
|
|
|
self.editor.saveNow(lambda: self._addTags(tags, label, prompt, func))
|
|
|
|
|
|
|
|
def _addTags(self, tags, label, prompt, func):
|
2012-12-21 08:51:59 +01:00
|
|
|
if prompt is None:
|
2020-11-17 08:42:43 +01:00
|
|
|
prompt = tr(TR.BROWSING_ENTER_TAGS_TO_ADD)
|
2012-12-21 08:51:59 +01:00
|
|
|
if tags is None:
|
|
|
|
(tags, r) = getTag(self, self.col, prompt)
|
|
|
|
else:
|
|
|
|
r = True
|
|
|
|
if not r:
|
|
|
|
return
|
|
|
|
if func is None:
|
|
|
|
func = self.col.tags.bulkAdd
|
|
|
|
if label is None:
|
2020-11-17 08:42:43 +01:00
|
|
|
label = tr(TR.BROWSING_ADD_TAGS)
|
2012-12-21 08:51:59 +01:00
|
|
|
if label:
|
|
|
|
self.mw.checkpoint(label)
|
|
|
|
self.model.beginReset()
|
|
|
|
func(self.selectedNotes(), tags)
|
|
|
|
self.model.endReset()
|
2020-08-16 18:49:51 +02:00
|
|
|
self.mw.requireReset(reason=ResetReason.BrowserAddTags, context=self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def deleteTags(self, tags=None, label=None):
|
|
|
|
if label is None:
|
2020-11-17 08:42:43 +01:00
|
|
|
label = tr(TR.BROWSING_DELETE_TAGS)
|
2019-12-23 01:34:10 +01:00
|
|
|
self.addTags(
|
2020-11-17 08:42:43 +01:00
|
|
|
tags,
|
|
|
|
label,
|
|
|
|
tr(TR.BROWSING_ENTER_TAGS_TO_DELETE),
|
|
|
|
func=self.col.tags.bulkRem,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2017-08-11 06:40:51 +02:00
|
|
|
def clearUnusedTags(self):
|
|
|
|
self.editor.saveNow(self._clearUnusedTags)
|
|
|
|
|
|
|
|
def _clearUnusedTags(self):
|
2021-01-04 05:13:20 +01:00
|
|
|
def on_done(fut: Future):
|
|
|
|
fut.result()
|
|
|
|
self.on_tag_list_update()
|
|
|
|
|
|
|
|
self.mw.taskman.run_in_background(self.col.tags.registerNotes, on_done)
|
2017-08-11 06:40:51 +02:00
|
|
|
|
2017-08-12 08:08:10 +02:00
|
|
|
# Suspending
|
2012-12-21 08:51:59 +01:00
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def isSuspended(self):
|
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
|
|
|
|
2016-07-14 12:23:44 +02:00
|
|
|
def onSuspend(self):
|
|
|
|
self.editor.saveNow(self._onSuspend)
|
|
|
|
|
|
|
|
def _onSuspend(self):
|
|
|
|
sus = not self.isSuspended()
|
2012-12-21 08:51:59 +01:00
|
|
|
c = self.selectedCards()
|
|
|
|
if sus:
|
2020-08-31 08:14:04 +02:00
|
|
|
self.col.sched.suspend_cards(c)
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2020-08-31 08:14:04 +02:00
|
|
|
self.col.sched.unsuspend_cards(c)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model.reset()
|
2020-08-16 18:49:51 +02:00
|
|
|
self.mw.requireReset(reason=ResetReason.BrowserSuspend, context=self)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-10 04:15:10 +01:00
|
|
|
# Exporting
|
|
|
|
######################################################################
|
|
|
|
|
2020-02-11 23:12:09 +01:00
|
|
|
def _on_export_notes(self):
|
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
|
|
|
|
2017-08-12 08:08:10 +02:00
|
|
|
def onSetFlag(self, n):
|
2019-12-06 05:23:54 +01:00
|
|
|
if not self.card:
|
|
|
|
return
|
2020-09-27 04:31:25 +02:00
|
|
|
self.editor.saveNow(lambda: self._on_set_flag(n))
|
|
|
|
|
|
|
|
def _on_set_flag(self, n: int):
|
2018-11-12 02:11:53 +01:00
|
|
|
# flag needs toggling off?
|
|
|
|
if n == self.card.userFlag():
|
|
|
|
n = 0
|
2017-08-12 08:08:10 +02:00
|
|
|
self.col.setUserFlag(n, self.selectedCards())
|
|
|
|
self.model.reset()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2018-11-12 03:10:50 +01:00
|
|
|
def _updateFlagsMenu(self):
|
|
|
|
flag = self.card and self.card.userFlag()
|
|
|
|
flag = flag or 0
|
|
|
|
|
|
|
|
f = self.form
|
2019-12-23 01:34:10 +01:00
|
|
|
flagActions = [
|
|
|
|
f.actionRed_Flag,
|
|
|
|
f.actionOrange_Flag,
|
|
|
|
f.actionGreen_Flag,
|
|
|
|
f.actionBlue_Flag,
|
|
|
|
]
|
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
|
|
|
|
2017-08-16 12:30:29 +02:00
|
|
|
def onMark(self, mark=None):
|
|
|
|
if mark is None:
|
|
|
|
mark = not self.isMarked()
|
|
|
|
if mark:
|
|
|
|
self.addTags(tags="marked", label=False)
|
|
|
|
else:
|
|
|
|
self.deleteTags(tags="marked", label=False)
|
|
|
|
|
|
|
|
def isMarked(self):
|
2020-03-23 18:16:39 +01:00
|
|
|
return bool(self.card and self.card.note().hasTag("Marked"))
|
2017-08-16 12:30:29 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Repositioning
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def reposition(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
self.editor.saveNow(self._reposition)
|
|
|
|
|
|
|
|
def _reposition(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
cids = self.selectedCards()
|
|
|
|
cids2 = self.col.db.list(
|
2020-01-31 09:58:03 +01:00
|
|
|
f"select id from cards where type = {CARD_TYPE_NEW} and id in "
|
|
|
|
+ ids2str(cids)
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
if not cids2:
|
2020-11-17 08:42:43 +01:00
|
|
|
return showInfo(tr(TR.BROWSING_ONLY_NEW_CARDS_CAN_BE_REPOSITIONED))
|
2012-12-21 08:51:59 +01:00
|
|
|
d = QDialog(self)
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(d)
|
2012-12-21 08:51:59 +01:00
|
|
|
d.setWindowModality(Qt.WindowModal)
|
|
|
|
frm = aqt.forms.reposition.Ui_Dialog()
|
|
|
|
frm.setupUi(d)
|
|
|
|
(pmin, pmax) = self.col.db.first(
|
2020-02-02 10:19:45 +01:00
|
|
|
f"select min(due), max(due) from cards where type={CARD_TYPE_NEW} and odid=0"
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2014-01-14 07:06:22 +01:00
|
|
|
pmin = pmin or 0
|
|
|
|
pmax = pmax or 0
|
2020-11-17 12:47:47 +01:00
|
|
|
txt = tr(TR.BROWSING_QUEUE_TOP, val=pmin)
|
|
|
|
txt += "\n" + tr(TR.BROWSING_QUEUE_BOTTOM, val=pmax)
|
2012-12-21 08:51:59 +01:00
|
|
|
frm.label.setText(txt)
|
2020-11-03 02:07:53 +01:00
|
|
|
frm.start.selectAll()
|
2012-12-21 08:51:59 +01:00
|
|
|
if not d.exec_():
|
|
|
|
return
|
|
|
|
self.model.beginReset()
|
2020-11-17 08:42:43 +01:00
|
|
|
self.mw.checkpoint(tr(TR.ACTIONS_REPOSITION))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.col.sched.sortCards(
|
2019-12-23 01:34:10 +01:00
|
|
|
cids,
|
|
|
|
start=frm.start.value(),
|
|
|
|
step=frm.step.value(),
|
|
|
|
shuffle=frm.randomize.isChecked(),
|
|
|
|
shift=frm.shift.isChecked(),
|
|
|
|
)
|
2016-07-14 12:23:44 +02:00
|
|
|
self.search()
|
2020-08-16 18:49:51 +02:00
|
|
|
self.mw.requireReset(reason=ResetReason.BrowserReposition, context=self)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model.endReset()
|
|
|
|
|
|
|
|
# Rescheduling
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def reschedule(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
self.editor.saveNow(self._reschedule)
|
|
|
|
|
|
|
|
def _reschedule(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
d = QDialog(self)
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(d)
|
2012-12-21 08:51:59 +01:00
|
|
|
d.setWindowModality(Qt.WindowModal)
|
|
|
|
frm = aqt.forms.reschedule.Ui_Dialog()
|
|
|
|
frm.setupUi(d)
|
|
|
|
if not d.exec_():
|
|
|
|
return
|
|
|
|
self.model.beginReset()
|
2020-11-17 08:42:43 +01:00
|
|
|
self.mw.checkpoint(tr(TR.BROWSING_RESCHEDULE))
|
2012-12-21 08:51:59 +01:00
|
|
|
if frm.asNew.isChecked():
|
|
|
|
self.col.sched.forgetCards(self.selectedCards())
|
|
|
|
else:
|
2013-05-17 09:27:58 +02:00
|
|
|
fmin = frm.min.value()
|
|
|
|
fmax = frm.max.value()
|
|
|
|
fmax = max(fmin, fmax)
|
2019-12-23 01:34:10 +01:00
|
|
|
self.col.sched.reschedCards(self.selectedCards(), fmin, fmax)
|
2016-07-14 12:23:44 +02:00
|
|
|
self.search()
|
2020-08-16 18:49:51 +02:00
|
|
|
self.mw.requireReset(reason=ResetReason.BrowserReschedule, context=self)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model.endReset()
|
|
|
|
|
|
|
|
# Edit: selection
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def selectNotes(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
self.editor.saveNow(self._selectNotes)
|
|
|
|
|
|
|
|
def _selectNotes(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
nids = self.selectedNotes()
|
|
|
|
# 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
|
|
|
|
|
|
|
search = "nid:" + ",".join([str(x) for x in nids])
|
|
|
|
self.search_for(search)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
tv.selectAll()
|
|
|
|
|
|
|
|
def invertSelection(self):
|
|
|
|
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)
|
|
|
|
gui_hooks.state_did_reset.append(self.onReset)
|
2020-01-15 08:45:35 +01:00
|
|
|
gui_hooks.editor_did_fire_typing_timer.append(self.refreshCurrentCard)
|
|
|
|
gui_hooks.editor_did_load_note.append(self.onLoadNote)
|
2020-01-15 22:53:12 +01:00
|
|
|
gui_hooks.editor_did_unfocus_field.append(self.on_unfocus_field)
|
2020-05-22 02:47:14 +02:00
|
|
|
gui_hooks.sidebar_should_refresh_decks.append(self.on_item_added)
|
|
|
|
gui_hooks.sidebar_should_refresh_notetypes.append(self.on_item_added)
|
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)
|
|
|
|
gui_hooks.state_did_reset.remove(self.onReset)
|
2020-01-15 08:45:35 +01:00
|
|
|
gui_hooks.editor_did_fire_typing_timer.remove(self.refreshCurrentCard)
|
|
|
|
gui_hooks.editor_did_load_note.remove(self.onLoadNote)
|
2020-01-15 22:53:12 +01:00
|
|
|
gui_hooks.editor_did_unfocus_field.remove(self.on_unfocus_field)
|
2020-05-22 02:47:14 +02:00
|
|
|
gui_hooks.sidebar_should_refresh_decks.remove(self.on_item_added)
|
|
|
|
gui_hooks.sidebar_should_refresh_notetypes.remove(self.on_item_added)
|
2020-01-15 22:53:12 +01:00
|
|
|
|
2020-01-15 23:53:28 +01:00
|
|
|
def on_unfocus_field(self, changed: bool, note: Note, field_idx: int) -> None:
|
2020-01-15 22:53:12 +01:00
|
|
|
self.refreshCurrentCard(note)
|
|
|
|
|
|
|
|
# covers the tag, note and deck case
|
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
|
|
|
|
2020-04-03 11:30:42 +02:00
|
|
|
def on_tag_list_update(self):
|
2021-01-23 10:59:12 +01:00
|
|
|
self.sidebar.refresh()
|
2020-04-03 11:30:42 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def onUndoState(self, on):
|
|
|
|
self.form.actionUndo.setEnabled(on)
|
|
|
|
if on:
|
|
|
|
self.form.actionUndo.setText(self.mw.form.actionUndo.text())
|
|
|
|
|
|
|
|
# Edit: replacing
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def onFindReplace(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
self.editor.saveNow(self._onFindReplace)
|
|
|
|
|
2020-05-05 14:14:12 +02:00
|
|
|
def _onFindReplace(self) -> None:
|
|
|
|
nids = self.selectedNotes()
|
|
|
|
if not nids:
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
|
|
|
import anki.find
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-05-05 14:14:12 +02:00
|
|
|
def find():
|
|
|
|
return anki.find.fieldNamesForNotes(self.mw.col, nids)
|
|
|
|
|
|
|
|
def on_done(fut):
|
|
|
|
self._on_find_replace_diag(fut.result(), nids)
|
|
|
|
|
|
|
|
self.mw.taskman.with_progress(find, on_done, self)
|
|
|
|
|
|
|
|
def _on_find_replace_diag(self, fields: List[str], nids: List[int]) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
d = QDialog(self)
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(d)
|
2012-12-21 08:51:59 +01:00
|
|
|
frm = aqt.forms.findreplace.Ui_Dialog()
|
|
|
|
frm.setupUi(d)
|
|
|
|
d.setWindowModality(Qt.WindowModal)
|
2020-05-31 05:04:21 +02:00
|
|
|
|
2020-05-31 05:39:00 +02:00
|
|
|
combo = "BrowserFindAndReplace"
|
2020-06-05 03:39:53 +02:00
|
|
|
findhistory = restore_combo_history(frm.find, combo + "Find")
|
2020-08-09 04:10:34 +02:00
|
|
|
frm.find.completer().setCaseSensitivity(True)
|
2020-06-05 03:39:53 +02:00
|
|
|
replacehistory = restore_combo_history(frm.replace, combo + "Replace")
|
2020-08-09 04:10:34 +02:00
|
|
|
frm.replace.completer().setCaseSensitivity(True)
|
2020-05-31 05:18:51 +02:00
|
|
|
|
2020-06-05 03:39:53 +02:00
|
|
|
restore_is_checked(frm.re, combo + "Regex")
|
|
|
|
restore_is_checked(frm.ignoreCase, combo + "ignoreCase")
|
2020-05-31 05:57:11 +02:00
|
|
|
|
2020-05-31 05:39:00 +02:00
|
|
|
frm.find.setFocus()
|
2020-11-17 08:42:43 +01:00
|
|
|
allfields = [tr(TR.BROWSING_ALL_FIELDS)] + fields
|
2020-05-31 21:52:58 +02:00
|
|
|
frm.field.addItems(allfields)
|
2020-06-08 20:54:20 +02:00
|
|
|
restore_combo_index_for_session(frm.field, allfields, combo + "Field")
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(frm.buttonBox.helpRequested, self.onFindReplaceHelp)
|
2014-06-18 20:47:45 +02:00
|
|
|
restoreGeom(d, "findreplace")
|
|
|
|
r = d.exec_()
|
|
|
|
saveGeom(d, "findreplace")
|
|
|
|
if not r:
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2020-05-31 21:52:58 +02:00
|
|
|
|
2020-06-08 20:54:20 +02:00
|
|
|
save_combo_index_for_session(frm.field, combo + "Field")
|
2012-12-21 08:51:59 +01:00
|
|
|
if frm.field.currentIndex() == 0:
|
|
|
|
field = None
|
|
|
|
else:
|
2019-12-23 01:34:10 +01:00
|
|
|
field = fields[frm.field.currentIndex() - 1]
|
2020-05-05 14:14:12 +02:00
|
|
|
|
2020-06-05 03:39:53 +02:00
|
|
|
search = save_combo_history(frm.find, findhistory, combo + "Find")
|
|
|
|
replace = save_combo_history(frm.replace, replacehistory, combo + "Replace")
|
2020-05-31 05:18:51 +02:00
|
|
|
|
2020-05-05 14:14:12 +02:00
|
|
|
regex = frm.re.isChecked()
|
|
|
|
nocase = frm.ignoreCase.isChecked()
|
|
|
|
|
2020-06-05 03:39:53 +02:00
|
|
|
save_is_checked(frm.re, combo + "Regex")
|
|
|
|
save_is_checked(frm.ignoreCase, combo + "ignoreCase")
|
2020-05-31 05:57:11 +02:00
|
|
|
|
2020-11-17 08:42:43 +01:00
|
|
|
self.mw.checkpoint(tr(TR.BROWSING_FIND_AND_REPLACE))
|
2020-05-05 14:14:12 +02:00
|
|
|
# starts progress dialog as well
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model.beginReset()
|
2020-05-05 14:14:12 +02:00
|
|
|
|
|
|
|
def do_search():
|
2020-05-06 01:25:23 +02:00
|
|
|
return self.col.find_and_replace(
|
|
|
|
nids, search, replace, regex, field, nocase
|
|
|
|
)
|
2020-05-05 14:14:12 +02:00
|
|
|
|
|
|
|
def on_done(fut):
|
2016-07-14 12:23:44 +02:00
|
|
|
self.search()
|
2020-08-16 18:49:51 +02:00
|
|
|
self.mw.requireReset(reason=ResetReason.BrowserFindReplace, context=self)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.model.endReset()
|
2020-05-05 14:14:12 +02:00
|
|
|
|
|
|
|
total = len(nids)
|
|
|
|
try:
|
|
|
|
changed = fut.result()
|
|
|
|
except InvalidInput as e:
|
2021-01-16 06:37:40 +01:00
|
|
|
show_invalid_search_error(e)
|
2020-05-05 14:14:12 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
showInfo(
|
|
|
|
tr(TR.FINDREPLACE_NOTES_UPDATED, changed=changed, total=total),
|
|
|
|
parent=self,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2020-05-05 14:14:12 +02:00
|
|
|
|
|
|
|
self.mw.taskman.run_in_background(do_search, on_done)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def onFindReplaceHelp(self):
|
2021-01-25 14:45:47 +01:00
|
|
|
openHelp(HelpPage.BROWSING_FIND_AND_REPLACE)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Edit: finding dupes
|
|
|
|
######################################################################
|
|
|
|
|
2021-01-13 11:58:59 +01:00
|
|
|
# filter called by the editor
|
|
|
|
def search_dupe(self, mid: int, text: str):
|
|
|
|
self.form.searchEdit.lineEdit().setText(
|
2021-01-28 11:17:24 +01:00
|
|
|
self.col.search_string(dupe=(mid, text))
|
2021-01-13 11:58:59 +01:00
|
|
|
)
|
|
|
|
self.onSearchActivated()
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def onFindDupes(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
self.editor.saveNow(self._onFindDupes)
|
|
|
|
|
|
|
|
def _onFindDupes(self):
|
2020-02-11 23:28:21 +01:00
|
|
|
d = QDialog(self)
|
2016-07-04 05:22:35 +02:00
|
|
|
self.mw.setupDialogGC(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
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def onFin(code):
|
|
|
|
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
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def onClick():
|
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()
|
|
|
|
|
2020-02-12 22:00:13 +01:00
|
|
|
def duplicatesReport(self, web, fname, search, frm, web_context):
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw.progress.start()
|
|
|
|
res = self.mw.col.findDupes(fname, search)
|
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>"""
|
|
|
|
% (
|
|
|
|
"nid:" + ",".join(str(id) for id in nids),
|
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()
|
|
|
|
|
2013-10-03 17:07:11 +02:00
|
|
|
def _onTagDupes(self, res):
|
|
|
|
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)
|
2020-11-17 08:42:43 +01:00
|
|
|
self.col.tags.bulkAdd(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
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def dupeLinkClicked(self, link):
|
2020-10-19 20:51:36 +02:00
|
|
|
self.search_for(link)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.onNote()
|
|
|
|
|
|
|
|
# Jumping
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def _moveCur(self, dir=None, idx=None):
|
|
|
|
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
|
|
|
|
|
|
|
def onPreviousCard(self):
|
2017-08-05 07:15:19 +02:00
|
|
|
self.focusTo = self.editor.currentField
|
2016-07-14 12:23:44 +02:00
|
|
|
self.editor.saveNow(self._onPreviousCard)
|
|
|
|
|
|
|
|
def _onPreviousCard(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
self._moveCur(QAbstractItemView.MoveUp)
|
|
|
|
|
|
|
|
def onNextCard(self):
|
2017-08-05 07:15:19 +02:00
|
|
|
self.focusTo = self.editor.currentField
|
2016-07-14 12:23:44 +02:00
|
|
|
self.editor.saveNow(self._onNextCard)
|
|
|
|
|
|
|
|
def _onNextCard(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
self._moveCur(QAbstractItemView.MoveDown)
|
|
|
|
|
|
|
|
def onFirstCard(self):
|
|
|
|
sm = self.form.tableView.selectionModel()
|
|
|
|
idx = sm.currentIndex()
|
|
|
|
self._moveCur(None, self.model.index(0, 0))
|
|
|
|
if not self.mw.app.keyboardModifiers() & Qt.ShiftModifier:
|
|
|
|
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
|
|
|
|
|
|
|
def onLastCard(self):
|
|
|
|
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))
|
2012-12-21 08:51:59 +01:00
|
|
|
if not self.mw.app.keyboardModifiers() & Qt.ShiftModifier:
|
|
|
|
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
|
|
|
|
|
|
|
def onFind(self):
|
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()
|
|
|
|
|
|
|
|
def onNote(self):
|
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
|
|
|
|
|
|
|
def onCardList(self):
|
|
|
|
self.form.tableView.setFocus()
|
|
|
|
|
|
|
|
def focusCid(self, cid):
|
|
|
|
try:
|
|
|
|
row = self.model.cards.index(cid)
|
|
|
|
except:
|
|
|
|
return
|
|
|
|
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):
|
2020-01-15 22:41:23 +01:00
|
|
|
def __init__(self, browser, nids) -> 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)
|
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()
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def setup(self):
|
|
|
|
# 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
|
|
|
|
|
|
|
|
def onReset(self):
|
|
|
|
self.modelChanged(self.browser.col.models.current())
|
|
|
|
|
|
|
|
def modelChanged(self, model):
|
|
|
|
self.targetModel = model
|
|
|
|
self.rebuildTemplateMap()
|
|
|
|
self.rebuildFieldMap()
|
|
|
|
|
|
|
|
def rebuildTemplateMap(self, key=None, attr=None):
|
|
|
|
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)
|
|
|
|
|
|
|
|
def rebuildFieldMap(self):
|
|
|
|
return self.rebuildTemplateMap(key="f", attr="flds")
|
|
|
|
|
|
|
|
def onComboChanged(self, i, cb, key):
|
|
|
|
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
|
|
|
|
|
|
|
|
def getTemplateMap(self, old=None, combos=None, new=None):
|
|
|
|
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
|
|
|
|
|
|
|
def getFieldMap(self):
|
|
|
|
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")
|
|
|
|
|
|
|
|
def reject(self):
|
|
|
|
self.cleanup()
|
|
|
|
return QDialog.reject(self)
|
|
|
|
|
|
|
|
def accept(self):
|
|
|
|
# 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
|
|
|
|
|
|
|
def onHelp(self):
|
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
|
|
|
|
|
|
|
|
def __init__(self, browser: Browser, *args, **kwargs):
|
|
|
|
super().__init__(browser, *args, **kwargs)
|
|
|
|
self.browser = browser
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(self)
|
2020-02-12 22:12:45 +01:00
|
|
|
|
|
|
|
def reject(self):
|
|
|
|
saveGeom(self, "revlog")
|
|
|
|
return QDialog.reject(self)
|