From 0e5223fac472cf4ac8408f26ba41b3578d695896 Mon Sep 17 00:00:00 2001 From: Martyn Gigg <martyn.gigg@stfc.ac.uk> Date: Tue, 22 Feb 2011 12:12:29 +0000 Subject: [PATCH] Fix problems with the script window positioning and closing when in the preferences dialog. Fixes #1841 and Re #2258 --- .../MantidPlot/src/ApplicationWindow.cpp | 113 ++++++++++-------- .../Mantid/MantidPlot/src/ApplicationWindow.h | 8 +- Code/Mantid/MantidPlot/src/PythonScript.cpp | 3 +- Code/Mantid/MantidPlot/src/ScriptEditor.cpp | 2 +- Code/Mantid/MantidPlot/src/ScriptEditor.h | 5 +- .../Mantid/MantidPlot/src/ScriptingWindow.cpp | 42 ++++--- Code/Mantid/MantidPlot/src/ScriptingWindow.h | 15 ++- 7 files changed, 109 insertions(+), 79 deletions(-) diff --git a/Code/Mantid/MantidPlot/src/ApplicationWindow.cpp b/Code/Mantid/MantidPlot/src/ApplicationWindow.cpp index 749a5e68a4c..4adb20aafa8 100644 --- a/Code/Mantid/MantidPlot/src/ApplicationWindow.cpp +++ b/Code/Mantid/MantidPlot/src/ApplicationWindow.cpp @@ -407,6 +407,15 @@ void ApplicationWindow::init(bool factorySettings) results->insertPlainText("The scripting language is set to muParser. This is probably not what you want! Change the default in View->Preferences."); results->setTextColor(Qt::black); } + + // And the script window + // MG 09/02/2010 : Removed parent from scripting window. If it has one then it doesn't respect the always on top + // flag, it is treated as a sub window of its parent + scriptingWindow = new ScriptingWindow(scriptingEnv(),NULL); + scriptingWindow->setAttribute(Qt::WA_DeleteOnClose, false); + connect(scriptingWindow, SIGNAL(closeMe()), this, SLOT(saveScriptWindowGeometry())); + connect(scriptingWindow, SIGNAL(hideMe()), this, SLOT(showScriptWindow())); + connect(scriptingWindow, SIGNAL(chooseScriptingLanguage()), this, SLOT(showScriptingLangDialog())); } void ApplicationWindow::showLogWindowContextMenu(const QPoint & p) @@ -491,8 +500,6 @@ void ApplicationWindow::initGlobalConstants() autoSearchUpdatesRequest = false; show_windows_policy = ActiveFolder; - d_script_win_on_top = false; //M. Gigg, Mantid - d_script_win_rect = QRect(0, 0, 600, 660); d_init_window_type = NoWindow; QString aux = qApp->applicationDirPath(); @@ -564,6 +571,9 @@ void ApplicationWindow::initGlobalConstants() autoSaveTime = 15; d_backup_files = true; defaultScriptingLang = "Python"; //Mantid M. Gigg + // Scripting window geometry + d_script_win_pos = QPoint(250,200); + d_script_win_size = QSize(600,660); d_thousands_sep = true; d_locale = QLocale::system().name(); if (!d_thousands_sep) @@ -4900,10 +4910,8 @@ void ApplicationWindow::readSettings() settings.endGroup(); // ExportImage settings.beginGroup("/ScriptWindow"); - d_script_win_on_top = settings.value("/AlwaysOnTop", false).toBool(); //M. Gigg, Mantid - d_script_win_rect = QRect(settings.value("/x", 100).toInt(), settings.value("/y", 50).toInt(), - settings.value("/width", 600).toInt(), settings.value("/height", 660).toInt()); - d_script_win_arrow = settings.value("/ProgressArrow", true).toBool(); // Mantid - restore progress arrow state + d_script_win_pos = settings.value("/pos", QPoint(250,200)).toPoint(); + d_script_win_size = settings.value("/size", QSize(600,660)).toSize(); settings.endGroup(); settings.beginGroup("/ToolBars"); @@ -4917,7 +4925,7 @@ void ApplicationWindow::readSettings() d_display_tool_bar = settings.value("/DisplayToolBar", false).toBool(); d_format_tool_bar = settings.value("/FormatToolBar", true).toBool(); settings.endGroup(); - + //--------------------------------- // Mantid @@ -5256,20 +5264,18 @@ void ApplicationWindow::saveSettings() settings.setValue("/KeepAspect", d_keep_plot_aspect); settings.endGroup(); // ExportImage - if(m_scriptInterpreter) - { - m_scriptInterpreter->saveSettings(); - m_interpreterDock->hide(); - } - - if( scriptingWindow ) - { - scriptingWindow->raise();//Mantid - scriptingWindow->show();//Mantid - scriptingWindow->saveSettings(); //Mantid - scriptingWindow->hide(); - } - + + if(m_scriptInterpreter ) m_scriptInterpreter->saveSettings(); + if( scriptingWindow ) + { + settings.beginGroup("/ScriptWindow"); + // Geometry is applied by the app window + settings.setValue("/size", d_script_win_size); + settings.setValue("/pos", d_script_win_pos); + settings.endGroup(); + // Other specific settings + scriptingWindow->saveSettings(); + } settings.beginGroup("/ToolBars"); settings.setValue("/FileToolBar", d_file_tool_bar); @@ -8916,6 +8922,14 @@ void ApplicationWindow::closeEvent( QCloseEvent* ce ) } } + + if( scriptingWindow ) + { + this->showScriptWindow(true); + scriptingWindow->saveSettings(); + scriptingWindow->close(); + } + //Save the settings and exit saveSettings(); mantidUI->shutdown(); @@ -12552,11 +12566,11 @@ void ApplicationWindow::createActions() actionNoteEvaluate->setShortcut(tr("Ctrl+Return")); #ifdef SCRIPTING_PYTHON - actionShowScriptWindow = new QAction(getQPixmap("python_xpm"), tr("&Script Window"), this); + actionShowScriptWindow = new QAction(getQPixmap("python_xpm"), tr("Toggle &Script Window"), this); actionShowScriptWindow->setShortcut(tr("F3")); - actionShowScriptWindow->setToggleAction( true ); + actionShowScriptWindow->setToggleAction(true); connect(actionShowScriptWindow, SIGNAL(activated()), this, SLOT(showScriptWindow())); - actionShowScriptInterpreter = new QAction(getQPixmap("python_xpm"), tr("Script &Interpreter"), this); + actionShowScriptInterpreter = new QAction(getQPixmap("python_xpm"), tr("Toggle Script &Interpreter"), this); actionShowScriptInterpreter->setShortcut(tr("F4")); actionShowScriptInterpreter->setToggleAction(true); connect(actionShowScriptInterpreter, SIGNAL(activated()), this, SLOT(showScriptInterpreter())); @@ -15456,35 +15470,28 @@ void ApplicationWindow::goToColumn() } } -void ApplicationWindow::showScriptWindow() -{ - if (!scriptingWindow) - { - // MG 09/02/2010 : Removed parent from scripting window. If it has one then it doesn't respect the always on top - // flag, it is treated as a sub window of its parent - scriptingWindow = new ScriptingWindow(scriptingEnv(),NULL); - connect(scriptingWindow, SIGNAL(chooseScriptingLanguage()), this, SLOT(showScriptingLangDialog())); - scriptingWindow->resize(d_script_win_rect.size()); - scriptingWindow->move(d_script_win_rect.topLeft()); - } - - if (!scriptingWindow->isVisible()) - { - Qt::WindowFlags flags = Qt::Window; - if (d_script_win_on_top) - { - flags |= Qt::WindowStaysOnTopHint; - } - scriptingWindow->setWindowFlags(flags); - scriptingWindow->show(); - scriptingWindow->setFocus(); - } - else - { - scriptingWindow->hide(); - } -} - +void ApplicationWindow::showScriptWindow(bool forceVisible) +{ + if( forceVisible || scriptingWindow->isMinimized() || !scriptingWindow->isVisible() ) + { + scriptingWindow->resize(d_script_win_size); + scriptingWindow->move(d_script_win_pos); + scriptingWindow->show(); + scriptingWindow->setFocus(); + } + else + { + saveScriptWindowGeometry(); + scriptingWindow->hide(); + } +} + +void ApplicationWindow::saveScriptWindowGeometry() +{ + d_script_win_size = scriptingWindow->size(); + d_script_win_pos = scriptingWindow->pos(); +} + void ApplicationWindow::showScriptInterpreter() { if( !m_interpreterDock ) diff --git a/Code/Mantid/MantidPlot/src/ApplicationWindow.h b/Code/Mantid/MantidPlot/src/ApplicationWindow.h index 043d487c7cf..4c455d25712 100644 --- a/Code/Mantid/MantidPlot/src/ApplicationWindow.h +++ b/Code/Mantid/MantidPlot/src/ApplicationWindow.h @@ -693,7 +693,8 @@ public slots: //! Connected to the context menu signal from lv; it's called when there are no items selected in the list void showListViewPopupMenu(const QPoint &p); - void showScriptWindow(); + void showScriptWindow(bool forceVisible = false); + void saveScriptWindowGeometry(); void showScriptInterpreter(); void showMoreWindows(); void showMarkerPopupMenu(); @@ -1118,8 +1119,9 @@ public: bool d_plot_tool_bar, d_plot3D_tool_bar, d_display_tool_bar, d_format_tool_bar; bool d_backup_files; WindowType d_init_window_type; - QRect d_script_win_rect, d_app_rect; - bool d_script_win_on_top; + QRect d_app_rect; + QPoint d_script_win_pos; + QSize d_script_win_size; bool d_script_win_arrow; bool d_inform_rename_table; QString d_export_col_separator; diff --git a/Code/Mantid/MantidPlot/src/PythonScript.cpp b/Code/Mantid/MantidPlot/src/PythonScript.cpp index 7141505c8c4..54c021d3ff0 100644 --- a/Code/Mantid/MantidPlot/src/PythonScript.cpp +++ b/Code/Mantid/MantidPlot/src/PythonScript.cpp @@ -103,7 +103,8 @@ void PythonScript::updatePath(const QString & filename, bool append) } else { - pyCode = "sys.path.remove(r'%1')"; + pyCode = "if r'%1' in sys.path:\n" + " sys.path.remove(r'%1')"; } setCode(pyCode.arg(scriptPath)); exec(); diff --git a/Code/Mantid/MantidPlot/src/ScriptEditor.cpp b/Code/Mantid/MantidPlot/src/ScriptEditor.cpp index f7db4139fb3..ae87c545197 100644 --- a/Code/Mantid/MantidPlot/src/ScriptEditor.cpp +++ b/Code/Mantid/MantidPlot/src/ScriptEditor.cpp @@ -158,7 +158,7 @@ ScriptEditor::ScriptEditor(QWidget *parent, bool interpreter_mode, QsciLexer *co connect(m_print, SIGNAL(activated()), this, SLOT(print())); m_zoomIn = new QAction(("Zoom &In"), this); - m_zoomIn->setShortcut(tr("Ctrl++")); + m_zoomIn->setShortcut(Qt::ControlModifier + Qt::Key_Minus); connect(m_zoomIn, SIGNAL(activated()),this,SLOT(zoomIn())); m_zoomOut = new QAction(("Zoom &Out"), this); diff --git a/Code/Mantid/MantidPlot/src/ScriptEditor.h b/Code/Mantid/MantidPlot/src/ScriptEditor.h index 0b3723a86b2..eda3ac73b6e 100644 --- a/Code/Mantid/MantidPlot/src/ScriptEditor.h +++ b/Code/Mantid/MantidPlot/src/ScriptEditor.h @@ -207,7 +207,6 @@ public slots: void displayOutput(const QString& msg, bool error); /// Overrride the paste command when in interpreter mode void paste(); - signals: /// Inform observers that undo information is available @@ -235,7 +234,6 @@ private: /// if it's start of multi line bool isStartOfMultiLine(); - /// if it's multiline statement bool isMultiLineStatement(); ///if it's end of multi line @@ -248,8 +246,9 @@ private: /// The file name associated with this editor QString m_filename; - //Each editor needs its own undo/redo etc + /// Each editor needs its own undo/redo etc QAction *m_undo, *m_redo, *m_cut, *m_copy, *m_paste, *m_print; + /// Zoom in/out actions QAction *m_zoomIn,*m_zoomOut; /// The margin marker int m_marker_handle; diff --git a/Code/Mantid/MantidPlot/src/ScriptingWindow.cpp b/Code/Mantid/MantidPlot/src/ScriptingWindow.cpp index d0b0ccf13b1..0cd349094c1 100755 --- a/Code/Mantid/MantidPlot/src/ScriptingWindow.cpp +++ b/Code/Mantid/MantidPlot/src/ScriptingWindow.cpp @@ -338,19 +338,38 @@ 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()); settings.setValue("/ProgressArrow", m_manager->m_toggle_progress->isChecked()); settings.setValue("/LastDirectoryVisited", m_manager->m_last_dir); settings.setValue("/RecentScripts",m_manager->recentScripts()); settings.endGroup(); +} +/** + * Override the closeEvent + * @param event :: A pointer to the event object + */ +void ScriptingWindow::closeEvent(QCloseEvent *event) +{ + emit closeMe(); + // This will ensure each is saved correctly m_manager->closeAllTabs(); + 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(); } + /** * Open a script directly. This is here for backwards compatability with the old ScriptWindow * class @@ -425,9 +444,6 @@ void ScriptingWindow::fileAboutToShow() m_file_menu->insertSeparator(); m_file_menu->addAction(m_manager->m_close_tab); - // Close scripting window - m_file_menu->addAction(m_close_window); - } /** @@ -504,10 +520,6 @@ void ScriptingWindow::initMenus() m_print_output = new QAction(tr("Print &Output"), this); connect(m_print_output, SIGNAL(activated()), m_output_dock, SLOT(print())); - m_close_window = new QAction(tr("Close Script Window"), this); - m_close_window->setShortcut(tr("F3")); - connect(m_close_window, SIGNAL(activated()), this, SLOT(close())); - //************* Edit menu ************* m_edit_menu = menuBar()->addMenu(tr("&Edit")); connect(m_edit_menu, SIGNAL(aboutToShow()), this, SLOT(editAboutToShow())); @@ -533,7 +545,9 @@ void ScriptingWindow::initMenus() 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_hide->setShortcut(tr("F3")); + // 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())); m_window_menu->addAction(m_hide); m_window_menu->insertSeparator(); @@ -559,5 +573,5 @@ void ScriptingWindow::initMenus() */ QString ScriptingWindow::saveToString() { - return m_manager->saveToString(); + return m_manager->saveToString(); } diff --git a/Code/Mantid/MantidPlot/src/ScriptingWindow.h b/Code/Mantid/MantidPlot/src/ScriptingWindow.h index bab8259d4ab..e967f0b4f01 100755 --- a/Code/Mantid/MantidPlot/src/ScriptingWindow.h +++ b/Code/Mantid/MantidPlot/src/ScriptingWindow.h @@ -17,7 +17,8 @@ class QPoint; class QMenu; class QAction; class QCloseEvent; -class ApplicationWindow; +class QShowEvent; +class QHideEvent; /** @class ScriptOutputDock @@ -61,7 +62,7 @@ public: ScriptOutputDock(const QString & title, ScriptManagerWidget *manager, QWidget *parent = 0, Qt::WindowFlags flags = 0); - //Is there anything here + /// Is there anything here bool isEmpty() const; public slots: @@ -109,6 +110,10 @@ public: ScriptingWindow(ScriptingEnv *env,QWidget *parent = 0, Qt::WindowFlags flags = 0); ///Destructor ~ScriptingWindow(); + /// Override the closeEvent + void closeEvent(QCloseEvent *event); + /// Override the showEvent + void showEvent(QShowEvent *event); /// Is a script running? bool isScriptRunning() const; ///Save the current state of the script window for next time @@ -125,6 +130,10 @@ public: signals: /// Show the scripting language dialog void chooseScriptingLanguage(); + /// Tell others we are closing + void closeMe(); + /// Tell others we are hiding + void hideMe(); private: /// Create menu bar and menu actions @@ -162,8 +171,6 @@ private: QAction *m_always_on_top, *m_hide, *m_toggle_output, *m_print_output; /// Change scripting language QAction *m_scripting_lang; - /// Close the scripting window - QAction *m_close_window; }; #endif //SCRIPTINGWINDOW_H_ -- GitLab