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.

I am very sorry Juergen that you spend some times to fix the slider bug. But rest assure that it was not for nothing because now I know the correct logic ;-).

Please find attached the patch. If you know about Qt4 model/view programming I would be thankful if you could take a look. Maybe Angus or Andre could help here also. Pretty please...

Thanks in advance,
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,69 +31,117 @@
 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;
+       BOOST_ASSERT(type_ >= 0 && type_ < (int) toc_models_.size());
+       return toc_models_[type_];
+}
+
+
+void QToc::goTo(QModelIndex const & index)
+{
+       ControlToc::goTo(toc_models_[type_]->item(index));
+}
+
+
+void QToc::update()
+{
+       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);
+}
 
-       controller().goTo(*iter);
+
+void QToc::updateToc(int type)
+{
+       vector<string> const & choice = getTypes();
+
+       toc_models_[type]->populate(getContents(choice[type]));
 }
 
+
 void QToc::moveUp()
 {
-       controller().outline(toc::UP);
-       update_contents();
+       outline(toc::UP);
+       updateToc(type_);
 }
 
 
 void QToc::moveDown()
 {
-       controller().outline(toc::DOWN);
-       update_contents();
+       outline(toc::DOWN);
+       updateToc(type_);
 }
 
 
 void QToc::moveIn()
 {
-       controller().outline(toc::IN);
-       update_contents();
+       outline(toc::IN);
+       updateToc(type_);
 }
 
 
 void QToc::moveOut()
 {
-       controller().outline(toc::OUT);
-       update_contents();
+       outline(toc::OUT);
+       updateToc(type_);
 }
 
 
Index: src/frontends/qt4/QToc.h
===================================================================
--- src/frontends/qt4/QToc.h    (revision 13687)
+++ src/frontends/qt4/QToc.h    (working copy)
@@ -14,27 +14,37 @@
 #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 updateToc(int type);
+
+       bool canOutline();
+       
+       QStandardItemModel * tocModel();
+       QStandardItemModel * setTocModel(int type);
+
+       QStringListModel * typeModel()
+       { return &type_model_; }
+
+       void goTo(QModelIndex const & index);
+
        void moveUp();
        void moveDown();
        void moveIn();
@@ -42,19 +52,12 @@
 
 private:
 
-       /// select an entry
-       void select(std::string const & text);
+       std::vector<TocModel *> toc_models_;
 
-       virtual void apply() {}
+       QStringListModel type_model_;
 
-       /// update dialog
-       virtual void update_contents();
-
-       /// build dialog
-       virtual void build_dialog();
-
-       /// the toc list
-       lyx::toc::Toc toclist;
+       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,16 @@
 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);
-
-       // disable sorting
-       tocTW->setSortingEnabled(false);
-       tocTW->setColumnCount(1);
-
-       // 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()));
+       update();
 }
 
 
@@ -64,10 +52,9 @@
        accept();
 }
 
-void QTocDialog::on_tocTW_currentItemChanged(QTreeWidgetItem * current,
-                                                                
QTreeWidgetItem * previous)
+void QTocDialog::on_tocTV_activated(const QModelIndex & index )
 {
-       form_->select(fromqstr(current->text(0)));
+       form_->goTo(index);
 }
 
 void QTocDialog::on_closePB_clicked()
@@ -78,6 +65,7 @@
 void QTocDialog::on_updatePB_clicked()
 {
        form_->update();
+       update();
 }
 
 void QTocDialog::on_depthSL_valueChanged(int depth)
@@ -86,12 +74,22 @@
                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();
+       tocTV->setModel(form_->setTocModel(value));
+       tocTV->showColumn(0);
+       tocTV->showColumn(1);
 }
 
 void QTocDialog::on_moveUpPB_clicked()
@@ -122,6 +120,7 @@
        enableButtons();
 }
 
+
 void QTocDialog::enableButtons(bool enable)
 {
        moveUpPB->setEnabled(enable);
@@ -131,129 +130,55 @@
        updatePB->setEnabled(enable);
 }
 
-void QTocDialog::updateType()
+
+void QTocDialog::update()
 {
-       typeCO->clear();
+       typeCO->setModel(form_->typeModel());
+       tocTV->setModel(form_->tocModel());
+       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);
+       tocTV->show();
 
-       vector<string> const & choice = form_->controller().getTypes();
-       string const & type = 
toc::getType(form_->controller().params().getCmdName());
+       lyxerr[Debug::GUI]
+               << "form_->tocModel()->rowCount " << 
form_->tocModel()->rowCount()
+               << "\nform_->tocModel()->columnCount " << 
form_->tocModel()->columnCount()
+               << endl;
+//     setTitle(form_->guiname())
 
-       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);
-               }
-       }
 }
 
-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,32 +17,41 @@
 #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 on_tocTV_activated(const QModelIndex & index );
 
        void on_closePB_clicked();
        void on_updatePB_clicked();
@@ -55,12 +64,9 @@
 
 protected:
        void enableButtons(bool enable = true);
-       void closeEvent(QCloseEvent * e);
 
 private:
 
-       void populateItem(QTreeWidgetItem * parentItem, 
toc::Toc::const_iterator& iter);
-
        QToc * form_;
 
        /// depth of list shown
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