This repository was archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathAskUserForFile.cpp
More file actions
87 lines (72 loc) · 3.57 KB
/
Copy pathAskUserForFile.cpp
File metadata and controls
87 lines (72 loc) · 3.57 KB
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
// Copyright (c) 2021 The Orbit Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "SourcePathsMappingUI/AskUserForFile.h"
#include <QAbstractButton>
#include <QCheckBox>
#include <QDir>
#include <QFileDialog>
#include <QFileInfo>
#include <QMessageBox>
#include <QObject>
#include <QPushButton>
#include <QSettings>
#include <QString>
#include <QVariant>
#include <Qt>
#include <filesystem>
#include <memory>
namespace orbit_source_paths_mapping_ui {
constexpr const char* kAutocreateMappingKey = "auto_create_mapping";
constexpr const char* kPreviousSourcePathsMappingDirectoryKey =
"previous_source_paths_mapping_directory";
static std::unique_ptr<QCheckBox> CreateSourcePathsMappingCheckBox() {
std::unique_ptr<QCheckBox> check_box = std::make_unique<QCheckBox>(
"Automatically create a source paths mapping from my selected file.");
check_box->setToolTip(
"If enabled, Orbit will automatically try to create a source paths mapping from it. The "
"common suffix between the path given in the debug information and the local file path will "
"be stripped. From the rest a mapping will be created.");
QSettings settings{};
check_box->setCheckState(settings.value(kAutocreateMappingKey, true).toBool() ? Qt::Checked
: Qt::Unchecked);
return check_box;
}
std::optional<UserAnswers> AskUserForSourceFilePath(QWidget* parent,
const std::filesystem::path& file_path) {
QMessageBox message_box{QMessageBox::Warning, "Source code file not found",
QString("Could not find the source code file \"%1\" on this machine.")
.arg(QString::fromStdString(file_path.string())),
QMessageBox::Cancel, parent};
QPushButton* pick_file_button = message_box.addButton("Choose file...", QMessageBox::ActionRole);
{
std::unique_ptr<QCheckBox> check_box = CreateSourcePathsMappingCheckBox();
// Ownership will be transferred to message_box
message_box.setCheckBox(check_box.release());
}
std::optional<UserAnswers> maybe_user_answers;
QObject::connect(pick_file_button, &QAbstractButton::clicked, parent, [&]() {
std::optional<std::filesystem::path> maybe_local_file_path =
ShowFileOpenDialog(parent, file_path);
if (!maybe_local_file_path.has_value()) return;
maybe_user_answers.emplace();
maybe_user_answers->local_file_path = maybe_local_file_path.value();
maybe_user_answers->infer_source_paths_mapping = message_box.checkBox()->isChecked();
});
message_box.exec();
return maybe_user_answers;
}
std::optional<std::filesystem::path> ShowFileOpenDialog(QWidget* parent,
const std::filesystem::path& file_path) {
QSettings settings{};
QDir previous_directory{
settings.value(kPreviousSourcePathsMappingDirectoryKey, QDir::currentPath()).toString()};
const QString file_name = QString::fromStdString(file_path.filename().string());
QString user_chosen_file = QFileDialog::getOpenFileName(
parent, QString{"Choose %1"}.arg(QString::fromStdString(file_path.string())),
previous_directory.filePath(file_name), file_name);
if (user_chosen_file.isEmpty()) return std::nullopt;
settings.setValue(kPreviousSourcePathsMappingDirectoryKey, QFileInfo{user_chosen_file}.path());
return std::filesystem::path{user_chosen_file.toStdString()};
}
} // namespace orbit_source_paths_mapping_ui