annotate src/libqxt/qxtcsvmodel.cpp @ 29:c2aec2f33928

Add readme with some basic information.
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 06 May 2015 15:06:38 +0200
parents 73efe717b944
children 0c05958d254c
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;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
182 QTextStream stream(file);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
183 if(codec) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
184 stream.setCodec(codec);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
185 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
186 stream.setAutoDetectUnicode(true);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
187 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
188 while(!stream.atEnd()) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
189 if(buffer != QChar(0)) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
190 ch = buffer;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
191 buffer = QChar(0);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
192 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
193 stream >> ch;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
194 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
195 if(ch == '\n' && readCR)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
196 continue;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
197 else if(ch == '\r')
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
198 readCR = true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
199 else
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
200 readCR = false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
201 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
202 row << field;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
203 field.clear();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
204 if(!row.isEmpty()) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
205 if(!headerSet) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
206 d_ptr->header = row;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
207 headerSet = true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
208 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
209 d_ptr->csvData.append(row);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
210 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
211 if(row.length() > d_ptr->maxColumn) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
212 d_ptr->maxColumn = row.length();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
213 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
214 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
215 row.clear();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
216 } 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
217 quote = ch;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
218 do {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
219 stream >> ch;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
220 if(ch == '\\' && d_ptr->quoteMode & BackslashEscape) {
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 } else if(ch == quote) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
223 if(d_ptr->quoteMode & TwoQuoteEscape) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
224 stream >> buffer;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
225 if(buffer == quote) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
226 buffer = QChar(0);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
227 field.append(ch);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
228 continue;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
229 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
230 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
231 break;
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 field.append(ch);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
234 } while(!stream.atEnd());
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
235 } else if(ch == separator) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
236 row << field;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
237 field.clear();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
238 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
239 field.append(ch);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
240 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
241 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
242 if(!field.isEmpty())
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
243 row << field;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
244 if(!row.isEmpty()) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
245 if(!headerSet)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
246 d_ptr->header = row;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
247 else
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
248 d_ptr->csvData.append(row);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
249 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
250 file->close();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
251 }
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
254 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
255 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
256 void QxtCsvModel::setHeaderData(const QStringList& data)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
257 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
258 qxt_d().header = data;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
259 emit headerDataChanged(Qt::Horizontal, 0, data.count());
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
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
262 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
263 \reimp
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 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
266 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
267 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
268 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
269 if(section < 0) return false; // Bogus input
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
270 while(section > qxt_d().header.size()) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
271 qxt_d().header << QString();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
272 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
273 qxt_d().header[section] = value.toString();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
274 emit headerDataChanged(Qt::Horizontal, section, section);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
275 return true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
276 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
277
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
278 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
279 \reimp
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 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
282 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
283 if (index.parent() != QModelIndex()) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
284
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
285 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
286 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
287 QStringList& row = qxt_d().csvData[index.row()];
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
288 while(row.length() <= index.column())
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
289 row << QString();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
290 row[index.column()] = data.toString();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
291 emit dataChanged(index, index);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
292 return true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
293 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
294 return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
295 }
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
298 \reimp
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 bool QxtCsvModel::insertRow(int row, const QModelIndex& parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
301 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
302 return insertRows(row, 1, parent);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
303 }
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
306 \reimp
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 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
309 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
310 if (parent != QModelIndex() || row < 0) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
311 emit beginInsertRows(parent, row, row + count);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
312 QxtCsvModelPrivate& d_ptr = qxt_d();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
313 if(row >= rowCount()) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
314 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
315 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
316 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
317 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
318 emit endInsertRows();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
319 return true;
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
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
322 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
323 \reimp
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 bool QxtCsvModel::removeRow(int row, const QModelIndex& parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
326 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
327 return removeRows(row, 1, parent);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
328 }
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
331 \reimp
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 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
334 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
335 if (parent != QModelIndex() || row < 0) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
336 if (row >= rowCount()) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
337 if (row + count >= rowCount()) count = rowCount() - row;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
338 emit beginRemoveRows(parent, row, row + count);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
339 QxtCsvModelPrivate& d_ptr = qxt_d();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
340 for (int i = 0;i < count;i++)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
341 d_ptr.csvData.removeAt(row);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
342 emit endRemoveRows();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
343 return true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
344 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
345
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
346 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
347 \reimp
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 bool QxtCsvModel::insertColumn(int col, const QModelIndex& parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
350 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
351 return insertColumns(col, 1, parent);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
352 }
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
355 \reimp
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 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
358 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
359 if (parent != QModelIndex() || col < 0) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
360 beginInsertColumns(parent, col, col + count - 1);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
361 QxtCsvModelPrivate& d_ptr = qxt_d();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
362 for(int i = 0; i < rowCount(); i++) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
363 QStringList& row = d_ptr.csvData[i];
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
364 while(col >= row.length()) row.append(QString());
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
365 for(int j = 0; j < count; j++) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
366 row.insert(col, QString());
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
367 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
368 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
369 for(int i = 0; i < count ;i++)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
370 d_ptr.header.insert(col, QString());
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
371 d_ptr.maxColumn += count;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
372 endInsertColumns();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
373 return true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
374 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
375
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
376 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
377 \reimp
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 bool QxtCsvModel::removeColumn(int col, const QModelIndex& parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
380 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
381 return removeColumns(col, 1, parent);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
382 }
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
385 \reimp
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 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
388 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
389 if (parent != QModelIndex() || col < 0) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
390 if (col >= columnCount()) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
391 if (col + count >= columnCount()) count = columnCount() - col;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
392 emit beginRemoveColumns(parent, col, col + count);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
393 QxtCsvModelPrivate& d_ptr = qxt_d();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
394 QString before, after;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
395 for(int i = 0; i < rowCount(); i++) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
396 for(int j = 0; j < count; j++) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
397 d_ptr.csvData[i].removeAt(col);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
398 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
399 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
400 for(int i = 0; i < count; i++)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
401 d_ptr.header.removeAt(col);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
402 emit endRemoveColumns();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
403 return true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
404 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
405
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
406 static QString qxt_addCsvQuotes(QxtCsvModel::QuoteMode mode, QString field)
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 bool addDoubleQuotes = ((mode & QxtCsvModel::DoubleQuote) && field.contains('"'));
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
409 bool addSingleQuotes = ((mode & QxtCsvModel::SingleQuote) && field.contains('\''));
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
410 bool quoteField = (mode & QxtCsvModel::AlwaysQuoteOutput) || addDoubleQuotes || addSingleQuotes;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
411 if(quoteField && !addDoubleQuotes && !addSingleQuotes) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
412 if(mode & QxtCsvModel::DoubleQuote)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
413 addDoubleQuotes = true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
414 else if(mode & QxtCsvModel::SingleQuote)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
415 addSingleQuotes = true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
416 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
417 if(mode & QxtCsvModel::BackslashEscape) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
418 if(addDoubleQuotes)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
419 return '"' + field.replace("\\", "\\\\").replace("\"", "\\\"") + '"';
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
420 if(addSingleQuotes)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
421 return '\'' + field.replace("\\", "\\\\").replace("'", "\\'") + '\'';
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
422 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
423 if(addDoubleQuotes)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
424 return '"' + field.replace("\"", "\"\"") + '"';
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
425 if(addSingleQuotes)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
426 return '\'' + field.replace("'", "''") + '\'';
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
427 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
428 return field;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
429 }
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
432 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
433
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
434 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
435 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
436 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
437 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
438 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
439 const QxtCsvModelPrivate& d_ptr = qxt_d();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
440 int row, col, rows, cols;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
441 rows = rowCount();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
442 cols = columnCount();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
443 QString data;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
444 if(!dest->isOpen()) dest->open(QIODevice::WriteOnly | QIODevice::Truncate);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
445 QTextStream stream(dest);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
446 if(codec) stream.setCodec(codec);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
447 if(withHeader) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
448 data = "";
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
449 for(col = 0; col < cols; ++col) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
450 if(col > 0) data += separator;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
451 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
452 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
453 stream << data << endl;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
454 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
455 for(row = 0; row < rows; ++row)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
456 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
457 const QStringList& rowData = d_ptr.csvData[row];
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
458 data = "";
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
459 for(col = 0; col < cols; ++col) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
460 if(col > 0) data += separator;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
461 if(col < rowData.length())
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
462 data += qxt_addCsvQuotes(d_ptr.quoteMode, rowData.at(col));
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
463 else
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
464 data += qxt_addCsvQuotes(d_ptr.quoteMode, QString());;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
465 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
466 stream << data << endl;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
467 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
468 stream << flush;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
469 dest->close();
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
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
472 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
473 \overload
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 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
476
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
477 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
478 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
479 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
480 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
481 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
482 QFile dest(filename);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
483 toCSV(&dest, withHeader, separator, codec);
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
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
486 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
487 \reimp
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 Qt::ItemFlags QxtCsvModel::flags(const QModelIndex& index) const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
490 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
491 return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
492 }
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
495 * Returns the current quoting mode.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
496 * \sa setQuoteMode
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 QxtCsvModel::QuoteMode QxtCsvModel::quoteMode() const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
499 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
500 return qxt_d().quoteMode;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
501 }
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
504 * 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
505 *
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
506 * 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
507 * \sa quoteMode
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
508 * \sa QuoteOption
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
509 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
510 void QxtCsvModel::setQuoteMode(QuoteMode mode)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
511 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
512 qxt_d().quoteMode = mode;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
513 }
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
516 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
517
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
518 \sa text
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
519 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
520 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
521 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
522 setData(index(row, column), value);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
523 }
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
526 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
527
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
528 \sa setText
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
529 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
530 QString QxtCsvModel::text(int row, int column) const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
531 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
532 return data(index(row, column)).toString();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
533 }
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
536 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
537
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
538 \sa headerText
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
539 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
540 void QxtCsvModel::setHeaderText(int column, const QString& value)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
541 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
542 setHeaderData(column, Qt::Horizontal, value);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
543 }
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
546 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
547
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
548 \sa setText
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
549 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
550 QString QxtCsvModel::headerText(int column) const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
551 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
552 return headerData(column, Qt::Horizontal).toString();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
553 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)