Skip to content

Commit

Permalink
Use native save dialogs for OSX running 10.6 package on 10.8. Re #6497
Browse files Browse the repository at this point in the history
This changes all uses of getSaveFileName to a custom handler that we
already had so that we can switch to Qt dialogs where necessary.
  • Loading branch information
martyngigg committed Jan 30, 2013
1 parent 829c633 commit fa10e39
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 28 deletions.
13 changes: 7 additions & 6 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.cpp
Expand Up @@ -184,6 +184,7 @@
#include "Mantid/FirstTimeSetup.h"
#include "Mantid/SetUpParaview.h"

#include "MantidQtAPI/FileDialogHandler.h"
#include "MantidQtAPI/InterfaceManager.h"
#include "MantidQtAPI/UserSubWindow.h"
#include "MantidQtAPI/AlgorithmInputHistory.h"
Expand Down Expand Up @@ -6060,7 +6061,7 @@ void ApplicationWindow::savetoNexusFile()
if(fileDir.isEmpty())
{fileDir="C\\Mantid\\Test\\Nexus";
}
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File As"), fileDir, filter, &selectedFilter);
QString fileName = MantidQt::API::FileDialogHandler::getSaveFileName(this, tr("Save File As"), fileDir, filter, &selectedFilter);
if ( !fileName.isEmpty() ){
std::string wsName;
MdiSubWindow *w = activeWindow();
Expand Down Expand Up @@ -6119,7 +6120,7 @@ void ApplicationWindow::saveProjectAs(const QString& fileName, bool compress)
filter += tr("Compressed MantidPlot project")+" (*.mantid.gz)";

QString selectedFilter;
fn = QFileDialog::getSaveFileName(this, tr("Save Project As"), workingDir, filter, &selectedFilter);
fn = MantidQt::API::FileDialogHandler::getSaveFileName(this, tr("Save Project As"), workingDir, filter, &selectedFilter);
if (selectedFilter.contains(".gz"))
compress = true;
}
Expand Down Expand Up @@ -6199,7 +6200,7 @@ void ApplicationWindow::saveAsTemplate(MdiSubWindow* w, const QString& fileName)
filter = tr("MantidPlot 3D Surface Template")+" (*.qst)";

QString selectedFilter;
fn = QFileDialog::getSaveFileName(this, tr("Save Window As Template"), templatesDir + "/" + w->objectName(), filter, &selectedFilter);
fn = MantidQt::API::FileDialogHandler::getSaveFileName(this, tr("Save Window As Template"), templatesDir + "/" + w->objectName(), filter, &selectedFilter);

if (!fn.isEmpty()){
QFileInfo fi(fn);
Expand Down Expand Up @@ -6528,7 +6529,7 @@ void ApplicationWindow::exportASCII(const QString& tableName, const QString& sep
return;

QString selectedFilter;
QString fname = QFileDialog::getSaveFileName(this, tr("Choose a filename to save under"),
QString fname = MantidQt::API::FileDialogHandler::getSaveFileName(this, tr("Choose a filename to save under"),
asciiDirPath + "/" + w->objectName(), "*.txt;;*.dat;;*.DAT", &selectedFilter);
if (!fname.isEmpty() ){
QFileInfo fi(fname);
Expand Down Expand Up @@ -7567,7 +7568,7 @@ void ApplicationWindow::exportPDF()
return;
}

QString fname = QFileDialog::getSaveFileName(this, tr("Choose a filename to save under"), workingDir, "*.pdf");
QString fname = MantidQt::API::FileDialogHandler::getSaveFileName(this, tr("Choose a filename to save under"), workingDir, "*.pdf");
if (!fname.isEmpty() ){
QFileInfo fi(fname);
QString baseName = fi.fileName();
Expand Down Expand Up @@ -15220,7 +15221,7 @@ void ApplicationWindow::saveFolderAsProject(Folder *f)
filter += tr("Compressed MantidPlot project")+" (*.qti.gz)";

QString selectedFilter;
QString fn = QFileDialog::getSaveFileName(this, tr("Save project as"), workingDir, filter, &selectedFilter);
QString fn = MantidQt::API::FileDialogHandler::getSaveFileName(this, tr("Save project as"), workingDir, filter, &selectedFilter);
if ( !fn.isEmpty() ){
QFileInfo fi(fn);
workingDir = fi.dirPath(true);
Expand Down
7 changes: 5 additions & 2 deletions Code/Mantid/MantidPlot/src/FitDialog.cpp
Expand Up @@ -64,6 +64,9 @@

#include <qwt_plot_curve.h>

#include "MantidQtAPI/FileDialogHandler.h"


FitDialog::FitDialog(Graph *g, QWidget* parent, Qt::WFlags fl )
: QDialog( parent, fl )
{
Expand Down Expand Up @@ -621,7 +624,7 @@ void FitDialog::saveUserFunction()
ApplicationWindow *app = dynamic_cast<ApplicationWindow *>(this->parent());
QString filter = tr("MantidPlot fit model")+" (*.fit);;";
filter += tr("All files")+" (*.*)";
QString fn = QFileDialog::getSaveFileName(app, tr("MantidPlot") + " - " + tr("Save Fit Model As"),
QString fn = MantidQt::API::FileDialogHandler::getSaveFileName(app, tr("MantidPlot") + " - " + tr("Save Fit Model As"),
app->fitModelsPath + "/" + name, filter);
if (!fn.isEmpty()){
QFileInfo fi(fn);
Expand Down Expand Up @@ -1359,7 +1362,7 @@ void FitDialog::saveInitialGuesses()
ApplicationWindow *app = dynamic_cast<ApplicationWindow *>(this->parent());
QString filter = tr("MantidPlot fit model") + " (*.fit);;";
filter += tr("All files") + " (*.*)";
QString fn = QFileDialog::getSaveFileName(app, tr("MantidPlot") + " - " + tr("Save Fit Model As"),
QString fn = MantidQt::API::FileDialogHandler::getSaveFileName(app, tr("MantidPlot") + " - " + tr("Save Fit Model As"),
app->fitModelsPath + "/" + d_current_fit->objectName(), filter);
if (!fn.isEmpty()){
QFileInfo fi(fn);
Expand Down
3 changes: 2 additions & 1 deletion Code/Mantid/MantidPlot/src/Mantid/AlgorithmHistoryWindow.cpp
Expand Up @@ -2,6 +2,7 @@
#include "MantidAPI/AlgorithmManager.h"

#include "MantidQtAPI/AlgorithmInputHistory.h"
#include "MantidQtAPI/FileDialogHandler.h"

#include <QLineEdit>
#include <QLabel>
Expand Down Expand Up @@ -300,7 +301,7 @@ void AlgorithmHistoryWindow::writeToScriptFile()
{
scriptDir = prevDir;
}
QString filePath = QFileDialog::getSaveFileName(this,tr("Save Script As "),scriptDir,tr("Script files (*.py)"));
QString filePath = MantidQt::API::FileDialogHandler::getSaveFileName(this,tr("Save Script As "),scriptDir,tr("Script files (*.py)"));
// An empty string indicates they clicked cancel
if( filePath.isEmpty() ) return;

Expand Down
Expand Up @@ -42,6 +42,8 @@
#include <QStackedLayout>
#include <QKeyEvent>

#include "MantidQtAPI/FileDialogHandler.h"

#include <numeric>
#include <fstream>
#include <stdexcept>
Expand Down Expand Up @@ -679,7 +681,7 @@ void InstrumentWindow::saveImage()
}
}
QString selectedFilter = "*.png";
QString filename = QFileDialog::getSaveFileName(this, "Save image ...", m_savedialog_dir, filter, &selectedFilter);
QString filename = MantidQt::API::FileDialogHandler::getSaveFileName(this, "Save image ...", m_savedialog_dir, filter, &selectedFilter);

// If its empty, they cancelled the dialog
if( filename.isEmpty() ) return;
Expand Down Expand Up @@ -1073,7 +1075,7 @@ void InstrumentWindow::sumDetsToWorkspace()

void InstrumentWindow::createIncludeGroupingFile()
{
QString fname = QFileDialog::getSaveFileName(this,"Save grouping file");
QString fname = MantidQt::API::FileDialogHandler::getSaveFileName(this,"Save grouping file");
if (!fname.isEmpty())
{
DetXMLFile mapFile(m_selectedDetectors,DetXMLFile::Sum,fname);
Expand All @@ -1083,7 +1085,7 @@ void InstrumentWindow::createIncludeGroupingFile()

void InstrumentWindow::createExcludeGroupingFile()
{
QString fname = QFileDialog::getSaveFileName(this,"Save grouping file");
QString fname = MantidQt::API::FileDialogHandler::getSaveFileName(this,"Save grouping file");
if (!fname.isEmpty())
{
DetXMLFile mapFile(m_instrumentActor->getAllDetIDs(),m_selectedDetectors,fname);
Expand Down
Expand Up @@ -43,6 +43,8 @@
#include <QApplication>
#include <QFileDialog>

#include "MantidQtAPI/FileDialogHandler.h"

#include <numeric>
#include <cfloat>
#include <algorithm>
Expand Down Expand Up @@ -490,7 +492,7 @@ void InstrumentWindowMaskTab::saveMaskingToFile(bool invertMask)
{
clearShapes();
QString saveDir = QString::fromStdString(Mantid::Kernel::ConfigService::Instance().getString("defaultsave.directory"));
QString fileName = QFileDialog::getSaveFileName(m_instrWindow,"Select location and name for the mask file",saveDir,"XML files (*.xml)");
QString fileName = MantidQt::API::FileDialogHandler::getSaveFileName(m_instrWindow,"Select location and name for the mask file",saveDir,"XML files (*.xml)");

if (!fileName.isEmpty())
{
Expand Down
3 changes: 2 additions & 1 deletion Code/Mantid/MantidPlot/src/Note.cpp
Expand Up @@ -36,6 +36,7 @@
#include <Qsci/qsciprinter.h>
#include <QPrintDialog>
#include "MantidKernel/ConfigService.h"
#include "MantidQtAPI/FileDialogHandler.h"

Note::Note(const QString& label, ApplicationWindow* parent, const QString& name, Qt::WFlags f)
: MdiSubWindow(parent, label, name, f)
Expand Down Expand Up @@ -124,7 +125,7 @@ QString Note::exportASCII(const QString &filename)
if (filename.isEmpty())
{
QString dir(Mantid::Kernel::ConfigService::Instance().getString("defaultsave.directory").c_str());
fn = QFileDialog::getSaveFileName(this, tr("Save Text to File"),dir,filter, &selectedFilter);
fn = MantidQt::API::FileDialogHandler::getSaveFileName(this, tr("Save Text to File"),dir,filter, &selectedFilter);
}
else
fn = filename;
Expand Down
4 changes: 3 additions & 1 deletion Code/Mantid/MantidPlot/src/TextFileIO.cpp
Expand Up @@ -7,6 +7,8 @@
#include <QTextStream>
#include <QApplication>

#include "MantidQtAPI/FileDialogHandler.h"

/**
* Construct an object with a list of file filters
*/
Expand Down Expand Up @@ -50,7 +52,7 @@ QString TextFileIO::askWhereToSave() const
{
QString selectedFilter;
QString filter = m_filters.join(";;");
QString filename = QFileDialog::getSaveFileName(NULL, "MantidPlot - Save", "",filter, &selectedFilter);
QString filename = MantidQt::API::FileDialogHandler::getSaveFileName(NULL, "MantidPlot - Save", "",filter, &selectedFilter);
if( filename.isEmpty() ) return QString();
if( QFileInfo(filename).suffix().isEmpty() )
{
Expand Down
29 changes: 25 additions & 4 deletions Code/Mantid/MantidQt/API/inc/MantidQtAPI/FileDialogHandler.h
Expand Up @@ -2,6 +2,11 @@
#define MANTIDQT_API_FILEDIALOGHANDLER_H_

#include <QFileDialog>
#ifdef Q_OS_DARWIN
#include <errno.h>
#include <sys/sysctl.h>
#endif


namespace MantidQt
{
Expand Down Expand Up @@ -34,8 +39,9 @@ namespace API
*/
struct FileDialogHandler
{
/** The MacOS's native save file browse button hangs so this function, which takes
* the same arguments as the Qt function, ensures a nonnative object is used on the Mac
/** The MacOS's native save dialog crashes when running a 10.6 package on 10.8 so this function, which takes
* the same arguments as the Qt function, ensures a nonnative object is used on the Mac when necessary.
* If compiled on 10.8 the native will be used
* @param parent :: the dialog will be shown centered over this parent widget
* @param caption :: The dialog's caption
* @param dir :: The file dialog's working directory will be set to dir. If dir includes a file name, the file will be selected
Expand All @@ -45,11 +51,26 @@ struct FileDialogHandler
*/
static QString getSaveFileName(QWidget * parent = 0, const QString & caption = QString(), const QString & dir = QString(), const QString & filter = QString(), QString * selectedFilter = 0, QFileDialog::Options options = 0)
{
#ifdef Q_OS_DARWIN
options = options | QFileDialog::DontUseNativeDialog;
#if defined(Q_OS_DARWIN) && defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
// If we are compiling on Snow Leopard
#if __MAC_OS_X_VERSION_MAX_ALLOWED == 1060
static int runningMountainLion(-1);
if(runningMountainLion < 0)
{
// Check if we are running Mountain Lion
// osrelease 12.x.x is OS X 10.8: http://stackoverflow.com/questions/11072804/mac-os-x-10-8-replacement-for-gestalt-for-testing-os-version-at-runtime
char str[256];
size_t size = sizeof(str);
sysctlbyname("kern.osrelease", str, &size, NULL, 0);
if(str[0] == '1' && str[1] == '2' ) runningMountainLion = 1;
else runningMountainLion = 0;
}
if(runningMountainLion) options = options | QFileDialog::DontUseNativeDialog;
#endif
#endif
return QFileDialog::getSaveFileName(parent, caption, dir, filter, selectedFilter, options);
}

};

}
Expand Down
4 changes: 2 additions & 2 deletions Code/Mantid/MantidQt/API/src/FilePropertyWidget.cpp
Expand Up @@ -2,7 +2,7 @@
#include "MantidKernel/System.h"
#include "MantidKernel/Property.h"
#include "MantidQtAPI/AlgorithmInputHistory.h"
#include <qfiledialog.h>
#include "MantidQtAPI/FileDialogHandler.h"

using namespace Mantid::Kernel;
//using namespace Mantid::API;
Expand Down Expand Up @@ -162,7 +162,7 @@ namespace API
filter.chop(2);
// Prepend the default filter
QString selectedFilter;
filename = QFileDialog::getSaveFileName(NULL, "Save file", AlgorithmInputHistory::Instance().getPreviousDirectory(), filter, &selectedFilter);
filename = MantidQt::API::FileDialogHandler::getSaveFileName(NULL, "Save file", AlgorithmInputHistory::Instance().getPreviousDirectory(), filter, &selectedFilter);

//Check the filename and append the selected filter if necessary
if( QFileInfo(filename).completeSuffix().isEmpty() )
Expand Down
3 changes: 2 additions & 1 deletion Code/Mantid/MantidQt/API/src/UserSubWindow.cpp
Expand Up @@ -7,6 +7,7 @@
#undef QT_NO_KEYWORDS
#include "MantidQtAPI/UserSubWindow.h"
#include "MantidQtAPI/AlgorithmInputHistory.h"
#include "MantidQtAPI/FileDialogHandler.h"

#include <QIcon>
#include <QMessageBox>
Expand Down Expand Up @@ -200,7 +201,7 @@ QString UserSubWindow::openFileDialog(const bool save, const QStringList &exts)
QString filename;
if( save )
{
filename = QFileDialog::getSaveFileName(this, "Save file", AlgorithmInputHistory::Instance().getPreviousDirectory(), filter);
filename = MantidQt::API::FileDialogHandler::getSaveFileName(this, "Save file", AlgorithmInputHistory::Instance().getPreviousDirectory(), filter);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/MantidQt/CustomInterfaces/src/MuonAnalysis.cpp
Expand Up @@ -358,7 +358,7 @@ void MuonAnalysis::runSaveGroupButton()
QString filter;
filter.append("Files (*.xml *.XML)");
filter += ";;AllFiles (*.*)";
QString groupingFile = QFileDialog::getSaveFileName(this,
QString groupingFile = MantidQt::API::FileDialogHandler::getSaveFileName(this,
"Save Grouping file as", prevPath, filter);

// Add extension if the groupingFile specified doesn't have one. (Solving Linux problem).
Expand Down
Expand Up @@ -2175,7 +2175,7 @@ void SANSRunWindow::handleReduceButtonClick(const QString & typeStr)
QString csv_file(m_uiForm.csv_filename->text());
if( m_dirty_batch_grid )
{
QString selected_file = QFileDialog::getSaveFileName(this, "Save as CSV", m_last_dir);
QString selected_file = MantidQt::API::FileDialogHandler::getSaveFileName(this, "Save as CSV", m_last_dir);
csv_file = saveBatchGrid(selected_file);
}
py_code.prepend("import SANSBatchMode as batch\n");
Expand Down
Expand Up @@ -2,7 +2,7 @@
#include <Qsci/qscilexerpython.h>
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/AlgorithmManager.h"
#include <qfiledialog.h>
#include "MantidQtAPI/FileDialogHandler.h"
#include <iosfwd>
#include <fstream>
#include <QFileInfo>
Expand Down Expand Up @@ -87,7 +87,7 @@ void ProcessingAlgoWidget::saveSettings()
void ProcessingAlgoWidget::btnSaveClicked()
{
// Save to a .py file
QString fileselection = QFileDialog::getSaveFileName(this, "Save a Python Script",
QString fileselection = MantidQt::API::FileDialogHandler::getSaveFileName(this, "Save a Python Script",
QFileInfo(m_lastFile).absoluteFilePath(), "Python scripts (*.py);;All files (*)");
if (!fileselection.isEmpty())
{
Expand Down
3 changes: 2 additions & 1 deletion Code/Mantid/MantidQt/MantidWidgets/src/ScriptEditor.cpp
Expand Up @@ -3,6 +3,7 @@
//-----------------------------------------------
#include "MantidQtMantidWidgets/ScriptEditor.h"
#include "MantidQtMantidWidgets/FindReplaceDialog.h"
#include "MantidQtAPI/FileDialogHandler.h"

// Qt
#include <QApplication>
Expand Down Expand Up @@ -254,7 +255,7 @@ void ScriptEditor::saveAs()
{
QString selectedFilter;
QString filter = "Scripts (*.py *.PY);;All Files (*)";
QString filename = QFileDialog::getSaveFileName(NULL, "MantidPlot - Save", "",filter, &selectedFilter);
QString filename = MantidQt::API::FileDialogHandler::getSaveFileName(NULL, "MantidPlot - Save", "",filter, &selectedFilter);

if( filename.isEmpty() )
{
Expand Down
3 changes: 2 additions & 1 deletion Code/Mantid/MantidQt/SliceViewer/src/SliceViewer.cpp
Expand Up @@ -70,6 +70,7 @@
#include "MantidAPI/IAlgorithm.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidQtAPI/AlgorithmRunner.h"
#include "MantidQtAPI/FileDialogHandler.h"


using namespace Mantid;
Expand Down Expand Up @@ -1105,7 +1106,7 @@ void SliceViewer::saveImage(const QString & filename)
QString fileselection;
if (filename.isEmpty())
{
fileselection = QFileDialog::getSaveFileName(this, tr("Pick a file to which to save the image"),
fileselection = MantidQt::API::FileDialogHandler::getSaveFileName(this, tr("Pick a file to which to save the image"),
QFileInfo(m_lastSavedFile).absoluteFilePath(),
tr("PNG files(*.png *.png)"));
// User cancelled if filename is still empty
Expand Down

0 comments on commit fa10e39

Please sign in to comment.