Engauge Digitizer 2
Loading...
Searching...
No Matches
WindowTable.cpp
Go to the documentation of this file.
1/******************************************************************************************************
2 * (C) 2016 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3 * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4 * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5 ******************************************************************************************************/
6
7#include <QApplication>
8#include <QClipboard>
9#include <qdebug.h>
10#include <QDrag>
11#include <QGuiApplication>
12#include <QHeaderView>
13#include <QMimeData>
14#include <QMouseEvent>
15#include <QStandardItemModel>
16#include "WindowModelBase.h"
17#include "WindowTable.h"
18
19// Modes:
20// -ContiguousSelection is an ok selection mode when dragging is disabled since user can click and drag
21// to easily define a rectangular selection. However, dragging changes that sequence into the start of a drag and drop.
22// -ExtendedSelection is best selection mode when dragging is disabled since it acts as ContiguousSelection and also
23// allows control-click to select/unselect individual cells
24// -MultiSelection is frustrating since user cannot easily remove existing selection by clicking on an unselected cell,
25// which results in tedious deselections
26const QAbstractItemView::SelectionMode SELECTION_MODE = QAbstractItemView::ExtendedSelection;
27
29{
30 horizontalHeader()->setStretchLastSection (true);
31 setModel (&model);
32 setSelectionMode (SELECTION_MODE);
33 // setDragEnabled (true); This is set later from MainWindowModel
34 setDragDropMode (QAbstractItemView::DragOnly);
35 horizontalHeader()->hide();
36 verticalHeader()->hide();
37 setEditTriggers (QAbstractItemView::NoEditTriggers); // Control is read only
38
39 // No WhatsThis text is needed since this table is within a dockable widget that has the same WhatsThis text for
40 // a click anywhere in that widget
41
42 // Connect model to view so model can access the current selection
43 model.setView (*this);
44}
45
49
50void WindowTable::doDragAndClipboardCopy ()
51{
52 // Drag operation
53 QDrag *drag = new QDrag (this);
54 QMimeData *mimeData = new QMimeData;
55
56 mimeData->setText (exportText ());
57 drag->setMimeData (mimeData);
58
59 drag->exec (Qt::CopyAction);
60
61 // Copy to clipboard. In linux the result can be checked using 'xclip c -o'
62 QGuiApplication::clipboard()->setText (exportText ());
63}
64
65QString WindowTable::exportText () const
66{
67 QString rtn;
68
69 QModelIndexList indexes = selectionModel ()->selection ().indexes ();
70 if (indexes.count () > 0) {
71
72 // Copy the rectangular region around the selected cells. Tab-separation
73 // seems to nicely copy to spreadsheets and other apps
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) {
78 rowMin = index.row();
79 }
80 if (i == 0 || index.column() < colMin) {
81 colMin = index.column();
82 }
83 if (i == 0 || rowMax < index.row()) {
84 rowMax = index.row();
85 }
86 if (i == 0 || colMax < index.column()) {
87 colMax = index.column();
88 }
89 }
90
91 // Output table with tab separation
92 for (int row = rowMin; row <= rowMax; row++) {
93 for (int col = colMin; col <= colMax; col++) {
94 if (col > 0) {
95 rtn += QString ("\t");
96 }
97
98 rtn += model()->index (row, col).data().toString();
99 }
100
101 rtn += QString ("\n");
102 }
103 }
104
105 return rtn;
106}
107
108void WindowTable::focusInEvent (QFocusEvent *event)
109{
110 QTableView::focusInEvent (event);
111
113}
114
115void WindowTable::focusOutEvent (QFocusEvent *event)
116{
117 QTableView::focusOutEvent (event);
118
120}
121
122void WindowTable::mouseMoveEvent (QMouseEvent *event)
123{
124 // Only use left clicks
125 if (! (event->buttons() & Qt::LeftButton)) {
126 return;
127 }
128
129 // Ignore small moves
130 if ((event->pos() - m_pressPos).manhattanLength() < QApplication::startDragDistance ()) {
131 return;
132 }
133
134 doDragAndClipboardCopy ();
135
136 QTableView::mouseMoveEvent (event);
137}
138
139void WindowTable::mousePressEvent (QMouseEvent *event)
140{
141 // Only use left clicks
142 if (event->button() == Qt::LeftButton) {
143 m_pressPos = event->pos();
144 }
145
146 QTableView::mousePressEvent (event);
147}
148
149void WindowTable::selectionChanged(const QItemSelection &selected,
150 const QItemSelection &deselected)
151{
152 QTableView::selectionChanged (selected,
153 deselected);
154
156}
const QAbstractItemView::SelectionMode SELECTION_MODE
Model for WindowTable.
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.