2018-07-11 19:18:50 +01:00
|
|
|
#include "noscrollcombobox.h"
|
|
|
|
|
2019-08-07 02:50:03 +01:00
|
|
|
#include <QCompleter>
|
2024-07-12 19:05:37 +01:00
|
|
|
#include <QLineEdit>
|
2019-08-07 02:50:03 +01:00
|
|
|
|
2018-07-11 19:18:50 +01:00
|
|
|
NoScrollComboBox::NoScrollComboBox(QWidget *parent)
|
|
|
|
: QComboBox(parent)
|
|
|
|
{
|
|
|
|
// Don't let scrolling hijack focus.
|
|
|
|
setFocusPolicy(Qt::StrongFocus);
|
2019-08-07 02:50:03 +01:00
|
|
|
|
|
|
|
// Make speed a priority when loading comboboxes.
|
2019-08-19 03:33:36 +01:00
|
|
|
setMinimumContentsLength(24);// an arbitrary limit
|
2021-02-06 00:43:49 +00:00
|
|
|
setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon);
|
2019-08-07 02:50:03 +01:00
|
|
|
|
|
|
|
// Allow items to be searched by any part of the word, displaying all matches.
|
|
|
|
setEditable(true);// can set to false manually when using
|
|
|
|
this->completer()->setCompletionMode(QCompleter::PopupCompletion);
|
|
|
|
this->completer()->setFilterMode(Qt::MatchContains);
|
2021-09-07 22:35:55 +01:00
|
|
|
|
2022-11-23 03:57:26 +00:00
|
|
|
static const QRegularExpression re("[^\\s]*");
|
2021-09-07 22:35:55 +01:00
|
|
|
QValidator *validator = new QRegularExpressionValidator(re);
|
|
|
|
this->setValidator(validator);
|
2018-07-11 19:18:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void NoScrollComboBox::wheelEvent(QWheelEvent *event)
|
|
|
|
{
|
|
|
|
// Only allow scrolling to modify contents when it explicitly has focus.
|
|
|
|
if (hasFocus())
|
|
|
|
QComboBox::wheelEvent(event);
|
|
|
|
}
|
2022-10-19 18:34:43 +01:00
|
|
|
|
2023-08-23 07:32:32 +01:00
|
|
|
void NoScrollComboBox::setItem(int index, const QString &text)
|
2022-10-19 18:34:43 +01:00
|
|
|
{
|
2023-08-23 07:32:32 +01:00
|
|
|
if (index >= 0) {
|
|
|
|
// Valid item
|
2022-10-19 18:34:43 +01:00
|
|
|
this->setCurrentIndex(index);
|
2023-08-23 07:32:32 +01:00
|
|
|
} else if (this->isEditable()) {
|
|
|
|
// Invalid item in editable box, just display the text
|
2022-10-19 18:34:43 +01:00
|
|
|
this->setCurrentText(text);
|
2023-08-23 07:32:32 +01:00
|
|
|
} else {
|
|
|
|
// Invalid item in uneditable box, display text as placeholder
|
|
|
|
// On Qt < 5.15 this will display an empty box
|
|
|
|
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
|
|
|
|
this->setPlaceholderText(text);
|
|
|
|
#endif
|
|
|
|
this->setCurrentIndex(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NoScrollComboBox::setTextItem(const QString &text)
|
|
|
|
{
|
|
|
|
this->setItem(this->findText(text), text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NoScrollComboBox::setNumberItem(int value)
|
|
|
|
{
|
|
|
|
this->setItem(this->findData(value), QString::number(value));
|
2022-10-19 18:34:43 +01:00
|
|
|
}
|
2023-12-19 18:42:07 +00:00
|
|
|
|
|
|
|
void NoScrollComboBox::setHexItem(uint32_t value)
|
|
|
|
{
|
|
|
|
this->setItem(this->findData(value), "0x" + QString::number(value, 16).toUpper());
|
|
|
|
}
|
2024-07-12 19:05:37 +01:00
|
|
|
|
|
|
|
void NoScrollComboBox::setClearButtonEnabled(bool enabled) {
|
|
|
|
if (this->lineEdit())
|
|
|
|
this->lineEdit()->setClearButtonEnabled(enabled);
|
|
|
|
}
|