Exemplo n.º 1
0
AutoComplete::AutoComplete(QWidget *parent, QLineEdit *editor):
        QObject(parent), buddy(parent), editor(editor), suggester(0) {

    enabled = true;

    popup = new QListWidget;
    popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    popup->installEventFilter(this);
    popup->setMouseTracking(true);
    popup->setWindowOpacity(.9);

    connect(popup, SIGNAL(itemClicked(QListWidgetItem*)),
            SLOT(doneCompletion()));

    // connect(popup, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)),
    //    SLOT(currentItemChanged(QListWidgetItem *)));

    // mouse hover
    // connect(popup, SIGNAL(itemEntered(QListWidgetItem*)),
    //    SLOT(currentItemChanged(QListWidgetItem *)));

    popup->setWindowFlags(Qt::Popup);
    popup->setFocusPolicy(Qt::NoFocus);
    popup->setFocusProxy(parent);

    timer = new QTimer(this);
    timer->setSingleShot(true);
    timer->setInterval(300);
    connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
    connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start()));

}
Exemplo n.º 2
0
SuggestCompletion::SuggestCompletion(QLineEdit *parent)
    : QObject(parent)
    , m_editor(parent)
{
    m_popup = new QTreeWidget;
    m_popup->setWindowFlags(Qt::Popup);
    m_popup->setFocusPolicy(Qt::NoFocus);
    m_popup->setFocusProxy(parent);
    m_popup->setMouseTracking(true);

    m_popup->setColumnCount(1);
    m_popup->setUniformRowHeights(true);
    m_popup->setRootIsDecorated(false);
    m_popup->setEditTriggers(QTreeWidget::NoEditTriggers);
    m_popup->setSelectionBehavior(QTreeWidget::SelectRows);
    m_popup->setFrameStyle(QFrame::Box | QFrame::Plain);
    m_popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_popup->header()->hide();

    m_popup->installEventFilter(this);

    connect(m_popup, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
            SLOT(doneCompletion()));

    m_timer = new QTimer(this);
    m_timer->setSingleShot(true);
    m_timer->setInterval(500);
    connect(m_timer, SIGNAL(timeout()), SLOT(autoSuggest()));

    connect(m_editor, SIGNAL(textEdited(QString)), m_timer, SLOT(start()));
}
Exemplo n.º 3
0
void KonqComboLineEdit::setCompletedItems( const QStringList& items, bool )
{
    QString txt;
    KonqComboCompletionBox *completionbox = static_cast<KonqComboCompletionBox*>( completionBox() );

    if ( completionbox && completionbox->isVisible() )
        // The popup is visible already - do the matching on the initial string,
        // not on the currently selected one.
        txt = completionbox->cancelledText();
    else
        txt = text();

    if ( !items.isEmpty() && !(items.count() == 1 && txt == items.first()) ) {
        if ( !completionBox( false ) ) {
            setCompletionBox( new KonqComboCompletionBox( this ) );
            completionbox = static_cast<KonqComboCompletionBox*>( completionBox() );
        }

        if ( completionbox->isVisible() ) {

            // This code is copied from KLineEdit::setCompletedItems
            QListWidgetItem* currentItem = completionbox->currentItem();

            QString currentSelection;
            if ( currentItem != 0 ) {
                currentSelection = currentItem->text();
            }

            completionbox->setItems( items );
            const QList<QListWidgetItem*> matchedItems = completionbox->findItems(currentSelection, Qt::MatchExactly);
            QListWidgetItem* matchedItem = matchedItems.isEmpty() ? 0 : matchedItems.first();

            if (matchedItem) {
                const bool blocked = completionbox->blockSignals(true);
                completionbox->setCurrentItem(matchedItem);
                completionbox->blockSignals(blocked);
            } else {
                completionbox->setCurrentRow(-1);
            }
        }
        else { // completion box not visible yet -> show it
            if ( !txt.isEmpty() )
                completionbox->setCancelledText( txt );
            completionbox->setItems( items );
            completionbox->popup();
        }

        if ( autoSuggest() ) {
            int index = items.first().indexOf( txt );
            QString newText = items.first().mid( index );
            setUserSelection( false );
            setCompletedText( newText, true );
        }
    }
    else
        if ( completionbox && completionbox->isVisible() )
            completionbox->hide();
}
Exemplo n.º 4
0
int GSuggestCompletion::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = QObject::qt_metacall(_c, _id, _a);
    if (_id < 0)
        return _id;
    if (_c == QMetaObject::InvokeMetaMethod) {
        switch (_id) {
        case 0: doneCompletion(); break;
        case 1: preventSuggest(); break;
        case 2: autoSuggest(); break;
        case 3: handleNetworkData((*reinterpret_cast< QNetworkReply*(*)>(_a[1]))); break;
        }
        _id -= 4;
    }
    return _id;
}
Exemplo n.º 5
0
GoogleSuggest::GoogleSuggest(LocationLineEdit *editor, QObject *parent)
  : QObject(parent), editor(editor), popup(new QListWidget()), m_enteredText(QString()) {
  popup->setWindowFlags(Qt::Popup);
  popup->setFocusPolicy(Qt::NoFocus);
  popup->setFocusProxy(editor);
  popup->setMouseTracking(true);
  popup->setSelectionBehavior(QAbstractItemView::SelectRows);
  popup->setFrameStyle(QFrame::Box | QFrame::Plain);
  popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  popup->installEventFilter(this);

  timer = new QTimer(this);
  timer->setSingleShot(true);
  timer->setInterval(500);

  connect(popup.data(), SIGNAL(itemClicked(QListWidgetItem*)), SLOT(doneCompletion()));
  connect(timer, SIGNAL(timeout()), SLOT(autoSuggest()));
  connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start()));
}