From 61dde5d7976b37b22c5ceac63fc168c3afedfabf Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Fri, 16 Mar 2007 14:14:55 +0000 Subject: [PATCH] Cleanup of the Toc model and controller: The objective is to let the View (TocWidget) decide which kind of list it want to show. The type given by InsetCommandParam() (as read by the controller (ControlToc) then just becomes a hint to the View. Most of the commit is about getting rid of the controller maintained list of types. * TocBackend - addType(), types_, types(): deleted. * ControlToc - tocs(): new - selectedType(): access to selected_type_ - initialiseParams(): transfer code from QToc::update() and look for selected type. - getTypes(), getContents(): deleted * QToc: do not maintain current type. Get the info from the View. - setTocModel(): deleted * TocWidget: always pass the currently selected type to the model/controller. - setTocModel(): new slot. - updateGui(): transfer code to setTocModel(). git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@17455 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/TocBackend.C | 18 ---- src/TocBackend.h | 8 +- src/frontends/controllers/ControlToc.C | 66 +++++++++------ src/frontends/controllers/ControlToc.h | 26 ++++-- src/frontends/qt4/QToc.C | 111 +++++++------------------ src/frontends/qt4/QToc.h | 11 ++- src/frontends/qt4/TocWidget.C | 65 ++++++++++----- src/frontends/qt4/TocWidget.h | 2 + 8 files changed, 137 insertions(+), 170 deletions(-) diff --git a/src/TocBackend.C b/src/TocBackend.C index c1de07da75..6b12ad2bee 100644 --- a/src/TocBackend.C +++ b/src/TocBackend.C @@ -121,19 +121,6 @@ Toc const & TocBackend::toc(std::string const & type) const } -bool TocBackend::addType(std::string const & type) -{ - // Is the type already supported? - TocList::iterator toclist_it = tocs_.find(type); - if (toclist_it != tocs_.end()) - return false; - - tocs_.insert(make_pair(type, Toc())); - types_.push_back(type); - - return true; -} - void TocBackend::updateItem(ParConstIterator const & par_it) { BufferParams const & bufparams = buffer_->params(); @@ -175,7 +162,6 @@ void TocBackend::updateItem(ParConstIterator const & par_it) void TocBackend::update() { tocs_.clear(); - types_.clear(); BufferParams const & bufparams = buffer_->params(); const int min_toclevel = bufparams.getLyXTextClass().min_toclevel(); @@ -223,10 +209,6 @@ void TocBackend::update() TocItem(pit, toclevel - min_toclevel, tocstring)); } } - - TocList::iterator it = tocs_.begin(); - for (; it != tocs_.end(); ++it) - types_.push_back(it->first); } diff --git a/src/TocBackend.h b/src/TocBackend.h index aa73a68e5b..ba8f9f8f38 100644 --- a/src/TocBackend.h +++ b/src/TocBackend.h @@ -96,8 +96,6 @@ public: void setBuffer(Buffer const * buffer) { buffer_ = buffer; } /// - bool addType(std::string const & type); - /// void update(); /// void updateItem(ParConstIterator const & pit); @@ -105,9 +103,7 @@ public: /// TocList const & tocs() const { return tocs_; } - /// - std::vector const & types() const - { return types_; } + /// Toc const & toc(std::string const & type) const; /// Return the first Toc Item before the cursor @@ -119,8 +115,6 @@ private: /// TocList tocs_; /// - std::vector types_; - /// Buffer const * buffer_; }; // TocBackend diff --git a/src/frontends/controllers/ControlToc.C b/src/frontends/controllers/ControlToc.C index 9bd9989564..df071bc1b0 100644 --- a/src/frontends/controllers/ControlToc.C +++ b/src/frontends/controllers/ControlToc.C @@ -4,6 +4,7 @@ * Licence details can be found in the file COPYING. * * \author Angus Leeming + * \author Abdelrazak Younes * * Full author contact details are available in file CREDITS. */ @@ -13,6 +14,7 @@ #include #include "ControlToc.h" + #include "buffer.h" #include "BufferView.h" #include "bufferparams.h" @@ -41,10 +43,42 @@ ControlToc::ControlToc(Dialog & d) } +TocList const & ControlToc::tocs() const +{ + return kernel().buffer().tocBackend().tocs(); +} + + bool ControlToc::initialiseParams(string const & data) { + if (!ControlCommand::initialiseParams(data)) + return false; + + types_.clear(); + type_names_.clear(); + TocList const & tocs = kernel().buffer().tocBackend().tocs(); + TocList::const_iterator it = tocs.begin(); + TocList::const_iterator end = tocs.end(); + for (; it != end; ++it) { + types_.push_back(it->first); + type_names_.push_back(getGuiName(it->first)); + } + + string selected_type ; + if(params()["type"].empty()) //Then plain toc... + selected_type = params().getCmdName(); + else + selected_type = to_ascii(params()["type"]); + selected_type_ = -1; + for (size_t i = 0; i != types_.size(); ++i) { + if (selected_type == types_[i]) { + selected_type_ = i; + break; + } + } + update(); - return ControlCommand::initialiseParams(data); + return true; } void ControlToc::goTo(TocItem const & item) @@ -54,9 +88,10 @@ void ControlToc::goTo(TocItem const & item) } -bool ControlToc::canOutline(string const & type) +bool ControlToc::canOutline(size_t type) const { - return type == "tableofcontents"; + BOOST_ASSERT(type >= 0 && type < int(types_.size())); + return types_[type] == "tableofcontents"; } @@ -84,25 +119,17 @@ void ControlToc::outlineOut() } -vector const & ControlToc::getTypes() const -{ - return kernel().buffer().tocBackend().types(); -} - - void ControlToc::updateBackend() { kernel().buffer().tocBackend().update(); } -TocIterator const ControlToc::getCurrentTocItem( - string const & type) const +TocIterator const ControlToc::getCurrentTocItem(size_t type) const { BOOST_ASSERT(kernel().bufferview()); - ParConstIterator it(kernel().bufferview()->cursor()); - return kernel().buffer().tocBackend().item(type, it); + return kernel().buffer().tocBackend().item(types_[type], it); } @@ -119,18 +146,5 @@ docstring const ControlToc::getGuiName(string const & type) const return _(type); } - -Toc const empty_list; - -Toc const & ControlToc::getContents(string const & type) const -{ - // This shouldn't be possible... - if (!kernel().isBufferAvailable()) { - return empty_list; - } - - return kernel().buffer().tocBackend().toc(type); -} - } // namespace frontend } // namespace lyx diff --git a/src/frontends/controllers/ControlToc.h b/src/frontends/controllers/ControlToc.h index 273349b30f..987df9707d 100644 --- a/src/frontends/controllers/ControlToc.h +++ b/src/frontends/controllers/ControlToc.h @@ -5,6 +5,7 @@ * Licence details can be found in the file COPYING. * * \author Angus Leeming + * \author Abdelrazak Younes * * Full author contact details are available in file CREDITS. */ @@ -33,20 +34,21 @@ public: /// \c ControlCommand inherited method. bool initialiseParams(std::string const & data); + /// + TocList const & tocs() const; + /// Goto this paragraph id void goTo(TocItem const &); /// Return the list of types available - std::vector const & getTypes() const; + std::vector const & typeNames() const + { return type_names_; } - /// Return the guiname from a given cmdName of the TOC param - docstring const getGuiName(std::string const & type) const; + /// + int selectedType() { return selected_type_; } /// Return the first TocItem before the cursor - TocIterator const getCurrentTocItem(std::string const & type) const; - - /// Given a type, returns the contents - Toc const & getContents(std::string const & type) const; + TocIterator const getCurrentTocItem(size_t type) const; /// Apply the selected outlining operation void outlineUp(); @@ -57,13 +59,21 @@ public: /// void outlineOut(); /// Test if outlining operation is possible - bool canOutline(std::string const & type); + bool canOutline(size_t type) const; /// void updateBackend(); public: /// Update the model data if needed. virtual void update() = 0; + +private: + /// Return the guiname from a given cmdName of the TOC param + docstring const getGuiName(std::string const & type) const; + + std::vector types_; + std::vector type_names_; + int selected_type_; }; } // namespace frontend diff --git a/src/frontends/qt4/QToc.C b/src/frontends/qt4/QToc.C index 505e6153d4..fc683e9ff0 100644 --- a/src/frontends/qt4/QToc.C +++ b/src/frontends/qt4/QToc.C @@ -39,85 +39,65 @@ QToc::QToc(Dialog & parent) } -bool QToc::canOutline() +bool QToc::canOutline(int type) const { - vector const & types = getTypes(); - - if (types.empty()) + if (type < 0) return false; - BOOST_ASSERT(type_ >= 0 && type_ < int(types.size())); - return ControlToc::canOutline(types[type_]); + return ControlToc::canOutline(type); } -int QToc::getTocDepth() +int QToc::getTocDepth(int type) { - if (type_ < 0) + if (type < 0) return 0; - return toc_models_[type_]->modelDepth(); + return toc_models_[type]->modelDepth(); } -QStandardItemModel * QToc::tocModel() +QStandardItemModel * QToc::tocModel(int type) { + if (type < 0) + return 0; + if (toc_models_.empty()) { lyxerr[Debug::GUI] << "QToc::tocModel(): no types available " << endl; return 0; } lyxerr[Debug::GUI] - << "QToc: type_ " << type_ + << "QToc: type_ " << type << " toc_models_.size() " << toc_models_.size() << endl; - BOOST_ASSERT(type_ >= 0 && type_ < int(toc_models_.size())); - return toc_models_[type_]; + BOOST_ASSERT(type >= 0 && type < int(toc_models_.size())); + return toc_models_[type]; } -QStandardItemModel * QToc::setTocModel(int type) +QModelIndex const QToc::getCurrentIndex(int type) const { - type_ = type; - - 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_]; -} - - -QModelIndex const QToc::getCurrentIndex() -{ - vector const & types = getTypes(); - if (types.empty() || type_ < 0) + if (type < 0) return QModelIndex(); - TocIterator const it = getCurrentTocItem(types[type_]); - if (it == getContents(types[type_]).end() || !it->isValid()) { - lyxerr[Debug::GUI] << "QToc::getCurrentIndex(): TocItem is invalid!" << endl; - return QModelIndex(); - } - - return toc_models_[type_]->modelIndex(it); + return toc_models_[type]->modelIndex(getCurrentTocItem(type)); } -void QToc::goTo(QModelIndex const & index) +void QToc::goTo(int type, QModelIndex const & index) { - if (!index.isValid() || index.model() != tocModel()) { + if (type < 0 || !index.isValid() + || index.model() != toc_models_[type]) { lyxerr[Debug::GUI] << "QToc::goTo(): QModelIndex is invalid!" << endl; return; } - BOOST_ASSERT(type_ >= 0 && type_ < int(toc_models_.size())); + BOOST_ASSERT(type >= 0 && type < int(toc_models_.size())); - TocIterator const it = toc_models_[type_]->tocIterator(index); + TocIterator const it = toc_models_[type]->tocIterator(index); lyxerr[Debug::GUI] << "QToc::goTo " << lyx::to_utf8(it->str()) @@ -127,12 +107,6 @@ void QToc::goTo(QModelIndex const & index) } -int QToc::getType() -{ - return type_; -} - - void QToc::update() { updateType(); @@ -143,37 +117,13 @@ void QToc::update() void QToc::updateType() { - QStringList type_list; - vector const & types = getTypes(); - if (types.empty()) { - type_model_.setStringList(type_list); - toc_models_.clear(); - lyxerr[Debug::GUI] << "QToc::updateType(): no types available " << endl; - return; - } + vector const & type_names = typeNames(); + BOOST_ASSERT(!type_names.empty()); + for (size_t i = 0; i != type_names.size(); ++i) + type_list.append(toqstr(type_names[i])); - string selected_type ; - if(params()["type"].empty()) //Then plain toc... - selected_type = params().getCmdName(); - else - selected_type = to_ascii(params()["type"]); - - QString gui_names_; - type_ = -1; - 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; - - } type_model_.setStringList(type_list); } @@ -181,13 +131,10 @@ void QToc::updateType() void QToc::updateToc() { toc_models_.clear(); - vector const & types = getTypes(); - - for (size_t i = 0; i != types.size(); ++i) { - - toc_models_.push_back(new TocModel(getContents(types[i]))); - } - + TocList::const_iterator it = tocs().begin(); + TocList::const_iterator end = tocs().end(); + for (; it != end; ++it) + toc_models_.push_back(new TocModel(it->second)); } diff --git a/src/frontends/qt4/QToc.h b/src/frontends/qt4/QToc.h index ea71c2b367..f7875b9003 100644 --- a/src/frontends/qt4/QToc.h +++ b/src/frontends/qt4/QToc.h @@ -39,22 +39,21 @@ public: /// void updateType(); - bool canOutline(); + bool canOutline(int type) const; - QStandardItemModel * tocModel(); - QStandardItemModel * setTocModel(int type); + QStandardItemModel * tocModel(int type); QStringListModel * typeModel() { return &type_model_; } /// - QModelIndex const getCurrentIndex(); + QModelIndex const getCurrentIndex(int type) const; /// - void goTo(QModelIndex const & index); + void goTo(int type, QModelIndex const & index); /// int getType(); /// - int getTocDepth(); + int getTocDepth(int type); Q_SIGNALS: /// Signal that the internal toc_models_ has been reset. diff --git a/src/frontends/qt4/TocWidget.C b/src/frontends/qt4/TocWidget.C index 304071e545..76a685216f 100644 --- a/src/frontends/qt4/TocWidget.C +++ b/src/frontends/qt4/TocWidget.C @@ -66,7 +66,7 @@ void TocWidget::selectionChanged(const QModelIndex & current, << ", " << current.column() << endl; - form_->goTo(current); + form_->goTo(typeCO->currentIndex(), current); } @@ -105,10 +105,10 @@ void TocWidget::setTreeDepth(int depth) // expanding and then collapsing is probably better, // but my qt 4.1.2 doesn't have expandAll().. //tocTV->expandAll(); - QModelIndexList indices = - form_->tocModel()->match(form_->tocModel()->index(0,0), - Qt::DisplayRole, "*", -1, - Qt::MatchWildcard|Qt::MatchRecursive); + QModelIndexList indices = tocTV->model()->match( + tocTV->model()->index(0,0), + Qt::DisplayRole, "*", -1, + Qt::MatchWildcard|Qt::MatchRecursive); int size = indices.size(); for (int i = 0; i < size; i++) { @@ -123,8 +123,7 @@ void TocWidget::setTreeDepth(int depth) void TocWidget::on_typeCO_activated(int value) { - form_->setTocModel(value); - updateGui(); + setTocModel(value); } @@ -134,7 +133,7 @@ void TocWidget::on_moveUpPB_clicked() QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes(); if (!list.isEmpty()) { enableButtons(false); - form_->goTo(list[0]); + form_->goTo(typeCO->currentIndex(), list[0]); form_->outlineUp(); enableButtons(true); } @@ -147,7 +146,7 @@ void TocWidget::on_moveDownPB_clicked() QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes(); if (!list.isEmpty()) { enableButtons(false); - form_->goTo(list[0]); + form_->goTo(typeCO->currentIndex(), list[0]); form_->outlineDown(); enableButtons(true); } @@ -160,7 +159,7 @@ void TocWidget::on_moveInPB_clicked() QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes(); if (!list.isEmpty()) { enableButtons(false); - form_->goTo(list[0]); + form_->goTo(typeCO->currentIndex(), list[0]); form_->outlineIn(); enableButtons(true); } @@ -172,7 +171,7 @@ void TocWidget::on_moveOutPB_clicked() QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes(); if (!list.isEmpty()) { enableButtons(false); - form_->goTo(list[0]); + form_->goTo(typeCO->currentIndex(), list[0]); form_->outlineOut(); enableButtons(true); } @@ -199,7 +198,7 @@ void TocWidget::enableButtons(bool enable) { updatePB->setEnabled(enable); - if (!form_->canOutline()) + if (!form_->canOutline(typeCO->currentIndex())) enable = false; moveUpPB->setEnabled(enable); @@ -211,7 +210,8 @@ void TocWidget::enableButtons(bool enable) void TocWidget::update() { - select(form_->getCurrentIndex()); + lyxerr[Debug::GUI] << "In TocWidget::update()" << endl; + select(form_->getCurrentIndex(typeCO->currentIndex())); QWidget::update(); } @@ -228,13 +228,25 @@ void TocWidget::updateGui() return; } + QString current_text = typeCO->currentText(); typeCO->setModel(type_model); - typeCO->setCurrentIndex(form_->getType()); + int const current_type = typeCO->findText(current_text); + if (current_type != -1) + typeCO->setCurrentIndex(current_type); + else + typeCO->setCurrentIndex(form_->selectedType()); + setTocModel(typeCO->currentIndex()); +} + + +void TocWidget::setTocModel(size_t type) +{ bool buttons_enabled = false; - if (form_->tocModel()) { - buttons_enabled = form_->tocModel()->rowCount() > 0; - tocTV->setModel(form_->tocModel()); + QStandardItemModel * toc_model = form_->tocModel(type); + if (toc_model) { + buttons_enabled = toc_model->rowCount() > 0; + tocTV->setModel(toc_model); tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers); } @@ -242,14 +254,21 @@ void TocWidget::updateGui() reconnectSelectionModel(); depthSL->setEnabled(true); - depthSL->setMaximum(form_->getTocDepth()); + depthSL->setMaximum(form_->getTocDepth(type)); depthSL->setValue(depth_); - select(form_->getCurrentIndex()); - lyxerr[Debug::GUI] - << "form_->tocModel()->rowCount " << form_->tocModel()->rowCount() - << "\nform_->tocModel()->columnCount " << form_->tocModel()->columnCount() - << endl; + lyxerr[Debug::GUI] << "In TocWidget::updateGui()" << endl; + + select(form_->getCurrentIndex(typeCO->currentIndex())); + + if (toc_model) { + lyxerr[Debug::GUI] + << "form_->tocModel()->rowCount " + << toc_model->rowCount() + << "\nform_->tocModel()->columnCount " + << toc_model->columnCount() + << endl; + } } diff --git a/src/frontends/qt4/TocWidget.h b/src/frontends/qt4/TocWidget.h index b19acc5acd..c04989fd04 100644 --- a/src/frontends/qt4/TocWidget.h +++ b/src/frontends/qt4/TocWidget.h @@ -34,6 +34,8 @@ protected Q_SLOTS: /// Update Gui of the display. void updateGui(); /// + void setTocModel(size_t type); + /// void select(QModelIndex const & index); /// void selectionChanged(const QModelIndex & current, -- 2.39.2