Debian Codegraph @

avogadro/qtplugins/3dmol/3dmoldialog.cpp

raw
1 /******************************************************************************
2
3 This source file is part of the Avogadro project.
4
5 Copyright 2016 Barry E Moore II
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 "3dmoldialog.h"
18 #include "ui_3dmoldialog.h"
19
20 #include <avogadro/core/elements.h>
21 #include <avogadro/qtgui/molecule.h>
22
23 #include <avogadro/io/fileformatmanager.h>
24
25 #include <string>
26
27 #include <QtGui/QClipboard>
28
29 using Avogadro::QtGui::Molecule;
30
31 namespace Avogadro::QtPlugins {
32
33 ThreeDMolDialog::ThreeDMolDialog(QtGui::Molecule* mol, QWidget* parent_)
34 : QDialog(parent_), m_molecule(nullptr), m_ui(new Ui::ThreeDMolDialog)
35 {
36 m_ui->setupUi(this);
37 setMolecule(mol);
38 }
39
40 ThreeDMolDialog::~ThreeDMolDialog()
41 {
42 delete m_ui;
43 }
44
45 void ThreeDMolDialog::setMolecule(QtGui::Molecule* mol)
46 {
47 if (mol == m_molecule)
48 return;
49
50 if (m_molecule)
51 m_molecule->disconnect(this);
52
53 m_molecule = mol;
54
55 if (!m_molecule)
56 return;
57
58 connect(m_molecule, SIGNAL(changed(unsigned int)), SLOT(updateLabels()));
59 connect(m_molecule, SIGNAL(destroyed()), SLOT(moleculeDestroyed()));
60 connect(m_ui->closeButton, SIGNAL(clicked()), SLOT(close()));
61 connect(m_ui->copyButton, SIGNAL(clicked()), SLOT(copyToClipboard()));
62
63 updateLabels();
64 }
65
66 void ThreeDMolDialog::updateLabels()
67 {
68 if (m_molecule) {
69 updateTextBrowser();
70 } else {
71 m_ui->plainTextEdit->clear();
72 }
73 }
74
75 void ThreeDMolDialog::updateTextBrowser()
76 {
77 QString text =
78 "<script src='http://3Dmol.csb.pitt.edu/build/3Dmol-min.js'></script>\n";
79 // MDL representation
80 std::string mol;
81 bool writeSDF =
82 Io::FileFormatManager::instance().writeString(*m_molecule, mol, "sdf");
83 text.append("<div style='height: 400px; width: 400px; position: relative;' "
84 "class='viewer_3Dmoljs' data-element='molecule'");
85 if (writeSDF)
86 text.append(" data-type='sdf' data-backgroundcolor='0xffffff' "
87 "data-style='stick'></div>\n");
88 else
89 text.append(" data-type='xyz' data-backgroundcolor='0xffffff' "
90 "data-style='stick'></div>\n");
91
92 text.append("<textarea id='molecule' style='display: none;'>");
93 if (writeSDF) {
94 text.append(mol.c_str());
95 } else {
96 // couldn't write MDL for some reason, write XYZ
97 size_t numAtoms = m_molecule->atomCount();
98 text.append(QString::number(numAtoms) + "\n");
99 text.append("Generated by Avogadro.\n");
100
101 for (size_t i = 0; i < numAtoms; ++i) {
102 Core::Atom atom = m_molecule->atom(i);
103 QString line("%1 %2 %3 %4\n");
104 text.append(line.arg(Core::Elements::symbol(atom.atomicNumber()))
105 .arg(atom.position3d().x())
106 .arg(atom.position3d().y())
107 .arg(atom.position3d().z()));
108 } // end for(atoms)
109 }
110
111 text.append("</textarea>");
112 m_ui->plainTextEdit->setPlainText(text);
113 }
114
115 void ThreeDMolDialog::moleculeDestroyed()
116 {
117 m_molecule = nullptr;
118 updateLabels();
119 }
120
121 void ThreeDMolDialog::copyToClipboard()
122 {
123 QApplication::clipboard()->setText(m_ui->plainTextEdit->toPlainText());
124 }
125
126 } // namespace Avogadro::QtPlugins