annotate src/libqxt/qxtcsvmodel.cpp @ 69:689c82e7d07e

Fix documented Linux build requirements.
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 26 May 2015 12:44:10 +0200
parents 89997d853bf6
children 5923d569167b
rev   line source
1
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
1
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
2 /****************************************************************************
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
3 ** Copyright (c) 2006 - 2011, the LibQxt project.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
4 ** See the Qxt AUTHORS file for a list of authors and copyright holders.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
5 ** All rights reserved.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
6 **
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
7 ** Redistribution and use in source and binary forms, with or without
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
8 ** modification, are permitted provided that the following conditions are met:
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
9 ** * Redistributions of source code must retain the above copyright
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
10 ** notice, this list of conditions and the following disclaimer.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
11 ** * Redistributions in binary form must reproduce the above copyright
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
12 ** notice, this list of conditions and the following disclaimer in the
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
13 ** documentation and/or other materials provided with the distribution.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
14 ** * Neither the name of the LibQxt project nor the
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
15 ** names of its contributors may be used to endorse or promote products
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
16 ** derived from this software without specific prior written permission.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
17 **
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
18 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
19 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
20 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
21 ** DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
22 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
23 ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
24 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
25 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
26 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
27 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
28 **
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
29 ** <http://libqxt.org> <foundation@libqxt.org>
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
30 *****************************************************************************/
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
31
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
32 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
33 \class QxtCsvModel
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
34 \inmodule QxtCore
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
35 \brief The QxtCsvModel class provides a QAbstractTableModel for CSV Files
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
36 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
37
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
38
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
39
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
40 #include "qxtcsvmodel.h"
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
41 #include <QFile>
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
42 #include <QTextStream>
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
43 #include <QDebug>
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
44
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
45 class QxtCsvModelPrivate : public QxtPrivate<QxtCsvModel>
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
46 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
47 public:
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
48 QxtCsvModelPrivate() : csvData(), header(), maxColumn(0), quoteMode(QxtCsvModel::DefaultQuoteMode)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
49 {}
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
50 QXT_DECLARE_PUBLIC(QxtCsvModel)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
51
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
52 QList<QStringList> csvData;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
53 QStringList header;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
54 int maxColumn;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
55 QxtCsvModel::QuoteMode quoteMode;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
56 };
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
57
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
58 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
59 Creates an empty QxtCsvModel with parent \a parent.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
60 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
61 QxtCsvModel::QxtCsvModel(QObject *parent) : QAbstractTableModel(parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
62 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
63 QXT_INIT_PRIVATE(QxtCsvModel);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
64 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
65
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
66 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
67 Creates a QxtCsvModel with the parent \a parent and content loaded from \a file.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
68
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
69 See \a setSource for information on the \a withHeader and \a separator properties, or
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
70 if you need control over the quoting method or codec used to parse the file.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
71
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
72 \sa setSource
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
73 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
74 QxtCsvModel::QxtCsvModel(QIODevice *file, QObject *parent, bool withHeader, QChar separator) : QAbstractTableModel(parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
75 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
76 QXT_INIT_PRIVATE(QxtCsvModel);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
77 setSource(file, withHeader, separator);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
78 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
79
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
80 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
81 \overload
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
82
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
83 Creates a QxtCsvModel with the parent \a parent and content loaded from \a file.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
84
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
85 See \a setSource for information on the \a withHeader and \a separator properties, or
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
86 if you need control over the quoting method or codec used to parse the file.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
87
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
88 \sa setSource
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
89 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
90 QxtCsvModel::QxtCsvModel(const QString filename, QObject *parent, bool withHeader, QChar separator) : QAbstractTableModel(parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
91 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
92 QXT_INIT_PRIVATE(QxtCsvModel);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
93 QFile src(filename);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
94 setSource(&src, withHeader, separator);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
95 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
96
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
97 QxtCsvModel::~QxtCsvModel()
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
98 {}
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
99
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
100 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
101 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
102 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
103 int QxtCsvModel::rowCount(const QModelIndex& parent) const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
104 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
105 if (parent.row() != -1 && parent.column() != -1) return 0;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
106 return qxt_d().csvData.count();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
107 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
108
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
109 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
110 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
111 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
112 int QxtCsvModel::columnCount(const QModelIndex& parent) const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
113 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
114 if (parent.row() != -1 && parent.column() != -1) return 0;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
115 return qxt_d().maxColumn;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
116 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
117
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
118 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
119 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
120 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
121 QVariant QxtCsvModel::data(const QModelIndex& index, int role) const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
122 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
123 if(index.parent() != QModelIndex()) return QVariant();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
124 if(role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
125 if(index.row() < 0 || index.column() < 0 || index.row() >= rowCount())
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
126 return QVariant();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
127 const QStringList& row = qxt_d().csvData[index.row()];
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
128 if(index.column() >= row.length())
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
129 return QVariant();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
130 return row[index.column()];
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
131 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
132 return QVariant();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
133 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
134
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
135 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
136 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
137 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
138 QVariant QxtCsvModel::headerData(int section, Qt::Orientation orientation, int role) const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
139 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
140 if(section < qxt_d().header.count() && orientation == Qt::Horizontal && (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole))
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
141 return qxt_d().header[section];
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
142 else
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
143 return QAbstractTableModel::headerData(section, orientation, role);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
144 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
145
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
146 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
147 \overload
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
148
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
149 Reads in a CSV file from the provided \a file using \a codec.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
150 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
151 void QxtCsvModel::setSource(const QString filename, bool withHeader, QChar separator, QTextCodec* codec)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
152 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
153 QFile src(filename);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
154 setSource(&src, withHeader, separator, codec);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
155 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
156
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
157 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
158 Reads in a CSV file from the provided \a file using \a codec.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
159
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
160 The value of \a separator will be used to delimit fields, subject to the specified \a quoteMode.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
161 If \a withHeader is set to true, the first line of the file will be used to populate the model's
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
162 horizontal header.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
163
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
164 \sa quoteMode
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
165 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
166 void QxtCsvModel::setSource(QIODevice *file, bool withHeader, QChar separator, QTextCodec* codec)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
167 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
168 QxtCsvModelPrivate* d_ptr = &qxt_d();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
169 bool headerSet = !withHeader;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
170 if(!file->isOpen())
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
171 file->open(QIODevice::ReadOnly);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
172 if(withHeader)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
173 d_ptr->maxColumn = 0;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
174 else
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
175 d_ptr->maxColumn = d_ptr->header.size();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
176 d_ptr->csvData.clear();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
177 QStringList row;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
178 QString field;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
179 QChar quote;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
180 QChar ch, buffer(0);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
181 bool readCR = false;
37
0c05958d254c (issue 13, 6) Add constant index number column to data and view
Andre Heinecke <andre.heinecke@intevation.de>
parents: 17
diff changeset
182 int idx_nr = 1; // XXX added for retraceit
1
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
183 QTextStream stream(file);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
184 if(codec) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
185 stream.setCodec(codec);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
186 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
187 stream.setAutoDetectUnicode(true);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
188 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
189 while(!stream.atEnd()) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
190 if(buffer != QChar(0)) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
191 ch = buffer;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
192 buffer = QChar(0);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
193 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
194 stream >> ch;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
195 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
196 if(ch == '\n' && readCR)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
197 continue;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
198 else if(ch == '\r')
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
199 readCR = true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
200 else
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
201 readCR = false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
202 if(ch != separator && (ch.category() == QChar::Separator_Line || ch.category() == QChar::Separator_Paragraph || ch.category() == QChar::Other_Control)) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
203 row << field;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
204 field.clear();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
205 if(!row.isEmpty()) {
40
89997d853bf6 Ignore Index Nr. in filter list
Andre Heinecke <andre.heinecke@intevation.de>
parents: 37
diff changeset
206 row.insert(0, headerSet ? QString::number(idx_nr++) : QString::fromLatin1("#Index Nr.")); // XXX added for retraceit
1
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
207 if(!headerSet) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
208 d_ptr->header = row;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
209 headerSet = true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
210 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
211 d_ptr->csvData.append(row);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
212 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
213 if(row.length() > d_ptr->maxColumn) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
214 d_ptr->maxColumn = row.length();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
215 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
216 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
217 row.clear();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
218 } else if((d_ptr->quoteMode & DoubleQuote && ch == '"') || (d_ptr->quoteMode & SingleQuote && ch == '\'')) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
219 quote = ch;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
220 do {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
221 stream >> ch;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
222 if(ch == '\\' && d_ptr->quoteMode & BackslashEscape) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
223 stream >> ch;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
224 } else if(ch == quote) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
225 if(d_ptr->quoteMode & TwoQuoteEscape) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
226 stream >> buffer;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
227 if(buffer == quote) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
228 buffer = QChar(0);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
229 field.append(ch);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
230 continue;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
231 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
232 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
233 break;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
234 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
235 field.append(ch);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
236 } while(!stream.atEnd());
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
237 } else if(ch == separator) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
238 row << field;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
239 field.clear();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
240 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
241 field.append(ch);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
242 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
243 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
244 if(!field.isEmpty())
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
245 row << field;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
246 if(!row.isEmpty()) {
37
0c05958d254c (issue 13, 6) Add constant index number column to data and view
Andre Heinecke <andre.heinecke@intevation.de>
parents: 17
diff changeset
247 row.insert(0, headerSet ? QString::number(idx_nr++) : QString::fromLatin1("#Index Nr.")); // XXX added for relayit
1
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
248 if(!headerSet)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
249 d_ptr->header = row;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
250 else
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
251 d_ptr->csvData.append(row);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
252 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
253 file->close();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
254 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
255
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
256 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
257 Sets the horizontal headers of the model to the values provided in \a data.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
258 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
259 void QxtCsvModel::setHeaderData(const QStringList& data)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
260 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
261 qxt_d().header = data;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
262 emit headerDataChanged(Qt::Horizontal, 0, data.count());
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
263 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
264
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
265 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
266 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
267 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
268 bool QxtCsvModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant& value, int role)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
269 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
270 if(orientation != Qt::Horizontal) return false; // We don't support the vertical header
17
73efe717b944 Fix bug in csv model to allow changing headerdata
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1
diff changeset
271 if(role != Qt::DisplayRole && role != Qt::EditRole) return false; // We don't support any other roles
1
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
272 if(section < 0) return false; // Bogus input
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
273 while(section > qxt_d().header.size()) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
274 qxt_d().header << QString();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
275 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
276 qxt_d().header[section] = value.toString();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
277 emit headerDataChanged(Qt::Horizontal, section, section);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
278 return true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
279 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
280
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
281 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
282 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
283 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
284 bool QxtCsvModel::setData(const QModelIndex& index, const QVariant& data, int role)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
285 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
286 if (index.parent() != QModelIndex()) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
287
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
288 if(role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
289 if(index.row() >= rowCount() || index.column() >= columnCount() || index.row() < 0 || index.column() < 0) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
290 QStringList& row = qxt_d().csvData[index.row()];
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
291 while(row.length() <= index.column())
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
292 row << QString();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
293 row[index.column()] = data.toString();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
294 emit dataChanged(index, index);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
295 return true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
296 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
297 return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
298 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
299
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
300 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
301 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
302 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
303 bool QxtCsvModel::insertRow(int row, const QModelIndex& parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
304 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
305 return insertRows(row, 1, parent);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
306 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
307
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
308 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
309 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
310 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
311 bool QxtCsvModel::insertRows(int row, int count, const QModelIndex& parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
312 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
313 if (parent != QModelIndex() || row < 0) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
314 emit beginInsertRows(parent, row, row + count);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
315 QxtCsvModelPrivate& d_ptr = qxt_d();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
316 if(row >= rowCount()) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
317 for(int i = 0; i < count; i++) d_ptr.csvData << QStringList();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
318 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
319 for(int i = 0; i < count; i++) d_ptr.csvData.insert(row, QStringList());
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
320 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
321 emit endInsertRows();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
322 return true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
323 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
324
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
325 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
326 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
327 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
328 bool QxtCsvModel::removeRow(int row, const QModelIndex& parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
329 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
330 return removeRows(row, 1, parent);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
331 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
332
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
333 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
334 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
335 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
336 bool QxtCsvModel::removeRows(int row, int count, const QModelIndex& parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
337 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
338 if (parent != QModelIndex() || row < 0) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
339 if (row >= rowCount()) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
340 if (row + count >= rowCount()) count = rowCount() - row;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
341 emit beginRemoveRows(parent, row, row + count);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
342 QxtCsvModelPrivate& d_ptr = qxt_d();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
343 for (int i = 0;i < count;i++)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
344 d_ptr.csvData.removeAt(row);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
345 emit endRemoveRows();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
346 return true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
347 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
348
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
349 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
350 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
351 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
352 bool QxtCsvModel::insertColumn(int col, const QModelIndex& parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
353 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
354 return insertColumns(col, 1, parent);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
355 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
356
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
357 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
358 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
359 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
360 bool QxtCsvModel::insertColumns(int col, int count, const QModelIndex& parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
361 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
362 if (parent != QModelIndex() || col < 0) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
363 beginInsertColumns(parent, col, col + count - 1);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
364 QxtCsvModelPrivate& d_ptr = qxt_d();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
365 for(int i = 0; i < rowCount(); i++) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
366 QStringList& row = d_ptr.csvData[i];
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
367 while(col >= row.length()) row.append(QString());
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
368 for(int j = 0; j < count; j++) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
369 row.insert(col, QString());
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
370 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
371 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
372 for(int i = 0; i < count ;i++)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
373 d_ptr.header.insert(col, QString());
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
374 d_ptr.maxColumn += count;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
375 endInsertColumns();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
376 return true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
377 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
378
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
379 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
380 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
381 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
382 bool QxtCsvModel::removeColumn(int col, const QModelIndex& parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
383 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
384 return removeColumns(col, 1, parent);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
385 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
386
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
387 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
388 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
389 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
390 bool QxtCsvModel::removeColumns(int col, int count, const QModelIndex& parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
391 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
392 if (parent != QModelIndex() || col < 0) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
393 if (col >= columnCount()) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
394 if (col + count >= columnCount()) count = columnCount() - col;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
395 emit beginRemoveColumns(parent, col, col + count);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
396 QxtCsvModelPrivate& d_ptr = qxt_d();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
397 QString before, after;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
398 for(int i = 0; i < rowCount(); i++) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
399 for(int j = 0; j < count; j++) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
400 d_ptr.csvData[i].removeAt(col);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
401 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
402 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
403 for(int i = 0; i < count; i++)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
404 d_ptr.header.removeAt(col);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
405 emit endRemoveColumns();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
406 return true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
407 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
408
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
409 static QString qxt_addCsvQuotes(QxtCsvModel::QuoteMode mode, QString field)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
410 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
411 bool addDoubleQuotes = ((mode & QxtCsvModel::DoubleQuote) && field.contains('"'));
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
412 bool addSingleQuotes = ((mode & QxtCsvModel::SingleQuote) && field.contains('\''));
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
413 bool quoteField = (mode & QxtCsvModel::AlwaysQuoteOutput) || addDoubleQuotes || addSingleQuotes;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
414 if(quoteField && !addDoubleQuotes && !addSingleQuotes) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
415 if(mode & QxtCsvModel::DoubleQuote)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
416 addDoubleQuotes = true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
417 else if(mode & QxtCsvModel::SingleQuote)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
418 addSingleQuotes = true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
419 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
420 if(mode & QxtCsvModel::BackslashEscape) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
421 if(addDoubleQuotes)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
422 return '"' + field.replace("\\", "\\\\").replace("\"", "\\\"") + '"';
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
423 if(addSingleQuotes)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
424 return '\'' + field.replace("\\", "\\\\").replace("'", "\\'") + '\'';
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
425 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
426 if(addDoubleQuotes)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
427 return '"' + field.replace("\"", "\"\"") + '"';
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
428 if(addSingleQuotes)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
429 return '\'' + field.replace("'", "''") + '\'';
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
430 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
431 return field;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
432 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
433
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
434 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
435 Outputs the content of the model as a CSV file to the device \a dest using \a codec.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
436
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
437 Fields in the output file will be separated by \a separator. Set \a withHeader to true
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
438 to output a row of headers at the top of the file.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
439 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
440 void QxtCsvModel::toCSV(QIODevice* dest, bool withHeader, QChar separator, QTextCodec* codec) const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
441 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
442 const QxtCsvModelPrivate& d_ptr = qxt_d();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
443 int row, col, rows, cols;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
444 rows = rowCount();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
445 cols = columnCount();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
446 QString data;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
447 if(!dest->isOpen()) dest->open(QIODevice::WriteOnly | QIODevice::Truncate);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
448 QTextStream stream(dest);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
449 if(codec) stream.setCodec(codec);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
450 if(withHeader) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
451 data = "";
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
452 for(col = 0; col < cols; ++col) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
453 if(col > 0) data += separator;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
454 data += qxt_addCsvQuotes(d_ptr.quoteMode, d_ptr.header.at(col));
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
455 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
456 stream << data << endl;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
457 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
458 for(row = 0; row < rows; ++row)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
459 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
460 const QStringList& rowData = d_ptr.csvData[row];
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
461 data = "";
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
462 for(col = 0; col < cols; ++col) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
463 if(col > 0) data += separator;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
464 if(col < rowData.length())
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
465 data += qxt_addCsvQuotes(d_ptr.quoteMode, rowData.at(col));
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
466 else
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
467 data += qxt_addCsvQuotes(d_ptr.quoteMode, QString());;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
468 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
469 stream << data << endl;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
470 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
471 stream << flush;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
472 dest->close();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
473 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
474
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
475 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
476 \overload
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
477
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
478 Outputs the content of the model as a CSV file to the file specified by \a filename using \a codec.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
479
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
480 Fields in the output file will be separated by \a separator. Set \a withHeader to true
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
481 to output a row of headers at the top of the file.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
482 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
483 void QxtCsvModel::toCSV(const QString filename, bool withHeader, QChar separator, QTextCodec* codec) const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
484 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
485 QFile dest(filename);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
486 toCSV(&dest, withHeader, separator, codec);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
487 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
488
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
489 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
490 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
491 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
492 Qt::ItemFlags QxtCsvModel::flags(const QModelIndex& index) const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
493 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
494 return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
495 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
496
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
497 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
498 * Returns the current quoting mode.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
499 * \sa setQuoteMode
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
500 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
501 QxtCsvModel::QuoteMode QxtCsvModel::quoteMode() const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
502 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
503 return qxt_d().quoteMode;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
504 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
505
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
506 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
507 * Sets the current quoting mode. The default quoting mode is BothQuotes | BackslashEscape.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
508 *
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
509 * The quoting mode determines what kinds of quoting is used for reading and writing CSV files.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
510 * \sa quoteMode
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
511 * \sa QuoteOption
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
512 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
513 void QxtCsvModel::setQuoteMode(QuoteMode mode)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
514 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
515 qxt_d().quoteMode = mode;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
516 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
517
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
518 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
519 Sets the content of the cell at row \a row and column \a column to \a value.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
520
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
521 \sa text
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
522 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
523 void QxtCsvModel::setText(int row, int column, const QString& value)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
524 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
525 setData(index(row, column), value);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
526 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
527
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
528 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
529 Fetches the content of the cell at row \a row and column \a column.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
530
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
531 \sa setText
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
532 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
533 QString QxtCsvModel::text(int row, int column) const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
534 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
535 return data(index(row, column)).toString();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
536 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
537
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
538 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
539 Sets the content of the header for column \a column to \a value.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
540
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
541 \sa headerText
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
542 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
543 void QxtCsvModel::setHeaderText(int column, const QString& value)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
544 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
545 setHeaderData(column, Qt::Horizontal, value);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
546 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
547
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
548 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
549 Fetches the content of the cell at row \a row and column \a column.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
550
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
551 \sa setText
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
552 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
553 QString QxtCsvModel::headerText(int column) const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
554 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
555 return headerData(column, Qt::Horizontal).toString();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
556 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)