| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "reportpane.h" |
| 6 | #include "codeporting.h" |
| 7 | |
| 8 | #include <QTableWidget> |
| 9 | #include <QHeaderView> |
| 10 | #include <QDebug> |
| 11 | #include <QHBoxLayout> |
| 12 | |
| 13 | ReportPane::ReportPane(CodePorting *_codePorting, QWidget *parent) : QWidget(parent) |
| 14 | , srcTableWidget(new QTableWidget(this)) |
| 15 | , libTableWidget(new QTableWidget(this)) |
| 16 | , codePorting(_codePorting) |
| 17 | { |
| 18 | initTableWidget(); |
| 19 | } |
| 20 | |
| 21 | void ReportPane::refreshDispaly() |
| 22 | { |
| 23 | auto &&srcReport = codePorting->getSourceReport(); |
| 24 | auto &&libReport = codePorting->getDependLibReport(); |
| 25 | refreshTableView(srcTableWidget, srcReport); |
| 26 | refreshTableView(libTableWidget, libReport); |
| 27 | } |
| 28 | |
| 29 | void ReportPane::srcCellSelected(int row, int col) |
| 30 | { |
| 31 | qDebug() << "srcCellSelected: " << row << col; |
| 32 | |
| 33 | auto &&report = codePorting->getSourceReport(); |
| 34 | if (report.size()) { |
| 35 | auto items = report[row]; |
| 36 | if (items.size() == CodePorting::kItemsCount) { |
| 37 | QString range = items[CodePorting::kCodeRange]; |
| 38 | QRegularExpression reg("(?<=\\()(\\d)*, (\\d)*(?=\\))" ); |
| 39 | auto match = reg.match(range); |
| 40 | int startLine = 0; |
| 41 | int endLine = 0; |
| 42 | if (match.hasMatch()) { |
| 43 | QStringList lines = match.captured().split("," ); |
| 44 | if (lines.size() == 2) { |
| 45 | startLine = lines.front().toInt(); |
| 46 | endLine = lines.back().toInt(); |
| 47 | } |
| 48 | } |
| 49 | emit selectedChanged(items[CodePorting::kFilePath], items[CodePorting::kSuggestion], startLine, endLine); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void ReportPane::libCellSelected(int row, int col) |
| 55 | { |
| 56 | qDebug() << "libCellSelected: " << row << col; |
| 57 | // TODO(mozart) |
| 58 | } |
| 59 | |
| 60 | void ReportPane::initTableWidget() |
| 61 | { |
| 62 | setTableWidgetStyle(srcTableWidget, codePorting->getSrcItemNames()); |
| 63 | setTableWidgetStyle(libTableWidget, codePorting->getLibItemNames()); |
| 64 | |
| 65 | connect(srcTableWidget, &QTableWidget::cellDoubleClicked, this, &ReportPane::srcCellSelected); |
| 66 | connect(libTableWidget, &QTableWidget::cellDoubleClicked, this, &ReportPane::libCellSelected); |
| 67 | |
| 68 | QTabWidget *tabWidget = new QTabWidget(this); |
| 69 | tabWidget->addTab(srcTableWidget, tr("Source files to migrate" )); |
| 70 | |
| 71 | tabWidget->addTab(libTableWidget, tr("Architecture-dependent library files" )); |
| 72 | tabWidget->setTabPosition(QTabWidget::South); |
| 73 | |
| 74 | auto hLayout = new QHBoxLayout(this); |
| 75 | hLayout->setContentsMargins(0, 0, 0, 9); |
| 76 | this->setLayout(hLayout); |
| 77 | hLayout->addWidget(tabWidget); |
| 78 | } |
| 79 | |
| 80 | void ReportPane::refreshTableView(QTableWidget *widget, const QList<QStringList> &report) |
| 81 | { |
| 82 | if (widget && report.size() > 0) { |
| 83 | widget->clearContents(); |
| 84 | |
| 85 | int itemsCount = report.size(); |
| 86 | widget->setRowCount(itemsCount); |
| 87 | |
| 88 | int row = 0; |
| 89 | int col = 0; |
| 90 | for (auto itItem = report.begin(); itItem != report.end(); ++itItem) { |
| 91 | for (auto data : *itItem) { |
| 92 | widget->setItem(row, col, new QTableWidgetItem(data)); |
| 93 | col++; |
| 94 | } |
| 95 | row++; |
| 96 | col = 0; |
| 97 | } |
| 98 | widget->resizeColumnsToContents(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void ReportPane::setTableWidgetStyle(QTableWidget *tableWidget, const QStringList &colNames) |
| 103 | { |
| 104 | tableWidget->setColumnCount(colNames.count()); |
| 105 | tableWidget->setHorizontalHeaderLabels(colNames); |
| 106 | tableWidget->verticalHeader()->setVisible(true); |
| 107 | tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 108 | tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 109 | tableWidget->setSelectionMode(QAbstractItemView::SingleSelection); |
| 110 | tableWidget->setShowGrid(true); |
| 111 | } |
| 112 | |