]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiExternal.cpp
* fix spelling in comments to please John.
[lyx.git] / src / frontends / qt4 / GuiExternal.cpp
index 2fe1ce848abe26dbd71a81587f4a6f7e824e47a3..38443e8db5d30872f70a7a4888a6d84d67165084 100644 (file)
@@ -28,7 +28,6 @@
 #include "graphics/GraphicsImage.h"
 
 #include "support/convert.h"
-#include "support/FileFilterList.h"
 #include "support/filetools.h"
 #include "support/lstrings.h"
 #include "support/lyxlib.h"
@@ -39,6 +38,7 @@
 #include "Validator.h"
 
 #include <QCheckBox>
+#include <QGroupBox>
 #include <QLineEdit>
 #include <QPushButton>
 #include <QTabWidget>
@@ -80,6 +80,8 @@ char const * const origin_gui_strs[] = {
 
 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);
@@ -98,20 +100,16 @@ GuiExternal::GuiExternal(GuiView & lv)
        connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
        connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
 
-       connect(displayCB, SIGNAL(toggled(bool)),
-               showCO, SLOT(setEnabled(bool)));
-       connect(displayCB, SIGNAL(toggled(bool)),
+       /*
+       connect(displayGB, SIGNAL(toggled(bool)),
                displayscaleED, SLOT(setEnabled(bool)));
-       connect(showCO, SIGNAL(activated(QString)),
-               this, SLOT(change_adaptor()));
+               */
        connect(originCO, SIGNAL(activated(int)),
                this, SLOT(change_adaptor()));
        connect(aspectratioCB, SIGNAL(stateChanged(int)),
                this, SLOT(change_adaptor()));
        connect(browsePB, SIGNAL(clicked()),
                this, SLOT(browseClicked()));
-       connect(editPB, SIGNAL(clicked()),
-               this, SLOT(editClicked()));
        connect(externalCO, SIGNAL(activated(QString)),
                this, SLOT(templateChanged()));
        connect(extraED, SIGNAL(textChanged(QString)),
@@ -122,7 +120,7 @@ GuiExternal::GuiExternal(GuiView & lv)
                this, SLOT(widthUnitChanged()));
        connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
                this, SLOT(change_adaptor()));
-       connect(displayCB, SIGNAL(stateChanged(int)),
+       connect(displayGB, SIGNAL(toggled(bool)),
                this, SLOT(change_adaptor()));
        connect(displayscaleED, SIGNAL(textChanged(QString)),
                this, SLOT(change_adaptor()));
@@ -167,12 +165,10 @@ GuiExternal::GuiExternal(GuiView & lv)
 
        bc().addReadOnly(fileED);
        bc().addReadOnly(browsePB);
-       bc().addReadOnly(editPB);
        bc().addReadOnly(externalCO);
        bc().addReadOnly(draftCB);
        bc().addReadOnly(displayscaleED);
-       bc().addReadOnly(showCO);
-       bc().addReadOnly(displayCB);
+       bc().addReadOnly(displayGB);
        bc().addReadOnly(angleED);
        bc().addReadOnly(originCO);
        bc().addReadOnly(heightUnitCO);
@@ -209,41 +205,48 @@ GuiExternal::GuiExternal(GuiView & lv)
        for (size_t i = 0; i != sizeof(origins) / sizeof(origins[0]); ++i)
                originCO->addItem(qt_(origin_gui_strs[i]));
 
-       // Fill the width combo
-       widthUnitCO->addItem(qt_("Scale%"));
-       for (int i = 0; i < num_units; i++)
-               widthUnitCO->addItem(qt_(unit_name_gui[i]));
+       // add scale item
+       widthUnitCO->insertItem(0, qt_("Scale%"), "scale");
 }
 
 
 bool GuiExternal::activateAspectratio() const
 {
-       if (widthUnitCO->currentIndex() == 0)
+       if (usingScale())
                return false;
 
-       string const wstr = fromqstr(widthED->text());
-       if (wstr.empty())
+       QString const wstr = widthED->text();
+       if (wstr.isEmpty())
                return false;
-       bool const wIsDbl = isStrDbl(wstr);
-       if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
+       bool wIsDbl;
+       double val = wstr.trimmed().toDouble(&wIsDbl);
+       if (wIsDbl && float_equal(val, 0.0, 0.05))
                return false;
        Length l;
-       if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
+       if (!wIsDbl && (!isValidLength(fromqstr(wstr), &l) || l.zero()))
                return false;
 
-       string const hstr = fromqstr(heightED->text());
-       if (hstr.empty())
+       QString const hstr = heightED->text();
+       if (hstr.isEmpty())
                return false;
-       bool const hIsDbl = isStrDbl(hstr);
-       if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
+       bool hIsDbl;
+       val = hstr.trimmed().toDouble(&hIsDbl);
+       if (hIsDbl && float_equal(val, 0.0, 0.05))
                return false;
-       if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
+       if (!hIsDbl && (!isValidLength(fromqstr(hstr), &l) || l.zero()))
                return false;
 
        return true;
 }
 
 
+bool GuiExternal::usingScale() const
+{
+       return (widthUnitCO->itemData(
+               widthUnitCO->currentIndex()).toString() == "scale");
+}
+
+
 void GuiExternal::bbChanged()
 {
        bbChanged_ = true;
@@ -269,12 +272,6 @@ void GuiExternal::change_adaptor()
 }
 
 
-void GuiExternal::editClicked()
-{
-       editExternal();
-}
-
-
 void GuiExternal::extraChanged(const QString & text)
 {
        extra_[extraFormatCO->currentText()] = text;
@@ -299,7 +296,7 @@ void GuiExternal::getbbClicked()
        if (filename.empty())
                return;
 
-       FileName const abs_file(makeAbsPath(filename, fromqstr(bufferFilepath())));
+       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);
@@ -345,96 +342,22 @@ void GuiExternal::templateChanged()
 
 void GuiExternal::widthUnitChanged()
 {
-       bool useHeight = (widthUnitCO->currentIndex() > 0);
-
-       if (useHeight)
-               widthED->setValidator(unsignedLengthValidator(widthED));
-       else
+       if (usingScale())
                widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
+       else
+               widthED->setValidator(unsignedLengthValidator(widthED));
 
-       heightED->setEnabled(useHeight);
-       heightUnitCO->setEnabled(useHeight);
+       heightED->setEnabled(!usingScale());
+       heightUnitCO->setEnabled(!usingScale());
        changed();
 }
 
 
-static Length::UNIT defaultUnit()
-{
-       Length::UNIT default_unit = Length::CM;
-       switch (lyxrc.default_papersize) {
-       case PAPER_USLETTER:
-       case PAPER_USLEGAL:
-       case PAPER_USEXECUTIVE:
-               default_unit = Length::IN;
-               break;
-       default:
-               break;
-       }
-       return default_unit;
-}
-
-
-static void setDisplay(
-       QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
-       external::DisplayType display, unsigned int scale, bool read_only)
-{
-       int item = 0;
-       switch (display) {
-       case external::DefaultDisplay:
-               item = 0;
-               break;
-       case external::MonochromeDisplay:
-               item = 1;
-               break;
-       case external::GrayscaleDisplay:
-               item = 2;
-               break;
-       case external::ColorDisplay:
-               item = 3;
-               break;
-       case external::PreviewDisplay:
-               item = 4;
-               break;
-       case external::NoDisplay:
-               item = 0;
-               break;
-       }
-
-       showCO.setCurrentIndex(item);
-       bool const no_display = display == external::NoDisplay;
-       showCO.setEnabled(!no_display && !read_only);
-       displayCB.setChecked(!no_display);
-       scaleED.setEnabled(!no_display && !read_only);
-       scaleED.setText(QString::number(scale));
-}
-
-
-static external::DisplayType display(QCheckBox const & displayCB,
-       QComboBox const & showCO)
-{
-       if (!displayCB.isChecked())
-               return external::NoDisplay;
-       switch (showCO.currentIndex()) {
-       default:
-       case 0:
-               return external::DefaultDisplay;
-       case 1:
-               return external::MonochromeDisplay;
-       case 2:
-               return external::GrayscaleDisplay;
-       case 3:
-               return external::ColorDisplay;
-       case 4:
-               return external::PreviewDisplay;
-       }
-}
-
-
 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
        external::RotationData const & data)
 {
        originCO.setCurrentIndex(int(data.origin()));
-       angleED.setText(toqstr(data.angle));
+       doubleToWidget(&angleED, data.angle);
 }
 
 
@@ -444,11 +367,11 @@ static void getRotation(external::RotationData & data,
        typedef external::RotationData::OriginType OriginType;
 
        data.origin(static_cast<OriginType>(originCO.currentIndex()));
-       data.angle = fromqstr(angleED.text());
+       data.angle = widgetToDoubleStr(&angleED);
 }
 
 
-static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
+static void setSize(QLineEdit & widthED, LengthCombo & widthUnitCO,
        QLineEdit & heightED, LengthCombo & heightUnitCO,
        QCheckBox & aspectratioCB,
        external::ResizeData const & data)
@@ -462,19 +385,15 @@ static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
        }
 
        if (using_scale) {
-               widthED.setText(toqstr(scale));
-               widthUnitCO.setCurrentIndex(0);
-       } else {
-               widthED.setText(QString::number(data.width.value()));
-               // Because 'Scale' is position 0...
-               // Note also that width cannot be zero here, so
-               // we don't need to worry about the default unit.
-               widthUnitCO.setCurrentIndex(data.width.unit() + 1);
-       }
+               doubleToWidget(&widthED, scale);
+               widthUnitCO.setCurrentItem("scale");
+       } else
+               lengthToWidgets(&widthED, &widthUnitCO,
+                               data.width.asString(), Length::defaultUnit());
 
        string const h = data.height.zero() ? string() : data.height.asString();
-       Length::UNIT default_unit = data.width.zero() ?
-               defaultUnit() : data.width.unit();
+       Length::UNIT const default_unit = data.width.zero() ?
+               Length::defaultUnit() : data.width.unit();
        lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
 
        heightED.setEnabled(!using_scale);
@@ -491,33 +410,17 @@ static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
 static void getSize(external::ResizeData & data,
        QLineEdit const & widthED, QComboBox const & widthUnitCO,
        QLineEdit const & heightED, LengthCombo const & heightUnitCO,
-       QCheckBox const & aspectratioCB)
+       QCheckBox const & aspectratioCB, bool const scaling)
 {
-       string const width = fromqstr(widthED.text());
-
-       if (widthUnitCO.currentIndex() > 0) {
-               // Subtract one, because scale is 0.
-               int const unit = widthUnitCO.currentIndex() - 1;
-
-               Length w;
-               if (isValidLength(width, &w))
-                       data.width = w;
-               else if (isStrDbl(width))
-                       data.width = Length(convert<double>(width),
-                                          static_cast<Length::UNIT>(unit));
-               else
-                       data.width = Length();
-
-               data.scale = string();
-
-       } else {
+       if (scaling) {
                // scaling instead of a width
-               data.scale = width;
+               data.scale = widgetToDoubleStr(&widthED);
                data.width = Length();
+       } else {
+               data.width = Length(widgetsToLength(&widthED, &widthUnitCO));
+               data.scale = string();
        }
-
        data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
-
        data.keepAspectRatio = aspectratioCB.isChecked();
 }
 
@@ -556,13 +459,11 @@ static void getCrop(external::ClipData & data,
 
 void GuiExternal::updateContents()
 {
-       tab->setCurrentIndex(0);
-
        string const name =
                params_.filename.outputFilename(fromqstr(bufferFilepath()));
        fileED->setText(toqstr(name));
 
-       int index = -1;
+       int index = 0;
        external::TemplateManager::Templates::const_iterator i1, i2;
        i1 = external::TemplateManager::get().getTemplates().begin();
        i2 = external::TemplateManager::get().getTemplates().end();
@@ -578,8 +479,14 @@ void GuiExternal::updateContents()
 
        draftCB->setChecked(params_.draft);
 
-       setDisplay(*displayCB, *showCO, *displayscaleED,
-                  params_.display, params_.lyxscale, isBufferReadonly());
+       displayGB->setChecked(params_.display);
+       displayscaleED->setText(QString::number(params_.lyxscale));
+       bool scaled = params_.display && !isBufferReadonly() &&
+                       (params_.preview_mode != PREVIEW_INSTANT);
+       displayscaleED->setEnabled(scaled);
+       scaleLA->setEnabled(scaled);
+       displayGB->setEnabled(lyxrc.display_graphics);
+
 
        setRotation(*angleED, *originCO, params_.rotationdata);
 
@@ -599,22 +506,33 @@ void GuiExternal::updateTemplate()
        externalTB->setPlainText(qt_(templ.helpText));
 
        // Ascertain which (if any) transformations the template supports
-       // and disable tabs hosting unsupported transforms.
+       // and disable tabs and Group Boxes hosting unsupported transforms.
        typedef vector<external::TransformID> 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;
-       tab->setTabEnabled(tab->indexOf(rotatetab), found);
+       rotationGB->setEnabled(found);
+
        found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
-       tab->setTabEnabled(tab->indexOf(scaletab), found);
+       scaleGB->setEnabled(found);
 
        found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
-       tab->setTabEnabled(tab->indexOf(croptab), found);
+       cropGB->setEnabled(found);
+
+       tab->setTabEnabled(tab->indexOf(sizetab),
+               rotationGB->isEnabled()
+               || scaleGB->isEnabled()
+               || cropGB->isEnabled());
 
        found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
-       tab->setTabEnabled(tab->indexOf(optionstab), found);
+       optionsGB->setEnabled(found);
+
+       bool scaled = displayGB->isChecked() && displayGB->isEnabled() &&
+                       !isBufferReadonly() && (templ.preview_mode != PREVIEW_INSTANT);
+       displayscaleED->setEnabled(scaled);
+       scaleLA->setEnabled(scaled);
 
        if (!found)
                return;
@@ -639,7 +557,7 @@ void GuiExternal::updateTemplate()
 
        bool const enabled = extraFormatCO->count()  > 0;
 
-       tab->setTabEnabled(tab->indexOf(optionstab), enabled);
+       optionsGB->setEnabled(enabled);
        extraED->setEnabled(enabled && !isBufferReadonly());
        extraFormatCO->setEnabled(enabled);
 
@@ -657,20 +575,20 @@ void GuiExternal::applyView()
 
        params_.draft = draftCB->isChecked();
        params_.lyxscale = displayscaleED->text().toInt();
-       params_.display = display(*displayCB, *showCO);
+       params_.display = displayGB->isChecked();
 
-       if (tab->isTabEnabled(tab->indexOf(rotatetab)))
+       if (rotationGB->isEnabled())
                getRotation(params_.rotationdata, *angleED, *originCO);
 
-       if (tab->isTabEnabled(tab->indexOf(scaletab)))
+       if (scaleGB->isEnabled())
                getSize(params_.resizedata, *widthED, *widthUnitCO,
-                       *heightED, *heightUnitCO, *aspectratioCB);
+                       *heightED, *heightUnitCO, *aspectratioCB, usingScale());
 
-       if (tab->isTabEnabled(tab->indexOf(croptab)))
+       if (cropGB->isEnabled())
                getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
                        *xrED, *ytED, bbChanged_);
 
-       if (tab->isTabEnabled(tab->indexOf(optionstab))) {
+       if (optionsGB->isEnabled()) {
                MapType::const_iterator it = extra_.begin();
                MapType::const_iterator const end = extra_.end();
                for (; it != end; ++it)
@@ -699,22 +617,15 @@ void GuiExternal::dispatchParams()
 }
 
 
-void GuiExternal::editExternal()
-{
-       applyView();
-       string const lfun = InsetExternal::params2string(params_, buffer());
-       dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
-}
-
-
-static string templateFilters(string const & template_name)
+static QStringList templateFilters(QString const & template_name)
 {
        /// Determine the template file extension
        external::TemplateManager const & etm =
                external::TemplateManager::get();
        external::Template const * const et_ptr =
-               etm.getTemplateByName(template_name);
-       return et_ptr ? et_ptr->fileRegExp : string();
+               etm.getTemplateByName(fromqstr(template_name));
+
+       return fileFilters(et_ptr ? toqstr(et_ptr->fileRegExp) : QString());
 }
 
 
@@ -723,8 +634,7 @@ QString GuiExternal::browse(QString const & input,
 {
        QString const title = qt_("Select external file");
        QString const bufpath = bufferFilepath();
-       FileFilterList const filter =
-               FileFilterList(from_utf8(templateFilters(fromqstr(template_name))));
+       QStringList const filter = templateFilters(template_name);
 
        QString const label1 = qt_("Documents|#o#O");
        QString const dir1 = toqstr(lyxrc.document_path);
@@ -739,4 +649,4 @@ Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
 } // namespace frontend
 } // namespace lyx
 
-#include "GuiExternal_moc.cpp"
+#include "moc_GuiExternal.cpp"