Mercurial > octave-nkf
diff gui/src/octave-adapter/octave-link.cc @ 14707:674740c44c09 gui
Changed various files to matche file naming conventions.
author | Jacob Dawid <jacob.dawid@googlemail.com> |
---|---|
date | Thu, 31 May 2012 22:19:53 +0200 |
parents | gui/src/backend/octavelink.cc@f86884be20fc |
children | f50591409306 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gui/src/octave-adapter/octave-link.cc Thu May 31 22:19:53 2012 +0200 @@ -0,0 +1,157 @@ +/* OctaveGUI - A graphical user interface for Octave + * Copyright (C) 2011 John P. Swensen, Jacob Dawid (jacob.dawid@googlemail.com) + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ + +#include "octave-link.h" +#include "load-path.h" +#include "oct-env.h" +#include <QDir> +#include <QApplication> + +int octave_readline_hook () +{ + OctaveLink::instance ()->triggerUpdateHistoryModel (); + OctaveLink::instance ()->buildSymbolInformation (); + OctaveLink::instance ()->updateCurrentWorkingDirectory (); + return 0; +} + +void octave_exit_hook (int status) +{ + Q_UNUSED (status); + OctaveLink::instance ()->terminateOctave (); +} + +OctaveLink OctaveLink::m_singleton; + +OctaveLink::OctaveLink ():QObject () +{ + m_historyModel = new QStringListModel (this); + m_workspaceModel = new WorkspaceModel (this); + + m_workspaceModel->insertTopLevelItem(0, new TreeItem ("Local")); + m_workspaceModel->insertTopLevelItem(1, new TreeItem ("Global")); + m_workspaceModel->insertTopLevelItem(2, new TreeItem ("Persistent")); + m_workspaceModel->insertTopLevelItem(3, new TreeItem ("Hidden")); + + _updateWorkspaceModelTimer.setInterval (1000); + _updateWorkspaceModelTimer.setSingleShot (false); + connect(&_updateWorkspaceModelTimer, SIGNAL (timeout ()), + m_workspaceModel, SLOT (updateFromSymbolTable ())); + + _symbolInformationSemaphore = new QSemaphore (1); + _currentWorkingDirectory = ""; +} + +OctaveLink::~OctaveLink () +{ +} + +void +OctaveLink::launchOctave () +{ + // Create both threads. + m_octaveMainThread = new OctaveMainThread (this); + command_editor::add_event_hook (octave_readline_hook); + octave_exit = octave_exit_hook; + + // Start the first one. + m_octaveMainThread->start (); + _updateWorkspaceModelTimer.start (); +} + +void +OctaveLink::terminateOctave () +{ + qApp->quit (); +} + +void +OctaveLink::triggerUpdateHistoryModel () +{ + // Determine the client's (our) history length and the one of the server. + int clientHistoryLength = m_historyModel->rowCount (); + int serverHistoryLength = command_history::length (); + + // If were behind the server, iterate through all new entries and add them to our history. + if (clientHistoryLength < serverHistoryLength) + { + for (int i = clientHistoryLength; i < serverHistoryLength; i++) + { + m_historyModel->insertRow (0); + m_historyModel->setData (m_historyModel->index (0), QString (command_history::get_entry (i).c_str ())); + } + } +} + +void +OctaveLink::updateCurrentWorkingDirectory () +{ + QString _queriedWorkingDirectory = octave_env::get_current_directory ().c_str(); + if (_currentWorkingDirectory != _queriedWorkingDirectory) + { + _currentWorkingDirectory = _queriedWorkingDirectory; + QDir::setCurrent (_currentWorkingDirectory); + emit workingDirectoryChanged (_currentWorkingDirectory); + } +} + +void +OctaveLink::acquireSymbolInformation () +{ + _symbolInformationSemaphore->acquire (1); +} + +void +OctaveLink::releaseSymbolInformation () +{ + _symbolInformationSemaphore->release (1); +} + +void +OctaveLink::buildSymbolInformation () +{ + std::list < symbol_table::symbol_record > symbolTable = symbol_table::all_variables (); + + acquireSymbolInformation (); + _symbolInformation.clear (); + for (std::list < symbol_table::symbol_record > ::iterator iterator = symbolTable.begin (); + iterator != symbolTable.end (); iterator++) + { + SymbolInformation symbolInformation; + symbolInformation.fromSymbolRecord (*iterator); + _symbolInformation.push_back (symbolInformation); + } + releaseSymbolInformation (); +} + +const QList <SymbolInformation>& +OctaveLink::symbolInformation () const +{ + return _symbolInformation; +} + +QStringListModel * +OctaveLink::historyModel () +{ + return m_historyModel; +} + +WorkspaceModel * +OctaveLink::workspaceModel () +{ + return m_workspaceModel; +}