-
Notifications
You must be signed in to change notification settings - Fork 3
/
displaytablesdialog.cpp
132 lines (102 loc) · 3.66 KB
/
displaytablesdialog.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
#include "displaytablesdialog.h"
#include "ui_displaytablesdialog.h"
#include <QFileDialog>
#include <QDir>
#include <QtXlsx>
#include "windowtools.h"
DisplayTablesDialog::DisplayTablesDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::DisplayTablesDialog)
{
ui->setupUi(this);
setWindowTitle(tr("Results Table"));
WindowTools::SetDialogFixedMaximize(this);
}
DisplayTablesDialog::~DisplayTablesDialog()
{
delete ui;
}
/**
* @brief DisplayTablesDialog::InsertData
* @param data
*/
void DisplayTablesDialog::InsertData(QList<QList<QStringList>> * data)
{
QList<QTableWidget *> mTables;
mTables.append(ui->tableWidgetOverall);
mTables.append(ui->tableWidgetOne);
mTables.append(ui->tableWidgetTwo);
mTables.append(ui->tableWidgetThree);
for (int i(0); i<mTables.count(); i++)
{
mTables.at(i)->clear();
mTables.at(i)->setRowCount(0);
mTables.at(i)->setColumnCount(0);
QStringList temp;
for (int tableNumberRows(0); tableNumberRows < data->at(i).count(); tableNumberRows++)
{
temp = data->at(i).at(tableNumberRows);
mTables.at(i)->insertRow(mTables.at(i)->rowCount());
for (int tableNumberCols(0); tableNumberCols < data->at(i).at(tableNumberRows).count(); tableNumberCols++)
{
if(tableNumberCols >= mTables.at(i)->columnCount())
{
mTables.at(i)->insertColumn(mTables.at(i)->columnCount());
}
// Less one to adjust for Excel-style coordinates
mTables.at(i)->setItem(tableNumberRows - 1, tableNumberCols - 1, new QTableWidgetItem(temp.at(tableNumberCols)));
}
}
}
}
void DisplayTablesDialog::on_pushButtonSave_clicked()
{
QString file_name;
QString fileFilter = "Spreadsheet (*.xlsx)";
#ifdef _WIN32
file_name = QFileDialog::getSaveFileName(this, tr("Save spreadsheet file"), QDir::homePath(),
fileFilter);
#elif TARGET_OS_MAC
file_name = QFileDialog::getSaveFileName(this, tr("Save spreadsheet file"), QDir::homePath(),
fileFilter, &fileFilter, QFileDialog::Option::DontUseNativeDialog);
if (!file_name.contains(".xlsx"))
{
file_name.append(".xlsx");
}
#endif
if(!file_name.trimmed().isEmpty())
{
QList<QTableWidget *> mTables;
mTables.append(ui->tableWidgetOverall);
mTables.append(ui->tableWidgetOne);
mTables.append(ui->tableWidgetTwo);
mTables.append(ui->tableWidgetThree);
QStringList sheetNames;
sheetNames << "Case Report" << "Schedule One" << "Schedule Two" << "Schedule Three";
QApplication::setOverrideCursor(Qt::WaitCursor);
QXlsx::Document xlsx;
int rows, cols;
QString temp;
for (int i(0); i<mTables.count(); i++)
{
rows = mTables.at(i)->rowCount();
cols = mTables.at(i)->columnCount();
xlsx.addSheet(sheetNames.at(i));
for (int r=0; r<rows; r++)
{
for (int c=0; c<cols; c++)
{
QTableWidgetItem *item = mTables.at(i)->item(r, c);
if (item != NULL && !item->text().isEmpty())
{
temp = mTables.at(i)->item(r, c)->data(Qt::DisplayRole).toString();
xlsx.write(r + 1, c + 1, temp);
}
}
}
}
xlsx.selectSheet(sheetNames.at(0));
xlsx.saveAs(file_name);
QApplication::restoreOverrideCursor();
}
}