Cockatrice/cockatrice/src/interface/widgets/utility/card_completer_styler.h
BruebachL bbe438b09a
[Completer] Highlight popup rows without rewriting the line edit (#7502)
* [Completer] Highlight popup rows without rewriting the line edit

Selecting a popup row with ClearAndSelect emits selectionChanged, which
QCompleter turns into highlighted() and QLineEdit turns into setText().
Since be2c16eca5 (#7466) that rewrites the typed text on every completion
reset / arrow-key press (e.g. typing "A" becomes "Axe" and cannot be
edited), regressing the 514014dc00 (#7447) fix that used NoUpdate.

Add selectWithoutRewrite(), which blocks the selection-model signals while
applying ClearAndSelect so the highlight stays visible without touching
the line edit, then syncs the preview manually. Route both the
above-popup auto-select and Up/Down navigation through it, and handle
Up/Down for below-popups too instead of falling through to Qt's
rewriting default. Enter/click acceptance via activated() is unchanged.

* [Completer] Repaint popup highlight after silent selection change

Blocking the selection model's signals also suppresses the view's own
selectionChanged/currentChanged repaints, so force a viewport update
after restoring signals. Use EnsureVisible to match Qt's normal scroll
behaviour.

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
2026-10-16 19:01:38 +02:00

70 lines
1.8 KiB
C++

/**
* @file card_completer_styler.h
* @ingroup UtilityWidgets
*/
//! \todo Document this file.
#ifndef CARD_COMPLETER_STYLER_H
#define CARD_COMPLETER_STYLER_H
#include <QModelIndex>
#include <QObject>
#include <qtmetamacros.h>
class CardInfoPictureEnlargedWidget;
class QCompleter;
class QKeyEvent;
class QPoint;
class ReversedCompleterModel;
/**
* @brief Applies styled row painting and a card-image preview to a card completer.
*
* The completer popup rows are painted by CardCompleterDelegate and the image of
* the currently selected (or hovered) row is shown beside the popup.
*/
class CardCompleterStyler : public QObject
{
Q_OBJECT
public:
/**
* @brief Styles @p completer and follows its current selection.
*
* The styler is parented to the completer so it lives exactly as long as the
* completer itself.
*/
static void apply(QCompleter *completer);
protected:
bool eventFilter(QObject *obj, QEvent *ev) override;
private slots:
void updatePreview(const QModelIndex &index);
void updatePreviewFromHover(const QPoint &pos);
void onCompletionReset();
private:
explicit CardCompleterStyler(QCompleter *completer, QObject *parent = nullptr);
~CardCompleterStyler() override;
void showPreview();
void hidePreview();
void stopPreviewFade();
void reposition();
void applyPopupWidth();
void updateOrientation();
void ensureClosestSelected();
void selectWithoutRewrite(const QModelIndex &index);
bool handlePopupKeyPress(QKeyEvent *event);
bool isPopupAboveWidget() const;
QCompleter *completer;
ReversedCompleterModel *reversedModel;
CardInfoPictureEnlargedWidget *preview;
QModelIndex previewedIndex;
bool above;
};
#endif // CARD_COMPLETER_STYLER_H