MainDlg.cpp 12.5 KB
Newer Older
1 2 3 4 5
/*
* KmPlot - a math. function plotter for the KDE-Desktop
*
* Copyright (C) 1998, 1999  Klaus-Dieter Mller
*               2000, 2002 kd.moeller@t-online.de
6
*
7 8 9 10 11 12 13
* 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.
14
*
15 16 17 18
* 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.
19
*
20 21
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
Waldo Bastian's avatar
Waldo Bastian committed
22
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23 24 25
*
*/

26 27 28 29 30 31
// Qt includes
#include <qdom.h>
#include <qfile.h>

// KDE includes
#include <kdebug.h>
32 33
#include <kedittoolbar.h>
#include <kkeydialog.h>
34

Matthias Messmer's avatar
Matthias Messmer committed
35 36 37 38
// local includes
#include "MainDlg.h"
#include "MainDlg.moc"
#include "kprinterdlg.h"
39
#include "ksettingsdlg.h"
Matthias Messmer's avatar
Matthias Messmer committed
40 41 42 43
#include "misc.h"

MainDlg::MainDlg( const char* name ) : KMainWindow( 0, name )
{
44 45 46 47 48 49 50
	init();
	fdlg = 0;
	bez = 0;
	view = new View( this );
	setCentralWidget( view );
	setupActions();
	setupStatusBar();
51 52 53 54 55 56 57
}

MainDlg::~MainDlg()
{
}

void MainDlg::setupActions()
Matthias Messmer's avatar
Matthias Messmer committed
58
{
59 60 61 62 63 64 65 66 67
	// standard actions
	KStdAction::openNew( this, SLOT( neu() ), actionCollection() );
	KStdAction::open( this, SLOT( load() ), actionCollection() );
	KStdAction::print( this, SLOT( print() ), actionCollection() );
	KStdAction::save( this, SLOT( save() ), actionCollection() );
	KStdAction::saveAs( this, SLOT( saveas() ), actionCollection() );
	KStdAction::quit( kapp, SLOT( closeAllWindows() ), actionCollection() );
	connect( kapp, SIGNAL( lastWindowClosed() ), kapp, SLOT( quit() ) );
	KStdAction::helpContents( this, SLOT( hilfe() ), actionCollection(), "helpcontents" );
68 69 70

	createStandardStatusBarAction();
	setStandardToolBarMenuEnabled(true);
71 72 73 74 75 76 77 78

	// KmPLot specific actions
	( void ) new KAction( i18n( "&Axes..." ), 0, this, SLOT( achsen() ), actionCollection(), "axes" );
	( void ) new KAction( i18n( "&Scale..." ), 0, this, SLOT( skalierung() ), actionCollection(), "scale" );
	( void ) new KAction( i18n( "&Grid..." ), 0, this, SLOT( raster() ), actionCollection(), "grid" );
	( void ) new KAction( i18n( "&Step..." ), 0, this, SLOT( schrittw() ), actionCollection(), "step" );
	view_bezeichnungen = new KToggleAction( i18n( "&Names" ), 0, this, SLOT( bezeichnungen() ), actionCollection(), "names" );

79
	( void ) new KAction( i18n( "Functions..." ), "kfkt.png", 0, this, SLOT( funktionen() ), actionCollection(), "functions" );
80 81 82
	( void ) new KAction( i18n( "Coordinate System I" ), "ksys1.png", 0, this, SLOT( onachsen1() ), actionCollection(), "coord_i" );
	( void ) new KAction( i18n( "Coordinate System II" ), "ksys2.png", 0, this, SLOT( onachsen2() ), actionCollection(), "coord_ii" );
	( void ) new KAction( i18n( "Coordinate System III" ), "ksys3.png", 0, this, SLOT( onachsen3() ), actionCollection(), "coord_iii" );
83

84 85
  KStdAction::keyBindings(this, SLOT(optionsConfigureKeys()), actionCollection());
  KStdAction::configureToolbars(this, SLOT(optionsConfigureToolbars()), actionCollection());
86
 KStdAction::preferences( this, SLOT( slotSettings() ), actionCollection());
87 88

	createGUI( locate( "data", "kmplot/kmplotui.rc" ) );
89 90 91
}

void MainDlg::setupStatusBar()
92 93 94 95 96 97 98 99
{   stbar=statusBar();
	stbar->insertFixedItem("1234567890", 1);
	stbar->insertFixedItem("1234567890", 2);
	stbar->insertItem("", 3, 1);
	stbar->changeItem("", 1);
	stbar->changeItem("", 2);
    stbar->setItemAlignment(3, AlignLeft);
	view->stbar=stbar;
100 101 102 103 104 105
}


// Slots

void MainDlg::neu()
Matthias Messmer's avatar
Matthias Messmer committed
106
{
107 108 109 110
	init(); // set globals to default
	datei = ""; // empty filename == new file
	setCaption( datei );
	view->update();
111 112
}

Matthias Messmer's avatar
Matthias Messmer committed
113 114
void MainDlg::save()
{
115
	if ( datei.isEmpty() )            // if there is no file name set yet
116 117 118
		saveas();
	else
		doSave();
Matthias Messmer's avatar
Matthias Messmer committed
119
}
120

Matthias Messmer's avatar
Matthias Messmer committed
121 122
void MainDlg::saveas()
{
123
	datei = KFileDialog::getSaveFileName( QDir::currentDirPath(), i18n( "*.fkt|KmPlot Files (*.fkt)\n*|All Files" ), this, i18n( "Save As" ) );
124 125
	if ( !datei.isEmpty() )
	{
126
		if ( datei.find( "." ) == -1 )            // no file extension
127 128 129 130
			datei = datei + ".fkt"; // use fkt-type as default
		doSave();
		setCaption( datei );
	}
Matthias Messmer's avatar
Matthias Messmer committed
131
}
132

Matthias Messmer's avatar
Matthias Messmer committed
133 134 135
// here the real storing is done...
void MainDlg::doSave()
{
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	////////////
	// save as svg by drawing into a QPicture and saving it as svg
	/*    if ( datei.right( 4 ).lower() == ".svg" )
	    {
	        QPicture pic;
	        view->draw( &pic );
	        pic.save( datei, "svg" );
	        return ;
	    }*/

	///////////
	// saving as xml by a QDomDocument
	QDomDocument doc( "kmpdoc" );
	// the root tag
	QDomElement root = doc.createElement( "kmpdoc" );
	doc.appendChild( root );

	// the axes tag
	QDomElement tag = doc.createElement( "axes" );

	tag.setAttribute( "color", QColor( AchsenFarbe ).name() );
	tag.setAttribute( "width", AchsenDicke );
	tag.setAttribute( "tic-width", TeilstrichDicke );
	tag.setAttribute( "tic-legth", TeilstrichLaenge );

	addTag( doc, tag, "mode", QString::number( mode ) );
	addTag( doc, tag, "xmin", xminstr );
	addTag( doc, tag, "xmax", xmaxstr );
	addTag( doc, tag, "ymin", yminstr );
	addTag( doc, tag, "ymax", ymaxstr );
	addTag( doc, tag, "xcoord", QString::number( koordx ) );
	addTag( doc, tag, "ycoord", QString::number( koordy ) );

	root.appendChild( tag );

	tag = doc.createElement( "grid" );

	tag.setAttribute( "color", QColor( GitterFarbe ).name() );
	tag.setAttribute( "width", GitterDicke );

	addTag( doc, tag, "mode", QString::number( g_mode ) );

	root.appendChild( tag );

	tag = doc.createElement( "scale" );

	addTag( doc, tag, "tic-x", tlgxstr );
	addTag( doc, tag, "tic-y", tlgystr );
	addTag( doc, tag, "print-tic-x", drskalxstr );
	addTag( doc, tag, "print-tic-y", drskalystr );

	root.appendChild( tag );

	addTag( doc, root, "step", QString::number( rsw ) );

	for ( int ix = 0; ix < ps.ufanz; ix++ )
	{
		if ( !ps.fktext[ ix ].extstr.isEmpty() )
		{
			tag = doc.createElement( "function" );

			tag.setAttribute( "number", ix );
			tag.setAttribute( "visible", ps.fktext[ ix ].f_mode );
			tag.setAttribute( "visible-deriv", ps.fktext[ ix ].f1_mode );
			tag.setAttribute( "visible-2nd-deriv", ps.fktext[ ix ].f2_mode );
			tag.setAttribute( "width", ps.fktext[ ix ].dicke );
			tag.setAttribute( "color", QColor( ps.fktext[ ix ].farbe ).name() );

			addTag( doc, tag, "equation", ps.fktext[ ix ].extstr );

			root.appendChild( tag );
		}
	}

	QFile xmlfile( datei );
	xmlfile.open( IO_WriteOnly );
	QTextStream ts( &xmlfile );
	doc.save( ts, 4 );
	xmlfile.close();
215 216
}

217 218
void MainDlg::addTag( QDomDocument &doc, QDomElement &parentTag, const QString tagName, const QString tagValue )
{
219 220 221 222
	QDomElement tag = doc.createElement( tagName );
	QDomText value = doc.createTextNode( tagValue );
	tag.appendChild( value );
	parentTag.appendChild( tag );
223
}
224 225

void MainDlg::load()
Matthias Messmer's avatar
Matthias Messmer committed
226
{
227
	QString d = KFileDialog::getOpenFileName( QDir::currentDirPath(), i18n( "*.fkt|KmPlot Files (*.fkt)\n*|All Files" ), this, i18n( "Open" ) );
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	if ( d.isEmpty() )
		return ;

	init();

	QDomDocument doc( "kmpdoc" );

	QFile f( d );
	if ( !f.open( IO_ReadOnly ) )
		return ;
	if ( !doc.setContent( &f ) )
	{
		f.close();
		return ;
	}
	f.close();

	QDomElement element = doc.documentElement();
	for ( QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling() )
	{
		if ( n.nodeName() == "axes" )
			parseAxes( n.toElement() );
		if ( n.nodeName() == "grid" )
			parseGrid( n.toElement() );
		if ( n.nodeName() == "scale" )
			parseScale( n.toElement() );
		if ( n.nodeName() == "step" )
			parseStep( n.toElement() );
		if ( n.nodeName() == "function" )
			parseFunction( n.toElement() );
	}

	///////////
	// postprocessing loading
	switch ( koordx )
	{
	case 0:
		xmin = -8.0;
		xmax = 8.0;
		break;
	case 1:
		xmin = -5.0;
		xmax = 5.5;
		break;
	case 2:
		xmin = 0.0;
		xmax = 16.0;
		break;
	case 3:
		xmin = 0.0;
		xmax = 10.0;
		break;
	case 4:
		xmin = ps.eval( xminstr );
		xmax = ps.eval( xmaxstr );
	}
	switch ( koordy )
	{
	case 0:
		ymin = -8.0;
		ymax = 8.0;
		break;
	case 1:
		ymin = -5.0;
		ymax = 5.5;
		break;
	case 2:
		ymin = 0.0;
		ymax = 16.0;
		break;
	case 3:
		ymin = 0.0;
		ymax = 10.0;
		break;
	case 4:
		ymin = ps.eval( yminstr );
		ymax = ps.eval( ymaxstr );
	}


	datei = d;

	setCaption( datei );
	view->update();
312 313
}

314
void MainDlg::parseAxes( const QDomElement & n )
315
{
316 317 318 319 320 321 322 323 324 325 326 327 328 329
	kdDebug() << "parsing axes" << endl;

	AchsenDicke = n.attribute( "width", "1" ).toInt();
	AchsenFarbe = QColor( n.attribute( "color", "#000000" ) ).rgb();
	TeilstrichDicke = n.attribute( "tic-width", "3" ).toInt();
	TeilstrichLaenge = n.attribute( "tic-length", "10" ).toInt();

	mode = n.namedItem( "mode" ).toElement().text().toInt();
	xminstr = n.namedItem( "xmin" ).toElement().text();
	xmaxstr = n.namedItem( "xmax" ).toElement().text();
	yminstr = n.namedItem( "ymin" ).toElement().text();
	ymaxstr = n.namedItem( "ymax" ).toElement().text();
	koordx = n.namedItem( "xcoord" ).toElement().text().toInt();
	koordy = n.namedItem( "ycoord" ).toElement().text().toInt();
330 331
}

332
void MainDlg::parseGrid( const QDomElement & n )
333
{
334
	kdDebug() << "parsing grid" << endl;
335

336 337
	GitterFarbe = QColor( n.attribute( "color", "#c0c0c0" ) ).rgb();
	GitterDicke = n.attribute( "width", "1" ).toInt();
338

339
	g_mode = n.namedItem( "mode" ).toElement().text().toInt();
340 341
}

342
void MainDlg::parseScale( const QDomElement & n )
343
{
344
	kdDebug() << "parsing scale" << endl;
345

346 347 348 349
	tlgxstr = n.namedItem( "tic-x" ).toElement().text();
	tlgystr = n.namedItem( "tic-y" ).toElement().text();
	drskalxstr = n.namedItem( "print-tic-x" ).toElement().text();
	drskalystr = n.namedItem( "print-tic-y" ).toElement().text();
350

351 352 353 354
	tlgx = ps.eval( tlgxstr );
	tlgy = ps.eval( tlgystr );
	drskalx = ps.eval( drskalxstr );
	drskaly = ps.eval( drskalystr );
355 356
}

357
void MainDlg::parseStep( const QDomElement & n )
358
{
359 360
	kdDebug() << "parsing step" << endl;
	rsw = n.text().toDouble();
361 362
}

363
void MainDlg::parseFunction( const QDomElement & n )
364
{
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
	kdDebug() << "parsing function" << endl;

	int ix = n.attribute( "number" ).toInt();
	ps.fktext[ ix ].f_mode = n.attribute( "visible" ).toInt();
	ps.fktext[ ix ].f1_mode = n.attribute( "visible-deriv" ).toInt();
	ps.fktext[ ix ].f2_mode = n.attribute( "visible-2nd-deriv" ).toInt();
	ps.fktext[ ix ].dicke = n.attribute( "width" ).toInt();
	ps.fktext[ ix ].farbe = QColor( n.attribute( "color" ) ).rgb();

	ps.fktext[ ix ].extstr = n.namedItem( "equation" ).toElement().text();
	QCString fstr = ps.fktext[ ix ].extstr.utf8();
	if ( !fstr.isEmpty() )
	{
		int i = fstr.find( ';' );
		QCString str;
		if ( i == -1 )
			str = fstr;
		else
			str = fstr.left( i );
		ix = ps.addfkt( str );
		ps.getext( ix );
	}
387 388
}

389
void MainDlg::print()
Matthias Messmer's avatar
Matthias Messmer committed
390
{
391
	KPrinter prt( QPrinter::PrinterResolution );
392

393
	prt.addDialogPage( new KPrinterDlg( this, "KmPlot page" ) );
Matthias Messmer's avatar
Matthias Messmer committed
394

Sven Leiber's avatar
Sven Leiber committed
395
	if ( prt.setup( this, i18n("Print Function") ) )
396 397 398 399 400
	{
		prt.setFullPage( true );
		printtable = prt.option( "app-kmplot-printtable" ) != "-1";
		view->draw( &prt );
	}
401 402 403
}

void MainDlg::bezeichnungen()
Matthias Messmer's avatar
Matthias Messmer committed
404
{
405 406 407
	if ( !bez )
		bez = new BezWnd( this, "bez" );
	if ( view_bezeichnungen->isChecked() )
408 409 410 411
	{
		bez->move( 200, 200 );
		bez->show();
	}
412 413
	else
		bez->hide();
414 415 416
}

void MainDlg::funktionen()
417
{
418 419 420
	if ( !fdlg )
		fdlg = new FktDlg( this );
	fdlg->show();
421 422 423
}

void MainDlg::skalierung()
Matthias Messmer's avatar
Matthias Messmer committed
424
{
425
	SkalDlg skdlg;
426

427 428
	skdlg.exec();
	view->update();
429 430 431
}

void MainDlg::schrittw()
Matthias Messmer's avatar
Matthias Messmer committed
432
{
433
	SwDlg sdlg;
434

435 436
	sdlg.exec();
	view->update();
437 438 439
}

void MainDlg::raster()
Matthias Messmer's avatar
Matthias Messmer committed
440
{
441
	RstDlg rdlg( this, "rdlg" );
442

443 444
	rdlg.exec();
	view->update();
445 446 447
}

void MainDlg::achsen()
Matthias Messmer's avatar
Matthias Messmer committed
448
{
449
	KoordDlg kdlg;
450

451 452
	kdlg.exec();
	view->update();
453 454 455
}

void MainDlg::onachsen1()
Matthias Messmer's avatar
Matthias Messmer committed
456
{
457 458 459 460
	koordx = koordy = 0;
	xmin = ymin = -8.;
	xmax = ymax = 8.;
	view->update();
461 462 463
}

void MainDlg::onachsen2()
Matthias Messmer's avatar
Matthias Messmer committed
464
{
465 466 467 468 469 470 471
	koordx = 2;
	koordy = 0;
	xmin = 0.;
	ymin = -8.;
	xmax = 16.;
	ymax = 8.;
	view->update();
472 473 474
}

void MainDlg::onachsen3()
Matthias Messmer's avatar
Matthias Messmer committed
475
{
476 477 478 479
	koordx = koordy = 2;
	xmin = ymin = 0.;
	xmax = ymax = 16.;
	view->update();
480 481 482
}

void MainDlg::hilfe()
Matthias Messmer's avatar
Matthias Messmer committed
483
{
484
	kapp->invokeHelp( "", "kmplot" );
485 486
}

487 488
void MainDlg::slotSettings()
{
489 490
	KSettingsDlg * settings_dlg = new KSettingsDlg( this, "settings_dlg" );
	settings_dlg->exec();
491
}
492 493 494 495 496 497 498 499

void MainDlg::newToolbarConfig()
{
    createGUI();
    applyMainWindowSettings( KGlobal::config(), autoSaveGroup() );
}
void MainDlg::optionsConfigureKeys()
{
500
  KKeyDialog::configure(actionCollection());
501 502 503 504 505 506 507 508 509
}

void MainDlg::optionsConfigureToolbars()
{
    saveMainWindowSettings( KGlobal::config(), autoSaveGroup() );
    KEditToolbar dlg(actionCollection());
    connect(&dlg, SIGNAL(newToolbarConfig()), this, SLOT(newToolbarConfig()));
    dlg.exec();
}