]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiRef.cpp
Cosmetics.
[lyx.git] / src / frontends / qt4 / GuiRef.cpp
index 91979cb0c0737fafa6b91e2fe0b67ba39f0662a5..d344ab64faaa77255f82ef56983b4a238cb9e2c5 100644 (file)
@@ -4,7 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author John Levon
- * \author Jürgen Spitzmüller
+ * \author Jürgen Spitzmüller
  *
  * Full author contact details are available in file CREDITS.
  */
 
 #include "GuiRef.h"
 
-#include "ControlRef.h"
+#include "Buffer.h"
+#include "BufferList.h"
+#include "FuncRequest.h"
+
 #include "qt_helpers.h"
 
 #include "insets/InsetRef.h"
 
+#include "support/FileName.h"
+#include "support/FileNameList.h"
+#include "support/filetools.h" // makeAbsPath, makeDisplayPath
+
 #include <QLineEdit>
 #include <QCheckBox>
 #include <QListWidget>
 #include <QToolTip>
 #include <QCloseEvent>
 
-
-using std::vector;
-using std::string;
-
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 namespace frontend {
 
-GuiRefDialog::GuiRefDialog(LyXView & lv)
-       : GuiDialog(lv, "ref")
+GuiRef::GuiRef(GuiView & lv)
+       : GuiDialog(lv, "ref", qt_("Cross-reference")),
+         params_(insetCode("ref"))
 {
        setupUi(this);
-       setController(new ControlRef(*this));
-       setViewTitle(_("Cross-reference"));
 
-       sort_ = false;
        at_ref_ = false;
 
+       //FIXME: when/if we support the xr package for cross-reference
+       //between independant files. Those can be re-enabled.
+       refsL->setEnabled(false);
+       refsL->hide();
+       bufferCO->setEnabled(false);
+       bufferCO->hide();
+
+       // Enabling is set in updateRefs. Disable for now in case no
+       // call to updateContents follows (e.g. read-only documents).
+       sortCB->setEnabled(false);
+       refsLW->setEnabled(false);
+       gotoPB->setEnabled(false);
+
        connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
        connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
        connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
        connect(closePB, SIGNAL(clicked()), this, SLOT(reset_dialog()));
-       connect(this, SIGNAL(rejected()), this, SLOT(reset_dialog()));
+       connect(this, SIGNAL(rejected()), this, SLOT(dialog_rejected()));
 
        connect(typeCO, SIGNAL(activated(int)),
                this, SLOT(changed_adaptor()));
-       connect(referenceED, SIGNAL(textChanged(const QString &)),
+       connect(referenceED, SIGNAL(textChanged(QString)),
                this, SLOT(changed_adaptor()));
-       connect(nameED, SIGNAL(textChanged(const QString &)),
+       connect(nameED, SIGNAL(textChanged(QString)),
                this, SLOT(changed_adaptor()));
        connect(refsLW, SIGNAL(itemClicked(QListWidgetItem *)),
                this, SLOT(refHighlighted(QListWidgetItem *)));
@@ -62,8 +78,8 @@ GuiRefDialog::GuiRefDialog(LyXView & lv)
                this, SLOT(selectionChanged()));
        connect(refsLW, SIGNAL(itemActivated(QListWidgetItem *)),
                this, SLOT(refSelected(QListWidgetItem *)));
-       connect(sortCB, SIGNAL(clicked(bool)),
-               this, SLOT(sortToggled(bool)));
+       connect(sortCB, SIGNAL(clicked()),
+               this, SLOT(sortToggled()));
        connect(gotoPB, SIGNAL(clicked()),
                this, SLOT(gotoClicked()));
        connect(updatePB, SIGNAL(clicked()),
@@ -71,8 +87,6 @@ GuiRefDialog::GuiRefDialog(LyXView & lv)
        connect(bufferCO, SIGNAL(activated(int)),
                this, SLOT(updateClicked()));
 
-       setFocusProxy(refsLW);
-
        bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
        bc().setOK(okPB);
        bc().setApply(applyPB);
@@ -85,29 +99,25 @@ GuiRefDialog::GuiRefDialog(LyXView & lv)
        bc().addReadOnly(bufferCO);
 
        restored_buffer_ = -1;
+       active_buffer_ = -1;
 }
 
 
-ControlRef & GuiRefDialog::controller() const
-{
-       return static_cast<ControlRef &>(Dialog::controller());
-}
-
-
-void GuiRefDialog::changed_adaptor()
+void GuiRef::changed_adaptor()
 {
        changed();
 }
 
 
-void GuiRefDialog::gotoClicked()
+void GuiRef::gotoClicked()
 {
        gotoRef();
 }
 
-void GuiRefDialog::selectionChanged()
+
+void GuiRef::selectionChanged()
 {
-       if (readOnly())
+       if (isBufferReadonly())
                return;
 
        QList<QListWidgetItem *> selections = refsLW->selectedItems();
@@ -119,9 +129,9 @@ void GuiRefDialog::selectionChanged()
 }
 
 
-void GuiRefDialog::refHighlighted(QListWidgetItem * sel)
+void GuiRef::refHighlighted(QListWidgetItem * sel)
 {
-       if (readOnly())
+       if (isBufferReadonly())
                return;
 
 /*     int const cur_item = refsLW->currentRow();
@@ -142,9 +152,9 @@ void GuiRefDialog::refHighlighted(QListWidgetItem * sel)
 }
 
 
-void GuiRefDialog::refSelected(QListWidgetItem * sel)
+void GuiRef::refSelected(QListWidgetItem * sel)
 {
-       if (readOnly())
+       if (isBufferReadonly())
                return;
 
 /*     int const cur_item = refsLW->currentRow();
@@ -159,105 +169,112 @@ void GuiRefDialog::refSelected(QListWidgetItem * sel)
 }
 
 
-void GuiRefDialog::sortToggled(bool on)
+void GuiRef::sortToggled()
 {
-       sort_ = on;
        redoRefs();
 }
 
 
-void GuiRefDialog::updateClicked()
+void GuiRef::updateClicked()
 {
        updateRefs();
 }
 
 
-void GuiRefDialog::reset_dialog()
+void GuiRef::dialog_rejected()
+{
+       reset_dialog();
+       // We have to do this manually, instead of calling slotClose(), because
+       // the dialog has already been made invisible before rejected() triggers.
+       Dialog::disconnect();
+}
+
+
+void GuiRef::reset_dialog()
 {
        at_ref_ = false;
        setGotoRef();
 }
 
 
-void GuiRefDialog::closeEvent(QCloseEvent * e)
+void GuiRef::closeEvent(QCloseEvent * e)
 {
-       slotWMHide();
+       slotClose();
        reset_dialog();
        e->accept();
 }
 
 
-void GuiRefDialog::update_contents()
+void GuiRef::updateContents()
 {
-       InsetCommandParams const & params = controller().params();
-
        int orig_type = typeCO->currentIndex();
 
-       referenceED->setText(toqstr(params["reference"]));
+       referenceED->setText(toqstr(params_["reference"]));
 
-       nameED->setText(toqstr(params["name"]));
-       nameED->setReadOnly(!nameAllowed() && !readOnly());
+       nameED->setText(toqstr(params_["name"]));
+       nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
 
        // restore type settings for new insets
-       if (params["reference"].empty())
+       if (params_["reference"].empty())
                typeCO->setCurrentIndex(orig_type);
        else
-               typeCO->setCurrentIndex(InsetRef::getType(params.getCmdName()));
-       typeCO->setEnabled(typeAllowed() && !readOnly());
+               typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
+       typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
        if (!typeAllowed())
                typeCO->setCurrentIndex(0);
 
-       sortCB->setChecked(sort_);
-
        // insert buffer list
        bufferCO->clear();
-       vector<string> const buffers = controller().getBufferList();
-       for (vector<string>::const_iterator it = buffers.begin();
-               it != buffers.end(); ++it) {
-               bufferCO->addItem(toqstr(*it));
+       FileNameList const & buffers = theBufferList().fileNames();
+       for (FileNameList::const_iterator it = buffers.begin();
+            it != buffers.end(); ++it) {
+               bufferCO->addItem(toqstr(makeDisplayPath(it->absFilename())));
        }
+
+       int const thebuffer = theBufferList().bufferNum(buffer().fileName());
        // restore the buffer combo setting for new insets
-       if (params["reference"].empty() && restored_buffer_ != -1
-       && restored_buffer_ < bufferCO->count())
+       if (params_["reference"].empty() && restored_buffer_ != -1
+           && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_)
                bufferCO->setCurrentIndex(restored_buffer_);
-       else
-               bufferCO->setCurrentIndex(controller().getBufferNum());
+       else {
+               int const num = theBufferList().bufferNum(buffer().fileName());
+               bufferCO->setCurrentIndex(num);
+               if (thebuffer != active_buffer_)
+                       restored_buffer_ = num;
+       }
+       active_buffer_ = thebuffer;
 
        updateRefs();
        bc().setValid(false);
 }
 
 
-void GuiRefDialog::applyView()
+void GuiRef::applyView()
 {
-       InsetCommandParams & params = controller().params();
-
        last_reference_ = referenceED->text();
 
-       params.setCmdName(InsetRef::getName(typeCO->currentIndex()));
-       params["reference"] = qstring_to_ucs4(last_reference_);
-       params["name"] = qstring_to_ucs4(nameED->text());
+       params_.setCmdName(InsetRef::getName(typeCO->currentIndex()));
+       params_["reference"] = qstring_to_ucs4(last_reference_);
+       params_["name"] = qstring_to_ucs4(nameED->text());
 
        restored_buffer_ = bufferCO->currentIndex();
 }
 
 
-bool GuiRefDialog::nameAllowed()
+bool GuiRef::nameAllowed()
 {
-       Kernel::DocType const doc_type = kernel().docType();
-       return doc_type != Kernel::LATEX &&
-               doc_type != Kernel::LITERATE;
+       KernelDocType const doc_type = docType();
+       return doc_type != LATEX && doc_type != LITERATE;
 }
 
 
-bool GuiRefDialog::typeAllowed()
+bool GuiRef::typeAllowed()
 {
-       Kernel::DocType const doc_type = kernel().docType();
-       return doc_type != Kernel::DOCBOOK;
+       return docType() != DOCBOOK;
 }
 
 
-void GuiRefDialog::setGoBack()
+void GuiRef::setGoBack()
 {
        gotoPB->setText(qt_("&Go Back"));
        gotoPB->setToolTip("");
@@ -265,7 +282,7 @@ void GuiRefDialog::setGoBack()
 }
 
 
-void GuiRefDialog::setGotoRef()
+void GuiRef::setGotoRef()
 {
        gotoPB->setText(qt_("&Go to Label"));
        gotoPB->setToolTip("");
@@ -273,24 +290,24 @@ void GuiRefDialog::setGotoRef()
 }
 
 
-void GuiRefDialog::gotoRef()
+void GuiRef::gotoRef()
 {
-       string ref(fromqstr(referenceED->text()));
+       string ref = fromqstr(referenceED->text());
 
        if (at_ref_) {
                // go back
                setGotoRef();
-               controller().gotoBookmark();
+               gotoBookmark();
        } else {
                // go to the ref
                setGoBack();
-               controller().gotoRef(ref);
+               gotoRef(ref);
        }
        at_ref_ = !at_ref_;
 }
 
 
-void GuiRefDialog::redoRefs()
+void GuiRef::redoRefs()
 {
        // Prevent these widgets from emitting any signals whilst
        // we modify their state.
@@ -304,12 +321,12 @@ void GuiRefDialog::redoRefs()
        // the first item inserted
        QString const oldSelection(referenceED->text());
 
-       for (std::vector<docstring>::const_iterator iter = refs_.begin();
+       for (vector<docstring>::const_iterator iter = refs_.begin();
                iter != refs_.end(); ++iter) {
                refsLW->addItem(toqstr(*iter));
        }
 
-       if (sort_)
+       if (sortCB->isEnabled() && sortCB->isChecked())
                refsLW->sortItems();
 
        referenceED->setText(oldSelection);
@@ -319,20 +336,18 @@ void GuiRefDialog::redoRefs()
        if (!oldSelection.isEmpty() || !last_reference_.isEmpty()) {
                bool const newInset = oldSelection.isEmpty();
                QString textToFind = newInset ? last_reference_ : oldSelection;
-               bool foundItem = false;
-               for (int i = 0; !foundItem && i < refsLW->count(); ++i) {
+               last_reference_.clear();
+               for (int i = 0; i != refsLW->count(); ++i) {
                        QListWidgetItem * item = refsLW->item(i);
                        if (textToFind == item->text()) {
                                refsLW->setCurrentItem(item);
                                refsLW->setItemSelected(item, !newInset);
                                //Make sure selected item is visible
                                refsLW->scrollToItem(item);
-                               foundItem = true;
+                               last_reference_ = textToFind;
+                               break;
                        }
                }
-               if (foundItem)
-                       last_reference_ = textToFind;
-               else last_reference_ = "";
        }
        refsLW->setUpdatesEnabled(true);
        refsLW->update();
@@ -343,24 +358,62 @@ void GuiRefDialog::redoRefs()
 }
 
 
-void GuiRefDialog::updateRefs()
+void GuiRef::updateRefs()
 {
        refs_.clear();
-       string const name = controller().getBufferName(bufferCO->currentIndex());
-       refs_ = controller().getLabelList(name);
+       int const the_buffer = bufferCO->currentIndex();
+       if (the_buffer != -1) {
+               FileName const & name = theBufferList().fileNames()[the_buffer];
+               Buffer const * buf = theBufferList().getBuffer(name);
+               buf->getLabelList(refs_);
+       }       
        sortCB->setEnabled(!refs_.empty());
        refsLW->setEnabled(!refs_.empty());
+       // refsLW should only be the focus proxy when it is enabled
+       setFocusProxy(refs_.empty() ? 0 : refsLW);
        gotoPB->setEnabled(!refs_.empty());
        redoRefs();
 }
 
 
-bool GuiRefDialog::isValid()
+bool GuiRef::isValid()
 {
        return !referenceED->text().isEmpty();
 }
 
+
+void GuiRef::gotoRef(string const & ref)
+{
+       dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
+       dispatch(FuncRequest(LFUN_LABEL_GOTO, ref));
+}
+
+
+void GuiRef::gotoBookmark()
+{
+       dispatch(FuncRequest(LFUN_BOOKMARK_GOTO, "0"));
+}
+
+
+bool GuiRef::initialiseParams(std::string const & data)
+{
+       InsetCommand::string2params("ref", data, params_);
+       return true;
+}
+
+
+void GuiRef::dispatchParams()
+{
+       std::string const lfun = InsetCommand::params2string("ref", params_);
+       dispatch(FuncRequest(getLfun(), lfun));
+}
+
+
+
+Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
+
+
 } // namespace frontend
 } // namespace lyx
 
-#include "GuiRef_moc.cpp"
+#include "moc_GuiRef.cpp"