2001-03-12 12:47:07 +00:00
|
|
|
/**
|
2002-03-11 22:47:41 +00:00
|
|
|
* \file qt2/FileDialog.C
|
2002-09-24 13:57:09 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2001-03-12 12:47:07 +00:00
|
|
|
*
|
|
|
|
* \author John Levon
|
2004-08-09 13:00:30 +00:00
|
|
|
* \author Jean-Marc Lasgouttes
|
2002-09-24 13:57:09 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2001-03-12 12:47:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2001-03-12 12:47:07 +00:00
|
|
|
#include "frontends/FileDialog.h"
|
2004-01-08 10:59:51 +00:00
|
|
|
|
2004-05-20 09:36:28 +00:00
|
|
|
#include "FileDialog_private.h"
|
|
|
|
#include "qt_helpers.h"
|
|
|
|
|
2004-01-08 10:59:51 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "gettext.h"
|
|
|
|
|
2004-11-25 11:08:32 +00:00
|
|
|
#include "support/filefilterlist.h"
|
2004-01-08 10:59:51 +00:00
|
|
|
|
2004-08-09 13:00:30 +00:00
|
|
|
/** when this is defined, the code will use
|
|
|
|
* QFileDialog::getOpenFileName and friends to create filedialogs.
|
|
|
|
* Effects:
|
|
|
|
* - the dialog does not use the quick directory buttons (Button
|
|
|
|
* parameters);
|
|
|
|
* - with Qt/Mac or Qt/Win, the dialogs native to the environment are used.
|
|
|
|
*
|
|
|
|
* Therefore there is a tradeoff in enabling or disabling this (JMarc)
|
|
|
|
*/
|
|
|
|
#ifdef Q_WS_MACX
|
|
|
|
#define USE_NATIVE_FILEDIALOG 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_NATIVE_FILEDIALOG
|
|
|
|
#include <qapplication.h>
|
|
|
|
#include "support/filetools.h"
|
|
|
|
using lyx::support::MakeAbsPath;
|
|
|
|
#endif
|
2004-01-08 10:59:51 +00:00
|
|
|
|
|
|
|
using lyx::support::FileFilterList;
|
2001-03-12 12:47:07 +00:00
|
|
|
|
|
|
|
using std::endl;
|
2004-01-08 10:59:51 +00:00
|
|
|
using std::string;
|
2001-03-12 12:47:07 +00:00
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2005-01-19 15:03:31 +00:00
|
|
|
class FileDialog::Private {
|
|
|
|
public:
|
2002-08-25 20:53:39 +00:00
|
|
|
Button b1;
|
|
|
|
Button b2;
|
|
|
|
};
|
2002-10-19 10:32:57 +00:00
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2004-01-08 10:59:51 +00:00
|
|
|
FileDialog::FileDialog(string const & t,
|
2002-10-20 01:48:28 +00:00
|
|
|
kb_action s, Button b1, Button b2)
|
2003-02-21 12:22:25 +00:00
|
|
|
: private_(new FileDialog::Private), title_(t), success_(s)
|
2001-03-12 12:47:07 +00:00
|
|
|
{
|
2002-08-25 20:53:39 +00:00
|
|
|
private_->b1 = b1;
|
|
|
|
private_->b2 = b2;
|
2001-03-12 12:47:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FileDialog::~FileDialog()
|
|
|
|
{
|
2002-08-25 20:53:39 +00:00
|
|
|
delete private_;
|
2001-03-12 12:47:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-08 10:59:51 +00:00
|
|
|
FileDialog::Result const FileDialog::save(string const & path,
|
|
|
|
FileFilterList const & filters,
|
|
|
|
string const & suggested)
|
2001-03-12 12:47:07 +00:00
|
|
|
{
|
2002-10-20 01:48:28 +00:00
|
|
|
lyxerr[Debug::GUI] << "Select with path \"" << path
|
2004-11-26 12:31:39 +00:00
|
|
|
<< "\", mask \"" << filters.as_string()
|
2004-08-09 13:00:30 +00:00
|
|
|
<< "\", suggested \"" << suggested << '"' << endl;
|
|
|
|
FileDialog::Result result;
|
|
|
|
result.first = FileDialog::Chosen;
|
2001-03-12 12:47:07 +00:00
|
|
|
|
2004-08-09 13:00:30 +00:00
|
|
|
#ifdef USE_NATIVE_FILEDIALOG
|
|
|
|
string const startsWith = MakeAbsPath(suggested, path);
|
|
|
|
result.second = fromqstr(
|
|
|
|
QFileDialog::getSaveFileName(toqstr(startsWith),
|
2004-11-26 12:31:39 +00:00
|
|
|
toqstr(filters.as_string()),
|
2004-08-09 13:00:30 +00:00
|
|
|
qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
|
|
|
|
title_.c_str()));
|
|
|
|
#else
|
|
|
|
LyXFileDialog dlg(path, filters, title_, private_->b1, private_->b2);
|
2002-09-16 15:41:12 +00:00
|
|
|
dlg.setMode(QFileDialog::AnyFile);
|
2002-10-19 10:32:57 +00:00
|
|
|
|
2001-03-12 12:47:07 +00:00
|
|
|
if (!suggested.empty())
|
2002-12-17 20:37:13 +00:00
|
|
|
dlg.setSelection(toqstr(suggested));
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2003-01-06 14:02:24 +00:00
|
|
|
lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
|
2002-11-17 08:32:09 +00:00
|
|
|
int res = dlg.exec();
|
|
|
|
lyxerr[Debug::GUI] << "result " << res << endl;
|
|
|
|
if (res == QDialog::Accepted)
|
2003-10-01 12:02:17 +00:00
|
|
|
result.second = fromqstr(dlg.selectedFile());
|
2002-11-17 08:32:09 +00:00
|
|
|
dlg.hide();
|
2004-08-09 13:00:30 +00:00
|
|
|
#endif
|
2002-11-17 08:32:09 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-11-27 10:30:28 +00:00
|
|
|
|
2004-01-08 10:59:51 +00:00
|
|
|
FileDialog::Result const FileDialog::open(string const & path,
|
|
|
|
FileFilterList const & filters,
|
|
|
|
string const & suggested)
|
2002-11-17 08:32:09 +00:00
|
|
|
{
|
|
|
|
lyxerr[Debug::GUI] << "Select with path \"" << path
|
2004-11-26 12:31:39 +00:00
|
|
|
<< "\", mask \"" << filters.as_string()
|
2004-08-09 13:00:30 +00:00
|
|
|
<< "\", suggested \"" << suggested << '"' << endl;
|
|
|
|
FileDialog::Result result;
|
|
|
|
result.first = FileDialog::Chosen;
|
|
|
|
|
|
|
|
#ifdef USE_NATIVE_FILEDIALOG
|
|
|
|
string const startsWith = MakeAbsPath(suggested, path);
|
|
|
|
result.second = fromqstr(
|
|
|
|
QFileDialog::getOpenFileName(toqstr(startsWith),
|
2004-11-26 12:31:39 +00:00
|
|
|
toqstr(filters.as_string()),
|
2004-08-09 13:00:30 +00:00
|
|
|
qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
|
|
|
|
title_.c_str()));
|
|
|
|
#else
|
|
|
|
LyXFileDialog dlg(path, filters, title_, private_->b1, private_->b2);
|
2002-11-17 08:32:09 +00:00
|
|
|
|
|
|
|
if (!suggested.empty())
|
2002-12-17 20:37:13 +00:00
|
|
|
dlg.setSelection(toqstr(suggested));
|
2002-10-19 10:32:57 +00:00
|
|
|
|
2003-01-06 14:02:24 +00:00
|
|
|
lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
|
2002-08-25 20:53:39 +00:00
|
|
|
int res = dlg.exec();
|
|
|
|
lyxerr[Debug::GUI] << "result " << res << endl;
|
|
|
|
if (res == QDialog::Accepted)
|
2003-10-01 12:02:17 +00:00
|
|
|
result.second = fromqstr(dlg.selectedFile());
|
2002-08-25 20:53:39 +00:00
|
|
|
dlg.hide();
|
2004-08-09 13:00:30 +00:00
|
|
|
#endif
|
2002-08-25 20:53:39 +00:00
|
|
|
return result;
|
2001-03-12 12:47:07 +00:00
|
|
|
}
|
2003-01-14 21:51:34 +00:00
|
|
|
|
|
|
|
|
2004-01-08 10:59:51 +00:00
|
|
|
FileDialog::Result const FileDialog::opendir(string const & path,
|
|
|
|
string const & suggested)
|
2003-01-14 21:51:34 +00:00
|
|
|
{
|
2004-08-09 13:00:30 +00:00
|
|
|
lyxerr[Debug::GUI] << "Select with path \"" << path
|
|
|
|
<< "\", suggested \"" << suggested << '"' << endl;
|
|
|
|
FileDialog::Result result;
|
|
|
|
result.first = FileDialog::Chosen;
|
|
|
|
|
|
|
|
#ifdef USE_NATIVE_FILEDIALOG
|
|
|
|
string const startsWith = MakeAbsPath(suggested, path);
|
|
|
|
result.second = fromqstr(
|
|
|
|
QFileDialog::getExistingDirectory(toqstr(startsWith),
|
|
|
|
qApp->focusWidget() ? qApp->focusWidget() : qApp->mainWidget(),
|
|
|
|
title_.c_str()));
|
|
|
|
#else
|
2004-01-08 10:59:51 +00:00
|
|
|
FileFilterList const filter(_("Directories"));
|
2003-01-14 21:51:34 +00:00
|
|
|
|
|
|
|
LyXFileDialog dlg(path, filter, title_, private_->b1, private_->b2);
|
|
|
|
|
|
|
|
dlg.setMode(QFileDialog::DirectoryOnly);
|
|
|
|
|
|
|
|
if (!suggested.empty())
|
|
|
|
dlg.setSelection(toqstr(suggested));
|
|
|
|
|
|
|
|
lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
|
|
|
|
int res = dlg.exec();
|
|
|
|
lyxerr[Debug::GUI] << "result " << res << endl;
|
|
|
|
if (res == QDialog::Accepted)
|
2003-10-01 12:02:17 +00:00
|
|
|
result.second = fromqstr(dlg.selectedFile());
|
2003-01-14 21:51:34 +00:00
|
|
|
dlg.hide();
|
2004-08-09 13:00:30 +00:00
|
|
|
#endif
|
2003-01-14 21:51:34 +00:00
|
|
|
return result;
|
2003-02-21 12:22:25 +00:00
|
|
|
}
|