Commit 6c78cb30 authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Orgad Shaneh

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: default avatarEike Ziller <eike.ziller@digia.com>
parent 77b76640
/****************************************************************************
**
** 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
/****************************************************************************
**
** 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
......@@ -121,7 +121,7 @@ bool FancyLineEditPrivate::eventFilter(QObject *obj, QEvent *event)
// --------- FancyLineEdit
FancyLineEdit::FancyLineEdit(QWidget *parent) :
QLineEdit(parent),
CompletingLineEdit(parent),
d(new FancyLineEditPrivate(this))
{
ensurePolished();
......@@ -222,20 +222,6 @@ void FancyLineEdit::resizeEvent(QResizeEvent *)
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)
{
d->m_iconbutton[side]->setPixmap(buttonPixmap);
......
......@@ -31,8 +31,8 @@
#define FANCYLINEEDIT_H
#include "utils_global.h"
#include "completinglineedit.h"
#include <QLineEdit>
#include <QAbstractButton>
QT_BEGIN_NAMESPACE
......@@ -66,7 +66,7 @@ private:
QPixmap m_pixmap;
};
class QTCREATOR_UTILS_EXPORT FancyLineEdit : public QLineEdit
class QTCREATOR_UTILS_EXPORT FancyLineEdit : public CompletingLineEdit
{
Q_OBJECT
Q_ENUMS(Side)
......@@ -114,11 +114,10 @@ private slots:
protected:
void resizeEvent(QResizeEvent *e);
bool event(QEvent *e);
private:
// 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 updateMargins();
......
......@@ -28,6 +28,7 @@
****************************************************************************/
#include "historycompleter.h"
#include "fancylineedit.h"
#include "qtcassert.h"
......@@ -35,7 +36,6 @@
#include <QItemDelegate>
#include <QKeyEvent>
#include <QLineEdit>
#include <QListView>
#include <QPainter>
......@@ -59,7 +59,7 @@ public:
QStringList list;
QString historyKey;
int maxLines;
QLineEdit *lineEdit;
FancyLineEdit *lineEdit;
};
class HistoryLineDelegate : public QItemDelegate
......@@ -160,7 +160,7 @@ void HistoryCompleterPrivate::saveEntry(const QString &str)
theSettings->setValue(historyKey, list);
}
HistoryCompleter::HistoryCompleter(QLineEdit *lineEdit, const QString &historyKey, QObject *parent)
HistoryCompleter::HistoryCompleter(FancyLineEdit *lineEdit, const QString &historyKey, QObject *parent)
: QCompleter(parent),
d(new HistoryCompleterPrivate)
{
......@@ -176,7 +176,6 @@ HistoryCompleter::HistoryCompleter(QLineEdit *lineEdit, const QString &historyKe
setModel(d);
setPopup(new HistoryLineView(d));
lineEdit->installEventFilter(this);
connect(lineEdit, SIGNAL(editingFinished()), this, SLOT(saveHistory()));
}
......@@ -191,17 +190,6 @@ HistoryCompleter::~HistoryCompleter()
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
{
return d->rowCount();
......
......@@ -35,11 +35,12 @@
#include <QCompleter>
QT_BEGIN_NAMESPACE
class QLineEdit;
class QSettings;
QT_END_NAMESPACE
namespace Utils {
class FancyLineEdit;
namespace Internal { class HistoryCompleterPrivate; }
class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter
......@@ -48,7 +49,7 @@ class QTCREATOR_UTILS_EXPORT HistoryCompleter : public QCompleter
public:
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);
private:
......@@ -56,7 +57,6 @@ private:
int historySize() const;
int maximalHistorySize() const;
void setMaximalHistorySize(int numberOfEntries);
bool eventFilter(QObject *obj, QEvent *event);
public Q_SLOTS:
void clearHistory();
......
......@@ -87,7 +87,8 @@ SOURCES += $$PWD/environment.cpp \
$$PWD/unixutils.cpp \
$$PWD/function.cpp \
$$PWD/ansiescapecodehandler.cpp \
$$PWD/execmenu.cpp
$$PWD/execmenu.cpp \
$$PWD/completinglineedit.cpp
win32 {
SOURCES += \
......@@ -182,7 +183,8 @@ HEADERS += \
$$PWD/qtcoverride.h \
$$PWD/function.h \
$$PWD/ansiescapecodehandler.h \
$$PWD/execmenu.h
$$PWD/execmenu.h \
$$PWD/completinglineedit.h
FORMS += $$PWD/filewizardpage.ui \
$$PWD/projectintropage.ui \
......
......@@ -45,6 +45,8 @@ QtcLibrary {
"classnamevalidatinglineedit.h",
"codegeneration.cpp",
"codegeneration.h",
"completinglineedit.cpp",
"completinglineedit.h",
"completingtextedit.cpp",
"completingtextedit.h",
"consoleprocess.cpp",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment