103 lines
3.1 KiB
C++
103 lines
3.1 KiB
C++
![]() |
/*=============================================================================
|
||
|
Copyright (C) 2012 - 2013 Allied Vision Technologies. All Rights Reserved.
|
||
|
|
||
|
Redistribution of this file, in original or modified form, without
|
||
|
prior written consent of Allied Vision Technologies is prohibited.
|
||
|
|
||
|
-------------------------------------------------------------------------------
|
||
|
|
||
|
File: LineEditCompleter.cpp
|
||
|
|
||
|
Description: LineEdit implementation used by Filter
|
||
|
|
||
|
-------------------------------------------------------------------------------
|
||
|
|
||
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
|
||
|
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF TITLE,
|
||
|
NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||
|
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
||
|
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||
|
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
||
|
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
|
||
|
=============================================================================*/
|
||
|
|
||
|
#include "LineEditCompleter.h"
|
||
|
|
||
|
|
||
|
LineEditCompleter::LineEditCompleter(QWidget *parent) : QLineEdit(parent), c(0)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
LineEditCompleter::~LineEditCompleter()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void LineEditCompleter::setCompleter(MultiCompleter *completer)
|
||
|
{
|
||
|
if (c)
|
||
|
QObject::disconnect(c, 0, this, 0);
|
||
|
|
||
|
c = completer;
|
||
|
|
||
|
if (!c)
|
||
|
return;
|
||
|
|
||
|
c->setWidget(this);
|
||
|
connect(completer, SIGNAL(activated(const QString&)), this, SLOT(insertCompletion(const QString&)));
|
||
|
}
|
||
|
|
||
|
MultiCompleter *LineEditCompleter::completer() const
|
||
|
{
|
||
|
return c;
|
||
|
}
|
||
|
|
||
|
void LineEditCompleter::insertCompletion(const QString& completion)
|
||
|
{
|
||
|
setText(completion);
|
||
|
}
|
||
|
|
||
|
void LineEditCompleter::keyPressEvent(QKeyEvent *e)
|
||
|
{
|
||
|
if (c && c->popup()->isVisible())
|
||
|
{
|
||
|
/* The following keys are forwarded by the completer to the widget */
|
||
|
switch (e->key())
|
||
|
{
|
||
|
case Qt::Key_Enter:
|
||
|
case Qt::Key_Return:
|
||
|
case Qt::Key_Escape:
|
||
|
case Qt::Key_Tab:
|
||
|
case Qt::Key_Backtab:
|
||
|
e->ignore();
|
||
|
return; /* Let the completer do default behavior */
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool isShortcut = (e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E;
|
||
|
if (!isShortcut)
|
||
|
QLineEdit::keyPressEvent(e); /* Don't send the shortcut (CTRL-E) to the text edit. */
|
||
|
|
||
|
if (!c)
|
||
|
return;
|
||
|
|
||
|
bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
|
||
|
if (!isShortcut && !ctrlOrShift && e->modifiers() != Qt::NoModifier)
|
||
|
{
|
||
|
c->popup()->hide();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
c->update(text());
|
||
|
c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
|
||
|
|
||
|
if( Qt::Key_Enter == e->key())
|
||
|
{
|
||
|
c->popup()->hide();
|
||
|
}
|
||
|
}
|
||
|
|