1 /******************************************************************************
2 
3   This source file is part of the Avogadro project.
4 
5   Copyright 2014 Kitware, Inc.
6 
7   This source code is released under the New BSD License, (the "License").
8 
9   Unless required by applicable law or agreed to in writing, software
10   distributed under the License is distributed on an "AS IS" BASIS,
11   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   See the License for the specific language governing permissions and
13   limitations under the License.
14 
15 ******************************************************************************/
16 
17 #include "vrml.h"
18 
19 #include <avogadro/qtgui/molecule.h>
20 #include <avogadro/rendering/camera.h>
21 #include <avogadro/rendering/scene.h>
22 #include <avogadro/rendering/vrmlvisitor.h>
23 
24 #include <QtCore/QTextStream>
25 #include <QtGui/QClipboard>
26 #include <QtGui/QIcon>
27 #include <QtGui/QKeySequence>
28 #include <QtWidgets/QAction>
29 #include <QtWidgets/QApplication>
30 #include <QtWidgets/QFileDialog>
31 #include <QtWidgets/QMessageBox>
32 
33 #include <string>
34 #include <vector>
35 
36 namespace Avogadro {
37 namespace QtPlugins {
38 
VRML(QObject * p)39 VRML::VRML(QObject* p)
40   : Avogadro::QtGui::ExtensionPlugin(p), m_molecule(nullptr), m_scene(nullptr),
41     m_camera(nullptr), m_action(new QAction(tr("VRML Render"), this))
42 {
43   connect(m_action, SIGNAL(triggered()), SLOT(render()));
44 }
45 
~VRML()46 VRML::~VRML()
47 {
48 }
49 
actions() const50 QList<QAction*> VRML::actions() const
51 {
52   QList<QAction*> result;
53   return result << m_action;
54 }
55 
menuPath(QAction *) const56 QStringList VRML::menuPath(QAction*) const
57 {
58   return QStringList() << tr("&File") << tr("&Export");
59 }
60 
setMolecule(QtGui::Molecule * mol)61 void VRML::setMolecule(QtGui::Molecule* mol)
62 {
63   m_molecule = mol;
64 }
65 
setScene(Rendering::Scene * scene)66 void VRML::setScene(Rendering::Scene* scene)
67 {
68   m_scene = scene;
69 }
70 
setCamera(Rendering::Camera * camera)71 void VRML::setCamera(Rendering::Camera* camera)
72 {
73   m_camera = camera;
74 }
75 
render()76 void VRML::render()
77 {
78   if (!m_scene || !m_camera)
79     return;
80 
81   QString filename = QFileDialog::getSaveFileName(
82     qobject_cast<QWidget*>(parent()), tr("Save File"), QDir::homePath(),
83     tr("VRML (*.wrl);;Text file (*.txt)"));
84   QFile file(filename);
85   if (!file.open(QIODevice::WriteOnly))
86     return;
87 
88   QTextStream fileStream(&file);
89   Rendering::VRMLVisitor visitor(*m_camera);
90   visitor.begin();
91   m_scene->rootNode().accept(visitor);
92   fileStream << visitor.end().c_str();
93 
94   file.close();
95 }
96 
97 } // namespace QtPlugins
98 } // namespace Avogadro
99