Abdelrazak Younes a écrit :
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?

You're a lucky guy, I have one, use "kernel().bufferview()->cursor()".

Thanks for the hint, here is an updated patch. It works well, try it :-)

OK to commit?

Abdel.

Index: src/frontends/controllers/ControlToc.C
===================================================================
--- src/frontends/controllers/ControlToc.C      (revision 13692)
+++ src/frontends/controllers/ControlToc.C      (working copy)
@@ -15,6 +15,7 @@
 #include "ControlToc.h"
 #include "funcrequest.h"
 #include "gettext.h"
+#include "BufferView.h"
 
 using std::vector;
 using std::string;
@@ -57,6 +58,17 @@
 }
 
 
+toc::TocItem const ControlToc::getCurrentTocItem(
+       string const & type) const
+{
+       BufferView const * const bv = kernel().bufferview();
+       if (!bv)
+               return toc::TocItem(-1, -1, "");
+
+       return toc::getCurrentTocItem(kernel().buffer(), bv->cursor(), 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,8 @@
 #include "funcrequest.h"
 #include "LyXAction.h"
 #include "paragraph.h"
+#include "cursor.h"
+#include "debug.h"
 
 #include "frontends/LyXView.h"
 
@@ -28,6 +30,8 @@
 
 #include "support/convert.h"
 
+#include <algorithm>
+
 using std::vector;
 using std::max;
 using std::ostream;
@@ -136,6 +140,39 @@
 }
 
 
+TocItem const getCurrentTocItem(Buffer const & buf, LCursor const & cur,
+                                                               std::string 
const & type)
+{
+       TocList tmp = toc::getTocList(buf);
+       TocList::iterator toclist_it = tmp.find(type);
+       if (toclist_it == tmp.end())
+               return TocItem(-1, -1, "");
+
+       Toc const toc_vector = toclist_it->second;
+
+       ParConstIterator const current(cur);
+
+       for (int i = 0; i != int(toc_vector.size()); ++i) {
+               
+               ParConstIterator const it 
+                       = buf.getParFromID(toc_vector[i].id_);
+
+               // A good solution would be to do:
+               //
+               //if (std::distance(current, it) < 0)
+               //      return toc_vector[i-1];
+               //
+               // But for an unknown reason, std::distance(current, it) always
+               // returns  a positive value... So for now, we do:              
+               if (it.pit() > current.pit())   
+                       return toc_vector[std::max(0, i-1)];
+       }
+
+       // This is dead code in principle:
+       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)
@@ -26,6 +26,7 @@
 class LyXView;
 class Paragraph;
 class FuncRequest;
+class LCursor;
 
 namespace lyx {
 namespace toc {
@@ -60,6 +61,10 @@
 ///
 std::vector<std::string> const getTypes(Buffer const &);
 
+/// Return the first TocItem before the cursor
+TocItem const getCurrentTocItem(Buffer const &, LCursor const &,
+                                                               std::string 
const & type);
+
 ///
 void asciiTocList(std::string const &, Buffer const &, std::ostream &);
 

Reply via email to