diff options
author | Orgad Shaneh <orgad.shaneh@audiocodes.com> | 2013-12-13 14:34:28 +0200 |
---|---|---|
committer | Orgad Shaneh <orgads@gmail.com> | 2014-01-14 11:30:48 +0100 |
commit | 6c78cb303e71fef9564fe7d1b3b5ec9870e318d5 (patch) | |
tree | 775c15187c9c46631bbfbf0a9725361627e082ca | |
parent | 77b766400e38bd460d1319a3e0b7eaa004e6225f (diff) | |
download | qt-creator-6c78cb303e71fef9564fe7d1b3b5ec9870e318d5.tar.gz |
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>
-rw-r--r-- | src/libs/utils/completinglineedit.cpp | 74 | ||||
-rw-r--r-- | src/libs/utils/completinglineedit.h | 52 | ||||
-rw-r--r-- | src/libs/utils/fancylineedit.cpp | 16 | ||||
-rw-r--r-- | src/libs/utils/fancylineedit.h | 7 | ||||
-rw-r--r-- | src/libs/utils/historycompleter.cpp | 18 | ||||
-rw-r--r-- | src/libs/utils/historycompleter.h | 6 | ||||
-rw-r--r-- | src/libs/utils/utils-lib.pri | 6 | ||||
-rw-r--r-- | src/libs/utils/utils.qbs | 2 |
8 files changed, 142 insertions, 39 deletions
diff --git a/src/libs/utils/completinglineedit.cpp b/src/libs/utils/completinglineedit.cpp new file mode 100644 index 0000000000..e6536e54e9 --- /dev/null +++ b/src/libs/utils/completinglineedit.cpp @@ -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 diff --git a/src/libs/utils/completinglineedit.h b/src/libs/utils/completinglineedit.h new file mode 100644 index 0000000000..26ebc39f0a --- /dev/null +++ b/src/libs/utils/completinglineedit.h @@ -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 diff --git a/src/libs/utils/fancylineedit.cpp b/src/libs/utils/fancylineedit.cpp index 50289423d2..5956884f9a 100644 --- a/src/libs/utils/fancylineedit.cpp +++ b/src/libs/utils/fancylineedit.cpp @@ -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); diff --git a/src/libs/utils/fancylineedit.h b/src/libs/utils/fancylineedit.h index 7ad7d6ee70..23571c51fc 100644 --- a/src/libs/utils/fancylineedit.h +++ b/src/libs/utils/fancylineedit.h @@ -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(); diff --git a/src/libs/utils/historycompleter.cpp b/src/libs/utils/historycompleter.cpp index df31af96a6..e47037379d 100644 --- a/src/libs/utils/historycompleter.cpp +++ b/src/libs/utils/historycompleter.cpp @@ -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(); diff --git a/src/libs/utils/historycompleter.h b/src/libs/utils/historycompleter.h index 3d9352a6ae..41cb01cb3f 100644 --- a/src/libs/utils/historycompleter.h +++ b/src/libs/utils/historycompleter.h @@ -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(); diff --git a/src/libs/utils/utils-lib.pri b/src/libs/utils/utils-lib.pri index 2825a5661f..319e2be935 100644 --- a/src/libs/utils/utils-lib.pri +++ b/src/libs/utils/utils-lib.pri @@ -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 \ diff --git a/src/libs/utils/utils.qbs b/src/libs/utils/utils.qbs index 99174d119c..5fc781e7d8 100644 --- a/src/libs/utils/utils.qbs +++ b/src/libs/utils/utils.qbs @@ -45,6 +45,8 @@ QtcLibrary { "classnamevalidatinglineedit.h", "codegeneration.cpp", "codegeneration.h", + "completinglineedit.cpp", + "completinglineedit.h", "completingtextedit.cpp", "completingtextedit.h", "consoleprocess.cpp", |