File: kmplot.cpp

package info (click to toggle)
kmplot 4%3A16.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,552 kB
  • ctags: 1,244
  • sloc: cpp: 12,200; xml: 314; sh: 5; makefile: 1
file content (283 lines) | stat: -rw-r--r-- 8,503 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
215
216
217
218
219
220
221
222
223
224
225
226
227
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
/*
* 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*
*/

#include "kmplot.h"

#include <kconfig.h>
#include <kconfiggroup.h>
#include <kedittoolbar.h>
#include <kshortcutsdialog.h>
#include <kfiledialog.h>
#include <klibloader.h>
#include <kmessagebox.h>
#include <kmplotprogress.h>
#include <kstatusbar.h>
#include <kstandardaction.h>
#include <kurl.h>
#include <kactioncollection.h>
#include "maindlg.h"
#include <ktoolinvocation.h>
#include <ktogglefullscreenaction.h>

#include "kmplotadaptor.h"

static QUrl urlFromArg(const QString &arg)
{
#if QT_VERSION >= 0x050400
    return QUrl::fromUserInput(arg, QDir::currentPath(), QUrl::AssumeLocalFile);
#else
    // Logic from QUrl::fromUserInput(QString, QString, UserInputResolutionOptions)
    return (QUrl(arg, QUrl::TolerantMode).isRelative() && !QDir::isAbsolutePath(arg))
           ? QUrl::fromLocalFile(QDir::current().absoluteFilePath(arg))
           : QUrl::fromUserInput(arg);
#endif
}

KmPlot::KmPlot( const QCommandLineParser& parser )
		: KParts::MainWindow()
{
	setObjectName( "KmPlot" );

	// set the shell's ui resource file
	setXMLFile("kmplot_shell.rc");
	// then, setup our actions
	setupActions();

	// setup the status bar
	setupStatusBar();

	// this routine will find and load our Part.  it finds the Part by
	// name which is a bad idea usually.. but it's alright in this
	// case since our Part is made for this Shell
    KPluginFactory *factory = KPluginLoader("kmplotpart").factory();
	if (factory)
	{
		// now that the Part is loaded, we cast it to a Part to get
		// our hands on it
        m_part = factory->create<KParts::ReadWritePart>(this);
		if (m_part)
		{
			// tell the KParts::MainWindow that this is indeed the main widget
			setCentralWidget(m_part->widget());
			//m_part->widget()->setFocus();
			// and integrate the part's GUI with the shell's
			setupGUI(Keys | ToolBar | Save);
			createGUI(m_part);
		}
	}
	else
	{
		// if we couldn't find our Part, we exit since the Shell by
		// itself can't do anything useful
		KMessageBox::error(this, i18n("Could not find KmPlot's part."));
		qApp->quit();
		// we return here, cause qApp->quit() only means "exit the
		// next time we enter the event loop...
		return;
	}

//FIXME port to KF5
//	if (!initialGeometrySet())
//		resize( QSize(800, 520).expandedTo(minimumSizeHint()));

	// apply the saved mainwindow settings, if any, and ask the mainwindow
	// to automatically save settings if changed: window size, toolbar
	// position, icon size, etc.
	setAutoSaveSettings();
	{
		bool exit = false;
        bool first = true;
		foreach(const QString& arg, parser.positionalArguments())
		{
			QUrl url = urlFromArg(arg);
			if (first)
			{
				exit = !load(url);
			}
			else
				openFileInNewWindow( url );
		}
		if (exit)
			deleteLater(); // couln't open the file, and therefore exit
		first = false;
	}

	show();

    new KmPlotAdaptor(this);
    QDBusConnection::sessionBus().registerObject("/kmplot", this);

    if ( parser.isSet("function") )
    {
        QString f = parser.value("function");
        QDBusReply<bool> reply = QDBusInterface( QDBusConnection::sessionBus().baseService(), "/parser", "org.kde.kmplot.Parser").call( QDBus::BlockWithGui, "addFunction", f, "" );
    }
}

KmPlot::~KmPlot()
{}

void KmPlot::slotUpdateFullScreen( bool checked)
{
	if (checked)
	{
		KToggleFullScreenAction::setFullScreen( this, true );
		//m_fullScreen->plug( toolBar( "mainToolBar" ) ); deprecated annma 2006-03-01
	}
	else
	{
		KToggleFullScreenAction::setFullScreen( this, false );
		//m_fullScreen->unplug( toolBar( "mainToolBar" ) ); deprecated annma 2006-03-01
	}
}

bool KmPlot::load(const QUrl& url)
{
	m_part->openUrl( url );
	if (m_part->url().isEmpty())
		return false;
	setCaption(url.toDisplayString());
	return true;
}

void KmPlot::setupActions()
{
	KStandardAction::openNew(this, SLOT(fileNew()), actionCollection());
	KStandardAction::open(this, SLOT(fileOpen()), actionCollection());
	KStandardAction::quit(this, SLOT(close()), actionCollection());

	createStandardStatusBarAction();
	setStandardToolBarMenuEnabled(true);

	m_fullScreen = KStandardAction::fullScreen( NULL, NULL, this, actionCollection());
	actionCollection()->addAction("fullscreen", m_fullScreen);
	connect(m_fullScreen, &KToggleFullScreenAction::toggled, this, &KmPlot::slotUpdateFullScreen);
}

void KmPlot::fileNew()
{
	// About this function, the style guide (
	// http://developer.kde.org/documentation/standards/kde/style/basics/index.html )
	// says that it should open a new window if the document is _not_
	// in its initial state.  This is what we do here..
	if ( !m_part->url().isEmpty() || isModified() )
		//KToolInvocation::startServiceByDesktopName("kmplot");
		KToolInvocation::kdeinitExec("kmplot");
}

void KmPlot::applyNewToolbarConfig()
{
	applyMainWindowSettings(KSharedConfig::openConfig()->group( QString() ));
}

void KmPlot::fileOpen()
{
	// this slot is called whenever the File->Open menu is selected,
	// the Open shortcut is pressed (usually CTRL+O) or the Open toolbar
	// button is clicked
	QUrl const url = KFileDialog::getOpenUrl( QUrl::fromLocalFile(QDir::currentPath()),
	                 i18n( "*.fkt|KmPlot Files (*.fkt)\n*|All Files" ),
			 this, i18n( "Open" ) );

	if ( !url.isEmpty())
	{
		// About this function, the style guide (
		// http://developer.kde.org/documentation/standards/kde/style/basics/index.html )
		// says that it should open a new window if the document is _not_
		// in its initial state.  This is what we do here..
		if ( m_part->url().isEmpty() && !isModified() )
			load( url ); // we open the file in this window...
		else
			openFileInNewWindow(url); // we open the file in a new window...
	}
}

void KmPlot::fileOpen(const QUrl &url)
{
	if ( !url.isEmpty())
	{
		// About this function, the style guide (
		// http://developer.kde.org/documentation/standards/kde/style/basics/index.html )
		// says that it should open a new window if the document is _not_
		// in its initial state.  This is what we do here..
		if ( m_part->url().isEmpty() && !isModified() )
         load(url); // we open the file in this window...
		else
         openFileInNewWindow(url); // we open the file in a new window...
	}
}


void KmPlot::openFileInNewWindow(const QUrl &url)
{
	KToolInvocation::kdeinitExec("kmplot", QStringList() << url.url());
}

bool KmPlot::isModified()
{
	QDBusReply<bool> reply = QDBusInterface( QDBusConnection::sessionBus().baseService(), "/maindlg", "org.kde.kmplot.MainDlg").call( QDBus::BlockWithGui, "isModified" );
    return reply.value();
}

bool KmPlot::queryClose()
{
	return m_part->queryClose();
}

void KmPlot::setStatusBarText(const QString &text, int id)
{
	static_cast<KStatusBar *>(statusBar())->changeItem(text,id);
}


void KmPlot::setupStatusBar()
{
	KStatusBar *statusBar = new KStatusBar(this);
	setStatusBar(statusBar);

	statusBar->insertFixedItem( "1234567890123456", 1 );
	statusBar->insertFixedItem( "1234567890123456", 2 );
	statusBar->insertItem( "", 3, 3 );
	statusBar->insertItem( "", 4 );
	statusBar->changeItem( "", 1 );
	statusBar->changeItem( "", 2 );
	statusBar->setItemAlignment( 3, Qt::AlignLeft );

	m_progressBar = new KmPlotProgress( statusBar );
	m_progressBar->setMaximumHeight( statusBar->height()-10 );
	connect(m_progressBar, &KmPlotProgress::cancelDraw, this, &KmPlot::cancelDraw);
	statusBar->addWidget(m_progressBar);
}


void KmPlot::setDrawProgress( double progress )
{
	m_progressBar->setProgress( progress );
}


void KmPlot::cancelDraw()
{
	QDBusInterface( QDBusConnection::sessionBus().baseService(), "/kmplot", "org.kde.kmplot.KmPlot" ).call( QDBus::NoBlock, "stopDrawing" );
}