diff --git a/src/libs/utils/completinglineedit.cpp b/src/libs/utils/completinglineedit.cpp new file mode 100644 index 00000000000..e6536e54e92 --- /dev/null +++ b/src/libs/utils/completinglineedit.cpp @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Orgad Shaneh . +** 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 +#include +#include +#include + +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(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 00000000000..26ebc39f0a5 --- /dev/null +++ b/src/libs/utils/completinglineedit.h @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Orgad Shaneh . +** 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 + +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 50289423d28..5956884f9a4 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(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 7ad7d6ee704..23571c51fcb 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 #include 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 df31af96a63..e47037379d1 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 #include -#include #include #include @@ -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(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 3d9352a6ae4..41cb01cb3f6 100644 --- a/src/libs/utils/historycompleter.h +++ b/src/libs/utils/historycompleter.h @@ -35,11 +35,12 @@ #include 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 2825a5661f6..319e2be935e 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 99174d119c1..5fc781e7d80 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",