1 /******************************************************************************
2 
3   This source file is part of the Avogadro project.
4 
5   Copyright 2016 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 "importcrystaldialog.h"
18 #include "ui_importcrystaldialog.h"
19 
20 #include <avogadro/core/molecule.h>
21 #include <avogadro/io/fileformatmanager.h>
22 
23 #include <QDebug>
24 
25 #include <QtWidgets/QApplication>
26 #include <QtWidgets/QMessageBox>
27 
28 #include <QtGui/QClipboard>
29 
30 using std::vector;
31 using std::string;
32 
33 namespace Avogadro {
34 namespace QtPlugins {
35 
ImportCrystalDialog(QWidget * p)36 ImportCrystalDialog::ImportCrystalDialog(QWidget* p)
37   : QDialog(p), m_ui(new Ui::ImportCrystalDialog)
38 {
39   m_ui->setupUi(this);
40 }
41 
~ImportCrystalDialog()42 ImportCrystalDialog::~ImportCrystalDialog()
43 {
44   delete m_ui;
45 }
46 
importCrystalClipboard(Avogadro::Core::Molecule & mol)47 bool ImportCrystalDialog::importCrystalClipboard(Avogadro::Core::Molecule& mol)
48 {
49   QString text = QApplication::clipboard()->text();
50   m_ui->edit_text->setText(text);
51   // If the user rejected, just return false
52   if (this->exec() == QDialog::Rejected)
53     return false;
54 
55   // Use POSCAR format by default. If the extension was set, use that instead
56   std::string ext = m_ui->edit_extension->text().toStdString();
57   if (ext.empty())
58     ext = "POSCAR";
59 
60   // Update the text
61   text = m_ui->edit_text->toPlainText();
62   std::stringstream s(text.toStdString());
63 
64   if (Io::FileFormatManager::instance().readString(mol, s.str(), ext))
65     return true;
66 
67   // Print out the error messages from the read if we failed
68   if (!Io::FileFormatManager::instance().error().empty()) {
69     qDebug() << "FileFormatManager error message:"
70              << QString::fromStdString(
71                   Io::FileFormatManager::instance().error());
72   }
73 
74   displayInvalidFormatMessage();
75   return false;
76 }
77 
displayInvalidFormatMessage()78 void ImportCrystalDialog::displayInvalidFormatMessage()
79 {
80   QMessageBox::critical(
81     this, tr("Cannot Parse Text"),
82     tr("Failed to read the data with the supplied format."));
83   reject();
84   close();
85 }
86 
87 } // namespace QtPlugins
88 } // namespace Avogadro
89