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