summaryrefslogtreecommitdiff
path: root/src/frontend/keychooser/ckeychooserwidget.cpp
blob: a11bd6f86c5b0d721115a1a02beff83c129b0400 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2008 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "frontend/keychooser/ckeychooserwidget.h"

#include <QComboBox>
#include <QDebug>
#include <QFocusEvent>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QString>
#include <QWheelEvent>
#include "frontend/keychooser/cscrollerwidgetset.h"


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() {
    setFocusPolicy(Qt::WheelFocus);
    setLineEdit(new BtKeyLineEdit(this));
    if (lineEdit()) {
        installEventFilter( lineEdit() );
    }
}

/** Reimplementation. */
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) {
            int index = findText(currentText());
            if (index == -1) {
                index = 0;// return 0 if not found
            }
            setCurrentIndex( index );
            emit focusOut( index );

            return false;
        }
        else if (f->reason() == Qt::PopupFocusReason) {
            return false;
        }
        else if (f->reason() == Qt::ActiveWindowFocusReason) {
            emit activated(currentText());
            return false;
        }
        else if (f->reason() == Qt::MouseFocusReason) {
            emit activated(currentText());
            return false;
        }
        else if (o == this) {
            emit activated(currentText());
            return false;
        }
    }

    return QComboBox::eventFilter(o, e);
}

/** Scrolls in the list if the wheel of the mouse was used. */
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()) ) {
        setCurrentIndex(current + change);
        e->accept();
        emit activated( currentIndex() );
    }
    else {
        e->ignore();
    }
}

//**********************************************************************************/

CKeyChooserWidget::CKeyChooserWidget(int count, const bool useNextPrevSignals,  QWidget *parent ) : QWidget(parent) {
    m_useNextPrevSignals = useNextPrevSignals;

    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) {
    m_useNextPrevSignals = useNextPrevSignals;

    if (list) {
        m_list = *list; //deep copy the items of list
    }
    else {
        m_list.clear();
    }

    init();
    reset(m_list, 0, false);
}

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
        m_list.append( QString::number(i) );
    }

    reset(&m_list, index, 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())
    //	return;

    m_list = list;
    reset(&m_list, index, do_emit);
}


void CKeyChooserWidget::reset(QStringList *list, int index, bool do_emit) {
    //if (isResetting || !updatesEnabled())
    if (isResetting)
        return;

    //  qWarning("starting insert");
    isResetting = true;

    oldKey = QString::null;

    //  m_comboBox->setUpdatesEnabled(false);
    //DON'T REMOVE THE HIDE: Otherwise QComboBox's sizeHint() function won't work properly
    m_comboBox->hide();
    m_comboBox->clear();
    if (list) {
        m_comboBox->insertItems(-1, *list);
    }

    if (!list || (list && !list->count())) { //nothing in the combobox
        setEnabled(false);
    }
    else if (!isEnabled()) { //was disabled
        setEnabled(true);
    }

    if (list->count()) {
        m_comboBox->setCurrentIndex(index);
    }
    if (do_emit) {
        emit changed(m_comboBox->currentIndex());
    }

    const QSize dummySize = m_comboBox->sizeHint(); //without this function call the combo box won't be properly sized!
    //DON'T REMOVE OR MOVE THE show()! Otherwise QComboBox's sizeHint() function won't work properly!
    m_comboBox->show();

    //  m_comboBox->setFont( m_comboBox->font() );
    //  m_comboBox->setUpdatesEnabled(true);

    isResetting = false;
    //  qWarning("inserted");
}

/** Initializes this widget. We need this function because we have more than one constructor. */
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);
    m_comboBox->setFocusPolicy(Qt::WheelFocus);

    m_mainLayout = new QHBoxLayout( this );
    m_mainLayout->setSpacing(0);
    m_mainLayout->setContentsMargins(0, 0, 0, 0);
    m_mainLayout->addWidget(m_comboBox);

    m_scroller = new CScrollerWidgetSet(this);

    m_mainLayout->addWidget( m_scroller );
    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()));
    connect(m_scroller, SIGNAL(change(int)), SLOT(changeCombo(int)) );

    connect(m_comboBox, SIGNAL(activated(int)), SLOT(slotComboChanged(int)));
    //  connect(m_comboBox, SIGNAL(activated(const QString&)), SLOT(slotReturnPressed(const QString&)));
    connect(m_comboBox->lineEdit(), SIGNAL(returnPressed()), SLOT(slotReturnPressed()));
    connect(m_comboBox, SIGNAL(focusOut(int)), SIGNAL(focusOut(int)));

    updatelock = false;
    isResetting = false;
}

/** Is called when the return key was presed in the combobox. */
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) {
//			emit changed(index);
            emit focusOut(index); // a workaround because focusOut is not checked, the slot connected to changed to check
            break;
        }
    }
}

/** Is called when the current item of the combo box was changed. */
void CKeyChooserWidget::slotComboChanged(int index) {
    qDebug() << "CKeyChooserWidget::slotComboChanged(int index)";
    if (!updatesEnabled()) {
        return;
    }

    setUpdatesEnabled(false);

    const QString key = comboBox()->itemText( index );
    if (oldKey.isNull() || (oldKey != key)) {
        emit changed(index);
    }

    oldKey = key;

    setUpdatesEnabled(true);
}

/** 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) {
    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 ret = false;
    const int count = comboBox()->count();
    for (int i = 0; i < count; ++i) {
        if (comboBox()->itemText(i) == item) {
            comboBox()->setCurrentIndex(i);
            ret = true;
            break;
        }
    }
    if (!ret)
        comboBox()->setCurrentIndex(-1);
    return ret;
}

/* Handlers for the various scroller widgetset. */
void CKeyChooserWidget::lock() {
    updatelock = true;
    comboBox()->setEditable(false);
    oldKey = comboBox()->currentText();
}

void CKeyChooserWidget::unlock() {
    updatelock = false;
    comboBox()->setEditable(true);
    comboBox()->setEditText(comboBox()->itemText(comboBox()->currentIndex()));
    if (comboBox()->currentText() != oldKey) {
        emit changed(comboBox()->currentIndex());
    }
}

void CKeyChooserWidget::changeCombo(int n) {
    const int old_index = comboBox()->currentIndex();
    int new_index = old_index + n;

    //index of highest Item
    const int max = comboBox()->count() - 1;
    if (new_index > max) new_index = max;
    if (new_index < 0) new_index = 0;

    if (new_index != old_index) {
        comboBox()->setCurrentIndex(new_index);
        if (!updatelock)
            emit changed(new_index);
    }
}