File indexing completed on 2024-05-12 05:52:08

0001 /*
0002     SPDX-FileCopyrightText: 2023 Waqar Ahmed <waqar.17a@gmail.com
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 #include "KateTextHintManager.h"
0006 
0007 #include "tooltip.h"
0008 
0009 #include <KTextEditor/MainWindow>
0010 #include <KTextEditor/TextHintInterface>
0011 #include <KTextEditor/View>
0012 
0013 KateTextHintProvider::KateTextHintProvider(KTextEditor::MainWindow *mainWindow, QObject *parent)
0014     : QObject(parent)
0015 {
0016     Q_ASSERT(mainWindow);
0017     auto mgr = static_cast<KateTextHintManager *>(mainWindow->property("textHintManager").value<QObject *>());
0018     if (!mgr) {
0019         mgr = new KateTextHintManager(mainWindow);
0020         mainWindow->setProperty("textHintManager", QVariant::fromValue(mgr));
0021     }
0022     mgr->registerProvider(this);
0023 }
0024 
0025 class KTETextHintProvider : public KTextEditor::TextHintProvider
0026 {
0027     KateTextHintManager *m_mgr;
0028     QPointer<KTextEditor::View> m_view;
0029 
0030 public:
0031     KTETextHintProvider(KateTextHintManager *mgr)
0032         : m_mgr(mgr)
0033     {
0034         Q_ASSERT(m_mgr);
0035     }
0036 
0037     ~KTETextHintProvider()
0038     {
0039         if (m_view) {
0040             m_view->unregisterTextHintProvider(this);
0041         }
0042     }
0043 
0044     void setView(KTextEditor::View *v)
0045     {
0046         if (m_view) {
0047             m_view->unregisterTextHintProvider(this);
0048         }
0049         if (v) {
0050             m_view = v;
0051             m_view->registerTextHintProvider(this);
0052         }
0053     }
0054 
0055     KTextEditor::View *view() const
0056     {
0057         return m_view;
0058     }
0059 
0060     virtual QString textHint(KTextEditor::View *view, const KTextEditor::Cursor &position) override
0061     {
0062         m_mgr->ontextHintRequested(view, position);
0063         return {};
0064     }
0065 };
0066 
0067 KateTextHintManager::KateTextHintManager(KTextEditor::MainWindow *mainWindow)
0068     : QObject(mainWindow)
0069     , m_provider(new KTETextHintProvider(this))
0070 {
0071     connect(mainWindow, &KTextEditor::MainWindow::viewChanged, this, [this](KTextEditor::View *v) {
0072         m_provider->setView(v);
0073     });
0074 }
0075 
0076 KateTextHintManager::~KateTextHintManager()
0077 {
0078     m_provider->setView(nullptr);
0079     delete m_provider;
0080 }
0081 
0082 void KateTextHintManager::registerProvider(KateTextHintProvider *provider)
0083 {
0084     if (std::find(m_providers.begin(), m_providers.end(), provider) == m_providers.end()) {
0085         m_providers.push_back(provider);
0086         connect(provider, &QObject::destroyed, this, [this](QObject *provider) {
0087             auto it = std::find(m_providers.begin(), m_providers.end(), provider);
0088             if (it != m_providers.end()) {
0089                 m_providers.erase(it);
0090             }
0091         });
0092         connect(provider, &KateTextHintProvider::textHintAvailable, this, &KateTextHintManager::onTextHintAvailable);
0093         connect(provider, &KateTextHintProvider::showTextHint, this, &KateTextHintManager::onShowTextHint);
0094     }
0095 }
0096 
0097 void KateTextHintManager::ontextHintRequested(KTextEditor::View *v, KTextEditor::Cursor c)
0098 {
0099     for (auto provider : m_providers) {
0100         Q_EMIT provider->textHintRequested(v, c);
0101     }
0102 }
0103 
0104 void KateTextHintManager::showTextHint(const QString &hint, TextHintMarkupKind kind, KTextEditor::Cursor pos, bool force)
0105 {
0106     if (QStringView(hint).trimmed().isEmpty() || !pos.isValid()) {
0107         return;
0108     }
0109     auto view = m_provider->view();
0110     if (!view) {
0111         return;
0112     }
0113 
0114     QPoint p = view->cursorToCoordinate(pos);
0115     KateTooltip::show(hint, kind, view->mapToGlobal(p), view, force);
0116 }
0117 
0118 void KateTextHintManager::onTextHintAvailable(const QString &hint, TextHintMarkupKind kind, KTextEditor::Cursor pos)
0119 {
0120     showTextHint(hint, kind, pos, false);
0121 }
0122 
0123 void KateTextHintManager::onShowTextHint(const QString &hint, TextHintMarkupKind kind, KTextEditor::Cursor pos)
0124 {
0125     showTextHint(hint, kind, pos, true);
0126 }
0127 
0128 #include "moc_KateTextHintManager.cpp"