Skip to content
Snippets Groups Projects
ApplicationWindow.cpp 584 KiB
Newer Older
/***************************************************************************
	File                 : ApplicationWindow.cpp
	Project              : QtiPlot
--------------------------------------------------------------------
	Copyright            : (C) 2006 by Ion Vasilief,
	                       Tilman Hoener zu Siederdissen,
                           Knut Franke
	Email (use @ for *)  : ion_vasilief*yahoo.fr, thzs*gmx.net,
	                       knut.franke*gmx.de
	Description          : QtiPlot's main window

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *  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 2 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, write to the Free Software           *
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
 *   Boston, MA  02110-1301  USA                                           *
 *                                                                         *
 ***************************************************************************/
#include "globals.h"
#include "ApplicationWindow.h"
#include "pixmaps.h"
#include "CurvesDialog.h"
#include "PlotDialog.h"
#include "AxesDialog.h"
#include "LineDialog.h"
#include "TextDialog.h"
#include "ExportDialog.h"
#include "TableDialog.h"
#include "SetColValuesDialog.h"
#include "ErrDialog.h"
#include "LegendWidget.h"
#include "ArrowMarker.h"
#include "ImageMarker.h"
#include "Graph.h"
#include "Plot.h"
#include "Grid.h"
#include "PlotWizard.h"
#include "PolynomFitDialog.h"
#include "ExpDecayDialog.h"
#include "FunctionDialog.h"
#include "FitDialog.h"
#include "SurfaceDialog.h"
#include "Graph3D.h"
#include "Plot3DDialog.h"
#include "ImageDialog.h"
#include "MultiLayer.h"
#include "LayerDialog.h"
#include "DataSetDialog.h"
#include "IntDialog.h"
#include "ConfigDialog.h"
#include "MatrixDialog.h"
#include "MatrixSizeDialog.h"
#include "MatrixValuesDialog.h"
#include "MatrixModel.h"
#include "MatrixCommand.h"
#include "importOPJ.h"
#include "AssociationsDialog.h"
#include "RenameWindowDialog.h"
#include "QwtErrorPlotCurve.h"
#include "InterpolationDialog.h"
#include "ImportASCIIDialog.h"
#include "ImageExportDialog.h"
#include "SmoothCurveDialog.h"
#include "FilterDialog.h"
#include "FFTDialog.h"
#include "Note.h"
#include "Folder.h"
#include "FindDialog.h"
#include "ScaleDraw.h"
#include "plot2D/ScaleEngine.h"
#include "ScriptingLangDialog.h"
#include "ScriptingWindow.h"
#include "ScriptFileInterpreter.h"
#include "TableStatistics.h"
#include "Fit.h"
#include "MultiPeakFit.h"
#include "PolynomialFit.h"
#include "SigmoidalFit.h"
#include "LogisticFit.h"
#include "NonLinearFit.h"
#include "FunctionCurve.h"
#include "QwtPieCurve.h"
#include "Spectrogram.h"
#include "Integration.h"
#include "Differentiation.h"
#include "SmoothFilter.h"
#include "FFTFilter.h"
#include "Convolution.h"
#include "Correlation.h"
#include "CurveRangeDialog.h"
#include "ColorBox.h"
#include "QwtHistogram.h"
#include "OpenProjectDialog.h"
#include "ColorMapDialog.h"
#include "TextEditor.h"
#include "SymbolDialog.h"
#include "CustomActionDialog.h"
#include "MdiSubWindow.h"
#include "FloatingWindow.h"

// TODO: move tool-specific code to an extension manager
#include "ScreenPickerTool.h"
#include "DataPickerTool.h"
#include "TranslateCurveTool.h"
#include "MultiPeakFitTool.h"
#include "LineProfileTool.h"
#include "RangeSelectorTool.h"
#include "PlotToolInterface.h"
#include "Mantid/MantidMatrix.h"
#include "Mantid/MantidTable.h"
#include "Mantid/MantidMatrixCurve.h"
#include "ContourLinesEditor.h"
#include "Mantid/InstrumentWidget/InstrumentWindow.h"
#include "Mantid/RemoveErrorsDialog.h"

#include <stdio.h>
#include <stdlib.h>

#include <qwt_scale_engine.h>
#include <QFileDialog>
#include <QInputDialog>
#include <QProgressDialog>
#include <QPrintDialog>
#include <QPixmapCache>
#include <QMenuBar>
#include <QClipboard>
#include <QTranslator>
#include <QSplitter>
#include <QSettings>
#include <QApplication>
#include <QMessageBox>
#include <QPrinter>
#include <QPrinterInfo>
#include <QActionGroup>
#include <QAction>
#include <QToolBar>
#include <QKeySequence>
#include <QImageReader>
#include <QImageWriter>
#include <QDateTime>
#include <QShortcut>
#include <QDockWidget>
#include <QTextStream>
#include <QVarLengthArray>
#include <QList>
#include <QUrl>
#include <QFontComboBox>
#include <QSpinBox>
#include <QMdiArea>
#include <QMdiSubWindow>
#include <QUndoStack>
#include <QUndoView>
#include <QSignalMapper>
#include <QDesktopWidget>

#include <zlib.h>

#include <gsl/gsl_sort.h>

#include <boost/regex.hpp>

//Mantid
#include "ScriptingWindow.h"

#include "Mantid/MantidUI.h"
#include "Mantid/MantidAbout.h"
#include "Mantid/PeakPickerTool.h"
#include "Mantid/ManageCustomMenus.h"
#include "Mantid/FirstTimeSetup.h"
#include "Mantid/SetUpParaview.h"

#include "MantidQtAPI/InterfaceManager.h"
#include "MantidQtAPI/UserSubWindow.h"
#include "MantidQtAPI/AlgorithmInputHistory.h"
#include "MantidQtAPI/ManageUserDirectories.h"

#include "MantidQtMantidWidgets/ICatSearch.h"
#include "MantidQtMantidWidgets/ICatMyDataSearch.h"
#include "MantidQtMantidWidgets/ICatAdvancedSearch.h"
#include "MantidQtMantidWidgets/FitPropertyBrowser.h"
#include "MantidQtMantidWidgets/MuonFitPropertyBrowser.h"

#include "MantidKernel/ConfigService.h"
#include "MantidKernel/Logger.h"
#include "MantidKernel/MantidVersion.h"

#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/AlgorithmFactory.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/AnalysisDataService.h"

using namespace Qwt3D;
using namespace MantidQt::API;

extern "C"
{
void file_compress(const char  *file, const char  *mode);
void file_uncompress(const char  *file);
}

ApplicationWindow::ApplicationWindow(bool factorySettings)
: QMainWindow(), 
Scripted(ScriptingLangManager::newEnv(this)),
blockWindowActivation(false),
m_enableQtiPlotFitting(false),
m_exitCode(0),
#ifdef Q_OS_MAC // Mac
  settings(QSettings::IniFormat,QSettings::UserScope, "ISIS", "MantidPlot")
#else
  settings("ISIS", "MantidPlot")
#endif
{
  QStringList empty;
  init(factorySettings, empty);
}

ApplicationWindow::ApplicationWindow(bool factorySettings, const QStringList& args)
: QMainWindow(), 
Scripted(ScriptingLangManager::newEnv(this)),
blockWindowActivation(false),
m_enableQtiPlotFitting(false),
m_exitCode(0),
#ifdef Q_OS_MAC // Mac
  settings(QSettings::IniFormat,QSettings::UserScope, "ISIS", "MantidPlot")
#else
  settings("ISIS", "MantidPlot")
#endif
{
  init(factorySettings, args);
}

/**
 * Calls QCoreApplication::exit(m_exitCode)
 */
void ApplicationWindow::exitWithPresetCode()
{
  QCoreApplication::exit(m_exitCode);
}

void ApplicationWindow::init(bool factorySettings, const QStringList& args)
  QCoreApplication::setOrganizationName("ISIS");
  QCoreApplication::setApplicationName("MantidPlot");
#ifdef SHARED_MENUBAR
  m_sharedMenuBar = new QMenuBar(NULL);
  //setMenuBar(m_sharedMenuBar);
  m_sharedMenuBar->setNativeMenuBar(true);
#endif
  mantidUI = new MantidUI(this);
  setAttribute(Qt::WA_DeleteOnClose);

  setWindowTitle(tr("MantidPlot - untitled"));//Mantid
  setObjectName("main application");
  initGlobalConstants();
  QPixmapCache::setCacheLimit(20*QPixmapCache::cacheLimit ());

  tablesDepend = new QMenu(this);

  explorerWindow = new QDockWidget( this );
  explorerWindow->setWindowTitle(tr("Project Explorer"));
  explorerWindow->setObjectName("explorerWindow"); // this is needed for QMainWindow::restoreState()
  explorerWindow->setMinimumHeight(150);
  addDockWidget( Qt::BottomDockWidgetArea, explorerWindow );

  actionSaveFile=NULL;
  actionSaveProject=NULL;
  folders = new FolderListView(this);
  folders->header()->setClickEnabled( false );
  folders->addColumn( tr("Folder") );
  folders->setRootIsDecorated( true );
  folders->setResizeMode(Q3ListView::LastColumn);
  folders->header()->hide();
  folders->setSelectionMode(Q3ListView::Single);

  connect(folders, SIGNAL(currentChanged(Q3ListViewItem *)),
      this, SLOT(folderItemChanged(Q3ListViewItem *)));
  connect(folders, SIGNAL(itemRenamed(Q3ListViewItem *, int, const QString &)),
      this, SLOT(renameFolder(Q3ListViewItem *, int, const QString &)));
  connect(folders, SIGNAL(contextMenuRequested(Q3ListViewItem *, const QPoint &, int)),
      this, SLOT(showFolderPopupMenu(Q3ListViewItem *, const QPoint &, int)));
  connect(folders, SIGNAL(dragItems(QList<Q3ListViewItem *>)),
      this, SLOT(dragFolderItems(QList<Q3ListViewItem *>)));
  connect(folders, SIGNAL(dropItems(Q3ListViewItem *)),
      this, SLOT(dropFolderItems(Q3ListViewItem *)));
  connect(folders, SIGNAL(renameItem(Q3ListViewItem *)),
      this, SLOT(startRenameFolder(Q3ListViewItem *)));
  connect(folders, SIGNAL(addFolderItem()), this, SLOT(addFolder()));
  connect(folders, SIGNAL(deleteSelection()), this, SLOT(deleteSelectedItems()));

  current_folder = new Folder( 0, tr("UNTITLED"));
  FolderListItem *fli = new FolderListItem(folders, current_folder);
  current_folder->setFolderListItem(fli);
  fli->setOpen( true );

  lv = new FolderListView();
  lv->addColumn (tr("Name"),-1 );
  lv->addColumn (tr("Type"),-1 );
  lv->addColumn (tr("View"),-1 );
  lv->addColumn (tr("Size"),-1 );
  lv->addColumn (tr("Created"),-1);
  lv->addColumn (tr("Label"),-1);
  lv->setResizeMode(Q3ListView::LastColumn);
  lv->setMinimumHeight(80);
  lv->setSelectionMode(Q3ListView::Extended);
  lv->setDefaultRenameAction (Q3ListView::Accept);

  explorerSplitter = new QSplitter(Qt::Horizontal, explorerWindow);
  explorerSplitter->addWidget(folders);
  explorerSplitter->addWidget(lv);
  explorerWindow->setWidget(explorerSplitter);

  QList<int> splitterSizes;
  explorerSplitter->setSizes( splitterSizes << 45 << 45);
  explorerWindow->hide();

  logWindow = new QDockWidget(this);
  logWindow->setObjectName("logWindow"); // this is needed for QMainWindow::restoreState()
  logWindow->setWindowTitle(tr("Results Log"));
  addDockWidget( Qt::TopDockWidgetArea, logWindow );

  results=new QTextEdit(logWindow);
  results->setReadOnly (true);
  results->setContextMenuPolicy(Qt::CustomContextMenu);
  connect(results, SIGNAL(customContextMenuRequested(const QPoint &)), this, 
	  SLOT(showLogWindowContextMenu(const QPoint &)));
  logWindow->setWidget(results);
  logWindow->hide();

  consoleWindow = new QDockWidget(this);
  consoleWindow->setObjectName("consoleWindow"); // this is needed for QMainWindow::restoreState()
  consoleWindow->setWindowTitle(tr("Scripting Console"));
  addDockWidget( Qt::TopDockWidgetArea, consoleWindow );
  console = new QTextEdit(consoleWindow);
  console->setReadOnly(true);
  console->setContextMenuPolicy(Qt::CustomContextMenu);
  connect(console, SIGNAL(customContextMenuRequested(const QPoint &)), this,
	  SLOT(showScriptConsoleContextMenu(const QPoint &)));
  consoleWindow->setWidget(console);
  consoleWindow->hide();
  m_interpreterDock = new QDockWidget(this);
  m_interpreterDock->setObjectName("interpreterDock"); // this is needed for QMainWindow::restoreState()
  m_interpreterDock->setWindowTitle("Script Interpreter");
  addDockWidget( Qt::BottomDockWidgetArea, m_interpreterDock );
  // This is a temporary widget so that when the settings are read the dock's visible state is correct.
  // It gets replaced with the script widget after the scripting language has been read and set
  m_interpreterDock->setWidget(new QTextEdit);
  m_interpreterDock->hide();

  undoStackWindow = new QDockWidget(this);
  undoStackWindow->setObjectName("undoStackWindow"); // this is needed for QMainWindow::restoreState()
  undoStackWindow->setWindowTitle(tr("Undo Stack"));
  addDockWidget(Qt::RightDockWidgetArea, undoStackWindow);

  d_undo_view = new QUndoView(undoStackWindow);
  d_undo_view->setCleanIcon(QIcon(getQPixmap("filesave_xpm")));
  undoStackWindow->setWidget(d_undo_view);
  undoStackWindow->hide();

  /*
  If applicable, set the Paraview path BEFORE libaries are loaded. Doing it here, before the call to MantidUI::init() prevents 
  the logs being poluted with library loading errors.
Owen Arnold's avatar
Owen Arnold committed
  trySetParaviewPath(args);
  //Initialize Mantid
  // MG: 01/02/2009 - Moved this to before scripting so that the logging is connected when
  // we register Python algorithms
  mantidUI->init();

  // Needs to be done after initialization of dock windows,
  // because we now use QDockWidget::toggleViewAction()
  createActions();
  initToolBars();
  initMainMenu();

  d_workspace = new QMdiArea();
  d_workspace->setOption(QMdiArea::DontMaximizeSubWindowOnActivation);
  d_workspace->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
  d_workspace->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
  setCentralWidget(d_workspace);

  setAcceptDrops(true);

  hiddenWindows = new QList<QWidget*>();

  scriptingWindow = NULL;
  d_text_editor = NULL;

  // List of registered PyQt interfaces
  QString pyqt_interfaces_as_str = QString::fromStdString(Mantid::Kernel::ConfigService::Instance().getString("mantidqt.python_interfaces"));
  pyqt_interfaces = QStringList::split(" ", pyqt_interfaces_as_str);

  renamedTables = QStringList();
  if (!factorySettings)
    readSettings();
  createLanguagesList();
  insertTranslatedStrings();
  disableToolbars();

//  assistant = new QAssistantClient( QString(), this );

  actionNextWindow = new QAction(QIcon(getQPixmap("next_xpm")), tr("&Next","next window"), this);
  actionNextWindow->setShortcut( tr("F5","next window shortcut") );
  connect(actionNextWindow, SIGNAL(activated()), d_workspace, SLOT(activateNextSubWindow()));

  actionPrevWindow = new QAction(QIcon(getQPixmap("prev_xpm")), tr("&Previous","previous window"), this);
  actionPrevWindow->setShortcut( tr("F6","previous window shortcut") );
  connect(actionPrevWindow, SIGNAL(activated()), d_workspace, SLOT(activatePreviousSubWindow()));

  connect(tablesDepend, SIGNAL(activated(int)), this, SLOT(showTable(int)));

  connect(this, SIGNAL(modified()),this, SLOT(modifiedProject()));
  connect(d_workspace, SIGNAL(subWindowActivated(QMdiSubWindow *)),
      this, SLOT(windowActivated(QMdiSubWindow*)));
  connect(lv, SIGNAL(doubleClicked(Q3ListViewItem *)),
      this, SLOT(activateWindow(Q3ListViewItem *)));
  connect(lv, SIGNAL(doubleClicked(Q3ListViewItem *)),
      this, SLOT(folderItemDoubleClicked(Q3ListViewItem *)));
  connect(lv, SIGNAL(contextMenuRequested(Q3ListViewItem *, const QPoint &, int)),
      this, SLOT(showWindowPopupMenu(Q3ListViewItem *, const QPoint &, int)));
  connect(lv, SIGNAL(dragItems(QList<Q3ListViewItem *>)),
      this, SLOT(dragFolderItems(QList<Q3ListViewItem *>)));
  connect(lv, SIGNAL(dropItems(Q3ListViewItem *)),
      this, SLOT(dropFolderItems(Q3ListViewItem *)));
  connect(lv, SIGNAL(renameItem(Q3ListViewItem *)),
      this, SLOT(startRenameFolder(Q3ListViewItem *)));
  connect(lv, SIGNAL(addFolderItem()), this, SLOT(addFolder()));
  connect(lv, SIGNAL(deleteSelection()), this, SLOT(deleteSelectedItems()));
  connect(lv, SIGNAL(itemRenamed(Q3ListViewItem *, int, const QString &)),
      this, SLOT(renameWindow(Q3ListViewItem *, int, const QString &)));

  connect(recent, SIGNAL(activated(int)), this, SLOT(openRecentProject(int)));
  //connect(&http, SIGNAL(done(bool)), this, SLOT(receivedVersionFile(bool)));

  //apply user settings
  updateAppFonts();
  setAppColors(workspaceColor, panelsColor, panelsTextColor, true);

  //Scripting
  m_script_envs = QHash<QString, ScriptingEnv*>();
  setScriptingLanguage(defaultScriptingLang);
  m_scriptInterpreter = new CommandLineInterpreter(*scriptingEnv(), m_interpreterDock);
  delete m_interpreterDock->widget();
  m_interpreterDock->setWidget(m_scriptInterpreter);
  m_iface_script = NULL;
  loadCustomActions();

  // Print a warning message if the scripting language is set to muParser
  if (defaultScriptingLang == "muParser")
  {
    logWindow->show();
    results->setTextColor(Qt::blue);
    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);
  }

  actionIPythonConsole->setVisible(testForIPython());

  // Need to show first time setup dialog?
  Mantid::Kernel::ConfigServiceImpl& config = Mantid::Kernel::ConfigService::Instance();
  std::string facility = config.getString("default.facility");
  std::string instrument = config.getString("default.instrument");
  if ( facility.empty() || instrument.empty() )
  {
    showFirstTimeSetup();
  }
  using namespace Mantid::API;
  // Do this as late as possible to avoid unnecessary updates
  AlgorithmFactory::Instance().enableNotifications();
  AlgorithmFactory::Instance().notificationCenter.postNotification(new AlgorithmFactoryUpdateNotification);

/*
Function tries to set the paraview path.

Owen Arnold's avatar
Owen Arnold committed
This is a windows only feature. the PATH enviromental variable can be set at runtime on windows.

- Abort if Vates libraries do not seem to be present.
- Othwerise, if the paraview.path is already in the properties file, use it.
- Otherwise, if the user is not using executeandquit command arguments launch the Setup gui.

@param commandArguments : all command line arguments.
*/
void ApplicationWindow::trySetParaviewPath(const QStringList& commandArguments)
{
Owen Arnold's avatar
Owen Arnold committed
#ifdef _WIN32
  if(this->hasVatesAvailable())//TODO: This condition is redundant since Vates will be shipped by default.
    Mantid::Kernel::ConfigServiceImpl& confService = Mantid::Kernel::ConfigService::Instance();
    //Early check of execute and quit command arguments used by system tests.
    QString str;
    bool b_skipDialog = false;
Owen Arnold's avatar
Owen Arnold committed
    foreach(str, commandArguments)
    {
      if(this->shouldExecuteAndQuit(str))
      {
        b_skipDialog = true;
        break;
      }
    }

    //ONLY If skipping is not already selected
    if(!b_skipDialog)
    {
      //If the ignore property exists and is set to true, then skip the dialog.
      const std::string paraviewIgnoreProperty = "paraview.ignore";
      b_skipDialog = confService.hasProperty(paraviewIgnoreProperty) && QString(confService.getString(paraviewIgnoreProperty).c_str()).toInt() > 0;
    if(this->hasParaviewPath())
    {
      //Already have a path in the properties file, just apply it.
      std::string path = confService.getString("paraview.path");
      confService.setParaviewLibraryPath(path);
    }
    else
    {
      //Only run the following if skipping is not implied.
      if(!b_skipDialog)
      {
        //Launch the dialog to set the PV path.
        SetUpParaview pv(SetUpParaview::FirstLaunch);
Owen Arnold's avatar
Owen Arnold committed
#else
  UNUSED_ARG(commandArguments)
#endif
/*
Getter to determine if the vates paraview plugins are available.

The code below may be used before MantidUI::init is called and therefore the implementation
must not rely on the Vates Libraries to be loaded in order to determine whether Vates is available.

@return TRUE if vates is available
*/
bool ApplicationWindow::hasVatesAvailable() const
{
  return Mantid::Kernel::ConfigService::Instance().quickVatesCheck();
}

/*
Getter to determine if the paraview path has been set.
*/
bool ApplicationWindow::hasParaviewPath() const
{
  Mantid::Kernel::ConfigServiceImpl& config = Mantid::Kernel::ConfigService::Instance();
  return config.hasProperty("paraview.path");
}

void ApplicationWindow::showLogWindowContextMenu(const QPoint & p)
{
  (void)p; //Avoid compiler warning
  QMenu *menu = results->createStandardContextMenu();
  if(!menu) return;
  if(results->text().isEmpty())
  {
    actionClearLogInfo->setEnabled(false);
  }
  else
  {
    actionClearLogInfo->setEnabled(true);
  }

  menu->addAction(actionClearLogInfo);
  //Mantid log level changes
  QMenu *logLevelMenu = menu->addMenu("&Log Level");
  logLevelMenu->addAction(actionLogLevelError);
  logLevelMenu->addAction(actionLogLevelWarning);
  logLevelMenu->addAction(actionLogLevelNotice);
  logLevelMenu->addAction(actionLogLevelInformation);
  logLevelMenu->addAction(actionLogLevelDebug);

  //check the right level
  int level = Mantid::Kernel::Logger::get("").getLevel(); //get the root logger logging level
  if (level == Poco::Message::PRIO_ERROR)
    actionLogLevelError->setChecked(true);
  if (level == Poco::Message::PRIO_WARNING)
    actionLogLevelWarning->setChecked(true);
  if (level == Poco::Message::PRIO_NOTICE)
    actionLogLevelNotice->setChecked(true);
  if (level == Poco::Message::PRIO_INFORMATION)
    actionLogLevelInformation->setChecked(true);
  if (level == Poco::Message::PRIO_DEBUG)
    actionLogLevelDebug->setChecked(true);

  //Mantid log level changes
  menu->popup(QCursor::pos());
}

void ApplicationWindow::setLogLevel(int level)
{
  //set the log level
  Mantid::Kernel::Logger::setLevelForAll(level);
}

void ApplicationWindow::showScriptConsoleContextMenu(const QPoint &p)
{
  (void)p;
  QMenu *menu = results->createStandardContextMenu();
  menu->addAction(actionClearConsole);
  menu->popup(QCursor::pos());
}

void ApplicationWindow::initWindow()
{
  switch(d_init_window_type){
  case TableWindow:
    newTable();
    break;
  case MatrixWindow:
    newMatrix();
    break;
  case MultiLayerWindow:
    newGraph();
    break;
  case NoteWindow:
    newNote();
    break;
  default:
    break;
  }
}

void ApplicationWindow::initGlobalConstants()
{
  d_auto_update_table_values = true;
  d_active_window = NULL;
  d_matrix_undo_stack_size = 10;

  d_opening_file = false;
  d_in_place_editing = true;

  d_matrix_tool_bar = true;
  d_file_tool_bar = true;
  d_table_tool_bar = true;
  d_column_tool_bar = true;
  d_edit_tool_bar = true;
  d_plot_tool_bar = true;
  d_plot3D_tool_bar = true;
  d_display_tool_bar = false;
  d_format_tool_bar = true;

  appStyle = qApp->style()->objectName();
  d_app_rect = QRect();
  projectname = "untitled";
  lastCopiedLayer = 0;
  d_text_copy = NULL;
  d_arrow_copy = NULL;
  d_image_copy = NULL;

  savingTimerId = 0;

  autoSearchUpdatesRequest = false;

  show_windows_policy = ActiveFolder;
  d_init_window_type = NoWindow;

  QString aux = qApp->applicationDirPath();
  workingDir = aux;

  d_translations_folder = aux + "/translations";
  helpFilePath = aux + "/manual/index.html";
  d_python_config_folder = aux;

  fitPluginsPath = aux + "fitPlugins";
  fitModelsPath = QString::null;
  templatesDir = aux;
  asciiDirPath = aux;
  imagesDirPath = aux;
  scriptsDirPath = aux;
  customActionsDirPath = QString::null;

  appFont = QFont();
  QString family = appFont.family();
  int pointSize = appFont.pointSize();
  tableTextFont = appFont;
  tableHeaderFont = appFont;
  plotAxesFont = QFont(family, pointSize, QFont::Bold, false);
  plotNumbersFont = QFont(family, pointSize );
  plotLegendFont = appFont;
  plotTitleFont = QFont(family, pointSize + 2, QFont::Bold,false);

  plot3DAxesFont = QFont(family, pointSize, QFont::Bold, false );
  plot3DNumbersFont = QFont(family, pointSize);
  plot3DTitleFont = QFont(family, pointSize + 2, QFont::Bold,false);

  autoSearchUpdates = false;
  appLanguage = QLocale::system().name().section('_',0,0);
  show_windows_policy = ApplicationWindow::ActiveFolder;

  workspaceColor = QColor("darkGray");
  panelsColor = QColor("#ffffff");
  panelsTextColor = QColor("#000000");
  tableBkgdColor = QColor("#ffffff");
  tableTextColor = QColor("#000000");
  tableHeaderColor = QColor("#000000");

  plot3DColors = QStringList();
  plot3DColors << "blue";
  plot3DColors << "#000000";
  plot3DColors << "#000000";
  plot3DColors << "#000000";
  plot3DColors << "red";
  plot3DColors << "#000000";
  plot3DColors << "#000000";
  plot3DColors << "#ffffff";

  d_graph_tick_labels_dist = 4;
  d_graph_axes_labels_dist = 2;

  autoSave = false;
  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)
    d_locale.setNumberOptions(QLocale::OmitGroupSeparator);

  d_decimal_digits = 13;

  d_extended_open_dialog = true;
  d_extended_export_dialog = true;
  d_extended_import_ASCII_dialog = true;
  d_extended_plot_dialog = true;

  d_add_curves_dialog_size = QSize(700, 400);
  d_show_current_folder = false;

  confirmCloseFolder = false;
  confirmCloseTable = false;
  confirmCloseMatrix = false;
  confirmClosePlot2D = false;
  confirmClosePlot3D = false;
  confirmCloseNotes = false;
  d_inform_rename_table = false;
  confirmCloseInstrWindow=false;

  d_show_table_comments = false;

  titleOn = true;
  d_show_axes = QVector<bool> (QwtPlot::axisCnt, true);
  // 'Factory' default is to not show top & right axes
  d_show_axes[1] = false;
  d_show_axes[3] = false;
  d_show_axes_labels = QVector<bool> (QwtPlot::axisCnt, true);
  canvasFrameWidth = 0;
  defaultPlotMargin = 0;
  drawBackbones = true;

  //these settings are overridden, but the default axes scales are linear
  d_axes_scales = QVector<QString> (QwtPlot::axisCnt, "linear");

  axesLineWidth = 1;
  autoscale2DPlots = true;
  autoScaleFonts = true;
  autoResizeLayers = true;
  antialiasing2DPlots = false; //Mantid
  fixedAspectRatio2DPlots = false; //Mantid
  d_scale_plots_on_print = false;
  d_print_cropmarks = false;
  d_synchronize_graph_scales = true;

  defaultCurveStyle = static_cast<int>(Graph::Line);
  defaultCurveLineWidth = 1;
  defaultSymbolSize = 7;

  majTicksStyle = static_cast<int>(ScaleDraw::Out);
  minTicksStyle = static_cast<int>(ScaleDraw::Out);
  minTicksLength = 5;
  majTicksLength = 9;

  legendFrameStyle = static_cast<int>(LegendWidget::Line);
  legendTextColor = Qt::black;
  legendBackground = Qt::white;
  legendBackground.setAlpha(0); // transparent by default;

  defaultArrowLineWidth = 1;
  defaultArrowColor = Qt::black;
  defaultArrowHeadLength = 4;
  defaultArrowHeadAngle = 45;
  defaultArrowHeadFill = true;
  defaultArrowLineStyle = Graph::getPenStyle("SolidLine");

  showPlot3DLegend = true;
  showPlot3DProjection = false;
  smooth3DMesh = false;
  plot3DResolution = 1;
  orthogonal3DPlots = false;
  autoscale3DPlots = true;

  fit_output_precision = 13;
  pasteFitResultsToPlot = false;
  writeFitResultsToLog = true;
  generateUniformFitPoints = true;
  fitPoints = 100;
  generatePeakCurves = true;
  peakCurvesColor = 2;
  fit_scale_errors = true;
  d_2_linear_fit_points = true;

  columnSeparator = "\t";
  ignoredLines = 0;
  renameColumns = true;
  strip_spaces = false;
  simplify_spaces = false;
  d_ASCII_file_filter = "*";
  d_ASCII_import_locale = QLocale::system().name();
  d_import_dec_separators = true;
  d_ASCII_import_mode = static_cast<int>(ImportASCIIDialog::NewTables);
  d_ASCII_comment_string = "#";
  d_ASCII_import_comments = false;
  d_ASCII_import_read_only = false;
  d_ASCII_import_preview = true;
  d_preview_lines = 100;
  d_ASCII_end_line = LF;
  d_eol = LF;
#ifdef Q_OS_MAC
  d_ASCII_end_line = CR;
  d_eol = CR;
#endif

  d_export_col_separator = "\t";
  d_export_col_names = false;
  d_export_col_comment = false;
  d_export_table_selection = false;

  d_image_export_filter = ".png";
  d_export_transparency = false;
  d_export_quality = 100;

  // MG: On Linux, if cups defines a printer queue that cannot be contact, the
  // QPrinter constructor hangs and doesn't timeout.

  //	QPrinterInfo::availablePrinters();


  //	d_export_resolution = QPrinter().resolution();
  d_export_color = true;
  d_export_vector_size = static_cast<int>(QPrinter::Custom);
  d_keep_plot_aspect = true;
}

QMenuBar* ApplicationWindow::myMenuBar()
{
#ifdef SHARED_MENUBAR
  return m_sharedMenuBar == NULL ? menuBar() : m_sharedMenuBar;
#else
  return menuBar();
#endif
}

void ApplicationWindow::initToolBars()
{
  initPlot3DToolBar();

  //	setWindowIcon(QIcon(getQPixmap("logo_xpm")));
  setWindowIcon(QIcon(":/MantidPlot_Icon_32offset.png"));
  QPixmap openIcon, saveIcon;

  fileTools = new QToolBar(tr( "File" ), this);
  fileTools->setObjectName("fileTools"); // this is needed for QMainWindow::restoreState()
  fileTools->setIconSize( QSize(18,20) );
  addToolBar( Qt::TopToolBarArea, fileTools );

  fileTools->addAction(actionNewProject);
  fileTools->addAction(actionNewFolder);
  fileTools->addAction(actionNewTable);
  fileTools->addAction(actionNewMatrix);
  fileTools->addAction(actionNewNote);
  fileTools->addAction(actionNewGraph);
  fileTools->addAction(actionNewFunctionPlot);
  fileTools->addAction(actionNewSurfacePlot);
  fileTools->addSeparator ();
  fileTools->addAction(actionManageDirs);
  fileTools->addSeparator ();
  
  fileTools->addAction(actionOpenProj);
  fileTools->addAction(actionLoadFile);

  fileTools->addAction(actionSaveProject);
  fileTools->addSeparator ();
  fileTools->addAction(actionLoad);
  fileTools->addSeparator ();
  fileTools->addAction(actionCopyWindow);
  fileTools->addAction(actionPrint);
  fileTools->addAction(actionExportPDF);
  fileTools->addSeparator();
  fileTools->addAction(actionShowExplorer);
  fileTools->addAction(actionShowLog);
#ifdef SCRIPTING_PYTHON
  fileTools->addAction(actionShowScriptWindow);
#endif

  editTools = new QToolBar(tr("Edit"), this);
  editTools->setObjectName("editTools"); // this is needed for QMainWindow::restoreState()
  editTools->setIconSize( QSize(18,20) );
  addToolBar( editTools );

  editTools->addAction(actionUndo);
  editTools->addAction(actionRedo);
  editTools->addAction(actionCutSelection);
  editTools->addAction(actionCopySelection);
  editTools->addAction(actionPasteSelection);
  editTools->addAction(actionClearSelection);

  plotTools = new QToolBar(tr("Plot"), this);
  plotTools->setObjectName("plotTools"); // this is needed for QMainWindow::restoreState()
  plotTools->setIconSize( QSize(16,20) );
  addToolBar( plotTools );

  plotTools->addAction(actionAddLayer);
  plotTools->addAction(actionShowLayerDialog);
  plotTools->addAction(actionAutomaticLayout);
  plotTools->addSeparator();
  plotTools->addAction(actionAddErrorBars);
  plotTools->addAction(actionRemoveErrorBars);
  plotTools->addAction(actionShowCurvesDialog);
  plotTools->addAction(actionAddFunctionCurve);
  plotTools->addAction(actionNewLegend);
  plotTools->addSeparator ();
  plotTools->addAction(actionUnzoom);

  dataTools = new QActionGroup( this );
  dataTools->setExclusive( true );

  btnPointer = new QAction(tr("Disable &Tools"), this);
  btnPointer->setActionGroup(dataTools);
  btnPointer->setCheckable( true );
  btnPointer->setIcon(QIcon(getQPixmap("pointer_xpm")) );
  btnPointer->setChecked(true);
  plotTools->addAction(btnPointer);


  actionMagnify->setActionGroup(dataTools);
  actionMagnify->setCheckable( true );
  plotTools->addAction(actionMagnify);

  btnZoomIn = new QAction(tr("&Zoom In"), this);
  btnZoomIn->setShortcut( tr("Ctrl++") );
  btnZoomIn->setActionGroup(dataTools);
  btnZoomIn->setCheckable( true );
  btnZoomIn->setIcon(QIcon(getQPixmap("zoom_xpm")) );
  plotTools->addAction(btnZoomIn);

  btnZoomOut = new QAction(tr("&Zoom Out"), this);
  btnZoomOut->setShortcut( tr("Ctrl+-") );
  btnZoomOut->setActionGroup(dataTools);
  btnZoomOut->setCheckable( true );
  btnZoomOut->setIcon(QIcon(getQPixmap("zoomOut_xpm")) );
  plotTools->addAction(btnZoomOut);


  btnCursor = new QAction(tr("&Data Reader"), this);
  btnCursor->setShortcut( tr("CTRL+D") );
  btnCursor->setActionGroup(dataTools);
  btnCursor->setCheckable( true );
  btnCursor->setIcon(QIcon(getQPixmap("select_xpm")) );
  //plotTools->addAction(btnCursor); disabled until fixed (#2783)

  btnSelect = new QAction(tr("&Select Data Range"), this);
  btnSelect->setShortcut( tr("ALT+S") );
  btnSelect->setActionGroup(dataTools);
  btnSelect->setCheckable( true );
  btnSelect->setIcon(QIcon(getQPixmap("cursors_xpm")) );
  //plotTools->addAction(btnSelect); disabled until fixed (#2783)

  btnPicker = new QAction(tr("S&creen Reader"), this);
  btnPicker->setActionGroup(dataTools);
  btnPicker->setCheckable( true );
  btnPicker->setIcon(QIcon(getQPixmap("cursor_16_xpm")) );
  plotTools->addAction(btnPicker); //disabled until fixed (#2783)

  actionDrawPoints = new QAction(tr("&Draw Data Points"), this);
  actionDrawPoints->setActionGroup(dataTools);
  actionDrawPoints->setCheckable( true );
  actionDrawPoints->setIcon(QIcon(getQPixmap("draw_points_xpm")) );