aboutsummaryrefslogtreecommitdiff
path: root/src/yuzu/deps_dialog.cpp
blob: 4cef05e05404916ee1b46352b3ba663c540ac8ca (plain) (blame)
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
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later

#include "yuzu/deps_dialog.h"
#include <QAbstractTextDocumentLayout>
#include <QDesktopServices>
#include <QIcon>
#include <QPainter>
#include <QTableWidget>
#include <QTextEdit>
#include "dep_hashes.h"
#include "ui_deps_dialog.h"
#include <fmt/ranges.h>

DepsDialog::DepsDialog(QWidget* parent)
    : QDialog(parent)
    , ui{std::make_unique<Ui::DepsDialog>()}
{
    ui->setupUi(this);

    constexpr size_t rows = Common::dep_hashes.size();
    ui->tableDeps->setRowCount(rows);

    QStringList labels;
    labels << tr("Dependency") << tr("Version");

    ui->tableDeps->setHorizontalHeaderLabels(labels);
    ui->tableDeps->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeMode::Stretch);
    ui->tableDeps->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeMode::Fixed);
    ui->tableDeps->horizontalHeader()->setMinimumSectionSize(200);

    for (size_t i = 0; i < rows; ++i) {
        const std::string name = Common::dep_names.at(i);
        const std::string sha = Common::dep_hashes.at(i);
        const std::string url = Common::dep_urls.at(i);

        std::string dependency = fmt::format("<a href=\"{}\">{}</a>", url, name);

        QTableWidgetItem *nameItem = new QTableWidgetItem(QString::fromStdString(dependency));
        QTableWidgetItem *shaItem = new QTableWidgetItem(QString::fromStdString(sha));

        ui->tableDeps->setItem(i, 0, nameItem);
        ui->tableDeps->setItem(i, 1, shaItem);
    }

    ui->tableDeps->setItemDelegateForColumn(0, new LinkItemDelegate(this));
}

DepsDialog::~DepsDialog() = default;

LinkItemDelegate::LinkItemDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{}

void LinkItemDelegate::paint(QPainter *painter,
                             const QStyleOptionViewItem &option,
                             const QModelIndex &index) const
{
    auto options = option;
    initStyleOption(&options, index);

    QTextDocument doc;
    QString html = index.data(Qt::DisplayRole).toString();
    doc.setHtml(html);

    options.text.clear();

    painter->save();
    painter->translate(options.rect.topLeft());
    doc.drawContents(painter, QRectF(0, 0, options.rect.width(), options.rect.height()));
    painter->restore();
}

QSize LinkItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem options = option;
    initStyleOption(&options, index);

    QTextDocument doc;
    doc.setHtml(options.text);
    doc.setTextWidth(options.rect.width());
    return QSize(doc.idealWidth(), doc.size().height());
}

bool LinkItemDelegate::editorEvent(QEvent *event,
                                   QAbstractItemModel *model,
                                   const QStyleOptionViewItem &option,
                                   const QModelIndex &index)
{
    if (event->type() == QEvent::MouseButtonRelease) {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        if (mouseEvent->button() == Qt::LeftButton) {
            QString html = index.data(Qt::DisplayRole).toString();
            QTextDocument doc;
            doc.setHtml(html);
            doc.setTextWidth(option.rect.width());

            // this is kinda silly but it werks
            QAbstractTextDocumentLayout *layout = doc.documentLayout();

            QPoint pos = mouseEvent->pos() - option.rect.topLeft();
            int charPos = layout->hitTest(pos, Qt::ExactHit);

            if (charPos >= 0) {
                QTextCursor cursor(&doc);
                cursor.setPosition(charPos);

                QTextCharFormat format = cursor.charFormat();

                if (format.isAnchor()) {
                    QString href = format.anchorHref();
                    if (!href.isEmpty()) {
                        QDesktopServices::openUrl(QUrl(href));
                        return true;
                    }
                }
            }
        }
    }
    return QStyledItemDelegate::editorEvent(event, model, option, index);
}