Utils: Introduce CompletingLineEdit

* Works around QTCREATORBUG-9453
* Triggers completion on Key_Down with empty prefix
* Replace current filters in HistoryCompleter and FancyLineEdit

Change-Id: I56bfd4e0ee969c5ae674de2f2de1081fcf6dc176
Reviewed-by: Eike Ziller <eike.ziller@digia.com>
This commit is contained in:
Orgad Shaneh
2013-12-13 14:34:28 +02:00
committed by Orgad Shaneh
parent 77b766400e
commit 6c78cb303e
8 changed files with 142 additions and 39 deletions

View File

@@ -0,0 +1,74 @@
/****************************************************************************
**
** Copyright (C) 2014 Orgad Shaneh <orgads@gmail.com>.
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "completinglineedit.h"
#include <QAbstractItemView>
#include <QCompleter>
#include <QEvent>
#include <QKeyEvent>
namespace Utils {
CompletingLineEdit::CompletingLineEdit(QWidget *parent) :
QLineEdit(parent)
{
}
bool CompletingLineEdit::event(QEvent *e)
{
// workaround for QTCREATORBUG-9453
if (e->type() == QEvent::ShortcutOverride) {
if (QCompleter *comp = completer()) {
if (comp->popup() && comp->popup()->isVisible()) {
QKeyEvent *ke = static_cast<QKeyEvent *>(e);
if (ke->key() == Qt::Key_Escape && !ke->modifiers()) {
ke->accept();
return true;
}
}
}
}
return QLineEdit::event(e);
}
void CompletingLineEdit::keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_Down && !e->modifiers()) {
if (QCompleter *comp = completer()) {
if (text().isEmpty() && !comp->popup()->isVisible()) {
comp->setCompletionPrefix(QString());
comp->complete();
}
}
}
return QLineEdit::keyPressEvent(e);
}
} // namespace Utils

View File

@@ -0,0 +1,52 @@
/****************************************************************************
**
** Copyright (C) 2014 Orgad Shaneh <orgads@gmail.com>.
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef COMPLETINGLINEEDIT_H
#define COMPLETINGLINEEDIT_H
#include "utils_global.h"
#include <QLineEdit>
namespace Utils {
class QTCREATOR_UTILS_EXPORT CompletingLineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit CompletingLineEdit(QWidget *parent = 0);
protected:
bool event(QEvent *e);
void keyPressEvent(QKeyEvent *e);
};
} // namespace Utils
#endif // COMPLETINGLINEEDIT_H

View File

@@ -121,7 +121,7 @@ bool FancyLineEditPrivate::eventFilter(QObject *obj, QEvent *event)
// --------- FancyLineEdit // --------- FancyLineEdit
FancyLineEdit::FancyLineEdit(QWidget *parent) : FancyLineEdit::FancyLineEdit(QWidget *parent) :
QLineEdit(parent), CompletingLineEdit(parent),
d(new FancyLineEditPrivate(this)) d(new FancyLineEditPrivate(this))
{ {
ensurePolished(); ensurePolished();
@@ -222,20 +222,6 @@ void FancyLineEdit::resizeEvent(QResizeEvent *)
updateButtonPositions(); updateButtonPositions();
} }
bool FancyLineEdit::event(QEvent *e)
{
// workaround for QTCREATORBUG-9453
if (e->type() == QEvent::ShortcutOverride && completer()
&& completer()->popup() && completer()->popup()->isVisible()) {
QKeyEvent *ke = static_cast<QKeyEvent *>(e);
if (ke->key() == Qt::Key_Escape && !ke->modifiers()) {
ke->accept();
return true;
}
}
return QLineEdit::event(e);
}
void FancyLineEdit::setButtonPixmap(Side side, const QPixmap &buttonPixmap) void FancyLineEdit::setButtonPixmap(Side side, const QPixmap &buttonPixmap)
{ {
d->m_iconbutton[side]->setPixmap(buttonPixmap); d->m_iconbutton[side]->setPixmap(buttonPixmap);

View File

@@ -31,8 +31,8 @@
#define FANCYLINEEDIT_H #define FANCYLINEEDIT_H
#include "utils_global.h" #include "utils_global.h"
#include "completinglineedit.h"
#include <QLineEdit>
#include <QAbstractButton> #include <QAbstractButton>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@@ -66,7 +66,7 @@ private:
QPixmap m_pixmap; QPixmap m_pixmap;
}; };
class QTCREATOR_UTILS_EXPORT FancyLineEdit : public QLineEdit class QTCREATOR_UTILS_EXPORT FancyLineEdit : public CompletingLineEdit
{ {
Q_OBJECT Q_OBJECT
Q_ENUMS(Side) Q_ENUMS(Side)
@@ -114,11 +114,10 @@ private slots:
protected: protected:
void resizeEvent(QResizeEvent *e); void resizeEvent(QResizeEvent *e);
bool event(QEvent *e);
private: private:
// Unimplemented, to force the user to make a decision on // Unimplemented, to force the user to make a decision on
// whether to use setHistoryKey() or setSpecialCompleter(). // whether to use setHistoryCompleter() or setSpecialCompleter().
void setCompleter(QCompleter *); void setCompleter(QCompleter *);
void updateMargins(); void updateMargins();

View File

@@ -28,6 +28,7 @@
****************************************************************************/ ****************************************************************************/
#include "historycompleter.h" #include "historycompleter.h"
#include "fancylineedit.h"
#include "qtcassert.h" #include "qtcassert.h"
@@ -35,7 +36,6 @@
#include <QItemDelegate> #include <QItemDelegate>
#include <QKeyEvent> #include <QKeyEvent>
#include <QLineEdit>
#include <QListView> #include <QListView>
#include <QPainter> #include <QPainter>
@@ -59,7 +59,7 @@ public:
QStringList list; QStringList list;
QString historyKey; QString historyKey;
int maxLines; int maxLines;
QLineEdit *lineEdit; FancyLineEdit *lineEdit;
}; };
class HistoryLineDelegate : public QItemDelegate class HistoryLineDelegate : public QItemDelegate
@@ -160,7 +160,7 @@ void HistoryCompleterPrivate::saveEntry(const QString &str)
theSettings->setValue(historyKey, list); theSettings->setValue(historyKey, list);
} }
HistoryCompleter::HistoryCompleter(QLineEdit *lineEdit, const QString &historyKey, QObject *parent) HistoryCompleter::HistoryCompleter(FancyLineEdit *lineEdit, const QString &historyKey, QObject *parent)
: QCompleter(parent), : QCompleter(parent),
d(new HistoryCompleterPrivate) d(new HistoryCompleterPrivate)
{ {
@@ -176,7 +176,6 @@ HistoryCompleter::HistoryCompleter(QLineEdit *lineEdit, const QString &historyKe
setModel(d); setModel(d);
setPopup(new HistoryLineView(d)); setPopup(new HistoryLineView(d));
lineEdit->installEventFilter(this);
connect(lineEdit, SIGNAL(editingFinished()), this, SLOT(saveHistory())); connect(lineEdit, SIGNAL(editingFinished()), this, SLOT(saveHistory()));
} }
@@ -191,17 +190,6 @@ 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
&& !popup()->isVisible()) {
setCompletionPrefix(QString());
complete();
}
return QCompleter::eventFilter(obj, event);
}
int HistoryCompleter::historySize() const int HistoryCompleter::historySize() const
{ {
return d->rowCount(); return d->rowCount();

View File

@@ -35,11 +35,12 @@
#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 {
class FancyLineEdit;
namespace Internal { class HistoryCompleterPrivate; } namespace Internal { class HistoryCompleterPrivate; }
class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter
@@ -48,7 +49,7 @@ class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter
public: public:
static void setSettings(QSettings *settings); static void setSettings(QSettings *settings);
HistoryCompleter(QLineEdit *lineEdit, const QString &historyKey, QObject *parent = 0); HistoryCompleter(FancyLineEdit *lineEdit, const QString &historyKey, QObject *parent = 0);
bool removeHistoryItem(int index); bool removeHistoryItem(int index);
private: private:
@@ -56,7 +57,6 @@ private:
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);
public Q_SLOTS: public Q_SLOTS:
void clearHistory(); void clearHistory();

View File

@@ -87,7 +87,8 @@ SOURCES += $$PWD/environment.cpp \
$$PWD/unixutils.cpp \ $$PWD/unixutils.cpp \
$$PWD/function.cpp \ $$PWD/function.cpp \
$$PWD/ansiescapecodehandler.cpp \ $$PWD/ansiescapecodehandler.cpp \
$$PWD/execmenu.cpp $$PWD/execmenu.cpp \
$$PWD/completinglineedit.cpp
win32 { win32 {
SOURCES += \ SOURCES += \
@@ -182,7 +183,8 @@ HEADERS += \
$$PWD/qtcoverride.h \ $$PWD/qtcoverride.h \
$$PWD/function.h \ $$PWD/function.h \
$$PWD/ansiescapecodehandler.h \ $$PWD/ansiescapecodehandler.h \
$$PWD/execmenu.h $$PWD/execmenu.h \
$$PWD/completinglineedit.h
FORMS += $$PWD/filewizardpage.ui \ FORMS += $$PWD/filewizardpage.ui \
$$PWD/projectintropage.ui \ $$PWD/projectintropage.ui \

View File

@@ -45,6 +45,8 @@ QtcLibrary {
"classnamevalidatinglineedit.h", "classnamevalidatinglineedit.h",
"codegeneration.cpp", "codegeneration.cpp",
"codegeneration.h", "codegeneration.h",
"completinglineedit.cpp",
"completinglineedit.h",
"completingtextedit.cpp", "completingtextedit.cpp",
"completingtextedit.h", "completingtextedit.h",
"consoleprocess.cpp", "consoleprocess.cpp",