Abdelrazak Younes a écrit :
Juergen Spitzmueller a écrit :
Abdelrazak Younes wrote:
Please
don't wait too much time on this as you motivated me to do the mode/view
separation (see attached) ;-)

Nice, but please don't tell me that you removed the slider now.

Not in the GUI but it is not implemented yet (but should be easy to do). I am now almost done with the model/view separation. It was quite a bit of work and I am pretty please with it. There remains a problem though (a big one): the TreeView does not show anything. I have read the documentaion many times, tried a lot of things but it does not work :-(
But, the controller and the model work well.

Solved! :-)
The toc model needs at least one manual column insertion (I though it was done automatically on rows insertion).

As a side note, I've added "bool ControlToc::canOutline" to test if outline is possible. I'll probably commit this as the qt2 frontend will need it in order to avoid a crash when figure or table is selected.

For the rest, here is a status:
- slider depth: not yet implemented
- navigating: only with mouse-click for now, the connection to the selectionModel doesn't work...
- outline: still some crashes and tracking the moved paragraph is buggy.

Any opinion against me committing this?

Updated patch attached.

Side question about paragraph ids for Lars:

I wanted to use toc::TocItem::id_ but it is not stable after an outline operation. Is your new uid going to be stable for that Lars? I would like to use that instead of the paragraph text (toc::TocItem::str) which is not guaranteed to be unique).

Side question about outline for Martin:

The outline function takes no argument. It moves or transforms the section where the cursor is. I think it should instead take a pit or a uid so that we can use the outline feature without having any cursor. What do you think?

Abdel.

Index: src/frontends/controllers/ControlToc.C
===================================================================
--- src/frontends/controllers/ControlToc.C      (revision 13684)
+++ src/frontends/controllers/ControlToc.C      (working copy)
@@ -37,6 +37,12 @@
 }
 
 
+bool ControlToc::canOutline(string const & type)
+{
+       return type == "TOC";
+}
+
+
 void ControlToc::outline(toc::OutlineOp op)
 {
        std::ostringstream o;
Index: src/frontends/controllers/ControlToc.h
===================================================================
--- src/frontends/controllers/ControlToc.h      (revision 13684)
+++ src/frontends/controllers/ControlToc.h      (working copy)
@@ -41,6 +41,9 @@
 
        /// Apply the selected outlining operation
        void outline(toc::OutlineOp op);
+       
+       /// Test if outlining operation is possible
+       bool canOutline(std::string const & type);
 };
 
 } // namespace frontend
Index: src/frontends/qt4/Dialogs.C
===================================================================
--- src/frontends/qt4/Dialogs.C (revision 13687)
+++ src/frontends/qt4/Dialogs.C (working copy)
@@ -18,7 +18,6 @@
 #include "ControlBranch.h"
 #include "ControlChanges.h"
 #include "ControlCharacter.h"
-#include "ControlCitation.h"
 #include "ControlDocument.h"
 #include "ControlErrorList.h"
 #include "ControlERT.h"
@@ -40,7 +39,6 @@
 #include "ControlSpellchecker.h"
 #include "ControlTabular.h"
 #include "ControlTabularCreate.h"
-#include "ControlToc.h"
 #include "ControlVSpace.h"
 #include "ControlWrap.h"
 
@@ -83,6 +81,7 @@
 #include "QTabularCreate.h"
 #include "QTexinfo.h"
 #include "QToc.h"
+#include "QTocDialog.h"
 #include "q3url.h"
 #include "QVSpace.h"
 #include "QWrap.h"
@@ -310,8 +309,9 @@
                dialog->bc().bp(new OkApplyCancelReadOnlyPolicy);
 #endif
        } else if (name == "toc") {
-               dialog->setController(new ControlToc(*dialog));
-               dialog->setView(new QToc(*dialog));
+               QToc * qtoc = new QToc(*dialog);
+               dialog->setController(qtoc);
+               dialog->setView(new QTocDialog(*dialog, qtoc));
                dialog->bc().bp(new OkCancelPolicy);
        } else if (name == "url") {
                dialog->setController(new ControlCommand(*dialog, name));
Index: src/frontends/qt4/Makefile.dialogs
===================================================================
--- src/frontends/qt4/Makefile.dialogs  (revision 13687)
+++ src/frontends/qt4/Makefile.dialogs  (working copy)
@@ -123,6 +123,7 @@
        QTabularDialog.C QTabularDialog.h \
        QTexinfoDialog.C QTexinfoDialog.h \
        QThesaurusDialog.C QThesaurusDialog.h \
+       TocModel.C TocModel.h \
        QTocDialog.C QTocDialog.h \
        QtView.C QtView.h \
        QURLDialog.C QURLDialog.h \
Index: src/frontends/qt4/QToc.C
===================================================================
--- src/frontends/qt4/QToc.C    (revision 13687)
+++ src/frontends/qt4/QToc.C    (working copy)
@@ -12,7 +12,7 @@
 #include <config.h>
 
 #include "QToc.h"
-#include "QTocDialog.h"
+#include "TocModel.h"
 #include "Qt2BC.h"
 #include "qt_helpers.h"
 
@@ -20,6 +20,8 @@
 
 #include "controllers/ControlToc.h"
 
+#include <algorithm>
+
 using std::endl;
 
 using std::pair;
@@ -29,71 +31,112 @@
 namespace lyx {
 namespace frontend {
 
-typedef QController<ControlToc, QView<QTocDialog> > base_class;
-
 QToc::QToc(Dialog & parent)
-       : base_class(parent, _("Table of Contents"))
-{}
+       : ControlToc(parent)
+{
+       update();
+}
 
 
-void QToc::build_dialog()
+bool QToc::canOutline()
 {
-       dialog_.reset(new QTocDialog(this));
+       vector<string> const & types = getTypes();
+
+       BOOST_ASSERT(type_ >= 0 && type_ < int(types.size()));
+       return ControlToc::canOutline(types[type_]);
 }
 
 
-void QToc::update_contents()
+QStandardItemModel * QToc::tocModel()
 {
-       dialog_->updateType();
-       dialog_->updateToc();
+       lyxerr[Debug::GUI]
+               << "QToc: type_ " << type_
+               << "  toc_models_.size() " << toc_models_.size()
+               << endl;
+
+       BOOST_ASSERT(type_ >= 0 && type_ < int(toc_models_.size()));
+       return toc_models_[type_];
 }
 
 
-void QToc::select(string const & text)
+QStandardItemModel * QToc::setTocModel(int type)
 {
-       toc::Toc::const_iterator iter = toclist.begin();
+       type_ = type;
 
-       for (; iter != toclist.end(); ++iter) {
-               if (iter->str == text)
-                       break;
-       }
+       lyxerr[Debug::GUI]
+               << "QToc: type_ " << type_
+               << "  toc_models_.size() " << toc_models_.size()
+               << endl;
 
-       if (iter == toclist.end()) {
-               lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry: "
-                       << text << endl;
-               return;
-       }
-
-       controller().goTo(*iter);
+       BOOST_ASSERT(type_ >= 0 && type_ < int(toc_models_.size()));
+       return toc_models_[type_];
 }
 
-void QToc::moveUp()
+
+void QToc::goTo(QModelIndex const & index)
 {
-       controller().outline(toc::UP);
-       update_contents();
+       ControlToc::goTo(toc_models_[type_]->item(index));
+
+       lyxerr[Debug::GUI]
+               << "QToc::goTo " << toc_models_[type_]->item(index).str
+               << endl;
 }
 
 
-void QToc::moveDown()
+void QToc::update()
 {
-       controller().outline(toc::DOWN);
-       update_contents();
+       toc_models_.clear();
+
+       QStringList type_list;
+
+       type_ = 0;
+
+       vector<string> const & types = getTypes();
+       string const & selected_type = toc::getType(params().getCmdName());
+       lyxerr[Debug::GUI] << "selected_type " << selected_type << endl;
+
+       QString gui_names_;
+       for (size_t i = 0; i != types.size(); ++i) {
+               string const & type_str = types[i];
+               type_list.append(toqstr(getGuiName(type_str)));
+               if (type_str == selected_type)
+                       type_ = i;
+               
+               lyxerr[Debug::GUI]
+                       << "QToc: new type " << type_str
+                       << "\ttoc_models_.size() " << toc_models_.size()
+                       << endl;
+
+               toc_models_.push_back(new TocModel(getContents(types[i])));
+       }
+       type_model_.setStringList(type_list);
 }
 
 
-void QToc::moveIn()
+void QToc::updateToc(int type)
 {
-       controller().outline(toc::IN);
-       update_contents();
+       vector<string> const & choice = getTypes();
+
+       toc_models_[type] = new TocModel(getContents(choice[type]));
 }
 
 
-void QToc::moveOut()
+void QToc::move(toc::OutlineOp const operation, QModelIndex & index)
 {
-       controller().outline(toc::OUT);
-       update_contents();
+       int toc_id = toc_models_[type_]->item(index).id_;
+       string toc_str = toc_models_[type_]->item(index).str;
+       toc_str.erase(0, toc_str.find(' ') + 1);
+
+       outline(operation);
+       updateToc(type_);
+
+       lyxerr[Debug::GUI]
+               << "Toc id " << toc_id
+               << "  Toc str " << toc_str
+               << endl;
+
+       index = toc_models_[type_]->index(toc_str);
 }
 
-
 } // namespace frontend
 } // namespace lyx
Index: src/frontends/qt4/QToc.h
===================================================================
--- src/frontends/qt4/QToc.h    (revision 13687)
+++ src/frontends/qt4/QToc.h    (working copy)
@@ -14,47 +14,47 @@
 #ifndef QTOC_H
 #define QTOC_H
 
-#include "QDialogView.h"
-#include "toc.h"
+#include "ControlToc.h"
 
+#include <QStandardItemModel>
+#include <QStringListModel>
+
 namespace lyx {
 namespace frontend {
 
 class ControlToc;
-class QTocDialog;
+class TocModel;
 
-//template<class W>
-class QToc :
-       public QController<ControlToc, QView<QTocDialog> >
+class QToc : public ControlToc
 {
 public:
-       friend class QTocDialog;
 
        QToc(Dialog &);
 
-       /// return the toc list
-       lyx::toc::Toc & get_toclist() { return toclist;}
+       void update();
 
-       void moveUp();
-       void moveDown();
-       void moveIn();
-       void moveOut();
+       void updateToc(int type);
 
-private:
+       bool canOutline();
+       
+       QStandardItemModel * tocModel();
+       QStandardItemModel * setTocModel(int type);
 
-       /// select an entry
-       void select(std::string const & text);
+       QStringListModel * typeModel()
+       { return &type_model_; }
 
-       virtual void apply() {}
+       void goTo(QModelIndex const & index);
 
-       /// update dialog
-       virtual void update_contents();
+       void move(toc::OutlineOp const operation, QModelIndex & index);
 
-       /// build dialog
-       virtual void build_dialog();
+private:
 
-       /// the toc list
-       lyx::toc::Toc toclist;
+       std::vector<TocModel *> toc_models_;
+
+       QStringListModel type_model_;
+
+       int type_;
+       int outline_type_;
 };
 
 } // namespace frontend
Index: src/frontends/qt4/QTocDialog.C
===================================================================
--- src/frontends/qt4/QTocDialog.C      (revision 13687)
+++ src/frontends/qt4/QTocDialog.C      (working copy)
@@ -19,10 +19,10 @@
 
 #include "debug.h"
 
-#include <QTreeWidget>
 #include <QTreeWidgetItem>
 #include <QPushButton>
 #include <QCloseEvent>
+#include <QHeaderView>
 
 #include <vector>
 #include <string>
@@ -34,28 +34,23 @@
 using std::vector;
 using std::string;
 
+
 namespace lyx {
 namespace frontend {
 
-QTocDialog::QTocDialog(QToc * form)
-       : form_(form), depth_(2)
+QTocDialog::QTocDialog(Dialog & dialog, QToc * form)
+       : Dialog::View(dialog, "Toc"), form_(form), depth_(2)
 {
        setupUi(this);
 
-       // Manage the cancel/close button
-       form_->bcview().setCancel(closePB);
+       update();
 
-       // disable sorting
-       tocTW->setSortingEnabled(false);
-       tocTW->setColumnCount(1);
+       connect(tocTV->selectionModel(),
+               SIGNAL(currentChanged(const QModelIndex &,
+                       const QModelIndex &)),
+               this, SLOT(selectionChanged(const QModelIndex &,
+                       const QModelIndex &)));
 
-       // hide the pointless QHeader
-//     QWidget * w = static_cast<QWidget*>(tocTW->child("list view header"));
-//     if (w)
-//             w->hide();
-
-//     connect(closePB, SIGNAL(clicked()),
-//             form, SLOT(slotClose()));
 }
 
 
@@ -64,196 +59,166 @@
        accept();
 }
 
-void QTocDialog::on_tocTW_currentItemChanged(QTreeWidgetItem * current,
-                                                                
QTreeWidgetItem * previous)
+void QTocDialog::selectionChanged(const QModelIndex & current,
+                                                                 const 
QModelIndex & previous)
 {
-       form_->select(fromqstr(current->text(0)));
+       lyxerr[Debug::GUI]
+               << "selectionChanged index " << current.row() << ", " << 
current.column()
+               << endl;
+
+       form_->goTo(current);
 }
 
+
+void QTocDialog::on_tocTV_clicked(const QModelIndex & index )
+{
+       lyxerr[Debug::GUI]
+               << "on_tocTV_clicked index " << index.row() << ", " << 
index.column()
+               << endl;
+
+       form_->goTo(index);
+}
+
+
 void QTocDialog::on_closePB_clicked()
 {
        accept();
 }
 
+
 void QTocDialog::on_updatePB_clicked()
 {
        form_->update();
+       update();
 }
 
+
 void QTocDialog::on_depthSL_valueChanged(int depth)
 {
        if (depth == depth_)
                return;
 
        depth_ = depth;
-       updateToc(true);
+
+/*
+       while (
+       tocTv->setExpanded();
+                       if (iter->depth > depth_)
+                               tocTV->collapseItem(topLevelItem);
+                       else if (iter->depth <= depth_)
+                               tocTV->expandItem(topLevelItem);
+*/
 }
 
+
 void QTocDialog::on_typeCO_activated(int value)
 {
-       updateToc();
+       form_->setTocModel(value);
+       enableButtons();
 }
 
+
 void QTocDialog::on_moveUpPB_clicked()
 {
-       enableButtons(false);
-       form_->moveUp();
-       enableButtons();
+       move(toc::UP);
 }
 
+
 void QTocDialog::on_moveDownPB_clicked()
 {
-       enableButtons(false);
-       form_->moveDown();
-       enableButtons();
+       move(toc::DOWN);
 }
 
+
 void QTocDialog::on_moveInPB_clicked()
 {
-       enableButtons(false);
-       form_->moveIn();
-       enableButtons();
+       move(toc::IN);
 }
 
+
 void QTocDialog::on_moveOutPB_clicked()
 {
+       move(toc::OUT);
+}
+
+
+void QTocDialog::move(toc::OutlineOp const operation)
+{
        enableButtons(false);
-       form_->moveOut();
+       QModelIndex index = tocTV->selectionModel()->selectedIndexes()[0];
+       form_->move(operation, index);
+       select(index);
        enableButtons();
 }
 
+void QTocDialog::select(QModelIndex const & index)
+{
+       tocTV->scrollTo(index);
+       tocTV->selectionModel()->select(index, QItemSelectionModel::Select);
+}
+
 void QTocDialog::enableButtons(bool enable)
 {
+       updatePB->setEnabled(enable);
+
+       if (!form_->canOutline())
+               enable = false;
+
        moveUpPB->setEnabled(enable);
        moveDownPB->setEnabled(enable);
        moveInPB->setEnabled(enable);
        moveOutPB->setEnabled(enable);
-       updatePB->setEnabled(enable);
 }
 
-void QTocDialog::updateType()
+
+void QTocDialog::update()
 {
-       typeCO->clear();
+       typeCO->setModel(form_->typeModel());
+       tocTV->setModel(form_->tocModel());
+//     tocTV->setRootIndex(form_->tocModel()->index(0,0));
+       tocTV->showColumn(0);
+//     tocTV->showColumn(1);
+       // hide the pointless QHeader for now
+       // in the future, new columns may appear 
+       // like labels, bookmarks, etc...
+       // tocTV->header()->hide();
+       tocTV->header()->setVisible(true);
+       enableButtons();
 
-       vector<string> const & choice = form_->controller().getTypes();
-       string const & type = 
toc::getType(form_->controller().params().getCmdName());
-
-       for (vector<string>::const_iterator it = choice.begin();
-               it != choice.end(); ++it) {
-               string const & guiname = form_->controller().getGuiName(*it);
-               typeCO->insertItem(toqstr(guiname));
-               if (*it == type) {
-                       typeCO->setCurrentItem(it - choice.begin());
-                       form_->setTitle(guiname);
-               }
-       }
+       lyxerr[Debug::GUI]
+               << "form_->tocModel()->rowCount " << 
form_->tocModel()->rowCount()
+               << "\nform_->tocModel()->columnCount " << 
form_->tocModel()->columnCount()
+               << endl;
+//     setTitle(form_->guiname())
 }
 
-void QTocDialog::updateToc(bool newdepth)
+
+void QTocDialog::apply()
 {
-       vector<string> const & choice = form_->controller().getTypes();
-       string type;
-       if (!choice.empty())
-               type = choice[typeCO->currentItem()];
+       // Nothing to do here... for now.
+       // Ideas welcome... (Abdel, 17042006)
+}
 
-       toc::Toc const & contents = form_->controller().getContents(type);
 
-       // Check if all elements are the same.
-       if (!newdepth && form_->get_toclist() == contents) {
-               return;
-       }
-
-       tocTW->clear();
-
-       form_->get_toclist() = contents;
-
-       if (form_->get_toclist().empty())
-               return;
-
-       tocTW->setUpdatesEnabled(false);
-
-       QTreeWidgetItem * topLevelItem;
-
-       toc::Toc::const_iterator iter = form_->get_toclist().begin();
-
-       while (iter != form_->get_toclist().end()) {
-
-               if (iter->depth == 1) {
-                       topLevelItem = new QTreeWidgetItem(tocTW);
-                       topLevelItem->setText(0, toqstr(iter->str));
-                       if (iter->depth > depth_)
-                               tocTW->collapseItem(topLevelItem);
-                       else if (iter->depth <= depth_)
-                               tocTW->expandItem(topLevelItem);
-
-                       lyxerr[Debug::GUI]
-                               << "Table of contents\n"
-                               << "Added Top Level item " << iter->str
-                               << " at depth " << iter->depth
-                               << endl;
-
-                       populateItem(topLevelItem, iter);
-               }
-
-               if (iter == form_->get_toclist().end())
-                       break;
-
-               ++iter;
-       }
-
-       tocTW->setUpdatesEnabled(true);
-       tocTW->update();
-       form_->setTitle(fromqstr(typeCO->currentText()));
-       tocTW->show();
+void QTocDialog::hide()
+{
+       accept();
 }
 
-void QTocDialog::populateItem(QTreeWidgetItem * parentItem, 
toc::Toc::const_iterator & iter)
-{
-       int curdepth = iter->depth + 1;
-       QTreeWidgetItem * item;
 
-       while (iter != form_->get_toclist().end()) {
-
-               ++iter;
-
-               if (iter == form_->get_toclist().end())
-                       break;
-
-               if (iter->depth < curdepth) {
-                       --iter;
-                       return;
-               }
-               if (iter->depth > curdepth) {
-//                     --iter;
-                       return;
-               }
-
-               item = new QTreeWidgetItem(parentItem);
-               item->setText(0, toqstr(iter->str));
-
-               if (iter->depth > depth_)
-                       tocTW->collapseItem(item);
-               else if (iter->depth <= depth_)
-                       tocTW->expandItem(item);
-
-               lyxerr[Debug::GUI]
-                       << "Table of contents: Added item " << iter->str
-                       << " at depth " << iter->depth
-                       << "  \", parent \""
-                       << fromqstr(parentItem->text(0)) << '"'
-                       << "expanded: " << tocTW->isItemExpanded(item)
-                       << endl;
-
-               populateItem(item, iter);
-       }
+void QTocDialog::show()
+{
+       form_->update();
+       update();
+       QDialog::show();
 }
 
 
-void QTocDialog::closeEvent(QCloseEvent * e)
+bool QTocDialog::isVisible() const
 {
-       form_->slotWMHide();
-       e->accept();
+       return QDialog::isVisible();
 }
 
+
 } // namespace frontend
 } // namespace lyx
Index: src/frontends/qt4/QTocDialog.h
===================================================================
--- src/frontends/qt4/QTocDialog.h      (revision 13687)
+++ src/frontends/qt4/QTocDialog.h      (working copy)
@@ -17,33 +17,48 @@
 #include "controllers/ControlToc.h"
 
 #include <QDialog>
-#include <QCloseEvent>
 
-class QTreeWidget;
-class QTreeWidgetItem;
+class QTreeViewItem;
 
 namespace lyx {
 namespace frontend {
 
 class QToc;
 
-class QTocDialog : public QDialog, public Ui::QTocUi {
+class QTocDialog : public QDialog, public Ui::QTocUi, public Dialog::View  {
        Q_OBJECT
 public:
-       QTocDialog(QToc * form);
+       QTocDialog(Dialog &, QToc * form);
+
        ~QTocDialog();
 
-       /// update the listview
-       void updateToc(bool newdepth=false);
+       virtual void apply();
 
-       /// update the float types
-       void updateType();
+       /// Hide the dialog from sight
+       void hide();
 
+       /// Redraw the dialog (e.g. if the colors have been remapped).
+       void redraw() {}
+
+       /// Create the dialog if necessary, update it and display it.
+       void show();
+
+       /// Update the display of the dialog whilst it is still visible.
+       void update();
+
+       /// \return true if the dialog is visible.
+       bool isVisible() const;
+
 protected slots:
        ///
-       void on_tocTW_currentItemChanged(QTreeWidgetItem * current,
-               QTreeWidgetItem * previous);
+       void select(QModelIndex const & index);
+       ///
+       void selectionChanged(const QModelIndex & current,
+               const QModelIndex & previous);
 
+       /// Temporary until the slot above work.
+       void on_tocTV_clicked(const QModelIndex & index );
+
        void on_closePB_clicked();
        void on_updatePB_clicked();
        void on_depthSL_valueChanged(int depth);
@@ -54,13 +69,14 @@
        void on_moveOutPB_clicked();
 
 protected:
+       ///
        void enableButtons(bool enable = true);
-       void closeEvent(QCloseEvent * e);
+       ///
+       void move(toc::OutlineOp const operation);
+       ///
 
 private:
 
-       void populateItem(QTreeWidgetItem * parentItem, 
toc::Toc::const_iterator& iter);
-
        QToc * form_;
 
        /// depth of list shown
Index: src/frontends/qt4/TocModel.C
===================================================================
--- src/frontends/qt4/TocModel.C        (revision 0)
+++ src/frontends/qt4/TocModel.C        (revision 0)
@@ -0,0 +1,158 @@
+/**
+ * \file QTocDialog.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author John Levon
+ * \author Abdelrazak Younes
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#include "TocModel.h"
+
+#include "debug.h"
+
+#include <vector>
+#include <string>
+
+using std::endl;
+using std::pair;
+using std::map;
+using std::vector;
+using std::string;
+using std::make_pair;
+
+namespace lyx {
+namespace frontend {
+
+
+TocModel::TocModel(toc::Toc const & toc_list)
+{
+       populate(toc_list);
+}
+
+
+TocModel const & TocModel::operator=(toc::Toc const & toc_list)
+{
+       populate(toc_list);
+       return *this;
+}
+
+toc::TocItem const TocModel::item(QModelIndex const & index) const
+{
+       ItemMap::const_iterator it = item_map_.find(index);
+       BOOST_ASSERT(it != item_map_.end());
+       
+       return it->second;
+}
+
+QModelIndex const TocModel::index(string const & toc_str) const
+{
+       IndexMap::const_iterator it = index_map_.find(toc_str);
+       BOOST_ASSERT(it != index_map_.end());
+       
+       return it->second;
+}
+
+void TocModel::clear()
+{
+       QStandardItemModel::clear();
+       item_map_.clear();
+       index_map_.clear();
+       removeRows(0, rowCount());
+       removeColumns(0, columnCount());
+}
+
+
+void TocModel::populate(toc::Toc const & toc_list)
+{
+       clear();
+
+       if (toc_list.empty())
+               return;
+
+       int current_row;
+       QModelIndex top_level_item;
+
+       toc::Toc::const_iterator iter = toc_list.begin();
+       toc::Toc::const_iterator end = toc_list.end();
+
+    insertColumns(0, 1);
+
+       while (iter != end) {
+
+               if (iter->depth == 1) {
+
+                       current_row = rowCount();
+                       insertRows(current_row, 1);
+                       top_level_item = QStandardItemModel::index(current_row, 
0);
+                       //setData(top_level_item, toqstr(iter->str));
+                       setData(top_level_item, toqstr(iter->str), 
Qt::DisplayRole);
+                       item_map_.insert(make_pair(top_level_item, *iter));
+                       index_map_.insert(make_pair(
+                               iter->str.substr(iter->str.find(' ') + 1), 
top_level_item));
+
+                       lyxerr[Debug::GUI]
+                               << "Toc: at depth " << iter->depth
+                               << ", added item " << iter->str
+                               << endl;
+
+                       populate(iter, end, top_level_item);
+               }
+
+               if (iter == end)
+                       break;
+
+               ++iter;
+       }
+       
+       setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
+//     emit headerDataChanged();
+}
+
+
+void TocModel::populate(toc::Toc::const_iterator & iter,
+                                               toc::Toc::const_iterator const 
& end,
+                                               QModelIndex const & parent)
+{
+       int curdepth = iter->depth + 1;
+       int current_row;
+       QModelIndex child_item;
+
+    insertColumns(0, 1, parent);
+       while (iter != end) {
+
+               ++iter;
+
+               if (iter == end)
+                       break;
+
+               if (iter->depth < curdepth) {
+                       --iter;
+                       return;
+               }
+               if (iter->depth > curdepth) {
+                       return;
+               }
+               
+               current_row = rowCount(parent);
+               insertRows(current_row, 1, parent);
+               child_item = QStandardItemModel::index(current_row, 0, parent);
+               //setData(child_item, toqstr(iter->str));
+               setData(child_item, toqstr(iter->str), Qt::DisplayRole);
+               item_map_.insert(make_pair(child_item, *iter));
+               index_map_.insert(make_pair(
+                       iter->str.substr(iter->str.find(' ') + 1), child_item));
+
+//             lyxerr[Debug::GUI]
+//                     << "Toc: at depth " << iter->depth
+//                     << ", added item " << iter->str 
+//                     << endl;
+
+               populate(iter, end, child_item);
+       }
+}
+
+} // namespace frontend
+} // namespace lyx
Index: src/frontends/qt4/TocModel.h
===================================================================
--- src/frontends/qt4/TocModel.h        (revision 0)
+++ src/frontends/qt4/TocModel.h        (revision 0)
@@ -0,0 +1,64 @@
+// -*- C++ -*-
+/**
+ * \file TocModel.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Abdelrazak Younes
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#ifndef TOCMODEL_H
+#define TOCMODEL_H
+
+#include "toc.h"
+
+#include "qt_helpers.h"
+
+#include <QStandardItemModel>
+
+#include <map>
+#include <string>
+
+namespace lyx {
+namespace frontend {
+
+class TocModel: public QStandardItemModel {
+       Q_OBJECT
+public:
+       ///
+       TocModel() {}
+       ///
+       TocModel(toc::Toc const & toc_list);
+       ///
+       ~TocModel() {}
+       ///
+       TocModel const & operator=(toc::Toc const & toc_list);
+       ///
+       void clear();
+       ///
+       void populate(toc::Toc const & toc_list);
+       ///
+       toc::TocItem const item(QModelIndex const & index) const;
+       ///
+       QModelIndex const index(std::string const & toc_str) const;
+
+private:
+       ///
+       void populate(toc::Toc::const_iterator & iter,
+               toc::Toc::const_iterator const & end,
+               QModelIndex const & parent);
+
+       typedef std::map<QModelIndex, toc::TocItem> ItemMap;
+       ///
+       typedef std::map<std::string, QModelIndex> IndexMap;
+       ///
+       ItemMap item_map_;
+       IndexMap index_map_;
+};
+
+} // namespace frontend
+} // namespace lyx
+
+#endif // TOCMODEL_H
Index: src/frontends/qt4/ui/QTocUi.ui
===================================================================
--- src/frontends/qt4/ui/QTocUi.ui      (revision 13687)
+++ src/frontends/qt4/ui/QTocUi.ui      (working copy)
@@ -63,9 +63,6 @@
      </item>
     </layout>
    </item>
-   <item row="1" column="0" >
-    <widget class="QTreeWidget" name="tocTW" />
-   </item>
    <item row="4" column="0" >
     <layout class="QGridLayout" >
      <property name="margin" >
@@ -170,6 +167,9 @@
      </item>
     </layout>
    </item>
+   <item row="1" column="0" >
+    <widget class="QTreeView" name="tocTV" />
+   </item>
   </layout>
  </widget>
  <pixmapfunction>qPixmapFromMimeSource</pixmapfunction>

Reply via email to