2006-04-18 09:57:47 +00:00
|
|
|
/**
|
|
|
|
* \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.
|
|
|
|
*/
|
|
|
|
|
2006-07-08 13:27:43 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2006-04-18 09:57:47 +00:00
|
|
|
#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 {
|
2006-04-28 09:16:48 +00:00
|
|
|
|
2006-04-18 09:57:47 +00:00
|
|
|
|
2006-11-13 16:53:49 +00:00
|
|
|
TocModel::TocModel(Toc const & toc)
|
2006-04-18 09:57:47 +00:00
|
|
|
{
|
2006-04-22 18:48:28 +00:00
|
|
|
populate(toc);
|
2006-04-18 09:57:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-13 16:53:49 +00:00
|
|
|
TocModel const & TocModel::operator=(Toc const & toc)
|
2006-04-18 09:57:47 +00:00
|
|
|
{
|
2006-04-22 18:48:28 +00:00
|
|
|
populate(toc);
|
2006-04-18 09:57:47 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2006-04-28 09:16:48 +00:00
|
|
|
|
2006-04-18 09:57:47 +00:00
|
|
|
|
2006-04-22 18:48:28 +00:00
|
|
|
TocIterator const TocModel::tocIterator(QModelIndex const & index) const
|
2006-04-18 09:57:47 +00:00
|
|
|
{
|
2006-04-22 18:48:28 +00:00
|
|
|
TocMap::const_iterator map_it = toc_map_.find(index);
|
|
|
|
BOOST_ASSERT(map_it != toc_map_.end());
|
|
|
|
return map_it->second;
|
2006-04-18 09:57:47 +00:00
|
|
|
}
|
2006-04-28 09:16:48 +00:00
|
|
|
|
2006-04-18 09:57:47 +00:00
|
|
|
|
2006-04-22 18:48:28 +00:00
|
|
|
QModelIndex const TocModel::modelIndex(TocIterator const & it) const
|
2006-04-18 09:57:47 +00:00
|
|
|
{
|
2006-04-22 18:48:28 +00:00
|
|
|
ModelMap::const_iterator map_it = model_map_.find(it);
|
|
|
|
//BOOST_ASSERT(it != model_map_.end());
|
2006-04-18 09:57:47 +00:00
|
|
|
|
2006-04-22 18:48:28 +00:00
|
|
|
if (map_it == model_map_.end())
|
2006-04-18 09:57:47 +00:00
|
|
|
return QModelIndex();
|
|
|
|
|
2006-04-22 18:48:28 +00:00
|
|
|
return map_it->second;
|
2006-04-18 09:57:47 +00:00
|
|
|
}
|
2006-04-28 09:16:48 +00:00
|
|
|
|
2006-04-18 09:57:47 +00:00
|
|
|
|
|
|
|
void TocModel::clear()
|
|
|
|
{
|
|
|
|
QStandardItemModel::clear();
|
2006-04-22 18:48:28 +00:00
|
|
|
toc_map_.clear();
|
|
|
|
model_map_.clear();
|
2006-04-18 09:57:47 +00:00
|
|
|
removeRows(0, rowCount());
|
|
|
|
removeColumns(0, columnCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-13 16:53:49 +00:00
|
|
|
void TocModel::populate(Toc const & toc)
|
2006-04-18 09:57:47 +00:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
|
2006-04-22 18:48:28 +00:00
|
|
|
if (toc.empty())
|
2006-04-18 09:57:47 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
int current_row;
|
|
|
|
QModelIndex top_level_item;
|
|
|
|
|
2006-04-22 18:48:28 +00:00
|
|
|
TocIterator iter = toc.begin();
|
|
|
|
TocIterator end = toc.end();
|
2006-04-18 09:57:47 +00:00
|
|
|
|
2006-04-28 09:16:48 +00:00
|
|
|
insertColumns(0, 1);
|
2006-04-18 09:57:47 +00:00
|
|
|
|
|
|
|
while (iter != end) {
|
|
|
|
|
2006-04-28 09:16:48 +00:00
|
|
|
if (iter->isValid()) {
|
2006-04-18 09:57:47 +00:00
|
|
|
|
|
|
|
current_row = rowCount();
|
|
|
|
insertRows(current_row, 1);
|
|
|
|
top_level_item = QStandardItemModel::index(current_row, 0);
|
2006-04-22 18:48:28 +00:00
|
|
|
//setData(top_level_item, toqstr(iter->str()));
|
|
|
|
setData(top_level_item, toqstr(iter->str()), Qt::DisplayRole);
|
2006-11-09 13:13:28 +00:00
|
|
|
|
|
|
|
// 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) );
|
2006-04-28 09:16:48 +00:00
|
|
|
model_map_[iter] = top_level_item;
|
2006-04-18 09:57:47 +00:00
|
|
|
|
|
|
|
lyxerr[Debug::GUI]
|
2006-04-22 18:48:28 +00:00
|
|
|
<< "Toc: at depth " << iter->depth()
|
2006-10-11 19:40:50 +00:00
|
|
|
<< ", added item " << lyx::to_utf8(iter->str())
|
2006-04-18 09:57:47 +00:00
|
|
|
<< endl;
|
|
|
|
|
|
|
|
populate(iter, end, top_level_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iter == end)
|
|
|
|
break;
|
|
|
|
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
|
|
|
|
// emit headerDataChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-22 18:48:28 +00:00
|
|
|
void TocModel::populate(TocIterator & iter,
|
|
|
|
TocIterator const & end,
|
2006-04-18 09:57:47 +00:00
|
|
|
QModelIndex const & parent)
|
|
|
|
{
|
2006-04-22 18:48:28 +00:00
|
|
|
int curdepth = iter->depth() + 1;
|
2006-04-18 09:57:47 +00:00
|
|
|
int current_row;
|
|
|
|
QModelIndex child_item;
|
|
|
|
|
2006-04-28 09:16:48 +00:00
|
|
|
insertColumns(0, 1, parent);
|
2006-04-18 09:57:47 +00:00
|
|
|
while (iter != end) {
|
|
|
|
|
|
|
|
++iter;
|
|
|
|
|
|
|
|
if (iter == end)
|
|
|
|
break;
|
|
|
|
|
2006-04-22 18:48:28 +00:00
|
|
|
if (iter->depth() < curdepth) {
|
2006-04-18 09:57:47 +00:00
|
|
|
--iter;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
current_row = rowCount(parent);
|
|
|
|
insertRows(current_row, 1, parent);
|
|
|
|
child_item = QStandardItemModel::index(current_row, 0, parent);
|
2006-04-22 18:48:28 +00:00
|
|
|
//setData(child_item, toqstr(iter->str()));
|
|
|
|
setData(child_item, toqstr(iter->str()), Qt::DisplayRole);
|
2006-11-09 13:13:28 +00:00
|
|
|
|
|
|
|
// 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) );
|
2006-04-28 09:16:48 +00:00
|
|
|
model_map_[iter] = child_item;
|
2006-04-18 09:57:47 +00:00
|
|
|
populate(iter, end, child_item);
|
|
|
|
}
|
|
|
|
}
|
2006-04-28 09:16:48 +00:00
|
|
|
|
2006-04-18 09:57:47 +00:00
|
|
|
|
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
2006-05-18 08:51:12 +00:00
|
|
|
|
|
|
|
#include "TocModel_moc.cpp"
|