-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinputspreadsheet.cpp
104 lines (87 loc) · 3.29 KB
/
inputspreadsheet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include<inputspreadsheet.h>
#include<QKeyEvent>
#include<QApplication>
#include<QClipboard>
#include<QMessageBox>
inputSpreadSheet::inputSpreadSheet(QWidget *parent)
:QTableWidget(parent)
{
}
inputSpreadSheet::inputSpreadSheet(int rows, int cols, QWidget *parent)
:QTableWidget(rows, cols, parent)
{
}
inputSpreadSheet::~inputSpreadSheet()
{
}
void
inputSpreadSheet::keyPressEvent(QKeyEvent * event)
{
if(event->matches(QKeySequence::Copy) )
{
QString clipboardString;
QModelIndexList selectedIndexes = this->selectionModel()->selectedIndexes();
for (int i = 0; i < selectedIndexes.count(); ++i)
{
QModelIndex current = selectedIndexes[i];
QString displayText = current.data(Qt::DisplayRole).toString();
// If there exists another column beyond this one.
if (i + 1 < selectedIndexes.count())
{
QModelIndex next = selectedIndexes[i+1];
// If the column is on different row, the clipboard should take note.
if (next.row() != current.row())
{
displayText.append("\n");
}
else
{
// Otherwise append a column separator.
displayText.append("\t ");
}
}
clipboardString.append(displayText);
}
QApplication::clipboard()->setText(clipboardString);
} else if(event->matches(QKeySequence::Paste) ) {
/*
QString selected_text = qApp->clipboard()->text();
QStringList cells = selected_text.split(QRegExp(QLatin1String("\\n|\\t")));
while(!cells.empty() && cells.back().size() == 0)
{
cells.pop_back(); // strip empty trailing tokens
}
int rows = selected_text.count(QLatin1Char('\n'));
int cols = cells.size() / rows;
if(cells.size() % rows != 0)
{
// error, uneven number of columns, probably bad data
QMessageBox::critical(this, tr("Error"),
tr("Invalid clipboard data, unable to perform paste operation."));
return;
}
if(cols != columnCount())
{
// error, clipboard does not match current number of columns
QMessageBox::critical(this, tr("Error"),
tr("Invalid clipboard data, incorrect number of columns."));
return;
}
// don't clear the grid, we want to keep any existing headers
setRowCount(rows);
// setColumnCount(cols);
int cell = 0;
for(int row=0; row < rows; ++row)
{
for(int col=0; col < cols; ++col, ++cell)
{
QTableWidgetItem *newItem = new QTableWidgetItem(cells[cell]);
setItem(row, col, newItem);
}
}
*/
QMessageBox::critical(this, "You requested Paste", "Paste function not implemented yet!");
} else {
QTableWidget::keyPressEvent(event);
}
}