diff --git a/src/libs/utils/fancylineedit.cpp b/src/libs/utils/fancylineedit.cpp index a1a5bfbf376..bcb8a3393d5 100644 --- a/src/libs/utils/fancylineedit.cpp +++ b/src/libs/utils/fancylineedit.cpp @@ -114,7 +114,6 @@ public: bool m_iconEnabled[2]; HistoryCompleter *m_completer; - QString m_historyKey; }; @@ -306,8 +305,8 @@ bool FancyLineEdit::hasAutoHideButton(Side side) const void FancyLineEdit::setHistoryKey(const QString &historyKey) { QTC_ASSERT(!d->m_completer, return); - d->m_historyKey = historyKey; d->m_completer = new HistoryCompleter(this, historyKey); + QLineEdit::setCompleter(d->m_completer); } void FancyLineEdit::setSpecialCompleter(QCompleter *completer) diff --git a/src/libs/utils/historycompleter.cpp b/src/libs/utils/historycompleter.cpp index 04e8df0fa93..8fcfa1436ba 100644 --- a/src/libs/utils/historycompleter.cpp +++ b/src/libs/utils/historycompleter.cpp @@ -29,6 +29,7 @@ **************************************************************************/ #include "historycompleter.h" + #include "qtcassert.h" #include @@ -49,20 +50,17 @@ static QSettings *theSettings = 0; class HistoryCompleterPrivate : public QAbstractListModel { public: - HistoryCompleterPrivate(HistoryCompleter *parent); + 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 fetchHistory(); void clearHistory(); void saveEntry(const QString &str); - bool eventFilter(QObject *obj, QEvent *event); QStringList list; QString historyKey; - HistoryCompleter *completer; - QWidget *lastSeenWidget; int maxLines; }; @@ -86,9 +84,13 @@ public: class HistoryLineView : public QListView { public: - HistoryLineView(HistoryCompleterPrivate *model_, int pixmapWith_) - : model(model_) , pixmapWidth(pixmapWith_) - {} + HistoryLineView(HistoryCompleterPrivate *model_) + : model(model_) + { + HistoryLineDelegate *delegate = new HistoryLineDelegate; + pixmapWidth = delegate->pixmap.width(); + setItemDelegate(delegate); + } private: void mousePressEvent(QMouseEvent *event) @@ -111,45 +113,9 @@ private: using namespace Internal; -HistoryCompleterPrivate::HistoryCompleterPrivate(HistoryCompleter *parent) - : QAbstractListModel(parent) - , completer(parent) - , lastSeenWidget(0) - , maxLines(30) -{ -} - -void HistoryCompleterPrivate::fetchHistory() -{ - QTC_ASSERT(theSettings, return); - if (!completer->widget()) { - list.clear(); - reset(); - return; - } - list = theSettings->value(historyKey).toStringList(); - reset(); -} - int HistoryCompleterPrivate::rowCount(const QModelIndex &parent) const { - if (lastSeenWidget != completer->widget()) { - if (lastSeenWidget) - lastSeenWidget->removeEventFilter(const_cast(this)); - completer->widget()->installEventFilter(const_cast(this)); - if (qobject_cast(lastSeenWidget)) - // this will result in spamming the history with garbage in some corner cases. - // not my idea. - disconnect(lastSeenWidget, SIGNAL(editingFinished()), completer, SLOT(saveHistory())); - HistoryCompleterPrivate *that = const_cast(this); - that->lastSeenWidget = completer->widget(); - that->fetchHistory(); - if (qobject_cast(lastSeenWidget)) - connect(lastSeenWidget, SIGNAL(editingFinished()), completer, SLOT(saveHistory())); - } - if (parent.isValid()) - return 0; - return list.count(); + return parent.isValid() ? 0 : list.count(); } QVariant HistoryCompleterPrivate::data(const QModelIndex &index, int role) const @@ -183,15 +149,6 @@ void HistoryCompleterPrivate::saveEntry(const QString &str) return; if (list.contains(str)) return; - if (!completer->widget()) - return; - if (lastSeenWidget != completer->widget()) { - if (lastSeenWidget) - lastSeenWidget->removeEventFilter(this); - completer->widget()->installEventFilter(this); - fetchHistory(); - lastSeenWidget = completer->widget(); - } beginInsertRows (QModelIndex(), list.count(), list.count()); list.prepend(str); list = list.mid(0, maxLines); @@ -199,40 +156,21 @@ void HistoryCompleterPrivate::saveEntry(const QString &str) theSettings->setValue(historyKey, list); } -bool HistoryCompleterPrivate::eventFilter(QObject *obj, QEvent *event) -{ - if (event->type() == QEvent::KeyPress && static_cast(event)->key() == Qt::Key_Down) { - completer->setCompletionPrefix(QString()); - completer->complete(); - } - return QAbstractListModel::eventFilter(obj,event); -} - -HistoryCompleter::HistoryCompleter(QObject *parent, const QString &historyKey) - : QCompleter(parent) - , d(new HistoryCompleterPrivate(this)) +HistoryCompleter::HistoryCompleter(QLineEdit *lineEdit, const QString &historyKey) + : d(new HistoryCompleterPrivate) { + QTC_ASSERT(lineEdit, return); QTC_ASSERT(!historyKey.isEmpty(), return); - - // make an assumption to allow pressing of the down - // key, before the first model run: - // parent is likely the lineedit + QTC_ASSERT(theSettings, return); d->historyKey = QLatin1String("CompleterHistory/") + historyKey; - - parent->installEventFilter(d); - QTC_ASSERT(theSettings, return); d->list = theSettings->value(d->historyKey).toStringList(); - - QLineEdit *l = qobject_cast(parent); - if (l && d->list.count()) - l->setText(d->list.at(0)); + if (d->list.count()) + lineEdit->setText(d->list.at(0)); setModel(d); - HistoryLineDelegate *delegate = new HistoryLineDelegate; - HistoryLineView *view = new HistoryLineView(d, delegate->pixmap.width()); - setPopup(view); - view->setItemDelegate(delegate); + setPopup(new HistoryLineView(d)); + lineEdit->installEventFilter(this); } HistoryCompleter::~HistoryCompleter() @@ -240,6 +178,15 @@ HistoryCompleter::~HistoryCompleter() delete d; } +bool HistoryCompleter::eventFilter(QObject *obj, QEvent *event) +{ + if (event->type() == QEvent::KeyPress && static_cast(event)->key() == Qt::Key_Down) { + setCompletionPrefix(QString()); + complete(); + } + return QCompleter::eventFilter(obj, event); +} + int HistoryCompleter::historySize() const { return d->rowCount(); diff --git a/src/libs/utils/historycompleter.h b/src/libs/utils/historycompleter.h index 9b8f73d94ac..8446262cc39 100644 --- a/src/libs/utils/historycompleter.h +++ b/src/libs/utils/historycompleter.h @@ -36,11 +36,11 @@ #include QT_BEGIN_NAMESPACE +class QLineEdit; class QSettings; QT_END_NAMESPACE namespace Utils { - namespace Internal { class HistoryCompleterPrivate; } class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter @@ -48,13 +48,15 @@ class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter Q_OBJECT public: - HistoryCompleter(QObject *parent, const QString &historyKey); + static void setSettings(QSettings *settings); + HistoryCompleter(QLineEdit *lineEdit, const QString &historyKey); + +private: ~HistoryCompleter(); int historySize() const; int maximalHistorySize() const; void setMaximalHistorySize(int numberOfEntries); - - static void setSettings(QSettings *settings); + bool eventFilter(QObject *obj, QEvent *event); public Q_SLOTS: void clearHistory(); diff --git a/src/plugins/cmakeprojectmanager/cmakeopenprojectwizard.cpp b/src/plugins/cmakeprojectmanager/cmakeopenprojectwizard.cpp index 879f0ae886a..76fae5dd7cb 100644 --- a/src/plugins/cmakeprojectmanager/cmakeopenprojectwizard.cpp +++ b/src/plugins/cmakeprojectmanager/cmakeopenprojectwizard.cpp @@ -33,7 +33,7 @@ #include #include -#include +#include #include #include #include @@ -295,9 +295,8 @@ void CMakeRunPage::initWidgets() } // Run CMake Line (with arguments) - m_argumentsLineEdit = new QLineEdit(this); - m_argumentsLineEdit->setCompleter( - new Utils::HistoryCompleter(m_argumentsLineEdit, QLatin1String("CMakeArgumentsLineEdit"))); + m_argumentsLineEdit = new Utils::FancyLineEdit(this); + m_argumentsLineEdit->setHistoryKey(QLatin1String("CMakeArgumentsLineEdit")); connect(m_argumentsLineEdit,SIGNAL(returnPressed()), this, SLOT(runCMake())); fl->addRow(tr("Arguments:"), m_argumentsLineEdit); diff --git a/src/plugins/cmakeprojectmanager/cmakeopenprojectwizard.h b/src/plugins/cmakeprojectmanager/cmakeopenprojectwizard.h index 7aee55ee5ba..7c3781f1a2c 100644 --- a/src/plugins/cmakeprojectmanager/cmakeopenprojectwizard.h +++ b/src/plugins/cmakeprojectmanager/cmakeopenprojectwizard.h @@ -42,7 +42,8 @@ #include namespace Utils { - class PathChooser; +class FancyLineEdit; +class PathChooser; } namespace ProjectExplorer { @@ -144,7 +145,7 @@ private: QPlainTextEdit *m_output; QPushButton *m_runCMake; Utils::QtcProcess *m_cmakeProcess; - QLineEdit *m_argumentsLineEdit; + Utils::FancyLineEdit *m_argumentsLineEdit; Utils::PathChooser *m_cmakeExecutable; QComboBox *m_generatorComboBox; QLabel *m_descriptionLabel; diff --git a/src/plugins/debugger/logwindow.cpp b/src/plugins/debugger/logwindow.cpp index 72124cfcb13..1bb421522f7 100644 --- a/src/plugins/debugger/logwindow.cpp +++ b/src/plugins/debugger/logwindow.cpp @@ -57,7 +57,7 @@ #include #include -#include +#include namespace Debugger { namespace Internal { @@ -341,9 +341,9 @@ LogWindow::LogWindow(QWidget *parent) QSizePolicy::MinimumExpanding); m_commandLabel = new QLabel(tr("Command:"), this); - m_commandEdit = new QLineEdit(this); + m_commandEdit = new Utils::FancyLineEdit(this); m_commandEdit->setFrame(false); - m_commandEdit->setCompleter(new Utils::HistoryCompleter(m_commandEdit, QLatin1String("DebuggerInput"))); + m_commandEdit->setHistoryKey(QLatin1String("DebuggerInput")); QHBoxLayout *commandBox = new QHBoxLayout; commandBox->addWidget(m_commandLabel); commandBox->addWidget(m_commandEdit); diff --git a/src/plugins/debugger/logwindow.h b/src/plugins/debugger/logwindow.h index 02fb0f540e5..e5b46b553ed 100644 --- a/src/plugins/debugger/logwindow.h +++ b/src/plugins/debugger/logwindow.h @@ -43,6 +43,8 @@ class QLineEdit; class QPlainTextEdit; QT_END_NAMESPACE +namespace Utils { class FancyLineEdit; } + namespace Debugger { namespace Internal { @@ -84,7 +86,7 @@ private: DebuggerPane *m_inputText; // scriptable input alone QTimer m_outputTimer; QString m_queuedOutput; - QLineEdit *m_commandEdit; + Utils::FancyLineEdit *m_commandEdit; QLabel *m_commandLabel; bool m_ignoreNextInputEcho; };