kparametereditor.cpp 8.83 KB
Newer Older
1 2 3
/*
* KmPlot - a math. function plotter for the KDE-Desktop
*
4
* Copyright (C) 2004  Fredrik Edemar <f_edemar@linux.se>
5
*               2006  David Saxton <david@bluehaze.org>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
*               
* This file is part of the KDE Project.
* KmPlot is part of the KDE-EDU Project.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
Dirk Mueller's avatar
Dirk Mueller committed
22
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 24 25 26 27
*
*/


#include <kdebug.h>
28
#include <kfiledialog.h>
29
#include <kinputdialog.h>
30
#include <kio/netaccess.h>
31 32 33 34
#include <klistbox.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kpushbutton.h>
35 36
#include <ktempfile.h>
#include <kurl.h>
37 38
#include <qfile.h>
#include <qtextstream.h>
39
#include <QList>
40
#include <QListWidget>
41

42 43
#include "kparametereditor.h"

44 45
#include <assert.h>

46 47
class ParameterValueList;

48
KParameterEditor::KParameterEditor( QList<Value> *l, QWidget *parent )
Fredrik Edemar's avatar
Fredrik Edemar committed
49
	: KDialog( parent ),
50
	  m_parameter(l)
51
{
Pino Toscano's avatar
Pino Toscano committed
52 53 54
	setCaption( i18n( "Parameter Editor" ) );
	setButtons( Ok | Cancel );

55 56
	m_mainWidget = new QParameterEditor( this );
	setMainWidget( m_mainWidget );
Fredrik Edemar's avatar
Fredrik Edemar committed
57 58 59

    setCaption( i18n("Parameter Editor") );
    setButtons(  Ok|Cancel );
60
	
61 62
	foreach ( Value v, *m_parameter )
		m_mainWidget->list->addItem( v.expression() );
63
	
64 65 66 67
	connect( m_mainWidget->cmdNew, SIGNAL( clicked() ), this, SLOT( cmdNew_clicked() ));
	connect( m_mainWidget->cmdDelete, SIGNAL( clicked() ), this, SLOT( cmdDelete_clicked() ));
	connect( m_mainWidget->cmdImport, SIGNAL( clicked() ), this, SLOT( cmdImport_clicked() ));
	connect( m_mainWidget->cmdExport, SIGNAL( clicked() ), this, SLOT( cmdExport_clicked() ));
68 69 70 71
	connect( m_mainWidget->list, SIGNAL(currentItemChanged( QListWidgetItem *, QListWidgetItem * )), this, SLOT(selectedConstantChanged( QListWidgetItem * )) );
	
	connect( m_mainWidget->value, SIGNAL( textEdited( const QString & ) ), this, SLOT( saveCurrentValue() ) );
	connect( m_mainWidget->value, SIGNAL( textChanged( const QString & ) ), this, SLOT( checkValueValid() ) );
72
	connect( m_mainWidget->value, SIGNAL( returnPressed() ), m_mainWidget->cmdNew, SLOT( animateClick() ) );
73 74
	
	checkValueValid();
75
	
76
	m_mainWidget->value->setFocus();
77 78 79 80
}

KParameterEditor::~KParameterEditor()
{
81 82
}

83

84 85 86
void KParameterEditor::accept()
{
	kDebug() << "saving\n";
87
	m_parameter->clear();
88
	QString item_text;
89
	for ( int i = 0; i < m_mainWidget->list->count(); i++ )
90
	{
91
		item_text = m_mainWidget->list->item(i)->text();
92
		if ( !item_text.isEmpty() )
93 94 95 96 97
		{
			Value value;
			if ( value.updateExpression( item_text ) )
				m_parameter->append( value );
		}
98
	}
99 100
	
	KDialog::accept();
101 102
}

103

104 105
void KParameterEditor::cmdNew_clicked()
{
106 107 108
	QListWidgetItem * item = new QListWidgetItem( m_mainWidget->list );
	m_mainWidget->list->setCurrentItem( item );
	m_mainWidget->value->setFocus();
109 110
}

111 112

void KParameterEditor::selectedConstantChanged( QListWidgetItem * current )
113
{
114 115
	m_mainWidget->cmdDelete->setEnabled( current != 0 );
	m_mainWidget->value->setText( current ? current->text() : QString::null );
116 117
}

118

119 120
void KParameterEditor::cmdDelete_clicked()
{
121 122 123 124 125 126 127 128 129
	QListWidgetItem * item = m_mainWidget->list->currentItem();
	if ( !item )
		return;
	
	m_mainWidget->value->clear();
	m_mainWidget->list->takeItem( m_mainWidget->list->currentRow() );
	delete item;
	
	m_mainWidget->cmdDelete->setEnabled( m_mainWidget->list->currentItem() != 0 );
130 131
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

void KParameterEditor::saveCurrentValue()
{
	QListWidgetItem * current = m_mainWidget->list->currentItem();
	if ( !current )
		current = new QListWidgetItem( m_mainWidget->list );
	
	current->setText( m_mainWidget->value->text() );
	m_mainWidget->list->setCurrentItem( current );
}


bool KParameterEditor::checkValueValid()
{
	QString valueText = m_mainWidget->value->text();
147 148
	(double) XParser::self()->eval( valueText );
	bool valid = (XParser::self()->parserError( false ) == 0);
149 150 151 152 153
	m_mainWidget->valueInvalidLabel->setVisible( !valueText.isEmpty() && !valid );
	return valid;
}


154 155
void KParameterEditor::cmdImport_clicked()
{
David Saxton's avatar
David Saxton committed
156
	KUrl url = KFileDialog::getOpenUrl( QString(),i18n("*.txt|Plain Text File "));
157
	if ( url.isEmpty() )
158
		return;
159 160 161
        
        if (!KIO::NetAccess::exists(url,true,this) )
        {
162
			KMessageBox::sorry(0,i18n("The file does not exist."));
163 164 165
                return;
        }
        
166
	bool verbose = false;
167 168 169 170 171 172
        QFile file;
        QString tmpfile;
        if ( !url.isLocalFile() )
        {
                if ( !KIO::NetAccess::download(url, tmpfile, this) )
                {
173
					KMessageBox::sorry(0,i18n("An error appeared when opening this file"));
174 175
                        return;
                }
176
                file.setFileName(tmpfile);
177 178
        }
        else
179
                file.setFileName(url.path() );
180
	
Laurent Montel's avatar
Laurent Montel committed
181
	if ( file.open(QIODevice::ReadOnly) )
182 183 184
	{
		QTextStream stream(&file);
		QString line;
185
		for( int i=1; !stream.atEnd();i++ )
186 187
		{
			line = stream.readLine();
188 189
			if (line.isEmpty())
				continue;
190 191
			XParser::self()->eval( line );
			if ( XParser::self()->parserError(false) == 0)
192
			{
193 194
				if ( !checkTwoOfIt(line) )
				{
195
					m_mainWidget->list->addItem(line);
196
				}
197
			}
198 199
			else if ( !verbose)
			{
200
				if ( KMessageBox::warningContinueCancel(this,i18n("Line %1 is not a valid parameter value and will therefore not be included. Do you want to continue?", i) ) == KMessageBox::Cancel)
201 202
				{
					file.close();
203
                                        KIO::NetAccess::removeTempFile( tmpfile );
204 205
					return;
				}
Laurent Montel's avatar
Laurent Montel committed
206
				else if (KMessageBox::warningYesNo(this,i18n("Would you like to be informed about other lines that cannot be read?"), QString(), i18n("Get Informed"), i18n("Ignore Information") ) == KMessageBox::No)
207 208
					verbose = true;
			}
209 210 211
		}
		file.close();
	}
212
	else
213
		KMessageBox::sorry(0,i18n("An error appeared when opening this file"));
214
        
215 216 217 218
        if ( !url.isLocalFile() )
                KIO::NetAccess::removeTempFile( tmpfile );
}

219

220 221
void KParameterEditor::cmdExport_clicked()
{
222
	if ( !m_mainWidget->list->count() )
223
                return;
David Saxton's avatar
David Saxton committed
224
	KUrl url = KFileDialog::getSaveUrl( QString(),i18n("*.txt|Plain Text File "));
225 226 227
        if ( url.isEmpty() )
                return;

228
        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?", url.url()), i18n( "Overwrite File?" ), KGuiItem( i18n( "&Overwrite" ) ) ) == KMessageBox::Continue )
229 230 231 232 233 234
        {
                QString tmpfile;
                QFile file;
                if ( !url.isLocalFile() )
                {
                        KTempFile tmpfile;
235
                        file.setFileName(tmpfile.name() );
236
                        
Laurent Montel's avatar
Laurent Montel committed
237
                        if (file.open( QIODevice::WriteOnly ) )
238
                        {
239 240 241 242 243 244 245 246 247
							QTextStream stream(&file);
							for ( int i = 0; i < m_mainWidget->list->count(); i++ )
							{
								QListWidgetItem * it = m_mainWidget->list->item( i );
								stream << it->text();
								if ( i < m_mainWidget->list->count()-1 )
									stream << endl; //only write a new line if there are more text
							}
							file.close();
248
                        }
249 250
						else
							KMessageBox::sorry(0,i18n("An error appeared when saving this file"));
251 252 253
                        
                        if ( !KIO::NetAccess::upload(tmpfile.name(),url, this) )
                        {
254
							KMessageBox::sorry(0,i18n("An error appeared when saving this file"));
255 256 257 258 259 260 261
                                tmpfile.unlink();
                                return;
                        }
                        tmpfile.unlink();
                }
                else
                {
262 263
					kDebug() << "url.path()="<<url.path()<<endl;
                        file.setFileName(url.path());
Laurent Montel's avatar
Laurent Montel committed
264
                        if (file.open( QIODevice::WriteOnly ) )
265
                        {
266 267 268 269 270 271 272 273 274
							QTextStream stream(&file);
							for ( int i = 0; i < m_mainWidget->list->count(); i++ )
							{
								QListWidgetItem * it = m_mainWidget->list->item( i );
								stream << it->text();
								if ( i < m_mainWidget->list->count()-1 )
									stream << endl; //only write a new line if there are more text
							}
							file.close();
275 276
                        }
                        else
277
							KMessageBox::sorry(0,i18n("An error appeared when saving this file"));
278 279 280 281
                }
        }


282 283
}

284

285
bool KParameterEditor::checkTwoOfIt(const QString & text)
286
{
287
	return !m_mainWidget->list->findItems(text,Qt::MatchExactly).isEmpty();
288 289
}

290
#include "kparametereditor.moc"