]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiExternal.cpp
Make a string translatable
[lyx.git] / src / frontends / qt4 / GuiExternal.cpp
index 59916545006902939115291da44e1d4b76492738..65790327bb88b7f20b7886074d31743620c3d8fc 100644 (file)
@@ -14,6 +14,7 @@
 
 #include "GuiExternal.h"
 
+#include "Buffer.h"
 #include "FuncRequest.h"
 #include "support/gettext.h"
 #include "Length.h"
@@ -23,6 +24,7 @@
 #include "insets/ExternalTemplate.h"
 #include "insets/InsetExternal.h"
 
+#include "graphics/epstools.h"
 #include "graphics/GraphicsCache.h"
 #include "graphics/GraphicsCacheItem.h"
 #include "graphics/GraphicsImage.h"
@@ -78,16 +80,6 @@ char const * const origin_gui_strs[] = {
        N_("Top right"), N_("Bottom right"), N_("Baseline right")
 };
 
-external::Template getTemplate(int i)
-{
-       if (external::TemplateManager::get().getTemplates().empty())
-               return Template();
-       external::TemplateManager::Templates::const_iterator i1
-               = external::TemplateManager::get().getTemplates().begin();
-       advance(i1, i);
-       return i1->second;
-}
-
 } // namespace anon
 
 
@@ -139,6 +131,14 @@ GuiExternal::GuiExternal(GuiView & lv)
        connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
        connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
        connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
+       connect(xrUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
+               this, SLOT(bbChanged()));
+       connect(ytUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
+               this, SLOT(bbChanged()));
+       connect(xlUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
+               this, SLOT(bbChanged()));
+       connect(ybUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
+               this, SLOT(bbChanged()));
        connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
 
        QIntValidator * validator = new QIntValidator(displayscaleED);
@@ -147,10 +147,10 @@ GuiExternal::GuiExternal(GuiView & lv)
 
        angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
 
-       xlED->setValidator(new QIntValidator(xlED));
-       ybED->setValidator(new QIntValidator(ybED));
-       xrED->setValidator(new QIntValidator(xrED));
-       ytED->setValidator(new QIntValidator(ytED));
+       xlED->setValidator(unsignedLengthValidator(xlED));
+       ybED->setValidator(unsignedLengthValidator(ybED));
+       xrED->setValidator(unsignedLengthValidator(xrED));
+       ytED->setValidator(unsignedLengthValidator(ytED));
 
        widthED->setValidator(unsignedLengthValidator(widthED));
        heightED->setValidator(unsignedLengthValidator(heightED));
@@ -182,6 +182,10 @@ GuiExternal::GuiExternal(GuiView & lv)
        bc().addReadOnly(xlED);
        bc().addReadOnly(xrED);
        bc().addReadOnly(ybED);
+       bc().addReadOnly(ytUnitCO);
+       bc().addReadOnly(xlUnitCO);
+       bc().addReadOnly(xrUnitCO);
+       bc().addReadOnly(ybUnitCO);
        bc().addReadOnly(extraFormatCO);
        bc().addReadOnly(extraED);
 
@@ -199,7 +203,9 @@ GuiExternal::GuiExternal(GuiView & lv)
        i1 = external::TemplateManager::get().getTemplates().begin();
        i2 = external::TemplateManager::get().getTemplates().end();
        for (; i1 != i2; ++i1)
-               externalCO->addItem(qt_(i1->second.lyxName));
+               externalCO->addItem(qt_(i1->second.guiName), toqstr(i1->second.lyxName));
+       // Sort alphabetically by(localized) GUI name
+       externalCO->model()->sort(0);
 
        // Fill the origins combo
        for (size_t i = 0; i != sizeof(origins) / sizeof(origins[0]); ++i)
@@ -207,6 +213,18 @@ GuiExternal::GuiExternal(GuiView & lv)
 
        // add scale item
        widthUnitCO->insertItem(0, qt_("Scale%"), "scale");
+
+       // remove all units from bb that depend on font or other dimensions
+       // we cannot use these, since we need to compare against absolute
+       // values from the image file.
+       xlUnitCO->noPercents();
+       xlUnitCO->removeFontDependent();
+       xrUnitCO->noPercents();
+       xrUnitCO->removeFontDependent();
+       ytUnitCO->noPercents();
+       ytUnitCO->removeFontDependent();
+       ybUnitCO->noPercents();
+       ybUnitCO->removeFontDependent();
 }
 
 
@@ -256,8 +274,8 @@ void GuiExternal::bbChanged()
 
 void GuiExternal::browseClicked()
 {
-       int const choice =  externalCO->currentIndex();
-       QString const template_name = toqstr(getTemplate(choice).lyxName);
+       QString const template_name =
+               externalCO->itemData(externalCO->currentIndex()).toString();
        QString const str = browse(fileED->text(), template_name);
        if (!str.isEmpty()) {
                fileED->setText(str);
@@ -299,7 +317,7 @@ void GuiExternal::getbbClicked()
        FileName const abs_file(support::makeAbsPath(filename, fromqstr(bufferFilePath())));
 
        // try to get it from the file, if possible
-       string bb = readBB_from_PSFile(abs_file);
+       string bb = graphics::readBB_from_PSFile(abs_file);
        if (bb.empty()) {
                // we don't, so ask the Graphics Cache if it has loaded the file
                int width = 0;
@@ -317,10 +335,15 @@ void GuiExternal::getbbClicked()
                bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
        }
 
-       xlED->setText(toqstr(token(bb, ' ', 0)));
-       ybED->setText(toqstr(token(bb, ' ', 1)));
-       xrED->setText(toqstr(token(bb, ' ', 2)));
-       ytED->setText(toqstr(token(bb, ' ', 3)));
+       doubleToWidget(xlED, token(bb, ' ', 0));
+       doubleToWidget(ybED, token(bb, ' ', 1));
+       doubleToWidget(xrED, token(bb, ' ', 2));
+       doubleToWidget(ytED, token(bb, ' ', 3));
+       // the values from the file always have the bigpoint-unit bp
+       xlUnitCO->setCurrentIndex(0);
+       ybUnitCO->setCurrentIndex(0);
+       xrUnitCO->setCurrentIndex(0);
+       ytUnitCO->setCurrentIndex(0);
 
        bbChanged_ = false;
 }
@@ -428,14 +451,17 @@ static void getSize(external::ResizeData & data,
 void setCrop(QCheckBox & clipCB,
        QLineEdit & xlED, QLineEdit & ybED,
        QLineEdit & xrED, QLineEdit & ytED,
+       LengthCombo & xlUnitCO, LengthCombo & ybUnitCO,
+       LengthCombo & xrUnitCO, LengthCombo & ytUnitCO,
        external::ClipData const & data)
 {
        clipCB.setChecked(data.clip);
-       graphics::BoundingBox const & bbox = data.bbox;
-       xlED.setText(QString::number(bbox.xl));
-       ybED.setText(QString::number(bbox.yb));
-       xrED.setText(QString::number(bbox.xr));
-       ytED.setText(QString::number(bbox.yt));
+       Length::UNIT const default_unit = data.bbox.xl.zero() ?
+               Length::defaultUnit() : data.bbox.xl.unit();
+       lengthToWidgets(&xlED, &xlUnitCO, data.bbox.xl, default_unit);
+       lengthToWidgets(&ybED, &ybUnitCO, data.bbox.yb, default_unit);
+       lengthToWidgets(&xrED, &xrUnitCO, data.bbox.xr, default_unit);
+       lengthToWidgets(&ytED, &ytUnitCO, data.bbox.yt, default_unit);
 }
 
 
@@ -443,6 +469,8 @@ static void getCrop(external::ClipData & data,
        QCheckBox const & clipCB,
        QLineEdit const & xlED, QLineEdit const & ybED,
        QLineEdit const & xrED, QLineEdit const & ytED,
+       LengthCombo const & xlUnitCO, LengthCombo const & ybUnitCO,
+       LengthCombo const & xrUnitCO, LengthCombo const & ytUnitCO,
        bool bb_changed)
 {
        data.clip = clipCB.isChecked();
@@ -450,10 +478,10 @@ static void getCrop(external::ClipData & data,
        if (!bb_changed)
                return;
 
-       data.bbox.xl = xlED.text().toInt();
-       data.bbox.yb = ybED.text().toInt();
-       data.bbox.xr = xrED.text().toInt();
-       data.bbox.yt = ytED.text().toInt();
+       data.bbox.xl = Length(widgetsToLength(&xlED, &xlUnitCO));
+       data.bbox.yb = Length(widgetsToLength(&ybED, &ybUnitCO));
+       data.bbox.xr = Length(widgetsToLength(&xrED, &xrUnitCO));
+       data.bbox.yt = Length(widgetsToLength(&ytED, &ytUnitCO));
 }
 
 
@@ -463,18 +491,8 @@ void GuiExternal::updateContents()
                params_.filename.outputFileName(fromqstr(bufferFilePath()));
        fileED->setText(toqstr(name));
 
-       int index = 0;
-       external::TemplateManager::Templates::const_iterator i1, i2;
-       i1 = external::TemplateManager::get().getTemplates().begin();
-       i2 = external::TemplateManager::get().getTemplates().end();
-       for (int i = 0; i1 != i2; ++i1, ++i) {
-               if (i1->second.lyxName == params_.templatename()) {
-                       index = i;
-                       break;
-               }
-       }
-
-       externalCO->setCurrentIndex(index);
+       externalCO->setCurrentIndex(
+               externalCO->findData(toqstr(params_.templatename())));
        updateTemplate();
 
        draftCB->setChecked(params_.draft);
@@ -493,7 +511,8 @@ void GuiExternal::updateContents()
        setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
                *aspectratioCB, params_.resizedata);
 
-       setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
+       setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED,
+               *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, params_.clipdata);
        bbChanged_ = !params_.clipdata.bbox.empty();
 
        isValid();
@@ -502,35 +521,36 @@ void GuiExternal::updateContents()
 
 void GuiExternal::updateTemplate()
 {
-       external::Template templ = getTemplate(externalCO->currentIndex());
-       externalTB->setPlainText(qt_(templ.helpText));
+       external::TemplateManager const & etm =
+               external::TemplateManager::get();
+       external::Template const * const templ = etm.getTemplateByName(
+               fromqstr(externalCO->itemData(externalCO->currentIndex()).toString()));
+       externalTB->setPlainText(toqstr(translateIfPossible(templ->helpText)));
 
        // Ascertain which (if any) transformations the template supports
        // and disable tabs and Group Boxes hosting unsupported transforms.
        typedef vector<external::TransformID> TransformIDs;
-       TransformIDs const transformIds = templ.transformIds;
+       TransformIDs const transformIds = templ->transformIds;
        TransformIDs::const_iterator tr_begin = transformIds.begin();
        TransformIDs::const_iterator const tr_end = transformIds.end();
 
-       bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
-       rotationGB->setEnabled(found);
+       bool rotate = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
+       rotationGB->setEnabled(rotate);
 
-       found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
-       scaleGB->setEnabled(found);
+       bool resize = std::find(tr_begin, tr_end, external::Resize) != tr_end;
+       scaleGB->setEnabled(resize);
 
-       found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
-       cropGB->setEnabled(found);
+       bool clip = std::find(tr_begin, tr_end, external::Clip) != tr_end;
+       cropGB->setEnabled(clip);
 
-       tab->setTabEnabled(tab->indexOf(sizetab),
-               rotationGB->isEnabled()
-               || scaleGB->isEnabled()
-               || cropGB->isEnabled());
+       sizetab->setEnabled(rotate || resize || clip);
+       tab->setTabEnabled(tab->indexOf(sizetab), rotate || resize || clip);
 
-       found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
+       bool found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
        optionsGB->setEnabled(found);
 
        bool scaled = displayGB->isChecked() && displayGB->isEnabled() &&
-                       !isBufferReadonly() && (templ.preview_mode != PREVIEW_INSTANT);
+                       !isBufferReadonly() && (templ->preview_mode != PREVIEW_INSTANT);
        displayscaleED->setEnabled(scaled);
        scaleLA->setEnabled(scaled);
 
@@ -543,8 +563,8 @@ void GuiExternal::updateTemplate()
        extraED->clear();
        extraFormatCO->clear();
 
-       external::Template::Formats::const_iterator it  = templ.formats.begin();
-       external::Template::Formats::const_iterator end = templ.formats.end();
+       external::Template::Formats::const_iterator it  = templ->formats.begin();
+       external::Template::Formats::const_iterator end = templ->formats.end();
        for (; it != end; ++it) {
                if (it->second.option_transformers.find(external::Extra) ==
                    it->second.option_transformers.end())
@@ -571,7 +591,7 @@ void GuiExternal::updateTemplate()
 void GuiExternal::applyView()
 {
        params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilePath()));
-       params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
+       params_.settemplate(fromqstr(externalCO->itemData(externalCO->currentIndex()).toString()));
 
        params_.draft = draftCB->isChecked();
        params_.lyxscale = displayscaleED->text().toInt();
@@ -585,8 +605,8 @@ void GuiExternal::applyView()
                        *heightED, *heightUnitCO, *aspectratioCB, usingScale());
 
        if (cropGB->isEnabled())
-               getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
-                       *xrED, *ytED, bbChanged_);
+               getCrop(params_.clipdata, *clipCB, *xlED, *ybED, *xrED, *ytED,
+                       *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, bbChanged_);
 
        if (optionsGB->isEnabled()) {
                MapType::const_iterator it = extra_.begin();
@@ -625,7 +645,14 @@ static QStringList templateFilters(QString const & template_name)
        external::Template const * const et_ptr =
                etm.getTemplateByName(fromqstr(template_name));
 
-       return fileFilters(et_ptr ? toqstr(et_ptr->fileRegExp) : QString());
+       string filter;
+       if (et_ptr && et_ptr->fileRegExp != "" && et_ptr->fileRegExp != "*") {
+               filter += to_utf8(_(et_ptr->guiName));
+               filter += " (";
+               filter += et_ptr->fileRegExp;
+               filter += ")";
+       }
+       return fileFilters(toqstr(filter));
 }
 
 
@@ -639,7 +666,7 @@ QString GuiExternal::browse(QString const & input,
        QString const label1 = qt_("Documents|#o#O");
        QString const dir1 = toqstr(lyxrc.document_path);
 
-       return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
+       return browseRelToParent(input, bufpath, title, filter, false, label1, dir1);
 }