Commit 439301a8 authored by MARCHAL Damien's avatar MARCHAL Damien

Merge branch 'feature_save_load_views' into 'master'

[SofaViewer] Add options for saving/restoring views at startup

See merge request !20
parents e338711d 9c1ab61c
......@@ -47,6 +47,7 @@ along with sofaqtquick. If not, see <http://www.gnu.org/licenses/>.
#include <QThread>
#include <QString>
#include <QDir>
#include <QSettings>
#include <qqml.h>
#include <qmath.h>
#include <limits>
......@@ -606,6 +607,74 @@ void SofaViewer::saveScreenshotWithResolution(const QString& path, int width, in
window()->update();
}
void SofaViewer::saveCameraToFile(int uiId) const
{
//if (!this->camera)
// return;
QUrl source = mySofaScene->source();
QString finalFilename = source.path();
if (source.isLocalFile())
finalFilename = source.toLocalFile();
QString viewBasename = source.fileName() + ".qtquickview";
QString viewPath = source.path();
QString viewFilename = viewPath + ".qtquickview";
QFileInfo fileInfo(viewFilename);
QSettings viewSettings(viewFilename, QSettings::IniFormat);
QString uiIdKey = QString(uiId) + "/";
QVariant eyeVariant, targetVariant, upVariant;
eyeVariant.setValue(this->camera()->eye());
targetVariant.setValue(this->camera()->target());
upVariant.setValue(this->camera()->up());
viewSettings.setValue(uiIdKey + "eye", eyeVariant);
viewSettings.setValue(uiIdKey + "target", targetVariant);
viewSettings.setValue(uiIdKey + "up", upVariant);
viewSettings.setValue(uiIdKey + "zFar", this->camera()->zFar());
viewSettings.setValue(uiIdKey + "zNear", this->camera()->zNear());
viewSettings.setValue(uiIdKey + "orthographic", this->camera()->orthographic());
//file update/creation is done when QSettings is destroyed apparently
}
void SofaViewer::loadCameraFromFile(int uiId)
{
QUrl source = mySofaScene->source();
QString finalFilename = source.path();
if (source.isLocalFile())
finalFilename = source.toLocalFile();
QString viewBasename = source.fileName() + ".qtquickview";
QString viewPath = source.path();
QString viewFilename = viewPath + ".qtquickview";
QFileInfo fileInfo(viewFilename);
if (!fileInfo.exists())
return; //TODO: inform the user somehow
QSettings viewSettings(viewFilename, QSettings::IniFormat);
QString uiIdKey = QString(uiId) + "/";
double zFar = viewSettings.value(uiIdKey + "zFar").toDouble();
double zNear = viewSettings.value(uiIdKey + "zNear").toDouble();
bool orthographic = viewSettings.value(uiIdKey + "orthographic").toBool();
QVariant eyeVariant = viewSettings.value(uiIdKey + "eye");
QVariant targetVariant = viewSettings.value(uiIdKey + "target");
QVariant upVariant = viewSettings.value(uiIdKey + "up");
QVector3D eye = eyeVariant.value<QVector3D>();
QVector3D target = targetVariant.value<QVector3D>();
QVector3D up = upVariant.value<QVector3D>();
this->camera()->lookAt(eye, target, up);
this->camera()->setZNear(zNear);
this->camera()->setZFar(zFar);
this->camera()->setOrthographic(orthographic);
}
QSize SofaViewer::nativeSize() const
{
return nativeRect().size();
......
......@@ -162,8 +162,11 @@ public:
Q_INVOKABLE void saveScreenshot(const QString& path);
Q_INVOKABLE void saveScreenshotWithResolution(const QString& path, int width, int height);
QOpenGLFramebufferObject* getFBO() const;
Q_INVOKABLE void saveCameraToFile(int uiId) const;
Q_INVOKABLE void loadCameraFromFile(int uiId);
QOpenGLFramebufferObject* getFBO() const;
signals:
void sofaSceneChanged(sofa::qtquick::SofaScene* newScene);
void rootsChanged(QList<sofa::qtquick::SofaComponent> newRoots);
......
......@@ -49,6 +49,12 @@ SofaViewer {
source: "qrc:/icon/alphaBackground.png"
}
property int uiId: 0
property int previousUiId: uiId
onUiIdChanged: {
SofaApplication.uiSettings.replace(previousUiId, uiId);
}
implicitWidth: 800
implicitHeight: 600
......@@ -60,6 +66,8 @@ SofaViewer {
if(root.sofaScene && root.sofaScene.ready)
recreateCamera();
loadCameraFromFile((Number(uiId)));
}
Component.onDestruction: {
......@@ -1054,6 +1062,32 @@ SofaViewer {
columns: 2
rowSpacing: 0
columnSpacing: 0
Button {
Layout.fillWidth: true
Layout.preferredWidth: parent.width
text: "Save"
onClicked: if(camera) root.saveCameraToFile(Number(uiId))
ToolTip {
anchors.fill: parent
description: "Save the current view to a sidecar file"
}
}
Button {
Layout.fillWidth: true
Layout.preferredWidth: parent.width
text: "Reload"
onClicked: if(camera) root.loadCameraFromFile(Number(uiId))
ToolTip {
anchors.fill: parent
description: "Reload the view from data contained in a sidecar file (if present)"
}
}
Button {
Layout.fillWidth: true
......@@ -1153,7 +1187,8 @@ SofaViewer {
anchors.fill: parent
description: "Isometric View"
}
}
}
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment