]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/TocModel.cpp
Compil fix.
[lyx.git] / src / frontends / qt4 / TocModel.cpp
index 360d70585715af3f32bd917be4aee88fb7b93d2a..ce23f24dc42489d9b791c50a5dbdfdef0a4a4d0a 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * \file GuiTocDialog.C
+ * \file TocModel.cpp
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
 
 #include "TocModel.h"
 
-#include "debug.h"
+#include "Buffer.h"
+#include "BufferView.h"
+#include "Cursor.h"
+#include "DocIterator.h"
+#include "FuncRequest.h"
+#include "LyXFunc.h"
 
-#include <boost/assert.hpp>
+#include "support/convert.h"
+#include "support/debug.h"
+#include "support/lassert.h"
 
 #include <climits>
-#include <vector>
-#include <string>
-
-using std::endl;
-using std::pair;
-using std::map;
-using std::vector;
-using std::string;
-using std::make_pair;
-using std::max;
-using std::min;
+
+using namespace std;
 
 namespace lyx {
 namespace frontend {
 
-
-TocModel::TocModel(Toc const & toc)
-{
-       populate(toc);
-}
-
-
-TocModel const & TocModel::operator=(Toc const & toc)
-{
-       populate(toc);
-       return *this;
-}
+typedef std::pair<QModelIndex, TocIterator> TocPair;
 
 
-TocIterator const TocModel::tocIterator(QModelIndex const & index) const
+TocIterator TocModel::tocIterator(QModelIndex const & index) const
 {
        TocMap::const_iterator map_it = toc_map_.find(index);
-       BOOST_ASSERT(map_it != toc_map_.end());
+       LASSERT(map_it != toc_map_.end(), /**/);
        return map_it->second;
 }
 
 
-QModelIndex const TocModel::modelIndex(TocIterator const & it) const
+QModelIndex TocModel::modelIndex(TocIterator const & it) const
 {
        ModelMap::const_iterator map_it = model_map_.find(it);
-       //BOOST_ASSERT(it != model_map_.end());
+       //LASSERT(it != model_map_.end(), /**/);
 
        if (map_it == model_map_.end())
                return QModelIndex();
@@ -108,10 +95,8 @@ void TocModel::populate(Toc const & toc)
                toc_map_.insert( TocPair(top_level_item, iter) );
                model_map_[iter] = top_level_item;
 
-               LYXERR(Debug::GUI)
-                       << "Toc: at depth " << iter->depth()
-                       << ", added item " << to_utf8(iter->str())
-                       << endl;
+               LYXERR(Debug::GUI, "Toc: at depth " << iter->depth()
+                       << ", added item " << toqstr(iter->str()));
 
                populate(iter, end, top_level_item);
 
@@ -126,18 +111,16 @@ void TocModel::populate(Toc const & toc)
 }
 
 
-void TocModel::populate(TocIterator & iter,
-                                               TocIterator const & end,
-                                               QModelIndex const & parent)
+void TocModel::populate(TocIterator & iter, TocIterator const & end,
+       QModelIndex const & parent)
 {
        int curdepth = iter->depth() + 1;
 
        int current_row;
        QModelIndex child_item;
-
        insertColumns(0, 1, parent);
-       while (iter != end) {
 
+       while (iter != end) {
                ++iter;
 
                if (iter == end)
@@ -166,11 +149,133 @@ void TocModel::populate(TocIterator & iter,
 }
 
 
-int TocModel::modelDepth()
+int TocModel::modelDepth() const
 {
        return maxdepth_ - mindepth_;
 }
 
+
+///////////////////////////////////////////////////////////////////////////////
+// TocModels implementation.
+///////////////////////////////////////////////////////////////////////////////
+void TocModels::clear()        
+{
+       types_.clear();
+       type_names_.clear();
+       const unsigned int size = models_.size();
+       for (unsigned int i = 0; i < size; ++i) {
+               delete models_[i];
+       }
+       models_.clear();
+}
+
+
+int TocModels::depth(int type)
+{
+       if (type < 0)
+               return 0;
+       return models_[type]->modelDepth();
+}
+
+
+QStandardItemModel * TocModels::model(int type)
+{
+       if (type < 0)
+               return 0;
+
+       if (models_.empty()) {
+               LYXERR(Debug::GUI, "TocModels::tocModel(): no types available ");
+               return 0;
+       }
+
+       LYXERR(Debug::GUI, "TocModels: type " << type
+               << "  models_.size() " << models_.size());
+
+       LASSERT(type >= 0 && type < int(models_.size()), /**/);
+       return models_[type];
+}
+
+
+QModelIndex TocModels::currentIndex(int type) const
+{
+       if (type < 0 || !bv_)
+               return QModelIndex();
+
+       TocIterator const it = bv_->buffer().masterBuffer()->tocBackend().item(
+               fromqstr(types_[type]), bv_->cursor());
+       return models_[type]->modelIndex(it);
+}
+
+
+void TocModels::goTo(int type, QModelIndex const & index) const
+{
+       if (type < 0 || !index.isValid()
+               || index.model() != models_[type]) {
+               LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
+               return;
+       }
+
+       LASSERT(type >= 0 && type < int(models_.size()), /**/);
+       TocIterator const it = models_[type]->tocIterator(index);
+       LYXERR(Debug::GUI, "TocModels::goTo " << it->str());
+       dispatch(it->action());
+}
+
+
+void TocModels::updateBackend() const
+{
+       bv_->buffer().masterBuffer()->tocBackend().update();
+       bv_->buffer().structureChanged();
+}
+
+
+void TocModels::reset(BufferView const * bv)
+{
+       bv_ = bv;
+       clear();
+       if (!bv_)
+               return;
+
+       TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
+       TocList::const_iterator it = tocs.begin();
+       TocList::const_iterator end = tocs.end();
+       for (; it != end; ++it) {
+               types_.push_back(toqstr(it->first));
+               type_names_.push_back(guiName(it->first, bv->buffer().params()));
+               models_.push_back(new TocModel(it->second));
+       }
+}
+
+
+bool TocModels::canOutline(int type) const
+{
+       if (type < 0 || type >= types_.size())
+               return false;
+       return types_[type] == "tableofcontents";
+}
+
+
+int TocModels::decodeType(QString const & str) const
+{
+       QString new_type;
+       if (str.contains("tableofcontents")) {
+               new_type = "tableofcontents";
+       } else if (str.contains("floatlist")) {
+               if (str.contains("\"figure"))
+                       new_type = "figure";
+               else if (str.contains("\"table"))
+                       new_type = "table";
+               else if (str.contains("\"algorithm"))
+                       new_type = "algorithm";
+       } else if (!str.isEmpty()) {
+               new_type = str;
+       } else {
+               // Default to Outliner.
+               new_type = "tableofcontents";
+       }
+       return types_.indexOf(new_type);
+}
+
 } // namespace frontend
 } // namespace lyx