kparametereditor.cpp 10.1 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
*
*/

Peter Hedlund's avatar
Peter Hedlund committed
26
#include "kparametereditor.h"
27 28

#include <kdebug.h>
29
#include <kfiledialog.h>
30
#include <KIcon>
31
#include <kinputdialog.h>
32
#include <kio/netaccess.h>
33 34 35
#include <klocale.h>
#include <kmessagebox.h>
#include <kpushbutton.h>
Jaison Lee's avatar
Jaison Lee committed
36
#include <ktemporaryfile.h>
37
#include <kurl.h>
38 39
#include <qfile.h>
#include <qtextstream.h>
40
#include <QList>
41
#include <QListWidget>
42

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
	m_mainWidget = new QParameterEditor( this );
55
	m_mainWidget->layout()->setMargin( 0 );
56
	setMainWidget( m_mainWidget );
57 58 59 60 61 62 63 64 65 66 67 68
	
	m_mainWidget->cmdNew->setIcon( KIcon("document-new" ) );
	m_mainWidget->cmdDelete->setIcon( KIcon("edit-delete" ) );
	m_mainWidget->moveUp->setIcon( KIcon("go-up") );
	m_mainWidget->moveDown->setIcon( KIcon("go-down") );
	m_mainWidget->cmdImport->setIcon( KIcon("document-open") );
	m_mainWidget->cmdExport->setIcon( KIcon("document-save") );
	
	m_mainWidget->list->setFocusPolicy( Qt::NoFocus );
	
	connect( m_mainWidget->value, SIGNAL(upPressed()), this, SLOT(prev()) );
	connect( m_mainWidget->value, SIGNAL(downPressed()), this, SLOT(next()) );
69
	
70
	foreach ( const Value &v, *m_parameter )
71
		m_mainWidget->list->addItem( v.expression() );
72
	
73 74
	connect( m_mainWidget->cmdNew, SIGNAL( clicked() ), this, SLOT( cmdNew_clicked() ));
	connect( m_mainWidget->cmdDelete, SIGNAL( clicked() ), this, SLOT( cmdDelete_clicked() ));
75 76
	connect( m_mainWidget->moveUp, SIGNAL(clicked()), this, SLOT(moveUp()) );
	connect( m_mainWidget->moveDown, SIGNAL(clicked()), this, SLOT(moveDown()) );
77 78
	connect( m_mainWidget->cmdImport, SIGNAL( clicked() ), this, SLOT( cmdImport_clicked() ));
	connect( m_mainWidget->cmdExport, SIGNAL( clicked() ), this, SLOT( cmdExport_clicked() ));
79 80 81 82
	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() ) );
83
	connect( m_mainWidget->value, SIGNAL( returnPressed() ), m_mainWidget->cmdNew, SLOT( animateClick() ) );
84 85
	
	checkValueValid();
86
	
87
	m_mainWidget->value->setFocus();
88 89 90 91
}

KParameterEditor::~KParameterEditor()
{
92 93
}

94

95 96 97
void KParameterEditor::accept()
{
	kDebug() << "saving\n";
98
	m_parameter->clear();
99
	QString item_text;
100
	for ( int i = 0; i < m_mainWidget->list->count(); i++ )
101
	{
102
		item_text = m_mainWidget->list->item(i)->text();
103
		if ( !item_text.isEmpty() )
104 105 106 107 108
		{
			Value value;
			if ( value.updateExpression( item_text ) )
				m_parameter->append( value );
		}
109
	}
110 111
	
	KDialog::accept();
112 113
}

114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
void KParameterEditor::moveUp()
{
	int current = m_mainWidget->list->currentRow();
	
	if ( current == 0 )
		return;
	
	QListWidgetItem * item = m_mainWidget->list->takeItem( current-1 );
	m_mainWidget->list->insertItem( current, item );
}


void KParameterEditor::moveDown()
{
	int current = m_mainWidget->list->currentRow();
	
	if ( current == m_mainWidget->list->count() - 1 )
		return;
	
	QListWidgetItem * item = m_mainWidget->list->takeItem( current+1 );
	m_mainWidget->list->insertItem( current, item );
}


139 140
void KParameterEditor::cmdNew_clicked()
{
141
	QListWidgetItem * item = new QListWidgetItem( m_mainWidget->list );
142
	item->setText( "0" );
143 144
	m_mainWidget->list->setCurrentItem( item );
	m_mainWidget->value->setFocus();
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	m_mainWidget->value->selectAll();
}


void KParameterEditor::prev()
{
	int current = m_mainWidget->list->currentRow();
	if ( current > 0 )
		m_mainWidget->list->setCurrentRow( current-1 );
}


void KParameterEditor::next()
{
	int current = m_mainWidget->list->currentRow();
	if ( current < m_mainWidget->list->count()-1 )
		m_mainWidget->list->setCurrentRow( current+1 );
	else
		cmdNew_clicked();
164 165
}

166 167

void KParameterEditor::selectedConstantChanged( QListWidgetItem * current )
168
{
169
	m_mainWidget->cmdDelete->setEnabled( current != 0 );
Peter Hedlund's avatar
Peter Hedlund committed
170
	m_mainWidget->value->setText( current ? current->text() : QString() );
171
	m_mainWidget->value->selectAll();
172 173
}

174

175 176
void KParameterEditor::cmdDelete_clicked()
{
177 178 179 180 181 182 183 184 185
	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 );
186 187
}

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

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();
203 204 205
	Parser::Error error;
	(double) XParser::self()->eval( valueText, & error );
	bool valid = (error == Parser::ParseSuccess);
206 207 208 209 210
	m_mainWidget->valueInvalidLabel->setVisible( !valueText.isEmpty() && !valid );
	return valid;
}


211 212
void KParameterEditor::cmdImport_clicked()
{
David Saxton's avatar
David Saxton committed
213
	KUrl url = KFileDialog::getOpenUrl( QString(),i18n("*.txt|Plain Text File "));
214
	if ( url.isEmpty() )
215
		return;
216 217 218
        
        if (!KIO::NetAccess::exists(url,true,this) )
        {
219
			KMessageBox::sorry(0,i18n("The file does not exist."));
220 221 222
                return;
        }
        
223
	bool verbose = false;
224 225 226 227 228 229
        QFile file;
        QString tmpfile;
        if ( !url.isLocalFile() )
        {
                if ( !KIO::NetAccess::download(url, tmpfile, this) )
                {
230
					KMessageBox::sorry(0,i18n("An error appeared when opening this file"));
231 232
                        return;
                }
233
                file.setFileName(tmpfile);
234 235
        }
        else
236
                file.setFileName(url.path() );
237
	
Laurent Montel's avatar
Laurent Montel committed
238
	if ( file.open(QIODevice::ReadOnly) )
239 240 241
	{
		QTextStream stream(&file);
		QString line;
242
		for( int i=1; !stream.atEnd();i++ )
243 244
		{
			line = stream.readLine();
245 246
			if (line.isEmpty())
				continue;
247 248 249 250
			
			Parser::Error error;
			XParser::self()->eval( line, & error );
			if ( error == Parser::ParseSuccess )
251
			{
252 253
				if ( !checkTwoOfIt(line) )
				{
254
					m_mainWidget->list->addItem(line);
255
				}
256
			}
257 258
			else if ( !verbose)
			{
259
				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)
260 261
				{
					file.close();
262
                                        KIO::NetAccess::removeTempFile( tmpfile );
263 264
					return;
				}
265
				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)
266 267
					verbose = true;
			}
268 269 270
		}
		file.close();
	}
271
	else
272
		KMessageBox::sorry(0,i18n("An error appeared when opening this file"));
273
        
274 275 276 277
        if ( !url.isLocalFile() )
                KIO::NetAccess::removeTempFile( tmpfile );
}

278

279 280
void KParameterEditor::cmdExport_clicked()
{
281
	if ( !m_mainWidget->list->count() )
282
                return;
David Saxton's avatar
David Saxton committed
283
	KUrl url = KFileDialog::getSaveUrl( QString(),i18n("*.txt|Plain Text File "));
284 285 286
        if ( url.isEmpty() )
                return;

287
        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 )
288 289 290
        {
                if ( !url.isLocalFile() )
                {
Jaison Lee's avatar
Jaison Lee committed
291
                        KTemporaryFile tmpfile;
292
                        
Jaison Lee's avatar
Jaison Lee committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306
			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"));
307
                        
Jaison Lee's avatar
Jaison Lee committed
308
                        if ( !KIO::NetAccess::upload(tmpfile.fileName(),url, this) )
309
                        {
Jaison Lee's avatar
Jaison Lee committed
310
				KMessageBox::sorry(0,i18n("An error appeared when saving this file"));
311 312 313 314 315
                                return;
                        }
                }
                else
                {
Jaison Lee's avatar
Jaison Lee committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329
	                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();
330 331
                        }
                        else
Jaison Lee's avatar
Jaison Lee committed
332
				KMessageBox::sorry(0,i18n("An error appeared when saving this file"));
333 334 335 336
                }
        }


337 338
}

339

340
bool KParameterEditor::checkTwoOfIt(const QString & text)
341
{
342
	return !m_mainWidget->list->findItems(text,Qt::MatchExactly).isEmpty();
343 344
}

345
#include "kparametereditor.moc"