File indexing completed on 2022-09-20 12:13:56

0001 /*************************************************************************************
0002  *  Copyright (C) 2007-2009 by Aleix Pol <aleixpol@kde.org>                          *
0003  *  Copyright (C) 2010-2012 by Percy Camilo T. Aucahuasi <percy.camilo.ta@gmail.com> *
0004  *                                                                                   *
0005  *  This program is free software; you can redistribute it and/or                    *
0006  *  modify it under the terms of the GNU General Public License                      *
0007  *  as published by the Free Software Foundation; either version 2                   *
0008  *  of the License, or (at your option) any later version.                           *
0009  *                                                                                   *
0010  *  This program 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                    *
0013  *  GNU General Public License for more details.                                     *
0014  *                                                                                   *
0015  *  You should have received a copy of the GNU General Public License                *
0016  *  along with this program; if not, write to the Free Software                      *
0017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0018  *************************************************************************************/
0019 
0020 #ifndef PLOTSMODEL_H
0021 #define PLOTSMODEL_H
0022 
0023 #include <QAbstractListModel>
0024 
0025 #include "plottingenums.h"
0026 #include "analitzaplot/analitzaplotexport.h"
0027 
0028 namespace Analitza
0029 {
0030 class PlotItem;
0031 class Variables;
0032 
0033 /**
0034  * \class PlotsModel
0035  * 
0036  * \ingroup AnalitzaPlotModule
0037  *
0038  * \brief Collection of many instances of PlotItem.
0039  *
0040  * This class contains all plots that will be rendered by plotters. Also it 
0041  * allows to show the plots in Qt views suchs QListView or QTreeView.
0042  */
0043 
0044 class ANALITZAPLOT_EXPORT PlotsModel : public QAbstractListModel
0045 {
0046 Q_OBJECT
0047 friend class PlotItem;
0048 public:
0049     enum Roles {
0050         DimensionRole = Qt::UserRole+1,
0051         PlotRole,
0052         DescriptionRole
0053     };
0054     
0055     explicit PlotsModel(QObject * parent = nullptr);
0056     ~PlotsModel() override;
0057 
0058     virtual Qt::ItemFlags flags(const QModelIndex & index) const override;
0059     virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
0060     virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override;
0061     virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override; // only title and check=visible
0062     virtual int rowCount(const QModelIndex & parent = QModelIndex()) const override;
0063     virtual int columnCount(const QModelIndex& parent) const override;
0064     virtual bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex()) override;
0065     QHash<int, QByteArray> roleNames() const override;
0066 
0067     void addPlot(PlotItem *it);
0068     void updatePlot(int row, PlotItem* it);
0069 
0070     /** @returns an identifier that's not present in the model */
0071     QString freeId() const;
0072 
0073     QModelIndex indexForName(const QString& name);
0074     Q_SCRIPTABLE void clear();
0075 
0076     void setResolution(int res);
0077 
0078     /**
0079      * Helper method to help easily add elements to the model
0080      */
0081     QStringList addFunction(const QString& expression, Analitza::Dimension dim, const QSharedPointer<Analitza::Variables>& vars);
0082 
0083     ///convenience function for QML
0084     Q_SCRIPTABLE QStringList addFunction(const QString& expression, int dim, const QSharedPointer<Analitza::Variables>& vars) { return addFunction(expression, Analitza::Dimension(dim), vars); }
0085 
0086     /**
0087      * Helper method to help easily add elements to the model
0088      */
0089     Q_SCRIPTABLE bool canAddFunction(const QString& expression, int dim, const QSharedPointer<Analitza::Variables>& vars);
0090 
0091 protected:
0092     void emitChanged(PlotItem* it);
0093 
0094 private:
0095     QList<PlotItem*> m_items;
0096     int m_resolution;
0097     int m_namingCount;
0098 };
0099 
0100 }
0101 
0102 #endif // PLOTSMODEL_H