mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-09-28 00:42:19 -07:00
Don't commit on ESC, switch to enum for triggers
Took 6 seconds
This commit is contained in:
parent
fdf2b069d7
commit
f717b8687d
5 changed files with 95 additions and 66 deletions
|
|
@ -1291,13 +1291,13 @@ void TabGame::createMessageDock(bool bReplay)
|
||||||
connect(this, &TabGame::chatMessageSent, game->getGameEventHandler(), &GameEventHandler::handleChatMessageSent);
|
connect(this, &TabGame::chatMessageSent, game->getGameEventHandler(), &GameEventHandler::handleChatMessageSent);
|
||||||
mentionModel = new QStringListModel(autocompleteUserList, sayEdit);
|
mentionModel = new QStringListModel(autocompleteUserList, sayEdit);
|
||||||
mentionCompleter = createMentionCompleter(mentionModel, sayEdit);
|
mentionCompleter = createMentionCompleter(mentionModel, sayEdit);
|
||||||
sayEdit->addCompleter(mentionCompleter, "@");
|
sayEdit->addCompleter(mentionCompleter, CompleterTrigger::Mention);
|
||||||
|
|
||||||
auto *cardDatabaseModel = new CardDatabaseModel(CardDatabaseManager::getInstance(), false, sayEdit);
|
auto *cardDatabaseModel = new CardDatabaseModel(CardDatabaseManager::getInstance(), false, sayEdit);
|
||||||
auto *displayModel = new CardDatabaseDisplayModel(sayEdit);
|
auto *displayModel = new CardDatabaseDisplayModel(sayEdit);
|
||||||
displayModel->setSourceModel(cardDatabaseModel);
|
displayModel->setSourceModel(cardDatabaseModel);
|
||||||
const CardCompleterSetup cardSetup = createCardCompleter(displayModel, sayEdit);
|
const CardCompleterSetup cardSetup = createCardCompleter(displayModel, sayEdit);
|
||||||
sayEdit->addCompleter(cardSetup.completer, "[[");
|
sayEdit->addCompleter(cardSetup.completer, CompleterTrigger::Card);
|
||||||
|
|
||||||
connect(sayEdit, &LineEditCompleter::cardPartialChanged, this, [this, cardSetup](const QString &text) {
|
connect(sayEdit, &LineEditCompleter::cardPartialChanged, this, [this, cardSetup](const QString &text) {
|
||||||
cardSetup.searchModel->updateSearchResults(text);
|
cardSetup.searchModel->updateSearchResults(text);
|
||||||
|
|
|
||||||
|
|
@ -147,13 +147,13 @@ TabRoom::TabRoom(TabSupervisor *_tabSupervisor,
|
||||||
|
|
||||||
mentionModel = new QStringListModel(autocompleteUserList, sayEdit);
|
mentionModel = new QStringListModel(autocompleteUserList, sayEdit);
|
||||||
mentionCompleter = createMentionCompleter(mentionModel, sayEdit);
|
mentionCompleter = createMentionCompleter(mentionModel, sayEdit);
|
||||||
sayEdit->addCompleter(mentionCompleter, "@");
|
sayEdit->addCompleter(mentionCompleter, CompleterTrigger::Mention);
|
||||||
|
|
||||||
auto *cardDatabaseModel = new CardDatabaseModel(CardDatabaseManager::getInstance(), false, sayEdit);
|
auto *cardDatabaseModel = new CardDatabaseModel(CardDatabaseManager::getInstance(), false, sayEdit);
|
||||||
auto *displayModel = new CardDatabaseDisplayModel(sayEdit);
|
auto *displayModel = new CardDatabaseDisplayModel(sayEdit);
|
||||||
displayModel->setSourceModel(cardDatabaseModel);
|
displayModel->setSourceModel(cardDatabaseModel);
|
||||||
const CardCompleterSetup cardSetup = createCardCompleter(displayModel, sayEdit);
|
const CardCompleterSetup cardSetup = createCardCompleter(displayModel, sayEdit);
|
||||||
sayEdit->addCompleter(cardSetup.completer, "[[");
|
sayEdit->addCompleter(cardSetup.completer, CompleterTrigger::Card);
|
||||||
|
|
||||||
connect(sayEdit, &LineEditCompleter::cardPartialChanged, this, [this, cardSetup](const QString &text) {
|
connect(sayEdit, &LineEditCompleter::cardPartialChanged, this, [this, cardSetup](const QString &text) {
|
||||||
cardSetup.searchModel->updateSearchResults(text);
|
cardSetup.searchModel->updateSearchResults(text);
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ struct ManaColor
|
||||||
QColor text;
|
QColor text;
|
||||||
};
|
};
|
||||||
|
|
||||||
ManaColor manaColour(QChar symbol)
|
ManaColor manaColor(QChar symbol)
|
||||||
{
|
{
|
||||||
switch (symbol.unicode()) {
|
switch (symbol.unicode()) {
|
||||||
case 'W':
|
case 'W':
|
||||||
|
|
@ -74,7 +74,7 @@ QColor CardCompleterDelegate::accentForColors(const QString &colors)
|
||||||
return QColor(100, 115, 135);
|
return QColor(100, 115, 135);
|
||||||
}
|
}
|
||||||
|
|
||||||
return manaColour(*seen.begin()).fill;
|
return manaColor(*seen.begin()).fill;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
@ -144,7 +144,7 @@ void CardCompleterDelegate::drawManaSymbol(QPainter *p, QPoint centre, const QSt
|
||||||
const QString label = isNumeric ? QString::number(numVal) : symbol;
|
const QString label = isNumeric ? QString::number(numVal) : symbol;
|
||||||
|
|
||||||
const ManaColor mc =
|
const ManaColor mc =
|
||||||
(symbol.length() == 1 && QString("WUBRG").contains(symbol)) ? manaColour(symbol[0]) : manaColour(QChar('X'));
|
(symbol.length() == 1 && QString("WUBRG").contains(symbol)) ? manaColor(symbol[0]) : manaColor(QChar('X'));
|
||||||
|
|
||||||
QPainterPath circle;
|
QPainterPath circle;
|
||||||
circle.addEllipse(pip);
|
circle.addEllipse(pip);
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ LineEditCompleter::LineEditCompleter(QWidget *parent) : LineEditUnfocusable(pare
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void LineEditCompleter::addCompleter(QCompleter *c, const QString &trigger)
|
void LineEditCompleter::addCompleter(QCompleter *c, CompleterTrigger trigger)
|
||||||
{
|
{
|
||||||
c->setWidget(this);
|
c->setWidget(this);
|
||||||
c->setCompletionMode(QCompleter::PopupCompletion);
|
c->setCompletionMode(QCompleter::PopupCompletion);
|
||||||
|
|
@ -40,7 +40,14 @@ void LineEditCompleter::focusOutEvent(QFocusEvent *e)
|
||||||
{
|
{
|
||||||
LineEditUnfocusable::focusOutEvent(e);
|
LineEditUnfocusable::focusOutEvent(e);
|
||||||
|
|
||||||
// Commit the highlighted completion so that tabbing away still applies it
|
// Only commit the highlighted completion when focus moves away via Tab.
|
||||||
|
// Other focus losses (e.g. the unfocus shortcut / Escape) must simply close
|
||||||
|
// the popup without inserting anything.
|
||||||
|
if (e->reason() != Qt::TabFocusReason) {
|
||||||
|
hideCompleterPopups();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (auto &info : completers) {
|
for (auto &info : completers) {
|
||||||
if (!info.completer->popup()->isVisible()) {
|
if (!info.completer->popup()->isVisible()) {
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -71,22 +78,32 @@ void LineEditCompleter::keyPressEvent(QKeyEvent *event)
|
||||||
QString prefix;
|
QString prefix;
|
||||||
|
|
||||||
for (auto &info : completers) {
|
for (auto &info : completers) {
|
||||||
if (info.trigger == "@") {
|
bool triggered = false;
|
||||||
int triggerPos = textValue.lastIndexOf("@", cursorPos - 1);
|
switch (info.trigger) {
|
||||||
if (triggerPos != -1 && (triggerPos == 0 || textValue[triggerPos - 1].isSpace())) {
|
case CompleterTrigger::Mention: {
|
||||||
active = &info;
|
int triggerPos = textValue.lastIndexOf("@", cursorPos - 1);
|
||||||
prefix = textValue.mid(triggerPos + 1, cursorPos - (triggerPos + 1));
|
if (triggerPos != -1 && (triggerPos == 0 || textValue[triggerPos - 1].isSpace())) {
|
||||||
|
triggered = true;
|
||||||
|
// Keep the "@" so the prefix matches the "@"-prefixed mention model entries.
|
||||||
|
prefix = textValue.mid(triggerPos, cursorPos - triggerPos);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if (info.trigger == "[[") {
|
case CompleterTrigger::Card: {
|
||||||
int triggerPos = textValue.lastIndexOf("[[", cursorPos - 1);
|
int triggerPos = textValue.lastIndexOf("[[", cursorPos - 1);
|
||||||
int closePos = textValue.indexOf("]]", triggerPos + 2);
|
int closePos = textValue.indexOf("]]", triggerPos + 2);
|
||||||
if (triggerPos != -1 && (closePos == -1 || closePos >= cursorPos)) {
|
if (triggerPos != -1 && (closePos == -1 || closePos >= cursorPos)) {
|
||||||
active = &info;
|
triggered = true;
|
||||||
prefix = textValue.mid(triggerPos + 2, cursorPos - (triggerPos + 2));
|
prefix = textValue.mid(triggerPos + 2, cursorPos - (triggerPos + 2));
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (triggered) {
|
||||||
|
active = &info;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!active) {
|
if (!active) {
|
||||||
|
|
@ -98,9 +115,12 @@ void LineEditCompleter::keyPressEvent(QKeyEvent *event)
|
||||||
|
|
||||||
active->completer->setCompletionPrefix(prefix);
|
active->completer->setCompletionPrefix(prefix);
|
||||||
|
|
||||||
if (active->trigger == "[[") {
|
switch (active->trigger) {
|
||||||
emit cardPartialChanged(prefix);
|
case CompleterTrigger::Card:
|
||||||
return;
|
emit cardPartialChanged(prefix);
|
||||||
|
return;
|
||||||
|
case CompleterTrigger::Mention:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
active->completer->complete();
|
active->completer->complete();
|
||||||
|
|
@ -126,51 +146,54 @@ void LineEditCompleter::insertCompletion(QCompleter *completer, const QString &c
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (info.trigger == "[[") {
|
switch (info.trigger) {
|
||||||
int triggerPos = t.lastIndexOf("[[", pos - 1);
|
case CompleterTrigger::Card: {
|
||||||
if (triggerPos == -1) {
|
int triggerPos = t.lastIndexOf("[[", pos - 1);
|
||||||
|
if (triggerPos == -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If an earlier "[[" is still open it also encloses the cursor, so
|
||||||
|
// replace from its start. Otherwise completing in text such as
|
||||||
|
// "[[Opt[[Amok" would leave a stray "[[" behind.
|
||||||
|
int startPos = triggerPos;
|
||||||
|
for (int searchFrom = triggerPos; searchFrom > 0;) {
|
||||||
|
const int earlier = t.lastIndexOf("[[", searchFrom - 1);
|
||||||
|
if (earlier == -1) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
const int earlierClose = t.indexOf("]]", earlier + 2);
|
||||||
|
if (earlierClose != -1 && earlierClose < pos) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
startPos = earlier;
|
||||||
|
searchFrom = earlier;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the cursor sits inside an already-closed [[...]] pair, replace
|
||||||
|
// the whole construct instead of leaving a duplicate closing bracket
|
||||||
|
// behind.
|
||||||
|
int insertEnd = pos;
|
||||||
|
const int closePos = t.indexOf("]]", startPos + 2);
|
||||||
|
if (closePos != -1 && closePos >= pos) {
|
||||||
|
insertEnd = closePos + 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString after = t.mid(insertEnd);
|
||||||
|
QString replaced = t.left(startPos + 2) + completion + "]] ";
|
||||||
|
setText(replaced + after);
|
||||||
|
setCursorPosition(replaced.length());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
case CompleterTrigger::Mention: {
|
||||||
// If an earlier "[[" is still open it also encloses the cursor, so
|
int triggerPos = t.lastIndexOf("@", pos - 1);
|
||||||
// replace from its start. Otherwise completing in text such as
|
if (triggerPos == -1) {
|
||||||
// "[[Opt[[Amok" would leave a stray "[[" behind.
|
return;
|
||||||
int startPos = triggerPos;
|
|
||||||
for (int searchFrom = triggerPos; searchFrom > 0;) {
|
|
||||||
const int earlier = t.lastIndexOf("[[", searchFrom - 1);
|
|
||||||
if (earlier == -1) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
const int earlierClose = t.indexOf("]]", earlier + 2);
|
setText(t.replace(triggerPos, pos - triggerPos, completion + " "));
|
||||||
if (earlierClose != -1 && earlierClose < pos) {
|
setCursorPosition(triggerPos + completion.length() + 1);
|
||||||
break;
|
return;
|
||||||
}
|
|
||||||
startPos = earlier;
|
|
||||||
searchFrom = earlier;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the cursor sits inside an already-closed [[...]] pair, replace
|
|
||||||
// the whole construct instead of leaving a duplicate closing bracket
|
|
||||||
// behind.
|
|
||||||
int insertEnd = pos;
|
|
||||||
const int closePos = t.indexOf("]]", startPos + 2);
|
|
||||||
if (closePos != -1 && closePos >= pos) {
|
|
||||||
insertEnd = closePos + 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString after = t.mid(insertEnd);
|
|
||||||
QString replaced = t.left(startPos + 2) + completion + "]] ";
|
|
||||||
setText(replaced + after);
|
|
||||||
setCursorPosition(replaced.length());
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int triggerPos = t.lastIndexOf("@", pos - 1);
|
|
||||||
if (triggerPos == -1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
setText(t.replace(triggerPos, pos - triggerPos, completion + " "));
|
|
||||||
setCursorPosition(triggerPos + completion.length() + 1);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,10 +17,16 @@
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
|
|
||||||
|
enum class CompleterTrigger
|
||||||
|
{
|
||||||
|
Mention, // "@" prefix, replaced in place with a trailing space.
|
||||||
|
Card // "[[" prefix, replaced with the completion plus a closing "]] ".
|
||||||
|
};
|
||||||
|
|
||||||
struct CompleterInfo
|
struct CompleterInfo
|
||||||
{
|
{
|
||||||
QCompleter *completer;
|
QCompleter *completer;
|
||||||
QString trigger; // e.g., "@" or "[["
|
CompleterTrigger trigger;
|
||||||
};
|
};
|
||||||
|
|
||||||
class LineEditCompleter : public LineEditUnfocusable
|
class LineEditCompleter : public LineEditUnfocusable
|
||||||
|
|
@ -43,7 +49,7 @@ protected:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit LineEditCompleter(QWidget *parent = nullptr);
|
explicit LineEditCompleter(QWidget *parent = nullptr);
|
||||||
void addCompleter(QCompleter *c, const QString &trigger);
|
void addCompleter(QCompleter *c, CompleterTrigger trigger);
|
||||||
|
|
||||||
bool hasVisibleCompleterPopup() const;
|
bool hasVisibleCompleterPopup() const;
|
||||||
void hideCompleterPopups();
|
void hideCompleterPopups();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue