kparametereditor.cpp 8.51 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
#include <klocale.h>
#include <kmessagebox.h>
#include <kpushbutton.h>
Jaison Lee's avatar
Jaison Lee committed
34
#include <ktemporaryfile.h>
35
#include <kurl.h>
36 37
#include <qfile.h>
#include <qtextstream.h>
38
#include <QList>
39
#include <QListWidget>
40

41 42
#include "kparametereditor.h"

43 44
#include <assert.h>

45 46
class ParameterValueList;

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

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

    setCaption( i18n("Parameter Editor") );
    setButtons(  Ok|Cancel );
59
	
60
	foreach ( const Value &v, *m_parameter )
61
		m_mainWidget->list->addItem( v.expression() );
62
	
63 64 65 66
	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() ));
67 68 69 70
	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() ) );
71
	connect( m_mainWidget->value, SIGNAL( returnPressed() ), m_mainWidget->cmdNew, SLOT( animateClick() ) );
72 73
	
	checkValueValid();
74
	
75
	m_mainWidget->value->setFocus();
76 77 78 79
}

KParameterEditor::~KParameterEditor()
{
80 81
}

82

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

102

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

110 111

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

117

118 119
void KParameterEditor::cmdDelete_clicked()
{
120 121 122 123 124 125 126 127 128
	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 );
129 130
}

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

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();
146 147 148
	Parser::Error error;
	(double) XParser::self()->eval( valueText, & error );
	bool valid = (error == Parser::ParseSuccess);
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 192 193
			
			Parser::Error error;
			XParser::self()->eval( line, & error );
			if ( error == Parser::ParseSuccess )
194
			{
195 196
				if ( !checkTwoOfIt(line) )
				{
197
					m_mainWidget->list->addItem(line);
198
				}
199
			}
200 201
			else if ( !verbose)
			{
202
				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)
203 204
				{
					file.close();
205
                                        KIO::NetAccess::removeTempFile( tmpfile );
206 207
					return;
				}
208
				else if (KMessageBox::warningYesNo(this,i18n("Would you like to be informed about other lines that cannot be read?"), QString(), KGuiItem(i18n("Get Informed")), KGuiItem(i18n("Ignore Information")) ) == KMessageBox::No)
209 210
					verbose = true;
			}
211 212 213
		}
		file.close();
	}
214
	else
215
		KMessageBox::sorry(0,i18n("An error appeared when opening this file"));
216
        
217 218 219 220
        if ( !url.isLocalFile() )
                KIO::NetAccess::removeTempFile( tmpfile );
}

221

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

230
        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 )
231 232 233
        {
                if ( !url.isLocalFile() )
                {
Jaison Lee's avatar
Jaison Lee committed
234
                        KTemporaryFile tmpfile;
235
                        
Jaison Lee's avatar
Jaison Lee committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249
			if (tmpfile.open() )
			{
				QTextStream stream(&tmpfile);
				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
				}
				stream.flush();
			}
			else
				KMessageBox::sorry(0,i18n("An error appeared when saving this file"));
250
                        
Jaison Lee's avatar
Jaison Lee committed
251
                        if ( !KIO::NetAccess::upload(tmpfile.fileName(),url, this) )
252
                        {
Jaison Lee's avatar
Jaison Lee committed
253
				KMessageBox::sorry(0,i18n("An error appeared when saving this file"));
254 255 256 257 258
                                return;
                        }
                }
                else
                {
Jaison Lee's avatar
Jaison Lee committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272
	                QFile file;
			kDebug() << "url.path()="<<url.path()<<endl;
			file.setFileName(url.path());
			if (file.open( QIODevice::WriteOnly ) )
			{
				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();
273 274
                        }
                        else
Jaison Lee's avatar
Jaison Lee committed
275
				KMessageBox::sorry(0,i18n("An error appeared when saving this file"));
276 277 278 279
                }
        }


280 281
}

282

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

288
#include "kparametereditor.moc"