| Michael Weghorn | e61a6f9 | 2025-04-18 23:00:09 +0200 | [diff] [blame] | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ |
| 2 | /* |
| Michael Meeks | d037b81 | 2026-03-26 15:06:16 +0000 | [diff] [blame] | 3 | * This file is part of the Collabora Office project. |
| Michael Weghorn | e61a6f9 | 2025-04-18 23:00:09 +0200 | [diff] [blame] | 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | */ |
| 9 | |
| 10 | #include <QtInstanceEntryTreeView.hxx> |
| 11 | #include <QtInstanceEntryTreeView.moc> |
| 12 | |
| Michael Weghorn | e61a6f9 | 2025-04-18 23:00:09 +0200 | [diff] [blame] | 13 | #include <QtWidgets/QCompleter> |
| 14 | |
| 15 | QtInstanceEntryTreeView::QtInstanceEntryTreeView(QWidget* pContainer, QLineEdit* pLineEdit, |
| 16 | QTreeView* pTreeView, |
| 17 | std::unique_ptr<weld::Entry> xEntry, |
| 18 | std::unique_ptr<weld::TreeView> xTreeView) |
| 19 | : weld::EntryTreeView(std::move(xEntry), std::move(xTreeView)) |
| 20 | , QtInstanceContainer(pContainer) |
| 21 | , m_pLineEdit(pLineEdit) |
| 22 | , m_pTreeView(pTreeView) |
| 23 | { |
| 24 | set_entry_completion(true, true); |
| 25 | |
| 26 | connect(m_pLineEdit, &QLineEdit::textChanged, this, &QtInstanceEntryTreeView::editTextChanged); |
| 27 | m_pLineEdit->installEventFilter(this); |
| 28 | } |
| 29 | |
| 30 | void QtInstanceEntryTreeView::grab_focus() { m_xEntry->grab_focus(); } |
| 31 | |
| 32 | void QtInstanceEntryTreeView::connect_focus_in(const Link<Widget&, void>& rLink) |
| 33 | { |
| 34 | m_xEntry->connect_focus_in(rLink); |
| 35 | } |
| 36 | |
| 37 | void QtInstanceEntryTreeView::connect_focus_out(const Link<Widget&, void>& rLink) |
| 38 | { |
| 39 | m_xEntry->connect_focus_out(rLink); |
| 40 | } |
| 41 | |
| 42 | void QtInstanceEntryTreeView::make_sorted() { m_xTreeView->make_sorted(); } |
| 43 | |
| 44 | bool QtInstanceEntryTreeView::changed_by_direct_pick() const |
| 45 | { |
| 46 | assert(false && "Not implemented yet"); |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | void QtInstanceEntryTreeView::set_entry_completion(bool bEnable, bool bCaseSensitive) |
| 51 | { |
| 52 | SolarMutexGuard g; |
| 53 | GetQtInstance().RunInMainThread([&] { |
| 54 | QCompleter* pCompleter = nullptr; |
| 55 | if (bEnable) |
| 56 | { |
| 57 | pCompleter = new QCompleter(m_pTreeView->model(), m_pTreeView); |
| 58 | pCompleter->setCompletionMode(QCompleter::InlineCompletion); |
| 59 | pCompleter->setFilterMode(Qt::MatchStartsWith); |
| 60 | Qt::CaseSensitivity eCaseSensitivity |
| 61 | = bCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive; |
| 62 | pCompleter->setCaseSensitivity(eCaseSensitivity); |
| 63 | } |
| 64 | |
| 65 | m_pLineEdit->setCompleter(pCompleter); |
| 66 | }); |
| 67 | } |
| 68 | |
| Michael Weghorn | e61a6f9 | 2025-04-18 23:00:09 +0200 | [diff] [blame] | 69 | bool QtInstanceEntryTreeView::eventFilter(QObject* pObject, QEvent* pEvent) |
| 70 | { |
| 71 | if (pObject != m_pLineEdit) |
| 72 | return false; |
| 73 | |
| 74 | switch (pEvent->type()) |
| 75 | { |
| 76 | case QEvent::KeyPress: |
| 77 | { |
| 78 | QKeyEvent* pKeyEvent = static_cast<QKeyEvent*>(pEvent); |
| 79 | |
| 80 | // don't intercept key event handling if modifiers (other than key pad) are used |
| 81 | if (pKeyEvent->modifiers() & ~Qt::KeypadModifier) |
| 82 | return false; |
| 83 | |
| 84 | switch (pKeyEvent->key()) |
| 85 | { |
| 86 | case Qt::Key_Down: |
| 87 | case Qt::Key_PageDown: |
| 88 | case Qt::Key_PageUp: |
| 89 | case Qt::Key_Up: |
| 90 | // forward key events for navigation through entries to the tree view |
| 91 | return QCoreApplication::sendEvent(m_pTreeView, pEvent); |
| 92 | default: |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | default: |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void QtInstanceEntryTreeView::editTextChanged(const QString& rText) |
| 102 | { |
| 103 | const int nIndex = m_xTreeView->find_text(toOUString(rText)); |
| 104 | if (nIndex < 0) |
| 105 | return; |
| 106 | |
| 107 | const QModelIndex aModelIndex = m_pTreeView->model()->index(nIndex, 0); |
| 108 | m_pTreeView->selectionModel()->setCurrentIndex(aModelIndex, |
| 109 | QItemSelectionModel::ClearAndSelect); |
| 110 | } |
| 111 | |
| 112 | /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |