forked from sqlitebrowser/sqlitebrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExportCsvDialog.cpp
More file actions
123 lines (110 loc) · 4.17 KB
/
ExportCsvDialog.cpp
File metadata and controls
123 lines (110 loc) · 4.17 KB
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
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QFileDialog>
#include "ExportCsvDialog.h"
#include "ui_ExportCsvDialog.h"
#include "sqlitedb.h"
#include "PreferencesDialog.h"
#include "sqlitetablemodel.h"
ExportCsvDialog::ExportCsvDialog(DBBrowserDB* db, QWidget* parent, const QString& query, const QString& selection)
: QDialog(parent),
ui(new Ui::ExportCsvDialog),
pdb(db),
m_sQuery(query)
{
// Create UI
ui->setupUi(this);
// If a SQL query was specified hide the table combo box. If not fill it with tables to export
if(query.isEmpty())
{
// Get list of tables to export
objectMap objects = pdb->getBrowsableObjects();
for(objectMap::ConstIterator i=objects.begin();i!=objects.end();++i)
ui->comboTable->addItem(QIcon(QString(":icons/%1").arg(i.value().gettype())), i.value().getname());
// Sort list of tables and select the table specified in the selection parameter or alternatively the first one
ui->comboTable->model()->sort(0);
if(selection.isEmpty())
ui->comboTable->setCurrentIndex(0);
else
ui->comboTable->setCurrentIndex(ui->comboTable->findText(selection));
} else {
// Hide table combo box
ui->labelTable->setVisible(false);
ui->comboTable->setVisible(false);
}
}
ExportCsvDialog::~ExportCsvDialog()
{
delete ui;
}
void ExportCsvDialog::accept()
{
// Get filename
QString fileName = QFileDialog::getSaveFileName(
this,
tr("Choose a filename to export data"),
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
tr("Text files(*.csv *.txt)"));
// Only if the user hasn't clicked the cancel button
if(fileName.size() > 0)
{
unsigned int first_column;
// Get data from selected table
SqliteTableModel tableModel(this, pdb);
if(m_sQuery.isEmpty())
{
tableModel.setTable(ui->comboTable->currentText());
first_column = 1;
} else {
tableModel.setQuery(m_sQuery);
first_column = 0;
}
while(tableModel.canFetchMore())
tableModel.fetchMore();
// Prepare the quote and separating characters
QString quoteChar = ui->comboQuoteCharacter->currentText();
QString quotequoteChar = quoteChar + quoteChar;
QString sepChar = ui->comboFieldSeparator->currentText();
if(sepChar == tr("Tab")) sepChar = "\t";
QString newlineChar = "\n";
// Open file
QFile file(fileName);
if(file.open(QIODevice::WriteOnly))
{
// Open text stream to the file
QTextStream stream(&file);
// Put field names in first row if user wants to have them
if(ui->checkHeader->isChecked())
{
for(int i=first_column;i<tableModel.columnCount();i++)
{
stream << quoteChar << tableModel.headerData(i, Qt::Horizontal).toString() << quoteChar;
if(i < tableModel.columnCount() - 1)
stream << sepChar;
else
stream << newlineChar;
}
}
// Get and write actual data
for(int i=0;i<tableModel.totalRowCount();i++)
{
for(int j=first_column;j<tableModel.columnCount();j++)
{
QString content = tableModel.data(tableModel.index(i, j)).toString();
stream << quoteChar << content.replace(quoteChar, quotequoteChar) << quoteChar;
if(j < tableModel.columnCount() - 1)
stream << sepChar;
else
stream << newlineChar;
}
}
// Done writing the file
file.close();
QMessageBox::information(this, QApplication::applicationName(), tr("Export completed."));
QDialog::accept();
} else {
QMessageBox::warning(this, QApplication::applicationName(), tr("Could not open output file."));
}
}
}