Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent duplicate 3D model files #787

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 54 additions & 5 deletions src/imp/3d/imp_package_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,48 @@ void ImpPackage::update_points()
}
}

std::string ImpPackage::ask_3d_model_filename(const std::string &current_filename)
std::vector<std::string> ImpPackage::ask_3d_model_filenames()
{
GtkFileChooserNative *native = gtk_file_chooser_native_new("Open", GTK_WINDOW(view_3d_window->gobj()),
GTK_FILE_CHOOSER_ACTION_OPEN, "_Open", "_Cancel");
auto chooser = Glib::wrap(GTK_FILE_CHOOSER(native));
auto filter = Gtk::FileFilter::create();
filter->set_name("STEP models");
filter->add_pattern("*.step");
filter->add_pattern("*.STEP");
filter->add_pattern("*.stp");
filter->add_pattern("*.STP");
chooser->add_filter(filter);
chooser->set_select_multiple(true);
chooser->set_current_folder(pool->get_base_path());

while (1) {
if (gtk_native_dialog_run(GTK_NATIVE_DIALOG(native)) == GTK_RESPONSE_ACCEPT) {
auto base_path = Gio::File::create_for_path(pool->get_base_path());
std::vector<std::string> rel_names;

for (const auto &file : chooser->get_files()) {
std::string rel_path = base_path->get_relative_path(file);
replace_backslash(rel_path);
rel_names.push_back(rel_path);
}
if (std::none_of(rel_names.cbegin(), rel_names.cend(), [](const auto &s) { return s.empty(); })) {
return rel_names;
}
else {
Gtk::MessageDialog md(*view_3d_window, "All models have to be in the pool directory",
false /* use_markup */, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK);
md.run();
}
}
else {
return {};
}
}
return {};
}

std::string ImpPackage::ask_replace_3d_model_filename(const std::string &current_filename)
{
GtkFileChooserNative *native = gtk_file_chooser_native_new("Open", GTK_WINDOW(view_3d_window->gobj()),
GTK_FILE_CHOOSER_ACTION_OPEN, "_Open", "_Cancel");
Expand All @@ -55,7 +96,8 @@ std::string ImpPackage::ask_3d_model_filename(const std::string &current_filenam
filter->add_pattern("*.STP");
chooser->add_filter(filter);
if (current_filename.size()) {
chooser->set_filename(Glib::build_filename(pool->get_base_path(), current_filename));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.gtk.org/gtk3/method.FileChooser.set_filename.html

says this should only be used in a save chooser, I found it caused problems with the directory defaulting to the last used directory instead of the model directory

Copy link
Member

@carrotIndustries carrotIndustries Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, for me it does work.

EDIT: only once...
EDIT2: nvm EDIT, different issue

chooser->set_current_folder(
Glib::path_get_dirname(Glib::build_filename(pool->get_base_path(), current_filename)));
}
else {
chooser->set_current_folder(pool->get_base_path());
Expand Down Expand Up @@ -158,10 +200,17 @@ void ImpPackage::construct_3d()
});
box->pack_end(*button_reload, false, false, 0);

auto button_add = Gtk::manage(new Gtk::Button("Add model"));
auto button_add = Gtk::manage(new Gtk::Button("Add models…"));
button_add->signal_clicked().connect([this] {
auto mfn = ask_3d_model_filename();
if (mfn.size()) {
auto new_mfns = ask_3d_model_filenames();
std::set<std::string> current_mfns;
for (const auto &[_, model] : core_package.models) {
current_mfns.insert(model.filename);
}
for (const auto &mfn : new_mfns) {
if (current_mfns.count(mfn)) {
continue;
}
auto uu = UUID::random();
if (core_package.models.size() == 0) { // first
core_package.default_model = uu;
Expand Down
21 changes: 13 additions & 8 deletions src/imp/3d/model_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,21 @@ ModelEditor::ModelEditor(ImpPackage &iimp, const UUID &iuu)

auto browse_button = Gtk::manage(new Gtk::Button("Browse…"));
browse_button->signal_clicked().connect([this, entry] {
Package::Model *model2 = nullptr;
if (imp.core_package.models.count(uu)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unable to a see a code path where imp.core_package.models[uu] goes away between the constructor and here, so I removed the check and directly use imp.core_package.models.at(uu). If the delete button is used then this button goes away too.

model2 = &imp.core_package.models.at(uu);
std::string mfn = imp.ask_replace_3d_model_filename(imp.core_package.models.at(uu).filename);
if (mfn.empty()) {
return;
}
auto mfn = imp.ask_3d_model_filename(model2 ? model2->filename : "");
if (mfn.size()) {
entry->set_text(mfn);
imp.view_3d_window->set_needs_update();
imp.view_3d_window->update(true);
if (std::find_if(imp.core_package.models.cbegin(), imp.core_package.models.cend(),
[mfn](auto &it) { return mfn == it.second.filename; })
!= imp.core_package.models.end()) {
Gtk::MessageDialog md(*imp.view_3d_window, "Model is already used in package", false /* use_markup */,
Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK);
md.run();
return;
}
entry->set_text(mfn);
imp.view_3d_window->set_needs_update();
imp.view_3d_window->update(/* clear = */ true);
});
box->pack_end(*browse_button, false, false, 0);

Expand Down
3 changes: 2 additions & 1 deletion src/imp/imp_package.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ class ImpPackage : public ImpLayer {

Glib::RefPtr<Gio::SimpleAction> snap_to_pad_bbox_action;

std::string ask_3d_model_filename(const std::string &current_filename = "");
std::vector<std::string> ask_3d_model_filenames();
std::string ask_replace_3d_model_filename(const std::string &current_filename);
void construct_3d();
void update_model_editors();
void reload_model_editor();
Expand Down
Loading