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-06-18 11:35:24 +00:00
|
|
|
#include <QSortFilterProxyModel>
|
|
|
|
|
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-17 15:10:03 +00:00
|
|
|
TocTypeModel::TocTypeModel(QObject * parent): QStandardItemModel(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TocTypeModel::reset()
|
|
|
|
{
|
|
|
|
QStandardItemModel::reset();
|
|
|
|
}
|
|
|
|
|
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-17 15:10:03 +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-17 15:10:03 +00:00
|
|
|
if (toc_->empty())
|
2008-06-12 15:43:31 +00:00
|
|
|
return QModelIndex();
|
|
|
|
|
2008-06-17 15:10:03 +00:00
|
|
|
unsigned int 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-09-11 21:35:26 +00:00
|
|
|
TocModel::TocModel(QObject * parent): QStandardItemModel(parent),
|
|
|
|
maxdepth_(0), mindepth_(0)
|
2008-06-17 15:10:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TocModel::reset()
|
|
|
|
{
|
|
|
|
QStandardItemModel::reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TocModel::reset(Toc const & toc)
|
2006-04-18 09:57:47 +00:00
|
|
|
{
|
2008-06-17 15:10:03 +00:00
|
|
|
toc_ = &toc;
|
|
|
|
if (toc_->empty()) {
|
2008-09-11 21:15:45 +00:00
|
|
|
maxdepth_ = 0;
|
|
|
|
mindepth_ = 0;
|
2008-06-17 15:10:03 +00:00
|
|
|
reset();
|
2006-04-18 09:57:47 +00:00
|
|
|
return;
|
2008-06-17 15:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
blockSignals(true);
|
2006-04-18 09:57:47 +00:00
|
|
|
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-17 15:10:03 +00:00
|
|
|
size_t end = toc_->size();
|
2008-06-12 17:34:01 +00:00
|
|
|
for (unsigned int index = 0; index != end; ++index) {
|
2008-06-17 15:10:03 +00:00
|
|
|
TocItem const & item = (*toc_)[index];
|
2008-06-12 15:31:10 +00:00
|
|
|
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);
|
2008-06-17 15:10:03 +00:00
|
|
|
blockSignals(false);
|
|
|
|
reset();
|
2006-04-18 09:57:47 +00:00
|
|
|
// emit headerDataChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-12 17:34:01 +00:00
|
|
|
void TocModel::populate(unsigned int & index, QModelIndex const & parent)
|
2006-04-18 09:57:47 +00:00
|
|
|
{
|
2008-06-17 15:10:03 +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-17 15:10:03 +00:00
|
|
|
size_t end = toc_->size();
|
2008-06-12 15:31:10 +00:00
|
|
|
++index;
|
|
|
|
for (; index != end; ++index) {
|
2008-06-17 15:10:03 +00:00
|
|
|
TocItem const & item = (*toc_)[index];
|
2008-06-12 15:31:10 +00:00
|
|
|
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
|
|
|
{
|
2008-09-11 21:02:18 +00:00
|
|
|
int const d = maxdepth_ - mindepth_;
|
|
|
|
LASSERT(d >= 0 && d <= 100, /* */);
|
|
|
|
return d;
|
2006-11-25 22:16:22 +00:00
|
|
|
}
|
|
|
|
|
2008-05-02 12:09:51 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TocModels implementation.
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2008-06-17 15:10:03 +00:00
|
|
|
|
|
|
|
TocModels::TocModels(): bv_(0)
|
|
|
|
{
|
|
|
|
names_ = new TocTypeModel(this);
|
2008-06-18 11:35:24 +00:00
|
|
|
names_sorted_ = new QSortFilterProxyModel(this);
|
|
|
|
names_sorted_->setSourceModel(names_);
|
|
|
|
#if QT_VERSION >= 0x040300
|
|
|
|
names_sorted_->setSortLocaleAware(true);
|
|
|
|
#endif
|
|
|
|
names_sorted_->sort(0);
|
2008-06-17 15:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-02 12:09:51 +00:00
|
|
|
void TocModels::clear()
|
|
|
|
{
|
2008-06-17 15:10:03 +00:00
|
|
|
names_->blockSignals(true);
|
|
|
|
names_->clear();
|
|
|
|
names_->blockSignals(false);
|
|
|
|
iterator end = models_.end();
|
|
|
|
for (iterator it = models_.begin(); it != end; ++it) {
|
|
|
|
it.value()->blockSignals(true);
|
|
|
|
it.value()->clear();
|
|
|
|
it.value()->blockSignals(false);
|
2008-05-02 12:09:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-17 15:10:03 +00:00
|
|
|
int TocModels::depth(QString const & type)
|
2008-05-02 12:09:51 +00:00
|
|
|
{
|
2008-06-17 15:10:03 +00:00
|
|
|
const_iterator it = models_.find(type);
|
|
|
|
if (!bv_ || it == models_.end())
|
2008-05-02 12:09:51 +00:00
|
|
|
return 0;
|
2008-06-17 15:10:03 +00:00
|
|
|
return it.value()->modelDepth();
|
2008-05-02 12:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-17 15:10:03 +00:00
|
|
|
QStandardItemModel * TocModels::model(QString const & type)
|
2008-05-02 12:09:51 +00:00
|
|
|
{
|
2008-06-17 15:10:03 +00:00
|
|
|
if (!bv_)
|
2008-05-02 12:09:51 +00:00
|
|
|
return 0;
|
2008-06-17 15:10:03 +00:00
|
|
|
iterator it = models_.find(type);
|
|
|
|
if (it != models_.end())
|
|
|
|
return it.value();
|
|
|
|
LYXERR0("type not found: " << type);
|
|
|
|
return 0;
|
2008-05-02 12:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-18 11:35:24 +00:00
|
|
|
QAbstractItemModel * TocModels::nameModel()
|
|
|
|
{
|
|
|
|
return names_sorted_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-17 15:10:03 +00:00
|
|
|
QModelIndex TocModels::currentIndex(QString const & type) const
|
2008-05-02 12:09:51 +00:00
|
|
|
{
|
2008-06-17 15:10:03 +00:00
|
|
|
const_iterator it = models_.find(type);
|
|
|
|
if (!bv_ || it == models_.end())
|
2008-05-02 12:09:51 +00:00
|
|
|
return QModelIndex();
|
2008-06-17 15:10:03 +00:00
|
|
|
return it.value()->modelIndex(bv_->cursor());
|
2008-05-02 12:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-17 15:10:03 +00:00
|
|
|
void TocModels::goTo(QString const & type, QModelIndex const & index) const
|
2008-05-02 12:09:51 +00:00
|
|
|
{
|
2008-06-17 15:10:03 +00:00
|
|
|
const_iterator it = models_.find(type);
|
|
|
|
if (it == models_.end() || !index.isValid()) {
|
2008-05-02 12:09:51 +00:00
|
|
|
LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
|
|
|
|
return;
|
|
|
|
}
|
2008-06-17 15:10:03 +00:00
|
|
|
LASSERT(index.model() == it.value(), return);
|
|
|
|
TocItem const item = it.value()->tocItem(index);
|
2008-06-12 15:31:10 +00:00
|
|
|
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();
|
2008-06-17 15:10:03 +00:00
|
|
|
if (!bv_) {
|
|
|
|
iterator end = models_.end();
|
|
|
|
for (iterator it = models_.begin(); it != end; ++it)
|
|
|
|
it.value()->reset();
|
|
|
|
names_->reset();
|
2008-05-02 12:09:51 +00:00
|
|
|
return;
|
2008-06-17 15:10:03 +00:00
|
|
|
}
|
2008-05-02 12:09:51 +00:00
|
|
|
|
2008-06-17 15:10:03 +00:00
|
|
|
names_->blockSignals(true);
|
|
|
|
names_->insertColumns(0, 1);
|
2008-05-02 12:09:51 +00:00
|
|
|
TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
|
|
|
|
TocList::const_iterator it = tocs.begin();
|
2008-06-17 15:10:03 +00:00
|
|
|
TocList::const_iterator toc_end = tocs.end();
|
|
|
|
for (; it != toc_end; ++it) {
|
|
|
|
QString const type = toqstr(it->first);
|
|
|
|
|
|
|
|
// First, fill in the toc models.
|
|
|
|
iterator mod_it = models_.find(type);
|
|
|
|
if (mod_it == models_.end())
|
|
|
|
mod_it = models_.insert(type, new TocModel(this));
|
|
|
|
mod_it.value()->reset(it->second);
|
|
|
|
|
|
|
|
// Fill in the names_ model.
|
|
|
|
QString const gui_name = guiName(it->first, bv->buffer().params());
|
|
|
|
int const current_row = names_->rowCount();
|
|
|
|
names_->insertRows(current_row, 1);
|
|
|
|
QModelIndex const index = names_->index(current_row, 0);
|
|
|
|
names_->setData(index, gui_name, Qt::DisplayRole);
|
|
|
|
names_->setData(index, type, Qt::UserRole);
|
2008-05-02 12:09:51 +00:00
|
|
|
}
|
2008-06-17 15:10:03 +00:00
|
|
|
names_->blockSignals(false);
|
|
|
|
names_->reset();
|
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"
|