-
Notifications
You must be signed in to change notification settings - Fork 22
/
SimpleSpreadsheetWidget.cpp
270 lines (237 loc) · 8.01 KB
/
SimpleSpreadsheetWidget.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "SimpleSpreadsheetWidget.h"
#include <QKeyEvent>
#include <QApplication>
#include <QClipboard>
#include <QDebug>
#include <QMimeData>
#include <QMessageBox>
#include <QJsonArray>
#include <QJsonObject>
SimpleSpreadsheetWidget::SimpleSpreadsheetWidget(int colCount, int rowCount, QStringList head, QList<int>types, QWidget *parent)
: QTableWidget(parent), numRow(rowCount), numCol(colCount), theHeadings(head), dataTypes(types)
{
this->setRowCount(rowCount);
this->setColumnCount(colCount);
this->setHorizontalHeaderLabels(head);
}
SimpleSpreadsheetWidget::~SimpleSpreadsheetWidget()
{
}
void SimpleSpreadsheetWidget::clear()
{
QTableWidget::clearContents();
}
int
SimpleSpreadsheetWidget::getNumRows(){
return numRow;
}
int SimpleSpreadsheetWidget::getNumColumns(){
return numCol;
}
bool SimpleSpreadsheetWidget::getString(int row, int col, QString &res){
QTableWidgetItem *theItem = this->item(row,col);
if (theItem == 0)
return false;
res = theItem->text();
return true;
}
bool SimpleSpreadsheetWidget::getDouble(int row, int col, double &res){
QTableWidgetItem *theItem = this->item(row,col);
if (theItem == 0)
return false;
QString textData = theItem->text();
res = textData.toDouble();
return true;
}
bool SimpleSpreadsheetWidget::getInt(int row, int col, int &res){
QTableWidgetItem *theItem = this->item(row,col);
if (theItem == 0)
return false;
QString textData = theItem->text();
res = textData.toInt();
return true;
}
int SimpleSpreadsheetWidget::setString(int row,int col, QString &data)
{
QTableWidgetItem *theCell = this->item(row, col);
if(!theCell)
{
theCell = new QTableWidgetItem;
this->setItem(row, col, theCell);
}
theCell->setText(data);
return 0;
}
int SimpleSpreadsheetWidget::setDouble(int row, int col, double data)
{
QTableWidgetItem *theCell = this->item(row, col);
if(!theCell)
{
theCell = new QTableWidgetItem;
this->setItem(row, col, theCell);
}
theCell->setText(QString::number(data));
return 0;
}
int SimpleSpreadsheetWidget::setInt(int row, int col, int data)
{
QTableWidgetItem *theCell = this->item(row, col);
if(!theCell)
{
theCell = new QTableWidgetItem;
this->setItem(row, col, theCell);
}
theCell->setText(QString::number(data));
return 0;
}
void
SimpleSpreadsheetWidget::outputToJSON(QJsonArray &rvArray)
{
// QJsonArray rvArray;
QApplication::setOverrideCursor(Qt::WaitCursor);
for (int row = 0; row < numRow; ++row) {
QTableWidgetItem *firstItem= this->item(row, 0);
//QModelIndex = this->it
if (firstItem != 0) {
QString firstItemString =firstItem->text();
qDebug() << firstItemString;
if (!firstItemString.isEmpty()) {
QJsonObject obj;
for (int column = 0; column < numCol; ++column) {
QTableWidgetItem *theItem = this->item(row,column);
if (theItem == 0)
break;
QString textData = theItem->text();
if (dataTypes.at(column) == SIMPLESPREADSHEET_QString)
obj[theHeadings.at(column)]=textData;
else if (dataTypes.at(column) == SIMPLESPREADSHEET_QDouble) {
// QString textPrecision = QString("%1").arg(textData, 0, 'g', 13);
obj[theHeadings.at(column)]=textData.toDouble();
} else if (dataTypes.at(column) == SIMPLESPREADSHEET_QInt)
obj[theHeadings.at(column)]=textData.toInt();
}
rvArray.append(obj);
}
}
}
QApplication::restoreOverrideCursor();
}
void
SimpleSpreadsheetWidget::inputFromJSON(QJsonArray &rvArray){
}
void SimpleSpreadsheetWidget::keyPressEvent( QKeyEvent *event )
{
if (event->key() == Qt::Key_C && event->modifiers() & Qt::ControlModifier) {
QApplication::clipboard()->setText( this->currentIndex().data().toString() );
//this->copy();
}
else if (event->modifiers() & Qt::ControlModifier && event->key() == Qt::Key_V) {
model()->setData( currentIndex(), QApplication::clipboard()->text() );
//this->paste();
}
else {
QTableWidget::keyPressEvent(event);
}
}
void SimpleSpreadsheetWidget::copy()
{
QItemSelectionModel * selection = selectionModel();
QModelIndexList indexes = selection->selectedIndexes();
qDebug() << "COPY";
if(indexes.size() < 1)
return;
// QModelIndex::operator < sorts first by row, then by column.
// this is what we need
// std::sort(indexes.begin(), indexes.end());
qSort(indexes);
// You need a pair of indexes to find the row changes
QModelIndex previous = indexes.first();
indexes.removeFirst();
QString selected_text_as_html;
QString selected_text;
selected_text_as_html.prepend("<html><style>br{mso-data-placement:same-cell;}</style><table><tr><td>");
QModelIndex current;
Q_FOREACH(current, indexes)
{
QVariant data = model()->data(previous);
QString text = data.toString();
selected_text.append(text);
text.replace("\n","<br>");
// At this point `text` contains the text in one cell
selected_text_as_html.append(text);
// If you are at the start of the row the row number of the previous index
// isn't the same. Text is followed by a row separator, which is a newline.
if (current.row() != previous.row())
{
selected_text_as_html.append("</td></tr><tr><td>");
selected_text.append(QLatin1Char('\n'));
}
// Otherwise it's the same row, so append a column separator, which is a tab.
else
{
selected_text_as_html.append("</td><td>");
selected_text.append(QLatin1Char('\t'));
}
previous = current;
}
// add last element
selected_text_as_html.append(model()->data(current).toString());
selected_text.append(model()->data(current).toString());
selected_text_as_html.append("</td></tr>");
QMimeData * md = new QMimeData;
md->setHtml(selected_text_as_html);
// qApp->clipboard()->setText(selected_text);
md->setText(selected_text);
qApp->clipboard()->setMimeData(md);
qDebug() << selected_text;
QApplication::clipboard()->setText(selected_text);
// selected_text.append(QLatin1Char('\n'));
// qApp->clipboard()->setText(selected_text);
}
void SimpleSpreadsheetWidget::paste()
{
qDebug() << "PASTE";
if(qApp->clipboard()->mimeData()->hasHtml())
{
// TODO, parse the html data
}
else
{
QString selected_text = qApp->clipboard()->text();
selected_text = QApplication::clipboard()->text();
qDebug() << selected_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);
}
}
}
}