kparametereditor.cpp 9.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
* KmPlot - a math. function plotter for the KDE-Desktop
*
* Copyright (C) 2004  Fredrik Edemar
*                     f_edemar@linux.se
*               
* 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>
35 36
#include <ktempfile.h>
#include <kurl.h>
37 38
#include <qfile.h>
#include <qtextstream.h>
39
#include <QList>
40
#include <q3listview.h>
41

42 43
#include "kparametereditor.h"

44 45
class ParameterValueList;

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

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

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

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

void KParameterEditor::cmdEdit_clicked()
{
115
	QString result=m_mainWidget->list->currentText();
116
	while (1)
117
	{
118 119 120 121 122
		bool ok;
		result = KInputDialog::getText( i18n("Parameter Value"), i18n( "Enter a new parameter value:" ), result, &ok );
		if ( !ok)
			return;
		m_parser->eval(result);
123
		if ( m_parser->parserError(false) != 0)
124
		{
125
			m_parser->parserError();
126 127 128 129
			continue;
		}
		if ( checkTwoOfIt(result) )
		{
130
			if( result != m_mainWidget->list->currentText() )
131 132 133
				KMessageBox::error(0,i18n("The value %1 already exists.").arg(result));
			continue;
		}
134 135 136
		m_mainWidget->list->removeItem( m_mainWidget->list->currentItem());
		m_mainWidget->list->insertItem(result);
		m_mainWidget->list->sort();
137
		break;
138
	}
139 140 141 142
}

void KParameterEditor::cmdDelete_clicked()
{
143 144
	m_mainWidget->list->removeItem( m_mainWidget->list->currentItem());
	m_mainWidget->list->sort();
145 146
}

147 148
void KParameterEditor::cmdImport_clicked()
{
Laurent Montel's avatar
Laurent Montel committed
149
	KUrl url = KFileDialog::getOpenURL( QString(),i18n("*.txt|Plain Text File "));
150
	if ( url.isEmpty() )
151
		return;
152 153 154
        
        if (!KIO::NetAccess::exists(url,true,this) )
        {
Andrew Coles's avatar
 
Andrew Coles committed
155
                KMessageBox::error(0,i18n("The file does not exist."));
156 157 158
                return;
        }
        
159
	bool verbose = false;
160 161 162 163 164 165 166 167 168 169 170 171
        QFile file;
        QString tmpfile;
        if ( !url.isLocalFile() )
        {
                if ( !KIO::NetAccess::download(url, tmpfile, this) )
                {
                        KMessageBox::error(0,i18n("An error appeared when opening this file"));
                        return;
                }
                file.setName(tmpfile);
        }
        else
Laurent Montel's avatar
Compile  
Laurent Montel committed
172
                file.setName(url.prettyURL(0) );
173
	
Laurent Montel's avatar
Laurent Montel committed
174
	if ( file.open(QIODevice::ReadOnly) )
175 176 177
	{
		QTextStream stream(&file);
		QString line;
178
		for( int i=1; !stream.atEnd();i++ )
179 180
		{
			line = stream.readLine();
181 182
			if (line.isEmpty())
				continue;
183
			m_parser->eval( line );
184
			if ( m_parser->parserError(false) == 0)
185
			{
186 187
				if ( !checkTwoOfIt(line) )
				{
188 189
					m_mainWidget->list->insertItem(line);
					m_mainWidget->list->sort();
190
				}
191
			}
192 193
			else if ( !verbose)
			{
194
				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)
195 196
				{
					file.close();
197
                                        KIO::NetAccess::removeTempFile( tmpfile );
198 199
					return;
				}
Laurent Montel's avatar
Laurent Montel committed
200
				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)
201 202
					verbose = true;
			}
203 204 205
		}
		file.close();
	}
206 207
	else
		KMessageBox::error(0,i18n("An error appeared when opening this file"));
208
        
209 210 211 212 213 214
        if ( !url.isLocalFile() )
                KIO::NetAccess::removeTempFile( tmpfile );
}

void KParameterEditor::cmdExport_clicked()
{
215
	if ( !m_mainWidget->list->count() )
216
                return;
Laurent Montel's avatar
Laurent Montel committed
217
        KUrl url = KFileDialog::getSaveURL( QString(),i18n("*.txt|Plain Text File "));
218 219 220 221 222 223 224 225 226 227 228 229
        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;
                        file.setName(tmpfile.name() );
                        
Laurent Montel's avatar
Laurent Montel committed
230
                        if (file.open( QIODevice::WriteOnly ) )
231 232
                        {
                                QTextStream stream(&file);
233
								Q3ListBoxItem *it = m_mainWidget->list->firstItem();
234 235 236 237 238 239 240 241 242
                                while ( 1 )
                                {
                                        stream << it->text();
                                        it = it->next();
                                        if (it)
                                                stream << endl; //only write a new line if there are more text
                                        else
                                                break;
                                }
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
                                file.close();
                        }
                        else
                                KMessageBox::error(0,i18n("An error appeared when saving this file"));
                        
                        if ( !KIO::NetAccess::upload(tmpfile.name(),url, this) )
                        {
                                KMessageBox::error(0,i18n("An error appeared when saving this file"));
                                tmpfile.unlink();
                                return;
                        }
                        tmpfile.unlink();
                }
                else
                {
Laurent Montel's avatar
Compile  
Laurent Montel committed
258
                        file.setName(url.prettyURL(0));
Laurent Montel's avatar
Laurent Montel committed
259
                        if (file.open( QIODevice::WriteOnly ) )
260 261
                        {
                                QTextStream stream(&file);
262
								Q3ListBoxItem *it = m_mainWidget->list->firstItem();
263 264 265 266 267 268 269 270 271
                                while ( 1 )
                                {
                                        stream << it->text();
                                        it = it->next();
                                        if (it)
                                                stream << endl; //only write a new line if there are more text
                                        else
                                                break;
                                }
272 273 274 275 276 277 278 279
                                file.close();
                        }
                        else
                                KMessageBox::error(0,i18n("An error appeared when saving this file"));
                }
        }


280 281
}

Laurent Montel's avatar
Laurent Montel committed
282
void KParameterEditor::varlist_clicked( Q3ListBoxItem * 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
	}
}


Laurent Montel's avatar
Laurent Montel committed
297
void KParameterEditor::varlist_doubleClicked( Q3ListBoxItem * )
298 299 300 301
{
	cmdEdit_clicked();
}

302
bool KParameterEditor::checkTwoOfIt(const QString & text)
303
{
304
	if ( m_mainWidget->list->findItem(text,Q3ListView::ExactMatch) == 0)
305 306 307 308 309
		return false;
	else
		return true;
}

310
#include "kparametereditor.moc"