* 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 "insets/InsetRef.h"
-#include "support/filetools.h" // MakeAbsPath, MakeDisplayPath
-
-#include <boost/filesystem/operations.hpp>
+#include "support/FileName.h"
+#include "support/FileNameList.h"
+#include "support/filetools.h" // makeAbsPath, makeDisplayPath
#include <QLineEdit>
#include <QCheckBox>
#include <QToolTip>
#include <QCloseEvent>
-#include <algorithm>
-
-using std::find;
-using std::vector;
-using std::string;
+using namespace std;
+using namespace lyx::support;
namespace lyx {
namespace frontend {
-using support::makeAbsPath;
-using support::makeDisplayPath;
-
-//FIXME It should be possible to eliminate lfun_name_
-//now and recover that information from params().insetType().
-//But let's not do that quite yet.
-/// Flags what action is taken by Kernel::dispatch()
-static std::string const lfun_name_ = "ref";
-
-GuiRef::GuiRef(LyXView & lv)
- : GuiDialog(lv, "ref"), Controller(*static_cast<Dialog*>(this)),
- params_("ref")
+GuiRef::GuiRef(GuiView & lv)
+ : GuiDialog(lv, "ref", qt_("Cross-reference")),
+ params_(insetCode("ref"))
{
setupUi(this);
- setController(this, false);
- setViewTitle(_("Cross-reference"));
- sort_ = false;
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(reset_dialog()));
- connect(this, SIGNAL(rejected()), this, SLOT(reset_dialog()));
+ 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()),
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);
restored_buffer_ = -1;
+ active_buffer_ = -1;
}
gotoRef();
}
+
void GuiRef::selectionChanged()
{
if (isBufferReadonly())
void GuiRef::refHighlighted(QListWidgetItem * sel)
{
- if (controller().isBufferReadonly())
+ if (isBufferReadonly())
return;
/* int const cur_item = refsLW->currentRow();
}
-void GuiRef::sortToggled(bool on)
+void GuiRef::sortToggled()
+{
+ caseSensitiveCB->setEnabled(sortCB->isChecked());
+ redoRefs();
+}
+
+
+void GuiRef::caseSensitiveToggled()
{
- sort_ = on;
redoRefs();
}
}
-void GuiRef::reset_dialog()
+void GuiRef::dialogRejected()
+{
+ resetDialog();
+ // 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::resetDialog()
{
at_ref_ = false;
setGotoRef();
void GuiRef::closeEvent(QCloseEvent * e)
{
slotClose();
- reset_dialog();
- GuiDialog::closeEvent(e);
+ resetDialog();
+ e->accept();
}
if (!typeAllowed())
typeCO->setCurrentIndex(0);
- sortCB->setChecked(sort_);
-
// insert buffer list
bufferCO->clear();
- vector<string> buffers = theBufferList().getFileNames();
- for (vector<string>::iterator it = buffers.begin();
+ FileNameList const & buffers = theBufferList().fileNames();
+ for (FileNameList::const_iterator it = buffers.begin();
it != buffers.end(); ++it) {
- bufferCO->addItem(toqstr(lyx::to_utf8(makeDisplayPath(*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())
+ && restored_buffer_ < bufferCO->count() && thebuffer == active_buffer_)
bufferCO->setCurrentIndex(restored_buffer_);
- else
- bufferCO->setCurrentIndex(bufferNum());
+ 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);
at_ref_ = !at_ref_;
}
+inline bool caseInsensitiveLessThan(QString const & s1, QString const & s2)
+{
+ return s1.toLower() < s2.toLower();
+}
+
void GuiRef::redoRefs()
{
// the first item inserted
QString const oldSelection(referenceED->text());
- for (std::vector<docstring>::const_iterator iter = refs_.begin();
- iter != refs_.end(); ++iter) {
- refsLW->addItem(toqstr(*iter));
+ 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_)
- refsLW->sortItems();
+ refsLW->addItems(refsStrings);
referenceED->setText(oldSelection);
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();
void GuiRef::updateRefs()
{
refs_.clear();
- string const name = theBufferList().getFileNames()[bufferCO->currentIndex()];
- Buffer const * buf = theBufferList().getBuffer(makeAbsPath(name).absFilename());
- buf->getLabelList(refs_);
+ 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();
}
}
-int GuiRef::bufferNum() const
-{
- vector<string> buffers = theBufferList().getFileNames();
- string const name = buffer().fileName();
- vector<string>::const_iterator cit =
- std::find(buffers.begin(), buffers.end(), name);
- if (cit == buffers.end())
- return 0;
- return int(cit - buffers.begin());
-}
-
-
-bool GuiRef::initialiseParams(string const & data)
+bool GuiRef::initialiseParams(std::string const & data)
{
- // The name passed with LFUN_INSET_APPLY is also the name
- // used to identify the mailer.
- InsetCommandMailer::string2params(lfun_name_, data, params_);
+ InsetCommand::string2params("ref", data, params_);
return true;
}
-void GuiRef::clearParams()
-{
- params_.clear();
-}
-
-
void GuiRef::dispatchParams()
{
- string const lfun = InsetCommandMailer::params2string(lfun_name_, params_);
+ std::string const lfun = InsetCommand::params2string("ref", params_);
dispatch(FuncRequest(getLfun(), lfun));
}
-Dialog * createGuiRef(LyXView & lv) { return new GuiRef(lv); }
+
+Dialog * createGuiRef(GuiView & lv) { return new GuiRef(lv); }
} // namespace frontend
} // namespace lyx
-#include "GuiRef_moc.cpp"
+#include "moc_GuiRef.cpp"