2006-03-05 17:24:44 +00:00
|
|
|
/**
|
2007-04-26 03:53:02 +00:00
|
|
|
* \file TocWidget.cpp
|
2006-03-05 17:24:44 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-03-12 14:23:44 +00:00
|
|
|
#include "TocWidget.h"
|
|
|
|
|
2008-05-30 11:33:24 +00:00
|
|
|
#include "GuiView.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
#include "qt_helpers.h"
|
2008-05-30 11:33:24 +00:00
|
|
|
#include "TocModel.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2008-06-17 15:44:14 +00:00
|
|
|
#include "Buffer.h"
|
2008-05-02 12:09:51 +00:00
|
|
|
#include "FuncRequest.h"
|
|
|
|
#include "LyXFunc.h"
|
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
2008-06-11 18:13:14 +00:00
|
|
|
#include "support/lassert.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-04-18 09:57:47 +00:00
|
|
|
#include <QHeaderView>
|
2008-03-04 12:39:03 +00:00
|
|
|
#include <QTimer>
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
using namespace std;
|
2006-04-18 09:57:47 +00:00
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
namespace lyx {
|
2007-05-04 17:46:03 +00:00
|
|
|
namespace frontend {
|
2007-03-12 14:23:44 +00:00
|
|
|
|
2008-05-30 11:33:24 +00:00
|
|
|
TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
|
|
|
|
: QWidget(parent), depth_(0), gui_view_(gui_view)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
|
|
|
setupUi(this);
|
|
|
|
|
2007-10-17 18:28:45 +00:00
|
|
|
moveOutTB->setIcon(QIcon(":/images/promote.png"));
|
|
|
|
moveInTB->setIcon(QIcon(":/images/demote.png"));
|
|
|
|
moveUpTB->setIcon(QIcon(":/images/up.png"));
|
|
|
|
moveDownTB->setIcon(QIcon(":/images/down.png"));
|
|
|
|
updateTB->setIcon(QIcon(":/images/reload.png"));
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2007-03-12 14:23:44 +00:00
|
|
|
// avoid flickering
|
|
|
|
tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-03-12 14:23:44 +00:00
|
|
|
tocTV->showColumn(0);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-03-12 14:23:44 +00:00
|
|
|
// hide the pointless QHeader for now
|
|
|
|
// in the future, new columns may appear
|
|
|
|
// like labels, bookmarks, etc...
|
|
|
|
// tocTV->header()->hide();
|
|
|
|
tocTV->header()->setVisible(false);
|
2007-05-04 17:46:03 +00:00
|
|
|
|
|
|
|
// Only one item selected at a time.
|
|
|
|
tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
|
2008-06-17 15:10:03 +00:00
|
|
|
|
|
|
|
// The toc types combo won't change its model.
|
|
|
|
typeCO->setModel(gui_view_.tocModels().nameModel());
|
2008-07-29 10:33:02 +00:00
|
|
|
|
|
|
|
// Make sure the buttons are disabled when first shown without a loaded
|
|
|
|
// Buffer.
|
|
|
|
enableControls(false);
|
2006-03-31 13:17:44 +00:00
|
|
|
}
|
|
|
|
|
2006-04-19 14:48:22 +00:00
|
|
|
|
2008-05-17 17:03:53 +00:00
|
|
|
void TocWidget::on_tocTV_activated(QModelIndex const & index)
|
2006-04-18 09:57:47 +00:00
|
|
|
{
|
2008-05-17 17:03:53 +00:00
|
|
|
goTo(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TocWidget::on_tocTV_clicked(QModelIndex const & index)
|
|
|
|
{
|
|
|
|
goTo(index);
|
2008-05-30 11:33:24 +00:00
|
|
|
gui_view_.setFocus();
|
2008-05-17 17:03:53 +00:00
|
|
|
}
|
2006-04-18 09:57:47 +00:00
|
|
|
|
2008-05-17 17:03:53 +00:00
|
|
|
|
|
|
|
void TocWidget::goTo(QModelIndex const & index)
|
|
|
|
{
|
|
|
|
LYXERR(Debug::GUI, "goto " << index.row()
|
|
|
|
<< ", " << index.column());
|
|
|
|
|
2008-06-17 15:10:03 +00:00
|
|
|
gui_view_.tocModels().goTo(current_type_, index);
|
2006-04-18 09:57:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-04 17:46:03 +00:00
|
|
|
void TocWidget::on_updateTB_clicked()
|
2006-03-31 13:17:44 +00:00
|
|
|
{
|
2007-05-10 16:25:11 +00:00
|
|
|
// The backend update can take some time so we disable
|
|
|
|
// the controls while waiting.
|
|
|
|
enableControls(false);
|
2008-05-30 11:33:24 +00:00
|
|
|
gui_view_.tocModels().updateBackend();
|
2006-11-17 17:19:43 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 16:46:46 +00:00
|
|
|
|
2006-11-17 17:19:43 +00:00
|
|
|
/* FIXME (Ugras 17/11/06):
|
2008-06-12 16:46:46 +00:00
|
|
|
I have implemented a indexDepth function to get the model indices. In my
|
2006-11-17 17:19:43 +00:00
|
|
|
opinion, somebody should derive a new qvariant class for tocModelItem
|
2008-06-12 16:46:46 +00:00
|
|
|
which saves the string data and depth information. That will save the
|
|
|
|
depth calculation. */
|
|
|
|
|
|
|
|
static int indexDepth(QModelIndex const & index, int depth = -1)
|
2006-12-30 21:51:05 +00:00
|
|
|
{
|
2006-11-17 17:19:43 +00:00
|
|
|
++depth;
|
2008-06-12 16:46:46 +00:00
|
|
|
return index.parent() == QModelIndex()
|
|
|
|
? depth : indexDepth(index.parent(), depth);
|
2006-03-31 13:17:44 +00:00
|
|
|
}
|
|
|
|
|
2006-04-18 09:57:47 +00:00
|
|
|
|
2007-03-12 14:23:44 +00:00
|
|
|
void TocWidget::on_depthSL_valueChanged(int depth)
|
2006-03-31 13:17:44 +00:00
|
|
|
{
|
|
|
|
if (depth == depth_)
|
|
|
|
return;
|
2006-11-25 22:16:22 +00:00
|
|
|
setTreeDepth(depth);
|
2008-05-30 12:13:42 +00:00
|
|
|
gui_view_.setFocus();
|
2006-11-25 22:16:22 +00:00
|
|
|
}
|
|
|
|
|
2006-03-31 13:17:44 +00:00
|
|
|
|
2007-03-12 14:23:44 +00:00
|
|
|
void TocWidget::setTreeDepth(int depth)
|
2006-11-25 22:16:22 +00:00
|
|
|
{
|
2007-03-12 14:23:44 +00:00
|
|
|
depth_ = depth;
|
2008-06-17 15:10:03 +00:00
|
|
|
if (!tocTV->model())
|
|
|
|
return;
|
2006-12-30 10:35:17 +00:00
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
// expanding and then collapsing is probably better,
|
2006-12-30 10:35:17 +00:00
|
|
|
// but my qt 4.1.2 doesn't have expandAll()..
|
2007-05-28 22:27:45 +00:00
|
|
|
//tocTV->expandAll();
|
2007-03-16 14:14:55 +00:00
|
|
|
QModelIndexList indices = tocTV->model()->match(
|
2008-09-08 01:18:33 +00:00
|
|
|
tocTV->model()->index(0, 0),
|
2007-05-28 22:27:45 +00:00
|
|
|
Qt::DisplayRole, "*", -1,
|
2007-08-19 09:44:34 +00:00
|
|
|
Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2006-12-30 10:35:17 +00:00
|
|
|
int size = indices.size();
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
QModelIndex index = indices[i];
|
2008-06-12 16:46:46 +00:00
|
|
|
tocTV->setExpanded(index, indexDepth(index) < depth_);
|
2006-11-17 17:19:43 +00:00
|
|
|
}
|
2006-03-31 13:17:44 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:33:24 +00:00
|
|
|
|
2008-06-17 15:10:03 +00:00
|
|
|
void TocWidget::on_typeCO_currentIndexChanged(int index)
|
2006-03-31 13:17:44 +00:00
|
|
|
{
|
2008-06-17 15:10:03 +00:00
|
|
|
current_type_ = typeCO->itemData(index).toString();
|
2008-06-11 18:13:14 +00:00
|
|
|
updateView();
|
2008-05-30 12:23:15 +00:00
|
|
|
gui_view_.setFocus();
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2006-04-18 09:57:47 +00:00
|
|
|
|
2008-05-30 11:59:57 +00:00
|
|
|
void TocWidget::outline(int func_code)
|
2006-03-29 16:57:47 +00:00
|
|
|
{
|
2007-04-17 10:27:53 +00:00
|
|
|
enableControls(false);
|
2007-03-12 14:23:44 +00:00
|
|
|
QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
|
2008-05-30 11:59:57 +00:00
|
|
|
if (list.isEmpty())
|
|
|
|
return;
|
|
|
|
enableControls(false);
|
|
|
|
goTo(list[0]);
|
|
|
|
dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
|
|
|
|
enableControls(true);
|
2008-05-30 12:13:42 +00:00
|
|
|
gui_view_.setFocus();
|
2008-05-30 11:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TocWidget::on_moveUpTB_clicked()
|
|
|
|
{
|
|
|
|
outline(LFUN_OUTLINE_UP);
|
2006-03-29 16:57:47 +00:00
|
|
|
}
|
|
|
|
|
2006-04-18 09:57:47 +00:00
|
|
|
|
2007-05-04 17:46:03 +00:00
|
|
|
void TocWidget::on_moveDownTB_clicked()
|
2006-03-29 16:57:47 +00:00
|
|
|
{
|
2008-05-30 11:59:57 +00:00
|
|
|
outline(LFUN_OUTLINE_DOWN);
|
2006-03-29 16:57:47 +00:00
|
|
|
}
|
|
|
|
|
2006-04-18 09:57:47 +00:00
|
|
|
|
2007-05-04 17:46:03 +00:00
|
|
|
void TocWidget::on_moveInTB_clicked()
|
2006-03-29 16:57:47 +00:00
|
|
|
{
|
2008-05-30 11:59:57 +00:00
|
|
|
outline(LFUN_OUTLINE_IN);
|
2006-03-29 16:57:47 +00:00
|
|
|
}
|
|
|
|
|
2006-04-18 09:57:47 +00:00
|
|
|
|
2007-05-04 17:46:03 +00:00
|
|
|
void TocWidget::on_moveOutTB_clicked()
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2008-05-30 11:59:57 +00:00
|
|
|
outline(LFUN_OUTLINE_OUT);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2006-04-22 18:48:28 +00:00
|
|
|
|
2007-03-12 14:23:44 +00:00
|
|
|
void TocWidget::select(QModelIndex const & index)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-04-19 14:48:22 +00:00
|
|
|
if (!index.isValid()) {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
|
2006-04-19 14:48:22 +00:00
|
|
|
return;
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
2006-04-19 14:48:22 +00:00
|
|
|
|
2007-05-28 22:27:45 +00:00
|
|
|
tocTV->scrollTo(index);
|
2008-05-17 17:03:53 +00:00
|
|
|
tocTV->clearSelection();
|
|
|
|
tocTV->setCurrentIndex(index);
|
2006-04-18 09:57:47 +00:00
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-04-22 18:48:28 +00:00
|
|
|
|
2008-06-17 15:10:03 +00:00
|
|
|
/// Test if outlining operation is possible
|
|
|
|
static bool canOutline(QString const & type)
|
|
|
|
{
|
|
|
|
return type == "tableofcontents";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-17 10:27:53 +00:00
|
|
|
void TocWidget::enableControls(bool enable)
|
2006-04-18 09:57:47 +00:00
|
|
|
{
|
2007-05-04 17:46:03 +00:00
|
|
|
updateTB->setEnabled(enable);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2008-06-17 15:10:03 +00:00
|
|
|
if (!canOutline(current_type_))
|
2006-04-18 09:57:47 +00:00
|
|
|
enable = false;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-05-04 17:46:03 +00:00
|
|
|
moveUpTB->setEnabled(enable);
|
|
|
|
moveDownTB->setEnabled(enable);
|
|
|
|
moveInTB->setEnabled(enable);
|
|
|
|
moveOutTB->setEnabled(enable);
|
2008-09-12 02:04:42 +00:00
|
|
|
if (!enable) {
|
|
|
|
depthSL->setMaximum(0);
|
|
|
|
depthSL->setValue(0);
|
|
|
|
}
|
2006-04-18 09:57:47 +00:00
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
|
2008-06-19 13:35:19 +00:00
|
|
|
/// Test if synchronized navigation is possible
|
|
|
|
static bool canNavigate(QString const & type)
|
|
|
|
{
|
|
|
|
// It is not possible to have synchronous navigation in a correctl
|
|
|
|
// and efficient way with the label type because Toc::item() do a linear
|
|
|
|
// seatch. Even if fixed, it might even not be desirable to do so if we
|
|
|
|
// want to support drag&drop of labels and references.
|
|
|
|
return type != "label";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-08 17:28:14 +00:00
|
|
|
void TocWidget::updateView()
|
2006-04-22 18:48:28 +00:00
|
|
|
{
|
2008-06-17 15:44:14 +00:00
|
|
|
if (!gui_view_.view()) {
|
|
|
|
enableControls(false);
|
|
|
|
typeCO->setEnabled(false);
|
|
|
|
tocTV->setModel(0);
|
|
|
|
tocTV->setEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
typeCO->setEnabled(true);
|
|
|
|
tocTV->setEnabled(true);
|
|
|
|
|
|
|
|
QStandardItemModel * toc_model = gui_view_.tocModels().model(current_type_);
|
|
|
|
if (tocTV->model() != toc_model) {
|
|
|
|
tocTV->setModel(toc_model);
|
|
|
|
tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
|
}
|
|
|
|
bool controls_enabled = toc_model && toc_model->rowCount() > 0
|
|
|
|
&& !gui_view_.buffer()->isReadonly();
|
|
|
|
enableControls(controls_enabled);
|
2008-09-12 02:11:07 +00:00
|
|
|
|
2008-06-17 15:44:14 +00:00
|
|
|
depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
|
|
|
|
depthSL->setValue(depth_);
|
2008-05-17 17:03:53 +00:00
|
|
|
setTreeDepth(depth_);
|
2008-06-19 13:35:19 +00:00
|
|
|
if (canNavigate(current_type_))
|
|
|
|
select(gui_view_.tocModels().currentIndex(current_type_));
|
2008-06-17 15:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static QString decodeType(QString const & str)
|
|
|
|
{
|
|
|
|
QString type = str;
|
|
|
|
if (type.contains("tableofcontents")) {
|
|
|
|
type = "tableofcontents";
|
|
|
|
} else if (type.contains("floatlist")) {
|
|
|
|
if (type.contains("\"figure"))
|
|
|
|
type = "figure";
|
|
|
|
else if (type.contains("\"table"))
|
|
|
|
type = "table";
|
|
|
|
else if (type.contains("\"algorithm"))
|
|
|
|
type = "algorithm";
|
|
|
|
}
|
|
|
|
return type;
|
2006-04-22 18:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-02 12:09:51 +00:00
|
|
|
void TocWidget::init(QString const & str)
|
2006-04-18 09:57:47 +00:00
|
|
|
{
|
2008-06-17 15:10:03 +00:00
|
|
|
int new_index;
|
|
|
|
if (str.isEmpty())
|
|
|
|
new_index = typeCO->findData(current_type_);
|
|
|
|
else
|
|
|
|
new_index = typeCO->findData(decodeType(str));
|
|
|
|
|
|
|
|
// If everything else fails, settle on the table of contents which is
|
|
|
|
// guaranted to exist.
|
|
|
|
if (new_index == -1) {
|
|
|
|
current_type_ = "tableofcontents";
|
|
|
|
new_index = typeCO->findData(current_type_);
|
2008-02-12 15:33:01 +00:00
|
|
|
}
|
|
|
|
|
2008-06-17 15:49:14 +00:00
|
|
|
typeCO->blockSignals(true);
|
2008-06-17 15:10:03 +00:00
|
|
|
typeCO->setCurrentIndex(new_index);
|
2007-05-10 16:25:11 +00:00
|
|
|
typeCO->blockSignals(false);
|
2007-03-16 14:14:55 +00:00
|
|
|
}
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
2006-05-18 08:51:12 +00:00
|
|
|
|
2007-03-12 14:23:44 +00:00
|
|
|
#include "TocWidget_moc.cpp"
|