[Move refactor] Reparent orphan classes (#6236)

* Move orphaned classes to their correct parent folders.

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2025-10-09 14:15:19 +02:00 committed by GitHub
parent 1ef07309d6
commit d9c65d4ae0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
143 changed files with 171 additions and 169 deletions

View file

@ -0,0 +1,87 @@
#include "custom_line_edit.h"
#include <QKeyEvent>
#include <QLineEdit>
#include <QObject>
#include <QTreeView>
#include <QWidget>
#include <libcockatrice/settings/cache_settings.h>
#include <libcockatrice/settings/shortcuts_settings.h>
LineEditUnfocusable::LineEditUnfocusable(QWidget *parent) : QLineEdit(parent)
{
installEventFilter(this);
}
LineEditUnfocusable::LineEditUnfocusable(const QString &contents, QWidget *parent) : QLineEdit(contents, parent)
{
installEventFilter(this);
}
bool LineEditUnfocusable::isUnfocusShortcut(QKeyEvent *event)
{
QString modifier;
QString keyNoMod;
if (event->modifiers() & Qt::ShiftModifier)
modifier += "Shift+";
if (event->modifiers() & Qt::ControlModifier)
modifier += "Ctrl+";
if (event->modifiers() & Qt::AltModifier)
modifier += "Alt+";
if (event->modifiers() & Qt::MetaModifier)
modifier += "Meta+";
keyNoMod = QKeySequence(event->key()).toString();
QKeySequence key(modifier + keyNoMod);
QList<QKeySequence> unfocusShortcut = SettingsCache::instance().shortcuts().getShortcut("Player/unfocusTextBox");
for (const auto &unfocusKey : unfocusShortcut) {
if (key.matches(unfocusKey) == QKeySequence::ExactMatch)
return true;
}
return false;
}
void LineEditUnfocusable::keyPressEvent(QKeyEvent *event)
{
if (isUnfocusShortcut(event)) {
clearFocus();
return;
}
QLineEdit::keyPressEvent(event);
}
bool LineEditUnfocusable::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() == QEvent::ShortcutOverride) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (isUnfocusShortcut(keyEvent)) {
event->accept();
return true;
}
}
return QLineEdit::eventFilter(watched, event);
}
void SearchLineEdit::keyPressEvent(QKeyEvent *event)
{
// List of key events that must be handled by the card list instead of the search box
static const QVector<Qt::Key> forwardToTreeView = {Qt::Key_Up, Qt::Key_Down, Qt::Key_PageDown, Qt::Key_PageUp};
// forward only if the search text is empty
static const QVector<Qt::Key> forwardWhenEmpty = {Qt::Key_Home, Qt::Key_End};
Qt::Key key = static_cast<Qt::Key>(event->key());
if (treeView) {
if (forwardToTreeView.contains(key))
QCoreApplication::sendEvent(treeView, event);
if (text().isEmpty() && forwardWhenEmpty.contains(key))
QCoreApplication::sendEvent(treeView, event);
}
LineEditUnfocusable::keyPressEvent(event);
}

View file

@ -0,0 +1,52 @@
/**
* @file custom_line_edit.h
* @ingroup UI
* @brief TODO: Document this.
*/
#ifndef CUSTOMLINEEDIT_H
#define CUSTOMLINEEDIT_H
#include <QLineEdit>
class QTreeView;
class QKeyEvent;
class QWidget;
class QString;
// Should be used when the there is a risk of conflict between line editor
// shortcuts and other shortcuts
class LineEditUnfocusable : public QLineEdit
{
Q_OBJECT
public:
explicit LineEditUnfocusable(QWidget *parent = nullptr);
explicit LineEditUnfocusable(const QString &contents, QWidget *parent = nullptr);
private:
static bool isUnfocusShortcut(QKeyEvent *key);
protected:
void keyPressEvent(QKeyEvent *event) override;
bool eventFilter(QObject *watched, QEvent *event) override;
};
class SearchLineEdit : public LineEditUnfocusable
{
private:
QTreeView *treeView;
protected:
void keyPressEvent(QKeyEvent *event) override;
public:
SearchLineEdit() : treeView(nullptr)
{
}
void setTreeView(QTreeView *_treeView)
{
treeView = _treeView;
}
};
#endif

View file

@ -0,0 +1,32 @@
#include "get_text_with_max.h"
QString getTextWithMax(QWidget *parent,
const QString &title,
const QString &label,
QLineEdit::EchoMode mode,
const QString &text,
bool *ok,
int max,
Qt::WindowFlags flags,
Qt::InputMethodHints inputMethodHints)
{
auto *dialog = new QInputDialog(parent, flags);
dialog->setWindowTitle(title);
dialog->setLabelText(label);
dialog->setTextValue(text);
dialog->setTextEchoMode(mode);
dialog->setInputMethodHints(inputMethodHints);
// find the qlineedit that this dialog holds, there should be only one
dialog->findChild<QLineEdit *>()->setMaxLength(max);
const int ret = dialog->exec();
if (ok != nullptr) {
*ok = !!ret;
}
if (ret) {
return dialog->textValue();
} else {
return QString();
}
}

View file

@ -0,0 +1,27 @@
/**
* @file get_text_with_max.h
* @ingroup UI
* @brief Custom QInputDialog::getText implementation that allows configuration of the max length
*/
#ifndef GETTEXTWITHMAX_H
#define GETTEXTWITHMAX_H
#include <QInputDialog>
#include <libcockatrice/utility/trice_limits.h>
QString getTextWithMax(QWidget *parent,
const QString &title,
const QString &label,
QLineEdit::EchoMode echo = QLineEdit::Normal,
const QString &text = QString(),
bool *ok = nullptr,
int max = MAX_NAME_LENGTH,
Qt::WindowFlags flags = Qt::WindowFlags(),
Qt::InputMethodHints inputMethodHints = Qt::ImhNone);
static inline QString getTextWithMax(QWidget *parent, const QString &title, const QString &label, int max)
{
return getTextWithMax(parent, title, label, QLineEdit::Normal, QString(), nullptr, max);
}
#endif // GETTEXTWITHMAX_H

View file

@ -0,0 +1,133 @@
#include "line_edit_completer.h"
#include <QAbstractItemView>
#include <QCompleter>
#include <QFocusEvent>
#include <QKeyEvent>
#include <QScrollBar>
#include <QStringListModel>
#include <QTextCursor>
#include <QWidget>
LineEditCompleter::LineEditCompleter(QWidget *parent) : LineEditUnfocusable(parent), c(nullptr)
{
}
void LineEditCompleter::focusOutEvent(QFocusEvent *e)
{
LineEditUnfocusable::focusOutEvent(e);
if (c->popup()->isVisible()) {
// Remove Popup
c->popup()->hide();
// Truncate the line to last space or whole string
QString textValue = text();
int lastIndex = textValue.length();
int lastWordStartIndex = textValue.lastIndexOf(" ") + 1;
int leftShift = qMin(lastIndex, lastWordStartIndex);
setText(textValue.left(leftShift));
// Insert highlighted line from popup
insert(c->completionModel()->index(c->popup()->currentIndex().row(), 0).data().toString() + " ");
// Set focus back to the textbox since tab was pressed
setFocus();
}
}
void LineEditCompleter::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Return:
case Qt::Key_Enter:
case Qt::Key_Escape:
if (c->popup()->isVisible()) {
event->ignore();
// Remove Popup
c->popup()->hide();
// Truncate the line to last space or whole string
QString textValue = text();
int lastIndexof = qMax(0, textValue.lastIndexOf(" "));
QString finalString = textValue.left(lastIndexof);
// Add a space if there's a word
if (finalString != "")
finalString += " ";
setText(finalString);
return;
}
break;
case Qt::Key_Space:
if (c->popup()->isVisible()) {
event->ignore();
// Remove Popup
c->popup()->hide();
// Truncate the line to last space or whole string
QString textValue = text();
int lastIndex = textValue.length();
int lastWordStartIndex = textValue.lastIndexOf(" ") + 1;
int leftShift = qMin(lastIndex, lastWordStartIndex);
setText(textValue.left(leftShift));
// Insert highlighted line from popup
insert(c->completionModel()->index(c->popup()->currentIndex().row(), 0).data().toString() + " ");
return;
}
break;
default:
break;
}
LineEditUnfocusable::keyPressEvent(event);
// return if the completer is null or if the most recently typed char was '@'.
// Only want the popup AFTER typing the first char of the mention.
if (!c || text().right(1).contains("@")) {
c->popup()->hide();
return;
}
// Set new completion prefix
c->setCompletionPrefix(cursorWord(text()));
if (c->completionPrefix().length() < 1) {
c->popup()->hide();
return;
}
// Draw completion box
QRect cr = cursorRect();
cr.setWidth(c->popup()->sizeHintForColumn(0) + c->popup()->verticalScrollBar()->sizeHint().width());
c->complete(cr);
// Select first item in the completion popup
QItemSelectionModel *sm = new QItemSelectionModel(c->completionModel());
c->popup()->setSelectionModel(sm);
sm->select(c->completionModel()->index(0, 0), QItemSelectionModel::ClearAndSelect);
sm->setCurrentIndex(c->completionModel()->index(0, 0), QItemSelectionModel::NoUpdate);
}
QString LineEditCompleter::cursorWord(const QString &line) const
{
return line.mid(line.left(cursorPosition()).lastIndexOf(" ") + 1,
cursorPosition() - line.left(cursorPosition()).lastIndexOf(" ") - 1);
}
void LineEditCompleter::insertCompletion(QString arg)
{
QString s_arg = arg + " ";
setText(text().replace(text().left(cursorPosition()).lastIndexOf(" ") + 1,
cursorPosition() - text().left(cursorPosition()).lastIndexOf(" ") - 1, s_arg));
}
void LineEditCompleter::setCompleter(QCompleter *completer)
{
c = completer;
c->setWidget(this);
connect(c, qOverload<const QString &>(&QCompleter::activated), this, &LineEditCompleter::insertCompletion);
}
void LineEditCompleter::setCompletionList(QStringList completionList)
{
if (!c || c->popup()->isVisible())
return;
QStringListModel *model;
model = (QStringListModel *)(c->model());
if (model == NULL)
model = new QStringListModel();
model->setStringList(completionList);
}

View file

@ -0,0 +1,35 @@
/**
* @file line_edit_completer.h
* @ingroup UI
* @brief TODO: Document this.
*/
#ifndef LINEEDITCOMPLETER_H
#define LINEEDITCOMPLETER_H
#include "custom_line_edit.h"
#include <QFocusEvent>
#include <QKeyEvent>
#include <QStringList>
class LineEditCompleter : public LineEditUnfocusable
{
Q_OBJECT
private:
QString cursorWord(const QString &line) const;
QCompleter *c;
private slots:
void insertCompletion(QString);
protected:
void keyPressEvent(QKeyEvent *event);
void focusOutEvent(QFocusEvent *e);
public:
explicit LineEditCompleter(QWidget *parent = nullptr);
void setCompleter(QCompleter *);
void setCompletionList(QStringList);
};
#endif

View file

@ -0,0 +1,219 @@
#include "sequence_edit.h"
#include <QHBoxLayout>
#include <QKeyEvent>
#include <QToolTip>
#include <libcockatrice/settings/cache_settings.h>
#include <utility>
SequenceEdit::SequenceEdit(const QString &_shortcutName, QWidget *parent) : QWidget(parent)
{
lineEdit = new QLineEdit(this);
clearButton = new QPushButton("", this);
defaultButton = new QPushButton("", this);
lineEdit->setMinimumWidth(70);
clearButton->setIcon(QPixmap("theme:icons/clearsearch"));
defaultButton->setIcon(QPixmap("theme:icons/update"));
auto *layout = new QHBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(1);
layout->addWidget(lineEdit);
layout->addWidget(clearButton);
layout->addWidget(defaultButton);
connect(clearButton, &QPushButton::clicked, this, &SequenceEdit::removeLastShortcut);
connect(defaultButton, &QPushButton::clicked, this, &SequenceEdit::restoreDefault);
lineEdit->installEventFilter(this);
setShortcutName(_shortcutName);
retranslateUi();
}
void SequenceEdit::setShortcutName(const QString &_shortcutName)
{
shortcutName = _shortcutName;
if (shortcutName.isEmpty()) {
clearButton->setEnabled(false);
defaultButton->setEnabled(false);
lineEdit->setEnabled(false);
lineEdit->setText("");
// Correct as in-line translation
lineEdit->setPlaceholderText(tr("Choose an action from the table"));
} else {
clearButton->setEnabled(true);
defaultButton->setEnabled(true);
lineEdit->setEnabled(true);
lineEdit->setText(SettingsCache::instance().shortcuts().getShortcutString(shortcutName));
// Correct as in-line translation
lineEdit->setPlaceholderText(tr("Hit the key/combination of keys you want to set for this action"));
}
}
QString SequenceEdit::getSequence()
{
return lineEdit->text();
}
void SequenceEdit::removeLastShortcut()
{
QString sequences = lineEdit->text();
if (!sequences.isEmpty()) {
if (sequences.lastIndexOf(";") > 0) {
QString validText = sequences.left(sequences.lastIndexOf(";"));
lineEdit->setText(validText);
} else {
lineEdit->clear();
}
updateSettings();
}
}
void SequenceEdit::restoreDefault()
{
lineEdit->setText(SettingsCache::instance().shortcuts().getDefaultShortcutString(shortcutName));
updateSettings();
}
void SequenceEdit::refreshShortcut()
{
lineEdit->setText(SettingsCache::instance().shortcuts().getShortcutString(shortcutName));
}
void SequenceEdit::clear()
{
lineEdit->setText("");
}
bool SequenceEdit::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
auto *keyEvent = reinterpret_cast<QKeyEvent *>(event);
// don't filter outside arrow key events
if (obj != lineEdit) {
switch (keyEvent->key()) {
case Qt::Key_Up:
case Qt::Key_Down:
case Qt::Key_Left:
case Qt::Key_Right:
return false;
default:
break;
}
}
if (event->type() == QEvent::KeyPress && !keyEvent->isAutoRepeat()) {
processKey(keyEvent);
} else if (event->type() == QEvent::KeyRelease && !keyEvent->isAutoRepeat()) {
finishShortcut();
}
return true;
}
return false;
}
void SequenceEdit::processKey(QKeyEvent *e)
{
int key = e->key();
if (key != Qt::Key_Control && key != Qt::Key_Shift && key != Qt::Key_Meta && key != Qt::Key_Alt) {
valid = true;
key |= translateModifiers(e->modifiers(), e->text());
}
keys = key;
currentKey++;
if (currentKey >= key) {
finishShortcut();
}
}
int SequenceEdit::translateModifiers(Qt::KeyboardModifiers state, const QString &text)
{
int result = 0;
// The shift modifier only counts when it is not used to type a symbol
// that is only reachable using the shift key anyway
if ((state & Qt::ShiftModifier) &&
(text.isEmpty() || !text.at(0).isPrint() || text.at(0).isLetterOrNumber() || text.at(0).isSpace())) {
result |= Qt::SHIFT;
}
if (state & Qt::ControlModifier) {
result |= Qt::CTRL;
}
if (state & Qt::MetaModifier) {
result |= Qt::META;
}
if (state & Qt::AltModifier) {
result |= Qt::ALT;
}
return result;
}
/**
*Validates that shortcut is valid (is a valid shortcut key sequence and doesn't conflict with any other shortcuts).
*Displays warning messages if it's not valid.
*
* @param sequence The shortcut key sequence
* @return True if the sequence isn't already self-contained
*/
bool SequenceEdit::validateShortcut(const QKeySequence &sequence)
{
if (sequence.isEmpty() || !valid) {
return true;
}
const auto &shortcutsSettings = SettingsCache::instance().shortcuts();
const QString sequenceString = sequence.toString();
if (!shortcutsSettings.isKeyAllowed(shortcutName, sequenceString)) {
QToolTip::showText(lineEdit->mapToGlobal(QPoint()), tr("Invalid key"));
return true;
}
if (!shortcutsSettings.isValid(shortcutName, sequenceString)) {
auto overlaps = shortcutsSettings.findOverlaps(shortcutName, sequenceString);
QToolTip::showText(lineEdit->mapToGlobal(QPoint()),
tr("Shortcut already in use by:") + " " + overlaps.join(", "));
return true;
}
if (!lineEdit->text().isEmpty()) {
if (lineEdit->text().contains(sequenceString)) {
return false;
}
lineEdit->setText(lineEdit->text() + ";");
}
lineEdit->setText(lineEdit->text() + sequenceString);
return true;
}
void SequenceEdit::finishShortcut()
{
if (!validateShortcut(QKeySequence(keys))) {
return;
}
currentKey = 0;
keys = 0;
valid = false;
updateSettings();
}
void SequenceEdit::updateSettings()
{
SettingsCache::instance().shortcuts().setShortcuts(shortcutName, lineEdit->text());
}
void SequenceEdit::retranslateUi()
{
clearButton->setText(tr("Clear"));
defaultButton->setText(tr("Restore default"));
setShortcutName(shortcutName);
}

View file

@ -0,0 +1,50 @@
/**
* @file sequence_edit.h
* @ingroup UI
* @brief TODO: Document this.
*/
#ifndef SEQUENCEEDIT_H
#define SEQUENCEEDIT_H
#include <QEvent>
#include <QKeySequence>
#include <QLineEdit>
#include <QPushButton>
#include <QWidget>
class SequenceEdit : public QWidget
{
Q_OBJECT
public:
explicit SequenceEdit(const QString &_shortcutName, QWidget *parent = nullptr);
QString getSequence();
void setShortcutName(const QString &_shortcutName);
void refreshShortcut();
void clear();
void retranslateUi();
private slots:
void removeLastShortcut();
void restoreDefault();
protected:
bool eventFilter(QObject *, QEvent *event) override;
private:
QString shortcutName;
QLineEdit *lineEdit;
QPushButton *clearButton;
QPushButton *defaultButton;
int keys = 0;
int currentKey = 0;
bool valid = false;
void processKey(QKeyEvent *e);
int translateModifiers(Qt::KeyboardModifiers state, const QString &text);
bool validateShortcut(const QKeySequence &sequence);
void finishShortcut();
void updateSettings();
};
#endif // SEQUENCEEDIT_H