kparametereditor.cpp 8.52 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>
Jaison Lee's avatar
Jaison Lee committed
35
#include <ktemporaryfile.h>
36
#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 149
	Parser::Error error;
	(double) XParser::self()->eval( valueText, & error );
	bool valid = (error == Parser::ParseSuccess);
150 151 152 153 154
	m_mainWidget->valueInvalidLabel->setVisible( !valueText.isEmpty() && !valid );
	return valid;
}


155 156
void KParameterEditor::cmdImport_clicked()
{
David Saxton's avatar
David Saxton committed
157
	KUrl url = KFileDialog::getOpenUrl( QString(),i18n("*.txt|Plain Text File "));
158
	if ( url.isEmpty() )
159
		return;
160 161 162
        
        if (!KIO::NetAccess::exists(url,true,this) )
        {
163
			KMessageBox::sorry(0,i18n("The file does not exist."));
164 165 166
                return;
        }
        
167
	bool verbose = false;
168 169 170 171 172 173
        QFile file;
        QString tmpfile;
        if ( !url.isLocalFile() )
        {
                if ( !KIO::NetAccess::download(url, tmpfile, this) )
                {
174
					KMessageBox::sorry(0,i18n("An error appeared when opening this file"));
175 176
                        return;
                }
177
                file.setFileName(tmpfile);
178 179
        }
        else
180
                file.setFileName(url.path() );
181
	
Laurent Montel's avatar
Laurent Montel committed
182
	if ( file.open(QIODevice::ReadOnly) )
183 184 185
	{
		QTextStream stream(&file);
		QString line;
186
		for( int i=1; !stream.atEnd();i++ )
187 188
		{
			line = stream.readLine();
189 190
			if (line.isEmpty())
				continue;
191 192 193 194
			
			Parser::Error error;
			XParser::self()->eval( line, & error );
			if ( error == Parser::ParseSuccess )
195
			{
196 197
				if ( !checkTwoOfIt(line) )
				{
198
					m_mainWidget->list->addItem(line);
199
				}
200
			}
201 202
			else if ( !verbose)
			{
203
				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)
204 205
				{
					file.close();
206
                                        KIO::NetAccess::removeTempFile( tmpfile );
207 208
					return;
				}
209
				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)
210 211
					verbose = true;
			}
212 213 214
		}
		file.close();
	}
215
	else
216
		KMessageBox::sorry(0,i18n("An error appeared when opening this file"));
217
        
218 219 220 221
        if ( !url.isLocalFile() )
                KIO::NetAccess::removeTempFile( tmpfile );
}

222

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

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


281 282
}

283

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

289
#include "kparametereditor.moc"