HistoryCompleter: Fix current text() handling

Can be seen in the Debugger's "Command:" input: Selecting a
history entry with "Return" properly executes the selected
item, but put something else into the line edit.

Change-Id: I2efa05374d9c31e8a80219794f2dbaaf50a01f9b
Reviewed-by: Christian Stenger <christian.stenger@digia.com>
This commit is contained in:
hjk
2014-07-07 15:04:52 +02:00
parent a4f5f7f748
commit b70377c872
4 changed files with 29 additions and 23 deletions

View File

@@ -299,8 +299,20 @@ bool FancyLineEdit::hasAutoHideButton(Side side) const
void FancyLineEdit::setHistoryCompleter(const QString &historyKey) void FancyLineEdit::setHistoryCompleter(const QString &historyKey)
{ {
QTC_ASSERT(!d->m_historyCompleter, return); QTC_ASSERT(!d->m_historyCompleter, return);
d->m_historyCompleter = new HistoryCompleter(this, historyKey, this); d->m_historyCompleter = new HistoryCompleter(historyKey, this);
QLineEdit::setCompleter(d->m_historyCompleter); QLineEdit::setCompleter(d->m_historyCompleter);
// Hitting <Return> in the popup first causes editingFinished()
// being emitted and more updates finally calling setText() (again).
// To make sure we report the "final" content delay the addEntry()
// "a bit".
connect(this, SIGNAL(editingFinished()),
this, SLOT(onEditingFinished()), Qt::QueuedConnection);
}
void FancyLineEdit::onEditingFinished()
{
d->m_historyCompleter->addEntry(text());
} }
void FancyLineEdit::setSpecialCompleter(QCompleter *completer) void FancyLineEdit::setSpecialCompleter(QCompleter *completer)

View File

@@ -162,6 +162,7 @@ signals:
private slots: private slots:
void iconClicked(); void iconClicked();
void onTextChanged(const QString &); void onTextChanged(const QString &);
void onEditingFinished();
protected: protected:
void resizeEvent(QResizeEvent *e); void resizeEvent(QResizeEvent *e);

View File

@@ -47,19 +47,18 @@ static QSettings *theSettings = 0;
class HistoryCompleterPrivate : public QAbstractListModel class HistoryCompleterPrivate : public QAbstractListModel
{ {
public: public:
HistoryCompleterPrivate() : maxLines(30), lineEdit(0) {} HistoryCompleterPrivate() : maxLines(30) {}
int rowCount(const QModelIndex &parent = QModelIndex()) const; int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()); bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
void clearHistory(); void clearHistory();
void saveEntry(const QString &str); void addEntry(const QString &str);
QStringList list; QStringList list;
QString historyKey; QString historyKey;
int maxLines; int maxLines;
FancyLineEdit *lineEdit;
}; };
class HistoryLineDelegate : public QItemDelegate class HistoryLineDelegate : public QItemDelegate
@@ -146,38 +145,33 @@ void HistoryCompleterPrivate::clearHistory()
endResetModel(); endResetModel();
} }
void HistoryCompleterPrivate::saveEntry(const QString &str) void HistoryCompleterPrivate::addEntry(const QString &str)
{ {
QTC_ASSERT(theSettings, return); const QString entry = str.trimmed();
const QString &entry = str.trimmed(); if (entry.isEmpty())
return;
int removeIndex = list.indexOf(entry); int removeIndex = list.indexOf(entry);
beginResetModel();
if (removeIndex != -1) if (removeIndex != -1)
removeRow(removeIndex); list.removeAt(removeIndex);
beginInsertRows (QModelIndex(), list.count(), list.count());
list.prepend(entry); list.prepend(entry);
list = list.mid(0, maxLines); list = list.mid(0, maxLines - 1);
endInsertRows(); endResetModel();
theSettings->setValue(historyKey, list); theSettings->setValue(historyKey, list);
} }
HistoryCompleter::HistoryCompleter(FancyLineEdit *lineEdit, const QString &historyKey, QObject *parent) HistoryCompleter::HistoryCompleter(const QString &historyKey, QObject *parent)
: QCompleter(parent), : QCompleter(parent),
d(new HistoryCompleterPrivate) d(new HistoryCompleterPrivate)
{ {
QTC_ASSERT(lineEdit, return);
QTC_ASSERT(!historyKey.isEmpty(), return); QTC_ASSERT(!historyKey.isEmpty(), return);
QTC_ASSERT(theSettings, return); QTC_ASSERT(theSettings, return);
d->historyKey = QLatin1String("CompleterHistory/") + historyKey; d->historyKey = QLatin1String("CompleterHistory/") + historyKey;
d->list = theSettings->value(d->historyKey).toStringList(); d->list = theSettings->value(d->historyKey).toStringList();
d->lineEdit = lineEdit;
if (d->list.count() && lineEdit->text().isEmpty())
lineEdit->setText(d->list.at(0));
setModel(d); setModel(d);
setPopup(new HistoryLineView(d)); setPopup(new HistoryLineView(d));
connect(lineEdit, SIGNAL(editingFinished()), this, SLOT(saveHistory()));
} }
bool HistoryCompleter::removeHistoryItem(int index) bool HistoryCompleter::removeHistoryItem(int index)
@@ -210,9 +204,9 @@ void HistoryCompleter::clearHistory()
d->clearHistory(); d->clearHistory();
} }
void HistoryCompleter::saveHistory() void HistoryCompleter::addEntry(const QString &str)
{ {
d->saveEntry(d->lineEdit->text()); d->addEntry(str);
} }
void HistoryCompleter::setSettings(QSettings *settings) void HistoryCompleter::setSettings(QSettings *settings)

View File

@@ -40,7 +40,6 @@ QT_END_NAMESPACE
namespace Utils { namespace Utils {
class FancyLineEdit;
namespace Internal { class HistoryCompleterPrivate; } namespace Internal { class HistoryCompleterPrivate; }
class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter
@@ -49,7 +48,7 @@ class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter
public: public:
static void setSettings(QSettings *settings); static void setSettings(QSettings *settings);
HistoryCompleter(FancyLineEdit *lineEdit, const QString &historyKey, QObject *parent = 0); HistoryCompleter(const QString &historyKey, QObject *parent = 0);
bool removeHistoryItem(int index); bool removeHistoryItem(int index);
private: private:
@@ -60,7 +59,7 @@ private:
public Q_SLOTS: public Q_SLOTS:
void clearHistory(); void clearHistory();
void saveHistory(); void addEntry(const QString &str);
private: private:
Internal::HistoryCompleterPrivate *d; Internal::HistoryCompleterPrivate *d;