diff --git a/analitzagui/operatorsmodel.cpp b/analitzagui/operatorsmodel.cpp index edece4b7..a65fb377 100644 --- a/analitzagui/operatorsmodel.cpp +++ b/analitzagui/operatorsmodel.cpp @@ -1,630 +1,628 @@ /************************************************************************************* * Copyright (C) 2007-2008 by Aleix Pol * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation; either version 2 * * of the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * *************************************************************************************/ #include "operatorsmodel.h" #include #include #include #include using Analitza::Operator; OperatorsModel::OperatorsModel(QObject *parent) : QAbstractTableModel(parent), m_vars(nullptr) { } QHash OperatorsModel::roleNames() const { auto ret = QAbstractTableModel::roleNames(); - ret.insert({ - {IsVariableRole, "isVariable"}, - {DescriptionRole, "description"} - }); + ret.insert(IsVariableRole, "isVariable"); + ret.insert(DescriptionRole, "description"); return ret; } QVariant OperatorsModel::data(const QModelIndex & index, int role) const { QVariant ret; if(role==Qt::DisplayRole) { if(index.row()keys()[var]; switch(index.column()) { case 0: ret=key; break; case 1: ret=m_vars->value(key)->toString(); break; } } } else if(role==Qt::FontRole && index.column()==1) { QFont f; f.setItalic(true); ret=f; } else if(role==DescriptionRole && index.column()==0) { Analitza::Operator oper((Analitza::Operator::OperatorType) (index.row()+1)); switch(index.column()) { case 0: ret=description(oper); break; } } else if(role==IsVariableRole && index.column()==0) { ret=index.row()count(); return count-2; } int OperatorsModel::columnCount(const QModelIndex &) const { return 4; } void OperatorsModel::updateInformation() { beginResetModel(); endResetModel(); } QString OperatorsModel::sample(const Analitza::Operator& oper) { QString funcname=oper.toString(); QString bounds; if(oper.isBounded()) { bounds=QCoreApplication::translate("Syntax for function bounding", " : var"); if(oper.operatorType()==Operator::sum || oper.operatorType()==Operator::product) bounds += QCoreApplication::translate("Syntax for function bounding values", "=from..to"); } QString sample = QCoreApplication::tr("%1(").arg(funcname); if(oper.nparams()<0) { return QCoreApplication::tr("%1... parameters, ...%2)").arg(sample, bounds); } else { for(int i=0; ib"); break; case Operator::lt: s = QCoreApplication::tr("Less than. lt(a,b)=a4 ? 1, ? 0 }"); break; case Operator::lt: s=QStringLiteral("piecewise { x<4 ? 1, ? 0 }"); break; case Operator::eq: s=QStringLiteral("piecewise { x=4 ? 1, ? 0 }"); break; case Operator::approx: s=QStringLiteral("piecewise { approx(x, 4) ? 1, ? 0 }"); break; case Operator::neq: s=QStringLiteral("piecewise { x!=4 ? 1, ? 0 }"); break; case Operator::geq: s=QStringLiteral("piecewise { x>=4 ? 1, ? 0 }"); break; case Operator::leq: s=QStringLiteral("piecewise { x<=4 ? 1, ? 0 }"); break; case Operator::_and: s=QStringLiteral("piecewise { and(x>-2, x<2) ? 1, ? 0 }"); break; case Operator::_or: s=QStringLiteral("piecewise { or(x>2, x>-2) ? 1, ? 0 }"); break; case Operator::_xor: s=QStringLiteral("piecewise { xor(x>0, x<3) ? 1, ? 0 }"); break; case Operator::implies: s=QStringLiteral("piecewise { implies(x<0, x<3) ? 1, ? 0 }"); break; case Operator::forall: s=QStringLiteral("piecewise { forall(t:t@list { true, false, false }) ? 1, ? 0 }"); break; case Operator::exists: s=QStringLiteral("piecewise { exists(t:t@list { true, false, false }) ? 1, ? 0 }"); break; case Operator::_not: s=QStringLiteral("piecewise { not(x>0) ? 1, ? 0 }"); break; case Operator::gcd: s=QStringLiteral("gcd(x, 3)"); break; case Operator::lcm: s=QStringLiteral("lcm(x, 4)"); break; case Operator::root: s=QStringLiteral("root(x, 2)"); break; case Operator::selector: s=QStringLiteral("scalarproduct(vector { 0, x }, vector { x, 0 })[1]"); break; case Operator::sum: s=QStringLiteral("x*sum(t*t:t=0..3)"); break; case Operator::product: s=QStringLiteral("product(t+t:t=1..3)"); break; case Operator::card: s=QStringLiteral("card(vector { x, 1, 2 })"); break; case Operator::scalarproduct: s=QStringLiteral("scalarproduct(vector { 0, x }, vector { x, 0 })[1]"); break; case Operator::diff: s=QStringLiteral("(diff(x^2:x))(x)"); break; case Operator::_union: s=QStringLiteral("union(list { 1, 2, 3 }, list { 4, 5, 6 })[rem(floor(x), 5)+3]"); break; case Operator::map: s=QStringLiteral("map(x->x+x, list { 1, 2, 3, 4, 5, 6 })[rem(floor(x), 5)+3]"); break; case Operator::filter: s=QStringLiteral("filter(u->rem(u, 2)=0, list { 2, 4, 3, 4, 8, 6 })[rem(floor(x), 5)+3]"); break; case Operator::transpose: s = QStringLiteral("transpose(matrix { matrixrow { 1, 2, 3, 4, 5, 6 } })[rem(floor(x), 5)+3][1]"); break; case Operator::real: s = QStringLiteral("real(x*i)"); break; case Operator::conjugate: s = QStringLiteral("conjugate(x*i)"); break; case Operator::arg: s = QStringLiteral("arg(x*i)"); break; case Operator::imaginary: s = QStringLiteral("imaginary(x*i)"); break; case Operator::factorial: case Operator::arcsech: case Operator::arcsec: case Operator::arccsch: case Operator::arccsc: // case Operator::arccoth: case Operator::sin: case Operator::cos: case Operator::tan: case Operator::sec: case Operator::csc: case Operator::cot: case Operator::sinh: case Operator::cosh: case Operator::tanh: case Operator::sech: case Operator::csch: case Operator::coth: case Operator::arcsin: case Operator::arccos: case Operator::arctan: case Operator::arccot: case Operator::arcsinh: case Operator::arccosh: // case Operator::arccsc: // case Operator::arccsch: // case Operator::arcsec: // case Operator::arcsech: case Operator::arctanh: case Operator::exp: case Operator::ln: case Operator::log: case Operator::abs: case Operator::floor: case Operator::ceiling: s=QStringLiteral("%1(x)").arg(o.toString()); break; case Operator::nOfOps: case Operator::none: case Operator::function: break; } return "x->"+s; } QModelIndex OperatorsModel::indexForOperatorName(const QString& id) const { Operator::OperatorType opt=Analitza::Operator::toOperatorType(id); if(opt==Operator::none) return QModelIndex(); else return index(opt-1, 0); } QString OperatorsModel::parameterHelp(const QModelIndex& index, int param, bool inbounds) const { Q_ASSERT(index.isValid()); QString ret; Analitza::Operator oper((Analitza::Operator::OperatorType) (index.row()+1)); QString funcname = oper.toString(); const int op=oper.nparams(); if(op == -1) { ret=QCoreApplication::translate("n-ary function prototype", "%1(..., par%2, ...)").arg(funcname).arg(param+1); } else { ret=standardFunctionCallHelp(funcname, param, op, inbounds, oper.isBounded()); } return ret; } QString OperatorsModel::standardFunctionCallHelp(const QString& funcname, int param, int paramcount, bool inbounds, bool isbounded) { QString sample = (param < paramcount || (inbounds && isbounded)) ? QCoreApplication::translate("Function name in function prototype", "%1(").arg(funcname) : QCoreApplication::translate("Uncorrect function name in function prototype", "%1(").arg(funcname); for(int i=0; i%1").arg(current); sample += current; if(i%1").arg(p); sample += p; } return sample+')'; } QString OperatorsModel::lastWord(int pos, const QString& exp) { int act=pos-1; for(; act>=0 && exp[act].isLetter(); act--) {} return exp.mid(act+1, pos-act-1); } /*QString OperatorsModel::operToString(const Operator& op) const { QStandardItem *it; for(int i=0; idata(Qt::EditRole).toInt()==op.operatorType()) { return item(i,0)->data(Qt::EditRole).toString(); } } return QString(); }*/ diff --git a/analitzaplot/plotsmodel.cpp b/analitzaplot/plotsmodel.cpp index aad45e44..98b5e297 100644 --- a/analitzaplot/plotsmodel.cpp +++ b/analitzaplot/plotsmodel.cpp @@ -1,307 +1,305 @@ /************************************************************************************* * Copyright (C) 2007-2009 by Aleix Pol * * Copyright (C) 2010-2012 by Percy Camilo T. Aucahuasi * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation; either version 2 * * of the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * *************************************************************************************/ #include "plotsmodel.h" #include "plotsfactory.h" #include "plotitem.h" #include "analitza/analitzautils.h" #include #include #include #include #include #include #include using namespace Analitza; PlotsModel::PlotsModel(QObject* parent) : QAbstractListModel(parent) , m_resolution(500) , m_namingCount(0) {} PlotsModel::~PlotsModel() { clear(); } QHash PlotsModel::roleNames() const { auto ret = QAbstractListModel::roleNames(); - ret.insert({ - { DescriptionRole, "description" } - }); + ret.insert(DescriptionRole, "description"); return ret; } void PlotsModel::clear() { if (!m_items.isEmpty()) { beginRemoveRows(QModelIndex(), 0, m_items.count()-1); qDeleteAll(m_items); m_items.clear(); endRemoveRows(); } } Qt::ItemFlags PlotsModel::flags(const QModelIndex & index) const { if(index.isValid()) return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEditable; else return Qt::NoItemFlags; } QVariant PlotsModel::headerData(int section, Qt::Orientation orientation, int role) const { if(role==Qt::DisplayRole && orientation==Qt::Horizontal) { switch(section) { case 0: return QCoreApplication::translate("@title:column", "Name"); case 1: return QCoreApplication::translate("@title:column", "Plot"); } } return QAbstractListModel::headerData(section, orientation, role); } QVariant PlotsModel::data(const QModelIndex & index, int role) const { if(!index.isValid() || index.row()>=m_items.count()) return QVariant(); PlotItem *tmpcurve = m_items.at(index.row()); switch(role) { case Qt::DisplayRole: case Qt::EditRole: switch(index.column()) { case 0: return tmpcurve->name(); case 1: return tmpcurve->display(); } break; case Qt::DecorationRole: if(index.column()==0) { QPixmap ico(16, 16); ico.fill(tmpcurve->color()); return QIcon(ico); } else return QIcon::fromTheme(tmpcurve->iconName()); case Qt::ToolTipRole: return tmpcurve->name(); case Qt::StatusTipRole: return tmpcurve->typeName(); case Qt::CheckStateRole: if(index.column()==0) return tmpcurve->isVisible() ? Qt::Checked : Qt::Unchecked; break; case DimensionRole: return int(tmpcurve->spaceDimension()); case PlotRole: return QVariant::fromValue(tmpcurve); case DescriptionRole: return tmpcurve->display(); } return QVariant(); } bool PlotsModel::setData(const QModelIndex& index, const QVariant& value, int role) { if (!index.isValid()) return false; switch(role) { case Qt::EditRole: switch(index.column()) { case 0: { //FIXME: actually I think the name should be stored in the model instead of plotItem //if there was another plot with that name, it shouldn't be accepted QString newName = value.toString(); if(!newName.isEmpty()) { m_items[index.row()]->setName(newName); emit dataChanged(index, index); } return !newName.isEmpty(); } case 1: { Analitza::Expression valexp = AnalitzaUtils::variantToExpression(value); PlotItem* it = m_items[index.row()]; PlotBuilder plot = PlotsFactory::self()->requestPlot(valexp, it->spaceDimension()); if (plot.canDraw()) { if (m_items[index.row()]->expression() != valexp) { QColor color=it->color(); QString name=it->name(); delete m_items[index.row()]; m_items[index.row()] = plot.create(color, name); } emit dataChanged(index, index); return true; } return false; } } //fallthrough case Qt::CheckStateRole: m_items[index.row()]->setVisible(value.toBool()); return true; case Qt::DecorationRole: m_items[index.row()]->setColor(value.value()); return true; } return false; } int PlotsModel::rowCount(const QModelIndex & parent) const { if(parent.isValid()) return 0; else return m_items.size(); } int PlotsModel::columnCount(const QModelIndex& parent) const { Q_UNUSED(parent); return 2; } bool PlotsModel::removeRows(int row, int count, const QModelIndex& parent) { Q_ASSERT(rowsetModel(this); m_items.append(it); if(FunctionGraph* g=dynamic_cast(it)) g->setResolution(m_resolution); endInsertRows(); m_namingCount++; } void PlotsModel::updatePlot(int row, PlotItem* it) { it->setModel(this); delete m_items[row]; m_items[row]=it; QModelIndex idx = index(row); emit dataChanged(idx, idx); } void PlotsModel::emitChanged(PlotItem* it) { int row = m_items.indexOf(it); QModelIndex idx = index(row); emit dataChanged(idx, idx); } QModelIndex PlotsModel::indexForName(const QString& name) { const int rows = rowCount(); for(int i=0; i(m_items[i]); if(g) g->setResolution(res); } } static QColor randomFunctionColor() { return QColor::fromHsv(QRandomGenerator::global()->bounded(255), 255, 225); } QStringList PlotsModel::addFunction(const QString& expression, Dimension dim, const QSharedPointer& vars) { Analitza::Expression e(expression, Analitza::Expression::isMathML(expression)); QString fname; do { fname = freeId(); } while(vars && vars->contains(fname)); QColor fcolor = randomFunctionColor(); QStringList err; PlotBuilder req = PlotsFactory::self()->requestPlot(e, dim, vars); if(req.canDraw()) { auto it = req.create(fcolor, fname); if(it->isCorrect()) addPlot(it); else { err = it->errors(); delete it; } } return err; } bool Analitza::PlotsModel::canAddFunction(const QString& expression, int _dim, const QSharedPointer& vars) { const Analitza::Dimension dim = static_cast(_dim); Analitza::Expression e(expression, Analitza::Expression::isMathML(expression)); PlotBuilder req = PlotsFactory::self()->requestPlot(e, dim, vars); return req.canDraw(); } diff --git a/declarative/analitzadeclarativeplugin.cpp b/declarative/analitzadeclarativeplugin.cpp index 423ff76b..b520b22a 100644 --- a/declarative/analitzadeclarativeplugin.cpp +++ b/declarative/analitzadeclarativeplugin.cpp @@ -1,39 +1,44 @@ /************************************************************************************* * Copyright (C) 2012 by Aleix Pol * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation; either version 2 * * of the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * *************************************************************************************/ #include "analitzadeclarativeplugin.h" #include "analitzawrapper.h" #include #include #include #include #include #include #include void AnalitzaDeclarativePlugin::registerTypes(const char* uri) { qmlRegisterType(uri, 1, 0, "Analitza"); qmlRegisterType(uri, 1, 0, "Expression"); qmlRegisterType(uri, 1, 0, "Graph2DView"); qmlRegisterType(uri, 1, 1, "Graph3DView"); qmlRegisterType(uri, 1, 0, "PlotsModel"); qmlRegisterType(uri, 1, 0, "VariablesModel"); qmlRegisterType(uri, 1, 0, "OperatorsModel"); + +#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) + qmlRegisterInterface("Analitza::Variables"); +#else qmlRegisterInterface(uri, 1); +#endif }