Newer
Older
Gigg, Martyn Anthony
committed
//-------------------------------------------
// Includes
//-------------------------------------------
#include "ScriptingWindow.h"
#include "ScriptManagerWidget.h"
#include "ScriptingEnv.h"
#include "pixmaps.h"
Gigg, Martyn Anthony
committed
// Mantid
#include "MantidKernel/ConfigService.h"
Gigg, Martyn Anthony
committed
#include "ApplicationWindow.h"
Gigg, Martyn Anthony
committed
//Qt
#include <QTextEdit>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QSettings>
#include <QPrintDialog>
#include <QPrinter>
Gigg, Martyn Anthony
committed
#include <QDateTime>
Gigg, Martyn Anthony
committed
#include <QFileDialog>
#include <QMessageBox>
#include <QApplication>
#include <QTextStream>
Gigg, Martyn Anthony
committed
//-------------------------------------------
// Public member functions
//-------------------------------------------
/**
* Constructor
Janik Zikovsky
committed
* @param env :: The scripting environment
* @param parent :: The parent widget
* @param flags :: Window flags passed to the base class
Gigg, Martyn Anthony
committed
*/
ScriptingWindow::ScriptingWindow(ScriptingEnv *env, bool capturePrint, QWidget *parent, Qt::WindowFlags flags) :
Gigg, Martyn Anthony
committed
QMainWindow(parent, flags), m_acceptClose(false)
Gigg, Martyn Anthony
committed
{
Gigg, Martyn Anthony
committed
Q_UNUSED(capturePrint);
Gigg, Martyn Anthony
committed
setObjectName("MantidScriptWindow");
// Sub-widgets
Gigg, Martyn Anthony
committed
m_manager = new ScriptManagerWidget(env, this);
Gigg, Martyn Anthony
committed
setCentralWidget(m_manager);
Gigg, Martyn Anthony
committed
QSettings settings;
settings.beginGroup("/ScriptWindow");
QString lastdir = settings.value("LastDirectoryVisited", "").toString();
// If nothing, set the last directory to the Mantid scripts directory (if present)
Gigg, Martyn Anthony
committed
if( lastdir.isEmpty() )
Gigg, Martyn Anthony
committed
{
lastdir = QString::fromStdString(Mantid::Kernel::ConfigService::Instance().getString("pythonscripts.directory"));
Gigg, Martyn Anthony
committed
}
m_manager->m_last_dir = lastdir;
if( env->supportsProgressReporting() )
{
m_manager->m_toggle_progress->setChecked(settings.value("ProgressArrow", true).toBool());
}
else
{
m_manager->m_toggle_progress->setChecked(false);
}
Sofia Antony
committed
//get the recent scripts values from registry
m_manager->setRecentScripts(settings.value("/RecentScripts").toStringList());
Gigg, Martyn Anthony
committed
settings.endGroup();
Sofia Antony
committed
// Create menus and actions
initMenus();
// This connection must occur after the objects have been created and initialized
connect(m_manager, SIGNAL(currentChanged(int)), this, SLOT(tabSelectionChanged()));
Gigg, Martyn Anthony
committed
Gigg, Martyn Anthony
committed
setWindowIcon(QIcon(":/MantidPlot_Icon_32offset.png"));
Gigg, Martyn Anthony
committed
setWindowTitle("MantidPlot: " + env->languageName() + " Window");
Gigg, Martyn Anthony
committed
setFocusProxy(m_manager);
}
/**
* Destructor
*/
ScriptingWindow::~ScriptingWindow()
{
delete m_manager;
}
/**
* Is a script executing?
* @returns A flag indicating the current state
*/
bool ScriptingWindow::isScriptRunning() const
{
return m_manager->isScriptRunning();
}
/**
* Save the settings on the window
*/
void ScriptingWindow::saveSettings()
{
QSettings settings;
settings.beginGroup("/ScriptWindow");
settings.setValue("/AlwaysOnTop", m_always_on_top->isChecked());
Gigg, Martyn Anthony
committed
settings.setValue("/ProgressArrow", m_manager->m_toggle_progress->isChecked());
settings.setValue("/LastDirectoryVisited", m_manager->m_last_dir);
Sofia Antony
committed
settings.setValue("/RecentScripts",m_manager->recentScripts());
Gigg, Martyn Anthony
committed
settings.endGroup();
Gigg, Martyn Anthony
committed
}
Gigg, Martyn Anthony
committed
Gigg, Martyn Anthony
committed
/**
* Override the closeEvent
* @param event :: A pointer to the event object
*/
void ScriptingWindow::closeEvent(QCloseEvent *event)
{
Gigg, Martyn Anthony
committed
// We ideally don't want a close button but are force by some window managers.
// Therefore if someone clicks close and MantidPlot is not quitting then we will just hide
if( !m_acceptClose )
{
Gigg, Martyn Anthony
committed
emit hideMe();
Gigg, Martyn Anthony
committed
//this->hide();
Gigg, Martyn Anthony
committed
return;
}
Gigg, Martyn Anthony
committed
emit closeMe();
// This will ensure each is saved correctly
Gigg, Martyn Anthony
committed
m_manager->closeAllTabs();
Gigg, Martyn Anthony
committed
event->accept();
}
/**
* Override the showEvent function
* @param event :: A pointer to the event object
*/
void ScriptingWindow::showEvent(QShowEvent *event)
{
if( m_manager->count() == 0 )
{
m_manager->newTab();
}
event->accept();
Gigg, Martyn Anthony
committed
}
Gigg, Martyn Anthony
committed
Gigg, Martyn Anthony
committed
/**
* Open a script directly. This is here for backwards compatability with the old ScriptWindow
* class
Janik Zikovsky
committed
* @param filename :: The file name
* @param newtab :: Do we want a new tab
Gigg, Martyn Anthony
committed
*/
Gigg, Martyn Anthony
committed
void ScriptingWindow::open(const QString & filename, bool newtab)
Gigg, Martyn Anthony
committed
{
Gigg, Martyn Anthony
committed
m_manager->open(newtab, filename);
Gigg, Martyn Anthony
committed
}
void ScriptingWindow::executeAll()
{
m_manager->executeAll();
}
//-------------------------------------------
// Private non-slot member functions
//-------------------------------------------
/**
* Accept a custom event and in this case test if it is a ScriptingChangeEvent
Janik Zikovsky
committed
* @param event :: The custom event
Gigg, Martyn Anthony
committed
*/
void ScriptingWindow::customEvent(QEvent *event)
{
if( !m_manager->isScriptRunning() && event->type() == SCRIPTING_CHANGE_EVENT )
{
ScriptingChangeEvent *sce = static_cast<ScriptingChangeEvent*>(event);
Gigg, Martyn Anthony
committed
setWindowTitle("MantidPlot: " + sce->scriptingEnv()->languageName() + " Window");
Gigg, Martyn Anthony
committed
}
}
/**
* Construct the file menu
*/
Gigg, Martyn Anthony
committed
void ScriptingWindow::fileMenuAboutToShow()
{
m_file_menu->clear();
Gigg, Martyn Anthony
committed
m_manager->populateFileMenu(*m_file_menu);
}
Gigg, Martyn Anthony
committed
/**
* Construct the edit menu
*/
Gigg, Martyn Anthony
committed
void ScriptingWindow::editMenuAboutToShow()
Gigg, Martyn Anthony
committed
{
m_edit_menu->clear();
Gigg, Martyn Anthony
committed
m_manager->populateEditMenu(*m_edit_menu);
}
Gigg, Martyn Anthony
committed
Gigg, Martyn Anthony
committed
/**
* Construct the exec menu for the current context
*/
void ScriptingWindow::execMenuAboutToShow()
{
m_run_menu->clear();
m_manager->populateExecMenu(*m_run_menu);
Gigg, Martyn Anthony
committed
}
Gigg, Martyn Anthony
committed
void ScriptingWindow::windowMenuAboutToShow()
Russell Taylor
committed
{
m_window_menu->clear();
m_window_menu->addAction(m_always_on_top);
m_window_menu->addAction(m_hide);
m_manager->populateWindowMenu(*m_window_menu);
Russell Taylor
committed
}
/**
*
*/
Gigg, Martyn Anthony
committed
void ScriptingWindow::updateWindowFlags()
{
Qt::WindowFlags flags = Qt::Window;
if( m_always_on_top->isChecked() )
{
flags |= Qt::WindowStaysOnTopHint;
}
setWindowFlags(flags);
//This is necessary due to the setWindowFlags function reparenting the window and causing is
//to hide itself
show();
}
void ScriptingWindow::tabSelectionChanged()
{
Gigg, Martyn Anthony
committed
}
Gigg, Martyn Anthony
committed
//-------------------------------------------
// Private non-slot member functions
//-------------------------------------------
/**
* Initialize the menus and actions
*/
void ScriptingWindow::initMenus()
{
//************* File menu *************
m_file_menu = menuBar()->addMenu(tr("&File"));
Gigg, Martyn Anthony
committed
#ifdef SCRIPTING_DIALOG
m_scripting_lang = new QAction(tr("Scripting &language"), this);
connect(m_scripting_lang, SIGNAL(activated()), this, SIGNAL(chooseScriptingLanguage()));
#endif
Gigg, Martyn Anthony
committed
connect(m_file_menu, SIGNAL(aboutToShow()), this, SLOT(fileMenuAboutToShow()));
Gigg, Martyn Anthony
committed
//************* Edit menu *************
m_edit_menu = menuBar()->addMenu(tr("&Edit"));
Gigg, Martyn Anthony
committed
connect(m_edit_menu, SIGNAL(aboutToShow()), this, SLOT(editMenuAboutToShow()));
Gigg, Martyn Anthony
committed
//************* Run menu *************
m_run_menu = menuBar()->addMenu(tr("E&xecute"));
Gigg, Martyn Anthony
committed
connect(m_run_menu, SIGNAL(aboutToShow()), this, SLOT(execMenuAboutToShow()));
Gigg, Martyn Anthony
committed
Gigg, Martyn Anthony
committed
//************* Window menu *************
Gigg, Martyn Anthony
committed
m_window_menu = menuBar()->addMenu(tr("&Window"));
connect(m_window_menu, SIGNAL(aboutToShow()), this, SLOT(windowMenuAboutToShow()));
Gigg, Martyn Anthony
committed
initWindowActions();
}
/**
* Create window actions
*/
void ScriptingWindow::initWindowActions()
{
Gigg, Martyn Anthony
committed
m_always_on_top = new QAction(tr("Always on &Top"), this);
m_always_on_top->setCheckable(true);
connect(m_always_on_top, SIGNAL(toggled(bool)), this, SLOT(updateWindowFlags()));
Gigg, Martyn Anthony
committed
Gigg, Martyn Anthony
committed
m_hide = new QAction(tr("&Hide"), this);
Russell Taylor
committed
#ifdef __APPLE__
m_hide->setShortcut(tr("Ctrl+3")); // F3 is used by the window manager on Mac
#else
Gigg, Martyn Anthony
committed
m_hide->setShortcut(tr("F3"));
Russell Taylor
committed
#endif
Gigg, Martyn Anthony
committed
// Note that we channel the hide through the parent so that we can save the geometry state
connect(m_hide, SIGNAL(activated()), this, SIGNAL(hideMe()));
Gigg, Martyn Anthony
committed
Gigg, Martyn Anthony
committed
}
Gigg, Martyn Anthony
committed
Sofia Antony
committed
/**
* calls ScriptManagerWidget saveToString and
* saves the currently opened script file names to a string
*/
QString ScriptingWindow::saveToString()
{
Gigg, Martyn Anthony
committed
return m_manager->saveToString();
Sofia Antony
committed
}
Gigg, Martyn Anthony
committed
/**
* Saves scripts file names to a string
* @param value If true a future close event will be accepted otherwise it will be ignored
*/
void ScriptingWindow::acceptCloseEvent(const bool value)
{
m_acceptClose = value;
}