11#include <QGuiApplication>
15#include <QStandardItemModel>
26const QAbstractItemView::SelectionMode
SELECTION_MODE = QAbstractItemView::ExtendedSelection;
30 horizontalHeader()->setStretchLastSection (
true);
34 setDragDropMode (QAbstractItemView::DragOnly);
35 horizontalHeader()->hide();
36 verticalHeader()->hide();
37 setEditTriggers (QAbstractItemView::NoEditTriggers);
50void WindowTable::doDragAndClipboardCopy ()
53 QDrag *drag =
new QDrag (
this);
54 QMimeData *mimeData =
new QMimeData;
56 mimeData->setText (exportText ());
57 drag->setMimeData (mimeData);
59 drag->exec (Qt::CopyAction);
62 QGuiApplication::clipboard()->setText (exportText ());
65QString WindowTable::exportText ()
const
69 QModelIndexList indexes = selectionModel ()->selection ().indexes ();
70 if (indexes.count () > 0) {
74 int rowMin = 0, rowMax = 0, colMin = 0, colMax = 0;
75 for (
int i = 0; i < indexes.count(); i++) {
76 QModelIndex index = indexes.at (i);
77 if (i == 0 || index.row() < rowMin) {
80 if (i == 0 || index.column() < colMin) {
81 colMin = index.column();
83 if (i == 0 || rowMax < index.row()) {
86 if (i == 0 || colMax < index.column()) {
87 colMax = index.column();
92 for (
int row = rowMin; row <= rowMax; row++) {
93 for (
int col = colMin; col <= colMax; col++) {
95 rtn += QString (
"\t");
98 rtn += model()->index (row, col).data().toString();
101 rtn += QString (
"\n");
110 QTableView::focusInEvent (event);
117 QTableView::focusOutEvent (event);
125 if (! (event->buttons() & Qt::LeftButton)) {
130 if ((event->pos() - m_pressPos).manhattanLength() < QApplication::startDragDistance ()) {
134 doDragAndClipboardCopy ();
136 QTableView::mouseMoveEvent (event);
142 if (event->button() == Qt::LeftButton) {
143 m_pressPos =
event->pos();
146 QTableView::mousePressEvent (event);
150 const QItemSelection &deselected)
152 QTableView::selectionChanged (selected,
const QAbstractItemView::SelectionMode SELECTION_MODE
void setView(WindowTable &view)
Save the view so this class can access the current selection.
virtual void focusOutEvent(QFocusEvent *)
Catch this table status change.
virtual void focusInEvent(QFocusEvent *)
Catch this table status change.
virtual void mousePressEvent(QMouseEvent *event)
Track the starting point of drag operations.
void signalTableStatusChange()
Sent when a change occurs that should affect the Copy menu item.
WindowTable(WindowModelBase &model)
Single constructor.
virtual void mouseMoveEvent(QMouseEvent *event)
Trigger drag operation if cursor was dragged more than a minimum distance.
virtual void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
Catch this table status change.