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)
{
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);
// 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)

View File

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

View File

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

View File

@@ -40,7 +40,6 @@ QT_END_NAMESPACE
namespace Utils {
class FancyLineEdit;
namespace Internal { class HistoryCompleterPrivate; }
class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter
@@ -49,7 +48,7 @@ class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter
public:
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);
private:
@@ -60,7 +59,7 @@ private:
public Q_SLOTS:
void clearHistory();
void saveHistory();
void addEntry(const QString &str);
private:
Internal::HistoryCompleterPrivate *d;