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.
28 #include <kfiledialog.h>
29 #include <kinputdialog.h>
30 #include <kio/netaccess.h>
33 #include <kmessagebox.h>
34 #include <kpushbutton.h>
35 #include <ktempfile.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
);
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()
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()
81 result
= KInputDialog::getText( i18n("Parameter Value"), i18n( "Enter a new parameter value:" ), result
, &ok
);
84 m_parser
->eval( result
);
85 if ( m_parser
->parserError(false) != 0 )
87 m_parser
->parserError();
90 if ( checkTwoOfIt(result
) )
92 KMessageBox::error(0,i18n("The value %1 already exists and will therefore not be added.").arg(result
));
95 list
->insertItem(result
);
101 void KParameterEditor::cmdEdit_clicked()
103 QString result
=list
->currentText();
107 result
= KInputDialog::getText( i18n("Parameter Value"), i18n( "Enter a new parameter value:" ), result
, &ok
);
110 m_parser
->eval(result
);
111 if ( m_parser
->parserError(false) != 0)
113 m_parser
->parserError();
116 if ( checkTwoOfIt(result
) )
118 if( result
!= list
->currentText() )
119 KMessageBox::error(0,i18n("The value %1 already exists.").arg(result
));
122 list
->removeItem( list
->currentItem());
123 list
->insertItem(result
);
129 void KParameterEditor::cmdDelete_clicked()
131 list
->removeItem( list
->currentItem());
135 void KParameterEditor::cmdImport_clicked()
137 KURL url
= KFileDialog::getOpenURL( QString::null
,i18n("*.txt|Plain Text File "));
141 if (!KIO::NetAccess::exists(url
,true,this) )
143 KMessageBox::error(0,i18n("The file does not exist."));
147 bool verbose
= false;
150 if ( !url
.isLocalFile() )
152 if ( !KIO::NetAccess::download(url
, tmpfile
, this) )
154 KMessageBox::error(0,i18n("An error appeared when opening this file"));
157 file
.setName(tmpfile
);
160 file
.setName(url
.prettyURL(0,KURL::StripFileProtocol
) );
162 if ( file
.open(IO_ReadOnly
) )
164 QTextStream
stream(&file
);
166 for( int i
=1; !stream
.atEnd();i
++ )
168 line
= stream
.readLine();
171 m_parser
->eval( line
);
172 if ( m_parser
->parserError(false) == 0)
174 if ( !checkTwoOfIt(line
) )
176 list
->insertItem(line
);
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
)
185 KIO::NetAccess::removeTempFile( tmpfile
);
188 else if (KMessageBox::warningYesNo(this,i18n("Would you like to be informed about other lines that cannot be read?") ) == KMessageBox::No
)
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() )
205 KURL url
= KFileDialog::getSaveURL( QString::null
,i18n("*.txt|Plain Text File "));
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
)
213 if ( !url
.isLocalFile() )
216 file
.setName(tmpfile
.name() );
218 if (file
.open( IO_WriteOnly
) )
220 QTextStream
stream(&file
);
221 QListBoxItem
*it
= list
->firstItem();
224 stream
<< it
->text();
227 stream
<< endl
; //only write a new line if there are more text
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"));
246 file
.setName(url
.prettyURL(0,KURL::StripFileProtocol
));
247 if (file
.open( IO_WriteOnly
) )
249 QTextStream
stream(&file
);
250 QListBoxItem
*it
= list
->firstItem();
253 stream
<< it
->text();
256 stream
<< endl
; //only write a new line if there are more text
263 KMessageBox::error(0,i18n("An error appeared when saving this file"));
270 void KParameterEditor::varlist_clicked( QListBoxItem
* item
)
274 cmdEdit
->setEnabled(true);
275 cmdDelete
->setEnabled(true);
279 cmdEdit
->setEnabled(false);
280 cmdDelete
->setEnabled(false);
285 void KParameterEditor::varlist_doubleClicked( QListBoxItem
* )
290 bool KParameterEditor::checkTwoOfIt(const QString
& text
)
292 if ( list
->findItem(text
,Qt::ExactMatch
) == 0)
298 #include "kparametereditor.moc"