summaryrefslogtreecommitdiff
path: root/src/frontend/keychooser/ckeychooserwidget.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/frontend/keychooser/ckeychooserwidget.cpp')
-rw-r--r--src/frontend/keychooser/ckeychooserwidget.cpp138
1 files changed, 100 insertions, 38 deletions
diff --git a/src/frontend/keychooser/ckeychooserwidget.cpp b/src/frontend/keychooser/ckeychooserwidget.cpp
index dd8f7a5..d3e1a55 100644
--- a/src/frontend/keychooser/ckeychooserwidget.cpp
+++ b/src/frontend/keychooser/ckeychooserwidget.cpp
@@ -22,20 +22,46 @@
#include <QLineEdit>
#include <QDebug>
+class BtKeyLineEdit : public QLineEdit
+{
+public:
+ BtKeyLineEdit(QWidget* parent)
+ : QLineEdit(parent)
+ {
+ }
+protected:
+ void focusInEvent(QFocusEvent* event)
+ {
+ Qt::FocusReason reason = event->reason();
+ if (reason == Qt::OtherFocusReason)
+ {
+ selectAll();
+ }
+ QWidget::focusInEvent(event);
+ }
+};
+
+
+
CKCComboBox::CKCComboBox()
-: QComboBox() {
+: QComboBox()
+{
setFocusPolicy(Qt::WheelFocus);
+ setLineEdit(new BtKeyLineEdit(this));
if (lineEdit()) {
installEventFilter( lineEdit() );
}
}
/** Reimplementation. */
-bool CKCComboBox::eventFilter( QObject *o, QEvent *e ) {
- if (e->type() == QEvent::FocusOut) {
+bool CKCComboBox::eventFilter( QObject *o, QEvent *e )
+{
+ if (e->type() == QEvent::FocusOut)
+ {
QFocusEvent* f = static_cast<QFocusEvent*>(e);
- if (o == lineEdit() && f->reason() == Qt::TabFocusReason) {
+ if (o == lineEdit() && f->reason() == Qt::TabFocusReason)
+ {
int index = findText(currentText());
if (index == -1) {
index = 0;// return 0 if not found
@@ -45,18 +71,22 @@ bool CKCComboBox::eventFilter( QObject *o, QEvent *e ) {
return false;
}
- else if (f->reason() == Qt::PopupFocusReason) {
+ else if (f->reason() == Qt::PopupFocusReason)
+ {
return false;
}
- else if (f->reason() == Qt::ActiveWindowFocusReason) {
+ else if (f->reason() == Qt::ActiveWindowFocusReason)
+ {
emit activated(currentText());
return false;
}
- else if (f->reason() == Qt::MouseFocusReason) {
+ else if (f->reason() == Qt::MouseFocusReason)
+ {
emit activated(currentText());
return false;
}
- else if (o == this) {
+ else if (o == this)
+ {
emit activated(currentText());
return false;
}
@@ -66,13 +96,15 @@ bool CKCComboBox::eventFilter( QObject *o, QEvent *e ) {
}
/** Scrolls in the list if the wheel of the mouse was used. */
-void CKCComboBox::wheelEvent( QWheelEvent* e ) {
+void CKCComboBox::wheelEvent( QWheelEvent* e )
+{
return QComboBox::wheelEvent(e);
const signed int change = (int)((float)e->delta()/(float)120);
int current = currentIndex();
- if ((current+change >= 0) && (current+change<count()) ) {
+ if ((current+change >= 0) && (current+change<count()) )
+ {
setCurrentIndex(current+change);
e->accept();
emit activated( currentIndex() );
@@ -84,23 +116,28 @@ void CKCComboBox::wheelEvent( QWheelEvent* e ) {
//**********************************************************************************/
-CKeyChooserWidget::CKeyChooserWidget(int count, const bool useNextPrevSignals, QWidget *parent ) : QWidget(parent) {
+CKeyChooserWidget::CKeyChooserWidget(int count, const bool useNextPrevSignals, QWidget *parent ) : QWidget(parent)
+{
m_useNextPrevSignals = useNextPrevSignals;
- for (int index=1; index <= count; index++) {
+ for (int index=1; index <= count; index++)
+ {
m_list.append( QString::number(index) );
}
init();
reset(m_list,0,false);
}
-CKeyChooserWidget::CKeyChooserWidget(QStringList *list, const bool useNextPrevSignals, QWidget *parent ) : QWidget(parent) {
+CKeyChooserWidget::CKeyChooserWidget(QStringList *list, const bool useNextPrevSignals, QWidget *parent ) : QWidget(parent)
+{
m_useNextPrevSignals = useNextPrevSignals;
- if (list) {
+ if (list)
+ {
m_list = *list; //deep copy the items of list
}
- else {
+ else
+ {
m_list.clear();
}
@@ -108,21 +145,24 @@ CKeyChooserWidget::CKeyChooserWidget(QStringList *list, const bool useNextPrevSi
reset(m_list,0,false);
}
-void CKeyChooserWidget::reset(const int count, int index, bool do_emit) {
+void CKeyChooserWidget::reset(const int count, int index, bool do_emit)
+{
//This prevents the widget from resetting during application load, which
//produces undesirable behavior.
//if (!updatesEnabled())
// return;
m_list.clear();
- for (int i=1; i <= count; i++) { //TODO: CHECK
+ for (int i=1; i <= count; i++)
+ { //TODO: CHECK
m_list.append( QString::number(i) );
}
reset(&m_list,index,do_emit);
}
-void CKeyChooserWidget::reset(QStringList& list, int index, bool do_emit) {
+void CKeyChooserWidget::reset(QStringList& list, int index, bool do_emit)
+{
//This prevents the widget from resetting during application load, which
//produces undesirable behavior.
//if (!updatesEnabled())
@@ -133,7 +173,8 @@ void CKeyChooserWidget::reset(QStringList& list, int index, bool do_emit) {
}
-void CKeyChooserWidget::reset(QStringList *list, int index, bool do_emit) {
+void CKeyChooserWidget::reset(QStringList *list, int index, bool do_emit)
+{
//if (isResetting || !updatesEnabled())
if (isResetting)
return;
@@ -147,18 +188,21 @@ void CKeyChooserWidget::reset(QStringList *list, int index, bool do_emit) {
//DON'T REMOVE THE HIDE: Otherwise QComboBox's sizeHint() function won't work properly
m_comboBox->hide();
m_comboBox->clear();
- if (list) {
+ if (list)
+ {
m_comboBox->insertItems(-1, *list);
}
if (!list || (list && !list->count())) { //nothing in the combobox
setEnabled(false);
}
- else if (!isEnabled()) { //was disabled
+ else if (!isEnabled())
+ { //was disabled
setEnabled(true);
}
- if (list->count()) {
+ if (list->count())
+ {
m_comboBox->setCurrentIndex(index);
}
if (do_emit) {
@@ -177,13 +221,15 @@ void CKeyChooserWidget::reset(QStringList *list, int index, bool do_emit) {
}
/** Initializes this widget. We need this function because we have more than one constructor. */
-void CKeyChooserWidget::init() {
+void CKeyChooserWidget::init()
+{
qDebug("CKeyChooserWidget::init");
oldKey = QString::null;
setFocusPolicy(Qt::WheelFocus);
m_comboBox = new CKCComboBox();
+ setFocusProxy(m_comboBox);
m_comboBox->setAutoCompletion( true );
m_comboBox->setEditable(true);
m_comboBox->setInsertPolicy(QComboBox::NoInsert);
@@ -200,6 +246,7 @@ void CKeyChooserWidget::init() {
m_mainLayout->addSpacing(0);
setTabOrder(m_comboBox, 0);
+ setFocusProxy(m_comboBox);
connect(m_scroller, SIGNAL(scroller_pressed()), SLOT(lock()));
connect(m_scroller, SIGNAL(scroller_released()), SLOT(unlock()));
@@ -215,13 +262,16 @@ void CKeyChooserWidget::init() {
}
/** Is called when the return key was presed in the combobox. */
-void CKeyChooserWidget::slotReturnPressed( /*const QString& text*/) {
+void CKeyChooserWidget::slotReturnPressed( /*const QString& text*/)
+{
Q_ASSERT(comboBox()->lineEdit());
qDebug("return pressed");
QString text = comboBox()->lineEdit()->text();
- for (int index = 0; index < comboBox()->count(); ++index) {
- if (comboBox()->itemText(index) == text) {
+ for (int index = 0; index < comboBox()->count(); ++index)
+ {
+ if (comboBox()->itemText(index) == text)
+ {
// emit changed(index);
emit focusOut(index); // a workaround because focusOut is not checked, the slot connected to changed to check
break;
@@ -230,16 +280,19 @@ void CKeyChooserWidget::slotReturnPressed( /*const QString& text*/) {
}
/** Is called when the current item of the combo box was changed. */
-void CKeyChooserWidget::slotComboChanged(int index) {
+void CKeyChooserWidget::slotComboChanged(int index)
+{
qDebug("CKeyChooserWidget::slotComboChanged(int index)");
- if (!updatesEnabled()) {
+ if (!updatesEnabled())
+ {
return;
}
setUpdatesEnabled(false);
const QString key = comboBox()->itemText( index );
- if (oldKey.isNull() || (oldKey != key)) {
+ if (oldKey.isNull() || (oldKey != key))
+ {
emit changed(index);
}
@@ -249,17 +302,21 @@ void CKeyChooserWidget::slotComboChanged(int index) {
}
/** Sets the tooltips for the given entries using the parameters as text. */
-void CKeyChooserWidget::setToolTips( const QString comboTip, const QString nextEntryTip, const QString scrollButtonTip, const QString previousEntryTip) {
+void CKeyChooserWidget::setToolTips( const QString comboTip, const QString nextEntryTip, const QString scrollButtonTip, const QString previousEntryTip)
+{
comboBox()->setToolTip(comboTip);
m_scroller->setToolTips(nextEntryTip, scrollButtonTip, previousEntryTip);
}
/** Sets the current item to the one with the given text */
-bool CKeyChooserWidget::setItem( const QString item ) {
+bool CKeyChooserWidget::setItem( const QString item )
+{
bool ret = false;
const int count = comboBox()->count();
- for (int i = 0; i < count; ++i) {
- if (comboBox()->itemText(i) == item) {
+ for (int i = 0; i < count; ++i)
+ {
+ if (comboBox()->itemText(i) == item)
+ {
comboBox()->setCurrentIndex(i);
ret = true;
break;
@@ -271,22 +328,26 @@ bool CKeyChooserWidget::setItem( const QString item ) {
}
/* Handlers for the various scroller widgetset. */
-void CKeyChooserWidget::lock() {
+void CKeyChooserWidget::lock()
+{
updatelock = true;
comboBox()->setEditable(false);
oldKey = comboBox()->currentText();
}
-void CKeyChooserWidget::unlock() {
+void CKeyChooserWidget::unlock()
+{
updatelock = false;
comboBox()->setEditable(true);
comboBox()->setEditText(comboBox()->itemText(comboBox()->currentIndex()));
- if (comboBox()->currentText() != oldKey) {
+ if (comboBox()->currentText() != oldKey)
+ {
emit changed(comboBox()->currentIndex());
}
}
-void CKeyChooserWidget::changeCombo(int n) {
+void CKeyChooserWidget::changeCombo(int n)
+{
const int old_index = comboBox()->currentIndex();
int new_index = old_index + n;
@@ -295,7 +356,8 @@ void CKeyChooserWidget::changeCombo(int n) {
if(new_index > max) new_index = max;
if(new_index < 0) new_index = 0;
- if(new_index != old_index) {
+ if(new_index != old_index)
+ {
comboBox()->setCurrentIndex(new_index);
if(!updatelock)
emit changed(new_index);