|
| 1 | +#include "historycombobox.h" |
| 2 | + |
| 3 | +/// |
| 4 | +/// \brief HistoryComboBox::HistoryComboBox |
| 5 | +/// \param parent |
| 6 | +/// \param maxItems |
| 7 | +/// \param settingsKey |
| 8 | +/// |
| 9 | +HistoryComboBox::HistoryComboBox(QWidget *parent, int maxItems, const QString &settingsKey) |
| 10 | + : QComboBox(parent), _maxItems(maxItems), _settingsKey(settingsKey) |
| 11 | +{ |
| 12 | + setEditable(true); |
| 13 | + setInsertPolicy(QComboBox::NoInsert); |
| 14 | + loadHistory(); |
| 15 | +} |
| 16 | + |
| 17 | +/// |
| 18 | +/// \brief HistoryComboBox::~HistoryComboBox |
| 19 | +/// |
| 20 | +HistoryComboBox::~HistoryComboBox() |
| 21 | +{ |
| 22 | +} |
| 23 | + |
| 24 | +/// |
| 25 | +/// \brief HistoryComboBox::addToHistory |
| 26 | +/// \param text |
| 27 | +/// |
| 28 | +void HistoryComboBox::addToHistory(const QString &text) |
| 29 | +{ |
| 30 | + if (text.isEmpty()) |
| 31 | + return; |
| 32 | + |
| 33 | + // Remove duplicates |
| 34 | + int index = findText(text); |
| 35 | + if (index >= 0) |
| 36 | + removeItem(index); |
| 37 | + |
| 38 | + // Add to the beginning |
| 39 | + insertItem(0, text); |
| 40 | + |
| 41 | + // Limit the number of items |
| 42 | + while (count() > _maxItems) |
| 43 | + removeItem(_maxItems); |
| 44 | +} |
| 45 | + |
| 46 | +/// |
| 47 | +/// \brief HistoryComboBox::saveHistory |
| 48 | +/// |
| 49 | +void HistoryComboBox::saveHistory() |
| 50 | +{ |
| 51 | + if (_settingsKey.isEmpty()) |
| 52 | + return; |
| 53 | + |
| 54 | + QSettings m(QSettings::NativeFormat, QSettings::UserScope, APP_NAME, APP_VERSION); |
| 55 | + m.beginGroup("HistoryComboBox"); |
| 56 | + |
| 57 | + QStringList history; |
| 58 | + for (int i = 0; i < count(); ++i) |
| 59 | + history << itemText(i); |
| 60 | + |
| 61 | + m.setValue(_settingsKey, history); |
| 62 | + m.endGroup(); |
| 63 | +} |
| 64 | + |
| 65 | +/// |
| 66 | +/// \brief HistoryComboBox::loadHistory |
| 67 | +/// |
| 68 | +void HistoryComboBox::loadHistory() |
| 69 | +{ |
| 70 | + if (_settingsKey.isEmpty()) |
| 71 | + return; |
| 72 | + |
| 73 | + QSettings m(QSettings::NativeFormat, QSettings::UserScope, APP_NAME, APP_VERSION); |
| 74 | + m.beginGroup("HistoryComboBox"); |
| 75 | + QStringList history = m.value(_settingsKey).toStringList(); |
| 76 | + |
| 77 | + if (!history.isEmpty()) |
| 78 | + addItems(history); |
| 79 | + |
| 80 | + m.endGroup(); |
| 81 | +} |
| 82 | + |
| 83 | +/// |
| 84 | +/// \brief HistoryComboBox::clearHistory |
| 85 | +/// |
| 86 | +void HistoryComboBox::clearHistory() |
| 87 | +{ |
| 88 | + clear(); |
| 89 | + if (!_settingsKey.isEmpty()) |
| 90 | + { |
| 91 | + QSettings m(QSettings::NativeFormat, QSettings::UserScope, APP_NAME, APP_VERSION); |
| 92 | + m.beginGroup("HistoryComboBox"); |
| 93 | + m.remove(_settingsKey); |
| 94 | + m.endGroup(); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/// |
| 99 | +/// \brief HistoryComboBox::setMaxItems |
| 100 | +/// \param max |
| 101 | +/// |
| 102 | +void HistoryComboBox::setMaxItems(int max) |
| 103 | +{ |
| 104 | + _maxItems = max; |
| 105 | + while (count() > _maxItems) |
| 106 | + removeItem(_maxItems); |
| 107 | +} |
| 108 | + |
| 109 | +/// |
| 110 | +/// \brief HistoryComboBox::maxItems |
| 111 | +/// \return |
| 112 | +/// |
| 113 | +int HistoryComboBox::maxItems() const |
| 114 | +{ |
| 115 | + return _maxItems; |
| 116 | +} |
| 117 | + |
| 118 | +/// |
| 119 | +/// \brief HistoryComboBox::focusOutEvent |
| 120 | +/// \param event |
| 121 | +/// |
| 122 | +void HistoryComboBox::focusOutEvent(QFocusEvent *event) |
| 123 | +{ |
| 124 | + QString currentText = this->currentText(); |
| 125 | + if (!currentText.isEmpty() && findText(currentText) == -1) |
| 126 | + addToHistory(currentText); |
| 127 | + |
| 128 | + QComboBox::focusOutEvent(event); |
| 129 | +} |
0 commit comments