lyx_mirror/src/frontends/qt4/TocModel.C

156 lines
2.9 KiB
C++
Raw Normal View History

/**
* \file QTocDialog.C
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author John Levon
* \author Abdelrazak Younes
*
* Full author contact details are available in file CREDITS.
*/
#include "TocModel.h"
#include "debug.h"
#include <vector>
#include <string>
using std::endl;
using std::pair;
using std::map;
using std::vector;
using std::string;
using std::make_pair;
namespace lyx {
namespace frontend {
TocModel::TocModel(TocBackend::Toc const & toc)
{
populate(toc);
}
TocModel const & TocModel::operator=(TocBackend::Toc const & toc)
{
populate(toc);
return *this;
}
TocIterator const TocModel::tocIterator(QModelIndex const & index) const
{
TocMap::const_iterator map_it = toc_map_.find(index);
BOOST_ASSERT(map_it != toc_map_.end());
return map_it->second;
}
QModelIndex const TocModel::modelIndex(TocIterator const & it) const
{
ModelMap::const_iterator map_it = model_map_.find(it);
//BOOST_ASSERT(it != model_map_.end());
if (map_it == model_map_.end())
return QModelIndex();
return map_it->second;
}
void TocModel::clear()
{
QStandardItemModel::clear();
toc_map_.clear();
model_map_.clear();
removeRows(0, rowCount());
removeColumns(0, columnCount());
}
void TocModel::populate(TocBackend::Toc const & toc)
{
clear();
if (toc.empty())
return;
int current_row;
QModelIndex top_level_item;
TocIterator iter = toc.begin();
TocIterator end = toc.end();
insertColumns(0, 1);
while (iter != end) {
if (iter->isValid()) {
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;
model_map_[iter] = top_level_item;
lyxerr[Debug::GUI]
<< "Toc: at depth " << iter->depth()
<< ", added item " << iter->str()
<< endl;
populate(iter, end, top_level_item);
}
if (iter == end)
break;
++iter;
}
setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
// emit headerDataChanged();
}
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) {
++iter;
if (iter == end)
break;
if (iter->depth() < curdepth) {
--iter;
return;
}
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;
model_map_[iter] = child_item;
populate(iter, end, child_item);
}
}
} // namespace frontend
} // namespace lyx
#include "TocModel_moc.cpp"