| 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 "insertfragment.h" |
| 7 |
|
| 8 |
#include <avogadro/qtgui/fileformatdialog.h> |
| 9 |
#include <avogadro/qtgui/insertfragmentdialog.h> |
| 10 |
#include <avogadro/qtgui/molecule.h> |
| 11 |
#include <avogadro/qtgui/rwmolecule.h> |
| 12 |
|
| 13 |
#include <avogadro/io/fileformat.h> |
| 14 |
#include <avogadro/io/fileformatmanager.h> |
| 15 |
|
| 16 |
#include <QtCore/QFileInfo> |
| 17 |
|
| 18 |
#include <QAction> |
| 19 |
#include <QtWidgets/QMessageBox> |
| 20 |
#include <QtWidgets/QProgressDialog> |
| 21 |
|
| 22 |
using Avogadro::Io::FileFormatManager; |
| 23 |
using Avogadro::QtGui::InsertFragmentDialog; |
| 24 |
using Avogadro::QtGui::Molecule; |
| 25 |
|
| 26 |
namespace Avogadro::QtPlugins { |
| 27 |
|
| 28 |
InsertFragment::InsertFragment(QObject* parent_) |
| 29 |
: Avogadro::QtGui::ExtensionPlugin(parent_), m_moleculeDialog(nullptr), |
| 30 |
m_crystalDialog(nullptr), m_molecule(nullptr), m_reader(nullptr) |
| 31 |
{ |
| 32 |
auto* action = new QAction(tr("Molecule…"), this); |
| 33 |
action->setProperty("menu priority", 890); |
| 34 |
action->setData("molecules"); // will also work for crystals |
| 35 |
connect(action, SIGNAL(triggered()), SLOT(showDialog())); |
| 36 |
m_actions.append(action); |
| 37 |
|
| 38 |
action = new QAction(tr("Crystal…"), this); |
| 39 |
action->setData("crystals"); // will also work for crystals |
| 40 |
action->setProperty("menu priority", 170); |
| 41 |
connect(action, SIGNAL(triggered()), SLOT(showDialog())); |
| 42 |
m_actions.append(action); |
| 43 |
} |
| 44 |
|
| 45 |
InsertFragment::~InsertFragment() |
| 46 |
{ |
| 47 |
delete m_reader; |
| 48 |
} |
| 49 |
|
| 50 |
QList<QAction*> InsertFragment::actions() const |
| 51 |
{ |
| 52 |
return m_actions; |
| 53 |
} |
| 54 |
|
| 55 |
QStringList InsertFragment::menuPath(QAction* action) const |
| 56 |
{ |
| 57 |
if (action->data() == "crystals") |
| 58 |
return QStringList() << tr("&File") << tr("&Import"); |
| 59 |
else |
| 60 |
return QStringList() << tr("&Build") << tr("&Insert"); |
| 61 |
} |
| 62 |
|
| 63 |
void InsertFragment::setMolecule(QtGui::Molecule* mol) |
| 64 |
{ |
| 65 |
m_molecule = mol; |
| 66 |
} |
| 67 |
|
| 68 |
void InsertFragment::showDialog() |
| 69 |
{ |
| 70 |
if (!m_molecule) |
| 71 |
return; |
| 72 |
|
| 73 |
QWidget* parentAsWidget = qobject_cast<QWidget*>(parent()); |
| 74 |
auto* theSender = qobject_cast<QAction*>(sender()); |
| 75 |
|
| 76 |
// Prompt user for input: |
| 77 |
bool crystal = theSender->data().toString() == "crystals"; |
| 78 |
if (crystal) { |
| 79 |
// create the dialog if it doesn't exist |
| 80 |
if (!m_crystalDialog) { |
| 81 |
m_crystalDialog = new InsertFragmentDialog(parentAsWidget, "crystals"); |
| 82 |
connect(m_crystalDialog, &InsertFragmentDialog::performInsert, this, |
| 83 |
&InsertFragment::performInsert); |
| 84 |
} |
| 85 |
m_crystalDialog->show(); |
| 86 |
m_crystalDialog->raise(); |
| 87 |
m_crystalDialog->activateWindow(); |
| 88 |
} else { |
| 89 |
// fragments - create the dialog if it doesn't exist |
| 90 |
if (!m_moleculeDialog) { |
| 91 |
m_moleculeDialog = new InsertFragmentDialog(parentAsWidget, "molecules"); |
| 92 |
connect(m_moleculeDialog, &InsertFragmentDialog::performInsert, this, |
| 93 |
&InsertFragment::performInsert); |
| 94 |
} |
| 95 |
m_moleculeDialog->show(); |
| 96 |
m_moleculeDialog->raise(); |
| 97 |
m_moleculeDialog->activateWindow(); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
void InsertFragment::performInsert(const QString& fileName, bool crystal) |
| 102 |
{ |
| 103 |
if (m_molecule == nullptr) |
| 104 |
return; |
| 105 |
|
| 106 |
// read the file into the new fragment |
| 107 |
Avogadro::QtGui::Molecule newMol(m_molecule->parent()); |
| 108 |
bool ok = |
| 109 |
Io::FileFormatManager::instance().readFile(newMol, fileName.toStdString()); |
| 110 |
if (!ok) { |
| 111 |
QMessageBox::critical(qobject_cast<QWidget*>(parent()), tr("Error"), |
| 112 |
tr("Error reading file (%1).").arg(fileName)); |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
if (crystal) { |
| 117 |
Molecule::MoleculeChanges changes = |
| 118 |
(Molecule::Atoms | Molecule::Bonds | Molecule::UnitCell | |
| 119 |
Molecule::NewCrystal | Molecule::Added | Molecule::Removed); |
| 120 |
|
| 121 |
// remove any bonds from newMol |
| 122 |
newMol.clearBonds(); |
| 123 |
m_molecule->undoMolecule()->modifyMolecule(newMol, changes, |
| 124 |
tr("Import Crystal")); |
| 125 |
// set the name |
| 126 |
std::string name = newMol.data("name").toString(); |
| 127 |
if (!name.empty()) |
| 128 |
m_molecule->setData("name", name); |
| 129 |
else { |
| 130 |
name = QFileInfo(fileName).baseName().toStdString(); |
| 131 |
m_molecule->setData("name", name); |
| 132 |
} |
| 133 |
|
| 134 |
emit requestActiveTool("Navigator"); |
| 135 |
} else { |
| 136 |
// insert mol into m_molecule |
| 137 |
m_molecule->undoMolecule()->appendMolecule(newMol, tr("Insert Fragment")); |
| 138 |
emit requestActiveTool("Manipulator"); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
} // namespace Avogadro::QtPlugins |