2001-08-19 13:25:15 +00:00
|
|
|
/**
|
|
|
|
* \file QToc.C
|
2002-09-24 13:57:09 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2001-08-19 13:25:15 +00:00
|
|
|
*
|
2002-09-24 13:57:09 +00:00
|
|
|
* \author John Levon
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2001-03-23 06:31:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2002-06-19 03:38:44 +00:00
|
|
|
#include "QToc.h"
|
|
|
|
#include "QTocDialog.h"
|
|
|
|
#include "Qt2BC.h"
|
2004-05-20 09:36:28 +00:00
|
|
|
#include "qt_helpers.h"
|
2002-06-19 03:38:44 +00:00
|
|
|
|
2004-05-20 09:36:28 +00:00
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
#include "controllers/ControlToc.h"
|
|
|
|
|
|
|
|
#include <qcombobox.h>
|
2002-06-19 03:38:44 +00:00
|
|
|
#include <qlistview.h>
|
|
|
|
#include <qpushbutton.h>
|
2004-05-20 09:36:28 +00:00
|
|
|
|
|
|
|
#include <stack>
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-01-25 19:34:22 +00:00
|
|
|
using std::endl;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
2002-01-25 19:34:22 +00:00
|
|
|
using std::pair;
|
|
|
|
using std::stack;
|
2001-03-23 06:31:30 +00:00
|
|
|
using std::vector;
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2004-05-19 15:11:37 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
2003-09-08 00:33:41 +00:00
|
|
|
|
2003-02-25 14:51:38 +00:00
|
|
|
typedef QController<ControlToc, QView<QTocDialog> > base_class;
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2003-02-25 14:51:38 +00:00
|
|
|
QToc::QToc(Dialog & parent)
|
2003-05-22 15:42:50 +00:00
|
|
|
: base_class(parent, _("LyX: Table of Contents")), depth_(1)
|
2002-01-20 18:38:52 +00:00
|
|
|
{}
|
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
|
2003-03-10 03:13:28 +00:00
|
|
|
bcview().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();
|
2004-05-19 15:11:37 +00:00
|
|
|
string const & type = toc::getType(controller().params().getCmdName());
|
2002-01-20 18:38:52 +00:00
|
|
|
|
|
|
|
for (vector<string>::const_iterator it = choice.begin();
|
|
|
|
it != choice.end(); ++it) {
|
2002-12-17 20:37:13 +00:00
|
|
|
dialog_->typeCO->insertItem(toqstr(*it));
|
2002-01-20 18:38:52 +00:00
|
|
|
if (*it == type) {
|
|
|
|
dialog_->typeCO->setCurrentItem(it - choice.begin());
|
2003-05-22 15:42:50 +00:00
|
|
|
setTitle(type);
|
2002-01-20 18:38:52 +00:00
|
|
|
}
|
|
|
|
}
|
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-12-17 20:37:13 +00:00
|
|
|
string type = fromqstr(dialog_->typeCO->currentText());
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2004-05-19 15:11:37 +00:00
|
|
|
toc::Toc 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;
|
2002-10-20 01:48:28 +00:00
|
|
|
stack<pair<QListViewItem *, QListViewItem *> > istack;
|
|
|
|
QListViewItem * last = 0;
|
|
|
|
QListViewItem * parent = 0;
|
|
|
|
QListViewItem * item;
|
2001-03-23 06:31:30 +00:00
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
// 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
|
2001-03-23 06:31:30 +00:00
|
|
|
|
2004-05-19 15:11:37 +00:00
|
|
|
for (toc::Toc::const_iterator iter = toclist.begin();
|
2002-10-20 01:48:28 +00:00
|
|
|
iter != toclist.end(); ++iter) {
|
2001-03-23 06:31:30 +00:00
|
|
|
if (iter->depth == curdepth) {
|
|
|
|
// insert it after the last one we processed
|
|
|
|
if (!parent)
|
2002-10-20 01:48:28 +00:00
|
|
|
item = (last ? new QListViewItem(dialog_->tocLV,last) : new QListViewItem(dialog_->tocLV));
|
2001-03-23 06:31:30 +00:00
|
|
|
else
|
2002-10-20 01:48:28 +00:00
|
|
|
item = (last ? new QListViewItem(parent,last) : new QListViewItem(parent));
|
2001-03-23 06:31:30 +00:00
|
|
|
} 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));
|
2002-10-20 01:48:28 +00:00
|
|
|
item = (last ? new QListViewItem(last) : new QListViewItem(dialog_->tocLV));
|
2001-03-23 06:31:30 +00:00
|
|
|
parent = last;
|
|
|
|
} else {
|
|
|
|
int diff = curdepth - iter->depth;
|
2002-10-20 01:48:28 +00:00
|
|
|
pair<QListViewItem *, QListViewItem * > top;
|
2001-03-23 06:31:30 +00:00
|
|
|
// 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)
|
2002-10-20 01:48:28 +00:00
|
|
|
item = (last ? new QListViewItem(dialog_->tocLV,last) : new QListViewItem(dialog_->tocLV));
|
2001-03-23 06:31:30 +00:00
|
|
|
else
|
2002-10-20 01:48:28 +00:00
|
|
|
item = (last ? new QListViewItem(parent,last) : new QListViewItem(parent));
|
2001-03-23 06:31:30 +00:00
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
lyxerr[Debug::GUI]
|
|
|
|
<< "Table of contents\n"
|
2002-12-17 20:37:13 +00:00
|
|
|
<< "Added item " << iter->str
|
2002-10-20 01:48:28 +00:00
|
|
|
<< " at depth " << iter->depth
|
|
|
|
<< ", previous sibling \""
|
2002-12-17 20:37:13 +00:00
|
|
|
<< (last ? fromqstr(last->text(0)) : "0")
|
2002-10-20 01:48:28 +00:00
|
|
|
<< "\", parent \""
|
2002-12-17 20:37:13 +00:00
|
|
|
<< (parent ? fromqstr(parent->text(0)) : "0") << '"'
|
2002-10-20 01:48:28 +00:00
|
|
|
<< endl;
|
2002-12-17 20:37:13 +00:00
|
|
|
item->setText(0, toqstr(iter->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
|
|
|
{
|
2004-05-19 15:11:37 +00:00
|
|
|
toc::Toc::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()) {
|
2003-01-06 14:02:24 +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-07-21 18:13:13 +00:00
|
|
|
controller().goTo(*iter);
|
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
|
|
|
}
|
2004-05-19 15:11:37 +00:00
|
|
|
|
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|