Warning, file /graphics/kdiagram/examples/tools/TableModel.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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