File indexing completed on 2024-05-12 04:20:13
0001 /** 0002 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved. 0003 * 0004 * This file is part of the KD Chart library. 0005 * 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #ifndef TABLEMODEL_H 0010 #define TABLEMODEL_H 0011 0012 #include <QVariant> 0013 #include <QVector> 0014 #include <QAbstractTableModel> 0015 #include "testtools_export.h" 0016 #include <QStringList> 0017 0018 /** TableModel uses a simple rectangular vector of vectors to represent a data 0019 table that can be displayed in regular Qt Interview views. 0020 Additionally, it provides a method to load CSV files exported by 0021 OpenOffice Calc in the default configuration. This allows to prepare test 0022 data using spreadsheet software. 0023 0024 It expects the CSV files in the subfolder ./modeldata. If the application 0025 is started from another location, it will ask for the location of the 0026 model data files. 0027 */ 0028 0029 class TESTTOOLS_EXPORT TableModel : public QAbstractTableModel 0030 { 0031 Q_OBJECT 0032 public: 0033 TableModel( QObject* parent = nullptr ); 0034 ~TableModel() override; 0035 0036 /** Return header data from the model. 0037 The model will use the first data row and the first data column of the 0038 physical data as source of column and row header data. This data is not 0039 exposed as model data, that means, the first model row and column will 0040 start at index (0, 0). 0041 */ 0042 QVariant headerData( int section, Qt::Orientation orientation, 0043 int role = Qt::DisplayRole ) const override; 0044 0045 int rowCount( const QModelIndex& parent = QModelIndex() ) const override; 0046 0047 int columnCount( const QModelIndex& parent = QModelIndex() ) const override; 0048 0049 QVariant data( const QModelIndex& index, int role = Qt::DisplayRole ) const override; 0050 0051 bool setData( const QModelIndex& index, const QVariant& value, int role = Qt::EditRole ) override; 0052 0053 /** Load the table from a comma separated file. 0054 * 0055 * The files are supposed to be Unicode (UTF8), have commas (',') as 0056 * delimiters, and quotes ('"') as text delimiters. All lines are expected 0057 * to provide the same number of fields. HINT: This is the default way 0058 * OO.o-Calc exports CSV files. 0059 * The cell data is expected to be floating point values, except for the 0060 * first row and the first column, where string values are expected (those 0061 * will be used as axis descriptors). If values cannot be converted to 0062 * qreals, their string representation will be used. 0063 * 0064 * @returns true if successful, false otherwise 0065 * 0066 * @sa titleText 0067 */ 0068 bool loadFromCSV( const QString& filename ); 0069 0070 /** 0071 * If both DataHasHorizontalHeaders and DataHasVerticalHeaders is 0072 * set true (that's the default setting) then loadFromCSV will interpret 0073 * the first field in the first row as title-text. 0074 * If no such field is found the loadFromCSV will set the title text to 0075 * an empty string. 0076 * 0077 * The text is stored and can be retrieved via titleText(), but the model 0078 * itself does nothing else with it: The calling application may use this 0079 * method and e.g. display the text as header or as title of the Legend 0080 * or as caption of the window ... 0081 * 0082 * @sa loadFromCSV 0083 */ 0084 const QString titleText() const { 0085 return m_titleText; 0086 } 0087 0088 /** 0089 * Setting the title text has no effect except that the text 0090 * can then be retrieved via titleText. 0091 * 0092 * TableModel is just storing this data but it does nothing 0093 * else with it, nor does Qt's IndeView model make use of it. 0094 */ 0095 void setTitleText( const QString& txt ) { 0096 m_titleText = txt; 0097 } 0098 0099 /** Make the model invalid, that is, provide no data. */ 0100 void clear(); 0101 0102 /** 0103 * Set to false if the data has no horizontal header 0104 */ 0105 void setDataHasHorizontalHeaders( bool value ) { 0106 m_dataHasHorizontalHeaders = value; 0107 } 0108 /** 0109 * Set to false if the data has no vertical header 0110 */ 0111 void setDataHasVerticalHeaders( bool value ) { 0112 m_dataHasVerticalHeaders = value; 0113 } 0114 /** 0115 * setSupplyHeaderData(false) allows to prevent the model from supplying header data, 0116 * even if parsing found any 0117 */ 0118 void setSupplyHeaderData( bool value ) { 0119 m_supplyHeaderData = value; 0120 } 0121 0122 protected: 0123 // the vector of rows: 0124 QVector< QVector<QVariant> > m_rows; 0125 0126 private: 0127 0128 // the header data: 0129 QStringList m_horizontalHeaderData; 0130 QStringList m_verticalHeaderData; 0131 QString m_titleText; 0132 bool m_dataHasHorizontalHeaders; 0133 bool m_dataHasVerticalHeaders; 0134 bool m_supplyHeaderData; 0135 }; 0136 0137 0138 #endif