To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / widgets / TypingSelectWidget.cpp
History | View | Annotate | Download (2.53 KB)
| 1 |
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
|---|---|
| 2 |
|
| 3 |
#include <dataquay/Uri.h> |
| 4 |
|
| 5 |
#include "TypingSelectWidget.h" |
| 6 |
|
| 7 |
#include <QGridLayout> |
| 8 |
#include <QLineEdit> |
| 9 |
#include <QListWidget> |
| 10 |
#include <QLabel> |
| 11 |
#include <QApplication> |
| 12 |
#include <QMenu> |
| 13 |
|
| 14 |
#include "Matcher.h" |
| 15 |
|
| 16 |
#include "Objects.h" |
| 17 |
|
| 18 |
using namespace Dataquay; |
| 19 |
|
| 20 |
namespace ClassicalData {
|
| 21 |
|
| 22 |
TypingSelectWidget::TypingSelectWidget(QWidget *parent) : |
| 23 |
QWidget(parent) |
| 24 |
{
|
| 25 |
QGridLayout *layout = new QGridLayout;
|
| 26 |
setLayout(layout); |
| 27 |
|
| 28 |
m_editor = new QLineEdit;
|
| 29 |
layout->addWidget(m_editor); |
| 30 |
|
| 31 |
connect(m_editor, SIGNAL(textEdited(const QString &)),
|
| 32 |
this, SLOT(textEdited(const QString &))); |
| 33 |
|
| 34 |
m_menu = new TypingSelectListMenu(this); |
| 35 |
connect(m_menu, SIGNAL(keyPressed(QKeyEvent *)), |
| 36 |
this, SLOT(menuKeyPressed(QKeyEvent *)));
|
| 37 |
connect(m_menu, SIGNAL(itemSelected(QString)), |
| 38 |
this, SLOT(menuItemSelected(QString)));
|
| 39 |
} |
| 40 |
|
| 41 |
void
|
| 42 |
TypingSelectWidget::menuKeyPressed(QKeyEvent *ev) |
| 43 |
{
|
| 44 |
m_editor->setFocus(); |
| 45 |
QApplication::sendEvent(m_editor, ev); |
| 46 |
} |
| 47 |
|
| 48 |
void
|
| 49 |
TypingSelectWidget::menuItemSelected(QString s) |
| 50 |
{
|
| 51 |
m_editor->setFocus(); |
| 52 |
m_editor->setText(s); |
| 53 |
m_menu->hide(); |
| 54 |
} |
| 55 |
|
| 56 |
void
|
| 57 |
TypingSelectWidget::textEdited(const QString &s)
|
| 58 |
{
|
| 59 |
if (m_matchers.empty()) return; |
| 60 |
|
| 61 |
GuessList results = m_matchers[0]->match(s, 10); |
| 62 |
|
| 63 |
if (results.empty()) {
|
| 64 |
m_menu->hide(); |
| 65 |
return;
|
| 66 |
} |
| 67 |
|
| 68 |
m_menu->clear(); |
| 69 |
|
| 70 |
foreach (Guess g, results) {
|
| 71 |
Composer *c = qobject_cast<Composer *>(g.entity()); |
| 72 |
if (!c) continue; |
| 73 |
m_menu->addAction(c->getSortName(true));
|
| 74 |
} |
| 75 |
m_menu->popup(m_editor->mapToGlobal(QPoint(0, m_editor->height())));
|
| 76 |
|
| 77 |
std::cerr << std::endl; |
| 78 |
} |
| 79 |
|
| 80 |
void
|
| 81 |
TypingSelectListMenu::keyPressEvent(QKeyEvent *e) |
| 82 |
{
|
| 83 |
switch (e->key()) {
|
| 84 |
// permit QMenu to have all its normal keystrokes, except for
|
| 85 |
// left, right, and space which are used by the text field
|
| 86 |
case Qt::Key_Tab:
|
| 87 |
case Qt::Key_Backtab:
|
| 88 |
case Qt::Key_PageUp:
|
| 89 |
case Qt::Key_PageDown:
|
| 90 |
case Qt::Key_Up:
|
| 91 |
case Qt::Key_Down:
|
| 92 |
case Qt::Key_Escape:
|
| 93 |
case Qt::Key_Back:
|
| 94 |
QMenu::keyPressEvent(e); |
| 95 |
return;
|
| 96 |
} |
| 97 |
|
| 98 |
if (e->key() == Qt::Key_Select ||
|
| 99 |
e->key() == Qt::Key_Return || |
| 100 |
e->key() == Qt::Key_Enter) {
|
| 101 |
QAction *a = activeAction(); |
| 102 |
if (a) {
|
| 103 |
emit itemSelected(a->text()); |
| 104 |
return;
|
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
emit keyPressed(e); |
| 109 |
} |
| 110 |
|
| 111 |
void
|
| 112 |
TypingSelectListMenu::keyReleaseEvent(QKeyEvent *e) |
| 113 |
{
|
| 114 |
std::cerr << "keyReleaseEvent " << this << std::endl; |
| 115 |
QMenu::keyReleaseEvent(e); |
| 116 |
} |
| 117 |
|
| 118 |
} |
| 119 |
|