|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <QMainWindow> |
| 4 | +#include <QListWidget> |
| 5 | +#include <QListWidgetItem> |
| 6 | +#include <QLineEdit> |
| 7 | +#include <QMap> |
| 8 | +#include <QVBoxLayout> |
| 9 | +#include <QKeyEvent> |
| 10 | +#include <QApplication> |
| 11 | + |
| 12 | +class LineEdit : public QLineEdit { |
| 13 | + Q_OBJECT |
| 14 | +signals: |
| 15 | + void changed(); |
| 16 | + |
| 17 | +private: |
| 18 | + void keyPressEvent(QKeyEvent* event) { |
| 19 | + QLineEdit::keyPressEvent(event); |
| 20 | + emit changed(); |
| 21 | + } |
| 22 | +}; |
| 23 | + |
| 24 | +class Menu : public QMainWindow { |
| 25 | + Q_OBJECT |
| 26 | + typedef QPair<QString, QString> QStringPair; |
| 27 | +public: |
| 28 | + Menu(int width, int height, const QList<QString>& values, const char* separator = NULL) |
| 29 | + : QMainWindow(0, Qt::Popup) |
| 30 | + { |
| 31 | + if (values.size() == 0) { |
| 32 | + QApplication::exit(0); |
| 33 | + } |
| 34 | + |
| 35 | + setCentralWidget(&_containerWidget); |
| 36 | + |
| 37 | + QVBoxLayout* layout = new QVBoxLayout(); |
| 38 | + layout->setContentsMargins(0, 0, 0, 0); |
| 39 | + layout->addWidget(&_listWidget); |
| 40 | + layout->addWidget(&_lineEdit); |
| 41 | + _containerWidget.setLayout(layout); |
| 42 | + |
| 43 | + foreach(const QString& val, values) { |
| 44 | + if (separator && val.contains(separator)) { |
| 45 | + int idx = val.indexOf(separator); |
| 46 | + _items.append(QStringPair(val.left(idx), val.right(val.size() - idx - strlen(separator)))); |
| 47 | + } else { |
| 48 | + _items.append(QStringPair(val, val)); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + resize(width, height); |
| 53 | + |
| 54 | + _listWidget.setFocusPolicy(Qt::NoFocus); |
| 55 | + _lineEdit.setFocus(Qt::OtherFocusReason); |
| 56 | + |
| 57 | + _rebuildList(); |
| 58 | + |
| 59 | + connect(&_lineEdit, SIGNAL(changed()), this, SLOT(_rebuildList())); |
| 60 | + connect(&_lineEdit, SIGNAL(editingFinished()), this, SLOT(_done())); |
| 61 | + } |
| 62 | + |
| 63 | +private slots: |
| 64 | + void _done() { |
| 65 | + if (_listWidget.count() > 0) { |
| 66 | + QListWidgetItem* item = _listWidget.currentItem(); |
| 67 | + if (!item) { |
| 68 | + item = _listWidget.item(0); |
| 69 | + } |
| 70 | + printf(qPrintable(_widgetToContent[item])); |
| 71 | + } |
| 72 | + QApplication::exit(0); |
| 73 | + } |
| 74 | + |
| 75 | + void _rebuildList() { |
| 76 | + if (_filter.isNull() || (_filter != _lineEdit.text())) { |
| 77 | + _filter = _lineEdit.text(); |
| 78 | + _listWidget.clear(); |
| 79 | + _widgetToContent.clear(); |
| 80 | + |
| 81 | + QList<const QStringPair*> matched; |
| 82 | + |
| 83 | + foreach (const QStringPair& pair, _items) { |
| 84 | + _process(matched, pair, pair.second, &_match1); |
| 85 | + } |
| 86 | + |
| 87 | + foreach (const QStringPair& pair, _items) { |
| 88 | + _process(matched, pair, pair.second, &_match2); |
| 89 | + } |
| 90 | + |
| 91 | + foreach (const QStringPair& pair, _items) { |
| 92 | + _process(matched, pair, pair.first, &_match1); |
| 93 | + } |
| 94 | + |
| 95 | + foreach (const QStringPair& pair, _items) { |
| 96 | + _process(matched, pair, pair.first, &_match2); |
| 97 | + } |
| 98 | + |
| 99 | + if (_listWidget.count() > 0) { |
| 100 | + _listWidget.setCurrentRow(0); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | +private: |
| 106 | + static bool _match1(const QString& filter, QString text) { |
| 107 | + if (filter.isEmpty()) { |
| 108 | + return true; |
| 109 | + } |
| 110 | + if (filter.toLower() == filter) { |
| 111 | + text = text.toLower(); |
| 112 | + } |
| 113 | + |
| 114 | + return text.contains(filter); |
| 115 | + } |
| 116 | + |
| 117 | + static bool _match2(const QString& filter, QString text) { |
| 118 | + if (filter.isEmpty()) { |
| 119 | + return true; |
| 120 | + } |
| 121 | + if (filter.toLower() == filter) { |
| 122 | + text = text.toLower(); |
| 123 | + } |
| 124 | + |
| 125 | + int fi = 0; |
| 126 | + for (int i = 0; i < text.size(); ++i) { |
| 127 | + if (text[i] == filter[fi]) { |
| 128 | + ++fi; |
| 129 | + if (fi == filter.size()) { |
| 130 | + return true; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + void _process(QList<const QStringPair*>& matched, const QStringPair& pair, const QString& value, bool (*matchFn)(const QString&, QString)) { |
| 139 | + if (matched.contains(&pair)) { |
| 140 | + return; |
| 141 | + } |
| 142 | + if ((*matchFn)(_filter, value)) { |
| 143 | + matched.append(&pair); |
| 144 | + QListWidgetItem* item = new QListWidgetItem(pair.second); |
| 145 | + _widgetToContent[item] = pair.first; |
| 146 | + _listWidget.addItem(item); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | +private: |
| 151 | + void keyPressEvent(QKeyEvent* event) { |
| 152 | + if (event->key() == Qt::Key_Up) { |
| 153 | + _listWidget.setCurrentRow(std::max(0, _listWidget.currentRow() - 1)); |
| 154 | + } else if (event->key() == Qt::Key_Down) { |
| 155 | + _listWidget.setCurrentRow(std::min(_listWidget.count(), _listWidget.currentRow() + 1)); |
| 156 | + } else if (event->key() == Qt::Key_Escape) { |
| 157 | + QApplication::exit(0); |
| 158 | + } |
| 159 | + QMainWindow::keyPressEvent(event); |
| 160 | + } |
| 161 | + |
| 162 | + QWidget _containerWidget; |
| 163 | + QString _filter; |
| 164 | + QListWidget _listWidget; |
| 165 | + LineEdit _lineEdit; |
| 166 | + QList<QStringPair> _items; |
| 167 | + QMap<QListWidgetItem*, QString> _widgetToContent; |
| 168 | +}; |
0 commit comments