]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt/GuiInclude.cpp
Make string-widget combination more l7n friendly
[lyx.git] / src / frontends / qt / GuiInclude.cpp
index c55d71fa7f21ddb116bef58f36c13e3cac16f3d8..6120d669ef5072096cfa3fb346a249d58fc38025 100644 (file)
 #include "GuiInclude.h"
 
 #include "Buffer.h"
+#include "BufferList.h"
 #include "BufferParams.h"
 #include "FuncRequest.h"
 #include "LyXRC.h"
 
 #include "qt_helpers.h"
-#include "LyXRC.h"
 
 #include "support/gettext.h"
 #include "support/lstrings.h"
 #include "support/FileName.h"
 #include "support/filetools.h"
 
+#include "frontends/alert.h"
+
 #include "insets/InsetListingsParams.h"
 #include "insets/InsetInclude.h"
 
 #include <QCheckBox>
+#include <QFile>
 #include <QLineEdit>
 #include <QPushButton>
 
@@ -81,7 +84,9 @@ GuiInclude::GuiInclude(GuiView & lv)
        bc().addReadOnly(typeCO);
        bc().addReadOnly(listingsED);
 
-       bc().addCheckedLineEdit(filenameED, filenameLA);
+       // FIXME does not make sense, as we do not have a validator
+       // for this widget
+       //bc().addCheckedLineEdit(filenameED, filenameLA);
 }
 
 
@@ -265,6 +270,72 @@ void GuiInclude::applyView()
        }
        params_["literal"] = literalCB->isChecked()
                        ? from_ascii("true") : from_ascii("false");
+
+       // Do we need to create a LyX file?
+       if (item == 0 || item == 1) {
+               QString fname = filenameED->text();
+               string const mypath = buffer().absFileName();
+               string const bpath = buffer().filePath();
+               QString absfname = makeAbsPath(fname, toqstr(bpath));
+               if (!QFile::exists(absfname)) {
+                       dispatch(FuncRequest(LFUN_BUFFER_NEW, fromqstr(absfname)));
+                       dispatch(FuncRequest(LFUN_BUFFER_WRITE));
+                       dispatch(FuncRequest(LFUN_BUFFER_SWITCH, mypath));
+               }
+       }
+}
+
+
+void GuiInclude::edit()
+{
+       if (!isValid())
+               return;
+       if (bc().policy().buttonStatus(ButtonPolicy::OKAY)) {
+               slotOK();
+               applyView();
+       } else
+               hideView();
+       QString const fname = filenameED->text();
+       string const bpath = buffer().filePath();
+       string const absfname = support::makeAbsPath(fromqstr(fname), bpath).absFileName();
+       // The button is enabled only if the document is already open.
+       // If something goes wrong and it is not, we'll get an error
+       // message from the dispatch. So no need for one here.
+       dispatch(FuncRequest(LFUN_BUFFER_SWITCH, absfname));
+}
+
+
+bool GuiInclude::isValid()
+{
+       QString fname = filenameED->text();
+       if (fname.isEmpty() || !validate_listings_params().empty()) {
+               editPB->setEnabled(false);
+               return false;
+       }
+
+       QPushButton * okbutton = buttonBox->button(QDialogButtonBox::Ok);
+       int const item = typeCO->currentIndex();
+       // Are we inputting or including a LyX file?
+       if (item != 0 && item != 1) {
+               okbutton->setText(qt_("&OK"));
+               return true;
+       }
+
+       string const bpath = buffer().filePath();
+       // Path might be relative to current Buffer, so make absolute
+       FileName const absfname = support::makeAbsPath(fromqstr(fname), bpath);
+
+       // Do we have a LyX filename?
+       if (!isLyXFileName(fromqstr(fname))) {
+               okbutton->setText(qt_("&OK"));
+               return absfname.exists();
+       }
+
+       // Set OK button text according to whether file already exists
+       okbutton->setText(absfname.exists() ? qt_("&OK") : qt_("&Create"));
+       // enable edit button iff file is open in some Buffer
+       editPB->setEnabled(theBufferList().getBuffer(absfname));
+       return true;
 }
 
 
@@ -288,25 +359,6 @@ void GuiInclude::browse()
 }
 
 
-void GuiInclude::edit()
-{
-       if (!isValid())
-               return;
-       if (bc().policy().buttonStatus(ButtonPolicy::OKAY)) {
-               slotOK();
-               applyView();
-       } else
-               hideView();
-       dispatch(FuncRequest(LFUN_INSET_EDIT));
-}
-
-
-bool GuiInclude::isValid()
-{
-       return !filenameED->text().isEmpty() && validate_listings_params().empty();
-}
-
-
 QString GuiInclude::browse(QString const & in_name, Type in_type) const
 {
        QString const title = qt_("Select document to include");
@@ -346,9 +398,6 @@ void GuiInclude::dispatchParams()
 }
 
 
-Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
-
-
 } // namespace frontend
 } // namespace lyx