kparametereditor.cpp 10.2 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 <kinputdialog.h>
31
#include <kio/netaccess.h>
32 33 34
#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 <assert.h>

44 45
class ParameterValueList;

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

53
	m_mainWidget = new QParameterEditor( this );
54
	m_mainWidget->layout()->setMargin( 0 );
55
	setMainWidget( m_mainWidget );
56
	
Christoph Feck's avatar
Christoph Feck committed
57 58 59 60 61 62
	m_mainWidget->cmdNew->setIcon( QIcon::fromTheme("document-new" ) );
	m_mainWidget->cmdDelete->setIcon( QIcon::fromTheme("edit-delete" ) );
	m_mainWidget->moveUp->setIcon( QIcon::fromTheme("go-up") );
	m_mainWidget->moveDown->setIcon( QIcon::fromTheme("go-down") );
	m_mainWidget->cmdImport->setIcon( QIcon::fromTheme("document-open") );
	m_mainWidget->cmdExport->setIcon( QIcon::fromTheme("document-save") );
63 64 65
	
	m_mainWidget->list->setFocusPolicy( Qt::NoFocus );
	
Laurent Montel's avatar
Laurent Montel committed
66 67
	connect(m_mainWidget->value, &EquationEdit::upPressed, this, &KParameterEditor::prev);
	connect(m_mainWidget->value, &EquationEdit::downPressed, this, &KParameterEditor::next);
68
	
69
	foreach ( const Value &v, *m_parameter )
70
		m_mainWidget->list->addItem( v.expression() );
71
	
Laurent Montel's avatar
Laurent Montel committed
72 73 74 75 76 77 78
	connect(m_mainWidget->cmdNew, &QPushButton::clicked, this, &KParameterEditor::cmdNew_clicked);
	connect(m_mainWidget->cmdDelete, &QPushButton::clicked, this, &KParameterEditor::cmdDelete_clicked);
	connect(m_mainWidget->moveUp, &QPushButton::clicked, this, &KParameterEditor::moveUp);
	connect(m_mainWidget->moveDown, &QPushButton::clicked, this, &KParameterEditor::moveDown);
	connect(m_mainWidget->cmdImport, &QPushButton::clicked, this, &KParameterEditor::cmdImport_clicked);
	connect(m_mainWidget->cmdExport, &QPushButton::clicked, this, &KParameterEditor::cmdExport_clicked);
	connect(m_mainWidget->list, &QListWidget::currentItemChanged, this, &KParameterEditor::selectedConstantChanged);
79
	
Laurent Montel's avatar
Laurent Montel committed
80 81
	connect(m_mainWidget->value, &EquationEdit::textEdited, this, &KParameterEditor::saveCurrentValue);
	connect(m_mainWidget->value, &EquationEdit::textChanged, this, &KParameterEditor::checkValueValid);
82
	connect( m_mainWidget->value, SIGNAL( returnPressed() ), m_mainWidget->cmdNew, SLOT( animateClick() ) );
83 84
	
	checkValueValid();
85
	
86
	m_mainWidget->value->setFocus();
87 88 89 90
}

KParameterEditor::~KParameterEditor()
{
91 92
}

93

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


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

165 166

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

173

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

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

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


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

277

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

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


336 337
}

338

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