File: MainWindow.cpp

package info (click to toggle)
mathgl 8.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 248,044 kB
  • sloc: cpp: 87,365; ansic: 3,299; javascript: 3,284; pascal: 1,562; python: 52; sh: 51; makefile: 47; f90: 22
file content (55 lines) | stat: -rw-r--r-- 2,532 bytes parent folder | download | duplicates (3)
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
#include "MainWindow.hpp"
#include "ui_MainWindow.h"
#include "mgl2/config.h"

#include <QWebFrame>
#include <QNetworkDiskCache>
#include <QDesktopServices>
//-----------------------------------------------------------------------------
int main(int argc, char *argv[])
{
	QApplication a(argc, argv);
	MainWindow w;
	w.show();
	return a.exec();
}
//-----------------------------------------------------------------------------
MainWindow::MainWindow(QWidget* const parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
	ui->setupUi(this);

	// configure webkit
	QWebSettings::globalSettings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
	QWebSettings::globalSettings()->setAttribute(QWebSettings::JavascriptEnabled, true);
	QWebSettings::globalSettings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, true);
	QWebSettings::globalSettings()->setAttribute(QWebSettings::LocalContentCanAccessFileUrls, true);
	QWebSettings::globalSettings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, true);
	QWebSettings::globalSettings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);
	QWebSettings::globalSettings()->setAttribute(QWebSettings::JavascriptCanAccessClipboard, true);

	// create non-cached QNetworkAccessManager and assign to webview
	QNetworkAccessManager* manager = new QNetworkAccessManager(this);
	QNetworkDiskCache* diskCache = new QNetworkDiskCache();
#ifdef MGL_USE_QT5
	const QString location = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
#else
	const QString location = QDesktopServices::storageLocation(QDesktopServices::CacheLocation);
#endif
	diskCache->setCacheDirectory(location);
	diskCache->setMaximumCacheSize(0);
	manager->setCache(diskCache);
	ui->webView->page()->setNetworkAccessManager(manager);

	// inject backend object each time javascript object is cleared
	connect(ui->webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(injectBackendObject()));
	// set url to load
	ui->webView->load(QUrl(QString("file:///%1/../../json/%2").arg(qApp->applicationDirPath(),"index.html")));
}
//-----------------------------------------------------------------------------
void MainWindow::injectBackendObject()
{
	ui->webView->page()->mainFrame()->addToJavaScriptWindowObject("globalBackend", &_backend);
}
//-----------------------------------------------------------------------------
MainWindow::~MainWindow()	{	delete ui;	}
//-----------------------------------------------------------------------------