File indexing completed on 2024-04-28 05:49:12

0001 /*
0002     SPDX-FileCopyrightText: 2011-21 Kåre Särs <kare.sars@iki.fi>
0003     SPDX-FileCopyrightText: 2022 Waqar Ahmed <waqar.17a@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 #include "ResultsTreeView.h"
0008 
0009 #include "Results.h"
0010 
0011 #include <QPushButton>
0012 
0013 #include <KSyntaxHighlighting/Theme>
0014 #include <KTextEditor/Editor>
0015 
0016 ResultsTreeView::ResultsTreeView(QWidget *parent)
0017     : QTreeView(parent)
0018     , m_detachButton(new QPushButton(this))
0019 {
0020     auto updateColors = [this](KTextEditor::Editor *e) {
0021         if (!e) {
0022             return;
0023         }
0024 
0025         const auto theme = e->theme();
0026         auto base = QColor::fromRgba(theme.editorColor(KSyntaxHighlighting::Theme::BackgroundColor));
0027         auto highlight = QColor::fromRgba(theme.editorColor(KSyntaxHighlighting::Theme::TextSelection));
0028         m_fg = QColor::fromRgba(theme.textColor(KSyntaxHighlighting::Theme::Normal));
0029 
0030         auto pal = palette();
0031         pal.setColor(QPalette::Base, base);
0032         pal.setColor(QPalette::Text, m_fg);
0033         pal.setColor(QPalette::Highlight, highlight);
0034         setPalette(pal);
0035     };
0036 
0037     connect(this, &ResultsTreeView::geometryChanged, this, [this] {
0038         auto topRight = viewport()->geometry().topRight();
0039         topRight.rx() -= 4;
0040         topRight.ry() += 4;
0041         auto btnGeometry = m_detachButton->geometry();
0042         btnGeometry.moveTopRight(topRight);
0043         m_detachButton->setGeometry(btnGeometry);
0044     });
0045 
0046     m_detachButton->setIcon(QIcon::fromTheme(QStringLiteral("draw-arrow")));
0047     m_detachButton->resize(m_detachButton->minimumSizeHint());
0048     connect(m_detachButton, &QAbstractButton::clicked, this, [this] {
0049         m_detachButton->setEnabled(false);
0050         m_detachButton->setVisible(false);
0051         Q_EMIT detachClicked();
0052     });
0053     m_detachButton->setVisible(false);
0054 
0055     auto *e = KTextEditor::Editor::instance();
0056     connect(e, &KTextEditor::Editor::configChanged, this, updateColors);
0057     updateColors(e);
0058 }
0059 
0060 void ResultsTreeView::initViewItemOption(QStyleOptionViewItem *option) const
0061 {
0062     QTreeView::initViewItemOption(option);
0063     // We set this here so that the "expand triangle" in the treeview
0064     // is always colored in a visible color. This is important for
0065     // styles like fusion, where it can be dark on dark
0066     option->palette.setColor(QPalette::WindowText, m_fg);
0067 }
0068 
0069 void ResultsTreeView::resizeEvent(QResizeEvent *e)
0070 {
0071     Q_EMIT geometryChanged();
0072     QTreeView::resizeEvent(e);
0073 }
0074 
0075 void ResultsTreeView::enterEvent(QEnterEvent *event)
0076 {
0077     auto *res = qobject_cast<Results *>(parent());
0078     if (!res) {
0079         qWarning() << Q_FUNC_INFO << "Unexpected null parent() Results";
0080         QTreeView::enterEvent(event);
0081         return;
0082     }
0083     m_detachButton->setVisible(!res->isEmpty() && !res->isDetachedToMainWindow);
0084     QTreeView::enterEvent(event);
0085 }
0086 
0087 void ResultsTreeView::leaveEvent(QEvent *e)
0088 {
0089     m_detachButton->hide();
0090     QTreeView::leaveEvent(e);
0091 }
0092 
0093 #include "moc_ResultsTreeView.cpp"