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
|
|
|
|
|
2012-12-22 01:17:10 +01:00
|
|
|
import re
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-03-31 15:00:26 +02:00
|
|
|
from aqt import gui_hooks
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
|
|
|
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
class TagEdit(QLineEdit):
|
|
|
|
|
2016-05-31 10:51:40 +02:00
|
|
|
lostFocus = pyqtSignal()
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# 0 = tags, 1 = decks
|
|
|
|
def __init__(self, parent, type=0):
|
|
|
|
QLineEdit.__init__(self, parent)
|
|
|
|
self.col = None
|
|
|
|
self.model = QStringListModel()
|
|
|
|
self.type = type
|
|
|
|
if type == 0:
|
|
|
|
self.completer = TagCompleter(self.model, parent, self)
|
|
|
|
else:
|
|
|
|
self.completer = QCompleter(self.model, parent)
|
|
|
|
self.completer.setCompletionMode(QCompleter.PopupCompletion)
|
|
|
|
self.completer.setCaseSensitivity(Qt.CaseInsensitive)
|
|
|
|
self.setCompleter(self.completer)
|
|
|
|
|
|
|
|
def setCol(self, col):
|
|
|
|
"Set the current col, updating list of available tags."
|
|
|
|
self.col = col
|
|
|
|
if self.type == 0:
|
|
|
|
l = sorted(self.col.tags.all())
|
|
|
|
else:
|
|
|
|
l = sorted(self.col.decks.allNames())
|
|
|
|
self.model.setStringList(l)
|
|
|
|
|
|
|
|
def focusInEvent(self, evt):
|
|
|
|
QLineEdit.focusInEvent(self, evt)
|
|
|
|
|
|
|
|
def keyPressEvent(self, evt):
|
2017-08-30 13:13:10 +02:00
|
|
|
if evt.key() in (Qt.Key_Up, Qt.Key_Down):
|
|
|
|
# show completer on arrow key up/down
|
|
|
|
if not self.completer.popup().isVisible():
|
|
|
|
self.showCompleter()
|
|
|
|
return
|
2019-12-23 01:34:10 +01:00
|
|
|
if evt.key() == Qt.Key_Tab and evt.modifiers() & Qt.ControlModifier:
|
2017-08-30 13:11:03 +02:00
|
|
|
# select next completion
|
2017-08-30 12:49:04 +02:00
|
|
|
if not self.completer.popup().isVisible():
|
|
|
|
self.showCompleter()
|
|
|
|
index = self.completer.currentIndex()
|
|
|
|
self.completer.popup().setCurrentIndex(index)
|
|
|
|
cur_row = index.row()
|
|
|
|
if not self.completer.setCurrentRow(cur_row + 1):
|
|
|
|
self.completer.setCurrentRow(0)
|
|
|
|
return
|
2013-05-22 05:03:30 +02:00
|
|
|
if evt.key() in (Qt.Key_Enter, Qt.Key_Return):
|
2017-08-30 13:11:03 +02:00
|
|
|
# apply first completion if no suggestion selected
|
|
|
|
selected_row = self.completer.popup().currentIndex().row()
|
|
|
|
if selected_row == -1:
|
|
|
|
self.completer.setCurrentRow(0)
|
|
|
|
index = self.completer.currentIndex()
|
|
|
|
self.completer.popup().setCurrentIndex(index)
|
2013-05-22 05:03:30 +02:00
|
|
|
self.hideCompleter()
|
2013-05-27 04:37:35 +02:00
|
|
|
QWidget.keyPressEvent(self, evt)
|
2013-05-22 05:03:30 +02:00
|
|
|
return
|
2012-12-21 08:51:59 +01:00
|
|
|
QLineEdit.keyPressEvent(self, evt)
|
|
|
|
if not evt.text():
|
|
|
|
# if it's a modifier, don't show
|
|
|
|
return
|
|
|
|
if evt.key() not in (
|
2019-12-23 01:34:10 +01:00
|
|
|
Qt.Key_Enter,
|
|
|
|
Qt.Key_Return,
|
|
|
|
Qt.Key_Escape,
|
|
|
|
Qt.Key_Space,
|
|
|
|
Qt.Key_Tab,
|
|
|
|
Qt.Key_Backspace,
|
|
|
|
Qt.Key_Delete,
|
|
|
|
):
|
2012-12-21 08:51:59 +01:00
|
|
|
self.showCompleter()
|
2020-03-31 15:00:26 +02:00
|
|
|
gui_hooks.tag_editor_received_a_key(self, evt)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def showCompleter(self):
|
|
|
|
self.completer.setCompletionPrefix(self.text())
|
|
|
|
self.completer.complete()
|
|
|
|
|
|
|
|
def focusOutEvent(self, evt):
|
|
|
|
QLineEdit.focusOutEvent(self, evt)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.lostFocus.emit()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.completer.popup().hide()
|
|
|
|
|
|
|
|
def hideCompleter(self):
|
2018-04-18 05:21:10 +02:00
|
|
|
if sip.isdeleted(self.completer):
|
|
|
|
return
|
2012-12-21 08:51:59 +01:00
|
|
|
self.completer.popup().hide()
|
|
|
|
|
|
|
|
|
2019-12-23 01:34:10 +01:00
|
|
|
class TagCompleter(QCompleter):
|
2012-12-21 08:51:59 +01:00
|
|
|
def __init__(self, model, parent, edit, *args):
|
|
|
|
QCompleter.__init__(self, model, parent)
|
|
|
|
self.tags = []
|
|
|
|
self.edit = edit
|
|
|
|
self.cursor = None
|
|
|
|
|
2016-05-31 10:51:40 +02:00
|
|
|
def splitPath(self, tags):
|
2017-08-30 14:19:15 +02:00
|
|
|
stripped_tags = tags.strip()
|
|
|
|
stripped_tags = re.sub(" +", " ", stripped_tags)
|
|
|
|
self.tags = self.edit.col.tags.split(stripped_tags)
|
2016-05-12 06:45:35 +02:00
|
|
|
self.tags.append("")
|
2012-12-21 08:51:59 +01:00
|
|
|
p = self.edit.cursorPosition()
|
2017-08-30 14:52:58 +02:00
|
|
|
if tags.endswith(" "):
|
|
|
|
self.cursor = len(self.tags) - 1
|
|
|
|
else:
|
|
|
|
self.cursor = stripped_tags.count(" ", 0, p)
|
2012-12-21 08:51:59 +01:00
|
|
|
return [self.tags[self.cursor]]
|
|
|
|
|
|
|
|
def pathFromIndex(self, idx):
|
|
|
|
if self.cursor is None:
|
|
|
|
return self.edit.text()
|
|
|
|
ret = QCompleter.pathFromIndex(self, idx)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.tags[self.cursor] = ret
|
2012-12-21 08:51:59 +01:00
|
|
|
try:
|
2016-05-12 06:45:35 +02:00
|
|
|
self.tags.remove("")
|
2012-12-21 08:51:59 +01:00
|
|
|
except ValueError:
|
|
|
|
pass
|
2019-12-20 10:19:03 +01:00
|
|
|
return " ".join(self.tags) + " "
|