2006-04-18 09:57:47 +00:00
|
|
|
/**
|
2007-11-14 00:21:31 +00:00
|
|
|
* \file TocModel.cpp
|
2006-04-18 09:57:47 +00:00
|
|
|
* 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"
|
|
|
|
|
2008-05-02 12:09:51 +00:00
|
|
|
#include "Buffer.h"
|
|
|
|
#include "BufferView.h"
|
|
|
|
#include "Cursor.h"
|
2008-05-16 13:49:49 +00:00
|
|
|
#include "DocIterator.h"
|
2008-05-02 12:09:51 +00:00
|
|
|
#include "FuncRequest.h"
|
|
|
|
#include "LyXFunc.h"
|
|
|
|
|
|
|
|
#include "support/convert.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2008-05-02 12:09:51 +00:00
|
|
|
|
2008-02-07 17:04:06 +00:00
|
|
|
#include <climits>
|
2007-09-16 10:36:57 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2006-04-18 09:57:47 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
2006-04-28 09:16:48 +00:00
|
|
|
|
2008-03-26 22:25:43 +00:00
|
|
|
typedef std::pair<QModelIndex, TocIterator> TocPair;
|
2006-04-28 09:16:48 +00:00
|
|
|
|
2006-04-18 09:57:47 +00:00
|
|
|
|
2008-03-26 22:25:43 +00:00
|
|
|
TocIterator 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);
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(map_it != toc_map_.end(), /**/);
|
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
|
|
|
|
2008-03-26 22:25:43 +00:00
|
|
|
QModelIndex 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);
|
2008-04-10 21:49:34 +00:00
|
|
|
//LASSERT(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();
|
2007-05-28 22:27:45 +00:00
|
|
|
|
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-11-25 22:16:22 +00:00
|
|
|
maxdepth_ = 0;
|
|
|
|
mindepth_ = INT_MAX;
|
2006-04-18 09:57:47 +00:00
|
|
|
|
|
|
|
while (iter != end) {
|
2007-06-12 12:29:19 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GUI, "Toc: at depth " << iter->depth()
|
2008-04-20 20:32:00 +00:00
|
|
|
<< ", added item " << toqstr(iter->str()));
|
2007-06-12 12:29:19 +00:00
|
|
|
|
|
|
|
populate(iter, end, top_level_item);
|
2006-04-18 09:57:47 +00:00
|
|
|
|
|
|
|
if (iter == end)
|
|
|
|
break;
|
|
|
|
|
|
|
|
++iter;
|
|
|
|
}
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2006-04-18 09:57:47 +00:00
|
|
|
setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
|
|
|
|
// emit headerDataChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-14 00:21:31 +00:00
|
|
|
void TocModel::populate(TocIterator & iter, TocIterator const & end,
|
|
|
|
QModelIndex const & parent)
|
2006-04-18 09:57:47 +00:00
|
|
|
{
|
2006-04-22 18:48:28 +00:00
|
|
|
int curdepth = iter->depth() + 1;
|
2007-05-28 22:27:45 +00:00
|
|
|
|
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
|
|
|
|
2008-04-20 20:32:00 +00:00
|
|
|
while (iter != end) {
|
2006-04-18 09:57:47 +00:00
|
|
|
++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;
|
|
|
|
}
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2006-11-25 22:16:22 +00:00
|
|
|
maxdepth_ = max(maxdepth_, iter->depth());
|
|
|
|
mindepth_ = min(mindepth_, iter->depth());
|
2006-04-18 09:57:47 +00:00
|
|
|
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
|
|
|
|
2007-11-14 00:21:31 +00:00
|
|
|
int TocModel::modelDepth() const
|
2006-11-25 22:16:22 +00:00
|
|
|
{
|
|
|
|
return maxdepth_ - mindepth_;
|
|
|
|
}
|
|
|
|
|
2008-05-02 12:09:51 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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();
|
|
|
|
|
2008-05-16 13:11:43 +00:00
|
|
|
TocIterator const it = bv_->buffer().masterBuffer()->tocBackend().item(
|
|
|
|
fromqstr(types_[type]), bv_->cursor());
|
|
|
|
return models_[type]->modelIndex(it);
|
2008-05-02 12:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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());
|
2008-05-16 12:48:13 +00:00
|
|
|
dispatch(it->action());
|
2008-05-02 12:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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));
|
2008-05-16 13:49:49 +00:00
|
|
|
type_names_.push_back(guiName(it->first, bv->buffer().params()));
|
2008-05-02 12:09:51 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2006-04-18 09:57:47 +00:00
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
2006-05-18 08:51:12 +00:00
|
|
|
|
|
|
|
#include "TocModel_moc.cpp"
|