moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kmplot / kmplot / kmplot.cpp
blob12297b819d63d6ef93ba609d9c21d99ca5572d71
1 /*
2 * KmPlot - a math. function plotter for the KDE-Desktop
4 * Copyright (C) 2004 Fredrik Edemar
5 * f_edemar@linux.se
6 *
7 * This file is part of the KDE Project.
8 * KmPlot is part of the KDE-EDU Project.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include "kmplot.h"
28 #include <kaction.h>
29 #include <kconfig.h>
30 #include <kedittoolbar.h>
31 #include <kkeydialog.h>
32 #include <kfiledialog.h>
33 #include <klibloader.h>
34 #include <klocale.h>
35 #include <kmessagebox.h>
36 #include <kstatusbar.h>
37 #include <kstdaction.h>
38 #include <kurl.h>
40 #include "MainDlg.h"
41 #include "kmplotprogress.h"
43 KmPlot::KmPlot( KCmdLineArgs* args)
44 : DCOPObject( "KmPlotShell" ), KParts::MainWindow( 0L, "KmPlot" )
46 // set the shell's ui resource file
47 setXMLFile("kmplot_shell.rc");
48 // then, setup our actions
49 setupActions();
51 // setup the status bar
52 setupStatusBar();
54 // this routine will find and load our Part. it finds the Part by
55 // name which is a bad idea usually.. but it's alright in this
56 // case since our Part is made for this Shell
57 KLibFactory *factory = KLibLoader::self()->factory("libkmplotpart");
58 if (factory)
60 // now that the Part is loaded, we cast it to a Part to get
61 // our hands on it
62 m_part = static_cast<KParts::ReadOnlyPart *>(factory->create(this,
63 "kmplot_part", "KParts::ReadOnlyPart" ));
64 if (m_part)
66 // tell the KParts::MainWindow that this is indeed the main widget
67 setCentralWidget(m_part->widget());
68 //m_part->widget()->setFocus();
69 // and integrate the part's GUI with the shell's
70 createGUI(m_part);
73 else
75 // if we couldn't find our Part, we exit since the Shell by
76 // itself can't do anything useful
77 KMessageBox::error(this, i18n("Could not find KmPlot's part."));
78 kapp->quit();
79 // we return here, cause kapp->quit() only means "exit the
80 // next time we enter the event loop...
81 return;
84 if (!initialGeometrySet())
85 resize( QSize(450, 520).expandedTo(minimumSizeHint()));
87 // apply the saved mainwindow settings, if any, and ask the mainwindow
88 // to automatically save settings if changed: window size, toolbar
89 // position, icon size, etc.
90 setAutoSaveSettings();
91 if (args)
93 bool exit = false;
94 for (int i=0; i < args->count(); i++ )
96 if (i==0)
98 if (!load(args->url(0) ) )
99 exit = true;
101 else
102 openFileInNewWindow( args->url(i) );
104 if (exit)
105 deleteLater(); // couln't open the file, and therefore exit
109 KmPlot::~KmPlot()
112 void KmPlot::slotUpdateFullScreen( bool checked)
114 if (checked)
116 showFullScreen();
117 m_fullScreen->plug( toolBar( "mainToolBar" ) );
119 else
121 showNormal();
122 m_fullScreen->unplug( toolBar( "mainToolBar" ) );
126 bool KmPlot::load(const KURL& url)
128 m_part->openURL( url );
129 if (m_part->url().isEmpty())
130 return false;
131 setCaption(url.prettyURL(0, KURL::StripFileProtocol));
132 return true;
135 void KmPlot::setupActions()
137 KStdAction::openNew(this, SLOT(fileNew()), actionCollection());
138 KStdAction::open(this, SLOT(fileOpen()), actionCollection());
139 KStdAction::quit(kapp, SLOT(quit()), actionCollection());
141 createStandardStatusBarAction();
142 setStandardToolBarMenuEnabled(true);
144 KStdAction::keyBindings(this, SLOT(optionsConfigureKeys()), actionCollection());
145 KStdAction::configureToolbars(this, SLOT(optionsConfigureToolbars()), actionCollection());
147 m_fullScreen = KStdAction::fullScreen( NULL, NULL, actionCollection(), this, "fullscreen");
148 connect( m_fullScreen, SIGNAL( toggled( bool )), this, SLOT( slotUpdateFullScreen( bool )));
151 void KmPlot::saveProperties(KConfig* /*config*/)
153 // the 'config' object points to the session managed
154 // config file. anything you write here will be available
155 // later when this app is restored
158 void KmPlot::readProperties(KConfig* /*config*/)
160 // the 'config' object points to the session managed
161 // config file. this function is automatically called whenever
162 // the app is being restored. read in here whatever you wrote
163 // in 'saveProperties'
166 void KmPlot::fileNew()
168 // About this function, the style guide (
169 // http://developer.kde.org/documentation/standards/kde/style/basics/index.html )
170 // says that it should open a new window if the document is _not_
171 // in its initial state. This is what we do here..
172 if ( !m_part->url().isEmpty() || isModified() )
173 //KApplication::startServiceByDesktopName("kmplot");
174 KApplication::kdeinitExec("kmplot");
177 bool KmPlot::stopProgressBar()
179 if (m_progressbar && m_progressbar->isShown())
181 m_progressbar->hide();
182 return true;
184 return false;
187 void KmPlot::startProgressBar(int steps)
189 if (m_progressbar)
191 m_progressbar->progress->setTotalSteps(steps);
192 m_progressbar->show();
196 void KmPlot::increaseProgressBar()
198 if (m_progressbar)
199 m_progressbar->increase();
202 void KmPlot::optionsConfigureKeys()
204 KKeyDialog::configure(actionCollection(), "kmplot_shell.rc");
207 void KmPlot::optionsConfigureToolbars()
209 saveMainWindowSettings(KGlobal::config() );
210 // use the standard toolbar editor
211 KEditToolbar dlg(factory());
212 connect(&dlg, SIGNAL(newToolbarConfig()), this, SLOT(applyNewToolbarConfig()));
213 dlg.exec();
216 void KmPlot::applyNewToolbarConfig()
218 applyMainWindowSettings(KGlobal::config());
221 void KmPlot::fileOpen()
223 // this slot is called whenever the File->Open menu is selected,
224 // the Open shortcut is pressed (usually CTRL+O) or the Open toolbar
225 // button is clicked
226 KURL const url = KFileDialog::getOpenURL( QDir::currentDirPath(),
227 i18n( "*.fkt|KmPlot Files (*.fkt)\n*.*|All Files" ), this, i18n( "Open" ) );
229 if ( !url.isEmpty())
231 // About this function, the style guide (
232 // http://developer.kde.org/documentation/standards/kde/style/basics/index.html )
233 // says that it should open a new window if the document is _not_
234 // in its initial state. This is what we do here..
235 if ( m_part->url().isEmpty() && !isModified() )
236 load( url ); // we open the file in this window...
237 else
238 openFileInNewWindow(url); // we open the file in a new window...
242 void KmPlot::fileOpen(const KURL &url)
244 if ( !url.isEmpty())
246 // About this function, the style guide (
247 // http://developer.kde.org/documentation/standards/kde/style/basics/index.html )
248 // says that it should open a new window if the document is _not_
249 // in its initial state. This is what we do here..
250 if ( m_part->url().isEmpty() && !isModified() )
251 load( KStandardDirs::realFilePath(url.url())); // we open the file in this window...
252 else
253 openFileInNewWindow(url); // we open the file in a new window...
258 void KmPlot::openFileInNewWindow(const KURL url)
260 KApplication::startServiceByDesktopName("kmplot",url.url());
263 bool KmPlot::checkModified()
265 QCString replyType;
266 QByteArray replyData;
267 kapp->dcopClient()->call(kapp->dcopClient()->appId(), "MainDlg","checkModified()", QByteArray(), replyType, replyData, false);
268 bool result;
269 QDataStream stream(replyData, IO_ReadOnly);
270 stream >> result;
271 return result;
274 bool KmPlot::isModified()
276 QCString replyType;
277 QByteArray replyData;
278 kapp->dcopClient()->call(kapp->dcopClient()->appId(), "MainDlg","isModified()", QByteArray(), replyType, replyData, false);
279 bool result;
280 QDataStream stream(replyData, IO_ReadOnly);
281 stream >> result;
282 return result;
285 bool KmPlot::queryClose()
287 return checkModified();
290 void KmPlot::setStatusBarText(const QString &text, int id)
292 statusBar()->changeItem(text,id);
296 void KmPlot::setupStatusBar()
298 statusBar()->insertFixedItem( "1234567890", 1 );
299 statusBar()->insertFixedItem( "1234567890", 2 );
300 statusBar()->insertItem( "", 3, 3 );
301 statusBar()->insertItem( "", 4 );
302 statusBar()->changeItem( "", 1 );
303 statusBar()->changeItem( "", 2 );
304 statusBar()->setItemAlignment( 3, AlignLeft );
306 m_progressbar = new KmPlotProgress( statusBar() );
307 m_progressbar->setMaximumHeight( statusBar()->height()-10 );
308 connect( m_progressbar->button, SIGNAL (clicked() ), this, SLOT( progressbar_clicked() ) );
309 statusBar()->addWidget(m_progressbar);
312 void KmPlot::progressbar_clicked()
314 kapp->dcopClient()->send(kapp->dcopClient()->appId(), "View","stopDrawing()", QByteArray());
317 #include "kmplot.moc"