ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
project_widget.cc
Go to the documentation of this file.
1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 // its contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
32 #include "ui/project_widget.h"
33 
34 #include "base/database.h"
35 
36 namespace colmap {
37 
38 ProjectWidget::ProjectWidget(QWidget* parent, OptionManager* options)
39  : QWidget(parent), options_(options), prev_selected_(false) {
40  setWindowFlags(Qt::Dialog);
41  setWindowModality(Qt::ApplicationModal);
42  setWindowTitle("Project");
43 
44  // Database path.
45  QPushButton* databse_path_new = new QPushButton(tr("New"), this);
46  connect(databse_path_new, &QPushButton::released, this,
47  &ProjectWidget::SelectNewDatabasePath);
48  QPushButton* databse_path_open = new QPushButton(tr("Open"), this);
49  connect(databse_path_open, &QPushButton::released, this,
50  &ProjectWidget::SelectExistingDatabasePath);
51  database_path_text_ = new QLineEdit(this);
52  database_path_text_->setText(
53  QString::fromStdString(*options_->database_path));
54 
55  // Image path.
56  QPushButton* image_path_select = new QPushButton(tr("Select"), this);
57  connect(image_path_select, &QPushButton::released, this,
58  &ProjectWidget::SelectImagePath);
59  image_path_text_ = new QLineEdit(this);
60  image_path_text_->setText(QString::fromStdString(*options_->image_path));
61 
62  // Save button.
63  QPushButton* create_button = new QPushButton(tr("Save"), this);
64  connect(create_button, &QPushButton::released, this, &ProjectWidget::Save);
65 
66  QGridLayout* grid = new QGridLayout(this);
67 
68  grid->addWidget(new QLabel(tr("Database"), this), 0, 0);
69  grid->addWidget(database_path_text_, 0, 1);
70  grid->addWidget(databse_path_new, 0, 2);
71  grid->addWidget(databse_path_open, 0, 3);
72 
73  grid->addWidget(new QLabel(tr("Images"), this), 1, 0);
74  grid->addWidget(image_path_text_, 1, 1);
75  grid->addWidget(image_path_select, 1, 2);
76 
77  grid->addWidget(create_button, 2, 2);
78 }
79 
80 bool ProjectWidget::IsValid() const {
83 }
84 
86  database_path_text_->clear();
87  image_path_text_->clear();
88 }
89 
90 std::string ProjectWidget::GetDatabasePath() const {
91  return database_path_text_->text().toUtf8().constData();
92 }
93 
94 std::string ProjectWidget::GetImagePath() const {
95  return image_path_text_->text().toUtf8().constData();
96 }
97 
98 void ProjectWidget::SetDatabasePath(const std::string& path) {
99  database_path_text_->setText(QString::fromStdString(path));
100 }
101 
102 void ProjectWidget::SetImagePath(const std::string& path) {
103  image_path_text_->setText(QString::fromStdString(path));
104 }
105 
106 void ProjectWidget::Save() {
107  if (IsValid()) {
108  *options_->database_path = GetDatabasePath();
109  *options_->image_path = GetImagePath();
110 
111  // Save empty database file.
112  Database database(*options_->database_path);
113 
114  hide();
115  } else {
116  QMessageBox::critical(this, "", tr("Invalid paths"));
117  }
118 }
119 
120 void ProjectWidget::SelectNewDatabasePath() {
121  QString database_path = QFileDialog::getSaveFileName(
122  this, tr("Select database file"), DefaultDirectory(),
123  tr("SQLite3 database (*.db)"));
124  if (database_path != "") {
125  if (!HasFileExtension(database_path.toUtf8().constData(), ".db")) {
126  database_path += ".db";
127  }
128  database_path_text_->setText(database_path);
129  }
130 }
131 
132 void ProjectWidget::SelectExistingDatabasePath() {
133  const auto database_path = QFileDialog::getOpenFileName(
134  this, tr("Select database file"), DefaultDirectory(),
135  tr("SQLite3 database (*.db)"));
136  if (database_path != "") {
137  database_path_text_->setText(database_path);
138  }
139 }
140 
141 void ProjectWidget::SelectImagePath() {
142  const auto image_path = QFileDialog::getExistingDirectory(
143  this, tr("Select image path..."), DefaultDirectory(),
144  QFileDialog::ShowDirsOnly);
145  if (image_path != "") {
146  image_path_text_->setText(image_path);
147  }
148 }
149 
150 QString ProjectWidget::DefaultDirectory() {
151  if (prev_selected_) {
152  return "";
153  }
154 
155  prev_selected_ = true;
156 
157  if (!options_->project_path->empty()) {
158  const auto parent_path = GetParentDir(*options_->project_path);
159  if (ExistsDir(parent_path)) {
160  return QString::fromStdString(parent_path);
161  }
162  }
163 
164  if (!database_path_text_->text().isEmpty()) {
165  const auto parent_path =
166  GetParentDir(database_path_text_->text().toUtf8().constData());
167  if (ExistsDir(parent_path)) {
168  return QString::fromStdString(parent_path);
169  }
170  }
171 
172  if (!image_path_text_->text().isEmpty()) {
173  return image_path_text_->text();
174  }
175 
176  return "";
177 }
178 
179 } // namespace colmap
std::shared_ptr< std::string > database_path
std::shared_ptr< std::string > project_path
std::shared_ptr< std::string > image_path
std::string GetDatabasePath() const
std::string GetImagePath() const
ProjectWidget(QWidget *parent, OptionManager *options)
void SetDatabasePath(const std::string &path)
void SetImagePath(const std::string &path)
static const std::string path
Definition: PointCloud.cpp:59
bool HasFileExtension(const std::string &file_name, const std::string &ext)
Definition: misc.cc:51
bool ExistsDir(const std::string &path)
Definition: misc.cc:104
std::string GetParentDir(const std::string &path)
Definition: misc.cc:128