diff --git a/avogadro/qtplugins/propertytables/propertyview.cpp b/avogadro/qtplugins/propertytables/propertyview.cpp index f9591e4d09..182bbed6d2 100644 --- a/avogadro/qtplugins/propertytables/propertyview.cpp +++ b/avogadro/qtplugins/propertytables/propertyview.cpp @@ -7,9 +7,13 @@ #include +#include +#include #include #include -#include +#include +#include + #include #include #include @@ -145,4 +149,41 @@ void PropertyView::hideEvent(QHideEvent*) this->deleteLater(); } +void PropertyView::keyPressEvent(QKeyEvent* event) +{ + // handle copy event + // thanks to https://www.walletfox.com/course/qtableviewcopypaste.php + if (!event->matches(QKeySequence::Copy)) { + QTableView::keyPressEvent(event); + return; + } + + // get the selected rows (if any) + QModelIndexList selectedRows = selectionModel()->selectedRows(); + + // if nothing is selected, copy everything to the clipboard + QString text; + if (selectedRows.isEmpty()) { + // iterate through every row and column and copy the data + for (int i = 0; i < model()->rowCount(); ++i) { + QStringList rowContents; + for (int j = 0; j < model()->columnCount(); ++j) + rowContents << model()->index(i, j).data().toString(); + text += rowContents.join("\t"); + text += "\n"; + } + } else { + // copy the selected rows to the clipboard + QItemSelectionRange range = selectionModel()->selection().first(); + for (auto i = range.top(); i <= range.bottom(); ++i) { + QStringList rowContents; + for (auto j = range.left(); j <= range.right(); ++j) + rowContents << model()->index(i, j).data().toString(); + text += rowContents.join("\t"); + text += "\n"; + } + } + QApplication::clipboard()->setText(text); +} + } // end namespace Avogadro diff --git a/avogadro/qtplugins/propertytables/propertyview.h b/avogadro/qtplugins/propertytables/propertyview.h index c0d2c90c5f..b6cfba77ac 100644 --- a/avogadro/qtplugins/propertytables/propertyview.h +++ b/avogadro/qtplugins/propertytables/propertyview.h @@ -30,6 +30,10 @@ class PropertyView : public QTableView void setSourceModel(PropertyModel* model) { m_model = model; } void hideEvent(QHideEvent* event); +protected: + // copy the selected properties to the clipboard + void keyPressEvent(QKeyEvent *event); + private: PropertyType m_type; QtGui::Molecule* m_molecule;