Rob Oakes wrote:
> Dear LyX Developers,
> 
> I am writing to request a bit of code review.  For the past week, or so,
> I have been working on the TocModel in LyX so that I can get it to
> support more than a single column.  This modification is important for
> the specialized views that I would like to create.  (For more
> information on what I'm trying to accomplish, take a look at this
> webpage: http://blog.oak-tree.us/index.php/2009/03/04/perfect-tool).
> 

Recently I read your article, nice idea!

Attached a patch which applies also against trunk.

I've tried to see your changes in action but somehow the TOC widget doesn't
appear any more (also without your patches). Seems trunk is broken.

Peter
Index: src/insets/InsetText.cpp
===================================================================
--- src/insets/InsetText.cpp    (Revision 34489)
+++ src/insets/InsetText.cpp    (Arbeitskopie)
@@ -673,9 +673,9 @@
 }
 
 
-
 void InsetText::addToToc(DocIterator const & cdit)
 {
+       TocItem paragraph_item;
        DocIterator dit = cdit;
        dit.push_back(CursorSlice(*this));
        Toc & toc = buffer().tocBackend().toc("tableofcontents");
@@ -692,13 +692,37 @@
                dit.pit() = pit;
                // the string that goes to the toc (could be the optarg)
                docstring tocstring;
+
+               // Create an Entry in the Table of Contents for the Paragraph
+               int const toclevel = par.layout().toclevel;
+               if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) 
{
+                       dit.pos() = 0;
+                       // insert this into the table of contents
+                       if (tocstring.empty())
+                               tocstring = par.asString(AS_STR_LABEL | 
AS_STR_INSETS);
+                       paragraph_item = TocItem(dit, toclevel - min_toclevel, 
tocstring, tocstring);
+               }
+
+               // Traverse the Paragraph Insets and allow them to be added to 
the
+               // appropriate toc list
                InsetList::const_iterator it  = par.insetList().begin();
                InsetList::const_iterator end = par.insetList().end();
                for (; it != end; ++it) {
                        Inset & inset = *it->inset;
                        dit.pos() = it->pos;
+
+                       // Check whether the inset is a note and add to 
paragraph_item
+                       if (inset.name()== from_ascii("Note:Note")){
+                               DocIterator pit = dit;
+                               pit.push_back(CursorSlice(inset));
+                               docstring str_summary = 
pit.paragraph().asString();
+                               
paragraph_item.summary_notes.push_back(TocItem(pit, toclevel - min_toclevel, 1,
+                                       str_summary, str_summary));
+                       }
+
                        //lyxerr << (void*)&inset << " code: " << 
inset.lyxCode() << std::endl;
                        inset.addToToc(dit);
+
                        switch (inset.lyxCode()) {
                        case OPTARG_CODE: {
                                if (!tocstring.empty())
@@ -715,16 +739,9 @@
                                break;
                        }
                }
-               // now the toc entry for the paragraph
-               int const toclevel = par.layout().toclevel;
-               if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) 
{
-                       dit.pos() = 0;
-                       // insert this into the table of contents
-                       if (tocstring.empty())
-                               tocstring = par.asString(AS_STR_LABEL | 
AS_STR_INSETS);
-                       toc.push_back(TocItem(dit, toclevel - min_toclevel,
-                               tocstring, tocstring));
-               }
+
+               if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel)
+                       toc.push_back(paragraph_item);
                
                // And now the list of changes.
                par.addChangesToToc(dit, buffer());
Index: src/TocBackend.cpp
===================================================================
--- src/TocBackend.cpp  (Revision 34489)
+++ src/TocBackend.cpp  (Arbeitskopie)
@@ -45,10 +45,14 @@
 ///////////////////////////////////////////////////////////////////////////
 
 TocItem::TocItem(DocIterator const & dit, int d, docstring const & s,
-       docstring const & t) : dit_(dit), depth_(d), str_(s), tooltip_(t)
+       docstring const & t) : dit_(dit), depth_(d), str_(s), tooltip_(t), 
column_(0)
 {
 }
 
+TocItem::TocItem(DocIterator const & dit, int d, int c, docstring const & s,
+       docstring const & t) : dit_(dit), depth_(d), str_(s), tooltip_(t), 
column_(c)
+{
+}
 
 int TocItem::id() const
 {
@@ -61,13 +65,16 @@
        return depth_;
 }
 
+int TocItem::modelColumn() const
+{
+       return column_;
+}
 
 docstring const & TocItem::str() const
 {
        return str_;
 }
 
-
 docstring const & TocItem::tooltip() const
 {
        return tooltip_;
@@ -93,7 +100,6 @@
        return FuncRequest(LFUN_PARAGRAPH_GOTO, arg);
 }
 
-
 ///////////////////////////////////////////////////////////////////////////
 //
 // TocBackend implementation
Index: src/frontends/qt4/TocWidget.cpp
===================================================================
--- src/frontends/qt4/TocWidget.cpp     (Revision 34489)
+++ src/frontends/qt4/TocWidget.cpp     (Arbeitskopie)
@@ -389,6 +389,8 @@
 {
        if (!gui_view_.documentBufferView()) {
                tocTV->setModel(0);
+               tocTV->showColumn(0);
+               tocTV->hideColumn(1);
                depthSL->setMaximum(0);
                depthSL->setValue(0);
                setEnabled(false);
@@ -405,6 +407,8 @@
                        gui_view_.tocModels().model(current_type_);
        if (tocTV->model() != toc_model) {
                tocTV->setModel(toc_model);
+               tocTV->showColumn(0);
+               tocTV->hideColumn(1);
                tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
                if (persistent_)
                        setTreeDepth(depth_);
Index: src/frontends/qt4/TocModel.cpp
===================================================================
--- src/frontends/qt4/TocModel.cpp      (Revision 34489)
+++ src/frontends/qt4/TocModel.cpp      (Arbeitskopie)
@@ -164,7 +164,7 @@
 
        model_->blockSignals(true);
        model_->beginResetModel();
-       model_->insertColumns(0, 1);
+       model_->insertColumns(0, 2);
        maxdepth_ = 0;
        mindepth_ = INT_MAX;
 
@@ -180,6 +180,15 @@
                model_->setData(top_level_item, index, Qt::UserRole);
                model_->setData(top_level_item, toqstr(item.tooltip()), 
Qt::ToolTipRole);
 
+               size_t notes_size = item.summary_notes.size();
+               if (notes_size != 0){
+                       QModelIndex summary_index = model_->index(current_row, 
1);
+                       TocItem const & summary_item = item.summary_notes[0];
+                       model_->setData(summary_index, 
toqstr(summary_item.str()), Qt::DisplayRole);
+                       model_->setData(summary_index, index, Qt::UserRole);
+                       model_->setData(summary_index, 
toqstr(summary_item.tooltip()), Qt::ToolTipRole);
+               }
+
                LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
                        << ", added item " << item.str());
 
@@ -203,7 +212,7 @@
 
        int current_row;
        QModelIndex child_item;
-       model_->insertColumns(0, 1, parent);
+       model_->insertColumns(0, 2, parent);
 
        size_t end = toc_->size();
        ++index;
@@ -221,6 +230,17 @@
                model_->setData(child_item, toqstr(item.str()), 
Qt::DisplayRole);
                model_->setData(child_item, index, Qt::UserRole);
                model_->setData(child_item, toqstr(item.tooltip()), 
Qt::ToolTipRole);
+
+               // Check for summary_notes
+               size_t notes_size = item.summary_notes.size();
+               if (notes_size != 0){
+                       QModelIndex summary_index = model_->index(current_row, 
1, parent);
+                       TocItem const & summary_item = item.summary_notes[0];
+                       model_->setData(summary_index, 
toqstr(summary_item.str()), Qt::DisplayRole);
+                       model_->setData(summary_index, index, Qt::UserRole);
+                       model_->setData(summary_index, 
toqstr(summary_item.tooltip()), Qt::ToolTipRole);
+               }
+
                populate(index, child_item);
                if (index >= end)
                        break;
Index: src/TocBackend.h
===================================================================
--- src/TocBackend.h    (Revision 34489)
+++ src/TocBackend.h    (Arbeitskopie)
@@ -45,6 +45,14 @@
                docstring const & s,
                docstring const & t = docstring()
                );
+
+       TocItem(DocIterator const & dit,
+               int depth,
+               int col,
+               docstring const & s,
+               docstring const & t = docstring()
+               );
+
        ///
        ~TocItem() {}
        ///
@@ -63,6 +71,14 @@
        /// the action corresponding to the goTo above
        FuncRequest action() const;
 
+       /// returns the model column that the item should add itself to
+       int modelColumn() const;
+
+       /// summary_notes_, vector of any note TocItems that
+       /// might be associated with a particular item.
+       std::vector<TocItem> summary_notes;
+
+
 protected:
        /// Current position of item.
        DocIterator dit_;
@@ -75,6 +91,10 @@
 
        /// The tooltip string
        docstring tooltip_;
+
+       /// model column, default = 0
+       int column_;
+
 };
 
 

Reply via email to