kparametereditor.cpp 9.34 KB
Newer Older
1 2 3 4 5
/*
* KmPlot - a math. function plotter for the KDE-Desktop
*
* Copyright (C) 2004  Fredrik Edemar
*                     f_edemar@linux.se
6
*               2006  David Saxton <david@bluehaze.org>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
*               
* 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
23
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24 25 26 27 28
*
*/


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

43 44
#include "kparametereditor.h"

45 46
class ParameterValueList;

47
KParameterEditor::KParameterEditor(XParser *m, QList<ParameterValueItem> *l, QWidget *parent )
48 49 50
	: KDialog( parent, i18n("Parameter Editor"), Ok ),
	  m_parameter(l),
	  m_parser(m)
51
{
52 53 54
	m_mainWidget = new QParameterEditor( this );
	setMainWidget( m_mainWidget );
	
55
	for (  QList<ParameterValueItem>::Iterator it = m_parameter->begin(); it != m_parameter->end(); ++it )
56 57
		m_mainWidget->list->addItem( (*it).expression );
	m_mainWidget->list->sortItems();
58
	
59 60 61 62 63
	connect( m_mainWidget->cmdNew, SIGNAL( clicked() ), this, SLOT( cmdNew_clicked() ));
	connect( m_mainWidget->cmdEdit, SIGNAL( clicked() ), this, SLOT( cmdEdit_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() ));
64 65
	connect( m_mainWidget->list, SIGNAL( itemDoubleClicked( QListWidgetItem * ) ), this, SLOT( varlist_doubleClicked( QListWidgetItem *) ));
	connect( m_mainWidget->list, SIGNAL( itemClicked ( QListWidgetItem * ) ), this, SLOT( varlist_clicked(QListWidgetItem *  ) ));
66 67 68 69 70
	
}

KParameterEditor::~KParameterEditor()
{
71 72 73 74 75
}

void KParameterEditor::accept()
{
	kDebug() << "saving\n";
76
	m_parameter->clear();
77
	QString item_text;
78
	for ( int i = 0; i < m_mainWidget->list->count(); i++ )
79
	{
80
		item_text = m_mainWidget->list->item(i)->text();
81 82
		if ( !item_text.isEmpty() )
			m_parameter->append( ParameterValueItem(item_text, m_parser->eval( item_text)) );
83
	}
84 85
	
	KDialog::accept();
86 87 88 89
}

void KParameterEditor::cmdNew_clicked()
{
90 91
	QString result="";
	while (1)
92
	{
93 94 95 96 97
		bool ok;
		result = KInputDialog::getText( i18n("Parameter Value"), i18n( "Enter a new parameter value:" ), result, &ok );
		if ( !ok)
			return;
		m_parser->eval( result );
98
		if ( m_parser->parserError(false) != 0 )
99
		{
100
			m_parser->parserError();
101 102 103 104
			continue;
		}
		if ( checkTwoOfIt(result) )
		{
105
			KMessageBox::sorry(0,i18n("The value %1 already exists and will therefore not be added.").arg(result));
106 107
			continue;
		}
108 109
		m_mainWidget->list->addItem(result);
		m_mainWidget->list->sortItems();
110
		break;
111
	}
112 113 114 115
}

void KParameterEditor::cmdEdit_clicked()
{
116 117 118
	QListWidgetItem * currentItem = m_mainWidget->list->currentItem();
	QString result = currentItem ? currentItem->text() : QString::null;
	
119
	while (1)
120
	{
121 122 123 124 125
		bool ok;
		result = KInputDialog::getText( i18n("Parameter Value"), i18n( "Enter a new parameter value:" ), result, &ok );
		if ( !ok)
			return;
		m_parser->eval(result);
126
		if ( m_parser->parserError(false) != 0)
127
		{
128
			m_parser->parserError();
129 130 131 132
			continue;
		}
		if ( checkTwoOfIt(result) )
		{
133 134 135 136 137
			currentItem = m_mainWidget->list->currentItem();
			QString currentText = currentItem ? currentItem->text() : QString::null;
			
			if( result != currentText )
				KMessageBox::sorry(0,i18n("The value %1 already exists.").arg(result));
138 139
			continue;
		}
140 141 142
		m_mainWidget->list->takeItem( m_mainWidget->list->currentRow() );
		m_mainWidget->list->addItem(result);
		m_mainWidget->list->sortItems();
143
		break;
144
	}
145 146 147 148
}

void KParameterEditor::cmdDelete_clicked()
{
149 150
	delete m_mainWidget->list->takeItem( m_mainWidget->list->currentRow() );
	m_mainWidget->list->sortItems();
151 152
}

153 154
void KParameterEditor::cmdImport_clicked()
{
Laurent Montel's avatar
Laurent Montel committed
155
	KUrl url = KFileDialog::getOpenURL( QString(),i18n("*.txt|Plain Text File "));
156
	if ( url.isEmpty() )
157
		return;
158 159 160
        
        if (!KIO::NetAccess::exists(url,true,this) )
        {
161
			KMessageBox::sorry(0,i18n("The file does not exist."));
162 163 164
                return;
        }
        
165
	bool verbose = false;
166 167 168 169 170 171
        QFile file;
        QString tmpfile;
        if ( !url.isLocalFile() )
        {
                if ( !KIO::NetAccess::download(url, tmpfile, this) )
                {
172
					KMessageBox::sorry(0,i18n("An error appeared when opening this file"));
173 174
                        return;
                }
175
                file.setFileName(tmpfile);
176 177
        }
        else
178
                file.setFileName(url.prettyURL(0) );
179
	
Laurent Montel's avatar
Laurent Montel committed
180
	if ( file.open(QIODevice::ReadOnly) )
181 182 183
	{
		QTextStream stream(&file);
		QString line;
184
		for( int i=1; !stream.atEnd();i++ )
185 186
		{
			line = stream.readLine();
187 188
			if (line.isEmpty())
				continue;
189
			m_parser->eval( line );
190
			if ( m_parser->parserError(false) == 0)
191
			{
192 193
				if ( !checkTwoOfIt(line) )
				{
194 195
					m_mainWidget->list->addItem(line);
					m_mainWidget->list->sortItems();
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?").arg(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 219 220
        if ( !url.isLocalFile() )
                KIO::NetAccess::removeTempFile( tmpfile );
}

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

        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?" ).arg( url.url()), i18n( "Overwrite File?" ), KGuiItem( i18n( "&Overwrite" ) ) ) == KMessageBox::Continue )
        {
                QString tmpfile;
                QFile file;
                if ( !url.isLocalFile() )
                {
                        KTempFile tmpfile;
234
                        file.setFileName(tmpfile.name() );
235
                        
Laurent Montel's avatar
Laurent Montel committed
236
                        if (file.open( QIODevice::WriteOnly ) )
237
                        {
238 239 240 241 242 243 244 245 246
							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();
247
                        }
248 249
						else
							KMessageBox::sorry(0,i18n("An error appeared when saving this file"));
250 251 252
                        
                        if ( !KIO::NetAccess::upload(tmpfile.name(),url, this) )
                        {
253
							KMessageBox::sorry(0,i18n("An error appeared when saving this file"));
254 255 256 257 258 259 260
                                tmpfile.unlink();
                                return;
                        }
                        tmpfile.unlink();
                }
                else
                {
261
                        file.setFileName(url.prettyURL(0));
Laurent Montel's avatar
Laurent Montel committed
262
                        if (file.open( QIODevice::WriteOnly ) )
263
                        {
264 265 266 267 268 269 270 271 272
							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
275
							KMessageBox::sorry(0,i18n("An error appeared when saving this file"));
276 277 278 279
                }
        }


280 281
}

282
void KParameterEditor::varlist_clicked( QListWidgetItem * item )
283 284 285
{
	if (item)
	{
286 287
		m_mainWidget->cmdEdit->setEnabled(true);
		m_mainWidget->cmdDelete->setEnabled(true);
288 289 290
	}
	else
	{
291 292
		m_mainWidget->cmdEdit->setEnabled(false);
		m_mainWidget->cmdDelete->setEnabled(false);		
293 294 295 296
	}
}


297
void KParameterEditor::varlist_doubleClicked( QListWidgetItem * )
298 299 300 301
{
	cmdEdit_clicked();
}

302
bool KParameterEditor::checkTwoOfIt(const QString & text)
303
{
304
	return !m_mainWidget->list->findItems(text,Qt::MatchExactly).isEmpty();
305 306
}

307
#include "kparametereditor.moc"