File indexing completed on 2023-10-01 07:35:31
0001 /************************************************************************************* 0002 * Copyright (C) 2010-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 "plotsdictionarymodel.h" 0020 #include "plotsmodel.h" 0021 #include <analitza/expression.h> 0022 #include <analitza/expressionstream.h> 0023 #include <analitzaplot/functiongraph.h> 0024 #include <analitzaplot/plotsfactory.h> 0025 #include <QFile> 0026 #include <QCoreApplication> 0027 #include <QStandardPaths> 0028 #include <QDir> 0029 0030 using namespace Analitza; 0031 0032 PlotsDictionaryModel::PlotsDictionaryModel(QObject* parent) 0033 : QStandardItemModel(parent) 0034 , m_currentItem(-1) 0035 { 0036 setHorizontalHeaderLabels(QStringList() << QCoreApplication::translate("@title:column", "Name")); 0037 } 0038 0039 PlotsDictionaryModel::~PlotsDictionaryModel() 0040 {} 0041 0042 void PlotsDictionaryModel::createDictionary(const QString& file) 0043 { 0044 QFile device(file); 0045 if (device.open(QFile::ReadOnly | QFile::Text)) { 0046 QTextStream stream(&device); 0047 Analitza::ExpressionStream s(&stream); 0048 while(!s.atEnd()) { 0049 Analitza::Expression expression(s.next()); 0050 Q_ASSERT(expression.isCorrect()); 0051 Q_ASSERT(!expression.name().isEmpty()); 0052 QStringList comments = expression.comments(); 0053 0054 QStandardItem* item = new QStandardItem; 0055 item->setText(expression.name()); 0056 if(!comments.isEmpty()) 0057 item->setToolTip(QCoreApplication::translate("dictionary", comments.first().trimmed().toUtf8())); //see Messages.sh for more info 0058 item->setData(expression.toString(), ExpressionRole); 0059 item->setData(file, FileRole); 0060 appendRow(item); 0061 } 0062 } else 0063 qWarning() << "couldn't open" << file; 0064 } 0065 0066 void PlotsDictionaryModel::createAllDictionaries() 0067 { 0068 // QStringList res = KGlobal::dirs()->findAllResources("data", "libanalitza/plots/*.plots"); 0069 QStringList res = QStandardPaths::locateAll(QStandardPaths::AppLocalDataLocation, QStringLiteral("libanalitza/plots")); 0070 foreach(const QString& dir, res) { 0071 QDir d(dir); 0072 foreach(const QString& f, d.entryList(QStringList("*.plots"))) { 0073 createDictionary(f); 0074 } 0075 } 0076 } 0077 0078 PlotsModel* PlotsDictionaryModel::plotModel() 0079 { 0080 if(!m_plots) { 0081 m_plots = new PlotsModel(this); 0082 updatePlotsModel(); 0083 } 0084 return m_plots; 0085 } 0086 0087 int PlotsDictionaryModel::currentRow() const 0088 { 0089 return m_currentItem; 0090 } 0091 0092 void PlotsDictionaryModel::setCurrentRow(int row) 0093 { 0094 if(row == m_currentItem) 0095 return; 0096 m_currentItem = row; 0097 if(m_plots) 0098 updatePlotsModel(); 0099 } 0100 0101 void PlotsDictionaryModel::updatePlotsModel() 0102 { 0103 Q_ASSERT(m_plots); 0104 m_plots->clear(); 0105 if(m_currentItem<0) 0106 return; 0107 0108 QModelIndex idx = index(m_currentItem, 0); 0109 Analitza::Expression exp(idx.data(ExpressionRole).toString()); 0110 PlotBuilder req = PlotsFactory::self()->requestPlot(exp, Dim2D); 0111 0112 if (!req.canDraw()){ // preference is given to 2D 0113 PlotBuilder req = PlotsFactory::self()->requestPlot(exp, Dim3D); 0114 Q_ASSERT(req.canDraw()); 0115 m_plots->addPlot(req.create(Qt::blue, idx.data(Qt::DisplayRole).toString())); 0116 return; 0117 } 0118 m_plots->addPlot(req.create(Qt::blue, idx.data(Qt::DisplayRole).toString())); 0119 } 0120 0121 Analitza::Dimension PlotsDictionaryModel::dimension() 0122 { 0123 return Dimension(m_plots->index(0,0).data(PlotsModel::DimensionRole).toInt()); 0124 } 0125 0126 void PlotsDictionaryModel::setCurrentIndex(const QModelIndex& idx) 0127 { 0128 setCurrentRow(idx.row()); 0129 }