#include "TocModel.h"
#include "Buffer.h"
-#include "BufferParams.h"
#include "BufferView.h"
-#include "DocIterator.h"
#include "Cursor.h"
-#include "FloatList.h"
+#include "DocIterator.h"
#include "FuncRequest.h"
#include "LyXFunc.h"
-#include "TextClass.h"
+#include "TocBackend.h"
#include "support/convert.h"
#include "support/debug.h"
namespace lyx {
namespace frontend {
-typedef std::pair<QModelIndex, TocIterator> TocPair;
-
-TocIterator TocModel::tocIterator(QModelIndex const & index) const
+TocItem const & TocModel::tocItem(QModelIndex const & index) const
{
- TocMap::const_iterator map_it = toc_map_.find(index);
- LASSERT(map_it != toc_map_.end(), /**/);
- return map_it->second;
+ return toc_[data(index, Qt::UserRole).toUInt()];
}
-QModelIndex TocModel::modelIndex(TocIterator const & it) const
+QModelIndex TocModel::modelIndex(DocIterator const & dit) const
{
- ModelMap::const_iterator map_it = model_map_.find(it);
- //LASSERT(it != model_map_.end(), /**/);
-
- if (map_it == model_map_.end())
+ if (toc_.empty())
return QModelIndex();
- return map_it->second;
-}
+ unsigned int const toc_index = toc_.item(dit) - toc_.begin();
+ QModelIndexList list = match(index(0, 0), Qt::UserRole,
+ QVariant(toc_index), 1,
+ Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
-void TocModel::clear()
-{
- QStandardItemModel::clear();
- toc_map_.clear();
- model_map_.clear();
- removeRows(0, rowCount());
- removeColumns(0, columnCount());
+ LASSERT(!list.isEmpty(), return QModelIndex());
+ return list[0];
}
-void TocModel::populate(Toc const & toc)
+TocModel::TocModel(Toc const & toc): toc_(toc)
{
- clear();
-
- if (toc.empty())
+ if (toc_.empty())
return;
int current_row;
QModelIndex top_level_item;
-
- TocIterator iter = toc.begin();
- TocIterator end = toc.end();
-
insertColumns(0, 1);
maxdepth_ = 0;
mindepth_ = INT_MAX;
- while (iter != end) {
- maxdepth_ = max(maxdepth_, iter->depth());
- mindepth_ = min(mindepth_, iter->depth());
+ size_t end = toc.size();
+ for (unsigned int index = 0; index != end; ++index) {
+ TocItem const & item = toc_[index];
+ maxdepth_ = max(maxdepth_, item.depth());
+ mindepth_ = min(mindepth_, item.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);
-
- // 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, "Toc: at depth " << iter->depth()
- << ", added item " << toqstr(iter->str()));
+ setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
+ setData(top_level_item, index, Qt::UserRole);
- populate(iter, end, top_level_item);
+ LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
+ << ", added item " << item.str());
- if (iter == end)
+ populate(index, top_level_item);
+ if (index >= end)
break;
-
- ++iter;
}
setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
}
-void TocModel::populate(TocIterator & iter, TocIterator const & end,
- QModelIndex const & parent)
+void TocModel::populate(unsigned int & index, QModelIndex const & parent)
{
- int curdepth = iter->depth() + 1;
+ int curdepth = toc_[index].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;
+ size_t end = toc_.size();
+ ++index;
+ for (; index != end; ++index) {
+ TocItem const & item = toc_[index];
+ if (item.depth() < curdepth) {
+ --index;
return;
}
-
- maxdepth_ = max(maxdepth_, iter->depth());
- mindepth_ = min(mindepth_, iter->depth());
+ maxdepth_ = max(maxdepth_, item.depth());
+ mindepth_ = min(mindepth_, item.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);
-
- // 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);
+ setData(child_item, toqstr(item.str()), Qt::DisplayRole);
+ setData(child_item, index, Qt::UserRole);
+ populate(index, child_item);
+ if (index >= end)
+ break;
}
}
{
if (type < 0 || !bv_)
return QModelIndex();
-
- TocIterator const it = bv_->buffer().masterBuffer()->tocBackend().item(
- fromqstr(types_[type]), bv_->cursor());
- return models_[type]->modelIndex(it);
+ return models_[type]->modelIndex(bv_->cursor());
}
}
LASSERT(type >= 0 && type < int(models_.size()), /**/);
- TocIterator const it = models_[type]->tocIterator(index);
- LYXERR(Debug::GUI, "TocModels::goTo " << it->str());
- dispatch(it->action());
+ TocItem const item = models_[type]->tocItem(index);
+ LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
+ dispatch(item.action());
}
}
-QString TocModels::guiName(string const & type) const
-{
- if (type == "tableofcontents")
- return qt_("Table of Contents");
- if (type == "child")
- return qt_("Child Documents");
- if (type == "graphics")
- return qt_("List of Graphics");
- if (type == "equation")
- return qt_("List of Equations");
- if (type == "footnote")
- return qt_("List of Footnotes");
- if (type == "listing")
- return qt_("List of Listings");
- if (type == "index")
- return qt_("List of Indexes");
- if (type == "marginalnote")
- return qt_("List of Marginal notes");
- if (type == "note")
- return qt_("List of Notes");
- if (type == "citation")
- return qt_("List of Citations");
- if (type == "label")
- return qt_("Labels and References");
-
- FloatList const & floats = bv_->buffer().params().documentClass().floats();
- if (floats.typeExist(type))
- return qt_(floats.getType(type).listName());
-
- return qt_(type);
-}
-
-
void TocModels::reset(BufferView const * bv)
{
bv_ = bv;
TocList::const_iterator end = tocs.end();
for (; it != end; ++it) {
types_.push_back(toqstr(it->first));
- type_names_.push_back(guiName(it->first));
+ type_names_.push_back(guiName(it->first, bv->buffer().params()));
models_.push_back(new TocModel(it->second));
}
}
// Default to Outliner.
new_type = "tableofcontents";
}
- return types_.indexOf(new_type);
+ int const type = types_.indexOf(new_type);
+ if (type != -1)
+ return type;
+ // If everything else fails, settle on the table of contents which is
+ // guaranted to exist.
+ return types_.indexOf("tableofcontents");
}
} // namespace frontend