Abdelrazak Younes a écrit :
Have you ever been frustrated that when opening the Toc, you have to
look for the section where the cursor is?
This patch fixes this for the qt4 version (the qt2 version should not be
complicated to do). The non-qt4 changes are straight forward, OK to apply?
IMHO, the toc interface is very inefficient. We should transform that to
a proper class that contains the TocList. Later, I think the toc and
layout labelling support function should be merged in a single class.
Oups, I didn't intent to send this now... this doesn't work yet.
Here is the patch anyway. I have a problem to retrieve the
ParConstIterator that corresponds to the current location in
"Toc.C: getCurrentTocItem()". I tried to use "DocIterator(buf.inset())"
and "buf.getCursor().asDocIterator(&buf.inset())" but both interface
give a wrong pit() (something like 54934888). Any idea someone?
Abdel.
Index: src/frontends/controllers/ControlToc.C
===================================================================
--- src/frontends/controllers/ControlToc.C (revision 13692)
+++ src/frontends/controllers/ControlToc.C (working copy)
@@ -57,6 +57,13 @@
}
+toc::TocItem const ControlToc::getCurrentTocItem(
+ string const & type) const
+{
+ return toc::getCurrentTocItem(kernel().buffer(), type);
+}
+
+
string const ControlToc::getGuiName(string const & type) const
{
if (type == "TOC")
Index: src/frontends/controllers/ControlToc.h
===================================================================
--- src/frontends/controllers/ControlToc.h (revision 13692)
+++ src/frontends/controllers/ControlToc.h (working copy)
@@ -36,6 +36,10 @@
/// Return the guiname from a given cmdName of the TOC param
std::string const getGuiName(std::string const & type) const;
+ /// Return the first TocItem before the cursor
+ toc::TocItem const getCurrentTocItem(
+ std::string const & type) const;
+
/// Given a type, returns the contents
toc::Toc const getContents(std::string const & type) const;
Index: src/frontends/qt4/QToc.C
===================================================================
--- src/frontends/qt4/QToc.C (revision 13692)
+++ src/frontends/qt4/QToc.C (working copy)
@@ -73,6 +73,23 @@
}
+QModelIndex const QToc::getCurrentIndex()
+{
+ vector<string> const & types = getTypes();
+ toc::TocItem const item = getCurrentTocItem(types[type_]);
+ if (item.id_ == -1) {
+ lyxerr[Debug::GUI]
+ << "QToc::getCurrentIndex(): TocItem is invalid!" <<
endl;
+ return QModelIndex();
+ }
+
+ string toc_str = item.str;
+ toc_str.erase(0, toc_str.find(' ') + 1);
+
+ return toc_models_[type_]->index(toc_str);
+}
+
+
void QToc::goTo(QModelIndex const & index)
{
if (!index.isValid()) {
Index: src/frontends/qt4/QToc.h
===================================================================
--- src/frontends/qt4/QToc.h (revision 13692)
+++ src/frontends/qt4/QToc.h (working copy)
@@ -43,6 +43,9 @@
QStringListModel * typeModel()
{ return &type_model_; }
+ ///
+ QModelIndex const getCurrentIndex();
+ ///
void goTo(QModelIndex const & index);
void move(toc::OutlineOp const operation, QModelIndex & index);
Index: src/frontends/qt4/QTocDialog.C
===================================================================
--- src/frontends/qt4/QTocDialog.C (revision 13692)
+++ src/frontends/qt4/QTocDialog.C (working copy)
@@ -154,10 +154,14 @@
{
tocTV->setModel(form_->tocModel());
- if (index.isValid()) {
- tocTV->scrollTo(index);
- tocTV->selectionModel()->select(index,
QItemSelectionModel::Select);
+ if (!index.isValid()) {
+ lyxerr[Debug::GUI]
+ << "QTocDialog::select(): QModelIndex is invalid!" <<
endl;
+ return;
}
+
+ tocTV->scrollTo(index);
+ tocTV->selectionModel()->select(index, QItemSelectionModel::Select);
}
void QTocDialog::enableButtons(bool enable)
@@ -192,6 +196,8 @@
this, SLOT(selectionChanged(const QModelIndex &,
const QModelIndex &)));
+ select(form_->getCurrentIndex());
+
lyxerr[Debug::GUI]
<< "form_->tocModel()->rowCount " <<
form_->tocModel()->rowCount()
<< "\nform_->tocModel()->columnCount " <<
form_->tocModel()->columnCount()
Index: src/toc.C
===================================================================
--- src/toc.C (revision 13684)
+++ src/toc.C (working copy)
@@ -19,6 +19,7 @@
#include "funcrequest.h"
#include "LyXAction.h"
#include "paragraph.h"
+#include "debug.h"
#include "frontends/LyXView.h"
@@ -28,6 +29,11 @@
#include "support/convert.h"
+#include <algorithm>
+#include <iostream>
+using std::cout;
+using std::endl;
+
using std::vector;
using std::max;
using std::ostream;
@@ -136,6 +142,42 @@
}
+TocItem const getCurrentTocItem(Buffer const & buf,
+ std::string
const & type)
+{
+ TocList tmp = toc::getTocList(buf);
+ TocList::iterator toclist_it = tmp.find(type);
+ if (toclist_it == tmp.end()) {
+ cout << type << " not found" << endl;
+ return TocItem(-1, -1, "");
+ }
+
+ Toc const toc_vector = toclist_it->second;
+
+ ParConstIterator const current(DocIterator(buf.inset()));
+// buf.getCursor().asDocIterator(&buf.inset()));
+
+ cout << "current->pit() " << current.pit() << endl;
+
+ for (size_t i = 0; i != toc_vector.size(); ++i) {
+
+ ParConstIterator const it
+ = buf.getParFromID(toc_vector[i].id_);
+
+ cout << "i=" << i
+ << " it.pit() " << it.pit()
+ << " std::distance(current, it)=" <<
std::distance(current, it) << endl;
+
+ if (std::distance(current, it) < 0)
+ return toc_vector[i-1];
+ }
+
+ // This is dead code in principle:
+ cout << "TocItem not found i=" << toc_vector.size() << endl;
+ return toc_vector[0];
+}
+
+
vector<string> const getTypes(Buffer const & buffer)
{
vector<string> types;
Index: src/toc.h
===================================================================
--- src/toc.h (revision 13684)
+++ src/toc.h (working copy)
@@ -60,6 +60,10 @@
///
std::vector<std::string> const getTypes(Buffer const &);
+/// Return the first TocItem before the cursor
+TocItem const getCurrentTocItem(Buffer const &,
+ std::string
const & type);
+
///
void asciiTocList(std::string const &, Buffer const &, std::ostream &);