| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "loadcoredialog.h" |
| 6 | #include "event_man.h" |
| 7 | |
| 8 | #include <QComboBox> |
| 9 | #include <QDialogButtonBox> |
| 10 | #include <QLineEdit> |
| 11 | #include <QPushButton> |
| 12 | #include <QFormLayout> |
| 13 | #include <QDir> |
| 14 | #include <QFileDialog> |
| 15 | |
| 16 | namespace ReverseDebugger { |
| 17 | namespace Internal { |
| 18 | |
| 19 | class StartCoredumpDialogPrivate |
| 20 | { |
| 21 | public: |
| 22 | QLineEdit *traceDir = nullptr; |
| 23 | QComboBox *pidInput = nullptr; |
| 24 | QComboBox *historyComboBox = nullptr; |
| 25 | QDialogButtonBox *buttonBox = nullptr; |
| 26 | }; |
| 27 | |
| 28 | LoadCoreDialog::LoadCoreDialog(QWidget *parent) |
| 29 | : QDialog(parent), |
| 30 | d(new StartCoredumpDialogPrivate) |
| 31 | { |
| 32 | setupUi(); |
| 33 | } |
| 34 | |
| 35 | LoadCoreDialog::~LoadCoreDialog() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | CoredumpRunParameters LoadCoreDialog::displayDlg(const QString &traceDir) |
| 40 | { |
| 41 | d->traceDir->setText(traceDir); |
| 42 | |
| 43 | CoredumpRunParameters ret; |
| 44 | auto code = exec(); |
| 45 | if (code == QDialog::Accepted) { |
| 46 | ret.pid = d->pidInput->currentText().toInt(); |
| 47 | ret.tracedir = d->traceDir->text(); |
| 48 | } |
| 49 | |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | void LoadCoreDialog::setupUi() |
| 54 | { |
| 55 | setWindowTitle(tr("Event Debugger Configure" )); |
| 56 | |
| 57 | // trace directory. |
| 58 | d->traceDir = new QLineEdit(this); |
| 59 | d->traceDir->setPlaceholderText(tr("Trace directory." )); |
| 60 | |
| 61 | QPushButton *btnBrowser = new QPushButton(this); |
| 62 | btnBrowser->setText(tr("Browse..." )); |
| 63 | |
| 64 | // pid |
| 65 | d->pidInput = new QComboBox(this); |
| 66 | |
| 67 | // history |
| 68 | d->historyComboBox = new QComboBox(this); |
| 69 | |
| 70 | // ok & cancel button. |
| 71 | d->buttonBox = new QDialogButtonBox(this); |
| 72 | d->buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok); |
| 73 | d->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("OK" )); |
| 74 | d->buttonBox->button(QDialogButtonBox::Cancel)->setText(tr("Cancel" )); |
| 75 | d->buttonBox->button(QDialogButtonBox::Ok)->setDefault(true); |
| 76 | |
| 77 | auto hLayout = new QHBoxLayout(); |
| 78 | hLayout->addWidget(d->traceDir); |
| 79 | hLayout->addWidget(btnBrowser); |
| 80 | |
| 81 | auto splitLine = [this]() -> QFrame * { |
| 82 | auto splitLine = new QFrame(this); |
| 83 | splitLine->setFrameShape(QFrame::HLine); |
| 84 | splitLine->setFrameShadow(QFrame::Sunken); |
| 85 | return splitLine; }; |
| 86 | |
| 87 | auto formLayout = new QFormLayout(); |
| 88 | formLayout->addRow(tr("trace directory:" ), hLayout); |
| 89 | formLayout->addRow(tr("process ID:" ), d->pidInput); |
| 90 | formLayout->addRow(splitLine()); |
| 91 | formLayout->addRow(tr("recent:" ), d->historyComboBox); |
| 92 | |
| 93 | auto centerLayout = new QVBoxLayout(this); |
| 94 | centerLayout->addLayout(formLayout); |
| 95 | centerLayout->addStretch(); |
| 96 | centerLayout->addWidget(splitLine()); |
| 97 | centerLayout->addWidget(d->buttonBox); |
| 98 | |
| 99 | connect(d->traceDir, &QLineEdit::textChanged, |
| 100 | this, &LoadCoreDialog::updatePid); |
| 101 | |
| 102 | connect(btnBrowser, &QPushButton::clicked, this, &LoadCoreDialog::showFileDialog); |
| 103 | |
| 104 | connect(d->historyComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), |
| 105 | this, &LoadCoreDialog::historyIndexChanged); |
| 106 | |
| 107 | connect(d->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); |
| 108 | connect(d->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); |
| 109 | |
| 110 | updatePid(); |
| 111 | } |
| 112 | |
| 113 | void LoadCoreDialog::updatePid() |
| 114 | { |
| 115 | QString traceDir = d->traceDir->text(); |
| 116 | QDir dir(traceDir); |
| 117 | bool okEnabled = dir.exists(); |
| 118 | d->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(okEnabled); |
| 119 | |
| 120 | // fill pid combo list here! |
| 121 | if (okEnabled) { |
| 122 | d->pidInput->clear(); |
| 123 | |
| 124 | dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks); |
| 125 | |
| 126 | QFileInfoList list = dir.entryInfoList(); |
| 127 | QString mapname = QLatin1String(MAP_FILE_NAME); |
| 128 | for (int i = 0; i < list.size(); ++i) { |
| 129 | QFileInfo fileInfo = list.at(i); |
| 130 | if (0 == fileInfo.fileName().indexOf(mapname)) { |
| 131 | d->pidInput->addItem(fileInfo.fileName().mid(mapname.size())); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | d->pidInput->setCurrentIndex(0); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void LoadCoreDialog::historyIndexChanged(int) |
| 140 | { |
| 141 | // do something. |
| 142 | } |
| 143 | |
| 144 | void LoadCoreDialog::showFileDialog() |
| 145 | { |
| 146 | QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory" ), |
| 147 | d->traceDir->text(), |
| 148 | QFileDialog::ShowDirsOnly |
| 149 | | QFileDialog::DontResolveSymlinks); |
| 150 | |
| 151 | if (!dir.isEmpty()) |
| 152 | d->traceDir->setText(dir); |
| 153 | } |
| 154 | |
| 155 | } // namespace ReverseDebugger |
| 156 | } // namespace Internal |
| 157 | |