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

0001 /*************************************************************************************
0002  *  Copyright (C) 2013 by Punit Mehta <punit9462@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 "dictionarycollection.h"
0020 #include "ui_dictionarycollection.h"
0021 
0022 //Analitza includes
0023 #include <analitzagui/plotsview2d.h>
0024 #include <analitzagui/plotsview3d_es.h>
0025 #include <analitzaplot/plotsfactory.h>
0026 #include <analitzaplot/plotsmodel.h>
0027 #include <analitzaplot/functiongraph.h>
0028 #include <analitzaplot/plotitem.h>
0029 
0030 //Qt includes
0031 #include <QDir>
0032 #include <QFileDialog>
0033 
0034 //KDE includes
0035 #include <KStandardDirs>
0036 #include <KMessageBox>
0037 
0038 //local includes
0039 #include "datastore.h"
0040 #include "spacesmodel.h"
0041 #include "spaceitem.h"
0042 #include "plotsbuilder.h"
0043 
0044 using namespace Analitza;
0045 
0046 DictionaryCollection::DictionaryCollection(QWidget* parent): QDockWidget(parent)
0047   ,m_document(nullptr)
0048   ,m_currentDimension(DimAll)
0049   ,m_dictionaryModel(nullptr)
0050 {
0051     m_widget = new Ui::DictionaryCollectionWidget;
0052     m_widget->setupUi(this);
0053     connect(m_widget->AddButton,SIGNAL(pressed()),this,SLOT(addPlotClicked()));
0054     connect(m_widget->importDictionary,SIGNAL(pressed()),this,SLOT(importDictionary()));
0055     connect(m_widget->plotsView,SIGNAL(doubleClicked(QModelIndex)),this,SLOT(addPlotinSpace(QModelIndex)));
0056 }
0057 
0058 DictionaryCollection::~DictionaryCollection()
0059 {
0060     delete m_widget;
0061 }
0062 
0063 void DictionaryCollection::setDashboardWidget(Dashboard *dashboard)
0064 {
0065     m_dashboard=dashboard;
0066 }
0067 
0068 void DictionaryCollection::setDocument(DataStore *doc)
0069 {
0070     m_document=doc;
0071 }
0072 
0073 void DictionaryCollection::setDictionaryDataMap()
0074 {
0075     m_DictionaryPathName=m_dashboard->dictionaryDataMap();
0076 }
0077 
0078 void DictionaryCollection::setDefaultDictionaries()
0079 {
0080     QStringList dictionaryFileNames=m_DictionaryPathName.values();
0081     foreach(const QString &file,dictionaryFileNames) {
0082         m_widget->dictionaryNames->addItem(QFileInfo(file).baseName());
0083     }
0084     connect(m_widget->dictionaryNames,SIGNAL(currentIndexChanged(int)),this,SLOT(setDictionaryData(int)));
0085 }
0086 
0087 void DictionaryCollection::setDictionaryData(int ind)
0088 {
0089     if(ind==-1 || m_document==nullptr)
0090         return;
0091 
0092     PlotsDictionaryModel *model=new PlotsDictionaryModel;
0093     model->clear();
0094     m_dictionaryModel=model;
0095 
0096     QString fileName=m_widget->dictionaryNames->itemText(ind).append(".plots");
0097     QString dirPath=m_DictionaryPathName.key(fileName);
0098     dirPath.append(fileName);
0099     model->createDictionary(dirPath);
0100 
0101     // decide the space dimension
0102     m_currentDimension=m_document->spacesModel()->space(m_document->currentSpace())->dimension();
0103 
0104     for(int i=0;i<model->rowCount();i++) {
0105         for(int j=0;j<model->columnCount();j++) {
0106             QString str =model->data(model->index(i,j),PlotsDictionaryModel::ExpressionRole).toString();
0107             QString exp=str.right(str.length()-str.indexOf(":=")-2); //parsing of expression from the line
0108             PlotBuilder req = PlotsFactory::self()->requestPlot(Analitza::Expression(exp), m_currentDimension);
0109 
0110             //remove un-wanted plots (mismatched with the current dimnesion) from the model
0111             if (!req.canDraw()) {
0112                 model->removeRow(i);
0113                 i--;// to check on which position current data is replaced with the next row
0114             }
0115         }
0116     }
0117     m_widget->plotsView->setModel(model);
0118 }
0119 
0120 void DictionaryCollection::addPlotClicked()
0121 {
0122     if(m_widget->plotsView->selectionModel()==nullptr)
0123         return;
0124 
0125     QModelIndex ind=m_widget->plotsView->selectionModel()->currentIndex();
0126 
0127     if(!ind.isValid()) {
0128         return;
0129     }
0130     addPlotinSpace(ind);
0131 }
0132 
0133 void DictionaryCollection::addPlotinSpace(const QModelIndex& ind)
0134 {
0135     if(!ind.isValid()) {
0136         return;
0137     }
0138 
0139     QString str=m_dictionaryModel->data(ind,PlotsDictionaryModel::ExpressionRole).toString();
0140     QString exp=str.right(str.length()-str.indexOf(":=")-2); //parsing of expression from the line
0141     PlotBuilder req = PlotsFactory::self()->requestPlot(Analitza::Expression(exp), m_currentDimension);
0142 
0143     FunctionGraph *item = nullptr;
0144     item = req.create(randomFunctionColor(),m_dictionaryModel->data(ind).toString());
0145     m_document->plotsModel()->addPlot(item);
0146 
0147     emit mapDataChanged();
0148 }
0149 
0150 void DictionaryCollection::importDictionary()
0151 {
0152     auto const path = QFileDialog::getOpenFileName(this, i18n("Select Dictionary to import"), {},
0153                      i18n( "Dictionary Files (*.plots);;All Files (*)" ));
0154 
0155     if(path.isEmpty())
0156         return;
0157     int currentIndex=m_widget->dictionaryNames->count();
0158 
0159     const QFileInfo fi(path);
0160     m_widget->dictionaryNames->addItem(fi.baseName());
0161     m_widget->dictionaryNames->setCurrentIndex(currentIndex);
0162     m_DictionaryPathName.insertMulti(fi.path().append('/'), fi.fileName());
0163     setDictionaryData(currentIndex);
0164 }
0165 
0166 bool DictionaryCollection::conains(const QString &dictionaryname)
0167 {
0168     if(m_widget->dictionaryNames->findText(dictionaryname)==-1) {
0169         return false;
0170     }
0171     return true;
0172 }
0173 
0174 int DictionaryCollection::indexOf(const QString &dictionaryname)
0175 {
0176     return m_widget->dictionaryNames->findText(dictionaryname);
0177 }
0178 
0179 int DictionaryCollection::totalDictionaries()
0180 {
0181    return m_widget->dictionaryNames->count();
0182 }
0183 
0184 QTreeView* DictionaryCollection::dictionaryPlotsView()
0185 {
0186     return m_widget->plotsView;
0187 }
0188 
0189 void DictionaryCollection::setSpaceDimension(Analitza::Dimension dim) {
0190     m_currentDimension=dim;
0191     setDictionaryData(m_widget->dictionaryNames->currentIndex());
0192 }
0193 
0194 #include "moc_dictionarycollection.cpp"