Commit 63615f31 authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧

A couple of fixes on the FunctionsModel.

Visibility on the 2D functions from the console.

svn path=/trunk/KDE/kdeedu/kalgebra/; revision=798909
parent c4fd643c
......@@ -222,4 +222,14 @@ void ConsoleHtml::clear()
updateView();
}
void ConsoleHtml::modifyVariable(const QString& name, const Expression& exp)
{
a.variables()->modify(name, exp);
}
void ConsoleHtml::removeVariable(const QString & name)
{
a.variables()->remove(name);
}
#include "consolehtml.moc"
......@@ -86,6 +86,8 @@ class ConsoleHtml : public KHTMLPart
private slots:
void context(const QString&, const QPoint& p);
void modifyVariable(const QString& name, const Expression& exp);
void removeVariable(const QString& name);
private:
Analitza a;
void sendStatus(const QString& msg) { emit status(msg); }
......
......@@ -31,9 +31,12 @@ function::function()
: m_function(0), m_show(true), m_color(Qt::black)
{}
// #include "container.h"
function::function(const QString &name, const Expression& newFunc, const QColor& color)
: m_function(0), m_show(true), m_color(color), m_name(name)
{
// objectWalker(newFunc.tree());
QString firstBVar=newFunc.isLambda() ? newFunc.bvarList()[0] : "x";
if(firstBVar=="x")
m_function=new FunctionY(newFunc);
......@@ -155,3 +158,8 @@ QStringList function::supportedBoundedVars()
ret.append("q");
return ret;
}
const Expression& function::expression() const
{
return *analitza()->expression();
}
......@@ -113,6 +113,8 @@ class function
const QStringList& errors() const { return m_err; }
const Expression& expression() const;
/** Returns a list of the supported bvars */
static QStringList supportedBoundedVars();
private:
......
......@@ -29,10 +29,14 @@ FunctionImpl::FunctionImpl(const Expression& newFunc)
: points(0), m_deriv(0), m_last_viewport(QRect()), m_last_resolution(0), m_last_max_res(0)
{
func.setExpression(newFunc);
Expression deriv=func.derivative();
if(func.isCorrect())
m_deriv = new Expression(deriv);
else
if(func.isCorrect()) {
Expression deriv = func.derivative();
if(func.isCorrect())
m_deriv = new Expression(deriv);
else
func.flushErrors();
Q_ASSERT(func.isCorrect());
} else
func.flushErrors();
}
......
......@@ -100,6 +100,7 @@ bool FunctionsModel::addFunction(const function& func)
m_selectedRow=funclist.count()-1;
endInsertRows();
sendStatus(i18n("%1 function added", func.toString()));
emit functionModified(func.name(), func.expression());
}
return exists;
......@@ -116,6 +117,7 @@ bool FunctionsModel::removeRows(int row, int count, const QModelIndex & parent)
QList<function>::iterator it=funclist.begin()+row;
for(int i=count-1; i>=0; i--) {
emit functionRemoved(it->name());
it=funclist.erase(it);
}
emit endRemoveRows();
......@@ -180,6 +182,7 @@ bool FunctionsModel::setShown(const QString& f, bool shown)
return false;
}
//TODO remove me
function* FunctionsModel::editFunction(int num)
{
Q_ASSERT(num<funclist.count());
......@@ -191,26 +194,14 @@ void FunctionsModel::editFunction(int num, const function& func)
Q_ASSERT(num<funclist.count());
funclist[num]=func;
QModelIndex idx=index(num, 0), idxEnd=index(num, columnCount());
QModelIndex idx=index(num, 0), idxEnd=index(num, columnCount()-1);
emit dataChanged(idx, idxEnd);
emit functionModified(func.name(), func.expression());
// update_points();
// this->repaint(); emit update
}
bool FunctionsModel::setData(const QModelIndex & idx, const QVariant &value, int role)
{
if(role==Selection) setSelected(idx);
else if(role==Shown) {
bool isshown=value.toBool();
funclist[idx.row()].setShown(isshown);
QModelIndex idx1=index(idx.row(), 0), idxEnd=index(idx.row(), columnCount());
emit dataChanged(idx1, idxEnd);
}
return false;
}
bool FunctionsModel::editFunction(const QString& toChange, const function& func)
{
bool exist=false;
......@@ -221,14 +212,28 @@ bool FunctionsModel::editFunction(const QString& toChange, const function& func)
exist=true;
*it = func;
it->setName(toChange);
QModelIndex idx=index(i, 0), idxEnd=index(i, columnCount());
QModelIndex idx=index(i, 0), idxEnd=index(i, columnCount()-1);
emit dataChanged(idx, idxEnd);
emit functionModified(toChange, func.expression());
}
}
return exist;
}
bool FunctionsModel::setData(const QModelIndex & idx, const QVariant &value, int role)
{
if(role==Selection) setSelected(idx);
else if(role==Shown) {
bool isshown=value.toBool();
funclist[idx.row()].setShown(isshown);
QModelIndex idx1=index(idx.row(), 0), idxEnd=index(idx.row(), columnCount()-1);
emit dataChanged(idx1, idxEnd);
}
return false;
}
void FunctionsModel::updatePoints(int i, const QRect & viewport, int resolution)
{
Q_ASSERT(i<funclist.count());
......
......@@ -71,8 +71,6 @@ class FunctionsModel : public QAbstractTableModel
const function& currentFunction() const;
function& currentFunction();
bool hasSelection() const { return m_selectedRow>=0 && !funclist.isEmpty(); }
const_iterator constBegin() const { return funclist.constBegin(); }
......@@ -91,14 +89,21 @@ class FunctionsModel : public QAbstractTableModel
int selectedRow() const { return m_selectedRow; }
signals:
/** Emits a status when it changes. */
/** Emits a status message when something changes. */
void status(const QString &msg);
void functionModified(const QString& name, const Expression& e);
void functionRemoved(const QString& name);
private:
function& currentFunction();
QLineF slope(const QPointF& dp) const;
QPair<QPointF, QString> calcImage(const QPointF& dp);
QList<function> funclist;
int m_selectedRow;
};
#endif
......@@ -154,7 +154,7 @@ void Graph2D::drawCartesianAxes(QPainter *finestra)
finestra->drawLine(p, p+QPointF(0.,-3.));
}
for(double y=yini; y>=viewport.bottom(); y-=inc) { // ralletes y
for(double y=yini; y>=viewport.bottom(); y-=inc) { // ticks y
p = toWidget(QPointF(0., y));
if(m_squares)
finestra->drawLine(QPointF(0., p.y()), QPointF(width(), p.y()));
......@@ -568,9 +568,10 @@ void Graph2D::zoomOut()
void Graph2D::update(const QModelIndex & startIdx, const QModelIndex & endIdx)
{
Q_ASSERT(startIdx.isValid() && endIdx.isValid());
int start=startIdx.row(), end=endIdx.row();
for(int i=start; i<end; i++) {
for(int i=start; i<=end; i++) {
m_model->updatePoints(i, toBiggerRect(viewport), static_cast<int>(floor(resolucio)));
}
valid=false;
......
......@@ -214,6 +214,11 @@ KAlgebra::KAlgebra(QWidget *p) : KMainWindow(p)
//Ego's reminder
menuBar()->addMenu(helpMenu());
connect(b_funcsModel, SIGNAL(functionModified(const QString &, const Expression & )),
c_results, SLOT(modifyVariable(const QString & , const Expression & )));
connect(b_funcsModel, SIGNAL(functionRemoved(const QString &)),
c_results, SLOT(removeVariable(const QString &)));
connect(tabs, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)));
tabChanged(0);
}
......@@ -222,10 +227,11 @@ void KAlgebra::new_func()
{
QString name = b_funced->name();
function f(b_funced->name(), Expression(b_funced->text(), b_funced->isMathML()), b_funced->color());
if(!b_funced->editing()) {
b_funcsModel->addFunction(f);
} else {
if(b_funced->editing()) {
b_funcsModel->editFunction(name, f);
} else {
b_funcsModel->addFunction(f);
}
b_funced->setEditing(false);
......@@ -239,7 +245,8 @@ void KAlgebra::edit_func(const QModelIndex &idx)
{
b_tools->setTabText(1, i18n("&Editing"));
b_tools->setCurrentIndex(1);
b_funced->setText(b_funcsModel->data(idx).toString());
b_funced->setName(b_funcsModel->data(idx.sibling(idx.row(), 0)).toString());
b_funced->setText(b_funcsModel->data(idx.sibling(idx.row(), 1)).toString());
b_funced->setEditing(true);
b_funced->setFocus();
}
......
......@@ -20,6 +20,7 @@
#include "variables.h"
#include <KLocale>
#include <KApplication>
#include <KDebug>
VariablesModel::VariablesModel(QObject *parent)
: QAbstractTableModel(parent), m_vars(0)
......@@ -72,5 +73,4 @@ void VariablesModel::updateInformation()
reset();
}
#include "variablesmodel.moc"
......@@ -23,6 +23,7 @@
#include "operator.h"
class Variables;
class Expression;
/** Variables model is a model class that has a relation of all operators string with their VariableType. */
class VariablesModel : public QAbstractTableModel
......@@ -42,6 +43,7 @@ class VariablesModel : public QAbstractTableModel
public slots:
/** Updates the variables information */
void updateInformation();
private:
const Variables *m_vars;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment