ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ProjectWidget.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "ProjectWidget.h"
9 
10 #include "base/database.h"
11 #include "util/option_manager.h"
12 
13 // CV_CORE_LIB
14 #include <CVTools.h>
15 
16 namespace cloudViewer {
17 
18 using namespace colmap;
19 
20 ProjectWidget::ProjectWidget(QWidget* parent, OptionManager* options)
21  : QWidget(parent), options_(options), prev_selected_(false) {
22  setWindowFlags(Qt::Dialog);
23  setWindowModality(Qt::ApplicationModal);
24  setWindowTitle("Project");
25 
26  // Database path.
27  QPushButton* databse_path_new = new QPushButton(tr("New"), this);
28  connect(databse_path_new, &QPushButton::released, this,
29  &ProjectWidget::SelectNewDatabasePath);
30  QPushButton* databse_path_open = new QPushButton(tr("Open"), this);
31  connect(databse_path_open, &QPushButton::released, this,
32  &ProjectWidget::SelectExistingDatabasePath);
33  database_path_text_ = new QLineEdit(this);
34  database_path_text_->setText(
35  QString::fromStdString(*options_->database_path));
36 
37  // Image path.
38  QPushButton* image_path_select = new QPushButton(tr("Select"), this);
39  connect(image_path_select, &QPushButton::released, this,
40  &ProjectWidget::SelectImagePath);
41  image_path_text_ = new QLineEdit(this);
42  image_path_text_->setText(QString::fromStdString(*options_->image_path));
43 
44  // Save button.
45  QPushButton* create_button = new QPushButton(tr("Save"), this);
46  connect(create_button, &QPushButton::released, this, &ProjectWidget::Save);
47 
48  QGridLayout* grid = new QGridLayout(this);
49 
50  grid->addWidget(new QLabel(tr("Database"), this), 0, 0);
51  grid->addWidget(database_path_text_, 0, 1);
52  grid->addWidget(databse_path_new, 0, 2);
53  grid->addWidget(databse_path_open, 0, 3);
54 
55  grid->addWidget(new QLabel(tr("Images"), this), 1, 0);
56  grid->addWidget(image_path_text_, 1, 1);
57  grid->addWidget(image_path_select, 1, 2);
58 
59  grid->addWidget(create_button, 2, 2);
60 }
61 
62 bool ProjectWidget::IsValid() const {
63  return ExistsDir(GetImagePath()) && !ExistsDir(GetDatabasePath()) &&
64  ExistsDir(GetParentDir(GetDatabasePath()));
65 }
66 
68  database_path_text_->clear();
69  image_path_text_->clear();
70 }
71 
72 void ProjectWidget::persistSave(const std::string& project_path,
73  const std::string& database_path,
74  const std::string& image_path) {
75  QSettings settings;
76  settings.beginGroup("Reconstruction");
77  settings.setValue("project_path", CVTools::ToQString(project_path));
78  settings.setValue("database_path", CVTools::ToQString(database_path));
79  settings.setValue("image_path", CVTools::ToQString(image_path));
80  settings.endGroup();
81 }
82 
83 std::string ProjectWidget::GetDatabasePath() const {
84  return database_path_text_->text().toUtf8().constData();
85 }
86 
87 std::string ProjectWidget::GetImagePath() const {
88  return image_path_text_->text().toUtf8().constData();
89 }
90 
91 void ProjectWidget::SetDatabasePath(const std::string& path) {
92  database_path_text_->setText(QString::fromStdString(path));
93 }
94 
95 void ProjectWidget::SetImagePath(const std::string& path) {
96  image_path_text_->setText(QString::fromStdString(path));
97 }
98 
99 void ProjectWidget::Save() {
100  if (IsValid()) {
101  *options_->database_path = GetDatabasePath();
102  *options_->image_path = GetImagePath();
103 
104  // Save empty database file.
105  Database database(*options_->database_path);
106  persistSave(*options_->project_path, *options_->database_path,
107  *options_->image_path);
108 
109  hide();
110  } else {
111  QMessageBox::critical(this, "", tr("Invalid paths"));
112  }
113 }
114 
115 void ProjectWidget::SelectNewDatabasePath() {
116  QString database_path = QFileDialog::getSaveFileName(
117  this, tr("Select database file"), DefaultDirectory(),
118  tr("SQLite3 database (*.db)"));
119  if (database_path != "") {
120  if (!HasFileExtension(database_path.toUtf8().constData(), ".db")) {
121  database_path += ".db";
122  }
123  database_path_text_->setText(database_path);
124  }
125 }
126 
127 void ProjectWidget::SelectExistingDatabasePath() {
128  const auto database_path = QFileDialog::getOpenFileName(
129  this, tr("Select database file"), DefaultDirectory(),
130  tr("SQLite3 database (*.db)"));
131  if (database_path != "") {
132  database_path_text_->setText(database_path);
133  }
134 }
135 
136 void ProjectWidget::SelectImagePath() {
137  const auto image_path = QFileDialog::getExistingDirectory(
138  this, tr("Select image path..."), DefaultDirectory(),
139  QFileDialog::ShowDirsOnly);
140  if (image_path != "") {
141  image_path_text_->setText(image_path);
142  }
143 }
144 
145 QString ProjectWidget::DefaultDirectory() {
146  if (prev_selected_) {
147  return "";
148  }
149 
150  prev_selected_ = true;
151 
152  if (!options_->project_path->empty()) {
153  const auto parent_path = GetParentDir(*options_->project_path);
154  if (ExistsDir(parent_path)) {
155  return QString::fromStdString(parent_path);
156  }
157  }
158 
159  if (!database_path_text_->text().isEmpty()) {
160  const auto parent_path =
161  GetParentDir(database_path_text_->text().toUtf8().constData());
162  if (ExistsDir(parent_path)) {
163  return QString::fromStdString(parent_path);
164  }
165  }
166 
167  if (!image_path_text_->text().isEmpty()) {
168  return image_path_text_->text();
169  }
170 
171  return "";
172 }
173 
174 } // namespace cloudViewer
static QString ToQString(const std::string &s)
Definition: CVTools.cpp:92
void SetImagePath(const std::string &path)
std::string GetImagePath() const
void persistSave(const std::string &project_path, const std::string &database_path, const std::string &image_path)
void SetDatabasePath(const std::string &path)
std::string GetDatabasePath() const
ProjectWidget(QWidget *parent, OptionManager *options)
static const std::string path
Definition: PointCloud.cpp:59
bool HasFileExtension(const std::string &file_name, const std::string &ext)
Definition: FileSystem.cpp:117
Generic file read and write utility for python interface.
colmap::OptionManager OptionManager