keditpolar.cpp 7.39 KB
Newer Older
1 2 3
/*
* KmPlot - a math. function plotter for the KDE-Desktop
*
4
* Copyright (C) 1998, 1999  Klaus-Dieter Möller
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
*               2000, 2002 kd.moeller@t-online.de
*
* 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
Dirk Mueller's avatar
Dirk Mueller committed
22
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 24 25 26 27 28
*
*/
// Qt includes
#include <qcheckbox.h>

// KDE includes
Matthias Messmer's avatar
Matthias Messmer committed
29
#include <kapplication.h>
30 31
#include <kcolorbutton.h>
#include <klineedit.h>
Fredrik Edemar's avatar
Fredrik Edemar committed
32 33 34 35
#include <klocale.h>
#include <kmessagebox.h>
#include <knuminput.h>

36 37

#include <kdebug.h>
Laurent Montel's avatar
Laurent Montel committed
38
#include <ktoolinvocation.h>
39
#include <kvbox.h>
40 41 42 43 44

// local includes
#include "keditpolar.h"
#include "keditpolar.moc"
#include "xparser.h"
Fredrik Edemar's avatar
Fredrik Edemar committed
45
#include "View.h"
46

47 48 49
KEditPolar::KEditPolar( XParser* parser, QWidget* parent )
	: KDialog( parent, i18n("Edit Polar Plot"), Ok|Cancel|Help ),
	  m_parser(parser)
50
{
51 52 53
// 	KVBox *page = makeVBoxMainWidget();
	m_editPolar = new QEditPolar( this );
	setMainWidget( m_editPolar );
54
	m_updatedfunction = 0;
55 56
}

57
void KEditPolar::initDialog( int id )
58
{
59 60
	m_id = id;
	if( m_id == -1 ) clearWidgets();
61 62 63 64 65
	else setWidgets();
}

void KEditPolar::clearWidgets()
{
66 67 68 69 70 71 72 73
	m_editPolar->kLineEditYFunction->clear();
	m_editPolar->checkBoxHide->setChecked( false );
	m_editPolar->customMinRange->setChecked( false );
	m_editPolar->customMaxRange->setChecked(false);
	m_editPolar->min->clear();
	m_editPolar->max->clear();
	m_editPolar->kIntNumInputLineWidth->setValue( m_parser->linewidth0 );
	m_editPolar->kColorButtonColor->setColor( m_parser->defaultColor(m_parser->getNextIndex() ) );
74 75 76 77
}

void KEditPolar::setWidgets()
{
78
        Ufkt *ufkt = &m_parser->ufkt[ m_parser->ixValue(m_id) ];
79
	QString function = ufkt->fstr;
80
	function = function.right( function.length()-1 );
81 82
	m_editPolar->kLineEditYFunction->setText( function );
	m_editPolar->checkBoxHide->setChecked( !ufkt->f_mode);
83
	if (ufkt->usecustomxmin)
84
	{
85 86
		m_editPolar->customMinRange->setChecked(true);
		m_editPolar->min->setText( ufkt->str_dmin );
87 88
	}
	else
89
		m_editPolar->customMinRange->setChecked(false);
90 91 92
	
	if (ufkt->usecustomxmin)
	{
93 94
		m_editPolar->customMaxRange->setChecked(true);
		m_editPolar->max->setText( ufkt->str_dmax );
95 96
	}
	else
97
		m_editPolar->customMaxRange->setChecked(false);
98
	
99 100
	m_editPolar->kIntNumInputLineWidth->setValue( ufkt->linewidth );
	m_editPolar->kColorButtonColor->setColor( ufkt->color );
101 102 103 104
}

void KEditPolar::accept()
{
105
	QString f_str = /*"r" + */m_editPolar->kLineEditYFunction->text();
106

107 108 109 110 111 112
	if ( m_id!=-1 )
		m_parser->fixFunctionName(f_str, XParser::Polar, m_id);
	else
		m_parser->fixFunctionName(f_str, XParser::Polar);
	Ufkt tmp_ufkt;  //all settings are saved here until we know that no errors have appeared

113
	tmp_ufkt.f_mode = !m_editPolar->checkBoxHide->isChecked();
114
	
115
	if( m_editPolar->customMinRange->isChecked() )
116
	{
117
		tmp_ufkt.usecustomxmin = true;
118 119
		tmp_ufkt.str_dmin = m_editPolar->min->text();
		tmp_ufkt.dmin = m_parser->eval( m_editPolar->min->text() );
120
		if ( m_parser->parserError() )
Fredrik Edemar's avatar
Fredrik Edemar committed
121
		{
122 123
			m_editPolar->min->setFocus();
			m_editPolar->min->selectAll();
Fredrik Edemar's avatar
Fredrik Edemar committed
124 125
			return;
		}
126 127 128
	}
	else
		tmp_ufkt.usecustomxmin = false;
129
	if( m_editPolar->customMaxRange->isChecked() )
130 131
	{
		tmp_ufkt.usecustomxmax = true;
132 133
		tmp_ufkt.str_dmax = m_editPolar->max->text();
		tmp_ufkt.dmax = m_parser->eval( m_editPolar->max->text() );
134
		if ( m_parser->parserError())
Fredrik Edemar's avatar
Fredrik Edemar committed
135
		{
136 137
			m_editPolar->max->setFocus();
			m_editPolar->max->selectAll();
Fredrik Edemar's avatar
Fredrik Edemar committed
138 139
			return;
		}
140
		if ( tmp_ufkt.usecustomxmin && tmp_ufkt.dmin >=  tmp_ufkt.dmax)
Fredrik Edemar's avatar
Fredrik Edemar committed
141
		{
142 143 144
			KMessageBox::sorry(this,i18n("The minimum range value must be lower than the maximum range value"));
			m_editPolar->min->setFocus();
			m_editPolar->min->selectAll();
Fredrik Edemar's avatar
Fredrik Edemar committed
145 146
			return;
		}
147
	}
148
	else
149 150
		tmp_ufkt.usecustomxmax = false;
	
151 152 153
	tmp_ufkt.f1_mode = 0;
	tmp_ufkt.f2_mode = 0;
	tmp_ufkt.integral_mode = 0;
154 155
	tmp_ufkt.linewidth = m_editPolar->kIntNumInputLineWidth->value();
	tmp_ufkt.color = m_editPolar->kColorButtonColor->color().rgb();
156
	tmp_ufkt.use_slider = -1;
157
        
158
        Ufkt *added_ufkt;
159
        if( m_id != -1 )  //when editing a function:
160
        {
161 162 163
                int const ix = m_parser->ixValue(m_id);
                if ( ix == -1) //The function could have been deleted
                {
164
                        KMessageBox::sorry(this,i18n("Function could not be found"));
165 166 167 168 169 170 171 172 173 174
                        return;
                }
                added_ufkt =  &m_parser->ufkt[ix];
                QString const old_fstr = added_ufkt->fstr;
                added_ufkt->fstr = f_str;
                m_parser->reparse(added_ufkt); //reparse the funcion
                if ( m_parser->parserError() != 0)
                {
                        added_ufkt->fstr = old_fstr;
                        m_parser->reparse(added_ufkt);
175
                        raise();
176 177
						m_editPolar->kLineEditYFunction->setFocus();
						m_editPolar->kLineEditYFunction->selectAll();
178 179
                        return;
                }
180
        }
181
        else
182
        {
183
                int const id = m_parser->addfkt(f_str );
Pino Toscano's avatar
Pino Toscano committed
184
                kDebug() << "id: " << id << endl;
185 186 187
                if( id == -1 ) 
                {
                        m_parser->parserError();
188
                        raise();
189 190
						m_editPolar->kLineEditYFunction->setFocus();
						m_editPolar->kLineEditYFunction->selectAll();
191 192 193
                        return;
                }
                added_ufkt =  &m_parser->ufkt.last();
194
        }
195
	//save all settings in the function now when we know no errors have appeared
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
	added_ufkt->f_mode = tmp_ufkt.f_mode;
	added_ufkt->f1_mode = tmp_ufkt.f1_mode;
	added_ufkt->f2_mode = tmp_ufkt.f2_mode;
	added_ufkt->integral_mode = tmp_ufkt.integral_mode;
	added_ufkt->integral_use_precision = tmp_ufkt.integral_use_precision;
	added_ufkt->linewidth = tmp_ufkt.linewidth;
	added_ufkt->f1_linewidth = tmp_ufkt.f1_linewidth;
	added_ufkt->f2_linewidth = tmp_ufkt.f2_linewidth;
	added_ufkt->integral_linewidth = tmp_ufkt.integral_linewidth;
	added_ufkt->str_dmin = tmp_ufkt.str_dmin;
	added_ufkt->str_dmax = tmp_ufkt.str_dmax;
	added_ufkt->dmin = tmp_ufkt.dmin;
	added_ufkt->dmax = tmp_ufkt.dmax;
	added_ufkt->str_startx = tmp_ufkt.str_startx;
	added_ufkt->str_starty = tmp_ufkt.str_starty;
	added_ufkt->oldx = tmp_ufkt.oldx;
	added_ufkt->starty = tmp_ufkt.starty;
	added_ufkt->startx = tmp_ufkt.startx;
	added_ufkt->integral_precision = tmp_ufkt.integral_precision;
	added_ufkt->color = tmp_ufkt.color;
	added_ufkt->f1_color = tmp_ufkt.f1_color;
	added_ufkt->f2_color = tmp_ufkt.f2_color;
	added_ufkt->integral_color = tmp_ufkt.integral_color;
	added_ufkt->parameters = tmp_ufkt.parameters;
	added_ufkt->use_slider = tmp_ufkt.use_slider;
	added_ufkt->usecustomxmin = tmp_ufkt.usecustomxmin;
	added_ufkt->usecustomxmax = tmp_ufkt.usecustomxmax;
	
224 225
	m_updatedfunction = added_ufkt;
	
226
	// call inherited method
227
	KDialog::accept();
228 229
}

230
Ufkt *KEditPolar::functionItem()
231
{
232
	return m_updatedfunction;
233
}
Matthias Messmer's avatar
Matthias Messmer committed
234 235 236

void KEditPolar::slotHelp()
{
Laurent Montel's avatar
Laurent Montel committed
237
	KToolInvocation::invokeHelp( "", "kmplot" );
238
}