File indexing completed on 2024-03-24 04:00:07
0001 /* 0002 SPDX-FileCopyrightText: 2022 Waqar Ahmed <waqar.17a@gmail.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 #include "documentation_tip.h" 0007 0008 #include <QDebug> 0009 #include <QHBoxLayout> 0010 #include <QScreen> 0011 0012 DocTip::DocTip(QWidget *parent) 0013 : QFrame(parent) 0014 , m_textView(new QTextBrowser(this)) 0015 { 0016 setFocusPolicy(Qt::NoFocus); 0017 0018 m_textView->setFrameStyle(QFrame::Box | QFrame::Raised); 0019 0020 setFixedWidth(250); 0021 setFixedHeight(150); 0022 0023 auto layout = new QHBoxLayout(this); 0024 layout->setContentsMargins({}); 0025 layout->setSpacing(0); 0026 setContentsMargins({}); 0027 layout->addWidget(&m_stack); 0028 m_stack.addWidget(m_textView); 0029 } 0030 0031 QWidget *DocTip::currentWidget() 0032 { 0033 return m_stack.currentWidget(); 0034 } 0035 0036 void DocTip::clearWidgets() 0037 { 0038 for (auto *widget : m_widgets) { 0039 widget->deleteLater(); 0040 } 0041 m_widgets.clear(); 0042 } 0043 0044 void DocTip::setText(const QString &s) 0045 { 0046 m_textView->setPlainText(s); 0047 if (m_stack.currentWidget() != m_textView) { 0048 m_stack.removeWidget(m_stack.currentWidget()); 0049 m_stack.addWidget(m_textView); 0050 } 0051 Q_ASSERT(m_stack.count() == 1); 0052 } 0053 0054 void DocTip::setWidget(QWidget *widget) 0055 { 0056 if (auto w = m_stack.currentWidget()) { 0057 if (w != m_textView) { 0058 m_widgets.push_back(w); 0059 } 0060 m_stack.removeWidget(w); 0061 } 0062 0063 if (!widget) { 0064 return; 0065 } 0066 0067 m_stack.addWidget(widget); 0068 0069 Q_ASSERT(m_stack.count() == 1); 0070 } 0071 0072 void DocTip::updatePosition(QWidget *completionWidget) 0073 { 0074 auto parent = parentWidget(); 0075 if (!parent) { 0076 qWarning() << Q_FUNC_INFO << "Unexpected null parent!"; 0077 return; 0078 } 0079 0080 const auto parentRight = parent->geometry().right(); 0081 auto completionWidgetRight = completionWidget->geometry().right(); 0082 constexpr int Margin = 8; 0083 0084 int x = 0; 0085 // is the completion widget too far right? 0086 if ((completionWidgetRight + this->width()) > parentRight) { 0087 // let's hope there is enough available space to the left of completionWidget 0088 x = (completionWidget->x() - this->width()) - Margin; 0089 } else { 0090 // we have enough space on the right 0091 x = completionWidget->x() + completionWidget->width() + Margin; 0092 } 0093 move(x, completionWidget->y()); 0094 }