moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kmplot / kmplot / kparametereditor.cpp
blobeaa468d0d9c17c5587d086c79bd4b755a82bcddd
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.
27 #include <kdebug.h>
28 #include <kfiledialog.h>
29 #include <kinputdialog.h>
30 #include <kio/netaccess.h>
31 #include <klistbox.h>
32 #include <klocale.h>
33 #include <kmessagebox.h>
34 #include <kpushbutton.h>
35 #include <ktempfile.h>
36 #include <kurl.h>
37 #include <qfile.h>
38 #include <qtextstream.h>
39 #include <qvaluelist.h>
41 #include "kparametereditor.h"
43 class ParameterValueList;
45 KParameterEditor::KParameterEditor(XParser *m, QValueList<ParameterValueItem> *l, QWidget *parent, const char *name)
46 : QParameterEditor(parent,name, true, Qt::WDestructiveClose), m_parameter(l), m_parser(m)
48 for ( QValueList<ParameterValueItem>::Iterator it = m_parameter->begin(); it != m_parameter->end(); ++it )
49 list->insertItem( (*it).expression );
50 list->sort();
52 connect( cmdNew, SIGNAL( clicked() ), this, SLOT( cmdNew_clicked() ));
53 connect( cmdEdit, SIGNAL( clicked() ), this, SLOT( cmdEdit_clicked() ));
54 connect( cmdDelete, SIGNAL( clicked() ), this, SLOT( cmdDelete_clicked() ));
55 connect( cmdImport, SIGNAL( clicked() ), this, SLOT( cmdImport_clicked() ));
56 connect( cmdExport, SIGNAL( clicked() ), this, SLOT( cmdExport_clicked() ));
57 connect( cmdClose, SIGNAL( clicked() ), this, SLOT( close() ));
58 connect( list, SIGNAL( doubleClicked( QListBoxItem * ) ), this, SLOT( varlist_doubleClicked( QListBoxItem *) ));
59 connect( list, SIGNAL( clicked ( QListBoxItem * ) ), this, SLOT( varlist_clicked(QListBoxItem * ) ));
63 KParameterEditor::~KParameterEditor()
65 m_parameter->clear();
66 QString item_text;
67 for (int i = 0; (uint)i <= list->count();i++)
69 item_text = list->text(i);
70 if ( !item_text.isEmpty() )
71 m_parameter->append( ParameterValueItem(item_text, m_parser->eval( item_text)) );
75 void KParameterEditor::cmdNew_clicked()
77 QString result="";
78 while (1)
80 bool ok;
81 result = KInputDialog::getText( i18n("Parameter Value"), i18n( "Enter a new parameter value:" ), result, &ok );
82 if ( !ok)
83 return;
84 m_parser->eval( result );
85 if ( m_parser->parserError(false) != 0 )
87 m_parser->parserError();
88 continue;
90 if ( checkTwoOfIt(result) )
92 KMessageBox::error(0,i18n("The value %1 already exists and will therefore not be added.").arg(result));
93 continue;
95 list->insertItem(result);
96 list->sort();
97 break;
101 void KParameterEditor::cmdEdit_clicked()
103 QString result=list->currentText();
104 while (1)
106 bool ok;
107 result = KInputDialog::getText( i18n("Parameter Value"), i18n( "Enter a new parameter value:" ), result, &ok );
108 if ( !ok)
109 return;
110 m_parser->eval(result);
111 if ( m_parser->parserError(false) != 0)
113 m_parser->parserError();
114 continue;
116 if ( checkTwoOfIt(result) )
118 if( result != list->currentText() )
119 KMessageBox::error(0,i18n("The value %1 already exists.").arg(result));
120 continue;
122 list->removeItem( list->currentItem());
123 list->insertItem(result);
124 list->sort();
125 break;
129 void KParameterEditor::cmdDelete_clicked()
131 list->removeItem( list->currentItem());
132 list->sort();
135 void KParameterEditor::cmdImport_clicked()
137 KURL url = KFileDialog::getOpenURL( QString::null,i18n("*.txt|Plain Text File "));
138 if ( url.isEmpty() )
139 return;
141 if (!KIO::NetAccess::exists(url,true,this) )
143 KMessageBox::error(0,i18n("The file does not exist."));
144 return;
147 bool verbose = false;
148 QFile file;
149 QString tmpfile;
150 if ( !url.isLocalFile() )
152 if ( !KIO::NetAccess::download(url, tmpfile, this) )
154 KMessageBox::error(0,i18n("An error appeared when opening this file"));
155 return;
157 file.setName(tmpfile);
159 else
160 file.setName(url.prettyURL(0,KURL::StripFileProtocol) );
162 if ( file.open(IO_ReadOnly) )
164 QTextStream stream(&file);
165 QString line;
166 for( int i=1; !stream.atEnd();i++ )
168 line = stream.readLine();
169 if (line.isEmpty())
170 continue;
171 m_parser->eval( line );
172 if ( m_parser->parserError(false) == 0)
174 if ( !checkTwoOfIt(line) )
176 list->insertItem(line);
177 list->sort();
180 else if ( !verbose)
182 if ( KMessageBox::warningYesNo(this,i18n("Line %1 is not a valid parameter value and will therefore not be included. Do you want to continue?").arg(i) ) == KMessageBox::No)
184 file.close();
185 KIO::NetAccess::removeTempFile( tmpfile );
186 return;
188 else if (KMessageBox::warningYesNo(this,i18n("Would you like to be informed about other lines that cannot be read?") ) == KMessageBox::No)
189 verbose = true;
192 file.close();
194 else
195 KMessageBox::error(0,i18n("An error appeared when opening this file"));
197 if ( !url.isLocalFile() )
198 KIO::NetAccess::removeTempFile( tmpfile );
201 void KParameterEditor::cmdExport_clicked()
203 if ( !list->count() )
204 return;
205 KURL url = KFileDialog::getSaveURL( QString::null,i18n("*.txt|Plain Text File "));
206 if ( url.isEmpty() )
207 return;
209 if( !KIO::NetAccess::exists( url,false,this ) || KMessageBox::warningContinueCancel( this, i18n( "A file named \"%1\" already exists. Are you sure you want to continue and overwrite this file?" ).arg( url.url()), i18n( "Overwrite File?" ), KGuiItem( i18n( "&Overwrite" ) ) ) == KMessageBox::Continue )
211 QString tmpfile;
212 QFile file;
213 if ( !url.isLocalFile() )
215 KTempFile tmpfile;
216 file.setName(tmpfile.name() );
218 if (file.open( IO_WriteOnly ) )
220 QTextStream stream(&file);
221 QListBoxItem *it = list->firstItem();
222 while ( 1 )
224 stream << it->text();
225 it = it->next();
226 if (it)
227 stream << endl; //only write a new line if there are more text
228 else
229 break;
231 file.close();
233 else
234 KMessageBox::error(0,i18n("An error appeared when saving this file"));
236 if ( !KIO::NetAccess::upload(tmpfile.name(),url, this) )
238 KMessageBox::error(0,i18n("An error appeared when saving this file"));
239 tmpfile.unlink();
240 return;
242 tmpfile.unlink();
244 else
246 file.setName(url.prettyURL(0,KURL::StripFileProtocol));
247 if (file.open( IO_WriteOnly ) )
249 QTextStream stream(&file);
250 QListBoxItem *it = list->firstItem();
251 while ( 1 )
253 stream << it->text();
254 it = it->next();
255 if (it)
256 stream << endl; //only write a new line if there are more text
257 else
258 break;
260 file.close();
262 else
263 KMessageBox::error(0,i18n("An error appeared when saving this file"));
270 void KParameterEditor::varlist_clicked( QListBoxItem * item )
272 if (item)
274 cmdEdit->setEnabled(true);
275 cmdDelete->setEnabled(true);
277 else
279 cmdEdit->setEnabled(false);
280 cmdDelete->setEnabled(false);
285 void KParameterEditor::varlist_doubleClicked( QListBoxItem * )
287 cmdEdit_clicked();
290 bool KParameterEditor::checkTwoOfIt(const QString & text)
292 if ( list->findItem(text,Qt::ExactMatch) == 0)
293 return false;
294 else
295 return true;
298 #include "kparametereditor.moc"