Commit 9ddc8395 authored by Fredrik Edemar's avatar Fredrik Edemar

* If you edit a function and something goes wrong (e.g boundcheck), the...

* If you edit a function and something goes wrong (e.g boundcheck), the function will not be deleted.
* Implement a new function called "reparse" in Parser which reparse a function. This is needed for editing constant in a better way. Plus, it can be useful in the future.
* Make "equation" in the editfunctionpage to be focused first when the dialog is shown.
* Use the default linewidth in keditparametric.
* Only functions with just anti-derivates enabled are allowed to use "y". Otherwise, a recursive error will happen.


CCMAIL:kdmoeller@foni.net
CCMAIL:bmlmessmer@web.de
raabe@kde.org

svn path=/trunk/kdeedu/kmplot/; revision=320244
parent 7ee1e78d
......@@ -490,11 +490,19 @@ void MainDlg::slotQuickEdit(const QString& tmp_f_str )
view->parser()->fixFunctionName(f_str);
int index = view->parser()->addfkt( f_str );
if( index == -1 )
if (index==-1)
{
view->parser()->errmsg();
m_quickEdit->setFocus();
m_quickEdit->selectAll();
return;
}
if ( f_str.contains('y') != 0)
{
KMessageBox::error( this, i18n( "Recursive function is not allowed"));
m_quickEdit->setFocus();
m_quickEdit->selectAll();
view->parser()->delfkt( index );
return;
}
......@@ -502,9 +510,12 @@ void MainDlg::slotQuickEdit(const QString& tmp_f_str )
view->parser()->fktext[index].f1_color = view->parser()->fktext[index].color0;
view->parser()->fktext[index].f2_color = view->parser()->fktext[index].color0;
view->parser()->fktext[index].anti_color = view->parser()->fktext[index].color0;
view->parser()->fktext[index].f1_linewidth = view->parser()->fktext[index].linewidth;
view->parser()->fktext[index].f2_linewidth = view->parser()->fktext[index].linewidth;
view->parser()->fktext[index].anti_linewidth = view->parser()->fktext[index].linewidth;
view->parser()->fktext[index].f_mode = 1;
view->parser()->fktext[index].anti_precision=Settings::relativeStepWidth();
view->parser()->fktext[ index ].extstr = f_str;
view->parser()->fktext[index].extstr = f_str;
view->parser()->getext( index );
m_quickEdit->clear();
m_modified = true;
......
......@@ -274,15 +274,15 @@ void View::plotfkt(int ix, QPainter *pDC)
return ;
}
else if(fktmode=='y') return ;
p_mode=0;
pDC->setPen(pen);
while(1)
{
{
k=0;
ke=m_parser->fktext[ix].k_anz;
do
{
{
if ( p_mode == 3 && stop_calculating)
break;
if( m_parser->fktext[ ix ].use_slider == -1 )
......@@ -291,6 +291,7 @@ void View::plotfkt(int ix, QPainter *pDC)
m_parser->setparameter(ix, sliders[ m_parser->fktext[ix].use_slider ]->slider->value() );
mflg=2;
bool forward_direction = true;
if ( p_mode == 3)
{
if ( m_parser->fktext[ix].anti_use_precision )
......@@ -304,6 +305,9 @@ void View::plotfkt(int ix, QPainter *pDC)
}
else
x=dmin;
if ( p_mode != 0 || m_parser->fktext[ix].f_mode) // if not the function is hidden
while (x>=dmin && x<=dmax)
{
......@@ -698,6 +702,12 @@ void View::mousePressEvent(QMouseEvent *e)
if ( m_popupmenushown>0)
return;
if (!stop_calculating && isDrawing) //stop drawing anti-derivatives
{
stop_calculating = true;
return;
}
if ( zoom_mode==1 ) //rectangle zoom
{
zoom_mode=4;
......@@ -796,11 +806,7 @@ void View::mousePressEvent(QMouseEvent *e)
return;
}
if (!stop_calculating && isDrawing) //stop drawing anti-derivatives
{
stop_calculating = true;
return;
}
if(e->button()==RightButton) //clicking with the right mouse button
{
g=tlgy/5.;
......
......@@ -51,7 +51,6 @@
#include "editfunction.h"
#include "editfunction.moc"
#include "View.h"
#include "xparser.h"
#include "editfunctionpage.h"
#include "editderivativespage.h"
#include "editantiderivativepage.h"
......@@ -78,6 +77,7 @@ void EditFunction::initDialog( int index )
m_index = index;
if( m_index == -1 ) clearWidgets();
else setWidgets();
editfunctionpage->equation->setFocus();
}
void EditFunction::clearWidgets()
......@@ -166,19 +166,30 @@ void EditFunction::accept()
else if( functionHas2Arguments() && KMessageBox::warningYesNo( this, i18n( "Function has 2 arguments, but you did not specify any parameter values.\nDo you want to continue anyway?" ), i18n( "Missing Parameter Values" ) ) != KMessageBox::Yes )
return;
// if we are editing an existing function, first delete the old one
if( m_index != -1 )
{
m_parser->delfkt( m_index );
m_index = -1;
}
QString f_str(functionItem() );
m_parser->fixFunctionName(f_str);
int index;
if( m_index != -1 ) //when editing a function:
{
index = m_index; //use the right function-index
QString old_fstr = m_parser->ufkt[index].fstr;
m_parser->ufkt[index].fstr = f_str;
m_parser->reparse(index); //reparse the funcion
if ( m_parser->errmsg() != 0)
{
m_parser->ufkt[index].fstr = old_fstr;
m_parser->reparse(index);
this->raise();
showPage(0);
editfunctionpage->equation->setFocus();
editfunctionpage->equation->selectAll();
return;
}
}
else
index = m_parser->addfkt( f_str ); //create a new function otherwise
int index = m_parser->addfkt( f_str );
if( index == -1 )
if( index == -1)
{
m_parser->errmsg();
this->raise();
......@@ -187,167 +198,178 @@ void EditFunction::accept()
editfunctionpage->equation->selectAll();
return;
}
m_parser->fktext[ index ].extstr = f_str;
m_parser->getext( index );
XParser::FktExt tmp_fktext; //all settings are saved here until we know that no errors have appeared
tmp_fktext.extstr = f_str;
if( editfunctionpage->customRange->isChecked() )
{
m_parser->fktext[ index ].str_dmin = editfunctionpage->min->text();
m_parser->fktext[ index ].dmin = m_parser->eval( editfunctionpage->min->text() );
tmp_fktext.str_dmin = editfunctionpage->min->text();
tmp_fktext.dmin = m_parser->eval( editfunctionpage->min->text() );
if (m_parser->errmsg() != 0)
{
showPage(0);
editfunctionpage->min->setFocus();
editfunctionpage->min->selectAll();
m_parser->delfkt( index );
if( m_index == -1 ) m_parser->delfkt(index);
return;
}
m_parser->fktext[ index ].str_dmax = editfunctionpage->max->text();
m_parser->fktext[ index ].dmax = m_parser->eval( editfunctionpage->max->text() );
tmp_fktext.str_dmax= editfunctionpage->max->text();
tmp_fktext.dmax = m_parser->eval( editfunctionpage->max->text() );
if (m_parser->errmsg() != 0)
{
showPage(0);
editfunctionpage->max->setFocus();
editfunctionpage->max->selectAll();
m_parser->delfkt( index );
if( m_index == -1 ) m_parser->delfkt(index);
return;
}
if ( m_parser->fktext[ index ].dmin >= m_parser->fktext[ index ].dmax)
if ( tmp_fktext.dmin >= tmp_fktext.dmax)
{
KMessageBox::error(this,i18n("The minimum range value must be lower than the maximum range value"));
showPage(0);
editfunctionpage->min->setFocus();
editfunctionpage->min->selectAll();
m_parser->delfkt( index );
if( m_index == -1 ) m_parser->delfkt(index);
return;
}
if ( m_parser->fktext[ index ].dmin<View::xmin || m_parser->fktext[ index ].dmax>View::xmax )
if ( tmp_fktext.dmin<View::xmin || tmp_fktext.dmax>View::xmax )
{
KMessageBox::error(this,i18n("Please insert a minimum and maximum range between %1 and %2").arg(View::xmin).arg(View::xmax) );
showPage(0);
editfunctionpage->min->setFocus();
editfunctionpage->min->selectAll();
m_parser->delfkt( index );
if( m_index == -1 ) m_parser->delfkt(index);
return;
}
}
else
{
m_parser->fktext[ index ].str_dmin ="0";
m_parser->fktext[ index ].dmin = 0;
m_parser->fktext[ index ].str_dmax = "0";
m_parser->fktext[ index ].dmax = 0;
tmp_fktext.str_dmin ="0";
tmp_fktext.dmin = 0;
tmp_fktext.str_dmax = "0";
tmp_fktext.dmax = 0;
}
if( editfunctionpage->useList->isChecked() )
m_parser->fktext[ index ].use_slider = -1;
tmp_fktext.use_slider = -1;
else
m_parser->fktext[ index ].use_slider = editfunctionpage->listOfSliders->currentItem();
tmp_fktext.use_slider = editfunctionpage->listOfSliders->currentItem();
if (editantiderivativepage->showAntiderivative->isChecked() )
{
double initx = m_parser->eval(editantiderivativepage->txtInitX->text());
m_parser->fktext[index].startx = initx;
m_parser->fktext[index].str_startx = editantiderivativepage->txtInitX->text();
tmp_fktext.startx = initx;
tmp_fktext.str_startx = editantiderivativepage->txtInitX->text();
if (m_parser->err != 0)
{
KMessageBox::error(this,i18n("Please insert a valid x-value"));
showPage(2);
editantiderivativepage->txtInitX->setFocus();
editantiderivativepage->txtInitX->selectAll();
m_parser->delfkt( index );
if( m_index == -1 ) m_parser->delfkt(index);
return;
}
double inity = m_parser->eval(editantiderivativepage->txtInitY->text());
m_parser->fktext[index].starty = inity;
m_parser->fktext[index].str_starty = editantiderivativepage->txtInitY->text();
tmp_fktext.starty = inity;
tmp_fktext.str_starty = editantiderivativepage->txtInitY->text();
if (m_parser->err != 0)
{
KMessageBox::error(this,i18n("Please insert a valid y-value"));
showPage(2);
editantiderivativepage->txtInitY->setFocus();
editantiderivativepage->txtInitY->selectAll();
m_parser->delfkt( index );
if( m_index == -1 ) m_parser->delfkt(index);
return;
}
if ( m_parser->fktext[ index ].dmin!=m_parser->fktext[ index ].dmax && ( initx<m_parser->fktext[ index ].dmin || initx>m_parser->fktext[ index ].dmax) )
if ( tmp_fktext.dmin!=tmp_fktext.dmax && ( initx<tmp_fktext.dmin || initx>tmp_fktext.dmax) )
{
KMessageBox::error(this,i18n("Please insert an initial x-value in the range between %1 and %2").arg(m_parser->fktext[ index ].dmin).arg( m_parser->fktext[ index ].dmax) );
KMessageBox::error(this,i18n("Please insert an initial x-value in the range between %1 and %2").arg(tmp_fktext.dmin).arg( tmp_fktext.dmax) );
showPage(2);
editantiderivativepage->txtInitX->setFocus();
editantiderivativepage->txtInitX->selectAll();
m_parser->delfkt( index );
if( m_index == -1 ) m_parser->delfkt(index);
return;
}
if ( m_parser->fktext[ index ].dmin!=m_parser->fktext[ index ].dmax && ( initx<View::xmin || initx>View::xmax) )
if ( tmp_fktext.dmin!=tmp_fktext.dmax && ( initx<View::xmin || initx>View::xmax) )
{
KMessageBox::error(this,i18n("Please insert an initial x-value in the range between %1 and %2").arg(View::xmax).arg( View::xmax) );
showPage(2);
editantiderivativepage->txtInitX->setFocus();
editantiderivativepage->txtInitX->selectAll();
m_parser->delfkt( index );
if( m_index == -1 ) m_parser->delfkt(index);
return;
}
m_parser->fktext[index].anti_mode = 1;
m_parser->fktext[ index ].anti_color = editantiderivativepage->colorAntiderivative->color().rgb();
m_parser->fktext[index].anti_use_precision = editantiderivativepage->customPrecision->isChecked();
m_parser->fktext[index].anti_precision = editantiderivativepage->precision->value();
m_parser->fktext[index].anti_linewidth = editantiderivativepage->lineWidthAntiderivative->value();
tmp_fktext.anti_mode = 1;
tmp_fktext.anti_color = editantiderivativepage->colorAntiderivative->color().rgb();
tmp_fktext.anti_use_precision = editantiderivativepage->customPrecision->isChecked();
tmp_fktext.anti_precision = editantiderivativepage->precision->value();
tmp_fktext.anti_linewidth = editantiderivativepage->lineWidthAntiderivative->value();
}
else
{
m_parser->fktext[index].anti_mode = 0;
m_parser->fktext[index].anti_color = m_parser->fktext[index].color0; //default color
tmp_fktext.anti_mode = 0;
tmp_fktext.anti_color = m_parser->fktext[index].color0; //default color
}
if( editfunctionpage->hide->isChecked() )
m_parser->fktext[ index ].f_mode = 0;
tmp_fktext.f_mode = 0;
else
m_parser->fktext[ index ].f_mode = 1;
tmp_fktext.f_mode = 1;
tmp_fktext.k_anz = 0;
if( !m_parameter.isEmpty() )
{
m_parser->fktext[ index ].str_parameter = m_parameter;
m_parser->fktext[ index ].k_anz = 0;
tmp_fktext.str_parameter = m_parameter;
for( QStringList::Iterator it = m_parameter.begin(); it != m_parameter.end(); ++it )
{
m_parser->fktext[ index ].k_liste[ m_parser->fktext[ index ].k_anz ] = m_parser->eval(( *it ) );
m_parser->fktext[ index ].k_anz++;
tmp_fktext.k_liste[ tmp_fktext.k_anz ] = m_parser->eval(( *it ) );
tmp_fktext.k_anz++;
}
}
m_parser->fktext[ index ].linewidth = editfunctionpage->lineWidth->value();
m_parser->fktext[ index ].color = editfunctionpage->color->color().rgb();
tmp_fktext.linewidth = editfunctionpage->lineWidth->value();
tmp_fktext.color = editfunctionpage->color->color().rgb();
editfunctionpage->equation->setText(f_str);
//editfunctionpage->equation->setText(f_str);
if( editderivativespage->showDerivative1->isChecked() )
{
m_parser->fktext[ index ].f1_mode = 1;
m_parser->fktext[ index ].f1_linewidth = editderivativespage->lineWidthDerivative1->value();
m_parser->fktext[ index ].f1_color = editderivativespage->colorDerivative1->color().rgb();
tmp_fktext.f1_mode = 1;
tmp_fktext.f1_linewidth = editderivativespage->lineWidthDerivative1->value();
tmp_fktext.f1_color = editderivativespage->colorDerivative1->color().rgb();
}
else
{
m_parser->fktext[ index ].f1_mode = 0;
m_parser->fktext[index].f1_color = m_parser->fktext[index].color0; //default color
tmp_fktext.f1_mode = 0;
tmp_fktext.f1_color = m_parser->fktext[index].color0; //default color
}
if( editderivativespage->showDerivative2->isChecked() )
{
m_parser->fktext[ index ].f2_mode = 1;
m_parser->fktext[ index ].f2_linewidth = editderivativespage->lineWidthDerivative2->value();
m_parser->fktext[ index ].f2_color = editderivativespage->colorDerivative2->color().rgb();
tmp_fktext.f2_mode = 1;
tmp_fktext.f2_linewidth = editderivativespage->lineWidthDerivative2->value();
tmp_fktext.f2_color = editderivativespage->colorDerivative2->color().rgb();
}
else
{
m_parser->fktext[index].f1_color = m_parser->fktext[index].color0; //default color
m_parser->fktext[ index ].f2_mode = 0;
tmp_fktext.f2_mode = 0;
tmp_fktext.f1_color = m_parser->fktext[index].color0; //default color
}
if ( f_str.contains('y') != 0 && tmp_fktext.f_mode || tmp_fktext.f1_mode || tmp_fktext.f2_mode)
{
KMessageBox::error( this, i18n( "Recursive function is only allowed when drawing anti-derivatives graphs") );
if( m_index == -1 ) m_parser->delfkt(index);
return;
}
m_parser->fktext[index] = tmp_fktext;
// call inherited method
KDialogBase::accept();
}
......@@ -372,4 +394,4 @@ void EditFunction::cmdParameter_clicked()
{
KParameterEditor *dlg = new KParameterEditor(m_parser,&m_parameter);
dlg->show();
}
}
\ No newline at end of file
......@@ -28,6 +28,8 @@
#include <kdialogbase.h>
#include "xparser.h"
class XParser;
class EditFunctionPage;
class EditDerivativesPage;
......@@ -64,6 +66,7 @@ class EditFunction : public KDialogBase
void setWidgets();
/// Check, if the function has 2 arguments (function group)
bool functionHas2Arguments();
/// Pointer to the parser instance
XParser* m_parser;
/// Current function index
......
......@@ -216,11 +216,13 @@ void KConstantEditor::editConstantSlot()
if (item!=0)
item->setText(1,value);
QString tmpName = locate("tmp", "") + "kmplot-" + KApplication::kApplication()->sessionId();
KmPlotIO::save( m_view->parser(), tmpName );
m_view->init();
KmPlotIO::load( m_view->parser(), tmpName );
QFile::remove( tmpName );
QString fname, fstr;
for ( int index = 0; index < m_view->parser()->ufanz; ++index )
{
if ( m_view->parser()->getfkt( index, fname, fstr ) == -1 ) continue;
if( fstr.contains(constant)!=0 )
m_view->parser()->reparse(index); //reparsing the function
}
m_view->drawPlot();
}
......
......@@ -63,6 +63,7 @@ void KEditParametric::clearWidgets()
checkBoxRange->setChecked( false );
min->clear();
max->clear();
kIntNumInputLineWidth->setValue( m_parser->dicke0 );
kColorButtonColor->setColor( m_parser->fktext[ m_parser->getNextIndex() ].color0 );
}
......@@ -84,23 +85,39 @@ void KEditParametric::setWidgets()
void KEditParametric::accept()
{
// if we are editing an existing function, first delete the old one
if( m_x_index != -1 )
{
m_parser->delfkt( m_x_index );
m_x_index = -1;
}
if( m_y_index != -1 )
if ( kLineEditXFunction->text().contains('y') != 0 || kLineEditYFunction->text().contains('y') != 0)
{
m_parser->delfkt( m_y_index );
m_y_index = -1;
KMessageBox::error( this, i18n( "Recursive function not allowed"));
kLineEditXFunction->setFocus();
kLineEditXFunction->selectAll();
return;
}
// find a name not already used
if( kLineEditName->text().isEmpty() )
kLineEditName->setText( newName() );
int index;
if( m_x_index != -1 ) //when editing a function:
{
index = m_x_index; //use the right function-index
QString old_fstr = m_parser->ufkt[index].fstr;
m_parser->ufkt[index].fstr = xFunction();
m_parser->reparse(index); //reparse the funcion
if ( m_parser->errmsg() != 0)
{
m_parser->ufkt[index].fstr = old_fstr;
m_parser->reparse(index);
this->raise();
kLineEditXFunction->setFocus();
kLineEditXFunction->selectAll();
return;
}
}
else
index = m_parser->addfkt(xFunction() );
int index = m_parser->addfkt( xFunction() );
if( index == -1 )
{
m_parser->errmsg();
......@@ -108,58 +125,85 @@ void KEditParametric::accept()
kLineEditXFunction->setFocus();
kLineEditXFunction->selectAll();
return;
}
m_parser->fktext[ index ].extstr = xFunction();
m_parser->getext( index );
}
XParser::FktExt tmp_fktext;
tmp_fktext.extstr = xFunction();
if( checkBoxHide->isChecked() )
m_parser->fktext[ index ].f_mode = 0;
tmp_fktext.f_mode = 0;
else
m_parser->fktext[ index ].f_mode = 1;
tmp_fktext.f_mode = 1;
if( checkBoxRange->isChecked() )
{
m_parser->fktext[ index ].str_dmin = min->text();
m_parser->fktext[ index ].dmin = m_parser->eval( min->text() );
tmp_fktext.str_dmin = min->text();
tmp_fktext.dmin = m_parser->eval( min->text() );
if ( m_parser->errmsg())
{
min->setFocus();
min->selectAll();
m_parser->delfkt( index );
return;
}
m_parser->fktext[ index ].str_dmax = max->text();
m_parser->fktext[ index ].dmax = m_parser->eval( max->text() );
tmp_fktext.str_dmax = max->text();
tmp_fktext.dmax = m_parser->eval( max->text() );
if ( m_parser->errmsg())
{
max->setFocus();
max->selectAll();
m_parser->delfkt( index );
return;
}
if ( m_parser->fktext[ index ].dmin >= m_parser->fktext[ index ].dmax)
if ( tmp_fktext.dmin >= tmp_fktext.dmax)
{
KMessageBox::error(this,i18n("The minimum range value must be lower than the maximum range value"));
min->setFocus();
min->selectAll();
m_parser->delfkt( index );
return;
}
if ( m_parser->fktext[ index ].dmin<View::xmin || m_parser->fktext[ index ].dmax>View::xmax )
if ( tmp_fktext.dmin<View::xmin || tmp_fktext.dmax>View::xmax )
{
KMessageBox::error(this,i18n("Please insert a minimum and maximum range between %1 and %2").arg(View::xmin).arg(View::xmax) );
min->setFocus();
min->selectAll();
m_parser->delfkt( index );
return;
}
}
else
{
tmp_fktext.str_dmin ="0";
tmp_fktext.dmin = 0;
tmp_fktext.str_dmax = "0";
tmp_fktext.dmax = 0;
}
tmp_fktext.linewidth = kIntNumInputLineWidth->value();
tmp_fktext.color = kColorButtonColor->color().rgb();
tmp_fktext.use_slider = -1;
tmp_fktext.k_anz = 0;
m_parser->fktext[ index ].linewidth = kIntNumInputLineWidth->value();
m_parser->fktext[ index ].color = kColorButtonColor->color().rgb();
m_parser->fktext[index] = tmp_fktext;
index = m_parser->addfkt( yFunction() );
if( m_y_index != -1 ) //when editing a function:
{
index = m_y_index; //use the right function-index
QString old_fstr = m_parser->ufkt[index].fstr;
m_parser->ufkt[index].fstr = yFunction();
m_parser->reparse(index); //reparse the funcion
if ( m_parser->errmsg() != 0) //when something went wrong:
{
m_parser->ufkt[index].fstr = old_fstr; //go back to the old expression
m_parser->reparse(index); //reparse
this->raise();
kLineEditXFunction->setFocus();
kLineEditXFunction->selectAll();
return;
}
}
else
index = m_parser->addfkt(yFunction() );
if( index == -1 )
{
m_parser->errmsg();
......@@ -168,9 +212,10 @@ void KEditParametric::accept()
kLineEditYFunction->selectAll();