]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/TocModel.C
code cosmetics whitespace, unneeded lyx:: qualification, indentation, unneeded destru...
[lyx.git] / src / frontends / qt4 / TocModel.C
index 570631cf2ac551cf60680e34d0688add8ac02c88..8f923d89f49e107b460fa611fb82832bfafacd0c 100644 (file)
@@ -24,18 +24,20 @@ using std::map;
 using std::vector;
 using std::string;
 using std::make_pair;
+using std::max;
+using std::min;
 
 namespace lyx {
 namespace frontend {
 
 
-TocModel::TocModel(TocBackend::Toc const & toc)
+TocModel::TocModel(Toc const & toc)
 {
        populate(toc);
 }
 
 
-TocModel const & TocModel::operator=(TocBackend::Toc const & toc)
+TocModel const & TocModel::operator=(Toc const & toc)
 {
        populate(toc);
        return *this;
@@ -72,13 +74,12 @@ void TocModel::clear()
 }
 
 
-void TocModel::populate(TocBackend::Toc const & toc)
+void TocModel::populate(Toc const & toc)
 {
        clear();
 
        if (toc.empty())
                return;
-
        int current_row;
        QModelIndex top_level_item;
 
@@ -86,22 +87,30 @@ void TocModel::populate(TocBackend::Toc const & toc)
        TocIterator end = toc.end();
 
        insertColumns(0, 1);
+       maxdepth_ = 0;
+       mindepth_ = INT_MAX;
 
        while (iter != end) {
 
                if (iter->isValid()) {
 
+                       maxdepth_ = max(maxdepth_, iter->depth());
+                       mindepth_ = min(mindepth_, iter->depth());
                        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);
-                       toc_map_[top_level_item] = iter;
+
+                       // This looks like a gcc bug, in principle this should work:
+                       //toc_map_[top_level_item] = iter;
+                       // but it crashes with gcc-4.1 and 4.0.2
+                       toc_map_.insert( TocPair(top_level_item, iter) );
                        model_map_[iter] = top_level_item;
 
-                       lyxerr[Debug::GUI]
+                       LYXERR(Debug::GUI)
                                << "Toc: at depth " << iter->depth()
-                               << ", added item " << lyx::to_utf8(iter->str())
+                               << ", added item " << to_utf8(iter->str())
                                << endl;
 
                        populate(iter, end, top_level_item);
@@ -123,6 +132,7 @@ void TocModel::populate(TocIterator & iter,
                                                QModelIndex const & parent)
 {
        int curdepth = iter->depth() + 1;
+       
        int current_row;
        QModelIndex child_item;
 
@@ -139,18 +149,29 @@ void TocModel::populate(TocIterator & iter,
                        return;
                }
                
+               maxdepth_ = max(maxdepth_, iter->depth());
+               mindepth_ = min(mindepth_, iter->depth());
                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);
-               toc_map_[child_item] = iter;
+
+               // This looks like a gcc bug, in principle this should work:
+               //toc_map_[child_item] = iter;
+               // but it crashes with gcc-4.1 and 4.0.2
+               toc_map_.insert( TocPair(child_item, iter) );
                model_map_[iter] = child_item;
                populate(iter, end, child_item);
        }
 }
 
 
+int TocModel::modelDepth()
+{
+       return maxdepth_ - mindepth_;
+}
+
 } // namespace frontend
 } // namespace lyx