forked from roniemartinez/qdsvtablemodel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqdsvtablemodel.cpp
More file actions
191 lines (169 loc) · 6.12 KB
/
qdsvtablemodel.cpp
File metadata and controls
191 lines (169 loc) · 6.12 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
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
/*************************************************************************
** Copyright (c) 2013 Ronie Martinez <ronmarti18@gmail.com> **
** All rights reserved. **
** **
** This library is free software; you can redistribute it and/or modify **
** it under the terms of the GNU Lesser General Public License as **
** published by the Free Software Foundation; either version 2.1 of the **
** License, or (at your option) any later version. **
** **
** This library is distributed in the hope that it will be useful, but **
** WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU **
** Lesser General Public License for more details. **
** **
** You should have received a copy of the GNU Lesser General Public **
** License along with this library; if not, write to the Free Software **
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA **
** 02110-1301 USA **
*************************************************************************/
#include <QTextStream>
#include <QRegExp>
#include <QFileInfo>
#include "qdsvtablemodel.h"
QDsvTableModel::QDsvTableModel(QObject *parent) :
QAbstractTableModel(parent)
{
}
int QDsvTableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return dsvMatrix.rowCount();
}
int QDsvTableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return dsvMatrix.columnCount();
}
QVariant QDsvTableModel::data(const QModelIndex &index, int role) const
{
if (index.isValid())
if (role == Qt::DisplayRole || role == Qt::EditRole)
return dsvMatrix.at(index.row(), index.column());
return QVariant();
}
bool QDsvTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole)
return dsvMatrix.setValue(index.row(), index.column(), value.toString());
return false;
}
Qt::ItemFlags QDsvTableModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = QAbstractItemModel::flags(index);
flags |= Qt::ItemIsEditable;
return flags;
}
void checkString(QString &temp, QList<QString> &list, QDsvMatrix<QString> &data,
const QChar &delimiter, const QChar &character = 0)
{
if(temp.count("\"")%2 == 0) {
if (temp.startsWith(QChar('\"')) && temp.endsWith( QChar('\"') ) ) {
temp.remove(QRegExp("^\"") );
temp.remove(QRegExp("\"$") );
}
temp.replace("\"\"", "\"");
list.append(temp);
if (character != delimiter) {
data.append(list);
list.clear();
}
temp.clear();
} else {
temp.append(character);
}
}
bool QDsvTableModel::loadFromFile(const QString &fileName, const QChar &delim)
{
dsvMatrix.clear();
QChar delimiter;
QFile file(fileName);
if (delim == 0) {
QString extension = QFileInfo(file).completeSuffix();
if (extension.toLower() == "csv")
delimiter = QChar(',');
else if (extension.toLower() == "tsv")
delimiter = QChar('\t');
else
return false; //unknown file extension = unknown delimiter
} else if (delim == QChar('"'))
return false; //the ONLY invalid delimiter is double quote (")
else
delimiter = delim;
if (!file.isOpen())
if (!file.open(QFile::ReadOnly|QFile::Text))
return false;
QString temp;
QChar lastCharacter;
QTextStream in(&file);
QList<QString> row;
while (true) {
QChar character;
in >> character;
if (in.atEnd()) {
if (lastCharacter == delimiter) //cases where last character is equal to the delimiter
temp = "";
checkString(temp, row, dsvMatrix, delimiter, QChar('\n'));
break;
} else if (character == delimiter || character == QChar('\n'))
checkString(temp, row, dsvMatrix, delimiter, character);
else {
temp.append(character);
lastCharacter = character;
}
}
file.close();
reset();
return true;
}
bool QDsvTableModel::loadFromData(const QByteArray &data, const QString &format)
{
//not yet implemented ...
Q_UNUSED(data);
Q_UNUSED(format);
return false;
}
bool QDsvTableModel::save(const QString &fileName, const QChar &delim,
QDsvTableModel::DsvWriteFlag flag)
{
QChar delimiter;
QFile file(fileName);
if (delim == 0) {
QString extension = QFileInfo(file).completeSuffix();
if (extension.toLower() == "csv")
delimiter = QChar(',');
else if (extension.toLower() == "tsv")
delimiter = QChar('\t');
else
return false; //unknown file extension = unknown delimiter
} else if (delim == QChar('"'))
return false; //the ONLY invalid delimiter is double quote (")
else
delimiter = delim;
if (!file.open(QIODevice::WriteOnly))
return false;
QTextStream out(&file);
out.setCodec("UTF-8");
out.setGenerateByteOrderMark(true);
for (int i = 0; i < dsvMatrix.rowCount(); ++i) {
for (int j = 0; j < dsvMatrix.columnCount(); ++j) {
QString s = dsvMatrix.at(i, j);
if (s.contains("\"")) {
s.replace("\"", "\"\"");
}
if (flag == UseDoubleQuotesIfNeeded) {
if (s.contains("\"") || s.contains(" ") || s.contains(delim))
s = "\"" + s + "\"";
} else {
s = "\"" + s + "\"";
}
out << s;
if (j == dsvMatrix.columnCount() - 1)
out << endl;
else
out << delimiter;
}
}
file.close();
return true;
}