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
//Qt
#include <QTextEdit>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QSettings>
#include <QPrintDialog>
#include <QPrinter>
Gigg, Martyn Anthony
committed
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
//***************************************************************************
//
// ScriptOutputDock class
//
//***************************************************************************
/**
* Constructor
* @param title The title
* @param parent The parent widget
* @param flags Window flags
*/
ScriptOutputDock::ScriptOutputDock(const QString & title, QWidget * parent,
Qt::WindowFlags flags ) :
QDockWidget(title, parent, flags)
{
setFeatures(QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable);
// The text display
m_text_display = new QTextEdit(this);
m_text_display->setReadOnly(true);
m_text_display->setLineWrapMode(QTextEdit::FixedColumnWidth);
m_text_display->setLineWrapColumnOrWidth(105);
m_text_display->setAutoFormatting(QTextEdit::AutoNone);
// Change to fix width font so that table formatting isn't screwed up
QFont f("Andale Mono");
f.setFixedPitch(true);
f.setPointSize(8);
m_text_display->setCurrentFont(f);
m_text_display->setMinimumWidth(5);
m_text_display->setMinimumHeight(5);
m_text_display->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_text_display, SIGNAL(customContextMenuRequested(const QPoint&)), this,
SLOT(showContextMenu(const QPoint&)));
initActions();
Gigg, Martyn Anthony
committed
setWidget(m_text_display);
}
/**
* Is there anything here
*/
bool ScriptOutputDock::isEmpty() const
{
return m_text_display->document()->isEmpty();
}
Gigg, Martyn Anthony
committed
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
/**
* Clear the text area
*/
void ScriptOutputDock::clear()
{
m_text_display->clear();
}
/**
* Change the title based on the script's execution state
* @param running The current state of the script environment
*/
void ScriptOutputDock::setScriptIsRunning(bool running)
{
QString title("Script Output - Status: ");
if( running )
{
title += "Running ...";
}
else
{
title += "Stopped";
}
setWindowTitle(title);
}
//-------------------------------------------
// Private slot member functions
//-------------------------------------------
/**
* Display an output message in the output dock
* @param msg The msg
* @param error Indicate that this is an error
*/
void ScriptOutputDock::displayOutputMessage(const QString &msg, bool error)
{
if( error )
{
m_text_display->setTextColor(Qt::red);
}
else
{
m_text_display->setTextColor(Qt::black);
}
m_text_display->textCursor().insertText(msg);
m_text_display->moveCursor(QTextCursor::End);
}
/**
* Display a context menu
*/
void ScriptOutputDock::showContextMenu(const QPoint & pos)
{
QMenu menu(this);
QAction* clear = new QAction("Clear", this);
connect(clear, SIGNAL(activated()), this, SLOT(clear()));
menu.addAction(clear);
//Copy action
menu.addAction(m_copy);
Gigg, Martyn Anthony
committed
if( !m_text_display->document()->isEmpty() )
{
QAction* print = new QAction(QPixmap(fileprint_xpm), "&Print", this);
connect(print, SIGNAL(activated()), this, SLOT(print()));
menu.addAction(print);
Gigg, Martyn Anthony
committed
}
menu.exec(m_text_display->mapToGlobal(pos));
}
void ScriptOutputDock::print()
{
QPrinter printer;
QPrintDialog *print_dlg = new QPrintDialog(&printer, this);
print_dlg->setWindowTitle(tr("Print Output"));
if (print_dlg->exec() != QDialog::Accepted)
return;
QTextDocument document(m_text_display->text());
document.print(&printer);
}
Gigg, Martyn Anthony
committed
//-------------------------------------------
// Private non-slot member functions
//-------------------------------------------
/**
* Create the actions associated with this widget
*/
void ScriptOutputDock::initActions()
{
// Copy action
m_copy = new QAction(QPixmap(copy_xpm), "Copy", this);
m_copy->setShortcut(tr("Ctrl+C"));
connect(m_copy, SIGNAL(activated()), m_text_display, SLOT(copy()));
}
Gigg, Martyn Anthony
committed
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
//***************************************************************************
//
// ScriptingWindow class
//
//***************************************************************************
//-------------------------------------------
// Public member functions
//-------------------------------------------
/**
* Constructor
* @param env The scripting environment
* @param parent The parent widget
* @param flags Window flags passed to the base class
*/
ScriptingWindow::ScriptingWindow(ScriptingEnv *env, QWidget *parent, Qt::WindowFlags flags) :
QMainWindow(parent, flags)
{
setObjectName("MantidScriptWindow");
// Sub-widgets
m_manager = new ScriptManagerWidget(env, this);
setCentralWidget(m_manager);
m_output_dock = new ScriptOutputDock(QString(), this);
m_output_dock->setScriptIsRunning(false);
//Set the height to 10% of the height of the window
addDockWidget(Qt::BottomDockWidgetArea, m_output_dock);
int dock_width = m_output_dock->geometry().width();
m_output_dock->resize(dock_width, this->geometry().height() * 0.01);
connect(m_manager, SIGNAL(MessageToPrint(const QString&,bool)), m_output_dock,
SLOT(displayOutputMessage(const QString&, bool)));
connect(m_manager, SIGNAL(ScriptIsActive(bool)), m_output_dock, SLOT(setScriptIsRunning(bool)));
// Create menus and actions
initMenus();
fileAboutToShow();
editAboutToShow();
Gigg, Martyn Anthony
committed
// 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
QSettings settings;
settings.beginGroup("/ScriptWindow");
QString lastdir = settings.value("LastDirectoryVisited", "").toString();
Gigg, Martyn Anthony
committed
// If nothgin, set the last directory to the Mantid scripts directory (if present)
if( lastdir.isEmpty() )
Sofia Antony
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);
}
Gigg, Martyn Anthony
committed
settings.endGroup();
Gigg, Martyn Anthony
committed
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
setWindowIcon(QIcon(":/MantidPlot_Icon_32offset.png"));
setWindowTitle("MantidPlot: " + env->scriptingLanguage() + " Window");
setFocusPolicy(Qt::StrongFocus);
setFocusProxy(m_manager);
}
/**
* Destructor
*/
ScriptingWindow::~ScriptingWindow()
{
delete m_manager;
delete m_output_dock;
}
/**
* 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());
QRect window_size(pos(), size());
settings.setValue("/x", window_size.x());
settings.setValue("/y", window_size.y());
settings.setValue("/width", window_size.width());
settings.setValue("/height", window_size.height());
Gigg, Martyn Anthony
committed
settings.setValue("/ProgressArrow", m_manager->m_toggle_progress->isChecked());
settings.setValue("/LastDirectoryVisited", m_manager->m_last_dir);
Gigg, Martyn Anthony
committed
settings.endGroup();
m_manager->closeAllTabs();
}
/**
* Open a script directly. This is here for backwards compatability with the old ScriptWindow
* class
* @param filename The file name
Gigg, Martyn Anthony
committed
* @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
* @param event The custom event
*/
void ScriptingWindow::customEvent(QEvent *event)
{
if( !m_manager->isScriptRunning() && event->type() == SCRIPTING_CHANGE_EVENT )
{
ScriptingChangeEvent *sce = static_cast<ScriptingChangeEvent*>(event);
setWindowTitle("MantidPlot: " + sce->scriptingEnv()->scriptingLanguage() + " Window");
}
}
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
* Construct the file menu
*/
void ScriptingWindow::fileAboutToShow()
{
m_file_menu->clear();
// New tab
m_file_menu->addAction(m_manager->m_new_tab);
// Open a file in current tab
m_file_menu->addAction(m_manager->m_open_curtab);
//Open in new tab
m_file_menu->addAction(m_manager->m_open_newtab);
// Save a script
m_file_menu->insertSeparator();
m_file_menu->addAction(m_manager->m_save);
// Save a script under a new file name
m_file_menu->addAction(m_manager->m_saveas);
//Print
if( m_manager->count() > 0 )
{
m_file_menu->addAction(m_manager->printAction());
}
if( !m_output_dock->isEmpty() )
{
m_file_menu->addAction(m_print_output);
}
// Close current tab
m_file_menu->insertSeparator();
m_file_menu->addAction(m_manager->m_close_tab);
}
Gigg, Martyn Anthony
committed
/**
* Construct the edit menu
*/
void ScriptingWindow::editAboutToShow()
{
m_edit_menu->clear();
if( m_manager->count() > 0 )
{
// Undo
m_edit_menu->addAction(m_manager->undoAction());
//Redo
m_edit_menu->addAction(m_manager->redoAction());
//Cut
m_edit_menu->addAction(m_manager->cutAction());
//Copy
m_edit_menu->addAction(m_manager->copyAction());
//Paste
m_edit_menu->addAction(m_manager->pasteAction());
//Find and replace
m_edit_menu->insertSeparator();
m_edit_menu->addAction(m_manager->m_find);
m_edit_menu->insertSeparator();
Gigg, Martyn Anthony
committed
//Clear output
m_edit_menu->addAction(m_clear_output);
Gigg, Martyn Anthony
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()
{
// Ensure that the shortcuts are active
fileAboutToShow();
editAboutToShow();
}
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"));
connect(m_file_menu, SIGNAL(aboutToShow()), this, SLOT(fileAboutToShow()));
m_print_output = new QAction(tr("Print &Output"), this);
connect(m_print_output, SIGNAL(activated()), m_output_dock, SLOT(print()));
Gigg, Martyn Anthony
committed
//************* Edit menu *************
m_edit_menu = menuBar()->addMenu(tr("&Edit"));
connect(m_edit_menu, SIGNAL(aboutToShow()), this, SLOT(editAboutToShow()));
// Clear output
m_clear_output = new QAction(tr("&Clear Output"), this);
Gigg, Martyn Anthony
committed
connect(m_clear_output, SIGNAL(activated()), m_output_dock, SLOT(clear()));
Gigg, Martyn Anthony
committed
//************* Run menu *************
m_run_menu = menuBar()->addMenu(tr("E&xecute"));
// Execute script
m_run_menu->addAction(m_manager->m_exec);
// Execute everything from a script
m_run_menu->addAction(m_manager->m_exec_all);
//Evaluate function for those environments that support one
// m_run_menu->addAction(m_manager->m_eval);
Gigg, Martyn Anthony
committed
//************* Window menu *************
Gigg, Martyn Anthony
committed
m_window_menu = menuBar()->addMenu(tr("&Window"));
//Always on top
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()));
m_window_menu->addAction(m_always_on_top);
//Hide
m_hide = new QAction(tr("&Hide"), this);
connect(m_hide, SIGNAL(activated()), this, SLOT(hide()));
m_window_menu->addAction(m_hide);
//Toggle output dock
m_toggle_output = m_output_dock->toggleViewAction();
m_toggle_output->setText("&Show Output");
m_toggle_output->setChecked(true);
m_window_menu->addAction(m_toggle_output);
Gigg, Martyn Anthony
committed
//Toggle progress
m_window_menu->addAction(m_manager->m_toggle_progress);
Gigg, Martyn Anthony
committed
}
Sofia Antony
committed
/**
* calls ScriptManagerWidget saveToString and
* saves the currently opened script file names to a string
*/
QString ScriptingWindow::saveToString()
{
return m_manager->saveToString();
}