Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial support for copy from tables #1506

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion avogadro/qtplugins/propertytables/propertyview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@

#include <avogadro/qtgui/molecule.h>

#include <QAction>
#include <QApplication>
#include <QtCore/QAbstractTableModel>
#include <QtCore/QSortFilterProxyModel>
#include <QAction>
#include <QtGui/QClipboard>
#include <QtGui/QKeyEvent>

#include <QtWidgets/QDialog>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QScrollBar>
Expand Down Expand Up @@ -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
7 changes: 5 additions & 2 deletions avogadro/qtplugins/propertytables/propertyview.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
class QProgressDialog;
namespace Avogadro {

namespace QtGui{
namespace QtGui {
class Molecule;
}

class PropertyView : public QTableView
{
Q_OBJECT
public:

explicit PropertyView(PropertyType type, QWidget* parent = 0);

void selectionChanged(const QItemSelection& selected,
Expand All @@ -30,6 +29,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;
Expand Down
Loading