]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiGraphics.cpp
Create new graphics from within LyX choosing a sample file to copy from.
[lyx.git] / src / frontends / qt4 / GuiGraphics.cpp
index 979cd2774e5c5f8ef81de455d4c66f7985a26e80..404df3c9d1282ae43771e75a1e15d7b6d0384ad2 100644 (file)
@@ -24,6 +24,7 @@
 #include "LengthCombo.h"
 #include "Length.h"
 #include "LyXRC.h"
+#include "Format.h"
 
 #include "graphics/epstools.h"
 #include "graphics/GraphicsCache.h"
@@ -42,6 +43,7 @@
 #include "support/types.h"
 
 #include <QCheckBox>
+#include <QFileDialog>
 #include <QLabel>
 #include <QLineEdit>
 #include <QPushButton>
@@ -110,7 +112,7 @@ static void setAutoTextCB(QCheckBox * checkBox, QLineEdit * lineEdit,
 
 
 GuiGraphics::GuiGraphics(GuiView & lv)
-       : GuiDialog(lv, "graphics", qt_("Graphics"))
+       : GuiDialog(lv, "graphics", qt_("Graphics")), bbChanged(false)
 {
        setupUi(this);
        
@@ -248,6 +250,13 @@ GuiGraphics::GuiGraphics(GuiView & lv)
 void GuiGraphics::change_adaptor()
 {
        changed();
+       string const fname = fromqstr(filename->text());
+       FileName dest_fname(FileName(fromqstr(bufferFilePath())), fname);
+       Format const * fmt = theFormats().getFormat(theFormats().getFormatFromFile(dest_fname));
+       LYXERR(Debug::GRAPHICS, "fmt: " << fmt
+               << ", fmt_name: " << theFormats().getFormatFromFile(dest_fname));
+       editPB->setEnabled(!fname.empty() && !dest_fname.isDirectory() && dest_fname.exists());
+       chooseSamplePB->setEnabled(!fname.empty() && !dest_fname.isDirectory());
 }
 
 
@@ -359,6 +368,91 @@ void GuiGraphics::on_browsePB_clicked()
 }
 
 
+bool GuiGraphics::checkFileExists()
+{
+       string const fname = fromqstr(filename->text());
+       FileName dest_fname(FileName(fromqstr(bufferFilePath())), fname);
+       if (fname.empty() || !dest_fname.exists()) {
+               return frontend::Alert::prompt(
+                       _("Continue?"), bformat(_("File '%1$s' does not exist. Continue?"), from_utf8(dest_fname.absFileName())),
+                       0, 1, _("&Yes"), _("&No")) == 0;
+       }
+       return true;
+}
+
+
+void GuiGraphics::on_chooseSamplePB_clicked()
+{
+       string fname = fromqstr(filename->text());
+       FileName dest_fname(FileName(fromqstr(bufferFilePath())), fname);
+       string fmt_name = theFormats().getFormatFromFile(FileName(fname));
+       string filter("Any file (*.*)");
+       if (!fmt_name.empty()) {
+               Format const *fmt = theFormats().getFormat(fmt_name);
+               filter = to_utf8(fmt->prettyname()) + " Files (*." + dest_fname.extension() + ")";
+       }
+       if (fname.empty() || dest_fname.isDirectory()) {
+               frontend::Alert::warning(_("Invalid destination file name!"), _("Invalid destination file name!"));
+               return;
+       }
+
+       QString const samplesDir = toqstr(addPath(package().user_support().absFileName(), "samples"));
+       string sample_name = fromqstr(QFileDialog::getOpenFileName(this, toqstr("Please, select sample template"), samplesDir, toqstr(filter)));
+       if (sample_name.empty())
+               // User pressed Cancel
+               return;
+       FileName sample_fname = FileName(sample_name);
+
+       //FileName sample_fname = libFileSearch(toqstr("samples"), toqstr(sample_name));
+       if (sample_fname.isDirectory() || !sample_fname.exists()) {
+               frontend::Alert::warning(_("Invalid sample file name"), _("Invalid sample file name"));
+               return;
+       }
+
+       string sample_fmt_name = theFormats().getFormatFromFile(sample_fname);
+       if (fmt_name.empty() && !sample_fmt_name.empty()) {
+               fmt_name = sample_fmt_name;
+               string ext = theFormats().getFormat(fmt_name)->extension();
+               if (!ext.empty()) {
+                       fname = fname + "." + ext;
+                       dest_fname.set(FileName(fromqstr(bufferFilePath())), fname);
+                       filename->setText(toqstr(fname));
+               }
+       }
+
+       if (fname.empty() || dest_fname.isDirectory()) {
+               frontend::Alert::warning(_("Invalid destination file name!"), _("Cannot copy sample file on an invalid destination file!"));
+               return;
+       }
+
+       if (!dest_fname.exists() || (dest_fname.exists() &&
+               frontend::Alert::prompt(
+                       _("Overwrite?"), bformat(_("File '%1$s' already exists. Overwrite with sample from template?"), from_utf8(dest_fname.absFileName())),
+                       0, 1, _("&Yes"), _("&No")) == 0)) {
+               sample_fname.copyTo(dest_fname);
+               dest_fname.refresh();
+               change_adaptor();
+       }
+}
+
+
+void GuiGraphics::on_editPB_clicked()
+{
+       string const fname = fromqstr(filename->text());
+       FileName dest_fname(FileName(fromqstr(bufferFilePath())), fname);
+       string fmt_name = theFormats().getFormatFromFile(FileName(fname));
+       if (checkFileExists())
+               theFormats().edit(buffer(), dest_fname, fmt_name);
+}
+
+
+void GuiGraphics::on_okPB_clicked()
+{
+       if (checkFileExists())
+               applyView();
+}
+
+
 void GuiGraphics::on_getPB_clicked()
 {
        getBB();
@@ -525,7 +619,7 @@ void GuiGraphics::paramsToDialog(InsetGraphicsParams const & igp)
        filename->setText(toqstr(name));
 
        // set the bounding box values
-       if (igp.bb.empty()) {
+       if (igp.bbox.empty()) {
                string const bb = readBoundingBox(igp.filename.absFileName());
                // the values from the file always have the bigpoint-unit bp
                doubleToWidget(lbX, token(bb, ' ', 0));
@@ -539,39 +633,18 @@ void GuiGraphics::paramsToDialog(InsetGraphicsParams const & igp)
                bbChanged = false;
        } else {
                // get the values from the inset
-               Length anyLength;
-               string const xl = token(igp.bb, ' ', 0);
-               string const yl = token(igp.bb, ' ', 1);
-               string const xr = token(igp.bb, ' ', 2);
-               string const yr = token(igp.bb, ' ', 3);
-               if (isValidLength(xl, &anyLength)) {
-                       doubleToWidget(lbX, anyLength.value());
-                       string const unit = unit_name[anyLength.unit()];
-                       lbXunit->setCurrentIndex(lbXunit->findData(toqstr(unit)));
-               } else {
-                       lbX->setText(toqstr(xl));
-               }
-               if (isValidLength(yl, &anyLength)) {
-                       doubleToWidget(lbY, anyLength.value());
-                       string const unit = unit_name[anyLength.unit()];
-                       lbYunit->setCurrentIndex(lbYunit->findData(toqstr(unit)));
-               } else {
-                       lbY->setText(toqstr(xl));
-               }
-               if (isValidLength(xr, &anyLength)) {
-                       doubleToWidget(rtX, anyLength.value());
-                       string const unit = unit_name[anyLength.unit()];
-                       rtXunit->setCurrentIndex(rtXunit->findData(toqstr(unit)));
-               } else {
-                       rtX->setText(toqstr(xl));
-               }
-               if (isValidLength(yr, &anyLength)) {
-                       doubleToWidget(rtY, anyLength.value());
-                       string const unit = unit_name[anyLength.unit()];
-                       rtYunit->setCurrentIndex(rtYunit->findData(toqstr(unit)));
-               } else {
-                       rtY->setText(toqstr(xl));
-               }
+               doubleToWidget(lbX, igp.bbox.xl.value());
+               string unit = unit_name[igp.bbox.xl.unit()];
+               lbXunit->setCurrentIndex(lbXunit->findData(toqstr(unit)));
+               doubleToWidget(lbY, igp.bbox.yb.value());
+               unit = unit_name[igp.bbox.yb.unit()];
+               lbYunit->setCurrentIndex(lbYunit->findData(toqstr(unit)));
+               doubleToWidget(rtX, igp.bbox.xr.value());
+               unit = unit_name[igp.bbox.xr.unit()];
+               rtXunit->setCurrentIndex(rtXunit->findData(toqstr(unit)));
+               doubleToWidget(rtY, igp.bbox.yt.value());
+               unit = unit_name[igp.bbox.yt.unit()];
+               rtYunit->setCurrentIndex(rtYunit->findData(toqstr(unit)));
                bbChanged = true;
        }
 
@@ -673,9 +746,8 @@ void GuiGraphics::applyView()
        igp.filename.set(fromqstr(filename->text()), fromqstr(bufferFilePath()));
 
        // the bb section
-       igp.bb.erase();
+       igp.bbox = graphics::BoundingBox();
        if (bbChanged) {
-               string bb;
                string lbXs = widgetToDoubleStr(lbX);
                string lbYs = widgetToDoubleStr(lbY);
                string rtXs = widgetToDoubleStr(rtX);
@@ -685,22 +757,17 @@ void GuiGraphics::applyView()
                        convert<int>(rtXs) + convert<int>(rtXs);
                if (bb_sum) {
                        if (lbXs.empty())
-                               bb = "0 ";
-                       else
-                               bb = lbXs + fromqstr(lbXunit->currentText()) + ' ';
+                               lbXs = "0";
+                       igp.bbox.xl = Length(lbXs + fromqstr(lbXunit->currentText()));
                        if (lbYs.empty())
-                               bb += "0 ";
-                       else
-                               bb += (lbYs + fromqstr(lbYunit->currentText()) + ' ');
+                               lbYs = "0";
+                       igp.bbox.yb = Length(lbYs + fromqstr(lbYunit->currentText()));
                        if (rtXs.empty())
-                               bb += "0 ";
-                       else
-                               bb += (rtXs + fromqstr(rtXunit->currentText()) + ' ');
+                               rtXs = "0";
+                       igp.bbox.xr = Length(rtXs + fromqstr(rtXunit->currentText()));
                        if (rtYs.empty())
-                               bb += '0';
-                       else
-                               bb += (rtYs + fromqstr(rtYunit->currentText()));
-                       igp.bb = bb;
+                               rtYs = "0";
+                       igp.bbox.yt = Length(rtYs + fromqstr(rtYunit->currentText()));
                }
        }