diff options
author | Gunnar Beutner <gbeutner@serenityos.org> | 2021-05-20 21:50:53 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-20 22:22:56 +0200 |
commit | 2a16c8bdb8b285209950a4e26b3eee1d95ec1a7a (patch) | |
tree | 65c2f48a960f161401d248e4982692c066205308 /Userland/Applications/3DFileViewer/main.cpp | |
parent | 728e6dad7306b2aceff46b19491346e49b0bba15 (diff) | |
download | serenity-2a16c8bdb8b285209950a4e26b3eee1d95ec1a7a.zip |
3DFileViewer: Clean up file handling
This unifies how 3DFileViewer handles the initial file when starting
the application and when opening files later on via the menu.
Errors are shown both for the initial load as well as when loading
files later on. An error during file load no longer clears the
existing model.
It also adds support for specifying the filename as a command-line
argument.
The opened file's name is shown in the titlebar.
Diffstat (limited to 'Userland/Applications/3DFileViewer/main.cpp')
-rw-r--r-- | Userland/Applications/3DFileViewer/main.cpp | 76 |
1 files changed, 41 insertions, 35 deletions
diff --git a/Userland/Applications/3DFileViewer/main.cpp b/Userland/Applications/3DFileViewer/main.cpp index 93c8c616d8..cb12a42431 100644 --- a/Userland/Applications/3DFileViewer/main.cpp +++ b/Userland/Applications/3DFileViewer/main.cpp @@ -59,11 +59,6 @@ private: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } glEndList(); - - // Load the teapot - m_mesh = m_mesh_loader->load("/home/anon/Documents/3D Models/teapot.obj"); - - dbgln("3DFileViewer: teapot mesh has {} triangles.", m_mesh->triangle_count()); } virtual void paint_event(GUI::PaintEvent&) override; @@ -109,15 +104,39 @@ void GLContextWidget::timer_event(Core::TimerEvent&) update(); } -bool GLContextWidget::load(const String& fname) +bool GLContextWidget::load(const String& filename) { - m_mesh = m_mesh_loader->load(fname); - if (!m_mesh.is_null()) { - dbgln("3DFileViewer: mesh has {} triangles.", m_mesh->triangle_count()); - return true; + auto file = Core::File::construct(filename); + + if (!file->filename().ends_with(".obj")) { + GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: invalid file type", filename), "Error", GUI::MessageBox::Type::Error); + return false; + } + + if (!file->open(Core::OpenMode::ReadOnly) && file->error() != ENOENT) { + GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: {}", filename, strerror(errno)), "Error", GUI::MessageBox::Type::Error); + return false; + } + + if (file->is_device()) { + GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: Can't open device files", filename), "Error", GUI::MessageBox::Type::Error); + return false; } - return false; + if (file->is_directory()) { + GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: Can't open directories", filename), "Error", GUI::MessageBox::Type::Error); + return false; + } + + auto new_mesh = m_mesh_loader->load(file); + if (new_mesh.is_null()) { + GUI::MessageBox::show(window(), String::formatted("Reading \"{}\" failed.", filename), "Error", GUI::MessageBox::Type::Error); + return false; + } + + m_mesh = new_mesh; + dbgln("3DFileViewer: mesh has {} triangles.", m_mesh->triangle_count()); + return true; } int main(int argc, char** argv) @@ -143,36 +162,20 @@ int main(int argc, char** argv) auto menubar = GUI::Menubar::construct(); auto& file_menu = menubar->add_menu("&File"); + auto load_model = [&](StringView const& filename) { + if (widget.load(filename)) { + auto canonical_path = Core::File::real_path_for(filename); + window->set_title(String::formatted("{} - 3D File Viewer", canonical_path)); + } + }; + file_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) { Optional<String> open_path = GUI::FilePicker::get_open_filepath(window); if (!open_path.has_value()) return; - auto file = Core::File::construct(open_path.value()); - - if (!file->filename().ends_with(".obj")) { - GUI::MessageBox::show(window, String::formatted("Opening \"{}\" failed: invalid file type", open_path.value()), "Error", GUI::MessageBox::Type::Error); - return; - } - - if (!file->open(Core::OpenMode::ReadOnly) && file->error() != ENOENT) { - GUI::MessageBox::show(window, String::formatted("Opening \"{}\" failed: {}", open_path.value(), strerror(errno)), "Error", GUI::MessageBox::Type::Error); - return; - } - - if (file->is_device()) { - GUI::MessageBox::show(window, String::formatted("Opening \"{}\" failed: Can't open device files", open_path.value()), "Error", GUI::MessageBox::Type::Error); - return; - } - - if (file->is_directory()) { - GUI::MessageBox::show(window, String::formatted("Opening \"{}\" failed: Can't open directories", open_path.value()), "Error", GUI::MessageBox::Type::Error); - return; - } - - if (!widget.load(file->filename())) - GUI::MessageBox::show(window, String::formatted("Reading \"{}\" failed.", open_path.value()), "Error", GUI::MessageBox::Type::Error); + load_model(open_path.value()); })); file_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { @@ -185,5 +188,8 @@ int main(int argc, char** argv) window->set_menubar(move(menubar)); window->show(); + auto filename = argc > 1 ? argv[1] : "/home/anon/Documents/3D Models/teapot.obj"; + load_model(filename); + return app->exec(); } |