From 56c99d8a239dbeabdb19636575da84599c82efba Mon Sep 17 00:00:00 2001 From: Maxime Gimeno Date: Mon, 13 May 2019 16:11:15 +0200 Subject: [PATCH] Replace IO API to be able to load/save several items at a time --- Polyhedron/demo/Polyhedron/MainWindow.cpp | 173 ++++++++++-------- Polyhedron/demo/Polyhedron/MainWindow.h | 9 +- .../Polyhedron/Plugins/IO/GOCAD_io_plugin.cpp | 33 ++-- .../Polyhedron/Plugins/IO/LAS_io_plugin.cpp | 29 +-- .../Polyhedron/Plugins/IO/Nef_io_plugin.cpp | 38 ++-- .../Polyhedron/Plugins/IO/OFF_io_plugin.cpp | 76 ++++++-- .../Plugins/IO/OFF_to_nef_io_plugin.cpp | 28 ++- .../Polyhedron/Plugins/IO/PLY_io_plugin.cpp | 79 ++++++-- .../Plugins/IO/Polylines_io_plugin.cpp | 53 ++++-- .../Polyhedron/Plugins/IO/STL_io_plugin.cpp | 40 ++-- .../Polyhedron/Plugins/IO/Surf_io_plugin.cpp | 25 ++- .../Polyhedron/Plugins/IO/XYZ_io_plugin.cpp | 38 ++-- .../Polyhedron/Plugins/IO/lcc_io_plugin.cpp | 17 +- .../Plugins/Mesh_3/C3t3_io_plugin.cpp | 131 +++++++------ .../Plugins/Mesh_3/Io_image_plugin.cpp | 30 ++- .../Plugins/PMP/Selection_plugin.cpp | 26 ++- .../Polyhedron_demo_io_plugin_interface.h | 16 +- .../CGAL/Three/Scene_3mf_item_interface.h | 4 + 18 files changed, 565 insertions(+), 280 deletions(-) create mode 100644 Three/include/CGAL/Three/Scene_3mf_item_interface.h diff --git a/Polyhedron/demo/Polyhedron/MainWindow.cpp b/Polyhedron/demo/Polyhedron/MainWindow.cpp index 7f7978a5e8f..d9b85c33c55 100644 --- a/Polyhedron/demo/Polyhedron/MainWindow.cpp +++ b/Polyhedron/demo/Polyhedron/MainWindow.cpp @@ -41,7 +41,7 @@ #include #include #include - +#include #ifdef QT_SCRIPT_LIB # include # ifdef QT_SCRIPTTOOLS_LIB @@ -998,20 +998,33 @@ void MainWindow::reloadItem() { CGAL::Three::Polyhedron_demo_io_plugin_interface* fileloader = findLoader(loader_name); QFileInfo fileinfo(filename); - - CGAL::Three::Scene_item* new_item = loadItem(fileinfo, fileloader); - if(!new_item) + bool ok; + QList new_items = loadItem(fileinfo, fileloader, ok, false); + if(!ok) return; - new_item->setName(item->name()); - new_item->setColor(item->color()); - new_item->setRenderingMode(item->renderingMode()); - new_item->setVisible(item->visible()); - Scene_item_with_properties *property_item = dynamic_cast(new_item); - scene->replaceItem(scene->item_id(item), new_item, true); - if(property_item) - property_item->copyProperties(item); - new_item->invalidateOpenGLBuffers(); - item->deleteLater(); + QVariant varian = item->property("load_mates"); + if (!varian.canConvert()) + qDebug()<<"Well, that's gonna be a problem !"; + QSequentialIterable iterable = varian.value(); + // Can use foreach: + int mate_id = 0; + Q_FOREACH(const QVariant &v, iterable) + { + Scene_item* mate = v.value(); + Scene_item* new_item = new_items[mate_id]; + if(!new_item) + qDebug()<<"That too, is gonna be a problem..."; + new_item->setName(mate->name()); + new_item->setColor(mate->color()); + new_item->setRenderingMode(mate->renderingMode()); + new_item->setVisible(mate->visible()); + Scene_item_with_properties *property_item = dynamic_cast(new_item); + scene->replaceItem(scene->item_id(mate), new_item, true); + if(property_item) + property_item->copyProperties(mate); + new_item->invalidateOpenGLBuffers(); + mate->deleteLater(); + } } } @@ -1105,7 +1118,7 @@ void MainWindow::open(QString filename) // collect all io_plugins and offer them to load if the file extension match one name filter // also collect all available plugin in case of a no extension match Q_FOREACH(CGAL::Three::Polyhedron_demo_io_plugin_interface* io_plugin, io_plugins) { - if ( !io_plugin->canLoad() ) continue; + if ( !io_plugin->canLoad(fileinfo) ) continue; all_items << io_plugin->name(); if ( file_matches_filter(io_plugin->loadNameFilters(), filename.toLower()) ) { @@ -1148,71 +1161,70 @@ void MainWindow::open(QString filename) settings.setValue("OFF open directory", fileinfo.absoluteDir().absolutePath()); - CGAL::Three::Scene_item* scene_item = loadItem(fileinfo, findLoader(load_pair.first)); + loadItem(fileinfo, findLoader(load_pair.first), ok); - if(!scene_item) + if(!ok) return; this->addToRecentFiles(fileinfo.absoluteFilePath()); - selectSceneItem(scene->addItem(scene_item)); - - CGAL::Three::Scene_group_item* group = - qobject_cast(scene_item); - if(group) - scene->redraw_model(); updateViewersBboxes(true); } bool MainWindow::open(QString filename, QString loader_name) { QFileInfo fileinfo(filename); - boost::optional item_opt; + boost::optional item_opt; CGAL::Three::Scene_item* item = 0; try { item_opt = wrap_a_call_to_cpp ([this, fileinfo, loader_name]() { - return loadItem(fileinfo, findLoader(loader_name)); + bool ok; + loadItem(fileinfo, findLoader(loader_name), ok); + return ok; }, this, __FILE__, __LINE__ ); if(!item_opt) return false; - else item = *item_opt; + //else item = *item_opt; } catch(std::logic_error& e) { std::cerr << e.what() << std::endl; return false; } - selectSceneItem(scene->addItem(item)); - - CGAL::Three::Scene_group_item* group = - qobject_cast(item); - if(group) - scene->redraw_model(); - return true; } -CGAL::Three::Scene_item* MainWindow::loadItem(QFileInfo fileinfo, CGAL::Three::Polyhedron_demo_io_plugin_interface* loader) { - CGAL::Three::Scene_item* item = NULL; +QList MainWindow::loadItem(QFileInfo fileinfo, + CGAL::Three::Polyhedron_demo_io_plugin_interface* loader, + bool &ok, + bool add_to_scene) { if(!fileinfo.isFile() || !fileinfo.isReadable()) { QMessageBox::warning(this, tr("Error"), QString("File %1 is not a readable file.") .arg(fileinfo.absoluteFilePath())); } - QApplication::setOverrideCursor(Qt::WaitCursor); - - item = loader->load(fileinfo); - QApplication::restoreOverrideCursor(); - if(!item) { + CGAL::Three::Three::CursorScopeGuard guard(QCursor(Qt::WaitCursor)); + QList result = loader->load(fileinfo, ok, add_to_scene); + selectSceneItem(scene->item_id(result.back())); + if(!ok) + { + QApplication::restoreOverrideCursor(); QMessageBox::warning(this, tr("Error"), QString("Could not load item from file %1 using plugin %2") .arg(fileinfo.absoluteFilePath()).arg(loader->name())); - return 0; + return QList(); } - - item->setProperty("source filename", fileinfo.absoluteFilePath()); - item->setProperty("loader_name", loader->name()); - return item; + for(Scene_item* item : result) + { + CGAL::Three::Scene_group_item* group = + qobject_cast(item); + if(group) + scene->redraw_model(); + item->setProperty("source filename", fileinfo.absoluteFilePath()); + item->setProperty("loader_name", loader->name()); + item->setProperty("load_mates",QVariant::fromValue(result)); + } + return result; } @@ -1831,7 +1843,7 @@ void MainWindow::on_actionLoad_triggered() typedef QMap FilterPluginMap; FilterPluginMap filterPluginMap; - Q_FOREACH(CGAL::Three::Polyhedron_demo_io_plugin_interface* plugin, io_plugins) { + for(CGAL::Three::Polyhedron_demo_io_plugin_interface* plugin : io_plugins) { QStringList split_filters = plugin->loadNameFilters().split(";;"); Q_FOREACH(const QString& filter, split_filters) { FilterPluginMap::iterator it = filterPluginMap.find(filter); @@ -1877,18 +1889,25 @@ void MainWindow::on_actionLoad_triggered() std::size_t nb_item = -1; Q_FOREACH(const QString& filename, dialog.selectedFiles()) { - + CGAL::Three::Scene_item* item = NULL; if(selectedPlugin) { QFileInfo info(filename); - item = loadItem(info, selectedPlugin); - item->setColor(colors_[++nb_item]); - Scene::Item_id index = scene->addItem(item); - selectSceneItem(index); - CGAL::Three::Scene_group_item* group = - qobject_cast(item); - if(group) - scene->redraw_model(); + bool ok; + QList result = loadItem(info, selectedPlugin, ok); + if(!ok) + continue; + for(Scene_item* item : result) + { + ++nb_item; + item->setColor(colors_[nb_item]); + Scene::Item_id index = scene->addItem(item); + selectSceneItem(index); + CGAL::Three::Scene_group_item* group = + qobject_cast(item); + if(group) + scene->redraw_model(); + } this->addToRecentFiles(filename); } else { int scene_size = scene->numberOfEntries(); @@ -1901,15 +1920,19 @@ void MainWindow::on_actionLoad_triggered() void MainWindow::on_actionSaveAs_triggered() { - Scene_item* item = NULL; - - Q_FOREACH(Scene::Item_id id, scene->selectionIndices()) + QList to_save; + for(Scene::Item_id id : scene->selectionIndices()) { - item = scene->item(id); + Scene_item* item = scene->item(id); + to_save.append(item); + } + while(!to_save.empty()) + { + Scene_item* item = to_save.front(); QVector canSavePlugins; QStringList filters; QString sf; - Q_FOREACH(CGAL::Three::Polyhedron_demo_io_plugin_interface* plugin, io_plugins) { + for(CGAL::Three::Polyhedron_demo_io_plugin_interface* plugin : io_plugins) { if(plugin->canSave(item)) { canSavePlugins << plugin; filters += plugin->saveNameFilters(); @@ -1925,7 +1948,7 @@ void MainWindow::on_actionSaveAs_triggered() tr("Cannot save"), tr("The selected object %1 cannot be saved.") .arg(item->name())); - return; + return; } Q_FOREACH(QString string, filters) { @@ -1961,14 +1984,14 @@ void MainWindow::on_actionSaveAs_triggered() dir, filters.join(";;"), &sf); - + if(filename.isEmpty()) return; last_saved_dir = QFileInfo(filename).absoluteDir().path(); extensions.indexIn(sf.split(";;").first()); QString filter_ext, filename_ext; filter_ext = extensions.cap().split(" ").first();// in case of syntax like (*.a *.b) - + filter_ext.remove(")"); filter_ext.remove("("); //remove * @@ -2016,18 +2039,18 @@ void MainWindow::on_actionSaveAs_triggered() } for(auto v : CGAL::QGLViewer::QGLViewerPool()) v->update(); - save(filename, item); + save(filename, to_save); } } -void MainWindow::save(QString filename, CGAL::Three::Scene_item* item) { +void MainWindow::save(QString filename, QList& to_save) { QFileInfo fileinfo(filename); bool saved = false; - Q_FOREACH(CGAL::Three::Polyhedron_demo_io_plugin_interface* plugin, io_plugins) { - if( plugin->canSave(item) && + for(CGAL::Three::Polyhedron_demo_io_plugin_interface* plugin : io_plugins) { + if( plugin->canSave(to_save.front()) && file_matches_filter(plugin->saveNameFilters(),filename.toLower()) ) { - if(plugin->save(item, fileinfo)) + if(plugin->save(fileinfo, to_save)) { saved = true; break; @@ -2038,7 +2061,7 @@ void MainWindow::save(QString filename, CGAL::Three::Scene_item* item) { QMessageBox::warning(this, tr("Cannot save"), tr("The selected object %1 was not saved. (Maybe a wrong extension ?)") - .arg(item->name())); + .arg(to_save.front()->name())); } void MainWindow::on_actionSaveSnapshot_triggered() @@ -2470,7 +2493,7 @@ QString MainWindow::get_item_stats() for(int i=0; iitem(id); - QString classname = item->property("classname").toString(); + QString classname = item->property("classname").toString(); if(classname.isEmpty()) classname = item->metaObject()->className(); if(classnames.at(i).contains(classname)) @@ -3131,14 +3154,16 @@ void MainWindow::on_action_Save_triggered() if(QMessageBox::question(this, "Save", "Are you sure you want to override these files ?") == QMessageBox::No) return; - Scene_item* item = nullptr; - Q_FOREACH(Scene::Item_id id, scene->selectionIndices()) + QList to_save; + + for(Scene::Item_id id : scene->selectionIndices()) { - item = scene->item(id); + Scene_item* item = scene->item(id); if(!item->property("source filename").toString().isEmpty()) { QString filename = item->property("source filename").toString(); - save(filename, item); + to_save.append(item); + save(filename, to_save); } } } diff --git a/Polyhedron/demo/Polyhedron/MainWindow.h b/Polyhedron/demo/Polyhedron/MainWindow.h index bae08376816..28a09278abd 100644 --- a/Polyhedron/demo/Polyhedron/MainWindow.h +++ b/Polyhedron/demo/Polyhedron/MainWindow.h @@ -87,11 +87,14 @@ public: @returns the IO plugin associated with `loader_name`*/ CGAL::Three::Polyhedron_demo_io_plugin_interface* findLoader(const QString& loader_name) const; - /*! \brief Loads an item with a given loader. + /*! \brief Loads on or more item with a given loader. * * throws `std::logic_error` if loading does not succeed or * `std::invalid_argument` if `fileinfo` specifies an invalid file*/ - CGAL::Three::Scene_item* loadItem(QFileInfo fileinfo, CGAL::Three::Polyhedron_demo_io_plugin_interface*); + QList loadItem(QFileInfo fileinfo, + CGAL::Three::Polyhedron_demo_io_plugin_interface*, + bool& ok, + bool add_to_scene=true); void computeViewerBBox(CGAL::qglviewer::Vec &min, CGAL::qglviewer::Vec &max); void updateViewerBbox(Viewer* vi, bool recenter, CGAL::qglviewer::Vec min, CGAL::qglviewer::Vec max); @@ -346,7 +349,7 @@ protected Q_SLOTS: //!Opens a dialog to save selected item if able. void on_actionSaveAs_triggered(); //!Calls the function save of the current plugin if able. - void save(QString filename, CGAL::Three::Scene_item* item); + void save(QString filename, QList& to_save); //!Calls the function saveSnapShot of the viewer. void on_actionSaveSnapshot_triggered(); //!Opens a Dialog to choose a color and make it the background color. diff --git a/Polyhedron/demo/Polyhedron/Plugins/IO/GOCAD_io_plugin.cpp b/Polyhedron/demo/Polyhedron/Plugins/IO/GOCAD_io_plugin.cpp index caae0705b5c..1709c1dfa60 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/IO/GOCAD_io_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/IO/GOCAD_io_plugin.cpp @@ -40,30 +40,31 @@ public: bool applicable(QAction*) const { return false;} QString nameFilters() const; QString name() const { return "gocad_plugin"; } - bool canLoad() const; - CGAL::Three::Scene_item* load(QFileInfo fileinfo); + bool canLoad(QFileInfo) const; + QList load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true); bool canSave(const CGAL::Three::Scene_item*); - bool save(const CGAL::Three::Scene_item*, QFileInfo fileinfo); + bool save(QFileInfo fileinfo, QList& ); }; QString Polyhedron_demo_gocad_plugin::nameFilters() const { return "GOCAD files (*.ts)"; } -bool Polyhedron_demo_gocad_plugin::canLoad() const { +bool Polyhedron_demo_gocad_plugin::canLoad(QFileInfo) const { return true; } -CGAL::Three::Scene_item* -Polyhedron_demo_gocad_plugin::load(QFileInfo fileinfo) { +QList +Polyhedron_demo_gocad_plugin::load(QFileInfo fileinfo, bool& ok, bool add_to_scene) { // Open file std::ifstream in(fileinfo.filePath().toUtf8()); if(!in) { std::cerr << "Error! Cannot open file " << (const char*)fileinfo.filePath().toUtf8() << std::endl; - return NULL; + ok = false; + return QList(); } @@ -74,7 +75,10 @@ Polyhedron_demo_gocad_plugin::load(QFileInfo fileinfo) { if(fileinfo.size() == 0) { CGAL::Three::Three::warning( tr("The file you are trying to load is empty.")); - return item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<(item->polyhedron()); @@ -82,7 +86,8 @@ Polyhedron_demo_gocad_plugin::load(QFileInfo fileinfo) { if(! read_gocad(P, in, name, color)){ std::cerr << "Error: Invalid polyhedron" << std::endl; delete item; - return 0; + ok = false; + return QList(); } t.stop(); @@ -98,7 +103,10 @@ Polyhedron_demo_gocad_plugin::load(QFileInfo fileinfo) { item->setColor(qcolor); } item->invalidateOpenGLBuffers(); - return item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<(item); } -bool Polyhedron_demo_gocad_plugin::save(const CGAL::Three::Scene_item* item, QFileInfo fileinfo) +bool Polyhedron_demo_gocad_plugin:: +save(QFileInfo fileinfo,QList& items) { + Scene_item* item = items.front(); // This plugin supports polyhedrons const Scene_surface_mesh_item* sm_item = qobject_cast(item); @@ -120,6 +130,7 @@ bool Polyhedron_demo_gocad_plugin::save(const CGAL::Three::Scene_item* item, QFi out.precision (std::numeric_limits::digits10 + 2); SMesh* poly = const_cast(sm_item->polyhedron()); write_gocad(*poly, out, qPrintable(fileinfo.baseName())); + items.pop_front(); return true; } diff --git a/Polyhedron/demo/Polyhedron/Plugins/IO/LAS_io_plugin.cpp b/Polyhedron/demo/Polyhedron/Plugins/IO/LAS_io_plugin.cpp index 37878451544..176824c2e16 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/IO/LAS_io_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/IO/LAS_io_plugin.cpp @@ -1,8 +1,9 @@ #include "Scene_points_with_normal_item.h" #include - +#include +#include #include - +using namespace CGAL::Three; class Polyhedron_demo_las_plugin : public QObject, public CGAL::Three::Polyhedron_demo_io_plugin_interface @@ -14,19 +15,19 @@ class Polyhedron_demo_las_plugin : public: QString name() const { return "las_plugin"; } QString nameFilters() const { return "LAS files (*.las);;Compressed LAS files (*.laz)"; } - bool canLoad() const; - CGAL::Three::Scene_item* load(QFileInfo fileinfo); + bool canLoad(QFileInfo fileinfo) const; + QList load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true); bool canSave(const CGAL::Three::Scene_item*); - bool save(const CGAL::Three::Scene_item*, QFileInfo fileinfo); + bool save(QFileInfo fileinfo,QList& ); }; -bool Polyhedron_demo_las_plugin::canLoad() const { +bool Polyhedron_demo_las_plugin::canLoad(QFileInfo fileinfo) const { return true; } -CGAL::Three::Scene_item* -Polyhedron_demo_las_plugin::load(QFileInfo fileinfo) { +QList Polyhedron_demo_las_plugin:: +load(QFileInfo fileinfo, bool& ok, bool add_to_scene) { std::ifstream in(fileinfo.filePath().toUtf8(), std::ios_base::binary); if(!in) @@ -37,11 +38,15 @@ Polyhedron_demo_las_plugin::load(QFileInfo fileinfo) { if(!item->read_las_point_set(in)) { delete item; - return 0; + ok = false; + return QList(); } item->setName(fileinfo.completeBaseName()); - return item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<(item); } -bool Polyhedron_demo_las_plugin::save(const CGAL::Three::Scene_item* item, QFileInfo fileinfo) +bool Polyhedron_demo_las_plugin::save(QFileInfo fileinfo,QList& items) { + Scene_item* item = items.front(); // Check extension (quietly) std::string extension = fileinfo.suffix().toUtf8().data(); if (extension != "las" && extension != "LAS") @@ -63,6 +69,7 @@ bool Polyhedron_demo_las_plugin::save(const CGAL::Three::Scene_item* item, QFile return false; std::ofstream out(fileinfo.filePath().toUtf8().data()); + items.pop_front(); return point_set_item->write_las_point_set(out); } diff --git a/Polyhedron/demo/Polyhedron/Plugins/IO/Nef_io_plugin.cpp b/Polyhedron/demo/Polyhedron/Plugins/IO/Nef_io_plugin.cpp index 11265427374..4aab33b4e0a 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/IO/Nef_io_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/IO/Nef_io_plugin.cpp @@ -17,32 +17,37 @@ class Polyhedron_demo_io_nef_plugin : public: QString nameFilters() const; QString name() const { return "io_nef_plugin"; } - bool canLoad() const; - CGAL::Three::Scene_item* load(QFileInfo fileinfo); + bool canLoad(QFileInfo) const; + QList load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true); bool canSave(const CGAL::Three::Scene_item*); - bool save(const CGAL::Three::Scene_item*, QFileInfo fileinfo); + bool save(QFileInfo fileinfo,QList& items); }; QString Polyhedron_demo_io_nef_plugin::nameFilters() const { return "nef files (*.nef3)"; } -bool Polyhedron_demo_io_nef_plugin::canLoad() const { +bool Polyhedron_demo_io_nef_plugin::canLoad(QFileInfo) const { return true; } -CGAL::Three::Scene_item* -Polyhedron_demo_io_nef_plugin::load(QFileInfo fileinfo) { +QList Polyhedron_demo_io_nef_plugin:: +load(QFileInfo fileinfo, bool& ok, bool add_to_scene) { //do not try file with extension different from nef3 - if (fileinfo.suffix() != "nef3") return 0; + if (fileinfo.suffix() != "nef3") + { + ok = false; + return QList(); + } // Open file std::ifstream in(fileinfo.filePath().toUtf8()); if(!in) { std::cerr << "Error! Cannot open file " << (const char*)fileinfo.filePath().toUtf8() << std::endl; - return NULL; + ok = false; + return QList(); } // Try to read .nef3 in a polyhedron @@ -51,15 +56,22 @@ Polyhedron_demo_io_nef_plugin::load(QFileInfo fileinfo) { if(fileinfo.size() == 0) { CGAL::Three::Three::warning( tr("The file you are trying to load is empty.")); - return item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<load(in)) { delete item; - return 0; + ok = false; + return QList(); } - return item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<(item); } -bool Polyhedron_demo_io_nef_plugin::save(const CGAL::Three::Scene_item* item, QFileInfo fileinfo) +bool Polyhedron_demo_io_nef_plugin::save(QFileInfo fileinfo,QList& items) { + Scene_item* item = items.front(); // This plugin supports polyhedrons and polygon soups const Scene_nef_polyhedron_item* nef_item = qobject_cast(item); @@ -79,6 +92,7 @@ bool Polyhedron_demo_io_nef_plugin::save(const CGAL::Three::Scene_item* item, QF std::ofstream out(fileinfo.filePath().toUtf8()); out.precision (std::numeric_limits::digits10 + 2); + items.pop_front(); return (nef_item && nef_item->save(out)); } diff --git a/Polyhedron/demo/Polyhedron/Plugins/IO/OFF_io_plugin.cpp b/Polyhedron/demo/Polyhedron/Plugins/IO/OFF_io_plugin.cpp index ba869f77b9d..58edff9b8e3 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/IO/OFF_io_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/IO/OFF_io_plugin.cpp @@ -40,22 +40,21 @@ public: } QString name() const { return "off_plugin"; } QString nameFilters() const { return "OFF files (*.off);;Wavefront OBJ (*.obj)"; } - bool canLoad() const; - CGAL::Three::Scene_item* load(QFileInfo fileinfo); - CGAL::Three::Scene_item* load_off(QFileInfo fileinfo); - CGAL::Three::Scene_item* load_obj(QFileInfo fileinfo); + bool canLoad(QFileInfo fileinfo) const; + QList load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true); + CGAL::Three::Scene_item* load_off(QFileInfo fileinfo); + CGAL::Three::Scene_item* load_obj(QFileInfo fileinfo); bool canSave(const CGAL::Three::Scene_item*); - bool save(const CGAL::Three::Scene_item*, QFileInfo fileinfo); + bool save(QFileInfo fileinfo,QList& ); }; -bool Polyhedron_demo_off_plugin::canLoad() const { +bool Polyhedron_demo_off_plugin::canLoad(QFileInfo) const { return true; } - -CGAL::Three::Scene_item* -Polyhedron_demo_off_plugin::load(QFileInfo fileinfo) { +QList Polyhedron_demo_off_plugin:: +load(QFileInfo fileinfo, bool& ok, bool add_to_scene) { if(fileinfo.size() == 0) { @@ -63,14 +62,41 @@ Polyhedron_demo_off_plugin::load(QFileInfo fileinfo) { Scene_surface_mesh_item* item = new Scene_surface_mesh_item(SMesh()); item->setName(fileinfo.completeBaseName()); - return item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<addItem(item); + return QList()<(); + } } else if(fileinfo.suffix().toLower() == "obj"){ - return load_obj(fileinfo); + + Scene_item* item = load_obj(fileinfo); + if(item) + { + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<(); + } } - return 0; + return QList(); } @@ -183,8 +209,12 @@ bool Polyhedron_demo_off_plugin::canSave(const CGAL::Three::Scene_item* item) qobject_cast(item); } -bool Polyhedron_demo_off_plugin::save(const CGAL::Three::Scene_item* item, QFileInfo fileinfo) + +bool +Polyhedron_demo_off_plugin:: +save(QFileInfo fileinfo,QList& items) { + Scene_item* item = items.front(); // This plugin supports point sets, surface_meshes and polygon soups const Scene_points_with_normal_item* points_item = qobject_cast(item); @@ -200,12 +230,26 @@ bool Polyhedron_demo_off_plugin::save(const CGAL::Three::Scene_item* item, QFile out.precision (std::numeric_limits::digits10 + 2); if(fileinfo.suffix().toLower() == "off"){ - return (sm_item && sm_item->save(out)) || + bool res = (sm_item && sm_item->save(out)) || (soup_item && soup_item->save(out)) || (points_item && points_item->write_off_point_set(out)); + if(res){ + items.pop_front(); + return true; + } + else{ + return false; + } } if(fileinfo.suffix().toLower() == "obj"){ - return (sm_item && sm_item->save_obj(out)); + bool res = (sm_item && sm_item->save_obj(out)); + if(res) + { + items.pop_front(); + return true; + } + else + return false; } return false; } diff --git a/Polyhedron/demo/Polyhedron/Plugins/IO/OFF_to_nef_io_plugin.cpp b/Polyhedron/demo/Polyhedron/Plugins/IO/OFF_to_nef_io_plugin.cpp index 5d142129b4c..dd765722381 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/IO/OFF_to_nef_io_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/IO/OFF_to_nef_io_plugin.cpp @@ -16,19 +16,19 @@ class Polyhedron_demo_off_to_nef_plugin : public: QString name() const { return "off_to_nef_plugin"; } QString nameFilters() const { return "OFF files, into nef (*.off)"; } - bool canLoad() const; - CGAL::Three::Scene_item* load(QFileInfo fileinfo); + bool canLoad(QFileInfo) const; + QList load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true); bool canSave(const CGAL::Three::Scene_item*); - bool save(const CGAL::Three::Scene_item*, QFileInfo fileinfo); + bool save(QFileInfo fileinfo,QList& ); }; -bool Polyhedron_demo_off_to_nef_plugin::canLoad() const { +bool Polyhedron_demo_off_to_nef_plugin::canLoad(QFileInfo) const { return true; } -CGAL::Three::Scene_item* -Polyhedron_demo_off_to_nef_plugin::load(QFileInfo fileinfo) { +QList Polyhedron_demo_off_to_nef_plugin:: +load(QFileInfo fileinfo, bool& ok, bool add_to_scene){ std::ifstream in(fileinfo.filePath().toUtf8()); if(!in) @@ -38,16 +38,23 @@ Polyhedron_demo_off_to_nef_plugin::load(QFileInfo fileinfo) { { CGAL::Three::Three::warning( tr("The file you are trying to load is empty.")); item->setName(fileinfo.completeBaseName()); - return item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<load_from_off(in)) { delete item; - return 0; + ok = false; + return QList()<setName(fileinfo.baseName()); - return item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<& items) { return false; } diff --git a/Polyhedron/demo/Polyhedron/Plugins/IO/PLY_io_plugin.cpp b/Polyhedron/demo/Polyhedron/Plugins/IO/PLY_io_plugin.cpp index cc2c1bcd865..d72084a7e0b 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/IO/PLY_io_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/IO/PLY_io_plugin.cpp @@ -13,7 +13,7 @@ #include #include #include - +using namespace CGAL::Three; class Polyhedron_demo_ply_plugin : public QObject, public CGAL::Three::Polyhedron_demo_io_plugin_interface @@ -31,20 +31,22 @@ public: } QString name() const { return "ply_plugin"; } QString nameFilters() const { return "PLY files (*.ply)"; } - bool canLoad() const; - CGAL::Three::Scene_item* load(QFileInfo fileinfo); + bool canLoad(QFileInfo fileinfo) const; + QList load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true); bool canSave(const CGAL::Three::Scene_item*); - bool save(const CGAL::Three::Scene_item*, QFileInfo fileinfo); + bool save(QFileInfo fileinfo,QList&); }; -bool Polyhedron_demo_ply_plugin::canLoad() const { +bool Polyhedron_demo_ply_plugin:: +canLoad(QFileInfo) const { return true; } -CGAL::Three::Scene_item* -Polyhedron_demo_ply_plugin::load(QFileInfo fileinfo) { +QList +Polyhedron_demo_ply_plugin:: +load(QFileInfo fileinfo, bool& ok, bool add_to_scene) { std::ifstream in(fileinfo.filePath().toUtf8(), std::ios_base::binary); if(!in) @@ -55,7 +57,8 @@ Polyhedron_demo_ply_plugin::load(QFileInfo fileinfo) { if(fileinfo.size() == 0) { CGAL::Three::Three::warning( tr("The file you are trying to load is empty.")); - return 0; + ok = false; + return QList(); } // Test if input is mesh or point set @@ -99,7 +102,10 @@ Polyhedron_demo_ply_plugin::load(QFileInfo fileinfo) { sm_item->setName(fileinfo.completeBaseName()); sm_item->comments() = comments; QApplication::restoreOverrideCursor(); - return sm_item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(sm_item); + return QList()<(); } Scene_polygon_soup_item* soup_item = new Scene_polygon_soup_item; soup_item->setName(fileinfo.completeBaseName()); soup_item->load (points, polygons, fcolors, vcolors); QApplication::restoreOverrideCursor(); - return soup_item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(soup_item); + return QList()<(); } if(item->has_normals()) item->setRenderingMode(CGAL::Three::Three::defaultPointSetRenderingMode()); item->setName(fileinfo.completeBaseName()); QApplication::restoreOverrideCursor(); - return item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<(); } bool Polyhedron_demo_ply_plugin::canSave(const CGAL::Three::Scene_item* item) @@ -152,8 +167,10 @@ bool Polyhedron_demo_ply_plugin::canSave(const CGAL::Three::Scene_item* item) || qobject_cast(item)); } -bool Polyhedron_demo_ply_plugin::save(const CGAL::Three::Scene_item* item, QFileInfo fileinfo) +bool Polyhedron_demo_ply_plugin:: +save(QFileInfo fileinfo,QList& items) { + Scene_item* item = items.front(); // Check extension (quietly) std::string extension = fileinfo.suffix().toUtf8().data(); if (extension != "ply" && extension != "PLY") @@ -179,25 +196,49 @@ bool Polyhedron_demo_ply_plugin::save(const CGAL::Three::Scene_item* item, QFile const Scene_points_with_normal_item* point_set_item = qobject_cast(item); if (point_set_item) - return point_set_item->write_ply_point_set(out, (choice == tr("Binary"))); + { + bool res = + point_set_item->write_ply_point_set(out, (choice == tr("Binary"))); + if(res) + items.pop_front(); + return res; + } // This plugin supports polygon soups const Scene_polygon_soup_item* soup_item = qobject_cast(item); if (soup_item) - return CGAL::write_PLY (out, soup_item->points(), soup_item->polygons()); + { + bool res = + CGAL::write_PLY (out, soup_item->points(), soup_item->polygons()); + if(res) + items.pop_front(); + return res; + } // This plugin supports surface meshes const Scene_surface_mesh_item* sm_item = qobject_cast(item); if (sm_item) - return CGAL::write_ply (out, *(sm_item->polyhedron()), sm_item->comments()); + { + bool res = + CGAL::write_ply (out, *(sm_item->polyhedron()), sm_item->comments()); + if(res) + items.pop_front(); + return res; + } // This plugin supports textured surface meshes const Scene_textured_surface_mesh_item* stm_item = qobject_cast(item); if (stm_item) - return CGAL::write_ply (out, *(stm_item->textured_face_graph())); + { + bool res = + CGAL::write_ply (out, *(stm_item->textured_face_graph())); + if(res) + items.pop_front(); + return res; + } return false; } diff --git a/Polyhedron/demo/Polyhedron/Plugins/IO/Polylines_io_plugin.cpp b/Polyhedron/demo/Polyhedron/Plugins/IO/Polylines_io_plugin.cpp index 769c0a0587e..222f7362d2f 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/IO/Polylines_io_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/IO/Polylines_io_plugin.cpp @@ -45,11 +45,11 @@ public: } QString name() const { return "polylines_io_plugin"; } QString nameFilters() const { return "Polylines files (*.polylines.txt *.cgal)"; } - bool canLoad() const; - CGAL::Three::Scene_item* load(QFileInfo fileinfo); + bool canLoad(QFileInfo fileinfo) const; + QList load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true); bool canSave(const CGAL::Three::Scene_item*); - bool save(const CGAL::Three::Scene_item*, QFileInfo fileinfo); + bool save(QFileInfo fileinfo,QList&); bool applicable(QAction* a) const { bool all_polylines_selected = true; Q_FOREACH(int index, scene->selectionIndices()) @@ -85,19 +85,21 @@ private: QAction* actionJoin_polylines; }; -bool Polyhedron_demo_polylines_io_plugin::canLoad() const { +bool Polyhedron_demo_polylines_io_plugin::canLoad(QFileInfo fileinfo) const{ return true; } -CGAL::Three::Scene_item* -Polyhedron_demo_polylines_io_plugin::load(QFileInfo fileinfo) { +QList +Polyhedron_demo_polylines_io_plugin:: +load(QFileInfo fileinfo, bool& ok, bool add_to_scene){ // Open file std::ifstream ifs(fileinfo.filePath().toUtf8()); if(!ifs) { std::cerr << "Error! Cannot open file " << (const char*)fileinfo.filePath().toUtf8() << std::endl; - return NULL; + ok = false; + return QList(); } if(fileinfo.size() == 0) @@ -105,7 +107,10 @@ Polyhedron_demo_polylines_io_plugin::load(QFileInfo fileinfo) { CGAL::Three::Three::warning( tr("The file you are trying to load is empty.")); Scene_polylines_item* item = new Scene_polylines_item; item->setName(fileinfo.completeBaseName()); - return item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()< > polylines; @@ -123,7 +128,11 @@ Polyhedron_demo_polylines_io_plugin::load(QFileInfo fileinfo) { Scene_polylines_item::Point_3 p; ifs >> p; polyline.push_back(p); - if(!ifs.good()) return 0; + if(!ifs.good()) + { + ok = false; + return QList(); + } } std::string line_remainder; std::getline(ifs, line_remainder); @@ -136,9 +145,17 @@ Polyhedron_demo_polylines_io_plugin::load(QFileInfo fileinfo) { std::cerr << " (metadata: \"" << qPrintable(metadata) << "\")\n"; } else { } - if(ifs.bad() || ifs.fail()) return 0; + if(ifs.bad() || ifs.fail()) + { + ok = false; + return QList(); + } + } + if(counter == 0) + { + ok = false; + return QList(); } - if(counter == 0) return 0; Scene_polylines_item* item = new Scene_polylines_item; item->polylines = polylines; item->setName(fileinfo.baseName()); @@ -146,7 +163,10 @@ Polyhedron_demo_polylines_io_plugin::load(QFileInfo fileinfo) { item->setProperty("polylines metadata", polylines_metadata); std::cerr << "Number of polylines in item: " << item->polylines.size() << std::endl; item->invalidateOpenGLBuffers(); - return item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<(item) != 0; } -bool Polyhedron_demo_polylines_io_plugin::save(const CGAL::Three::Scene_item* item, QFileInfo fileinfo) +bool Polyhedron_demo_polylines_io_plugin:: +save(QFileInfo fileinfo,QList& items) { + Scene_item* item = items.front(); const Scene_polylines_item* poly_item = qobject_cast(item); @@ -188,7 +210,10 @@ bool Polyhedron_demo_polylines_io_plugin::save(const CGAL::Three::Scene_item* it } out << std::endl; } - return (bool) out; + bool res = (bool) out; + if(res) + items.pop_front(); + return res; } void Polyhedron_demo_polylines_io_plugin::split() diff --git a/Polyhedron/demo/Polyhedron/Plugins/IO/STL_io_plugin.cpp b/Polyhedron/demo/Polyhedron/Plugins/IO/STL_io_plugin.cpp index 523698a72f7..475fc302197 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/IO/STL_io_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/IO/STL_io_plugin.cpp @@ -47,44 +47,51 @@ public: bool applicable(QAction*) const { return false;} QString nameFilters() const; QString name() const { return "stl_plugin"; } - bool canLoad() const; - CGAL::Three::Scene_item* load(QFileInfo fileinfo); + bool canLoad(QFileInfo fileinfo) const; + QList load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true); bool canSave(const CGAL::Three::Scene_item*); - bool save(const CGAL::Three::Scene_item*, QFileInfo fileinfo); + bool save(QFileInfo fileinfo,QList&); }; QString Polyhedron_demo_stl_plugin::nameFilters() const { return "STL files (*.stl)"; } -bool Polyhedron_demo_stl_plugin::canLoad() const { +bool Polyhedron_demo_stl_plugin::canLoad(QFileInfo) const { return true; } -CGAL::Three::Scene_item* -Polyhedron_demo_stl_plugin::load(QFileInfo fileinfo) { + +QList +Polyhedron_demo_stl_plugin:: +load(QFileInfo fileinfo, bool& ok, bool add_to_scene){ // Open file std::ifstream in(fileinfo.filePath().toUtf8(), std::ios::in | std::ios::binary); if(!in) { std::cerr << "Error! Cannot open file " << (const char*)fileinfo.filePath().toUtf8() << std::endl; - return NULL; + ok = false; + return QList(); } if(fileinfo.size() == 0) { CGAL::Three::Three::warning( tr("The file you are trying to load is empty.")); Scene_surface_mesh_item* item = new Scene_surface_mesh_item(); item->setName(fileinfo.completeBaseName()); - return item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()< > points; std::vector > triangles; if (!CGAL::read_STL(in, points, triangles)) { std::cerr << "Error: invalid STL file" << std::endl; - return NULL; + ok = false; + return QList(); } try{ @@ -98,7 +105,10 @@ Polyhedron_demo_stl_plugin::load(QFileInfo fileinfo) { else{ Scene_surface_mesh_item* item = new Scene_surface_mesh_item(SM); item->setName(fileinfo.completeBaseName()); - return item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<setName(fileinfo.completeBaseName()); item->load(points, triangles); - return item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<(item); } -bool Polyhedron_demo_stl_plugin::save(const CGAL::Three::Scene_item* item, QFileInfo fileinfo) +bool Polyhedron_demo_stl_plugin:: +save(QFileInfo fileinfo,QList& items) { + Scene_item* item = items.front(); const Scene_surface_mesh_item* sm_item = qobject_cast(item); @@ -144,6 +159,7 @@ bool Polyhedron_demo_stl_plugin::save(const CGAL::Three::Scene_item* item, QFile if (sm_item) { CGAL::write_STL(*sm_item->face_graph(), out); + items.pop_front(); return true; } return false; diff --git a/Polyhedron/demo/Polyhedron/Plugins/IO/Surf_io_plugin.cpp b/Polyhedron/demo/Polyhedron/Plugins/IO/Surf_io_plugin.cpp index dfac3829add..f97fed4afad 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/IO/Surf_io_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/IO/Surf_io_plugin.cpp @@ -42,19 +42,34 @@ public: bool applicable(QAction*) const { return false;} QString name() const { return "surf_io_plugin"; } QString nameFilters() const { return "Amira files (*.surf)"; } - bool canLoad() const{ return true; } + bool canLoad(QFileInfo) const{ return true; } template CGAL::Three::Scene_item* actual_load(QFileInfo fileinfo); - CGAL::Three::Scene_item* load(QFileInfo fileinfo); + QList load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true); bool canSave(const CGAL::Three::Scene_item*) { return false; } - bool save(const CGAL::Three::Scene_item*, QFileInfo) { return false; } + bool save(QFileInfo fileinfo,QList& ) { return false; } }; -CGAL::Three::Scene_item* Surf_io_plugin::load(QFileInfo fileinfo) +QList +Surf_io_plugin:: +load(QFileInfo fileinfo, bool& ok, bool add_to_scene) { - return actual_load(fileinfo); + Scene_item* item = + actual_load(fileinfo); + if(item) + { + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<(); + } } template< class FaceGraphItem> CGAL::Three::Scene_item* Surf_io_plugin::actual_load(QFileInfo fileinfo) diff --git a/Polyhedron/demo/Polyhedron/Plugins/IO/XYZ_io_plugin.cpp b/Polyhedron/demo/Polyhedron/Plugins/IO/XYZ_io_plugin.cpp index 7bed8af9e55..8ab464ed7c6 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/IO/XYZ_io_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/IO/XYZ_io_plugin.cpp @@ -21,26 +21,28 @@ public: QString name() const { return "xyz_plugin"; } QString nameFilters() const { return "XYZ as Point Set (*.xyz);;Point Set with Normal (*.pwn)"; } - bool canLoad() const; - CGAL::Three::Scene_item* load(QFileInfo fileinfo); + bool canLoad(QFileInfo) const; + QList load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true); bool canSave(const CGAL::Three::Scene_item*); - bool save(const CGAL::Three::Scene_item*, QFileInfo fileinfo); + bool save(QFileInfo fileinfo,QList&); }; -bool Polyhedron_demo_xyz_plugin::canLoad() const { +bool Polyhedron_demo_xyz_plugin::canLoad(QFileInfo) const { return true; } -CGAL::Three::Scene_item* -Polyhedron_demo_xyz_plugin::load(QFileInfo fileinfo) +QList +Polyhedron_demo_xyz_plugin:: +load(QFileInfo fileinfo, bool& ok, bool add_to_scene) { // Open file std::ifstream in(fileinfo.filePath().toUtf8().data()); if(!in) { std::cerr << "Error! Cannot open file " << fileinfo.filePath().toStdString() << std::endl; - return NULL; + ok = false; + return QList(); } @@ -50,19 +52,26 @@ Polyhedron_demo_xyz_plugin::load(QFileInfo fileinfo) Scene_points_with_normal_item* item = new Scene_points_with_normal_item(); item->setName(fileinfo.completeBaseName()); - return item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<setName(fileinfo.completeBaseName()); if(!point_set_item->read_xyz_point_set(in)) { delete point_set_item; - return NULL; + ok = false; + return QList(); } if(point_set_item->has_normals()) point_set_item->setRenderingMode(CGAL::Three::Three::defaultPointSetRenderingMode()); - return point_set_item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(point_set_item); + return QList()<(item); } -bool Polyhedron_demo_xyz_plugin::save(const CGAL::Three::Scene_item* item, QFileInfo fileinfo) +bool Polyhedron_demo_xyz_plugin:: +save(QFileInfo fileinfo,QList& items) { + Scene_item* item = items.front(); // Check extension (quietly) std::string extension = fileinfo.suffix().toUtf8().data(); if (extension != "xyz" && extension != "XYZ" && @@ -88,7 +99,10 @@ bool Polyhedron_demo_xyz_plugin::save(const CGAL::Three::Scene_item* item, QFile // Save point set as .xyz std::ofstream out(fileinfo.filePath().toUtf8().data()); out.precision (std::numeric_limits::digits10 + 2); - return point_set_item->write_xyz_point_set(out); + bool res = point_set_item->write_xyz_point_set(out); + if(res) + items.pop_front(); + return res; } #include "XYZ_io_plugin.moc" diff --git a/Polyhedron/demo/Polyhedron/Plugins/IO/lcc_io_plugin.cpp b/Polyhedron/demo/Polyhedron/Plugins/IO/lcc_io_plugin.cpp index 9bf6a73226c..2b4d0649093 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/IO/lcc_io_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/IO/lcc_io_plugin.cpp @@ -33,13 +33,14 @@ public: "3-map files (*.3map)"; } - bool canLoad() const { return true; } - CGAL::Three::Scene_item* load(QFileInfo fileinfo){ + bool canLoad(QFileInfo) const { return true; } + QList load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true){ // Open file std::ifstream ifs(fileinfo.filePath().toUtf8()); if(!ifs) { std::cerr << "Error! Cannot open file " << (const char*)fileinfo.filePath().toUtf8() << std::endl; - return nullptr; + ok = false; + return QList(); } Scene_lcc_item::LCC lcc; @@ -53,17 +54,21 @@ public: } if(!res) { - return nullptr; + ok = false; + return QList(); } Scene_lcc_item* new_item = new Scene_lcc_item(lcc); new_item->setName(fileinfo.fileName()); new_item->invalidateOpenGLBuffers(); - return new_item; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(new_item); + ok = true; + return QList()<& ){ return false; } diff --git a/Polyhedron/demo/Polyhedron/Plugins/Mesh_3/C3t3_io_plugin.cpp b/Polyhedron/demo/Polyhedron/Plugins/Mesh_3/C3t3_io_plugin.cpp index 68ccf77ea8e..93af632e9e8 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/Mesh_3/C3t3_io_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/Mesh_3/C3t3_io_plugin.cpp @@ -37,11 +37,11 @@ public: { return false; } - bool canLoad() const; - CGAL::Three::Scene_item* load(QFileInfo fileinfo); + bool canLoad(QFileInfo) const; + QList load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true); bool canSave(const CGAL::Three::Scene_item*); - bool save(const CGAL::Three::Scene_item*, QFileInfo fileinfo); + bool save(QFileInfo fileinfo,QList& ); private: bool try_load_other_binary_format(std::istream& in, C3t3& c3t3); @@ -50,21 +50,23 @@ private: }; -bool Polyhedron_demo_c3t3_binary_io_plugin::canLoad() const { +bool Polyhedron_demo_c3t3_binary_io_plugin::canLoad(QFileInfo) const { return true; } - -CGAL::Three::Scene_item* -Polyhedron_demo_c3t3_binary_io_plugin::load(QFileInfo fileinfo) { +QList +Polyhedron_demo_c3t3_binary_io_plugin::load( + QFileInfo fileinfo, bool& ok, bool add_to_scene) { // Open file + ok = true; std::ifstream in(fileinfo.filePath().toUtf8(), std::ios_base::in|std::ios_base::binary); if(!in) { std::cerr << "Error! Cannot open file " << (const char*)fileinfo.filePath().toUtf8() << std::endl; - return NULL; + ok = false; + return QList(); } Scene_c3t3_item* item = new Scene_c3t3_item(); @@ -72,7 +74,9 @@ Polyhedron_demo_c3t3_binary_io_plugin::load(QFileInfo fileinfo) { { CGAL::Three::Three::warning( tr("The file you are trying to load is empty.")); item->setName(fileinfo.completeBaseName()); - return item; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<< item; } if(fileinfo.suffix().toLower() == "cgal") { @@ -80,7 +84,9 @@ Polyhedron_demo_c3t3_binary_io_plugin::load(QFileInfo fileinfo) { if(item->load_binary(in)) { - return item; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList() << item; } item->c3t3().clear(); @@ -89,7 +95,9 @@ Polyhedron_demo_c3t3_binary_io_plugin::load(QFileInfo fileinfo) { item->c3t3_changed(); item->changed(); item->resetCutPlane(); - return item; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<< item; } item->c3t3().clear(); @@ -98,7 +106,9 @@ Polyhedron_demo_c3t3_binary_io_plugin::load(QFileInfo fileinfo) { item->c3t3_changed(); item->changed(); item->resetCutPlane(); - return item; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<c3t3_changed(); item->resetCutPlane(); - return item; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<c3t3().triangulation().number_of_finite_cells() == 0) { @@ -168,7 +180,8 @@ Polyhedron_demo_c3t3_binary_io_plugin::load(QFileInfo fileinfo) { // if all loading failed... delete item; - return NULL; + ok = false; + return QList(); } bool Polyhedron_demo_c3t3_binary_io_plugin::canSave(const CGAL::Three::Scene_item* item) @@ -179,51 +192,63 @@ bool Polyhedron_demo_c3t3_binary_io_plugin::canSave(const CGAL::Three::Scene_ite bool Polyhedron_demo_c3t3_binary_io_plugin:: -save(const CGAL::Three::Scene_item* item, QFileInfo fileinfo) +save(QFileInfo fileinfo, QList &items) { - const Scene_c3t3_item* c3t3_item = qobject_cast(item); - if ( NULL == c3t3_item ) - { - return false; - } + Scene_item* item = items.front(); + const Scene_c3t3_item* c3t3_item = qobject_cast(item); + if ( NULL == c3t3_item ) + { + return false; + } - QString path = fileinfo.absoluteFilePath(); + QString path = fileinfo.absoluteFilePath(); - if(path.endsWith(".cgal")) - { + if(path.endsWith(".cgal")) + { std::ofstream out(fileinfo.filePath().toUtf8(), - std::ios_base::out|std::ios_base::binary); + std::ios_base::out|std::ios_base::binary); - return out && c3t3_item->save_binary(out); - } - - else if (fileinfo.suffix() == "mesh") - { - std::ofstream medit_file (qPrintable(path)); - c3t3_item->c3t3().output_to_medit(medit_file,true,true); - return true; - } - else if (fileinfo.suffix() == "ma") - { - std::ofstream maya_file (qPrintable(path)); - c3t3_item->c3t3().output_to_maya( - maya_file,true); - return true; - } - else if (fileinfo.suffix() == "am") - { - std::ofstream avizo_file (qPrintable(path)); - CGAL::output_to_avizo(avizo_file, c3t3_item->c3t3()); - return true; - } - else if (fileinfo.suffix() == "off") - { - std::ofstream off_file(qPrintable(path)); - c3t3_item->c3t3().output_facets_in_complex_to_off(off_file); - return true; - } + bool ok = out && c3t3_item->save_binary(out); + if(!ok) + return false; else - return false; + { + items.pop_front(); + return true; + } + } + + else if (fileinfo.suffix() == "mesh") + { + std::ofstream medit_file (qPrintable(path)); + c3t3_item->c3t3().output_to_medit(medit_file,true,true); + items.pop_front(); + return true; + } + else if (fileinfo.suffix() == "ma") + { + std::ofstream maya_file (qPrintable(path)); + c3t3_item->c3t3().output_to_maya( + maya_file,true); + items.pop_front(); + return true; + } + else if (fileinfo.suffix() == "am") + { + std::ofstream avizo_file (qPrintable(path)); + CGAL::output_to_avizo(avizo_file, c3t3_item->c3t3()); + items.pop_front(); + return true; + } + else if (fileinfo.suffix() == "off") + { + std::ofstream off_file(qPrintable(path)); + c3t3_item->c3t3().output_facets_in_complex_to_off(off_file); + items.pop_front(); + return true; + } + else + return false; } struct Fake_mesh_domain { diff --git a/Polyhedron/demo/Polyhedron/Plugins/Mesh_3/Io_image_plugin.cpp b/Polyhedron/demo/Polyhedron/Plugins/Mesh_3/Io_image_plugin.cpp index 7e646bf1029..69e31f785fa 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/Mesh_3/Io_image_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/Mesh_3/Io_image_plugin.cpp @@ -282,15 +282,18 @@ public: Io_image_plugin() : planeSwitch(NULL) {} QString nameFilters() const; - bool canLoad() const; - CGAL::Three::Scene_item* load(QFileInfo fileinfo); + bool canLoad(QFileInfo) const; + QList load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true); bool canSave(const CGAL::Three::Scene_item*); - bool save(const CGAL::Three::Scene_item* item, QFileInfo fi) { + bool save(QFileInfo fileinfo, QList& items ) { + Scene_item* item = items.front(); const Scene_image_item* im_item = qobject_cast(item); point_image p_im = *im_item->image()->image(); - return _writeImage(&p_im, fi.filePath().toUtf8()) == 0; + bool ok = _writeImage(&p_im, fileinfo.filePath().toUtf8()) == 0; + items.pop_front(); + return ok; } QString name() const { return "segmented images"; } @@ -965,7 +968,7 @@ QString Io_image_plugin::nameFilters() const { } -bool Io_image_plugin::canLoad() const { +bool Io_image_plugin::canLoad(QFileInfo) const { return true; } @@ -987,8 +990,11 @@ void convert(Image* image) image->image()->wdim = 4; image->image()->wordKind = WK_FLOAT; } -CGAL::Three::Scene_item* -Io_image_plugin::load(QFileInfo fileinfo) { + +QList +Io_image_plugin::load(QFileInfo fileinfo, bool& ok, bool add_to_scene) +{ + ok = true; QApplication::restoreOverrideCursor(); Image* image = new Image; if(fileinfo.suffix() != "H" && fileinfo.suffix() != "HH" && @@ -1075,8 +1081,9 @@ Io_image_plugin::load(QFileInfo fileinfo) { success = false; } if(!success){ + ok = false; delete image; - return NULL; + return QList(); } } //read a sep file @@ -1117,7 +1124,8 @@ Io_image_plugin::load(QFileInfo fileinfo) { if(return_code != QDialog::Accepted) { delete image; - return NULL; + ok = false; + return QList(); } // Get selected precision @@ -1145,7 +1153,9 @@ Io_image_plugin::load(QFileInfo fileinfo) { else image_item = new Scene_image_item(image,voxel_scale, false); image_item->setName(fileinfo.baseName()); - return image_item; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(image_item); + return QList() << image_item; } bool Io_image_plugin::canSave(const CGAL::Three::Scene_item* item) diff --git a/Polyhedron/demo/Polyhedron/Plugins/PMP/Selection_plugin.cpp b/Polyhedron/demo/Polyhedron/Plugins/PMP/Selection_plugin.cpp index 7534498a7af..cd8512993f0 100644 --- a/Polyhedron/demo/Polyhedron/Plugins/PMP/Selection_plugin.cpp +++ b/Polyhedron/demo/Polyhedron/Plugins/PMP/Selection_plugin.cpp @@ -78,7 +78,7 @@ public: QString nameFilters() const { return "Selection files(*.selection.txt)"; } QString name() const { return "selection_sm_plugin"; } - bool canLoad() const { + bool canLoad(QFileInfo) const { Scene_item * item = CGAL::Three::Three::scene()->item( CGAL::Three::Three::scene()->mainSelectionIndex()); Scene_facegraph_item* fg_item = qobject_cast(item); @@ -91,26 +91,38 @@ public: return false; } - CGAL::Three::Scene_item* load(QFileInfo fileinfo) { - if(fileinfo.suffix().toLower() != "txt") return 0; + QList load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true) { + if(fileinfo.suffix().toLower() != "txt") + { + ok = false; + return QList(); + } // There will be no actual loading at this step. Scene_polyhedron_selection_item* item = new Scene_polyhedron_selection_item(); if(!item->load(fileinfo.filePath().toStdString())) { delete item; - return NULL; + ok = false; + return QList(); } item->setName(fileinfo.baseName()); - return item; + ok = true; + if(add_to_scene) + CGAL::Three::Three::scene()->addItem(item); + return QList()<(scene_item); } - bool save(const CGAL::Three::Scene_item* scene_item, QFileInfo fileinfo) { + bool save(QFileInfo fileinfo,QList& items) { + Scene_item* scene_item = items.front(); const Scene_polyhedron_selection_item* item = qobject_cast(scene_item); if(item == NULL) { return false; } - return item->save(fileinfo.filePath().toStdString()); + bool res = item->save(fileinfo.filePath().toStdString()); + if(res) + items.pop_front(); + return res; } bool applicable(QAction*) const { diff --git a/Three/include/CGAL/Three/Polyhedron_demo_io_plugin_interface.h b/Three/include/CGAL/Three/Polyhedron_demo_io_plugin_interface.h index 40210f4c6d9..8361b1e5384 100644 --- a/Three/include/CGAL/Three/Polyhedron_demo_io_plugin_interface.h +++ b/Three/include/CGAL/Three/Polyhedron_demo_io_plugin_interface.h @@ -58,17 +58,23 @@ public: //! Specifies if the io_plugin is able to load an item or not. //! This must be overriden. - virtual bool canLoad() const = 0; - //! Loads an item from a file. + virtual bool canLoad(QFileInfo fileinfo) const = 0; + //! Loads one or more item(s) from a file. `ok` is `true` if the loading + //! was successful, `false` otherwise. + //! New items will be added to the scene if `add_to_scene` is `true`. + //! You don't want that when you reload an item, for example, + //! as it will be added at some other point of the process. //! This must be overriden. - virtual Scene_item* load(QFileInfo fileinfo) = 0; + virtual QList load(QFileInfo fileinfo, bool& ok, bool add_to_scene=true) = 0; //!Specifies if the io_plugin can save the item or not. //!This must be overriden. virtual bool canSave(const Scene_item*) = 0; - //!Saves the item in the file corresponding to the path + //!Saves one or more items in the file corresponding to the path //!contained in fileinfo. Returns false if error. //! This must be overriden. - virtual bool save(const Scene_item*, QFileInfo fileinfo) = 0; + //! @attention When a file is successfully saved, it must be removed from the + //! list. + virtual bool save(QFileInfo fileinfo,QList& ) = 0; //! If this returns `true`, then the loader will be chosen as default in the //! list of available loaders when saving a file, which means it will be the diff --git a/Three/include/CGAL/Three/Scene_3mf_item_interface.h b/Three/include/CGAL/Three/Scene_3mf_item_interface.h new file mode 100644 index 00000000000..15b25cbfdbf --- /dev/null +++ b/Three/include/CGAL/Three/Scene_3mf_item_interface.h @@ -0,0 +1,4 @@ +#ifndef SCENE_3MF_ITEM_INTERFACE_H +#define SCENE_3MF_ITEM_INTERFACE_H + +#endif // SCENE_3MF_ITEM_INTERFACE_H