kparametereditor.cpp 9.4 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>
Laurent Montel's avatar
Laurent Montel committed
39
#include <q3valuelist.h>
40
#include <q3listview.h>
41

42 43
#include "kparametereditor.h"

44 45
class ParameterValueList;

Laurent Montel's avatar
Laurent Montel committed
46
KParameterEditor::KParameterEditor(XParser *m, Q3ValueList<ParameterValueItem> *l, QWidget *parent, const char *name)
47
	: QParameterEditor(parent,name, true, Qt::WDestructiveClose), m_parameter(l), m_parser(m)
48
{
Laurent Montel's avatar
Laurent Montel committed
49
	for (  Q3ValueList<ParameterValueItem>::Iterator it = m_parameter->begin(); it != m_parameter->end(); ++it )
50
		list->insertItem( (*it).expression );
51 52 53 54 55
	list->sort();
	
	connect( cmdNew, SIGNAL( clicked() ), this, SLOT( cmdNew_clicked() ));
	connect( cmdEdit, SIGNAL( clicked() ), this, SLOT( cmdEdit_clicked() ));
	connect( cmdDelete, SIGNAL( clicked() ), this, SLOT( cmdDelete_clicked() ));
56
	connect( cmdImport, SIGNAL( clicked() ), this, SLOT( cmdImport_clicked() ));
57
	connect( cmdExport, SIGNAL( clicked() ), this, SLOT( cmdExport_clicked() ));
58
	connect( cmdClose, SIGNAL( clicked() ), this, SLOT( close() ));
Laurent Montel's avatar
Laurent Montel committed
59 60
	connect( list, SIGNAL( doubleClicked( Q3ListBoxItem * ) ), this, SLOT( varlist_doubleClicked( Q3ListBoxItem *) ));
	connect( list, SIGNAL( clicked ( Q3ListBoxItem * ) ), this, SLOT( varlist_clicked(Q3ListBoxItem *  ) ));
61 62 63 64 65 66
	
}

KParameterEditor::~KParameterEditor()
{
	m_parameter->clear();
67
	QString item_text;
68
	for (int i = 0; (uint)i <= list->count();i++)
69
	{
70 71 72
		item_text = list->text(i);
		if ( !item_text.isEmpty() )
			m_parameter->append( ParameterValueItem(item_text, m_parser->eval( item_text)) );
73 74 75 76 77
	}
}

void KParameterEditor::cmdNew_clicked()
{
78 79
	QString result="";
	while (1)
80
	{
81 82 83 84 85
		bool ok;
		result = KInputDialog::getText( i18n("Parameter Value"), i18n( "Enter a new parameter value:" ), result, &ok );
		if ( !ok)
			return;
		m_parser->eval( result );
86
		if ( m_parser->parserError(false) != 0 )
87
		{
88
			m_parser->parserError();
89 90 91 92 93 94 95 96 97 98
			continue;
		}
		if ( checkTwoOfIt(result) )
		{
			KMessageBox::error(0,i18n("The value %1 already exists and will therefore not be added.").arg(result));
			continue;
		}
		list->insertItem(result);
		list->sort();
		break;
99
	}
100 101 102 103
}

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

void KParameterEditor::cmdDelete_clicked()
{
	list->removeItem( list->currentItem());
	list->sort();
}

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

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


269 270
}

Laurent Montel's avatar
Laurent Montel committed
271
void KParameterEditor::varlist_clicked( Q3ListBoxItem * item )
272 273 274 275 276 277 278 279 280 281 282 283 284 285
{
	if (item)
	{
		cmdEdit->setEnabled(true);
		cmdDelete->setEnabled(true);
	}
	else
	{
		cmdEdit->setEnabled(false);
		cmdDelete->setEnabled(false);		
	}
}


Laurent Montel's avatar
Laurent Montel committed
286
void KParameterEditor::varlist_doubleClicked( Q3ListBoxItem * )
287 288 289 290
{
	cmdEdit_clicked();
}

291
bool KParameterEditor::checkTwoOfIt(const QString & text)
292
{
293
	if ( list->findItem(text,Q3ListView::ExactMatch) == 0)
294 295 296 297 298
		return false;
	else
		return true;
}

299
#include "kparametereditor.moc"