forked from PavelMr/skytechx
/
cephtable.cpp
148 lines (119 loc) · 3.77 KB
/
cephtable.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
#include "cephtable.h"
#include "ui_cephtable.h"
#include "skcore.h"
#include <QPrinter>
#include <QPrintDialog>
CEphTable::CEphTable(QWidget *parent, QString name, QStringList header, QList<tableRow_t> row) :
QDialog(parent),
ui(new Ui::CEphTable)
{
ui->setupUi(this);
m_name = name;
setWindowTitle(tr("Ephemerides of ") + name);
QStandardItemModel *m = new QStandardItemModel(0, header.count());
for (int i = 0; i < header.count(); i++)
{
m->setHeaderData(i, Qt::Horizontal, header[i]);
}
for (int i = 0; i < row.count(); i++)
{
QList <QStandardItem *> items;
for (int j = 0; j < row[i].row.count(); j++)
{
QStandardItem *itm = new QStandardItem;
itm->setText(row[i].row[j]);
items.append(itm);
}
m->appendRow(items);
}
ui->tableView->setModel(m);
}
CEphTable::~CEphTable()
{
delete ui;
}
/////////////////////////////////////////
void CEphTable::on_pushButton_2_clicked()
/////////////////////////////////////////
{
done(DL_OK);
}
/////////////////////////////////////////
void CEphTable::on_pushButton_3_clicked()
/////////////////////////////////////////
{
QString name = QFileDialog::getSaveFileName(this, tr("Save File"),
"untitled.csv",
tr("CSV Files (*.csv)"));
if (name.isEmpty())
return;
const int rowCount = ui->tableView->model()->rowCount();
const int columnCount = ui->tableView->model()->columnCount();
SkFile fOut(name);
if (fOut.open(SkFile::WriteOnly | SkFile::Text))
{
QTextStream s(&fOut);
for (int column = 0; column < columnCount; column++)
{
s << ui->tableView->model()->headerData(column, Qt::Horizontal).toString();
s << ";";
}
s << "\n";
for (int row = 0; row < rowCount; row++)
{
for (int column = 0; column < columnCount; column++)
{
QString data = ui->tableView->model()->data(ui->tableView->model()->index(row, column)).toString().simplified();
s << data + ";";
}
s << "\n";
}
}
fOut.close();
}
///////////////////////////////////////
void CEphTable::on_pushButton_clicked()
///////////////////////////////////////
{
QString strStream;
QTextStream out(&strStream);
const int rowCount = ui->tableView->model()->rowCount();
const int columnCount = ui->tableView->model()->columnCount();
out << "<html>\n"
"<head>\n"
"<meta Content=\"Text/html; charset=Windows-1251\">\n"
<< QString("<title>%1</title>\n").arg(tr("Ephemerids - ") + m_name)
<< "</head>\n"
"<body bgcolor=#ffffff link=#5000A0>\n"
"<table border=1 cellspacing=0 cellpadding=2>\n";
// headers
out << "<tr bgcolor=#f0f0f0>";
for (int column = 0; column < columnCount; column++)
if (!ui->tableView->isColumnHidden(column))
out << QString("<th>%1</th>").arg(ui->tableView->model()->headerData(column, Qt::Horizontal).toString());
out << "</tr>\n";
// data table
for (int row = 0; row < rowCount; row++) {
out << "<tr>";
for (int column = 0; column < columnCount; column++) {
if (!ui->tableView->isColumnHidden(column)) {
QString data = ui->tableView->model()->data(ui->tableView->model()->index(row, column)).toString().simplified();
out << QString("<td bkcolor=0>%1</td>").arg((!data.isEmpty()) ? data : QString(" "));
}
}
out << "</tr>\n";
}
out << "</table>\n"
"</body>\n"
"</html>\n";
QTextDocument *document = new QTextDocument();
document->setHtml(strStream);
QPrinter printer;
QPrintDialog *dialog = new QPrintDialog(&printer, this);
if (dialog->exec() == QDialog::Accepted)
{
document->print(&printer);
}
delete dialog;
delete document;
}