moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kmplot / kmplot / kconstanteditor.cpp
blobd3778c6813d2ef91d86e66b5afb4cd5636ceb1b1
1 /*
2 * KmPlot - a math. function plotter for the KDE-Desktop
4 * Copyright (C) 2004 Fredrik Edemar
5 * f_edemar@linux.se
6 *
7 * This file is part of the KDE Project.
8 * KmPlot is part of the KDE-EDU Project.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <kapplication.h>
27 #include <kdebug.h>
28 #include <kinputdialog.h>
29 #include <klineedit.h>
30 #include <klistview.h>
31 #include <klocale.h>
32 #include <kmessagebox.h>
33 #include <kstandarddirs.h>
34 #include <qpushbutton.h>
35 #include <qstringlist.h>
36 #include <qvaluevector.h>
37 #include <qdom.h>
39 #include "kmplotio.h"
40 #include "kconstanteditor.h"
43 KConstantEditor::KConstantEditor(View *v, QWidget *parent, const char *name)
44 : QConstantEditor(parent,name), m_view(v)
46 QString str_value;
47 QValueVector<Constant>::iterator it;
48 for(it = m_view->parser()->constant.begin(); it!= m_view->parser()->constant.end() ;++it)
50 str_value.setNum(it->value);
51 (void) new QListViewItem(varlist, QChar(it->constant), str_value);
56 KConstantEditor::~KConstantEditor()
60 void KConstantEditor::cmdNew_clicked()
62 constant = '0';
63 KEditConstant *dlg = new KEditConstant(m_view->parser(), constant, value);
64 connect( dlg, SIGNAL( finished() ), this,SLOT(newConstantSlot() ) );
65 dlg->show();
68 void KConstantEditor::cmdEdit_clicked()
70 if ( !varlist->currentItem() )
71 return;
72 constant = varlist->currentItem()->text(0).at(0).latin1();
73 value = varlist->currentItem()->text(1);
75 KEditConstant *dlg = new KEditConstant(m_view->parser(), constant, value);
76 connect( dlg, SIGNAL( finished() ), this,SLOT(editConstantSlot() ) );
77 dlg->show();
80 void KConstantEditor::cmdDelete_clicked()
82 if ( !varlist->currentItem() )
83 return;
85 constant = varlist->currentItem()->text(0).at(0).latin1();
86 value = varlist->currentItem()->text(1);
87 QString str;
89 for( QValueVector<Ufkt>::iterator it = m_view->parser()->ufkt.begin(); it != m_view->parser()->ufkt.end(); ++it)
91 str = it->fstr;
92 for (int i=str.find(')'); (uint)i<str.length();i++)
93 if ( str.at(i) == constant )
95 KMessageBox::error(this, i18n("A function uses this constant; therefore, it cannot be removed."));
96 return;
99 QValueVector<Constant>::iterator it;
100 for(it = m_view->parser()->constant.begin(); it!= m_view->parser()->constant.end(); ++it)
102 if ( it->constant == constant)
104 if ( it++ == m_view->parser()->constant.end())
105 m_view->parser()->constant.pop_back();
106 else
108 it--;
109 m_view->parser()->constant.erase(it++);
111 KMessageBox::error(this, i18n("The item could not be found."));
112 return;
116 delete varlist->findItem(QChar(constant), 0); //removes the item from the constant list
119 void KConstantEditor::varlist_clicked( QListViewItem * item )
121 if (item)
123 cmdEdit->setEnabled(true);
124 cmdDelete->setEnabled(true);
125 cmdDuplicate->setEnabled(true);
127 else
129 cmdEdit->setEnabled(false);
130 cmdDelete->setEnabled(false);
131 cmdDuplicate->setEnabled(false);
135 void KConstantEditor::cmdDuplicate_clicked()
137 if (!varlist->currentItem())
138 return;
139 constant = varlist->currentItem()->text(0).at(0).latin1();
140 value = varlist->currentItem()->text(1);
142 QStringList list;
143 bool found;
144 for (char i='A'; i<'Z';i++)
146 found = false;
147 QValueVector<Constant>::iterator it;
148 for(it = m_view->parser()->constant.begin(); it!= m_view->parser()->constant.end() && !found;++it)
150 if ( it->constant == i || i == constant)
152 found = true;
155 if (!found)
156 list.append(QChar(i));
158 QStringList result = KInputDialog::getItemList(i18n("Choose Name"),i18n("Choose a name for the constant:"),list, QStringList(), false, &found);
159 if (found)
161 constant = (*result.begin()).at(0).latin1();
162 emit newConstantSlot();
167 void KConstantEditor::newConstantSlot()
169 double dvalue = m_view->parser()->eval(value);
170 m_view->parser()->constant.append( Constant(constant, dvalue) );
171 (void) new QListViewItem(varlist, QChar( constant ), value);
172 varlist->sort();
175 void KConstantEditor::editConstantSlot()
177 double dvalue = m_view->parser()->eval(value);
178 bool found = false;
179 QValueVector<Constant>::iterator it;
180 for(it = m_view->parser()->constant.begin(); it!= m_view->parser()->constant.end() && !found;++it)
182 if ( it->constant == constant)
184 it->value = dvalue;
185 found = true;
188 if (!found)
190 KMessageBox::error(this, i18n("The item could not be found."));
191 return;
194 QListViewItem *item = varlist->findItem(QChar(constant), 0);
195 if (item!=0)
196 item->setText(1,value);
199 int index = 0;
200 for( QValueVector<Ufkt>::iterator it = m_view->parser()->ufkt.begin(); it != m_view->parser()->ufkt.end(); ++it)
202 if( it->fstr.contains(constant)!=0 )
203 m_view->parser()->reparse(index); //reparsing the function
204 ++index;
207 m_view->drawPlot();
210 void KConstantEditor::varlist_doubleClicked( QListViewItem * )
212 cmdEdit_clicked();
215 #include "kconstanteditor.moc"