kparametereditor.cpp 8.82 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
#include <assert.h>

47 48
class ParameterValueList;

49
KParameterEditor::KParameterEditor(XParser *m, QList<ParameterValueItem> *l, QWidget *parent )
50
	: KDialog( parent, i18n("Parameter Editor"), Ok|Cancel ),
51 52
	  m_parameter(l),
	  m_parser(m)
53
{
54 55 56
	m_mainWidget = new QParameterEditor( this );
	setMainWidget( m_mainWidget );
	
57
	for (  QList<ParameterValueItem>::Iterator it = m_parameter->begin(); it != m_parameter->end(); ++it )
58 59
		m_mainWidget->list->addItem( (*it).expression );
	m_mainWidget->list->sortItems();
60
	
61 62 63 64
	connect( m_mainWidget->cmdNew, SIGNAL( clicked() ), this, SLOT( cmdNew_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() ));
65 66 67 68 69 70
	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() ) );
	
	checkValueValid();
71
	
72
	m_mainWidget->value->setFocus();
73 74 75 76
}

KParameterEditor::~KParameterEditor()
{
77 78
}

79

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

95

96 97
void KParameterEditor::cmdNew_clicked()
{
98 99 100
	QListWidgetItem * item = new QListWidgetItem( m_mainWidget->list );
	m_mainWidget->list->setCurrentItem( item );
	m_mainWidget->value->setFocus();
101 102
}

103 104

void KParameterEditor::selectedConstantChanged( QListWidgetItem * current )
105
{
106 107
	m_mainWidget->cmdDelete->setEnabled( current != 0 );
	m_mainWidget->value->setText( current ? current->text() : QString::null );
108 109
}

110

111 112
void KParameterEditor::cmdDelete_clicked()
{
113 114 115 116 117 118 119 120 121
	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 );
122
	m_mainWidget->list->sortItems();
123 124
}

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

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();
	(double) m_parser->eval( valueText );
	bool valid = (m_parser->parserError( false ) == 0);
	m_mainWidget->valueInvalidLabel->setVisible( !valueText.isEmpty() && !valid );
	return valid;
}


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) )
        {
155
			KMessageBox::sorry(0,i18n("The file does not exist."));
156 157 158
                return;
        }
        
159
	bool verbose = false;
160 161 162 163 164 165
        QFile file;
        QString tmpfile;
        if ( !url.isLocalFile() )
        {
                if ( !KIO::NetAccess::download(url, tmpfile, this) )
                {
166
					KMessageBox::sorry(0,i18n("An error appeared when opening this file"));
167 168
                        return;
                }
169
                file.setFileName(tmpfile);
170 171
        }
        else
172
                file.setFileName(url.path() );
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->addItem(line);
					m_mainWidget->list->sortItems();
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
	else
207
		KMessageBox::sorry(0,i18n("An error appeared when opening this file"));
208
        
209 210 211 212
        if ( !url.isLocalFile() )
                KIO::NetAccess::removeTempFile( tmpfile );
}

213

214 215
void KParameterEditor::cmdExport_clicked()
{
216
	if ( !m_mainWidget->list->count() )
217
                return;
Laurent Montel's avatar
Laurent Montel committed
218
        KUrl url = KFileDialog::getSaveURL( QString(),i18n("*.txt|Plain Text File "));
219 220 221 222 223 224 225 226 227 228
        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;
229
                        file.setFileName(tmpfile.name() );
230
                        
Laurent Montel's avatar
Laurent Montel committed
231
                        if (file.open( QIODevice::WriteOnly ) )
232
                        {
233 234 235 236 237 238 239 240 241
							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();
242
                        }
243 244
						else
							KMessageBox::sorry(0,i18n("An error appeared when saving this file"));
245 246 247
                        
                        if ( !KIO::NetAccess::upload(tmpfile.name(),url, this) )
                        {
248
							KMessageBox::sorry(0,i18n("An error appeared when saving this file"));
249 250 251 252 253 254 255
                                tmpfile.unlink();
                                return;
                        }
                        tmpfile.unlink();
                }
                else
                {
256 257
					kDebug() << "url.path()="<<url.path()<<endl;
                        file.setFileName(url.path());
Laurent Montel's avatar
Laurent Montel committed
258
                        if (file.open( QIODevice::WriteOnly ) )
259
                        {
260 261 262 263 264 265 266 267 268
							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();
269 270
                        }
                        else
271
							KMessageBox::sorry(0,i18n("An error appeared when saving this file"));
272 273 274 275
                }
        }


276 277
}

278

279
bool KParameterEditor::checkTwoOfIt(const QString & text)
280
{
281
	return !m_mainWidget->list->findItems(text,Qt::MatchExactly).isEmpty();
282 283
}

284
#include "kparametereditor.moc"