File indexing completed on 2024-05-12 16:33:36

0001 /* This file is part of the KDE project
0002 
0003    Copyright 2010 Johannes Simon <johannes.simon@gmail.com>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #ifndef KCHART_TABLESOURCE_H
0022 #define KCHART_TABLESOURCE_H
0023 
0024 // Qt
0025 #include <QObject>
0026 #include <QAbstractItemModel>
0027 
0028 // KoChart
0029 #include "ChartShape.h"
0030 
0031 
0032 class QString;
0033 
0034 
0035 namespace KoChart {
0036 
0037 class Table
0038 {
0039     friend class TableSource;
0040 public:
0041     QAbstractItemModel *model() const { return m_model; }
0042     QString name() const { return m_name; }
0043 
0044 private:
0045     Table(const QString &name, QAbstractItemModel *model);
0046 
0047     QString m_name;
0048     QAbstractItemModel *m_model;
0049 };
0050 
0051 typedef QMap<QString, Table*> TableMap;
0052 
0053 class TableSource : public QObject
0054 {
0055     Q_OBJECT
0056 
0057 public:
0058     TableSource();
0059     ~TableSource();
0060 
0061     /**
0062      * Returns the table (model/name pair) associated with @a tableName.
0063      *
0064      * Note: The table name will be updated automatically when changed
0065      * by some "table source".
0066      */
0067     Table *get(const QString &tableName) const;
0068 
0069     /**
0070      * Returns the table (model/name pair) associated with @a model.
0071      *
0072      * Note: The table name will be updated automatically when changed
0073      * by some "table source".
0074      */
0075     Table *get(const QAbstractItemModel *model) const;
0076 
0077     /**
0078      * Returns a map of all name/table pairs in this source.
0079      * Mostly for debugging purposes.
0080      */
0081     TableMap tableMap() const;
0082 
0083     /**
0084      * Sets the KSpread::SheetAccessModel instance to use to get notified
0085      * about added/removed/renamed sheets in Calligra Sheets.
0086      *
0087      * This method is only relevant if the chart is embedded in Calligra Sheets or
0088      * somehow needs access to Calligra Sheets' sheets.
0089      */
0090     void setSheetAccessModel(QAbstractItemModel *model);
0091 
0092     /**
0093      * Adds a named model to this source.
0094      *
0095      * @return Pointer to new table (name/model pair) instance
0096      */
0097     Table *add(const QString &name, QAbstractItemModel *model);
0098 
0099     /**
0100      * Makes sure that the name of the specified table always stays unique.
0101      *
0102      * Use this for programatically added tables (like internal chart table).
0103      * Whenever another table with the same name is added/renamed, the
0104      * table specified will be renamed (to a sane similar name) to not
0105      * collide with the new name.
0106      */
0107     // TODO
0108     // void setRenameOnNameClash(const QString &tableName);
0109     // or
0110     // void setRenameOnNameClash(Table *table);
0111 
0112     /**
0113      * Removes a table from this source.
0114      */
0115     void remove(const QString &name);
0116 
0117     /**
0118      * Renames a table that has previously been added.
0119      */
0120     void rename(const QString &from, const QString &to);
0121 
0122     /**
0123      * Removes all tables and the sheetAccessModel.
0124      *
0125      * Note that all Table* pointers from this source are invalid after
0126      * calling this method!
0127      */
0128     void clear();
0129 
0130 Q_SIGNALS:
0131     /**
0132      * Emitted whenever a table is added to this source.
0133      */
0134     void tableAdded(Table *table);
0135 
0136     /**
0137      * Emitted whenever a table is removed from this source
0138      *
0139      * Note that right after this signal is emitted, the Table* instance
0140      * is deleted, thus you can't use it anymore afterwards.
0141      */
0142     void tableRemoved(Table *table);
0143 
0144 private Q_SLOTS:
0145     /**
0146      * Methods that react on changes in the SheetAccessModel ("sam")
0147      */
0148     void samColumnsInserted(QModelIndex, int, int);
0149     void samColumnsRemoved(QModelIndex, int, int);
0150     void samDataChanged(const QModelIndex &first, const QModelIndex &last);
0151     void samHeaderDataChanged(Qt::Orientation, int, int );
0152 
0153 private:
0154     class Private;
0155     Private *const d;
0156 };
0157 
0158 } // namespace KoChart
0159 
0160 #endif // KCHART_TABLESOURCE_H