]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiRef.cpp
Make a string translatable
[lyx.git] / src / frontends / qt4 / GuiRef.cpp
index 141c67bc13733ac413da9d2c4c0e39eb0f0f94a6..6ef6be5b29e48c2d518ccb72a15311d0e16b075b 100644 (file)
 
 #include "GuiRef.h"
 
+#include "GuiApplication.h"
+
 #include "Buffer.h"
+#include "BufferParams.h"
 #include "BufferList.h"
 #include "FuncRequest.h"
 
@@ -48,6 +51,29 @@ GuiRef::GuiRef(GuiView & lv)
 
        at_ref_ = false;
 
+       // The filter bar
+       filter_ = new FancyLineEdit(this);
+       filter_->setButtonPixmap(FancyLineEdit::Right, getPixmap("images/", "editclear", "svgz,png"));
+       filter_->setButtonVisible(FancyLineEdit::Right, true);
+       filter_->setButtonToolTip(FancyLineEdit::Right, qt_("Clear text"));
+       filter_->setAutoHideButton(FancyLineEdit::Right, true);
+       filter_->setPlaceholderText(qt_("All available labels"));
+       filter_->setToolTip(qt_("Enter string to filter the list of available labels"));
+#if (QT_VERSION < 0x050000)
+       connect(filter_, SIGNAL(downPressed()),
+               refsTW, SLOT(setFocus()));
+#else
+       connect(filter_, &FancyLineEdit::downPressed,
+               refsTW, [=](){ focusAndHighlight(refsTW); });
+#endif
+
+       filterBarL->addWidget(filter_, 0);
+       findKeysLA->setBuddy(filter_);
+
+       sortingCO->addItem(qt_("By Occurrence"), "unsorted");
+       sortingCO->addItem(qt_("Alphabetically (Case-Insensitive)"), "nocase");
+       sortingCO->addItem(qt_("Alphabetically (Case-Sensitive)"), "case");
+
        refsTW->setColumnCount(1);
        refsTW->header()->setVisible(false);
 
@@ -59,11 +85,15 @@ GuiRef::GuiRef(GuiView & lv)
 
        connect(typeCO, SIGNAL(activated(int)),
                this, SLOT(changed_adaptor()));
+       connect(referenceED, SIGNAL(textChanged(QString)),
+               this, SLOT(refTextChanged(QString)));
        connect(referenceED, SIGNAL(textChanged(QString)),
                this, SLOT(changed_adaptor()));
-       connect(findLE, SIGNAL(textEdited(QString)), 
+       connect(filter_, SIGNAL(textEdited(QString)),
                this, SLOT(filterLabels()));
-       connect(csFindCB, SIGNAL(clicked()), 
+       connect(filter_, SIGNAL(rightButtonClicked()),
+               this, SLOT(resetFilter()));
+       connect(csFindCB, SIGNAL(clicked()),
                this, SLOT(filterLabels()));
        connect(nameED, SIGNAL(textChanged(QString)),
                this, SLOT(changed_adaptor()));
@@ -73,10 +103,8 @@ GuiRef::GuiRef(GuiView & lv)
                this, SLOT(selectionChanged()));
        connect(refsTW, SIGNAL(itemDoubleClicked(QTreeWidgetItem *, int)),
                this, SLOT(refSelected(QTreeWidgetItem *)));
-       connect(sortCB, SIGNAL(clicked()),
+       connect(sortingCO, SIGNAL(activated(int)),
                this, SLOT(sortToggled()));
-       connect(caseSensitiveCB, SIGNAL(clicked()),
-               this, SLOT(caseSensitiveToggled()));
        connect(groupCB, SIGNAL(clicked()),
                this, SLOT(groupToggled()));
        connect(gotoPB, SIGNAL(clicked()),
@@ -85,6 +113,14 @@ GuiRef::GuiRef(GuiView & lv)
                this, SLOT(updateClicked()));
        connect(bufferCO, SIGNAL(activated(int)),
                this, SLOT(updateClicked()));
+       connect(pluralCB, SIGNAL(clicked()),
+               this, SLOT(changed_adaptor()));
+       connect(capsCB, SIGNAL(clicked()),
+               this, SLOT(changed_adaptor()));
+       connect(noprefixCB, SIGNAL(clicked()),
+               this, SLOT(changed_adaptor()));
+
+       enableBoxes();
 
        bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
        bc().setOK(okPB);
@@ -94,6 +130,8 @@ GuiRef::GuiRef(GuiView & lv)
 
        restored_buffer_ = -1;
        active_buffer_ = -1;
+
+       setFocusProxy(filter_);
 }
 
 
@@ -106,15 +144,39 @@ void GuiRef::enableView(bool enable)
 }
 
 
+void GuiRef::enableBoxes()
+{
+       bool const isFormatted = 
+           (InsetRef::getName(typeCO->currentIndex()) == "formatted");
+       bool const isLabelOnly = 
+           (InsetRef::getName(typeCO->currentIndex()) == "labelonly");
+       bool const usingRefStyle = buffer().params().use_refstyle;
+       pluralCB->setEnabled(isFormatted && usingRefStyle);
+       capsCB->setEnabled(isFormatted && usingRefStyle);
+       noprefixCB->setEnabled(isLabelOnly);
+}
+
+
 void GuiRef::changed_adaptor()
 {
        changed();
+       enableBoxes();
 }
 
 
 void GuiRef::gotoClicked()
 {
+       // By setting last_reference_, we ensure that the reference
+       // to which we are going (or from which we are returning) is
+       // restored in the dialog. It's a bit of a hack, but it works,
+       // and no-one seems to have any better idea.
+       bool const toggled = 
+               last_reference_.isEmpty() || last_reference_.isNull();
+       if (toggled)
+               last_reference_ = referenceED->text();
        gotoRef();
+       if (toggled)
+               last_reference_.clear();
 }
 
 
@@ -157,6 +219,14 @@ void GuiRef::refHighlighted(QTreeWidgetItem * sel)
 }
 
 
+void GuiRef::refTextChanged(QString const & str)
+{
+       gotoPB->setEnabled(!str.isEmpty());
+       typeCO->setEnabled(!str.isEmpty());
+       typeLA->setEnabled(!str.isEmpty());
+}
+
+
 void GuiRef::refSelected(QTreeWidgetItem * sel)
 {
        if (isBufferReadonly())
@@ -180,13 +250,6 @@ void GuiRef::refSelected(QTreeWidgetItem * sel)
 
 
 void GuiRef::sortToggled()
-{
-       caseSensitiveCB->setEnabled(sortCB->isChecked());
-       redoRefs();
-}
-
-
-void GuiRef::caseSensitiveToggled()
 {
        redoRefs();
 }
@@ -241,7 +304,8 @@ void GuiRef::updateContents()
        nameL->setHidden(!nameAllowed());
 
        // restore type settings for new insets
-       if (params_["reference"].empty())
+       bool const new_inset = params_["reference"].empty();
+       if (new_inset)
                typeCO->setCurrentIndex(orig_type);
        else
                typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
@@ -249,9 +313,13 @@ void GuiRef::updateContents()
        if (!typeAllowed())
                typeCO->setCurrentIndex(0);
 
+       pluralCB->setChecked(params_["plural"] == "true");
+       capsCB->setChecked(params_["caps"] == "true");
+       noprefixCB->setChecked(params_["noprefix"] == "true");
+
        // insert buffer list
        bufferCO->clear();
-       FileNameList const & buffers = theBufferList().fileNames();
+       FileNameList const buffers(theBufferList().fileNames());
        for (FileNameList::const_iterator it = buffers.begin();
             it != buffers.end(); ++it) {
                bufferCO->addItem(toqstr(makeDisplayPath(it->absFileName())));
@@ -259,7 +327,7 @@ void GuiRef::updateContents()
 
        int const thebuffer = theBufferList().bufferNum(buffer().fileName());
        // restore the buffer combo setting for new insets
-       if (params_["reference"].empty() && restored_buffer_ != -1
+       if (new_inset && restored_buffer_ != -1
            && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_)
                bufferCO->setCurrentIndex(restored_buffer_);
        else {
@@ -271,7 +339,10 @@ void GuiRef::updateContents()
        active_buffer_ = thebuffer;
 
        updateRefs();
-       bc().setValid(false);
+       enableBoxes();
+       // Activate OK/Apply buttons if the users inserts a new ref
+       // and we have a valid pre-setting.
+       bc().setValid(isValid() && new_inset);
 }
 
 
@@ -282,7 +353,12 @@ void GuiRef::applyView()
        params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
        params_["reference"] = qstring_to_ucs4(last_reference_);
        params_["name"] = qstring_to_ucs4(nameED->text());
-
+       params_["plural"] = pluralCB->isChecked() ? 
+             from_ascii("true") : from_ascii("false");
+       params_["caps"] = capsCB->isChecked() ? 
+             from_ascii("true") : from_ascii("false");
+       params_["noprefix"] = noprefixCB->isChecked() ? 
+             from_ascii("true") : from_ascii("false");
        restored_buffer_ = bufferCO->currentIndex();
 }
 
@@ -303,16 +379,14 @@ bool GuiRef::typeAllowed()
 void GuiRef::setGoBack()
 {
        gotoPB->setText(qt_("&Go Back"));
-       gotoPB->setToolTip("");
-       gotoPB->setToolTip(qt_("Jump back"));
+       gotoPB->setToolTip(qt_("Jump back to the original cursor location"));
 }
 
 
 void GuiRef::setGotoRef()
 {
        gotoPB->setText(qt_("&Go to Label"));
-       gotoPB->setToolTip("");
-       gotoPB->setToolTip(qt_("Jump to label"));
+       gotoPB->setToolTip(qt_("Jump to the selected label"));
 }
 
 
@@ -379,14 +453,15 @@ void GuiRef::redoRefs()
        if (noprefix)
                refsCategories.insert(0, qt_("<No prefix>"));
 
-       if (sortCB->isEnabled() && sortCB->isChecked()) {
-               if(caseSensitiveCB->isEnabled() && caseSensitiveCB->isChecked())
-                       qSort(refsStrings.begin(), refsStrings.end());
-               else
-                       qSort(refsStrings.begin(), refsStrings.end(),
-                             caseInsensitiveLessThan /*defined above*/);
-       }
-       
+       QString const sort = sortingCO->isEnabled() ?
+                               sortingCO->itemData(sortingCO->currentIndex()).toString()
+                               : QString();
+       if (sort == "nocase")
+               qSort(refsStrings.begin(), refsStrings.end(),
+                     caseInsensitiveLessThan /*defined above*/);
+       else if (sort == "case")
+               qSort(refsStrings.begin(), refsStrings.end());
+
        if (groupCB->isChecked()) {
                QList<QTreeWidgetItem *> refsCats;
                for (int i = 0; i < refsCategories.size(); ++i) {
@@ -417,19 +492,18 @@ void GuiRef::redoRefs()
                refsTW->addTopLevelItems(refsItems);
        }
 
-       referenceED->setText(oldSelection);
-
        // restore the last selection or, for new insets, highlight
        // the previous selection
        if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
                bool const newInset = oldSelection.isEmpty();
                QString textToFind = newInset ? last_reference_ : oldSelection;
+               referenceED->setText(textToFind);
                last_reference_.clear();
                QTreeWidgetItemIterator it(refsTW);
                while (*it) {
                        if ((*it)->text(0) == textToFind) {
-                               refsTW->setCurrentItem(*it);
-                               refsTW->setItemSelected(*it, !newInset);
+                               refsTW->setCurrentItem(*it);
+                               refsTW->setItemSelected(*it, true);
                                //Make sure selected item is visible
                                refsTW->scrollToItem(*it);
                                last_reference_ = textToFind;
@@ -447,6 +521,10 @@ void GuiRef::redoRefs()
        // Re-activate the emission of signals by these widgets.
        refsTW->blockSignals(false);
        referenceED->blockSignals(false);
+
+       gotoPB->setEnabled(!referenceED->text().isEmpty());
+       typeCO->setEnabled(!referenceED->text().isEmpty());
+       typeLA->setEnabled(!referenceED->text().isEmpty());
 }
 
 
@@ -455,17 +533,14 @@ void GuiRef::updateRefs()
        refs_.clear();
        int const the_buffer = bufferCO->currentIndex();
        if (the_buffer != -1) {
-               FileName const & name = theBufferList().fileNames()[the_buffer];
+               FileNameList const names(theBufferList().fileNames());
+               FileName const & name = names[the_buffer];
                Buffer const * buf = theBufferList().getBuffer(name);
                buf->getLabelList(refs_);
        }
-       sortCB->setEnabled(!refs_.empty());
-       caseSensitiveCB->setEnabled(sortCB->isEnabled() && sortCB->isChecked());
+       sortingCO->setEnabled(!refs_.empty());
        refsTW->setEnabled(!refs_.empty());
        groupCB->setEnabled(!refs_.empty());
-       // refsTW should only be the focus proxy when it is enabled
-       setFocusProxy(refs_.empty() ? 0 : refsTW);
-       gotoPB->setEnabled(!refs_.empty());
        redoRefs();
 }
 
@@ -497,13 +572,20 @@ void GuiRef::filterLabels()
        while (*it) {
                (*it)->setHidden(
                        (*it)->childCount() == 0
-                       && !(*it)->text(0).contains(findLE->text(), cs)
+                       && !(*it)->text(0).contains(filter_->text(), cs)
                );
                ++it;
        }
 }
 
 
+void GuiRef::resetFilter()
+{
+       filter_->setText(QString());
+       filterLabels();
+}
+
+
 bool GuiRef::initialiseParams(std::string const & data)
 {
        InsetCommand::string2params(data, params_);