diff --git a/lib/ui/stdmenus.inc b/lib/ui/stdmenus.inc index 176dcc537b..3f712c1ad4 100644 --- a/lib/ui/stdmenus.inc +++ b/lib/ui/stdmenus.inc @@ -87,6 +87,7 @@ Menuset End Menu "file_export" + Item "Export As...|A" "buffer-export-as" ExportFormats Item "More Formats & Options...|O" "buffer-export custom" End diff --git a/src/FuncCode.h b/src/FuncCode.h index cd2d289a89..977036d189 100644 --- a/src/FuncCode.h +++ b/src/FuncCode.h @@ -450,6 +450,7 @@ enum FuncCode LFUN_SCRIPT_INSERT, // gb, 20101123 // 350 + LFUN_BUFFER_EXPORT_AS, // tommaso 20111006 LFUN_LASTACTION // end of the table }; diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp index 507a9d03a4..fded17e583 100644 --- a/src/LyXAction.cpp +++ b/src/LyXAction.cpp @@ -2977,6 +2977,14 @@ void LyXAction::init() * \endvar */ { LFUN_BUFFER_EXPORT_CUSTOM, "buffer-export-custom", ReadOnly, Buffer }, +/*! + * \var lyx::FuncCode lyx::LFUN_BUFFER_EXPORT_AS + * \li Action: Pops up a dialog for exporting the current buffer. + * \li Syntax: buffer-export + * \li Origin: tommaso, 6 Oct 2011 + * \endvar + */ + { LFUN_BUFFER_EXPORT_AS, "buffer-export-as", ReadOnly, Buffer }, /*! * \var lyx::FuncCode lyx::LFUN_BUFFER_PRINT * \li Action: Prints the current document. diff --git a/src/frontends/qt4/FileDialog.cpp b/src/frontends/qt4/FileDialog.cpp index 6b45eca578..70421f4994 100644 --- a/src/frontends/qt4/FileDialog.cpp +++ b/src/frontends/qt4/FileDialog.cpp @@ -81,7 +81,8 @@ void FileDialog::setButton2(QString const & label, QString const & dir) FileDialog::Result FileDialog::save(QString const & path, - QStringList const & filters, QString const & suggested) + QStringList const & filters, QString const & suggested, + QString & selectedFilter) { LYXERR(Debug::GUI, "Select with path \"" << path << "\", mask \"" << filters.join(";;") @@ -95,7 +96,7 @@ FileDialog::Result FileDialog::save(QString const & path, QString const name = QFileDialog::getSaveFileName(qApp->focusWidget(), title_, startsWith, filters.join(";;"), - 0, QFileDialog::DontConfirmOverwrite); + selectedFilter, QFileDialog::DontConfirmOverwrite); if (name.isNull()) result.first = FileDialog::Later; else @@ -118,12 +119,21 @@ FileDialog::Result FileDialog::save(QString const & path, result.second = internalPath(dlg.selectedFiles()[0]); else result.first = FileDialog::Later; + selectedFilter = dlg.selectedNameFilter(); dlg.hide(); #endif return result; } +FileDialog::Result FileDialog::save(QString const & path, + QStringList const & filters, QString const & suggested) +{ + QString selectedFilter = 0; + return save(path, filters, suggested, selectedFilter); +} + + FileDialog::Result FileDialog::open(QString const & path, QStringList const & filters, QString const & suggested) { diff --git a/src/frontends/qt4/FileDialog.h b/src/frontends/qt4/FileDialog.h index f5da6eb73e..510ebdceb8 100644 --- a/src/frontends/qt4/FileDialog.h +++ b/src/frontends/qt4/FileDialog.h @@ -70,6 +70,10 @@ public: Result save(QString const & path, QStringList const & filters, QString const & suggested = QString()); + /// Also retrieve the selected filter. + Result save(QString const & path, QStringList const & filters, + QString const & suggested, QString & selectedFilter); + private: class Private; friend class Private; diff --git a/src/frontends/qt4/GuiView.cpp b/src/frontends/qt4/GuiView.cpp index 2e0f37a11c..202b561bbb 100644 --- a/src/frontends/qt4/GuiView.cpp +++ b/src/frontends/qt4/GuiView.cpp @@ -1657,6 +1657,7 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag) } case LFUN_BUFFER_WRITE_AS: + case LFUN_BUFFER_EXPORT_AS: enable = doc_buffer; break; @@ -2313,6 +2314,64 @@ bool GuiView::renameBuffer(Buffer & b, docstring const & newname) } +bool GuiView::exportBufferAs(Buffer & b) +{ + FileName fname = b.fileName(); + FileName const oldname = fname; + + FileDialog dlg(qt_("Choose a filename to export the document as")); + dlg.setButton1(qt_("Documents|#o#O"), toqstr(lyxrc.document_path)); + + QStringList types; + types << "Any supported format (*.*)"; + Formats::const_iterator it = formats.begin(); + for (; it != formats.end(); ++it) + if (it->documentFormat()) + types << toqstr(it->name() + " (*." + it->extension() + ")"); + QString filter; + FileDialog::Result result = + dlg.save(toqstr(fname.onlyPath().absFileName()), + types, + toqstr(fname.onlyFileName()), + filter); + if (result.first != FileDialog::Chosen) + return false; + + string s = fromqstr(filter); + size_t pos = s.find(" ("); + LASSERT(pos != string::npos, /**/); + string fmt_name = s.substr(0, pos); + fname.set(fromqstr(result.second)); + if (fmt_name == "Any supported format") + fmt_name = formats.getFormatFromExtension(fname.extension()); + LYXERR(Debug::FILES, "fmt_name=" << fmt_name << ", fname=" << fname.absFileName()); + + if (fname.empty()) + return false; + + // fname is now the new Buffer location. + if (FileName(fname).exists()) { + docstring const file = makeDisplayPath(fname.absFileName(), 30); + docstring text = bformat(_("The document %1$s already " + "exists.\n\nDo you want to " + "overwrite that document?"), + file); + int const ret = Alert::prompt(_("Overwrite document?"), + text, 0, 2, _("&Overwrite"), _("&Rename"), _("&Cancel")); + switch (ret) { + case 0: break; + case 1: return exportBufferAs(b); + case 2: return false; + } + } + + FuncRequest cmd(LFUN_BUFFER_EXPORT, fmt_name + " " + fname.absFileName()); + DispatchResult dr; + dispatch(cmd, dr); + return dr.dispatched(); +} + + bool GuiView::saveBuffer(Buffer & b) { return saveBuffer(b, FileName()); } @@ -3158,6 +3217,11 @@ void GuiView::dispatch(FuncRequest const & cmd, DispatchResult & dr) break; } + case LFUN_BUFFER_EXPORT_AS: + LASSERT(doc_buffer, break); + exportBufferAs(*doc_buffer); + break; + case LFUN_BUFFER_UPDATE: { d.asyncBufferProcessing(argument, doc_buffer, diff --git a/src/frontends/qt4/GuiView.h b/src/frontends/qt4/GuiView.h index 667ce60788..2dcea7f7e0 100644 --- a/src/frontends/qt4/GuiView.h +++ b/src/frontends/qt4/GuiView.h @@ -353,6 +353,8 @@ private: /// void insertPlaintextFile(docstring const & fname, bool asParagraph); + /// + bool exportBufferAs(Buffer & b); /// Save a buffer as a new file. /**