]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiRef.cpp
* fix spelling in comments to please John.
[lyx.git] / src / frontends / qt4 / GuiRef.cpp
index ab3a9710739ea488e719c0b2606e511134599a81..d98a507b371d61ece7744b14900f688276016624 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 <config.h>
 
 #include "GuiRef.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::GuiRefDialog(GuiRef * form)
-       : form_(form)
+GuiRef::GuiRef(GuiView & lv)
+       : GuiDialog(lv, "ref", qt_("Cross-reference")),
+         params_(insetCode("ref"))
 {
        setupUi(this);
 
-       connect(okPB, SIGNAL(clicked()), form_, SLOT(slotOK()));
-       connect(applyPB, SIGNAL(clicked()), form_, SLOT(slotApply()));
-       connect(closePB, SIGNAL(clicked()), form_, SLOT(slotClose()));
-       connect(closePB, SIGNAL(clicked()), this, SLOT(reset_dialog()));
-       connect(this, SIGNAL(rejected()), this, SLOT(reset_dialog()));
+       at_ref_ = false;
+
+       // Enabling is set in updateRefs. Disable for now in case no
+       // call to updateContents follows (e.g. read-only documents).
+       sortCB->setEnabled(false);
+       caseSensitiveCB->setEnabled(false);
+       caseSensitiveCB->setChecked(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(resetDialog()));
+       connect(this, SIGNAL(rejected()), this, SLOT(dialogRejected()));
 
        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 *)));
        connect(refsLW, SIGNAL(itemSelectionChanged()),
                this, SLOT(selectionChanged()));
-       connect(refsLW, SIGNAL(itemActivated(QListWidgetItem *)),
+       connect(refsLW, SIGNAL(itemDoubleClicked(QListWidgetItem *)),
                this, SLOT(refSelected(QListWidgetItem *)));
-       connect(sortCB, SIGNAL(clicked(bool)),
-               this, SLOT(sortToggled(bool)));
+       connect(sortCB, SIGNAL(clicked()),
+               this, SLOT(sortToggled()));
+       connect(caseSensitiveCB, SIGNAL(clicked()),
+               this, SLOT(caseSensitiveToggled()));
        connect(gotoPB, SIGNAL(clicked()),
                this, SLOT(gotoClicked()));
        connect(updatePB, SIGNAL(clicked()),
@@ -70,29 +84,38 @@ GuiRefDialog::GuiRefDialog(GuiRef * form)
        connect(bufferCO, SIGNAL(activated(int)),
                this, SLOT(updateClicked()));
 
-       setFocusProxy(refsLW);
-}
+       bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
+       bc().setOK(okPB);
+       bc().setApply(applyPB);
+       bc().setCancel(closePB);
+       bc().addReadOnly(refsLW);
+       bc().addReadOnly(sortCB);
+       bc().addReadOnly(caseSensitiveCB);
+       bc().addReadOnly(nameED);
+       bc().addReadOnly(referenceED);
+       bc().addReadOnly(typeCO);
+       bc().addReadOnly(bufferCO);
 
-void GuiRefDialog::show()
-{
-       QDialog::show();
+       restored_buffer_ = -1;
+       active_buffer_ = -1;
 }
 
 
-void GuiRefDialog::changed_adaptor()
+void GuiRef::changed_adaptor()
 {
-       form_->changed();
+       changed();
 }
 
 
-void GuiRefDialog::gotoClicked()
+void GuiRef::gotoClicked()
 {
-       form_->gotoRef();
+       gotoRef();
 }
 
-void GuiRefDialog::selectionChanged()
+
+void GuiRef::selectionChanged()
 {
-       if (form_->readOnly())
+       if (isBufferReadonly())
                return;
 
        QList<QListWidgetItem *> selections = refsLW->selectedItems();
@@ -104,9 +127,9 @@ void GuiRefDialog::selectionChanged()
 }
 
 
-void GuiRefDialog::refHighlighted(QListWidgetItem * sel)
+void GuiRef::refHighlighted(QListWidgetItem * sel)
 {
-       if (form_->readOnly())
+       if (isBufferReadonly())
                return;
 
 /*     int const cur_item = refsLW->currentRow();
@@ -117,19 +140,19 @@ void GuiRefDialog::refHighlighted(QListWidgetItem * sel)
        if (cur_item_selected)
                referenceED->setText(sel->text());
 
-       if (form_->at_ref_)
-               form_->gotoRef();
+       if (at_ref_)
+               gotoRef();
        gotoPB->setEnabled(true);
-       if (form_->typeAllowed())
+       if (typeAllowed())
                typeCO->setEnabled(true);
-       if (form_->nameAllowed())
+       if (nameAllowed())
                nameED->setEnabled(true);
 }
 
 
-void GuiRefDialog::refSelected(QListWidgetItem * sel)
+void GuiRef::refSelected(QListWidgetItem * sel)
 {
-       if (form_->readOnly())
+       if (isBufferReadonly())
                return;
 
 /*     int const cur_item = refsLW->currentRow();
@@ -140,242 +163,275 @@ void GuiRefDialog::refSelected(QListWidgetItem * sel)
        if (cur_item_selected)
                referenceED->setText(sel->text());
        // <enter> or double click, inserts ref and closes dialog
-       form_->slotOK();
+       slotOK();
 }
 
 
-void GuiRefDialog::sortToggled(bool on)
+void GuiRef::sortToggled()
 {
-       form_->sort_ = on;
-       form_->redoRefs();
+       caseSensitiveCB->setEnabled(sortCB->isChecked());
+       redoRefs();
 }
 
 
-void GuiRefDialog::updateClicked()
+void GuiRef::caseSensitiveToggled()
 {
-       form_->updateRefs();
+       redoRefs();
 }
 
 
-void GuiRefDialog::reset_dialog() {
-       form_->at_ref_ = false;
-       form_->setGotoRef();
+void GuiRef::updateClicked()
+{
+       updateRefs();
 }
 
 
-void GuiRefDialog::closeEvent(QCloseEvent * e)
+void GuiRef::dialogRejected()
 {
-       form_->slotWMHide();
-       reset_dialog();
-       e->accept();
+       resetDialog();
+       // We have to do this manually, instead of calling slotClose(), because
+       // the dialog has already been made invisible before rejected() triggers.
+       Dialog::disconnect();
 }
 
 
-/////////////////////////////////////////////////////////////////////
-//
-// GuiRef
-//
-/////////////////////////////////////////////////////////////////////
-
-
-GuiRef::GuiRef(GuiDialog & parent)
-       : GuiView<GuiRefDialog>(parent, _("Cross-reference")),
-       sort_(false), at_ref_(false)
+void GuiRef::resetDialog()
 {
+       at_ref_ = false;
+       setGotoRef();
 }
 
 
-void GuiRef::build_dialog()
+void GuiRef::closeEvent(QCloseEvent * e)
 {
-       dialog_.reset(new GuiRefDialog(this));
-
-       bc().setOK(dialog_->okPB);
-       bc().setApply(dialog_->applyPB);
-       bc().setCancel(dialog_->closePB);
-       bc().addReadOnly(dialog_->refsLW);
-       bc().addReadOnly(dialog_->sortCB);
-       bc().addReadOnly(dialog_->nameED);
-       bc().addReadOnly(dialog_->referenceED);
-       bc().addReadOnly(dialog_->typeCO);
-       bc().addReadOnly(dialog_->bufferCO);
-
-       restored_buffer_ = -1;
+       slotClose();
+       resetDialog();
+       e->accept();
 }
 
 
-void GuiRef::update_contents()
+void GuiRef::updateContents()
 {
-       InsetCommandParams const & params = controller().params();
-
-       int orig_type = dialog_->typeCO->currentIndex();
+       int orig_type = typeCO->currentIndex();
 
-       dialog_->referenceED->setText(toqstr(params["reference"]));
+       referenceED->setText(toqstr(params_["reference"]));
 
-       dialog_->nameED->setText(toqstr(params["name"]));
-       dialog_->nameED->setReadOnly(!nameAllowed() && !readOnly());
+       nameED->setText(toqstr(params_["name"]));
+       nameED->setReadOnly(!nameAllowed() && !isBufferReadonly());
 
        // restore type settings for new insets
-       if (params["reference"].empty())
-               dialog_->typeCO->setCurrentIndex(orig_type);
+       if (params_["reference"].empty())
+               typeCO->setCurrentIndex(orig_type);
        else
-               dialog_->typeCO->setCurrentIndex(InsetRef::getType(params.getCmdName()));
-       dialog_->typeCO->setEnabled(typeAllowed() && !readOnly());
+               typeCO->setCurrentIndex(InsetRef::getType(params_.getCmdName()));
+       typeCO->setEnabled(typeAllowed() && !isBufferReadonly());
        if (!typeAllowed())
-               dialog_->typeCO->setCurrentIndex(0);
-
-       dialog_->sortCB->setChecked(sort_);
+               typeCO->setCurrentIndex(0);
 
        // insert buffer list
-       dialog_->bufferCO->clear();
-       vector<string> const buffers = controller().getBufferList();
-       for (vector<string>::const_iterator it = buffers.begin();
-               it != buffers.end(); ++it) {
-               dialog_->bufferCO->addItem(toqstr(*it));
+       bufferCO->clear();
+       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_ < dialog_->bufferCO->count())
-               dialog_->bufferCO->setCurrentIndex(restored_buffer_);
-       else
-               dialog_->bufferCO->setCurrentIndex(controller().getBufferNum());
+       if (params_["reference"].empty() && restored_buffer_ != -1
+           && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_)
+               bufferCO->setCurrentIndex(restored_buffer_);
+       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 GuiRef::apply()
+void GuiRef::applyView()
 {
-       InsetCommandParams & params = controller().params();
+       last_reference_ = referenceED->text();
 
-       last_reference_ = dialog_->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(dialog_->typeCO->currentIndex()));
-       params["reference"] = qstring_to_ucs4(last_reference_);
-       params["name"] = qstring_to_ucs4(dialog_->nameED->text());
-
-       restored_buffer_ = dialog_->bufferCO->currentIndex();
+       restored_buffer_ = bufferCO->currentIndex();
 }
 
 
 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 GuiRef::typeAllowed()
 {
-       Kernel::DocType const doc_type = kernel().docType();
-       return doc_type != Kernel::DOCBOOK;
+       return docType() != DOCBOOK;
 }
 
 
 void GuiRef::setGoBack()
 {
-       dialog_->gotoPB->setText(qt_("&Go Back"));
-       dialog_->gotoPB->setToolTip("");
-       dialog_->gotoPB->setToolTip(qt_("Jump back"));
+       gotoPB->setText(qt_("&Go Back"));
+       gotoPB->setToolTip("");
+       gotoPB->setToolTip(qt_("Jump back"));
 }
 
 
 void GuiRef::setGotoRef()
 {
-       dialog_->gotoPB->setText(qt_("&Go to Label"));
-       dialog_->gotoPB->setToolTip("");
-       dialog_->gotoPB->setToolTip(qt_("Jump to label"));
+       gotoPB->setText(qt_("&Go to Label"));
+       gotoPB->setToolTip("");
+       gotoPB->setToolTip(qt_("Jump to label"));
 }
 
 
 void GuiRef::gotoRef()
 {
-       string ref(fromqstr(dialog_->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_;
 }
 
+inline bool caseInsensitiveLessThan(QString const & s1, QString const & s2)
+{
+       return s1.toLower() < s2.toLower();
+}
+
 
 void GuiRef::redoRefs()
 {
        // Prevent these widgets from emitting any signals whilst
        // we modify their state.
-       dialog_->refsLW->blockSignals(true);
-       dialog_->referenceED->blockSignals(true);
-       dialog_->refsLW->setUpdatesEnabled(false);
+       refsLW->blockSignals(true);
+       referenceED->blockSignals(true);
+       refsLW->setUpdatesEnabled(false);
 
-       dialog_->refsLW->clear();
+       refsLW->clear();
 
        // need this because Qt will send a highlight() here for
        // the first item inserted
-       QString const oldSelection(dialog_->referenceED->text());
-
-       for (std::vector<docstring>::const_iterator iter = refs_.begin();
-               iter != refs_.end(); ++iter) {
-               dialog_->refsLW->addItem(toqstr(*iter));
+       QString const oldSelection(referenceED->text());
+
+       QStringList refsStrings;
+       vector<docstring>::const_iterator iter;
+       for (iter = refs_.begin(); iter != refs_.end(); ++iter)
+               refsStrings.append(toqstr(*iter));
+
+       if (sortCB->isEnabled() && sortCB->isChecked()) {
+               if(caseSensitiveCB->isEnabled() && caseSensitiveCB->isChecked())
+                       qSort(refsStrings.begin(), refsStrings.end());
+               else
+                       qSort(refsStrings.begin(), refsStrings.end(),
+                             caseInsensitiveLessThan /*defined above*/);
        }
 
-       if (sort_)
-               dialog_->refsLW->sortItems();
+       refsLW->addItems(refsStrings);
 
-       dialog_->referenceED->setText(oldSelection);
+       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;
-               bool foundItem = false;
-               for (int i = 0; !foundItem && i < dialog_->refsLW->count(); ++i) {
-                       QListWidgetItem * item = dialog_->refsLW->item(i);
+               last_reference_.clear();
+               for (int i = 0; i != refsLW->count(); ++i) {
+                       QListWidgetItem * item = refsLW->item(i);
                        if (textToFind == item->text()) {
-                               dialog_->refsLW->setCurrentItem(item);
-                               dialog_->refsLW->setItemSelected(item, !newInset);
+                               refsLW->setCurrentItem(item);
+                               refsLW->setItemSelected(item, !newInset);
                                //Make sure selected item is visible
-                               dialog_->refsLW->scrollToItem(item);
-                               foundItem = true;
+                               refsLW->scrollToItem(item);
+                               last_reference_ = textToFind;
+                               break;
                        }
                }
-               if (foundItem)
-                       last_reference_ = textToFind;
-               else last_reference_ = "";
        }
-       dialog_->refsLW->setUpdatesEnabled(true);
-       dialog_->refsLW->update();
+       refsLW->setUpdatesEnabled(true);
+       refsLW->update();
 
        // Re-activate the emission of signals by these widgets.
-       dialog_->refsLW->blockSignals(false);
-       dialog_->referenceED->blockSignals(false);
+       refsLW->blockSignals(false);
+       referenceED->blockSignals(false);
 }
 
 
 void GuiRef::updateRefs()
 {
        refs_.clear();
-       string const name = controller().getBufferName(dialog_->bufferCO->currentIndex());
-       refs_ = controller().getLabelList(name);
-       dialog_->sortCB->setEnabled(!refs_.empty());
-       dialog_->refsLW->setEnabled(!refs_.empty());
-       dialog_->gotoPB->setEnabled(!refs_.empty());
+       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());
+       caseSensitiveCB->setEnabled(sortCB->isEnabled() && sortCB->isChecked());
+       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 GuiRef::isValid()
 {
-       return !dialog_->referenceED->text().isEmpty();
+       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"