functiontools.cpp 6.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/*
* KmPlot - a math. function plotter for the KDE-Desktop
*
* Copyright (C) 2004  Fredrik Edemar <f_edemar@linux.se>
*               2006  David Saxton <david@bluehaze.org>
*
* This file is part of the KDE Project.
* KmPlot is part of the KDE-EDU Project.
*
* 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 "functiontools.h"
#include "ui_functiontools.h"
#include "view.h"
#include "xparser.h"

class FunctionToolsWidget : public QWidget, public Ui::FunctionTools
{
    public:
		FunctionToolsWidget( QWidget * parent = 0 ) : QWidget( parent )
        { setupUi(this); }
};


//BEGIN class FunctionTools
FunctionTools::FunctionTools(QWidget *parent )
	: KDialog( parent )
{
	m_widget = new FunctionToolsWidget( this );
	setMainWidget( m_widget );
	setButtons( Close );
	
	// Adjust margins
	m_widget->layout()->setMargin( 0 );
	for ( int i = 0; i < 2; ++ i )
		m_widget->stack->widget( i )->layout()->setMargin( 0 );
	
	init( CalculateY );
	
	connect( m_widget->xValue, SIGNAL(textChanged(const QString &)), this, SLOT(xChanged()) );
	connect( m_widget->min, SIGNAL(editingFinished()), this, SLOT(rangeEdited()) );
	connect( m_widget->max, SIGNAL(editingFinished()), this, SLOT(rangeEdited()) );
David Saxton's avatar
David Saxton committed
57
	connect( m_widget->list, SIGNAL(currentRowChanged(int)), this, SLOT(equationSelected(int)) );
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
}


FunctionTools::~FunctionTools()
{
}


void FunctionTools::init( Mode m )
{
	m_mode = m;
	
	switch ( m_mode )
	{
		case FindMinimum:
		{
			m_widget->rangeTitle->setText( i18n("Search between:") );
			setCaption(i18n("Find Minimum Point"));
			break;
		}
		
		case FindMaximum:
		{
			m_widget->rangeTitle->setText( i18n("Search between:") );
			setCaption(i18n("Find Maximum Point"));
			break;
		}
		
		case CalculateArea:
		{
			m_widget->rangeTitle->setText( i18n("Calculate the area between:") );
			setCaption(i18n("Area Under Graph"));
			break;
		}
		
		case CalculateY:
		{
			setCaption(i18n("Get y-Value"));
			break;
		}
	}
	
	if ( m_mode == CalculateY )
	{
		m_widget->xValue->setFocus();
		m_widget->stack->setCurrentIndex( 0 );
	}
	else
	{
		m_widget->min->setText( XParser::self()->number( View::self()->m_xmin ) );
		m_widget->max->setText( XParser::self()->number( View::self()->m_xmax ) );
		m_widget->min->setFocus();
		m_widget->stack->setCurrentIndex( 1 );
	}
	
	updateEquationList();
	setEquation( EquationPair( View::self()->m_currentPlot, 0 ) );
}


void FunctionTools::updateEquationList()
{
	EquationPair previousEquation = equation();
	
	m_widget->list->clear();
	m_equations.clear();
124 125 126
	
	// Can't e.g. calculate areas for parametric equations
	bool onlyCartesianLike = (m_mode != CalculateY); 
127 128 129

	foreach ( Function * function, XParser::self()->m_ufkt )
	{
130 131 132
		if ( onlyCartesianLike && function->type() != Function::Cartesian && function->type() != Function::Differential )
			continue;
		
133 134 135 136
		QList<Plot> plots = function->plots();
		
		for ( int i = 0; i < function->eq.size(); ++i )
		{
137
			foreach ( const Plot &plot, plots )
138 139 140 141
				m_equations << EquationPair( plot, i );
		}
	}
	
142
	foreach ( const EquationPair &eq, m_equations )
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	{
		Equation * equation = eq.first.function()->eq[ eq.second ];
		QListWidgetItem * item = new QListWidgetItem( equation->fstr(), m_widget->list );
		item->setTextColor( eq.first.color() );
	}
	
	setEquation( previousEquation );
}


EquationPair FunctionTools::equation( ) const
{
	int row = m_widget->list->currentRow();
	if ( row < 0 || row >= m_equations.size() )
		return EquationPair();
	else
		return m_equations[ row ];
}


void FunctionTools::setEquation( const EquationPair & equation )
{
	int row = m_equations.indexOf( equation);
	if ( row < 0 )
		row = 0;
	m_widget->list->setCurrentRow( row );
	equationSelected( row );
}


void FunctionTools::equationSelected( int equation )
{
	if ( equation < 0 || equation >= m_equations.size() )
		return;
	EquationPair current = m_equations[ equation ];
	
	switch ( m_mode )
	{
		case FindMinimum:
			findMinimum( current );
			break;
			
		case FindMaximum:
			findMaximum( current );
			break;
			
		case CalculateY:
			calculateY( current );
			break;
			
		case CalculateArea:
			calculateArea( current );
			break;
	}
}


void FunctionTools::xChanged()
{
	calculateY( equation() );
}


void FunctionTools::rangeEdited()
{
	switch ( m_mode )
	{
		case FindMinimum:
			findMinimum( equation() );
			break;
			
		case FindMaximum:
			findMaximum( equation() );
			break;
			
		case CalculateArea:
			calculateArea( equation() );
			break;
			
		case CalculateY:
			return;
	}
}


void FunctionTools::calculateY( const EquationPair & equation )
{
	if ( !equation.first.function() )
		return;
	
233
	double result = View::self()->value( equation.first, equation.second, m_widget->xValue->value(), true );
234
	
235
	Equation * eq = equation.first.function()->eq[ equation.second ];
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	
	m_widget->yResult->setText( QString( "%1(%2) = %3" )
			.arg( eq->name() )
			.arg( m_widget->xValue->text() )
			.arg( XParser::self()->number( result ) ) );
}


void FunctionTools::findMinimum( const EquationPair & equation )
{
	if ( !equation.first.function() )
		return;
	
	QPointF extremum = View::self()->findMinMaxValue( equation.first, View::Minimum, m_widget->min->value(), m_widget->max->value() );
	
	m_widget->rangeResult->setText( i18n("Minimum is at x = %1, %2(x) = %3",
									extremum.x(),
									equation.first.function()->eq[0]->name(),
									extremum.y() ) );
}


void FunctionTools::findMaximum( const EquationPair & equation )
{
	if ( !equation.first.function() )
		return;
	
	QPointF extremum = View::self()->findMinMaxValue( equation.first, View::Maximum, m_widget->min->value(), m_widget->max->value() );
	
	m_widget->rangeResult->setText( i18n("Maximum is at x = %1, %2(x) = %3",
									extremum.x(),
									equation.first.function()->eq[0]->name(),
									extremum.y() ) );
}


void FunctionTools::calculateArea( const EquationPair & equation )
{
274 275 276
	if ( !equation.first.function() )
		return;
	
277 278 279 280 281 282 283 284 285 286 287 288 289
	IntegralDrawSettings s;
	s.plot = equation.first;
	s.dmin = m_widget->min->value();
	s.dmax = m_widget->max->value();
                
	double area = View::self()->areaUnderGraph( s );
	
	m_widget->rangeResult->setText( i18n("Area is %1", area ) );
}
//END class FunctionTools


#include "functiontools.moc"