| 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 "copypaste.h" |
| 7 |
|
| 8 |
#include <avogadro/io/cjsonformat.h> |
| 9 |
#include <avogadro/io/fileformatmanager.h> |
| 10 |
|
| 11 |
#include <avogadro/qtgui/molecule.h> |
| 12 |
#include <avogadro/qtgui/rwmolecule.h> |
| 13 |
|
| 14 |
#include <QtCore/QMimeData> |
| 15 |
|
| 16 |
#include <QAction> |
| 17 |
#include <QtGui/QClipboard> |
| 18 |
#include <QtWidgets/QApplication> |
| 19 |
#include <QtWidgets/QMessageBox> |
| 20 |
|
| 21 |
#include <string> |
| 22 |
#include <vector> |
| 23 |
|
| 24 |
namespace Avogadro::QtPlugins { |
| 25 |
|
| 26 |
using namespace Avogadro::QtGui; |
| 27 |
|
| 28 |
CopyPaste::CopyPaste(QObject* parent_) |
| 29 |
: Avogadro::QtGui::ExtensionPlugin(parent_), m_pastedFormat(nullptr), |
| 30 |
m_copyAction(new QAction(tr("Copy"), this)), |
| 31 |
// don't translate SMILES, InChI, or XYZ |
| 32 |
m_copySMILES(new QAction("SMILES", this)), |
| 33 |
m_copyInChI(new QAction("InChI", this)), |
| 34 |
m_copyXYZ(new QAction("XYZ", this)), |
| 35 |
m_cutAction(new QAction(tr("Cut"), this)), |
| 36 |
m_clearAction(new QAction(tr("Delete"), this)), |
| 37 |
m_pasteAction(new QAction(tr("&Paste"), this)) |
| 38 |
{ |
| 39 |
m_cutAction->setShortcut(QKeySequence::Cut); |
| 40 |
m_cutAction->setIcon(QIcon::fromTheme("edit-cut")); |
| 41 |
m_cutAction->setProperty("menu priority", 560); |
| 42 |
connect(m_cutAction, SIGNAL(triggered()), SLOT(cut())); |
| 43 |
|
| 44 |
m_copyAction->setShortcut(QKeySequence::Copy); |
| 45 |
m_copyAction->setIcon(QIcon::fromTheme("edit-copy")); |
| 46 |
m_copyAction->setProperty("menu priority", 550); |
| 47 |
connect(m_copyAction, SIGNAL(triggered()), SLOT(copyCJSON())); |
| 48 |
|
| 49 |
m_copySMILES->setProperty("menu priority", 540); |
| 50 |
connect(m_copySMILES, SIGNAL(triggered()), SLOT(copySMILES())); |
| 51 |
|
| 52 |
m_copyInChI->setProperty("menu priority", 530); |
| 53 |
connect(m_copyInChI, SIGNAL(triggered()), SLOT(copyInChI())); |
| 54 |
|
| 55 |
m_copyXYZ->setProperty("menu priority", 520); |
| 56 |
connect(m_copyXYZ, SIGNAL(triggered()), SLOT(copyXYZ())); |
| 57 |
|
| 58 |
m_pasteAction->setShortcut(QKeySequence::Paste); |
| 59 |
m_pasteAction->setIcon(QIcon::fromTheme("edit-paste")); |
| 60 |
m_pasteAction->setProperty("menu priority", 510); |
| 61 |
connect(m_pasteAction, SIGNAL(triggered()), SLOT(paste())); |
| 62 |
|
| 63 |
m_clearAction->setShortcut(QKeySequence::Delete); |
| 64 |
m_clearAction->setIcon(QIcon::fromTheme("edit-delete")); |
| 65 |
m_clearAction->setProperty("menu priority", 500); |
| 66 |
connect(m_clearAction, SIGNAL(triggered()), SLOT(clear())); |
| 67 |
} |
| 68 |
|
| 69 |
CopyPaste::~CopyPaste() |
| 70 |
{ |
| 71 |
delete m_pastedFormat; |
| 72 |
} |
| 73 |
|
| 74 |
QList<QAction*> CopyPaste::actions() const |
| 75 |
{ |
| 76 |
QList<QAction*> result; |
| 77 |
return result << m_copyAction << m_copySMILES << m_copyInChI << m_copyXYZ |
| 78 |
<< m_cutAction << m_pasteAction << m_clearAction; |
| 79 |
} |
| 80 |
|
| 81 |
QStringList CopyPaste::menuPath(QAction* action) const |
| 82 |
{ |
| 83 |
if (action->text() != tr("SMILES") && action->text() != tr("InChI") && |
| 84 |
action->text() != tr("XYZ")) |
| 85 |
return QStringList() << tr("&Edit"); |
| 86 |
else |
| 87 |
return QStringList() << tr("&Edit") << tr("Copy As"); |
| 88 |
} |
| 89 |
|
| 90 |
void CopyPaste::setMolecule(QtGui::Molecule* mol) |
| 91 |
{ |
| 92 |
m_molecule = mol; |
| 93 |
} |
| 94 |
|
| 95 |
bool CopyPaste::copyCJSON() |
| 96 |
{ |
| 97 |
Io::CjsonFormat cjson; |
| 98 |
return copy(&cjson); |
| 99 |
} |
| 100 |
|
| 101 |
void CopyPaste::copySMILES() |
| 102 |
{ |
| 103 |
Io::FileFormatManager& formats = Io::FileFormatManager::instance(); |
| 104 |
Io::FileFormat* format(formats.newFormatFromFileExtension("smi")); |
| 105 |
|
| 106 |
copy(format); |
| 107 |
delete format; |
| 108 |
} |
| 109 |
|
| 110 |
void CopyPaste::copyInChI() |
| 111 |
{ |
| 112 |
Io::FileFormatManager& formats = Io::FileFormatManager::instance(); |
| 113 |
Io::FileFormat* format(formats.newFormatFromFileExtension("inchi")); |
| 114 |
|
| 115 |
copy(format); |
| 116 |
delete format; |
| 117 |
} |
| 118 |
|
| 119 |
void CopyPaste::copyXYZ() |
| 120 |
{ |
| 121 |
Io::FileFormatManager& formats = Io::FileFormatManager::instance(); |
| 122 |
Io::FileFormat* format(formats.newFormatFromFileExtension("xyz")); |
| 123 |
|
| 124 |
copy(format); |
| 125 |
delete format; |
| 126 |
} |
| 127 |
|
| 128 |
bool CopyPaste::copy(Io::FileFormat* format) |
| 129 |
{ |
| 130 |
if (m_molecule == nullptr || m_molecule->atomCount() == 0 || |
| 131 |
format == nullptr) |
| 132 |
return false; |
| 133 |
|
| 134 |
std::string output; |
| 135 |
QtGui::Molecule* copy = m_molecule; |
| 136 |
|
| 137 |
if (!m_molecule->isSelectionEmpty()) { |
| 138 |
// create a copy of the selected atoms only |
| 139 |
copy = new QtGui::Molecule(m_molecule->parent()); |
| 140 |
|
| 141 |
// go through the selected atoms and add them |
| 142 |
// make sure to track the new index |
| 143 |
std::vector<Index> atomIndex(m_molecule->atomCount(), 0); |
| 144 |
for (Index i = 0; i < m_molecule->atomCount(); ++i) |
| 145 |
if (m_molecule->atomSelected(i)) { |
| 146 |
auto a = copy->addAtom(m_molecule->atomicNumber(i)); |
| 147 |
a.setPosition3d(m_molecule->atomPosition3d(i)); |
| 148 |
|
| 149 |
// track the index |
| 150 |
atomIndex[i] = a.index(); |
| 151 |
} |
| 152 |
|
| 153 |
for (Index i = 0; i < m_molecule->bondCount(); ++i) { |
| 154 |
Core::Bond bond = m_molecule->bond(i); |
| 155 |
Index start = bond.atom1().index(); |
| 156 |
Index end = bond.atom2().index(); |
| 157 |
if (m_molecule->atomSelected(start) && m_molecule->atomSelected(start)) { |
| 158 |
copy->addBond(atomIndex[start], atomIndex[end], bond.order()); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if (!format->writeString(output, *copy)) { |
| 164 |
QMessageBox::warning( |
| 165 |
qobject_cast<QWidget*>(this->parent()), tr("Error Clipping Molecule"), |
| 166 |
tr("Error generating clipboard data.") + "\n" + |
| 167 |
tr("Output format: %1\n%2", "file format") |
| 168 |
.arg(QString::fromStdString(m_pastedFormat->name())) |
| 169 |
.arg(QString::fromStdString(m_pastedFormat->description())) + |
| 170 |
"\n\n" + |
| 171 |
tr("Reader error:\n%1") |
| 172 |
.arg(QString::fromStdString(m_pastedFormat->error()))); |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
QByteArray outputBA(output.c_str(), static_cast<int>(output.length())); |
| 177 |
|
| 178 |
auto* mimeData(new QMimeData); |
| 179 |
|
| 180 |
std::vector<std::string> mimeTypes(format->mimeTypes()); |
| 181 |
for (auto& mimeType : mimeTypes) |
| 182 |
mimeData->setData(QString::fromStdString(mimeType), outputBA); |
| 183 |
|
| 184 |
mimeData->setData("text/plain", outputBA); |
| 185 |
QApplication::clipboard()->setMimeData(mimeData); |
| 186 |
|
| 187 |
if (!m_molecule->isSelectionEmpty()) |
| 188 |
copy->deleteLater(); // don't leak our copy |
| 189 |
|
| 190 |
return true; |
| 191 |
} |
| 192 |
|
| 193 |
void CopyPaste::cut() |
| 194 |
{ |
| 195 |
if (!copyCJSON()) |
| 196 |
return; |
| 197 |
|
| 198 |
if (m_molecule->isSelectionEmpty()) |
| 199 |
m_molecule->undoMolecule()->clearAtoms(); |
| 200 |
else { |
| 201 |
// Remove atoms from the largest to the smallest index |
| 202 |
// (that way, the index doesn't change) |
| 203 |
for (Index i = m_molecule->atomCount(); i > 0; --i) |
| 204 |
// atoms go from 0 to atomCount()-1 |
| 205 |
if (m_molecule->atomSelected(i - 1)) |
| 206 |
m_molecule->undoMolecule()->removeAtom(i - 1); |
| 207 |
} |
| 208 |
|
| 209 |
m_molecule->emitChanged(QtGui::Molecule::Atoms | QtGui::Molecule::Bonds | |
| 210 |
QtGui::Molecule::Removed); |
| 211 |
} |
| 212 |
|
| 213 |
void CopyPaste::clear() |
| 214 |
{ |
| 215 |
if (m_molecule == nullptr || m_molecule->atomCount() == 0) |
| 216 |
return; |
| 217 |
|
| 218 |
if (m_molecule->isSelectionEmpty()) |
| 219 |
m_molecule->undoMolecule()->clearAtoms(); |
| 220 |
else { |
| 221 |
// Remove atoms from the largest to the smallest index |
| 222 |
// (that way, the index doesn't change) |
| 223 |
for (Index i = m_molecule->atomCount(); i > 0; --i) { |
| 224 |
// atoms go from 0 to atomCount()-1 |
| 225 |
if (m_molecule->atomSelected(i - 1)) |
| 226 |
m_molecule->undoMolecule()->removeAtom(i - 1); |
| 227 |
} |
| 228 |
} |
| 229 |
m_molecule->emitChanged(QtGui::Molecule::Atoms | QtGui::Molecule::Bonds | |
| 230 |
QtGui::Molecule::Removed); |
| 231 |
} |
| 232 |
|
| 233 |
void CopyPaste::paste() |
| 234 |
{ |
| 235 |
// Delete any old clipboard data. |
| 236 |
if (m_pastedFormat) { |
| 237 |
delete m_pastedFormat; |
| 238 |
m_pastedFormat = nullptr; |
| 239 |
m_pastedData.clear(); |
| 240 |
} |
| 241 |
|
| 242 |
if (!m_molecule) |
| 243 |
return; // nothing to do |
| 244 |
|
| 245 |
// make sure we clear the current selection |
| 246 |
for (Index i = 0; i < m_molecule->atomCount(); ++i) |
| 247 |
m_molecule->setAtomSelected(i, false); |
| 248 |
|
| 249 |
const QMimeData* mimeData(QApplication::clipboard()->mimeData()); |
| 250 |
|
| 251 |
if (!mimeData) { |
| 252 |
QMessageBox::warning(qobject_cast<QWidget*>(this->parent()), |
| 253 |
tr("Error Pasting Molecule"), |
| 254 |
tr("Cannot paste molecule: Clipboard empty!")); |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
// Try to find a reader that can handle the available mime-types. |
| 259 |
Io::FileFormatManager& mgr = Io::FileFormatManager::instance(); |
| 260 |
QStringList mimeTypes(mimeData->formats()); |
| 261 |
Io::FileFormat::Operations ops(Io::FileFormat::Read | Io::FileFormat::String); |
| 262 |
foreach (const QString& mimeType, mimeTypes) { |
| 263 |
if ((m_pastedFormat = |
| 264 |
mgr.newFormatFromMimeType(mimeType.toStdString(), ops))) { |
| 265 |
m_pastedData = mimeData->data(mimeType); |
| 266 |
break; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
// No mime-type match, default to cjson. |
| 271 |
if (!m_pastedFormat && mimeData->hasText()) { |
| 272 |
m_pastedFormat = new Io::CjsonFormat; |
| 273 |
m_pastedData = mimeData->text().toLatin1(); |
| 274 |
} |
| 275 |
|
| 276 |
if (!m_pastedFormat) |
| 277 |
return; |
| 278 |
|
| 279 |
// we have a format, so try to insert the new bits into m_molecule |
| 280 |
Avogadro::QtGui::Molecule mol(m_molecule->parent()); |
| 281 |
bool success = m_pastedFormat->readString( |
| 282 |
std::string(m_pastedData.constData(), m_pastedData.size()), mol); |
| 283 |
|
| 284 |
if (!success) { |
| 285 |
QMessageBox::warning( |
| 286 |
qobject_cast<QWidget*>(this->parent()), tr("Error Pasting Molecule"), |
| 287 |
tr("Error reading clipboard data.") + "\n" + |
| 288 |
tr("Detected format: %1\n%2", "file format description") |
| 289 |
.arg(QString::fromStdString(m_pastedFormat->name())) |
| 290 |
.arg(QString::fromStdString(m_pastedFormat->description())) + |
| 291 |
"\n\n" + |
| 292 |
tr("Reader error:\n%1") |
| 293 |
.arg(QString::fromStdString(m_pastedFormat->error()))); |
| 294 |
} |
| 295 |
|
| 296 |
// insert mol into m_molecule |
| 297 |
m_molecule->undoMolecule()->appendMolecule(mol, "Paste Molecule"); |
| 298 |
emit requestActiveTool("Manipulator"); |
| 299 |
|
| 300 |
delete m_pastedFormat; |
| 301 |
m_pastedFormat = nullptr; |
| 302 |
m_pastedData.clear(); |
| 303 |
} |
| 304 |
|
| 305 |
} // namespace Avogadro::QtPlugins |