27#include <QDialogButtonBox>
40QString expandUserPath(
const QString &path) {
41 if (path.startsWith(
"~")) {
42 return QDir::homePath() + path.mid(1);
49OpenProjectDialog::OpenProjectDialog(QWidget *parent) : QDialog(parent) {
50 setWindowTitle(tr(
"Open Project"));
54 auto *layout =
new QVBoxLayout(
this);
57 auto *formLayout =
new QFormLayout();
60 auto *dbRow =
new QHBoxLayout();
61 dbPathEdit_ =
new QLineEdit(
this);
64 dbPathEdit_->setClearButtonEnabled(
true);
65 dbBrowseButton_ =
new QPushButton(tr(
"Browse..."),
this);
66 dbRow->addWidget(dbPathEdit_);
67 dbRow->addWidget(dbBrowseButton_);
68 formLayout->addRow(tr(
"Compilation Database:"), dbRow);
71 auto *projectRow =
new QHBoxLayout();
72 projectRootEdit_ =
new QLineEdit(
this);
73 projectRootEdit_->setClearButtonEnabled(
true);
74 projectRootBrowseButton_ =
new QPushButton(tr(
"Browse..."),
this);
75 projectRow->addWidget(projectRootEdit_);
76 projectRow->addWidget(projectRootBrowseButton_);
77 formLayout->addRow(tr(
"Project Root (optional):"), projectRow);
79 layout->addLayout(formLayout);
83 new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
84 layout->addWidget(buttonBox);
87 connect(dbBrowseButton_, &QPushButton::clicked,
this,
88 &OpenProjectDialog::browseCompilationDatabase);
89 connect(projectRootBrowseButton_, &QPushButton::clicked,
this,
90 &OpenProjectDialog::browseProjectRoot);
91 connect(buttonBox, &QDialogButtonBox::accepted,
this,
92 &OpenProjectDialog::validateAndAccept);
93 connect(buttonBox, &QDialogButtonBox::rejected,
this, &QDialog::reject);
95 dbPathEdit_->setFocus();
99 return dbPathEdit_->text();
103 return projectRootEdit_->text();
106void OpenProjectDialog::browseCompilationDatabase() {
108 QString current = dbPathEdit_->text().trimmed();
109 if (!current.isEmpty()) {
110 QFileInfo info(current);
111 initialDir = info.absolutePath();
113 QString fileName = QFileDialog::getOpenFileName(
114 this, tr(
"Select Compilation Database"), initialDir,
115 tr(
"Compilation Database (*.json);;All Files (*)"));
116 if (!fileName.isEmpty()) {
117 dbPathEdit_->setText(fileName);
121void OpenProjectDialog::browseProjectRoot() {
122 QString dir = QFileDialog::getExistingDirectory(
123 this, tr(
"Select Project Root Directory"),
124 projectRootEdit_->text().trimmed(), QFileDialog::ShowDirsOnly);
125 if (!dir.isEmpty()) {
126 projectRootEdit_->setText(dir);
130void OpenProjectDialog::validateAndAccept() {
131 const QString rawDbPath = dbPathEdit_->text().trimmed();
132 if (rawDbPath.isEmpty()) {
133 QMessageBox::warning(
this, tr(
"Validation Error"),
134 tr(
"Please enter a compilation database path."));
138 const QString expandedDbPath = expandUserPath(rawDbPath);
139 QFileInfo dbInfo(expandedDbPath);
140 if (!dbInfo.exists() || !dbInfo.isFile()) {
141 QMessageBox::warning(
142 this, tr(
"Validation Error"),
143 tr(
"Compilation database not found:\n%1").arg(expandedDbPath));
147 dbPathEdit_->setText(dbInfo.absoluteFilePath());
149 const QString rawProjectRoot = projectRootEdit_->text().trimmed();
150 if (!rawProjectRoot.isEmpty()) {
151 const QString expandedProjectRoot = expandUserPath(rawProjectRoot);
152 QFileInfo rootInfo(expandedProjectRoot);
153 if (!rootInfo.exists() || !rootInfo.isDir()) {
154 QMessageBox::warning(
155 this, tr(
"Validation Error"),
156 tr(
"Project root directory not found:\n%1").arg(expandedProjectRoot));
159 projectRootEdit_->setText(rootInfo.absoluteFilePath());
161 projectRootEdit_->clear();
Dialog for opening a project with compilation database and optional project root.
QString projectRootPath() const
Get the selected project root path.
QString compilationDatabasePath() const
Get the selected compilation database path.