File indexing completed on 2025-01-19 04:46:30
0001 /* 0002 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #pragma once 0008 0009 #include <QAbstractTableModel> 0010 class QIODevice; 0011 class CsvParser; 0012 class QTextCodec; 0013 class QCsvModel : public QAbstractTableModel 0014 { 0015 Q_OBJECT 0016 0017 public: 0018 /** 0019 * Creates a new csv model. 0020 */ 0021 explicit QCsvModel(QObject *parent); 0022 0023 /** 0024 * Destroys the csv model. 0025 */ 0026 ~QCsvModel() override; 0027 0028 /** 0029 * Loads the data from the @p device into the model. 0030 */ 0031 bool load(QIODevice *device); 0032 0033 /** 0034 * Sets the character that is used for quoting. The default is '"'. 0035 */ 0036 void setTextQuote(QChar textQuote); 0037 0038 /** 0039 * Returns the character that is used for quoting. 0040 */ 0041 QChar textQuote() const; 0042 0043 /** 0044 * Sets the character that is used as delimiter for fields. 0045 * The default is ' '. 0046 */ 0047 void setDelimiter(QChar delimiter); 0048 0049 /** 0050 * Returns the delimiter that is used as delimiter for fields. 0051 */ 0052 QChar delimiter() const; 0053 0054 /** 0055 * Sets the row from where the parsing shall be started. 0056 * 0057 * Some csv files have some kind of header in the first line with 0058 * the column titles. To retrieve only the real data, set the start row 0059 * to '1' in this case. 0060 * 0061 * The default start row is 0. 0062 */ 0063 void setStartRow(uint startRow); 0064 0065 /** 0066 * Returns the start row. 0067 */ 0068 uint startRow() const; 0069 0070 /** 0071 * Sets the text codec that shall be used for parsing the csv list. 0072 * 0073 * The default is the system locale. 0074 */ 0075 void setTextCodec(QTextCodec *textCodec); 0076 0077 /** 0078 * Returns the text codec that is used for parsing the csv list. 0079 */ 0080 QTextCodec *textCodec() const; 0081 0082 /** 0083 * Inherited from QAbstractTableModel. 0084 */ 0085 int columnCount(const QModelIndex &parent = QModelIndex()) const override; 0086 0087 /** 0088 * Inherited from QAbstractTableModel. 0089 */ 0090 int rowCount(const QModelIndex &parent = QModelIndex()) const override; 0091 0092 /** 0093 * Inherited from QAbstractTableModel. 0094 */ 0095 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; 0096 0097 /** 0098 * Inherited from QAbstractTableModel. 0099 */ 0100 bool setData(const QModelIndex &index, const QVariant &data, int role = Qt::EditRole) override; 0101 0102 /** 0103 * Inherited from QAbstractTableModel. 0104 */ 0105 Qt::ItemFlags flags(const QModelIndex &index) const override; 0106 0107 Q_SIGNALS: 0108 /** 0109 * This signal is emitted whenever the model has loaded all data. 0110 */ 0111 void finishedLoading(); 0112 0113 private Q_SLOTS: 0114 void columnCountChanged(int columns); 0115 void rowCountChanged(int rows); 0116 void fieldChanged(const QString &data, int row, int column); 0117 0118 private: 0119 CsvParser *mParser = nullptr; 0120 QList<QString> mFieldIdentifiers; 0121 QMap<QPair<int, int>, QString> mFields; 0122 QIODevice *mDevice = nullptr; 0123 0124 int mRowCount = 0; 0125 int mColumnCount = 0; 0126 };