annotate src/libqxt/qxtcsvmodel.cpp @ 112:9daf778feaf1 1.4

Fix usage of WIN32 macro. With c++11 this is no longer set. _WIN32 is the better macro to use anyway.
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 08 Dec 2016 15:34:07 +0100
parents 5923d569167b
children
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:
81
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
48 QxtCsvModelPrivate() : csvData(), header(), header_tt(), maxColumn(0), quoteMode(QxtCsvModel::DefaultQuoteMode)
1
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;
81
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
54 QStringList header_tt;
1
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
55 int maxColumn;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
56 QxtCsvModel::QuoteMode quoteMode;
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
60 Creates an empty QxtCsvModel with parent \a parent.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
61 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
62 QxtCsvModel::QxtCsvModel(QObject *parent) : QAbstractTableModel(parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
63 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
64 QXT_INIT_PRIVATE(QxtCsvModel);
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
68 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
69
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
70 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
71 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
72
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
73 \sa setSource
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
74 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
75 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
76 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
77 QXT_INIT_PRIVATE(QxtCsvModel);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
78 setSource(file, withHeader, separator);
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
82 \overload
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
83
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
84 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
85
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
86 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
87 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
88
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
89 \sa setSource
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
90 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
91 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
92 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
93 QXT_INIT_PRIVATE(QxtCsvModel);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
94 QFile src(filename);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
95 setSource(&src, withHeader, separator);
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
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
98 QxtCsvModel::~QxtCsvModel()
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
102 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
103 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
104 int QxtCsvModel::rowCount(const QModelIndex& parent) const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
105 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
106 if (parent.row() != -1 && parent.column() != -1) return 0;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
107 return qxt_d().csvData.count();
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
111 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
112 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
113 int QxtCsvModel::columnCount(const QModelIndex& parent) const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
114 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
115 if (parent.row() != -1 && parent.column() != -1) return 0;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
116 return qxt_d().maxColumn;
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
120 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
121 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
122 QVariant QxtCsvModel::data(const QModelIndex& index, int role) const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
123 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
124 if(index.parent() != QModelIndex()) return QVariant();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
125 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
126 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
127 return QVariant();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
128 const QStringList& row = qxt_d().csvData[index.row()];
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
129 if(index.column() >= row.length())
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
130 return QVariant();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
131 return row[index.column()];
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
132 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
133 return QVariant();
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
137 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
138 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
139 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
140 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
141 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
142 return qxt_d().header[section];
81
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
143 else if (section < qxt_d().header_tt.count() && role == Qt::ToolTipRole) {
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
144 return qxt_d().header_tt[section];
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
145 } else
1
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
146 return QAbstractTableModel::headerData(section, orientation, role);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
147 }
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
150 \overload
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
151
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
152 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
153 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
154 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
155 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
156 QFile src(filename);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
157 setSource(&src, withHeader, separator, codec);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
158 }
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
161 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
162
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
163 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
164 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
165 horizontal header.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
166
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
167 \sa quoteMode
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
168 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
169 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
170 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
171 QxtCsvModelPrivate* d_ptr = &qxt_d();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
172 bool headerSet = !withHeader;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
173 if(!file->isOpen())
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
174 file->open(QIODevice::ReadOnly);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
175 if(withHeader)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
176 d_ptr->maxColumn = 0;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
177 else
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
178 d_ptr->maxColumn = d_ptr->header.size();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
179 d_ptr->csvData.clear();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
180 QStringList row;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
181 QString field;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
182 QChar quote;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
183 QChar ch, buffer(0);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
184 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
185 int idx_nr = 1; // XXX added for retraceit
1
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
186 QTextStream stream(file);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
187 if(codec) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
188 stream.setCodec(codec);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
189 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
190 stream.setAutoDetectUnicode(true);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
191 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
192 while(!stream.atEnd()) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
193 if(buffer != QChar(0)) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
194 ch = buffer;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
195 buffer = QChar(0);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
196 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
197 stream >> ch;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
198 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
199 if(ch == '\n' && readCR)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
200 continue;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
201 else if(ch == '\r')
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
202 readCR = true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
203 else
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
204 readCR = false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
205 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
206 row << field;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
207 field.clear();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
208 if(!row.isEmpty()) {
40
89997d853bf6 Ignore Index Nr. in filter list
Andre Heinecke <andre.heinecke@intevation.de>
parents: 37
diff changeset
209 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
210 if(!headerSet) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
211 d_ptr->header = row;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
212 headerSet = true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
213 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
214 d_ptr->csvData.append(row);
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 if(row.length() > d_ptr->maxColumn) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
217 d_ptr->maxColumn = row.length();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
218 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
219 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
220 row.clear();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
221 } 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
222 quote = ch;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
223 do {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
224 stream >> ch;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
225 if(ch == '\\' && d_ptr->quoteMode & BackslashEscape) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
226 stream >> ch;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
227 } else if(ch == quote) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
228 if(d_ptr->quoteMode & TwoQuoteEscape) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
229 stream >> buffer;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
230 if(buffer == quote) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
231 buffer = QChar(0);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
232 field.append(ch);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
233 continue;
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 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
236 break;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
237 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
238 field.append(ch);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
239 } while(!stream.atEnd());
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
240 } else if(ch == separator) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
241 row << field;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
242 field.clear();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
243 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
244 field.append(ch);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
245 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
246 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
247 if(!field.isEmpty())
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
248 row << field;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
249 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
250 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
251 if(!headerSet)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
252 d_ptr->header = row;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
253 else
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
254 d_ptr->csvData.append(row);
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 file->close();
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
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
259 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
260 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
261 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
262 void QxtCsvModel::setHeaderData(const QStringList& data)
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 qxt_d().header = data;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
265 emit headerDataChanged(Qt::Horizontal, 0, data.count());
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
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
268 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
269 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
270 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
271 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
272 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
273 if(section < 0) return false; // Bogus input
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
274 while(section > qxt_d().header.size()) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
275 qxt_d().header << QString();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
276 }
81
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
277 while(section >= qxt_d().header_tt.size()) {
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
278 qxt_d().header_tt << QString();
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
279 }
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
280 qDebug() << "Sect " << section << " size " << qxt_d().header_tt.size();
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
281 if(orientation != Qt::Horizontal) return false; // We don't support the vertical header
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
282 if(role == Qt::ToolTipRole) {
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
283 qxt_d().header_tt[section] = value.toString();
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
284 emit headerDataChanged(Qt::Horizontal, section, section);
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
285 return true;
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
286 }
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
287 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
288 qxt_d().header[section] = value.toString();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
289 emit headerDataChanged(Qt::Horizontal, section, section);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
290 return true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
291 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
292
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 \reimp
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 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
297 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
298 if (index.parent() != QModelIndex()) return false;
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 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
301 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
302 QStringList& row = qxt_d().csvData[index.row()];
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
303 while(row.length() <= index.column())
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
304 row << QString();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
305 row[index.column()] = data.toString();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
306 emit dataChanged(index, index);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
307 return true;
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 return false;
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
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 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
314 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
315 bool QxtCsvModel::insertRow(int row, const QModelIndex& parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
316 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
317 return insertRows(row, 1, parent);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
318 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
319
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 \reimp
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 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
324 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
325 if (parent != QModelIndex() || row < 0) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
326 emit beginInsertRows(parent, row, row + count);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
327 QxtCsvModelPrivate& d_ptr = qxt_d();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
328 if(row >= rowCount()) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
329 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
330 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
331 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
332 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
333 emit endInsertRows();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
334 return true;
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
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 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
339 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
340 bool QxtCsvModel::removeRow(int row, const QModelIndex& parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
341 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
342 return removeRows(row, 1, parent);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
343 }
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 \reimp
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 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
349 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
350 if (parent != QModelIndex() || row < 0) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
351 if (row >= rowCount()) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
352 if (row + count >= rowCount()) count = rowCount() - row;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
353 emit beginRemoveRows(parent, row, row + count);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
354 QxtCsvModelPrivate& d_ptr = qxt_d();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
355 for (int i = 0;i < count;i++)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
356 d_ptr.csvData.removeAt(row);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
357 emit endRemoveRows();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
358 return true;
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
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 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
363 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
364 bool QxtCsvModel::insertColumn(int col, const QModelIndex& parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
365 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
366 return insertColumns(col, 1, parent);
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 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
370 \reimp
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 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
373 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
374 if (parent != QModelIndex() || col < 0) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
375 beginInsertColumns(parent, col, col + count - 1);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
376 QxtCsvModelPrivate& d_ptr = qxt_d();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
377 for(int i = 0; i < rowCount(); i++) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
378 QStringList& row = d_ptr.csvData[i];
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
379 while(col >= row.length()) row.append(QString());
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
380 for(int j = 0; j < count; j++) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
381 row.insert(col, QString());
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 }
81
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
384 for(int i = 0; i < count ;i++) {
1
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
385 d_ptr.header.insert(col, QString());
81
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
386 d_ptr.header_tt.insert(col, QString());
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
387 }
1
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
388 d_ptr.maxColumn += count;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
389 endInsertColumns();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
390 return true;
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
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
393 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
394 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
395 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
396 bool QxtCsvModel::removeColumn(int col, const QModelIndex& parent)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
397 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
398 return removeColumns(col, 1, parent);
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
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 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
403 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
404 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
405 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
406 if (parent != QModelIndex() || col < 0) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
407 if (col >= columnCount()) return false;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
408 if (col + count >= columnCount()) count = columnCount() - col;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
409 emit beginRemoveColumns(parent, col, col + count);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
410 QxtCsvModelPrivate& d_ptr = qxt_d();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
411 QString before, after;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
412 for(int i = 0; i < rowCount(); i++) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
413 for(int j = 0; j < count; j++) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
414 d_ptr.csvData[i].removeAt(col);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
415 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
416 }
81
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
417 for(int i = 0; i < count; i++) {
1
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
418 d_ptr.header.removeAt(col);
81
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
419 d_ptr.header_tt.removeAt(col);
5923d569167b Make Header tooltips configurable
Andre Heinecke <andre.heinecke@intevation.de>
parents: 40
diff changeset
420 }
1
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
421 emit endRemoveColumns();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
422 return true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
423 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
424
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
425 static QString qxt_addCsvQuotes(QxtCsvModel::QuoteMode mode, QString field)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
426 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
427 bool addDoubleQuotes = ((mode & QxtCsvModel::DoubleQuote) && field.contains('"'));
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
428 bool addSingleQuotes = ((mode & QxtCsvModel::SingleQuote) && field.contains('\''));
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
429 bool quoteField = (mode & QxtCsvModel::AlwaysQuoteOutput) || addDoubleQuotes || addSingleQuotes;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
430 if(quoteField && !addDoubleQuotes && !addSingleQuotes) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
431 if(mode & QxtCsvModel::DoubleQuote)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
432 addDoubleQuotes = true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
433 else if(mode & QxtCsvModel::SingleQuote)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
434 addSingleQuotes = true;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
435 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
436 if(mode & QxtCsvModel::BackslashEscape) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
437 if(addDoubleQuotes)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
438 return '"' + field.replace("\\", "\\\\").replace("\"", "\\\"") + '"';
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
439 if(addSingleQuotes)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
440 return '\'' + field.replace("\\", "\\\\").replace("'", "\\'") + '\'';
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
441 } else {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
442 if(addDoubleQuotes)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
443 return '"' + field.replace("\"", "\"\"") + '"';
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
444 if(addSingleQuotes)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
445 return '\'' + field.replace("'", "''") + '\'';
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
446 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
447 return field;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
448 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
449
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
450 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
451 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
452
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
453 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
454 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
455 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
456 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
457 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
458 const QxtCsvModelPrivate& d_ptr = qxt_d();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
459 int row, col, rows, cols;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
460 rows = rowCount();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
461 cols = columnCount();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
462 QString data;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
463 if(!dest->isOpen()) dest->open(QIODevice::WriteOnly | QIODevice::Truncate);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
464 QTextStream stream(dest);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
465 if(codec) stream.setCodec(codec);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
466 if(withHeader) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
467 data = "";
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
468 for(col = 0; col < cols; ++col) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
469 if(col > 0) data += separator;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
470 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
471 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
472 stream << data << endl;
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 for(row = 0; row < rows; ++row)
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 const QStringList& rowData = d_ptr.csvData[row];
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
477 data = "";
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
478 for(col = 0; col < cols; ++col) {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
479 if(col > 0) data += separator;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
480 if(col < rowData.length())
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
481 data += qxt_addCsvQuotes(d_ptr.quoteMode, rowData.at(col));
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
482 else
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
483 data += qxt_addCsvQuotes(d_ptr.quoteMode, QString());;
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 stream << data << endl;
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 stream << flush;
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
488 dest->close();
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
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 \overload
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 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
495
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
496 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
497 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
498 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
499 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
500 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
501 QFile dest(filename);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
502 toCSV(&dest, withHeader, separator, codec);
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
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 \reimp
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
507 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
508 Qt::ItemFlags QxtCsvModel::flags(const QModelIndex& index) const
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 return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
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
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 * Returns the current quoting mode.
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
515 * \sa setQuoteMode
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 QxtCsvModel::QuoteMode QxtCsvModel::quoteMode() const
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 return qxt_d().quoteMode;
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
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 * 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
524 *
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
525 * 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
526 * \sa quoteMode
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
527 * \sa QuoteOption
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 void QxtCsvModel::setQuoteMode(QuoteMode mode)
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 qxt_d().quoteMode = mode;
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
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 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
536
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
537 \sa text
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 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
540 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
541 setData(index(row, column), value);
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
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 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
546
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
547 \sa setText
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 QString QxtCsvModel::text(int row, int column) const
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 return data(index(row, column)).toString();
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
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 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
556
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
557 \sa headerText
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
558 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
559 void QxtCsvModel::setHeaderText(int column, const QString& value)
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
560 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
561 setHeaderData(column, Qt::Horizontal, value);
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
562 }
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
563
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
564 /*!
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
565 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
566
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
567 \sa setText
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
568 */
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
569 QString QxtCsvModel::headerText(int column) const
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
570 {
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
571 return headerData(column, Qt::Horizontal).toString();
7a2637c3eb83 Add csv parser from libqxt
Andre Heinecke <andre.heinecke@intevation.de>
parents:
diff changeset
572 }
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)