History

first cut at splitting out scripting engine from agent to add to client

ZappoMan committed 12 years ago

Commit e2fbb7b1 parent 02d3d3812 files · +318 −31Browse files

Showing the changes to interface/src/Application.cpp (1 of 12 files). Show all

Unified · Split

interface/src/Application.cpp+61−1
@@ -139,7 +139,8 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : · 138 unmodified lines
139139 _recentMaxPackets(0),
140140 _resetRecentMaxPacketsSoon(true),
141141 _swatch(NULL),
142− _pasteMode(false)
142+ _pasteMode(false),
143+ _scriptEngine(NULL)
143144 {
144145 _applicationStartupTime = startup_time;
145146 _window->setWindowTitle("Interface");
@@ -4385,3 +4386,62 @@ void Application::packetSentNotification(ssize_t length) { · 4239 unmodified lines
43854386 _bandwidthMeter.outputStream(BandwidthMeter::VOXELS).updateValue(length);
43864387 }
43874388
4389+void Application::loadScript() {
4390+ // shut down and stop any existing script
4391+ if (_scriptEngine) {
4392+ _scriptEngine->stop();
4393+ _scriptEngine = NULL;
4394+ }
4395+
4396+
4397+ QString desktopLocation = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
4398+ QString suggestedName = desktopLocation.append("/script.js");
4399+
4400+ QString fileNameString = QFileDialog::getOpenFileName(_glWidget, tr("Open Script"), suggestedName,
4401+ tr("JavaScript Files (*.js)"));
4402+ QByteArray fileNameAscii = fileNameString.toLocal8Bit();
4403+ const char* fileName = fileNameAscii.data();
4404+
4405+ printf("fileName:%s\n",fileName);
4406+
4407+ std::ifstream file(fileName, std::ios::in|std::ios::binary|std::ios::ate);
4408+ if(!file.is_open()) {
4409+ printf("error loading file\n");
4410+ return;
4411+ }
4412+ qDebug("loading file %s...\n", fileName);
4413+
4414+ // get file length....
4415+ unsigned long fileLength = file.tellg();
4416+ file.seekg( 0, std::ios::beg );
4417+
4418+ // read the entire file into a buffer, WHAT!? Why not.
4419+ char* entireFile = new char[fileLength];
4420+ file.read((char*)entireFile, fileLength);
4421+ file.close();
4422+
4423+ QString script(entireFile);
4424+ delete[] entireFile;
4425+
4426+ // start the script on a new thread...
4427+ _scriptEngine = new ScriptEngine(script);
4428+
4429+ QThread* workerThread = new QThread(this);
4430+
4431+ connect(workerThread, SIGNAL(started()), _scriptEngine, SLOT(run()));
4432+
4433+ connect(_scriptEngine, SIGNAL(finished()), this, SLOT(assignmentCompleted()));
4434+ connect(_scriptEngine, SIGNAL(finished()), workerThread, SLOT(quit()));
4435+ connect(_scriptEngine, SIGNAL(finished()), _scriptEngine, SLOT(deleteLater()));
4436+ connect(_scriptEngine, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
4437+
4438+ _scriptEngine->moveToThread(workerThread);
4439+
4440+ // Starts an event loop, and emits workerThread->started()
4441+ workerThread->start();
4442+
4443+
4444+
4445+ // restore the main window's active state
4446+ _window->activateWindow();
4447+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t