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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2008-03-14
 * Description : User interface for searches
 *
 * SPDX-FileCopyrightText: 2008-2012 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "comboboxutilities.h"

// Qt includes

#include <QAbstractItemView>
#include <QAbstractListModel>
#include <QMouseEvent>
#include <QPointer>
#include <QPainter>
#include <QPen>
#include <QStyle>
#include <QStyleOption>
#include <QTreeView>
#include <QVBoxLayout>

// Local includes

#include "digikam_debug.h"

namespace Digikam
{

ProxyLineEdit::ProxyLineEdit(QWidget* const parent)
    : QLineEdit(parent)
{
    m_layout = new QVBoxLayout;
    m_layout->setSpacing(0);
    m_layout->setContentsMargins(QMargins());
    setLayout(m_layout);

    // unset text edit cursor

    unsetCursor();

    // unset clear button per default

    setClearButtonShown(false);

    connect(this, SIGNAL(textChanged(QString)),
            this, SLOT(slotTextChanged(QString)));
}

void ProxyLineEdit::setWidget(QWidget* widget)
{
    if (m_widget)
    {
        delete m_widget;
    }

    m_widget = widget;
    m_widget->setParent(this);
    m_layout->addWidget(m_widget);
}

void ProxyLineEdit::setClearButtonShown(bool show)
{
    setClearButtonEnabled(show);

    int rightMargin = show ? height() : 0;
    m_layout->setContentsMargins(0, 0, rightMargin, 0);
}

void ProxyLineEdit::slotTextChanged(const QString& text)
{
    if (text.isEmpty() && isClearButtonEnabled())
    {
        Q_EMIT signalClearButtonPressed();
    }
}

/**
 * NOTE: see bug #326718: We need to use QLineEdit parent class with these methods
 * to have clear button working fine.
 */
void ProxyLineEdit::mousePressEvent(QMouseEvent* event)
{
    QLineEdit::mousePressEvent(event);
}

void ProxyLineEdit::mouseReleaseEvent(QMouseEvent* event)
{
    QLineEdit::mouseReleaseEvent(event);
}

/**
 * We just re-implement all relevant QWidget event handlers and call
 * the QWidget implementation, not the QLineEdit one.
 */
void ProxyLineEdit::mouseMoveEvent(QMouseEvent* event)
{
    QWidget::mouseMoveEvent(event);
}

void ProxyLineEdit::mouseDoubleClickEvent(QMouseEvent* event)
{
    QWidget::mouseDoubleClickEvent(event);
}

void ProxyLineEdit::keyPressEvent(QKeyEvent* event)
{
    QWidget::keyPressEvent(event);
}

void ProxyLineEdit::focusInEvent(QFocusEvent* event)
{
    QWidget::focusInEvent(event);
}

void ProxyLineEdit::focusOutEvent(QFocusEvent* event)
{
    QWidget::focusOutEvent(event);
}

void ProxyLineEdit::paintEvent(QPaintEvent* event)
{
    QWidget::paintEvent(event);
}

void ProxyLineEdit::dragEnterEvent(QDragEnterEvent* event)
{
    QWidget::dragEnterEvent(event);
}

void ProxyLineEdit::dragMoveEvent(QDragMoveEvent* event)
{
    QWidget::dragMoveEvent(event);
}

void ProxyLineEdit::dragLeaveEvent(QDragLeaveEvent* event)
{
    QWidget::dragLeaveEvent(event);
}

void ProxyLineEdit::dropEvent(QDropEvent* event)
{
    QWidget::dropEvent(event);
}

void ProxyLineEdit::changeEvent(QEvent* event)
{
    QWidget::changeEvent(event);
}

void ProxyLineEdit::contextMenuEvent(QContextMenuEvent* event)
{
    QWidget::contextMenuEvent(event);
}

void ProxyLineEdit::inputMethodEvent(QInputMethodEvent* event)
{
    QWidget::inputMethodEvent(event);
}

QSize ProxyLineEdit::minimumSizeHint() const
{
    return QWidget::minimumSizeHint();
}

QSize ProxyLineEdit::sizeHint() const
{
    return QWidget::sizeHint();
}

// -------------------------------------------------------------------------

ProxyClickLineEdit::ProxyClickLineEdit(QWidget* const parent)
    : ProxyLineEdit(parent)
{
}

void ProxyClickLineEdit::mouseReleaseEvent(QMouseEvent* event)
{
    ProxyLineEdit::mouseReleaseEvent(event);

    if (event->button() == Qt::LeftButton)
    {
        Q_EMIT leftClicked();

        event->accept();
    }
}

// -------------------------------------------------------------------------

ModelIndexBasedComboBox::ModelIndexBasedComboBox(QWidget* const parent)
    : QComboBox(parent)
{
}

void ModelIndexBasedComboBox::hidePopup()
{
    m_currentIndex = view()->selectionModel()->currentIndex();
    QComboBox::hidePopup();
}

void ModelIndexBasedComboBox::showPopup()
{
    QComboBox::showPopup();

    if (m_currentIndex.isValid())
    {
        view()->selectionModel()->setCurrentIndex(m_currentIndex, QItemSelectionModel::ClearAndSelect);
    }
}

QModelIndex ModelIndexBasedComboBox::currentIndex() const
{
    return m_currentIndex;
}

void ModelIndexBasedComboBox::setCurrentIndex(const QModelIndex& index)
{
    m_currentIndex = index;
    view()->selectionModel()->setCurrentIndex(m_currentIndex, QItemSelectionModel::ClearAndSelect);
}

// -------------------------------------------------------------------------

StayPoppedUpComboBox::StayPoppedUpComboBox(QWidget* const parent)
    : ModelIndexBasedComboBox(parent)
{
}

void StayPoppedUpComboBox::installView(QAbstractItemView* view)
{
    if (m_view)
    {
        return;
    }

    // Create view

    m_view = view;

    // set on combo box

    setView(m_view);

    // Removing these event filters works just as the eventFilter() solution below,
    // but is much more dependent on Qt internals and not guaranteed to work in the future.
/*
    m_view->removeEventFilter(m_view->parent());
    m_view->viewport()->removeEventFilter(m_view->parent());
*/
    // Install event filters, _after_ setView() is called

    m_view->installEventFilter(this);
    m_view->viewport()->installEventFilter(this);
}

bool StayPoppedUpComboBox::eventFilter(QObject* o, QEvent* e)
{
    // The combo box has installed an event filter on the view.
    // If it catches a valid mouse button release there, it will hide the popup.
    // Here we prevent this by eating the event ourselves,
    // and then dispatching it to its destination.

    if ((o == m_view) || (o == m_view->viewport()))
    {
        switch (e->type())
        {

#if (QT_VERSION >= QT_VERSION_CHECK(6, 8, 0))

            case QEvent::MouseButtonPress:
            {
                QMouseEvent* m = static_cast<QMouseEvent*>(e);

                if (m_view->isVisible() && m_view->rect().contains(m->position().toPoint()))
                {
                    if ((o == m_view->viewport()) && (m->button() == Qt::RightButton))
                    {
                        QContextMenuEvent contextMenu(QContextMenuEvent::Mouse,
                                                      m->position().toPoint(),
                                                      m->globalPosition().toPoint());

                        sendViewportEventToView(&contextMenu);

                        return true;
                    }
                }

                break;
            }

#endif

            case QEvent::MouseButtonRelease:
            {
                QMouseEvent* m = static_cast<QMouseEvent*>(e);

                if (m_view->isVisible() && m_view->rect().contains(m->pos()))
                {
                    if (o == m_view)
                    {
                        o->event(e);
                    }
                    else
                    {
                        // Viewport: Calling event() does not work, viewportEvent() is needed.
                        // This is the event that gets redirected to the QTreeView finally!

                        sendViewportEventToView(e);
                    }

                    // we have dispatched the event privately; we filter it out from the main dispatching

                    return true;
                }

                break;
            }

            case QEvent::ContextMenu:
            {
                if (o != m_view)
                {
                    // for whatever reason, the position of the event is slightly wrong

                    QContextMenuEvent* m = static_cast<QContextMenuEvent*>(e);
                    QPoint correctPos    = m_view->viewport()->mapFromGlobal(m->globalPos());
                    QContextMenuEvent corrected(m->reason(), correctPos, m->globalPos(), m->modifiers());
                    sendViewportEventToView(&corrected);

                    return true;
                }

                break;
            }

            default:
            {
                break;
            }
        }
    }

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

// -------------------------------------------------------------------------

class Q_DECL_HIDDEN TreeViewComboBoxTreeView : public QTreeView
{
    Q_OBJECT

public:

    // Needed to make viewportEvent() public

    explicit TreeViewComboBoxTreeView(QWidget* const parent = nullptr)
        : QTreeView(parent)
    {
    }

    bool viewportEvent(QEvent* event) override
    {
        return QTreeView::viewportEvent(event);
    }
};

TreeViewComboBox::TreeViewComboBox(QWidget* const parent)
    : StayPoppedUpComboBox(parent)
{
}

void TreeViewComboBox::installView(QAbstractItemView* view)<--- Shadow argument
{
    // parent does the heavy work

    if (view)
    {
        StayPoppedUpComboBox::installView(view);
    }
    else
    {
        QPointer<TreeViewComboBoxTreeView> tview = new TreeViewComboBoxTreeView;
        StayPoppedUpComboBox::installView(tview);
    }
}

void TreeViewComboBox::sendViewportEventToView(QEvent* e)
{
    static_cast<TreeViewComboBoxTreeView*>(m_view)->viewportEvent(e);
}

QTreeView* TreeViewComboBox::view() const
{
    return static_cast<QTreeView*>(m_view);
}

// -------------------------------------------------------------------------

class Q_DECL_HIDDEN ListViewComboBoxListView : public QListView
{
    Q_OBJECT

public:

    // Needed to make viewportEvent() public

    explicit ListViewComboBoxListView(QWidget* const parent = nullptr)
        : QListView(parent)
    {
    }

    bool viewportEvent(QEvent* event) override
    {
        return QListView::viewportEvent(event);
    }

private:

    // Disable
    explicit ListViewComboBoxListView(QObject*);
};

ListViewComboBox::ListViewComboBox(QWidget* const parent)
    : StayPoppedUpComboBox(parent)
{
}

void ListViewComboBox::installView(QAbstractItemView* view)<--- Shadow argument
{
    // parent does the heavy work

    if (view)
    {
        StayPoppedUpComboBox::installView(view);
    }
    else
    {
        QPointer<ListViewComboBoxListView> lview = new ListViewComboBoxListView;
        StayPoppedUpComboBox::installView(lview);
    }
}

void ListViewComboBox::sendViewportEventToView(QEvent* e)
{
    static_cast<ListViewComboBoxListView*>(m_view)->viewportEvent(e);
}

QListView* ListViewComboBox::view() const
{
    return static_cast<QListView*>(m_view);
}

// -------------------------------------------------------------------------

class Q_DECL_HIDDEN TreeViewComboBoxLineEdit : public QLineEdit
{
    Q_OBJECT

public:

    // This line edit works like a weblink:
    // Readonly; A mouse press shows the popup; Cursor is the pointing hand.

    explicit TreeViewComboBoxLineEdit(QComboBox* const box)
        : QLineEdit(box),
          m_box    (box)
    {
        setReadOnly(true);
        setCursor(Qt::PointingHandCursor);
    }

    void mouseReleaseEvent(QMouseEvent* event) override
    {
        QLineEdit::mouseReleaseEvent(event);
        m_box->showPopup();
    }

    void wheelEvent(QWheelEvent* /*event*/) override
    {
        m_box->showPopup();
    }

public:

    QComboBox* m_box = nullptr;
};

// -------------------------------------------------------------------------

TreeViewLineEditComboBox::TreeViewLineEditComboBox(QWidget* const parent)
    : TreeViewComboBox(parent)
{
}

void TreeViewLineEditComboBox::setLineEditText(const QString& text)
{
    if (m_comboLineEdit)
    {
        m_comboLineEdit->setText(text);
    }
}

void TreeViewLineEditComboBox::installView(QAbstractItemView* view)
{
    // parent does the heavy work

    TreeViewComboBox::installView(view);

    installLineEdit();
}

void TreeViewLineEditComboBox::installLineEdit()
{
    if (!m_comboLineEdit)
    {
        setLineEdit(new TreeViewComboBoxLineEdit(this));
    }
}

void TreeViewLineEditComboBox::setLineEdit(QLineEdit* edit)
{
    m_comboLineEdit = edit;
    TreeViewComboBox::setLineEdit(edit);
}

} // namespace Digikam

#include "comboboxutilities.moc"

#include "moc_comboboxutilities.cpp"