{
Q_OBJECT
public:
~ChmCompleter();
public slots:
void doneCompletion();
void preventSuggest();
void autoSuggest();
void showPopup();
private:
};
{
Q_OBJECT
public:
ChmComboBox
(QWidget *a_parent
= 0);
protected slots:
void doSearch();
signals:
void textEntered
(const QString &a_s
);
private:
ChmCompleter *m_completer;
};
class ChmCompleter : public QListView
{
Q_OBJECT
public:
ChmCompleter(QLineEdit *a_editor, QWidget *a_parent = 0);
~ChmCompleter();
bool eventFilter(QObject *a_obj, QEvent *a_event);
public slots:
void doneCompletion();
void preventSuggest();
void autoSuggest();
void showPopup();
private:
QLineEdit *m_editor;
QTimer *m_timer;
};
class ChmComboBox : public QComboBox
{
Q_OBJECT
public:
ChmComboBox(QWidget *a_parent = 0);
protected slots:
void doSearch();
signals:
void textEntered(const QString &a_s);
private:
ChmCompleter *m_completer;
};
To copy to clipboard, switch view to plain text mode
// ************* ChmCompleter *****************************
{
setWindowFlags(Qt::Popup);
setFocusPolicy(Qt::NoFocus);
setFocusProxy(a_editor);
setMouseTracking(true);
installEventFilter(this);
m_timer->setSingleShot(true);
m_timer->setInterval(500);
connect(m_timer, SIGNAL(timeout()), this, SLOT(autoSuggest()) );
connect(m_editor,
SIGNAL(textEdited
(QString)), m_timer,
SLOT(start
()) );
}
ChmCompleter::~ChmCompleter()
{
if(m_timer)
delete(m_timer);
}
{
if(a_obj != this)
return false;
if(a_event
->type
() == QEvent::MouseButtonPress) {
hide();
m_editor->setFocus();
return true;
}
if(a_event
->type
() == QEvent::KeyPress) {
bool consumed = false;
int key = static_cast<QKeyEvent*>(a_event)->key();
switch(key)
{
case Qt::Key_Enter:
case Qt::Key_Return:
doneCompletion();
consumed = true;
case Qt::Key_Escape:
m_editor->setFocus();
hide();
consumed = true;
case Qt::Key_Up:
case Qt::Key_Down:
case Qt::Key_Home:
case Qt::Key_End:
case Qt::Key_PageUp:
case Qt::Key_PageDown:
break;
default:
m_editor->setFocus();
m_editor->event(a_event);
hide();
break;
}
return consumed;
}
return false;
}
void ChmCompleter::showPopup()
{
QModelIndexList mi = model()->match(start, Qt::DisplayRole, m_editor->text(), 1, Qt::MatchStartsWith);
if( mi.count() > 0)
setFocus();
show();
}
void ChmCompleter::doneCompletion()
{
m_timer->stop();
hide();
m_editor->setFocus();
if(index.isValid())
{
m_editor->setText(model()->data(index).toString());
}
}
void ChmCompleter::preventSuggest()
{
m_timer->stop();
}
void ChmCompleter::autoSuggest()
{
if(!str.isEmpty())
showPopup();
}
// ************* ChmComboBox **************
ChmComboBox
::ChmComboBox(QWidget *a_parent
){
setEditable(true);
setCompleter(0);
m_completer = new ChmCompleter(this->lineEdit(), this);
connect(lineEdit(), SIGNAL(returnPressed()), this, SLOT(doSearch()) );
setFocus();
lineEdit()->setPlaceholderText("Type or select Y-variable");
setModel(model);
setView(m_completer);
m_completer->installEventFilter(m_completer);
m_completer->viewport()->installEventFilter(m_completer);
}
void ChmComboBox::doSearch()
{
m_completer->preventSuggest();
if(lineEdit()->text() == lineEdit()->placeholderText())
return;
else if(findText(lineEdit()->text()) >= 0)
emit(currentIndexChanged(lineEdit()->text()));
else
// ATTENTION!!! add popup dialog here
}
// ************* ChmCompleter *****************************
ChmCompleter::ChmCompleter(QLineEdit *a_editor, QWidget *a_parent)
: QListView(a_parent),m_editor(a_editor)
{
setWindowFlags(Qt::Popup);
setFocusPolicy(Qt::NoFocus);
setFocusProxy(a_editor);
setMouseTracking(true);
installEventFilter(this);
m_timer = new QTimer(this);
m_timer->setSingleShot(true);
m_timer->setInterval(500);
connect(m_timer, SIGNAL(timeout()), this, SLOT(autoSuggest()) );
connect(m_editor, SIGNAL(textEdited(QString)), m_timer, SLOT(start()) );
}
ChmCompleter::~ChmCompleter()
{
if(m_timer)
delete(m_timer);
}
bool ChmCompleter::eventFilter(QObject *a_obj, QEvent *a_event)
{
if(a_obj != this)
return false;
if(a_event->type() == QEvent::MouseButtonPress)
{
hide();
m_editor->setFocus();
return true;
}
if(a_event->type() == QEvent::KeyPress)
{
bool consumed = false;
int key = static_cast<QKeyEvent*>(a_event)->key();
switch(key)
{
case Qt::Key_Enter:
case Qt::Key_Return:
doneCompletion();
consumed = true;
case Qt::Key_Escape:
m_editor->setFocus();
hide();
consumed = true;
case Qt::Key_Up:
case Qt::Key_Down:
case Qt::Key_Home:
case Qt::Key_End:
case Qt::Key_PageUp:
case Qt::Key_PageDown:
break;
default:
m_editor->setFocus();
m_editor->event(a_event);
hide();
break;
}
return consumed;
}
return false;
}
void ChmCompleter::showPopup()
{
QModelIndex start = model()->index(0, 0);
QModelIndexList mi = model()->match(start, Qt::DisplayRole, m_editor->text(), 1, Qt::MatchStartsWith);
if( mi.count() > 0)
selectionModel()->setCurrentIndex(mi.at(0), QItemSelectionModel::Select);
setFocus();
show();
}
void ChmCompleter::doneCompletion()
{
m_timer->stop();
hide();
m_editor->setFocus();
QModelIndex index = selectionModel()->currentIndex();
if(index.isValid())
{
m_editor->setText(model()->data(index).toString());
QMetaObject::invokeMethod(m_editor, "returnPressed");
}
}
void ChmCompleter::preventSuggest()
{
m_timer->stop();
}
void ChmCompleter::autoSuggest()
{
QString str = m_editor->text();
if(!str.isEmpty())
showPopup();
}
// ************* ChmComboBox **************
ChmComboBox::ChmComboBox(QWidget *a_parent)
: QComboBox(a_parent)
{
setEditable(true);
setCompleter(0);
setInsertPolicy(QComboBox::NoInsert);
m_completer = new ChmCompleter(this->lineEdit(), this);
connect(lineEdit(), SIGNAL(returnPressed()), this, SLOT(doSearch()) );
setFocus();
lineEdit()->setPlaceholderText("Type or select Y-variable");
QStringListModel *model = new QStringListModel(this);
setModel(model);
setView(m_completer);
m_completer->installEventFilter(m_completer);
m_completer->viewport()->installEventFilter(m_completer);
}
void ChmComboBox::doSearch()
{
m_completer->preventSuggest();
if(lineEdit()->text() == lineEdit()->placeholderText())
return;
else if(findText(lineEdit()->text()) >= 0)
emit(currentIndexChanged(lineEdit()->text()));
else
// ATTENTION!!! add popup dialog here
}
To copy to clipboard, switch view to plain text mode
Bookmarks