1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#include "filter-combo-box.hpp"
#include "ui-helpers.hpp"
#include <QCompleter>
#include <QLineEdit>
#include <QFocusEvent>
namespace advss {
bool FilterComboBox::_filteringEnabled = false;
FilterComboBox::FilterComboBox(QWidget *parent, const QString &placehodler)
: QComboBox(parent)
{
// If the filtering behaviour of the FilterComboBox is disabled it is
// just a regular QComboBox with the option to set a placeholder so exit
// the constructor early.
if (!_filteringEnabled) {
if (!placehodler.isEmpty()) {
setPlaceholderText(placehodler);
}
return;
}
// Allow edit for completer but don't add new entries on pressing enter
setEditable(true);
setInsertPolicy(InsertPolicy::NoInsert);
if (!placehodler.isEmpty()) {
lineEdit()->setPlaceholderText(placehodler);
// Make sure that the placeholder text is visible
QFontMetrics fontMetrics(font());
int textWidth = fontMetrics.boundingRect(placehodler).width();
QStyleOptionComboBox comboBoxOption;
comboBoxOption.initFrom(this);
int buttonWidth =
style()->subControlRect(QStyle::CC_ComboBox,
&comboBoxOption,
QStyle::SC_ComboBoxArrow, this)
.width();
setMinimumWidth(buttonWidth + textWidth);
}
setMaxVisibleItems(30);
auto c = completer();
c->setCaseSensitivity(Qt::CaseInsensitive);
c->setFilterMode(Qt::MatchContains);
c->setCompletionMode(QCompleter::PopupCompletion);
connect(c, QOverload<const QModelIndex &>::of(&QCompleter::highlighted),
this, &FilterComboBox::CompleterHighlightChanged);
connect(lineEdit(), &QLineEdit::textChanged, this,
&FilterComboBox::TextChanged);
}
void FilterComboBox::SetFilterBehaviourEnabled(bool value)
{
FilterComboBox::_filteringEnabled = value;
}
void FilterComboBox::SetAllowUnmatchedSelection(bool allow)
{
_allowUnmatchedSelection = allow;
}
void FilterComboBox::setCurrentText(const QString &text)
{
if (_filteringEnabled) {
lineEdit()->setText(text);
}
QComboBox::setCurrentText(text);
}
void FilterComboBox::setItemText(int index, const QString &text)
{
QComboBox::setItemText(index, text);
if (_filteringEnabled && index == currentIndex()) {
const QSignalBlocker b(this);
lineEdit()->setText(text);
}
}
QSize FilterComboBox::sizeHint() const
{
QSize size = QComboBox::sizeHint();
if (!_filteringEnabled) {
return QComboBox::sizeHint();
}
QFontMetrics fm(font());
int extra = fm.horizontalAdvance("X") * 2; // Add padding
size.setWidth(size.width() + extra);
return size;
}
void FilterComboBox::focusOutEvent(QFocusEvent *event)
{
// Reset on invalid selection
if (!_allowUnmatchedSelection && findText(currentText()) == -1) {
setCurrentIndex(-1);
Emit(-1, "");
}
QComboBox::focusOutEvent(event);
_lastCompleterHighlightRow = -1;
}
static int findXthOccurrence(QComboBox *list, int count, const QString &value)
{
if (value.isEmpty() || count < 1) {
return -1;
}
const auto size = list->count();
int idx = FindIdxInRagne(list, 0, size, value.toStdString(),
Qt::MatchContains | Qt::MatchFixedString);
if (count == 1) {
return idx;
}
for (int i = 1; i < count; i++) {
idx = FindIdxInRagne(list, idx, size, value.toStdString(),
Qt::MatchContains | Qt::MatchFixedString);
}
return idx;
}
void FilterComboBox::CompleterHighlightChanged(const QModelIndex &index)
{
_lastCompleterHighlightRow = index.row();
const auto text = currentText();
int idx = findXthOccurrence(this, _lastCompleterHighlightRow, text);
if (idx == -1) {
return;
}
Emit(idx, text);
}
void FilterComboBox::TextChanged(const QString &text)
{
auto c = completer();
const bool completerActive = c->completionCount() > 0;
int count = completerActive ? _lastCompleterHighlightRow + 1 : 1;
int idx = findXthOccurrence(this, count, text);
if (idx == -1) {
return;
}
}
void FilterComboBox::Emit(int index, const QString &text)
{
if (_lastEmittedIndex != index) {
_lastEmittedIndex = index;
emit currentIndexChanged(index);
}
if (_lastEmittedText != text) {
_lastEmittedText = text;
emit currentTextChanged(text);
}
}
} // namespace advss
|