mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-08 19:13:25 -08:00
Compare commits
3 commits
1e1f79db0d
...
efc7e7a079
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
efc7e7a079 | ||
|
|
be2c16eca5 | ||
|
|
b8e8f2c738 |
5 changed files with 136 additions and 27 deletions
|
|
@ -6,5 +6,4 @@ Name=Cockatrice
|
|||
Exec=cockatrice %U
|
||||
Icon=cockatrice
|
||||
Categories=Game;CardGame;
|
||||
MimeType=application/x-cockatrice;
|
||||
X-Scheme-Handler/cockatrice=true
|
||||
MimeType=application/x-cockatrice;x-scheme-handler/cockatrice;
|
||||
|
|
|
|||
|
|
@ -15,13 +15,14 @@ void CardNodeFunction::SetProviderIdToPreferred::operator()(const InnerDecklistN
|
|||
{
|
||||
Q_UNUSED(node);
|
||||
PrintingInfo preferredPrinting = CardDatabaseManager::query()->getPreferredPrinting(card->getName());
|
||||
QString providerId = preferredPrinting.getUuid();
|
||||
QString setShortName = preferredPrinting.getSet()->getShortName();
|
||||
QString collectorNumber = preferredPrinting.getProperty("num");
|
||||
CardSetPtr preferredSet = preferredPrinting.getSet();
|
||||
if (!preferredSet) {
|
||||
return;
|
||||
}
|
||||
|
||||
card->setCardProviderId(providerId);
|
||||
card->setCardCollectorNumber(collectorNumber);
|
||||
card->setCardSetShortName(setShortName);
|
||||
card->setCardProviderId(preferredPrinting.getUuid());
|
||||
card->setCardCollectorNumber(preferredPrinting.getProperty("num"));
|
||||
card->setCardSetShortName(preferredSet->getShortName());
|
||||
}
|
||||
|
||||
void CardNodeFunction::ClearPrintingData::operator()(const InnerDecklistNode *node, DecklistCardNode *card) const
|
||||
|
|
|
|||
|
|
@ -150,7 +150,8 @@ bool CardCompleterStyler::handlePopupKeyPress(QKeyEvent *event)
|
|||
|
||||
if (newRow != currentRow) {
|
||||
const QModelIndex newIndex = completer->completionModel()->index(newRow, completer->completionColumn());
|
||||
popup->selectionModel()->setCurrentIndex(newIndex, QItemSelectionModel::NoUpdate);
|
||||
popup->selectionModel()->setCurrentIndex(newIndex,
|
||||
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
@ -180,7 +181,7 @@ void CardCompleterStyler::ensureClosestSelected()
|
|||
|
||||
if (currentRow != closestRow) {
|
||||
popup->selectionModel()->setCurrentIndex(completionModel->index(closestRow, completer->completionColumn()),
|
||||
QItemSelectionModel::NoUpdate);
|
||||
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,12 +3,19 @@
|
|||
#include "../../../client/settings/cache_settings.h"
|
||||
#include "card_completer_styler.h"
|
||||
|
||||
#include <QAbstractItemView>
|
||||
#include <QCompleter>
|
||||
#include <QLineEdit>
|
||||
#include <QModelIndex>
|
||||
#include <QObject>
|
||||
#include <QPainter>
|
||||
#include <QPalette>
|
||||
#include <QRegularExpression>
|
||||
#include <QString>
|
||||
#include <QStringListModel>
|
||||
#include <QStyle>
|
||||
#include <QStyleOptionViewItem>
|
||||
#include <QStyledItemDelegate>
|
||||
#include <libcockatrice/card/card_localization.h>
|
||||
#include <libcockatrice/models/database/card/card_completer_proxy_model.h>
|
||||
#include <libcockatrice/models/database/card/card_search_model.h>
|
||||
|
|
@ -17,6 +24,45 @@
|
|||
|
||||
namespace
|
||||
{
|
||||
/**
|
||||
* @brief Paints the mention popup's current row with a visible highlight.
|
||||
*
|
||||
* A completer popup is a transient window that is usually not the application's
|
||||
* active window, so the style paints a selected row using the palette's
|
||||
* Inactive highlight group. Most themes set that colour close to the popup
|
||||
* background, which hides the current completion entirely. Mirror the active
|
||||
* highlight into the inactive group so the selection stays visible.
|
||||
*/
|
||||
class MentionCompleterDelegate : public QStyledItemDelegate
|
||||
{
|
||||
public:
|
||||
explicit MentionCompleterDelegate(QAbstractItemView *view) : QStyledItemDelegate(view), view(view)
|
||||
{
|
||||
}
|
||||
|
||||
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
|
||||
{
|
||||
QStyleOptionViewItem opt = option;
|
||||
opt.showDecorationSelected = true;
|
||||
|
||||
if (view->currentIndex() == index) {
|
||||
opt.state |= QStyle::State_HasFocus;
|
||||
}
|
||||
|
||||
if (opt.state & QStyle::State_Selected) {
|
||||
opt.palette.setColor(QPalette::Inactive, QPalette::Highlight,
|
||||
opt.palette.color(QPalette::Active, QPalette::Highlight));
|
||||
opt.palette.setColor(QPalette::Inactive, QPalette::HighlightedText,
|
||||
opt.palette.color(QPalette::Active, QPalette::HighlightedText));
|
||||
}
|
||||
|
||||
QStyledItemDelegate::paint(painter, opt, index);
|
||||
}
|
||||
|
||||
private:
|
||||
QAbstractItemView *view;
|
||||
};
|
||||
|
||||
void applyCardSearchLanguage(CardSearchModel *searchModel)
|
||||
{
|
||||
const CardsDisplaySettings &cardsDisplay = SettingsCache::instance().cardsDisplay();
|
||||
|
|
@ -69,5 +115,11 @@ QCompleter *createMentionCompleter(QStringListModel *model, QObject *parent)
|
|||
completer->setCaseSensitivity(Qt::CaseInsensitive);
|
||||
completer->setMaxVisibleItems(5);
|
||||
completer->setFilterMode(Qt::MatchStartsWith);
|
||||
|
||||
// Force the popup to be created so the highlight delegate can be installed;
|
||||
// otherwise QCompleter's own delegate hides the selection on most themes.
|
||||
QAbstractItemView *popup = completer->popup();
|
||||
popup->setItemDelegate(new MentionCompleterDelegate(popup));
|
||||
|
||||
return completer;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
#include "line_edit_completer.h"
|
||||
|
||||
#include <QAbstractItemModel>
|
||||
#include <QAbstractItemView>
|
||||
#include <QChar>
|
||||
#include <QCompleter>
|
||||
#include <QFocusEvent>
|
||||
#include <QItemSelectionModel>
|
||||
#include <QKeyEvent>
|
||||
#include <QModelIndex>
|
||||
#include <QVariant>
|
||||
|
|
@ -48,37 +50,80 @@ void LineEditCompleter::focusOutEvent(QFocusEvent *e)
|
|||
{
|
||||
LineEditUnfocusable::focusOutEvent(e);
|
||||
|
||||
// Only commit the highlighted completion when focus moves away via Tab.
|
||||
// Commit completion when focus moves away via Tab if popup is visible.
|
||||
// Other focus losses (e.g. the unfocus shortcut / Escape) must simply close
|
||||
// the popup without inserting anything.
|
||||
if (e->reason() != Qt::TabFocusReason) {
|
||||
hideCompleterPopups();
|
||||
bool popupWasVisible = false;
|
||||
for (auto &info : completers) {
|
||||
if (info.completer->popup()->isVisible()) {
|
||||
popupWasVisible = true;
|
||||
const QModelIndex currentIndex = info.completer->popup()->currentIndex();
|
||||
QString completionText;
|
||||
if (currentIndex.isValid()) {
|
||||
completionText = currentIndex.data().toString();
|
||||
} else {
|
||||
completionText = info.completer->currentCompletion();
|
||||
}
|
||||
if (!completionText.isEmpty()) {
|
||||
insertCompletion(info.completer, completionText);
|
||||
}
|
||||
}
|
||||
}
|
||||
hideCompleterPopups();
|
||||
|
||||
if (popupWasVisible && e->reason() == Qt::TabFocusReason) {
|
||||
// Refocus the edit so Tab doesn't move focus away
|
||||
// Use a queued connection to ensure focus is restored after the focus change completes
|
||||
QMetaObject::invokeMethod(this, "setFocus", Qt::QueuedConnection);
|
||||
e->accept();
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto &info : completers) {
|
||||
if (!info.completer->popup()->isVisible()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const QModelIndex currentIndex = info.completer->popup()->currentIndex();
|
||||
if (currentIndex.isValid()) {
|
||||
insertCompletion(info.completer, currentIndex.data().toString());
|
||||
}
|
||||
if (popupWasVisible) {
|
||||
return;
|
||||
}
|
||||
|
||||
hideCompleterPopups();
|
||||
}
|
||||
|
||||
void LineEditCompleter::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
LineEditUnfocusable::keyPressEvent(event);
|
||||
|
||||
if (event->key() == Qt::Key_Escape) {
|
||||
hideCompleterPopups();
|
||||
if (hasVisibleCompleterPopup()) {
|
||||
event->ignore();
|
||||
hideCompleterPopups();
|
||||
return;
|
||||
}
|
||||
LineEditUnfocusable::keyPressEvent(event);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter || event->key() == Qt::Key_Space) {
|
||||
for (auto &info : completers) {
|
||||
if (info.completer->popup()->isVisible()) {
|
||||
event->ignore();
|
||||
const QModelIndex currentIndex = info.completer->popup()->currentIndex();
|
||||
QString completionText;
|
||||
if (currentIndex.isValid()) {
|
||||
completionText = currentIndex.data().toString();
|
||||
} else {
|
||||
completionText = info.completer->currentCompletion();
|
||||
}
|
||||
if (!completionText.isEmpty()) {
|
||||
insertCompletion(info.completer, completionText);
|
||||
}
|
||||
hideCompleterPopups();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (event->key() == Qt::Key_Tab) {
|
||||
// Let Tab proceed normally; focusOutEvent will handle completion if popup is visible
|
||||
LineEditUnfocusable::keyPressEvent(event);
|
||||
return;
|
||||
}
|
||||
|
||||
LineEditUnfocusable::keyPressEvent(event);
|
||||
|
||||
QString textValue = text();
|
||||
int cursorPos = cursorPosition();
|
||||
|
||||
|
|
@ -132,6 +177,17 @@ void LineEditCompleter::keyPressEvent(QKeyEvent *event)
|
|||
}
|
||||
|
||||
active->completer->complete();
|
||||
|
||||
// The popup opens without a current row, so nothing is highlighted and there
|
||||
// is no visual indication of what Enter will insert. Select the row that
|
||||
// currentCompletion() would use so the current mention stays visible.
|
||||
auto *completionModel = active->completer->completionModel();
|
||||
const int currentRow = active->completer->currentRow();
|
||||
if (currentRow >= 0 && currentRow < completionModel->rowCount()) {
|
||||
active->completer->popup()->selectionModel()->setCurrentIndex(
|
||||
completionModel->index(currentRow, active->completer->completionColumn()),
|
||||
QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
|
||||
}
|
||||
}
|
||||
|
||||
void LineEditCompleter::insertCompletion(const QString &completion)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue