2001-08-19 13:25:15 +00:00
|
|
|
/**
|
|
|
|
* \file QToc.C
|
|
|
|
* Copyright 2001 the LyX Team
|
|
|
|
* Read the file COPYING
|
|
|
|
*
|
|
|
|
* \author John Levon <moz@compsoc.man.ac.uk>
|
2001-03-23 06:31:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
2001-03-23 06:31:30 +00:00
|
|
|
#include <stack>
|
|
|
|
|
|
|
|
#include <qslider.h>
|
2002-01-20 18:38:52 +00:00
|
|
|
#include <qlistview.h>
|
|
|
|
#include <qpushbutton.h>
|
|
|
|
#include <qcombobox.h>
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2001-08-19 13:25:15 +00:00
|
|
|
#include "QTocDialog.h"
|
|
|
|
#include "QToc.h"
|
2002-03-21 21:21:28 +00:00
|
|
|
#include "Qt2BC.h"
|
2001-03-23 06:31:30 +00:00
|
|
|
#include "gettext.h"
|
|
|
|
#include "support/lstrings.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
#include "QtLyXView.h"
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-01-25 19:34:22 +00:00
|
|
|
using std::endl;
|
|
|
|
using std::pair;
|
|
|
|
using std::stack;
|
2001-03-23 06:31:30 +00:00
|
|
|
using std::vector;
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
typedef Qt2CB<ControlToc, Qt2DB<QTocDialog> > base_class;
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
QToc::QToc(ControlToc & c)
|
|
|
|
: base_class(c, _("Table of contents"))
|
|
|
|
{}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
void QToc::build_dialog()
|
2001-03-23 06:31:30 +00:00
|
|
|
{
|
2002-01-20 18:38:52 +00:00
|
|
|
dialog_.reset(new QTocDialog(this));
|
2001-03-23 06:31:30 +00:00
|
|
|
|
2002-03-21 21:21:28 +00:00
|
|
|
// Manage the cancel/close button
|
2002-01-20 18:38:52 +00:00
|
|
|
bc().setCancel(dialog_->closePB);
|
2001-03-23 06:31:30 +00:00
|
|
|
}
|
|
|
|
|
2001-08-19 13:25:15 +00:00
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
void QToc::updateType()
|
2002-03-21 21:21:28 +00:00
|
|
|
{
|
2002-01-20 18:38:52 +00:00
|
|
|
dialog_->typeCO->clear();
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
vector<string> const & choice = controller().getTypes();
|
|
|
|
string const & type = toc::getType(controller().params().getCmdName());
|
|
|
|
|
|
|
|
for (vector<string>::const_iterator it = choice.begin();
|
|
|
|
it != choice.end(); ++it) {
|
|
|
|
dialog_->typeCO->insertItem(it->c_str());
|
|
|
|
if (*it == type) {
|
|
|
|
dialog_->typeCO->setCurrentItem(it - choice.begin());
|
|
|
|
dialog_->setCaption(type.c_str());
|
|
|
|
}
|
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
void QToc::update_contents()
|
2001-03-23 06:31:30 +00:00
|
|
|
{
|
2002-01-20 18:38:52 +00:00
|
|
|
updateType();
|
|
|
|
updateToc(depth_);
|
2001-03-23 06:31:30 +00:00
|
|
|
}
|
|
|
|
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2001-08-19 13:25:15 +00:00
|
|
|
void QToc::updateToc(int newdepth)
|
2001-03-23 06:31:30 +00:00
|
|
|
{
|
2002-02-06 15:36:29 +00:00
|
|
|
char const * str = dialog_->typeCO->currentText().latin1();
|
|
|
|
string type (str ? str : "");
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
Buffer::SingleList const & contents = controller().getContents(type);
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2001-03-23 06:31:30 +00:00
|
|
|
// Check if all elements are the same.
|
2002-01-20 18:38:52 +00:00
|
|
|
if (newdepth == depth_ && toclist == contents) {
|
|
|
|
return;
|
2001-03-23 06:31:30 +00:00
|
|
|
}
|
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
dialog_->tocLV->clear();
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
depth_ = newdepth;
|
2001-03-23 06:31:30 +00:00
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
toclist = contents;
|
2001-03-23 06:31:30 +00:00
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
if (toclist.empty())
|
2001-03-23 06:31:30 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
dialog_->tocLV->setUpdatesEnabled(false);
|
|
|
|
|
|
|
|
int curdepth = 0;
|
|
|
|
stack< pair< QListViewItem *, QListViewItem *> > istack;
|
|
|
|
QListViewItem *last = 0;
|
|
|
|
QListViewItem *parent = 0;
|
|
|
|
QListViewItem *item;
|
|
|
|
|
|
|
|
// Yes, it is this ugly. Two reasons - root items must have a QListView parent,
|
|
|
|
// rather than QListViewItem; and the TOC can move in and out an arbitrary number
|
|
|
|
// of levels
|
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
for (Buffer::SingleList::const_iterator iter = toclist.begin();
|
2001-03-23 06:31:30 +00:00
|
|
|
iter != toclist.end(); ++iter) {
|
|
|
|
if (iter->depth == curdepth) {
|
|
|
|
// insert it after the last one we processed
|
|
|
|
if (!parent)
|
|
|
|
item = (last) ? (new QListViewItem(dialog_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
|
|
|
|
else
|
|
|
|
item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
|
|
|
|
} else if (iter->depth > curdepth) {
|
|
|
|
int diff = iter->depth - curdepth;
|
|
|
|
// first save old parent and last
|
|
|
|
while (diff--)
|
|
|
|
istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
|
|
|
|
item = (last) ? (new QListViewItem(last)) : (new QListViewItem(dialog_->tocLV));
|
|
|
|
parent = last;
|
|
|
|
} else {
|
|
|
|
int diff = curdepth - iter->depth;
|
|
|
|
pair< QListViewItem *, QListViewItem * > top;
|
|
|
|
// restore context
|
|
|
|
while (diff--) {
|
|
|
|
top = istack.top();
|
|
|
|
istack.pop();
|
|
|
|
}
|
|
|
|
parent = top.first;
|
|
|
|
last = top.second;
|
|
|
|
// insert it after the last one we processed
|
|
|
|
if (!parent)
|
|
|
|
item = (last) ? (new QListViewItem(dialog_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
|
|
|
|
else
|
|
|
|
item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
|
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2001-03-23 06:31:30 +00:00
|
|
|
lyxerr[Debug::GUI] << "Table of contents" << endl << "Added item " << iter->str.c_str()
|
|
|
|
<< " at depth " << iter->depth << ", previous sibling \"" << (last ? last->text(0).latin1() : "0")
|
|
|
|
<< "\", parent \"" << (parent ? parent->text(0).latin1() : "0") << "\"" << endl;
|
|
|
|
item->setText(0,iter->str.c_str());
|
2002-01-20 18:38:52 +00:00
|
|
|
item->setOpen(iter->depth < depth_);
|
2001-03-23 06:31:30 +00:00
|
|
|
curdepth = iter->depth;
|
|
|
|
last = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog_->tocLV->setUpdatesEnabled(true);
|
|
|
|
dialog_->tocLV->update();
|
|
|
|
}
|
|
|
|
|
2001-08-19 13:25:15 +00:00
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
void QToc::select(string const & text)
|
2001-03-23 06:31:30 +00:00
|
|
|
{
|
2002-01-20 18:38:52 +00:00
|
|
|
Buffer::SingleList::const_iterator iter = toclist.begin();
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2001-03-23 06:31:30 +00:00
|
|
|
for (; iter != toclist.end(); ++iter) {
|
2002-01-20 18:38:52 +00:00
|
|
|
if (iter->str == text)
|
2001-03-23 06:31:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
if (iter == toclist.end()) {
|
2002-03-21 21:21:28 +00:00
|
|
|
lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry : "
|
2002-01-20 18:38:52 +00:00
|
|
|
<< text << endl;
|
2001-03-23 06:31:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
controller().Goto(iter->par->id());
|
2001-03-23 06:31:30 +00:00
|
|
|
}
|
|
|
|
|
2001-08-19 13:25:15 +00:00
|
|
|
|
2002-01-20 18:38:52 +00:00
|
|
|
void QToc::set_depth(int depth)
|
2001-03-23 06:31:30 +00:00
|
|
|
{
|
2002-01-20 18:38:52 +00:00
|
|
|
if (depth != depth_)
|
|
|
|
updateToc(depth);
|
2001-03-23 06:31:30 +00:00
|
|
|
}
|