2006-03-05 17:24:44 +00:00
|
|
|
/**
|
2006-04-24 13:25:50 +00:00
|
|
|
* \file qt4/FileDialog.C
|
2006-03-05 17:24:44 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author John Levon
|
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "frontends/FileDialog.h"
|
|
|
|
|
|
|
|
#include "FileDialog_private.h"
|
|
|
|
#include "qt_helpers.h"
|
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
#include "gettext.h"
|
|
|
|
|
|
|
|
#include "support/filefilterlist.h"
|
2006-12-05 19:51:50 +00:00
|
|
|
#include "support/os.h"
|
2006-03-05 17:24:44 +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)
|
|
|
|
*/
|
2006-12-30 10:30:02 +00:00
|
|
|
#ifdef Q_WS_MACX
|
2006-03-05 17:24:44 +00:00
|
|
|
#define USE_NATIVE_FILEDIALOG 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_NATIVE_FILEDIALOG
|
|
|
|
#include <qapplication.h>
|
|
|
|
#include "support/filetools.h"
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2006-04-12 13:53:01 +00:00
|
|
|
using lyx::support::makeAbsPath;
|
2006-03-05 17:24:44 +00:00
|
|
|
#endif
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
using support::FileFilterList;
|
2006-12-05 19:51:50 +00:00
|
|
|
using support::os::internal_path;
|
2006-03-05 17:24:44 +00:00
|
|
|
using std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
class FileDialog::Private {
|
|
|
|
public:
|
|
|
|
Button b1;
|
|
|
|
Button b2;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-10-09 16:33:20 +00:00
|
|
|
FileDialog::FileDialog(docstring const & t,
|
2006-03-05 17:24:44 +00:00
|
|
|
kb_action s, Button b1, Button b2)
|
|
|
|
: private_(new FileDialog::Private), title_(t), success_(s)
|
|
|
|
{
|
|
|
|
private_->b1 = b1;
|
|
|
|
private_->b2 = b2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FileDialog::~FileDialog()
|
|
|
|
{
|
|
|
|
delete private_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-09 16:33:20 +00:00
|
|
|
FileDialog::Result const FileDialog::save(docstring const & path,
|
2006-03-05 17:24:44 +00:00
|
|
|
FileFilterList const & filters,
|
2006-10-09 16:33:20 +00:00
|
|
|
docstring const & suggested)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-10-09 16:33:20 +00:00
|
|
|
lyxerr[Debug::GUI] << "Select with path \"" << lyx::to_utf8(path)
|
|
|
|
<< "\", mask \"" << lyx::to_utf8(filters.as_string())
|
|
|
|
<< "\", suggested \"" << lyx::to_utf8(suggested) << '"' << endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
FileDialog::Result result;
|
|
|
|
result.first = FileDialog::Chosen;
|
|
|
|
|
|
|
|
#ifdef USE_NATIVE_FILEDIALOG
|
2006-12-27 10:56:11 +00:00
|
|
|
docstring const startsWith = from_utf8(
|
|
|
|
makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
|
2006-12-05 19:51:50 +00:00
|
|
|
result.second = lyx::from_utf8(internal_path(fromqstr(
|
|
|
|
QFileDialog::getSaveFileName(qApp->focusWidget(),
|
|
|
|
toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ))));
|
2006-03-05 17:24:44 +00:00
|
|
|
#else
|
2006-05-07 10:20:43 +00:00
|
|
|
LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
|
|
|
|
dlg.setFileMode(QFileDialog::AnyFile);
|
|
|
|
dlg.setAcceptMode(QFileDialog::AcceptSave);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
if (!suggested.empty())
|
2006-05-07 10:20:43 +00:00
|
|
|
dlg.selectFile(toqstr(suggested));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
|
|
|
|
int res = dlg.exec();
|
|
|
|
lyxerr[Debug::GUI] << "result " << res << endl;
|
|
|
|
if (res == QDialog::Accepted)
|
2006-12-05 19:51:50 +00:00
|
|
|
result.second = lyx::from_utf8(internal_path(
|
|
|
|
fromqstr(dlg.selectedFiles()[0])));
|
2006-03-05 17:24:44 +00:00
|
|
|
dlg.hide();
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-09 16:33:20 +00:00
|
|
|
FileDialog::Result const FileDialog::open(docstring const & path,
|
2006-03-05 17:24:44 +00:00
|
|
|
FileFilterList const & filters,
|
2006-10-09 16:33:20 +00:00
|
|
|
docstring const & suggested)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-10-09 16:33:20 +00:00
|
|
|
lyxerr[Debug::GUI] << "Select with path \"" << lyx::to_utf8(path)
|
|
|
|
<< "\", mask \"" << lyx::to_utf8(filters.as_string())
|
|
|
|
<< "\", suggested \"" << lyx::to_utf8(suggested) << '"' << endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
FileDialog::Result result;
|
|
|
|
result.first = FileDialog::Chosen;
|
|
|
|
|
|
|
|
#ifdef USE_NATIVE_FILEDIALOG
|
2006-12-27 10:56:11 +00:00
|
|
|
docstring const startsWith = from_utf8(
|
|
|
|
makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
|
2006-12-05 19:51:50 +00:00
|
|
|
result.second = lyx::from_utf8(internal_path(fromqstr(
|
|
|
|
QFileDialog::getOpenFileName(qApp->focusWidget(),
|
|
|
|
toqstr(title_), toqstr(startsWith), toqstr(filters.as_string()) ))));
|
2006-03-05 17:24:44 +00:00
|
|
|
#else
|
2006-05-07 10:20:43 +00:00
|
|
|
LyXFileDialog dlg(title_, path, filters, private_->b1, private_->b2);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
if (!suggested.empty())
|
2006-05-07 10:20:43 +00:00
|
|
|
dlg.selectFile(toqstr(suggested));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
|
|
|
|
int res = dlg.exec();
|
|
|
|
lyxerr[Debug::GUI] << "result " << res << endl;
|
|
|
|
if (res == QDialog::Accepted)
|
2006-12-05 19:51:50 +00:00
|
|
|
result.second = lyx::from_utf8(internal_path(
|
|
|
|
fromqstr(dlg.selectedFiles()[0])));
|
2006-03-05 17:24:44 +00:00
|
|
|
dlg.hide();
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-09 16:33:20 +00:00
|
|
|
FileDialog::Result const FileDialog::opendir(docstring const & path,
|
|
|
|
docstring const & suggested)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-10-09 16:33:20 +00:00
|
|
|
lyxerr[Debug::GUI] << "Select with path \"" << lyx::to_utf8(path)
|
|
|
|
<< "\", suggested \"" << lyx::to_utf8(suggested) << '"' << endl;
|
2006-03-05 17:24:44 +00:00
|
|
|
FileDialog::Result result;
|
|
|
|
result.first = FileDialog::Chosen;
|
|
|
|
|
|
|
|
#ifdef USE_NATIVE_FILEDIALOG
|
2006-12-27 10:56:11 +00:00
|
|
|
docstring const startsWith = from_utf8(
|
|
|
|
makeAbsPath(to_utf8(suggested), to_utf8(path)).absFilename());
|
2006-12-05 19:51:50 +00:00
|
|
|
result.second = lyx::from_utf8(internal_path(fromqstr(
|
|
|
|
QFileDialog::getExistingDirectory(qApp->focusWidget(),
|
|
|
|
toqstr(title_),toqstr(startsWith)))));
|
2006-03-05 17:24:44 +00:00
|
|
|
#else
|
2006-10-09 16:33:20 +00:00
|
|
|
FileFilterList const filter(_("Directories"));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-05-07 10:20:43 +00:00
|
|
|
LyXFileDialog dlg(title_, path, filter, private_->b1, private_->b2);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-05-07 10:20:43 +00:00
|
|
|
dlg.setFileMode(QFileDialog::DirectoryOnly);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
if (!suggested.empty())
|
2006-05-07 10:20:43 +00:00
|
|
|
dlg.selectFile(toqstr(suggested));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
lyxerr[Debug::GUI] << "Synchronous FileDialog: " << endl;
|
|
|
|
int res = dlg.exec();
|
|
|
|
lyxerr[Debug::GUI] << "result " << res << endl;
|
|
|
|
if (res == QDialog::Accepted)
|
2006-12-05 19:51:50 +00:00
|
|
|
result.second = lyx::from_utf8(internal_path(
|
|
|
|
fromqstr(dlg.selectedFiles()[0])));
|
2006-03-05 17:24:44 +00:00
|
|
|
dlg.hide();
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|