close down history completer interface

Change-Id: Iaeff40be410d1f0facd687632c9ce4897eb2e613
Reviewed-by: Eike Ziller <eike.ziller@nokia.com>
This commit is contained in:
hjk
2012-08-23 14:26:24 +02:00
parent 903eeaf42f
commit 88f5949e38
7 changed files with 47 additions and 97 deletions

View File

@@ -114,7 +114,6 @@ public:
bool m_iconEnabled[2]; bool m_iconEnabled[2];
HistoryCompleter *m_completer; HistoryCompleter *m_completer;
QString m_historyKey;
}; };
@@ -306,8 +305,8 @@ bool FancyLineEdit::hasAutoHideButton(Side side) const
void FancyLineEdit::setHistoryKey(const QString &historyKey) void FancyLineEdit::setHistoryKey(const QString &historyKey)
{ {
QTC_ASSERT(!d->m_completer, return); QTC_ASSERT(!d->m_completer, return);
d->m_historyKey = historyKey;
d->m_completer = new HistoryCompleter(this, historyKey); d->m_completer = new HistoryCompleter(this, historyKey);
QLineEdit::setCompleter(d->m_completer);
} }
void FancyLineEdit::setSpecialCompleter(QCompleter *completer) void FancyLineEdit::setSpecialCompleter(QCompleter *completer)

View File

@@ -29,6 +29,7 @@
**************************************************************************/ **************************************************************************/
#include "historycompleter.h" #include "historycompleter.h"
#include "qtcassert.h" #include "qtcassert.h"
#include <QAbstractListModel> #include <QAbstractListModel>
@@ -49,20 +50,17 @@ static QSettings *theSettings = 0;
class HistoryCompleterPrivate : public QAbstractListModel class HistoryCompleterPrivate : public QAbstractListModel
{ {
public: public:
HistoryCompleterPrivate(HistoryCompleter *parent); 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 fetchHistory();
void clearHistory(); void clearHistory();
void saveEntry(const QString &str); void saveEntry(const QString &str);
bool eventFilter(QObject *obj, QEvent *event);
QStringList list; QStringList list;
QString historyKey; QString historyKey;
HistoryCompleter *completer;
QWidget *lastSeenWidget;
int maxLines; int maxLines;
}; };
@@ -86,9 +84,13 @@ public:
class HistoryLineView : public QListView class HistoryLineView : public QListView
{ {
public: public:
HistoryLineView(HistoryCompleterPrivate *model_, int pixmapWith_) HistoryLineView(HistoryCompleterPrivate *model_)
: model(model_) , pixmapWidth(pixmapWith_) : model(model_)
{} {
HistoryLineDelegate *delegate = new HistoryLineDelegate;
pixmapWidth = delegate->pixmap.width();
setItemDelegate(delegate);
}
private: private:
void mousePressEvent(QMouseEvent *event) void mousePressEvent(QMouseEvent *event)
@@ -111,45 +113,9 @@ private:
using namespace Internal; 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 int HistoryCompleterPrivate::rowCount(const QModelIndex &parent) const
{ {
if (lastSeenWidget != completer->widget()) { return parent.isValid() ? 0 : list.count();
if (lastSeenWidget)
lastSeenWidget->removeEventFilter(const_cast<HistoryCompleterPrivate *>(this));
completer->widget()->installEventFilter(const_cast<HistoryCompleterPrivate *>(this));
if (qobject_cast<QLineEdit *>(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<HistoryCompleterPrivate *>(this);
that->lastSeenWidget = completer->widget();
that->fetchHistory();
if (qobject_cast<QLineEdit *>(lastSeenWidget))
connect(lastSeenWidget, SIGNAL(editingFinished()), completer, SLOT(saveHistory()));
}
if (parent.isValid())
return 0;
return list.count();
} }
QVariant HistoryCompleterPrivate::data(const QModelIndex &index, int role) const QVariant HistoryCompleterPrivate::data(const QModelIndex &index, int role) const
@@ -183,15 +149,6 @@ void HistoryCompleterPrivate::saveEntry(const QString &str)
return; return;
if (list.contains(str)) if (list.contains(str))
return; 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()); beginInsertRows (QModelIndex(), list.count(), list.count());
list.prepend(str); list.prepend(str);
list = list.mid(0, maxLines); list = list.mid(0, maxLines);
@@ -199,40 +156,21 @@ void HistoryCompleterPrivate::saveEntry(const QString &str)
theSettings->setValue(historyKey, list); theSettings->setValue(historyKey, list);
} }
bool HistoryCompleterPrivate::eventFilter(QObject *obj, QEvent *event) HistoryCompleter::HistoryCompleter(QLineEdit *lineEdit, const QString &historyKey)
{ : d(new HistoryCompleterPrivate)
if (event->type() == QEvent::KeyPress && static_cast<QKeyEvent *>(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))
{ {
QTC_ASSERT(lineEdit, return);
QTC_ASSERT(!historyKey.isEmpty(), return); QTC_ASSERT(!historyKey.isEmpty(), return);
QTC_ASSERT(theSettings, return);
// make an assumption to allow pressing of the down
// key, before the first model run:
// parent is likely the lineedit
d->historyKey = QLatin1String("CompleterHistory/") + historyKey; d->historyKey = QLatin1String("CompleterHistory/") + historyKey;
parent->installEventFilter(d);
QTC_ASSERT(theSettings, return);
d->list = theSettings->value(d->historyKey).toStringList(); d->list = theSettings->value(d->historyKey).toStringList();
if (d->list.count())
QLineEdit *l = qobject_cast<QLineEdit *>(parent); lineEdit->setText(d->list.at(0));
if (l && d->list.count())
l->setText(d->list.at(0));
setModel(d); setModel(d);
HistoryLineDelegate *delegate = new HistoryLineDelegate; setPopup(new HistoryLineView(d));
HistoryLineView *view = new HistoryLineView(d, delegate->pixmap.width()); lineEdit->installEventFilter(this);
setPopup(view);
view->setItemDelegate(delegate);
} }
HistoryCompleter::~HistoryCompleter() HistoryCompleter::~HistoryCompleter()
@@ -240,6 +178,15 @@ HistoryCompleter::~HistoryCompleter()
delete d; delete d;
} }
bool HistoryCompleter::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyPress && static_cast<QKeyEvent *>(event)->key() == Qt::Key_Down) {
setCompletionPrefix(QString());
complete();
}
return QCompleter::eventFilter(obj, event);
}
int HistoryCompleter::historySize() const int HistoryCompleter::historySize() const
{ {
return d->rowCount(); return d->rowCount();

View File

@@ -36,11 +36,11 @@
#include <QCompleter> #include <QCompleter>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QLineEdit;
class QSettings; class QSettings;
QT_END_NAMESPACE QT_END_NAMESPACE
namespace Utils { namespace Utils {
namespace Internal { class HistoryCompleterPrivate; } namespace Internal { class HistoryCompleterPrivate; }
class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter
@@ -48,13 +48,15 @@ class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter
Q_OBJECT Q_OBJECT
public: public:
HistoryCompleter(QObject *parent, const QString &historyKey); static void setSettings(QSettings *settings);
HistoryCompleter(QLineEdit *lineEdit, const QString &historyKey);
private:
~HistoryCompleter(); ~HistoryCompleter();
int historySize() const; int historySize() const;
int maximalHistorySize() const; int maximalHistorySize() const;
void setMaximalHistorySize(int numberOfEntries); void setMaximalHistorySize(int numberOfEntries);
bool eventFilter(QObject *obj, QEvent *event);
static void setSettings(QSettings *settings);
public Q_SLOTS: public Q_SLOTS:
void clearHistory(); void clearHistory();

View File

@@ -33,7 +33,7 @@
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <utils/pathchooser.h> #include <utils/pathchooser.h>
#include <utils/historycompleter.h> #include <utils/fancylineedit.h>
#include <projectexplorer/profileinformation.h> #include <projectexplorer/profileinformation.h>
#include <projectexplorer/profilemanager.h> #include <projectexplorer/profilemanager.h>
#include <projectexplorer/toolchain.h> #include <projectexplorer/toolchain.h>
@@ -295,9 +295,8 @@ void CMakeRunPage::initWidgets()
} }
// Run CMake Line (with arguments) // Run CMake Line (with arguments)
m_argumentsLineEdit = new QLineEdit(this); m_argumentsLineEdit = new Utils::FancyLineEdit(this);
m_argumentsLineEdit->setCompleter( m_argumentsLineEdit->setHistoryKey(QLatin1String("CMakeArgumentsLineEdit"));
new Utils::HistoryCompleter(m_argumentsLineEdit, QLatin1String("CMakeArgumentsLineEdit")));
connect(m_argumentsLineEdit,SIGNAL(returnPressed()), this, SLOT(runCMake())); connect(m_argumentsLineEdit,SIGNAL(returnPressed()), this, SLOT(runCMake()));
fl->addRow(tr("Arguments:"), m_argumentsLineEdit); fl->addRow(tr("Arguments:"), m_argumentsLineEdit);

View File

@@ -42,7 +42,8 @@
#include <QPlainTextEdit> #include <QPlainTextEdit>
namespace Utils { namespace Utils {
class PathChooser; class FancyLineEdit;
class PathChooser;
} }
namespace ProjectExplorer { namespace ProjectExplorer {
@@ -144,7 +145,7 @@ private:
QPlainTextEdit *m_output; QPlainTextEdit *m_output;
QPushButton *m_runCMake; QPushButton *m_runCMake;
Utils::QtcProcess *m_cmakeProcess; Utils::QtcProcess *m_cmakeProcess;
QLineEdit *m_argumentsLineEdit; Utils::FancyLineEdit *m_argumentsLineEdit;
Utils::PathChooser *m_cmakeExecutable; Utils::PathChooser *m_cmakeExecutable;
QComboBox *m_generatorComboBox; QComboBox *m_generatorComboBox;
QLabel *m_descriptionLabel; QLabel *m_descriptionLabel;

View File

@@ -57,7 +57,7 @@
#include <utils/savedaction.h> #include <utils/savedaction.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/historycompleter.h> #include <utils/fancylineedit.h>
namespace Debugger { namespace Debugger {
namespace Internal { namespace Internal {
@@ -341,9 +341,9 @@ LogWindow::LogWindow(QWidget *parent)
QSizePolicy::MinimumExpanding); QSizePolicy::MinimumExpanding);
m_commandLabel = new QLabel(tr("Command:"), this); m_commandLabel = new QLabel(tr("Command:"), this);
m_commandEdit = new QLineEdit(this); m_commandEdit = new Utils::FancyLineEdit(this);
m_commandEdit->setFrame(false); m_commandEdit->setFrame(false);
m_commandEdit->setCompleter(new Utils::HistoryCompleter(m_commandEdit, QLatin1String("DebuggerInput"))); m_commandEdit->setHistoryKey(QLatin1String("DebuggerInput"));
QHBoxLayout *commandBox = new QHBoxLayout; QHBoxLayout *commandBox = new QHBoxLayout;
commandBox->addWidget(m_commandLabel); commandBox->addWidget(m_commandLabel);
commandBox->addWidget(m_commandEdit); commandBox->addWidget(m_commandEdit);

View File

@@ -43,6 +43,8 @@ class QLineEdit;
class QPlainTextEdit; class QPlainTextEdit;
QT_END_NAMESPACE QT_END_NAMESPACE
namespace Utils { class FancyLineEdit; }
namespace Debugger { namespace Debugger {
namespace Internal { namespace Internal {
@@ -84,7 +86,7 @@ private:
DebuggerPane *m_inputText; // scriptable input alone DebuggerPane *m_inputText; // scriptable input alone
QTimer m_outputTimer; QTimer m_outputTimer;
QString m_queuedOutput; QString m_queuedOutput;
QLineEdit *m_commandEdit; Utils::FancyLineEdit *m_commandEdit;
QLabel *m_commandLabel; QLabel *m_commandLabel;
bool m_ignoreNextInputEcho; bool m_ignoreNextInputEcho;
}; };