Skip to content

Commit

Permalink
Merge pull request #3394 from maxGimeno/Demo-Loading_scenes-GF
Browse files Browse the repository at this point in the history
Polyhedron demo: Add an action to save a scene
  • Loading branch information
lrineau committed Dec 10, 2018
2 parents be12901 + 4275495 commit 4320a15
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 9 deletions.
88 changes: 83 additions & 5 deletions Polyhedron/demo/Polyhedron/MainWindow.cpp
Expand Up @@ -12,7 +12,6 @@
#include <QtDebug>
#include <QFileDialog>
#include <QFileInfo>
#include <QSettings>
#include <QHeaderView>
#include <QMenu>
#include <QMenuBar>
Expand All @@ -38,6 +37,7 @@
#include <QDockWidget>
#include <QSpinBox>
#include <stdexcept>
#include <fstream>
#include <QTime>
#include <QWidgetAction>

Expand Down Expand Up @@ -1627,7 +1627,6 @@ void MainWindow::updateDisplayInfo() {

void MainWindow::readSettings()
{
QSettings settings;
viewer->setAntiAliasing(settings.value("antialiasing", false).toBool());
viewer->setFastDrawing(settings.value("quick_camera_mode", true).toBool());
scene->enableVisibilityRecentering(settings.value("offset_update", true).toBool());
Expand All @@ -1650,7 +1649,6 @@ void MainWindow::writeSettings()
{
this->writeState("MainWindow");
{
QSettings settings;
//setting plugin blacklist
QStringList blacklist;
Q_FOREACH(QString name,plugin_blacklist){ blacklist << name; }
Expand Down Expand Up @@ -2006,7 +2004,6 @@ void MainWindow::on_actionPreferences_triggered()
{
QDialog dialog(this);
Ui::PreferencesDialog prefdiag;
QSettings settings;
prefdiag.setupUi(&dialog);

float lineWidth[2];
Expand Down Expand Up @@ -2598,6 +2595,88 @@ void MainWindow::propagate_action()
}
}
}

void MainWindow::on_actionSa_ve_Scene_as_Script_triggered()
{
QString filename =
QFileDialog::getSaveFileName(this,
"Save the Scene as a Script File",
last_saved_dir,
"Qt Script files (*.js)");
std::ofstream os(filename.toUtf8());
if(!os)
return;
std::vector<QString> names;
std::vector<QString> loaders;
std::vector<QColor> colors;
std::vector<int> rendering_modes;
QStringList not_saved;
for(int i = 0; i < scene->numberOfEntries(); ++i)
{
Scene_item* item = scene->item(i);
QString loader = item->property("loader_name").toString();
QString source = item->property("source filename").toString();
if(loader.isEmpty())
{
not_saved.push_back(item->name());
continue;
}
names.push_back(source);
loaders.push_back(loader);
colors.push_back(item->color());
rendering_modes.push_back(item->renderingMode());
}
//path
os << "var camera = \""<<viewer->dumpCameraCoordinates().toStdString()<<"\";\n";
os << "var items = [";
for(std::size_t i = 0; i< names.size() -1; ++i)
{
os << "\'" << names[i].toStdString() << "\', ";
}
os<<"\'"<<names.back().toStdString()<<"\'];\n";

//plugin
os << "var loaders = [";
for(std::size_t i = 0; i< names.size() -1; ++i)
{
os << "\'" << loaders[i].toStdString() << "\', ";
}
os<<"\'"<<loaders.back().toStdString()<<"\'];\n";

//color
os << "var colors = [";
for(std::size_t i = 0; i< names.size() -1; ++i)
{
os << "[" << colors[i].red() <<", "<< colors[i].green() <<", "<< colors[i].blue() <<"], ";
}
os<<"[" << colors.back().red() <<", "<< colors.back().green() <<", "<< colors.back().blue() <<"]];\n";

//rendering mode
os << "var rendering_modes = [";
for(std::size_t i = 0; i< names.size() -1; ++i)
{
os << rendering_modes[i] << ", ";
}
os << rendering_modes.back()<<"];\n";
os <<"var initial_scene_size = scene.numberOfEntries;\n";
os << "items.forEach(function(item, index, array){\n";
os << " main_window.open(item, loaders[index]);\n";
os << " var it = scene.item(initial_scene_size+index);\n";
os << " var r = colors[index][0];\n";
os << " var g = colors[index][1];\n";
os << " var b = colors[index][2];\n";
os << " it.setRgbColor(r,g,b);\n";
os << " it.setRenderingMode(rendering_modes[index]);\n";
os << "});\n";
os << "viewer.moveCameraToCoordinates(camera, 0.05);\n";
os.close();
if(!not_saved.empty())
QMessageBox::warning(this,
"Items Not Saved",
QString("The following items could not be saved: %1").arg(
not_saved.join(", ")));
}

void MainWindow::setTransparencyPasses(int val)
{
viewer->setTotalPass(val);
Expand Down Expand Up @@ -2633,7 +2712,6 @@ void MainWindow::setDefaultSaveDir()
QString dirpath = QFileDialog::getExistingDirectory(this, "Set Default Save as Directory", def_save_dir);
if(!dirpath.isEmpty())
def_save_dir = dirpath;
QSettings settings;
settings.setValue("default_saveas_dir", def_save_dir);
}

Expand Down
1 change: 1 addition & 0 deletions Polyhedron/demo/Polyhedron/MainWindow.h
Expand Up @@ -440,6 +440,7 @@ protected Q_SLOTS:
const QString & fileName = QString());
#endif
public Q_SLOTS:
void on_actionSa_ve_Scene_as_Script_triggered();
void toggleFullScreen();
void setDefaultSaveDir();
void invalidate_bbox(bool do_recenter);
Expand Down
6 changes: 6 additions & 0 deletions Polyhedron/demo/Polyhedron/MainWindow.ui
Expand Up @@ -49,6 +49,7 @@
<addaction name="actionEraseAll"/>
<addaction name="actionDuplicate"/>
<addaction name="actionSaveAs"/>
<addaction name="actionSa_ve_Scene_as_Script"/>
<addaction name="actionSaveSnapshot"/>
<addaction name="separator"/>
<addaction name="actionLoadScript"/>
Expand Down Expand Up @@ -492,6 +493,11 @@
<string>Set Different Colors for Selected Items</string>
</property>
</action>
<action name="actionSa_ve_Scene_as_Script">
<property name="text">
<string>Sa&amp;ve the Scene as a Script File...</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand Down
Expand Up @@ -125,9 +125,9 @@ public: typedef std::list<Polyline_data> Polyline_data_list;
else { it->polyline->setWidth(3); }

if(selected_holes.find(it) != selected_holes.end())
{ it->polyline->setRbgColor(255, 0, 0); }
{ it->polyline->setRgbColor(255, 0, 0); }
else
{ it->polyline->setRbgColor(0, 0, 255); }
{ it->polyline->setRgbColor(0, 0, 255); }

it->polyline->drawEdges(viewer);
}
Expand Down
Expand Up @@ -346,7 +346,7 @@ class Polyhedron_demo_point_set_shape_detection_plugin :
g = static_cast<unsigned char>(64 + rand.get_int(0, 192));
b = static_cast<unsigned char>(64 + rand.get_int(0, 192));

point_item->setRbgColor(r, g, b);
point_item->setRgbColor(r, g, b);

std::size_t nb_colored_pts = 0;
if (dialog.generate_colored_point_set())
Expand Down
3 changes: 2 additions & 1 deletion Three/include/CGAL/Three/Scene_item.h
Expand Up @@ -316,7 +316,7 @@ public Q_SLOTS:
virtual void setColor(QColor c) { color_ = c;}
//!Setter for the RGB color of the item. Calls setColor(QColor).
//!@see setColor(QColor c)
void setRbgColor(int r, int g, int b) { setColor(QColor(r, g, b)); }
void setRgbColor(int r, int g, int b) { setColor(QColor(r, g, b)); }
//!Sets the name of the item.
virtual void setName(QString n) { name_ = n; }
//!Sets the visibility of the item.
Expand All @@ -325,6 +325,7 @@ public Q_SLOTS:
//!This function is called by `Scene::changeGroup` and should not be
//!called manually.
virtual void moveToGroup(Scene_group_item* group);
void setRenderingMode(int m) { setRenderingMode((RenderingMode)m);}
//!Sets the rendering mode of the item.
//!@see RenderingMode
virtual void setRenderingMode(RenderingMode m) {
Expand Down

0 comments on commit 4320a15

Please sign in to comment.