2 * KmPlot - a math. function plotter for the KDE-Desktop
4 * Copyright (C) 2004 Fredrik Edemar
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>
28 #include <kinputdialog.h>
29 #include <klineedit.h>
30 #include <klistview.h>
32 #include <kmessagebox.h>
33 #include <kstandarddirs.h>
34 #include <qpushbutton.h>
35 #include <qstringlist.h>
36 #include <qvaluevector.h>
40 #include "kconstanteditor.h"
43 KConstantEditor::KConstantEditor(View
*v
, QWidget
*parent
, const char *name
)
44 : QConstantEditor(parent
,name
), m_view(v
)
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()
63 KEditConstant
*dlg
= new KEditConstant(m_view
->parser(), constant
, value
);
64 connect( dlg
, SIGNAL( finished() ), this,SLOT(newConstantSlot() ) );
68 void KConstantEditor::cmdEdit_clicked()
70 if ( !varlist
->currentItem() )
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() ) );
80 void KConstantEditor::cmdDelete_clicked()
82 if ( !varlist
->currentItem() )
85 constant
= varlist
->currentItem()->text(0).at(0).latin1();
86 value
= varlist
->currentItem()->text(1);
89 for( QValueVector
<Ufkt
>::iterator it
= m_view
->parser()->ufkt
.begin(); it
!= m_view
->parser()->ufkt
.end(); ++it
)
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."));
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();
109 m_view
->parser()->constant
.erase(it
++);
111 KMessageBox::error(this, i18n("The item could not be found."));
116 delete varlist
->findItem(QChar(constant
), 0); //removes the item from the constant list
119 void KConstantEditor::varlist_clicked( QListViewItem
* item
)
123 cmdEdit
->setEnabled(true);
124 cmdDelete
->setEnabled(true);
125 cmdDuplicate
->setEnabled(true);
129 cmdEdit
->setEnabled(false);
130 cmdDelete
->setEnabled(false);
131 cmdDuplicate
->setEnabled(false);
135 void KConstantEditor::cmdDuplicate_clicked()
137 if (!varlist
->currentItem())
139 constant
= varlist
->currentItem()->text(0).at(0).latin1();
140 value
= varlist
->currentItem()->text(1);
144 for (char i
='A'; i
<'Z';i
++)
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
)
156 list
.append(QChar(i
));
158 QStringList result
= KInputDialog::getItemList(i18n("Choose Name"),i18n("Choose a name for the constant:"),list
, QStringList(), false, &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
);
175 void KConstantEditor::editConstantSlot()
177 double dvalue
= m_view
->parser()->eval(value
);
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
)
190 KMessageBox::error(this, i18n("The item could not be found."));
194 QListViewItem
*item
= varlist
->findItem(QChar(constant
), 0);
196 item
->setText(1,value
);
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
210 void KConstantEditor::varlist_doubleClicked( QListViewItem
* )
215 #include "kconstanteditor.moc"