moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kmplot / kmplot / FktDlg.cpp
blob7dfb13e9db1c14d6969e197fb82b965004058b96
1 /*
2 * KmPlot - a math. function plotter for the KDE-Desktop
4 * Copyright (C) 1998, 1999 Klaus-Dieter M�ler
5 * 2000, 2002 kd.moeller@t-online.de
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 // Qt includes
27 // #include <qpushbutton.h>
28 // #include <qpopupmenu.h>
30 // KDE includes
31 #include <kapplication.h>
32 #include <klistview.h>
33 #include <klocale.h>
34 #include <kmessagebox.h>
35 #include <kpushbutton.h>
36 #include <qcstring.h>
37 #include <qcursor.h>
38 #include <qheader.h>
40 // locale includes
41 #include "FktDlg.h"
42 #include "FktDlg.moc"
43 #include "editfunction.h"
44 #include "keditparametric.h"
45 #include "keditpolar.h"
46 #include "MainDlg.h"
47 #include "parseriface.h"
48 #include "xparser.h"
50 #include <kdebug.h>
52 FktDlg::FktDlg( QWidget* parent, View *view ) : FktDlgData( parent, "editPlots" ), m_view(view)
54 connect( cmdCopyFunction, SIGNAL( clicked() ), this, SLOT( slotCopyFunction()) );
55 connect( cmdMoveFunction, SIGNAL( clicked() ), this, SLOT( slotMoveFunction()) );
56 connect( lb_fktliste, SIGNAL( doubleClicked(QListViewItem *, const QPoint &, int)), this, SLOT(lb_fktliste_doubleClicked(QListViewItem *, const QPoint &, int)) );
57 connect( lb_fktliste, SIGNAL( clicked(QListViewItem *)), this, SLOT(lb_fktliste_clicked(QListViewItem *)));
58 connect( lb_fktliste, SIGNAL( spacePressed(QListViewItem *)), this, SLOT(lb_fktliste_spacePressed(QListViewItem *)));
60 lb_fktliste->addColumn( "" );
61 lb_fktliste->header()->hide();
62 lb_fktliste->setResizeMode( QListView::LastColumn );
65 FktDlg::~FktDlg()
69 void FktDlg::slotDelete()
71 if ( lb_fktliste->currentItem() == 0)
72 return;
73 QCheckListItem *currentItem = dynamic_cast<QCheckListItem *>(lb_fktliste->currentItem());
74 if( currentItem->text()[0] == 'x' )
76 // Delete pair of parametric function
77 int const id = getParamId( currentItem->text());
78 if ( id == -1)
79 return;
80 if ( !m_view->parser()->delfkt(id))
81 return;
83 else
85 // only one function to be deleted
86 if (!m_view->parser()->delfkt( getId( currentItem->text()) ) )
87 return;
89 lb_fktliste->takeItem( currentItem );
90 changed = true;
91 updateView();
92 if ( lb_fktliste->childCount()==0 )
93 PushButtonDel->setEnabled(false);
96 void FktDlg::lb_fktliste_doubleClicked( QListViewItem *, const QPoint &, int )
98 if ( mapFromGlobal(QCursor::pos()).x() <= 40 )
99 return;
100 slotEdit();
103 void FktDlg::lb_fktliste_spacePressed( QListViewItem *item)
105 if ( !item )
106 return;
107 QCheckListItem *currentItem = dynamic_cast<QCheckListItem *>(item);
108 int id;
109 if ( currentItem->text()[0] == 'x' )
110 id = getParamId(currentItem->text());
111 else
112 id = getId(currentItem->text());
113 Ufkt *function = &m_view->parser()->ufkt[ m_view->parser()->ixValue(id) ];
114 if ( id==-1 )
115 return;
117 function->f_mode=currentItem->isOn();
118 updateView();
121 void FktDlg::lb_fktliste_clicked( QListViewItem * item )
123 if ( mapFromGlobal(QCursor::pos()).x() > 40 )
124 return;
125 lb_fktliste_spacePressed(item);
129 void FktDlg::slotEdit()
131 QCheckListItem *currentItem = dynamic_cast<QCheckListItem *>(lb_fktliste->currentItem());
132 if ( currentItem == 0 )
134 PushButtonEdit->setEnabled(false);
135 return;
137 int const id = getId( currentItem->text().section( ";", 0, 0) ) ;
139 // find out the function type
140 char const prefix = m_view->parser()->ufkt[ m_view->parser()->ixValue(id) ].fstr.at(0).latin1();
142 if ( prefix == 'r')
143 slotEditPolar( id );
144 else if ( prefix == 'x')
145 slotEditParametric( id, m_view->parser()->ixValue(getId( currentItem->text().section( ";", 1, 1) )));
146 else
147 slotEditFunction( id );
150 int FktDlg::getId( const QString &f_str )
152 for( QValueVector<Ufkt>::iterator it = m_view->parser()->ufkt.begin(); it != m_view->parser()->ufkt.end(); ++it)
154 if ( it->fstr == f_str )
155 return it->id;
157 return -1;
160 int FktDlg::getParamId( const QString &f_str)
162 QString const fname = f_str.section( "(", 0, 0 );
163 for( QValueVector<Ufkt>::iterator it = m_view->parser()->ufkt.begin(); it != m_view->parser()->ufkt.end(); ++it)
165 if ( it->fname == fname )
166 return it->id;
168 return -1;
171 void FktDlg::updateView()
173 m_view->drawPlot();
176 void FktDlg::slotHasSelection()
178 bool const has_selection = !( lb_fktliste->currentItem() == 0 );
179 PushButtonEdit->setEnabled( has_selection );
180 PushButtonDel->setEnabled( has_selection );
181 cmdCopyFunction->setEnabled( has_selection );
182 cmdMoveFunction->setEnabled( has_selection );
185 void FktDlg::slotEditFunction( int id )
187 EditFunction* editFunction = new EditFunction( m_view->parser(), this );
188 if ( id==-1 ) editFunction->setCaption(i18n( "New Function Plot" ));
189 else editFunction->setCaption(i18n( "Edit Function Plot" ));
190 editFunction->initDialog( id );
191 if( editFunction->exec() == QDialog::Accepted )
193 Ufkt *function = editFunction->functionItem();
194 QCheckListItem *item;
195 if( id == -1 ) //a new function
196 item = new QCheckListItem(lb_fktliste, function->fstr, QCheckListItem::CheckBox); //a new function
197 else //change a function
199 item = dynamic_cast<QCheckListItem *>(lb_fktliste->currentItem());
200 item->setText(0, function->fstr );
202 item->setOn(function->f_mode);
203 lb_fktliste->sort();
204 changed = true;
205 updateView();
209 void FktDlg::slotEditParametric( int x_id, int y_id)
211 KEditParametric* editParametric = new KEditParametric( m_view->parser(), this );
212 if ( x_id==-1 && y_id==-1)
213 editParametric->setCaption(i18n( "New Parametric Plot" ));
214 editParametric->initDialog( x_id, y_id );
215 if( editParametric->exec() == QDialog::Accepted )
217 Ufkt *function_y = editParametric->functionItem();
218 Ufkt *function_x = &m_view->parser()->ufkt[m_view->parser()->ixValue(function_y->id - 1)]; //get the x-function
219 QCheckListItem *item;
220 if( x_id == -1 ) //a new function
222 item = new QCheckListItem(lb_fktliste, function_x->fstr+";"+function_y->fstr, QCheckListItem::CheckBox);
224 else //change a function
226 item = dynamic_cast<QCheckListItem *>(lb_fktliste->currentItem());
227 item->setText(0, function_x->fstr+";"+function_y->fstr );
229 item->setOn(function_y->f_mode);
230 lb_fktliste->sort();
231 changed = true;
232 updateView();
236 void FktDlg::slotEditPolar( int id )
238 KEditPolar* editPolar = new KEditPolar( m_view->parser(), this );
239 if ( id==-1) editPolar->setCaption(i18n( "New Polar Plot" ));
240 editPolar->initDialog( id );
241 if( editPolar->exec() == QDialog::Accepted )
243 Ufkt *function = editPolar->functionItem();
244 QCheckListItem *item;
245 if( id == -1 ) // a new function
247 item = new QCheckListItem(lb_fktliste, function->fstr, QCheckListItem::CheckBox);
249 else //change a function
251 item = dynamic_cast<QCheckListItem *>(lb_fktliste->currentItem());
252 item->setText(0, function->fstr );
254 item->setOn(function->f_mode);
255 lb_fktliste->sort();
256 changed = true;
257 updateView();
261 void FktDlg::slotNewFunction()
263 slotEditFunction();
266 void FktDlg::slotNewParametric()
268 slotEditParametric();
271 void FktDlg::slotNewPolar()
273 slotEditPolar();
276 void FktDlg::getPlots()
278 lb_fktliste->clear();
280 // adding all yet added functions
281 for( QValueVector<Ufkt>::iterator it = m_view->parser()->ufkt.begin(); it != m_view->parser()->ufkt.end(); ++it)
283 if( it->fname.isEmpty() || it->fstr[0] == 'y' ) continue;
284 QCheckListItem *item;
285 if( it->fstr[0] == 'x' )
287 QString y = it->fstr;
288 ++it;
289 item = new QCheckListItem( lb_fktliste, y + ";" + it->fstr, QCheckListItem::CheckBox );
291 else
292 item = new QCheckListItem(lb_fktliste, it->fstr, QCheckListItem::CheckBox);
293 item->setOn(it->f_mode);
295 lb_fktliste->sort();
298 void FktDlg::slotHelp()
300 kapp->invokeHelp( "", "kmplot" );
303 bool FktDlg::isChanged()
305 return changed;
308 void FktDlg::showEvent ( QShowEvent * )
310 changed = false;
313 void FktDlg::slotCopyFunction()
315 if ( lb_fktliste->currentItem() == 0)
317 cmdCopyFunction->setEnabled(false);
318 return;
320 QCheckListItem *currentItem = dynamic_cast<QCheckListItem *>(lb_fktliste->currentItem());
321 QString const fstr = currentItem->text();
322 m_view->parser()->sendFunction(getId(fstr));
325 void FktDlg::slotMoveFunction()
327 if ( lb_fktliste->currentItem() == 0)
329 cmdCopyFunction->setEnabled(false);
330 return;
332 QCheckListItem *currentItem = dynamic_cast<QCheckListItem *>(lb_fktliste->currentItem());
333 QString const fstr = currentItem->text();
334 if ( !m_view->parser()->sendFunction(getId( currentItem->text()) ) )
335 return;
336 slotDelete();