Skip to content

Commit

Permalink
Add initial support for copy from tables
Browse files Browse the repository at this point in the history
Should add a context menu too

Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis committed Dec 5, 2023
1 parent 34b34ea commit 9aaf503
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
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
4 changes: 4 additions & 0 deletions avogadro/qtplugins/propertytables/propertyview.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9aaf503

Please sign in to comment.