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
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.
27 // #include <qpushbutton.h>
28 // #include <qpopupmenu.h>
31 #include <kapplication.h>
32 #include <klistview.h>
34 #include <kmessagebox.h>
35 #include <kpushbutton.h>
43 #include "editfunction.h"
44 #include "keditparametric.h"
45 #include "keditpolar.h"
47 #include "parseriface.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
);
69 void FktDlg::slotDelete()
71 if ( lb_fktliste
->currentItem() == 0)
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());
80 if ( !m_view
->parser()->delfkt(id
))
85 // only one function to be deleted
86 if (!m_view
->parser()->delfkt( getId( currentItem
->text()) ) )
89 lb_fktliste
->takeItem( currentItem
);
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 )
103 void FktDlg::lb_fktliste_spacePressed( QListViewItem
*item
)
107 QCheckListItem
*currentItem
= dynamic_cast<QCheckListItem
*>(item
);
109 if ( currentItem
->text()[0] == 'x' )
110 id
= getParamId(currentItem
->text());
112 id
= getId(currentItem
->text());
113 Ufkt
*function
= &m_view
->parser()->ufkt
[ m_view
->parser()->ixValue(id
) ];
117 function
->f_mode
=currentItem
->isOn();
121 void FktDlg::lb_fktliste_clicked( QListViewItem
* item
)
123 if ( mapFromGlobal(QCursor::pos()).x() > 40 )
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);
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();
144 else if ( prefix
== 'x')
145 slotEditParametric( id
, m_view
->parser()->ixValue(getId( currentItem
->text().section( ";", 1, 1) )));
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
)
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
)
171 void FktDlg::updateView()
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
);
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
);
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
);
261 void FktDlg::slotNewFunction()
266 void FktDlg::slotNewParametric()
268 slotEditParametric();
271 void FktDlg::slotNewPolar()
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
;
289 item
= new QCheckListItem( lb_fktliste
, y
+ ";" + it
->fstr
, QCheckListItem::CheckBox
);
292 item
= new QCheckListItem(lb_fktliste
, it
->fstr
, QCheckListItem::CheckBox
);
293 item
->setOn(it
->f_mode
);
298 void FktDlg::slotHelp()
300 kapp
->invokeHelp( "", "kmplot" );
303 bool FktDlg::isChanged()
308 void FktDlg::showEvent ( QShowEvent
* )
313 void FktDlg::slotCopyFunction()
315 if ( lb_fktliste
->currentItem() == 0)
317 cmdCopyFunction
->setEnabled(false);
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);
332 QCheckListItem
*currentItem
= dynamic_cast<QCheckListItem
*>(lb_fktliste
->currentItem());
333 QString
const fstr
= currentItem
->text();
334 if ( !m_view
->parser()->sendFunction(getId( currentItem
->text()) ) )