File indexing completed on 2024-04-14 14:08:48

0001 /*************************************************************************************
0002  *  Copyright (C) 2012 by Percy Camilo T. Aucahuasi <percy.camilo.ta@gmail.com>      *
0003  *                                                                                   *
0004  *  This program is free software; you can redistribute it and/or                    *
0005  *  modify it under the terms of the GNU General Public License                      *
0006  *  as published by the Free Software Foundation; either version 2                   *
0007  *  of the License, or (at your option) any later version.                           *
0008  *                                                                                   *
0009  *  This program is distributed in the hope that it will be useful,                  *
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0012  *  GNU General Public License for more details.                                     *
0013  *                                                                                   *
0014  *  You should have received a copy of the GNU General Public License                *
0015  *  along with this program; if not, write to the Free Software                      *
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0017  *************************************************************************************/
0018 
0019 #include "datastore.h"
0020 
0021 //Analitza includes
0022 #include <analitza/variables.h>
0023 #include <analitza/expression.h>
0024 #include <analitzaplot/plotsmodel.h>
0025 #include <analitzaplot/planecurve.h>
0026 #include <analitzaplot/plotsdictionarymodel.h>
0027 
0028 //Qt includes
0029 #include <qitemselectionmodel.h>
0030 #include <QFile>
0031 #include <QString>
0032 #include <QUrl>
0033 #include <QFileDialog>
0034 
0035 //KDE includes
0036 #include <KMessageBox>
0037 #include <KLocalizedString>
0038 
0039 //local includes
0040 #include "spacesmodel.h"
0041 #include "spaceplotsproxymodel.h"
0042 #include "spaceitem.h"
0043 #include "plotseditor.h"
0044 
0045 using namespace Analitza;
0046 
0047 DataStore::DataStore(QObject* parent)
0048     : QObject(parent)
0049     , m_currentSpace(-1)
0050 {
0051     m_spacesModel = new SpacesModel(this);
0052     m_variables = new Analitza::Variables;
0053     m_plotsModel = new PlotsModel(this);
0054     
0055     m_plotsDictionaryModel = new PlotsDictionaryModel(this);
0056     
0057     connect(this, SIGNAL(spaceActivated(int)), SLOT(setCurrentSpace(int)));
0058 
0059     connect(m_plotsModel, SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(mapPlot(QModelIndex,int)));
0060 
0061     m_spacePlotsFilterProxyModel = new SpacePlotsFilterProxyModel(this);
0062     m_spacePlotsFilterProxyModel->setSourceModel(m_plotsModel);
0063 
0064     m_currentSelectionModel = new QItemSelectionModel(m_spacePlotsFilterProxyModel);
0065     connect(m_currentSelectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(selectCurrentPlot(QModelIndex)));
0066 
0067     m_currentSpaceSelectionModel = new QItemSelectionModel(m_spacesModel);
0068     
0069     connect(m_spacePlotsFilterProxyModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), SLOT(plotDataChanged(QModelIndex)));
0070 }
0071 
0072 DataStore::~DataStore()
0073 {
0074     delete m_variables;
0075 }
0076 
0077 bool DataStore::isMapped(SpaceItem* space, PlotItem* plot) const
0078 {
0079     return m_maps.values(space).contains(plot);
0080 }
0081 
0082 void DataStore::setCurrentSpace(int spaceidx)
0083 {
0084     if (m_spacesModel->index(spaceidx).isValid())
0085     {
0086         m_currentSpace = spaceidx;
0087         m_spacePlotsFilterProxyModel->setFilterSpaceDimension(m_spacesModel->space(spaceidx)->dimension());
0088         m_spacePlotsFilterProxyModel->setFilterSpace(m_spacesModel->space(spaceidx));
0089     } else {
0090         qWarning() << "wrong space" << spaceidx;
0091     }
0092 }
0093 
0094 void DataStore::mapPlot(const QModelIndex & parent, int start)
0095 {
0096     Q_ASSERT(m_currentSpace>=0);
0097     PlotItem* item = m_plotsModel->index(start, 0, parent).data(PlotsModel::PlotRole).value<PlotItem*>();
0098 
0099     m_maps.insertMulti(m_spacesModel->space(m_currentSpace), item);
0100 }
0101 
0102 void DataStore::selectCurrentPlot(const QModelIndex& curr)
0103 {
0104     if (!curr.isValid())
0105         return;
0106     
0107     int i = 0;
0108     switch (curr.data(PlotsModel::PlotRole).value<PlotItem*>()->coordinateSystem())
0109     {
0110         //TODO for 3d
0111         case Cartesian: i = 1; break;
0112         case Polar: i = 2; break;
0113         default : i=0; break;
0114     }
0115     emit gridStyleChanged(i);
0116 }
0117 
0118 void DataStore::plotDataChanged(const QModelIndex& topLeft)
0119 {
0120     int i = 0;
0121 
0122     switch (topLeft.data(PlotsModel::PlotRole).value<PlotItem*>()->coordinateSystem())
0123     {
0124         case Cartesian: i = 1; break;
0125         case Polar: i = 2; break;
0126         default : i=0; break;
0127     }
0128     emit gridStyleChanged(i);
0129 }
0130 
0131 void DataStore::removeCurrentSpace()
0132 {
0133     if (m_currentSpaceSelectionModel->hasSelection())
0134     {
0135 
0136         m_maps.remove(m_spacesModel->space(m_currentSpace));
0137 
0138         m_spacesModel->removeRow(m_currentSpace);
0139 
0140         m_currentSpaceSelectionModel->clear();
0141     }
0142 }
0143 
0144 void DataStore::unmapPlot(const QModelIndex & proxyindex )
0145 {
0146     QMap<SpaceItem*, PlotItem*>::iterator i = m_maps.begin();
0147 
0148     while (i != m_maps.end())
0149     {
0150         if (i.value() == proxyindex.data(PlotsModel::PlotRole).value<PlotItem*>())
0151         {
0152             m_maps.erase(i);
0153             break;
0154         }
0155         ++i;
0156     }
0157     m_spacePlotsFilterProxyModel->removeRow(proxyindex.row());
0158 }
0159 
0160 void DataStore::removeSpace(int row)
0161 {
0162     m_maps.remove(m_spacesModel->space(row));
0163     m_spacesModel->removeRow(row);
0164 }
0165 
0166 void DataStore::saveSpaceAsDictionary(QModelIndex ind)
0167 {
0168     const QString path = QFileDialog::getSaveFileName(nullptr, i18n( "Export the space as a Dictionary" ), {}, i18n("Plot-Dictionary Files (*.plots);;All Files (*)"));
0169     QFile file(path);
0170 
0171     if(!file.open(QFile::WriteOnly | QFile::Text)){
0172         qDebug() << "Error in writing";
0173         return;
0174     }
0175 
0176     QTextStream out(&file);
0177     if(m_maps.empty()) {
0178         KMessageBox::error(nullptr,i18n("Error while saving file, no plots available for this space"),i18n("Error while saving a dictionary"));
0179         return;
0180     }
0181 
0182     QList<Analitza::PlotItem*> itemList= m_maps.values(m_spacesModel->space(ind.row()));
0183 
0184     for(int i=0;i<itemList.size();i++) {
0185         QString name = itemList.at(i)->name();
0186         // we dont want the plots in the dictionary which dont have names !
0187         if(!name.isEmpty())
0188             out << name << " := " << itemList.at(i)->expression().toString() << "\n";
0189     }
0190     file.close();
0191 }
0192 
0193 void DataStore::clearAllData()
0194 {
0195     //delete prev data
0196     QItemSelectionModel *toBeRemovedCurrentSelectionModel = m_currentSelectionModel;
0197     QItemSelectionModel *toBeRemovedcurrentSpaceSelectionModel = m_currentSpaceSelectionModel;
0198     
0199     SpacesModel *toBeRemovedSpacesModel = m_spacesModel;
0200     PlotsModel *toBeRemovedPlotsModel = m_plotsModel;
0201 
0202     toBeRemovedCurrentSelectionModel->deleteLater();
0203     toBeRemovedcurrentSpaceSelectionModel->deleteLater();
0204     toBeRemovedPlotsModel->deleteLater();
0205     toBeRemovedSpacesModel->deleteLater();
0206     delete m_variables;
0207     
0208     //setup&bindnew data structs
0209     
0210     m_spacesModel = new SpacesModel(this);
0211     m_variables = new Analitza::Variables;
0212     m_plotsModel = new PlotsModel(this);
0213     
0214     connect(m_plotsModel, SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(mapPlot(QModelIndex,int)));
0215 
0216     m_spacePlotsFilterProxyModel->setSourceModel(m_plotsModel);
0217 
0218     m_currentSelectionModel = new QItemSelectionModel(m_spacePlotsFilterProxyModel);
0219     connect(m_currentSelectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(selectCurrentPlot(QModelIndex)));
0220 
0221     m_currentSpaceSelectionModel = new QItemSelectionModel(m_spacesModel);
0222 
0223     connect(m_spacePlotsFilterProxyModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), SLOT(plotDataChanged(QModelIndex)));
0224 }
0225 
0226 #include "moc_datastore.cpp"