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"
|
2008-06-12 15:31:10 +00:00
|
|
|
#include "TocBackend.h"
|
2008-05-02 12:09:51 +00:00
|
|
|
|
|
|
|
#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-06-12 15:31:10 +00:00
|
|
|
TocItem const & TocModel::tocItem(QModelIndex const & index) const
|
2006-04-18 09:57:47 +00:00
|
|
|
{
|
2008-06-12 15:31:10 +00:00
|
|
|
return toc_[data(index, Qt::UserRole).toUInt()];
|
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-06-12 15:31:10 +00:00
|
|
|
QModelIndex TocModel::modelIndex(DocIterator const & dit) const
|
2006-04-18 09:57:47 +00:00
|
|
|
{
|
2008-06-12 15:31:10 +00:00
|
|
|
size_t const toc_index = toc_.item(dit) - toc_.begin();
|
2006-04-18 09:57:47 +00:00
|
|
|
|
2008-06-12 15:31:10 +00:00
|
|
|
QModelIndexList list = match(index(0, 0), Qt::UserRole,
|
|
|
|
QVariant(toc_index), 1,
|
|
|
|
Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2008-06-12 15:31:10 +00:00
|
|
|
LASSERT(!list.isEmpty(), return QModelIndex());
|
|
|
|
return list[0];
|
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-06-12 15:31:10 +00:00
|
|
|
TocModel::TocModel(Toc const & toc): toc_(toc)
|
2006-04-18 09:57:47 +00:00
|
|
|
{
|
2008-06-12 15:31:10 +00:00
|
|
|
if (toc_.empty())
|
2006-04-18 09:57:47 +00:00
|
|
|
return;
|
|
|
|
int current_row;
|
|
|
|
QModelIndex top_level_item;
|
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
|
|
|
|
2008-06-12 15:31:10 +00:00
|
|
|
size_t end = toc.size();
|
|
|
|
for (size_t index = 0; index != end; ++index) {
|
|
|
|
TocItem const & item = toc_[index];
|
|
|
|
maxdepth_ = max(maxdepth_, item.depth());
|
|
|
|
mindepth_ = min(mindepth_, item.depth());
|
2007-06-12 12:29:19 +00:00
|
|
|
current_row = rowCount();
|
|
|
|
insertRows(current_row, 1);
|
|
|
|
top_level_item = QStandardItemModel::index(current_row, 0);
|
2008-06-12 15:31:10 +00:00
|
|
|
setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
|
|
|
|
setData(top_level_item, index, Qt::UserRole);
|
2007-06-12 12:29:19 +00:00
|
|
|
|
2008-06-12 15:31:10 +00:00
|
|
|
LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
|
|
|
|
<< ", added item " << item.str());
|
2006-04-18 09:57:47 +00:00
|
|
|
|
2008-06-12 15:31:10 +00:00
|
|
|
populate(index, top_level_item);
|
|
|
|
if (index >= end)
|
2006-04-18 09:57:47 +00:00
|
|
|
break;
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-12 15:31:10 +00:00
|
|
|
void TocModel::populate(size_t & index, QModelIndex const & parent)
|
2006-04-18 09:57:47 +00:00
|
|
|
{
|
2008-06-12 15:31:10 +00:00
|
|
|
int curdepth = toc_[index].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-06-12 15:31:10 +00:00
|
|
|
size_t end = toc_.size();
|
|
|
|
++index;
|
|
|
|
for (; index != end; ++index) {
|
|
|
|
TocItem const & item = toc_[index];
|
|
|
|
if (item.depth() < curdepth) {
|
|
|
|
--index;
|
2006-04-18 09:57:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-06-12 15:31:10 +00:00
|
|
|
maxdepth_ = max(maxdepth_, item.depth());
|
|
|
|
mindepth_ = min(mindepth_, item.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);
|
2008-06-12 15:31:10 +00:00
|
|
|
setData(child_item, toqstr(item.str()), Qt::DisplayRole);
|
|
|
|
setData(child_item, index, Qt::UserRole);
|
|
|
|
populate(index, child_item);
|
|
|
|
if (index >= end)
|
|
|
|
break;
|
2006-04-18 09:57:47 +00:00
|
|
|
}
|
|
|
|
}
|
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-06-12 15:31:10 +00:00
|
|
|
return models_[type]->modelIndex(bv_->cursor());
|
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()), /**/);
|
2008-06-12 15:31:10 +00:00
|
|
|
TocItem const item = models_[type]->tocItem(index);
|
|
|
|
LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
|
|
|
|
dispatch(item.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";
|
|
|
|
}
|
2008-06-02 13:59:00 +00:00
|
|
|
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");
|
2008-05-02 12:09:51 +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"
|