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-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);
|
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-05-30 11:33:24 +00:00
|
|
|
gui_view_.tocModels().goTo(typeCO->currentIndex(), 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;
|
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(
|
|
|
|
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-11 18:13:14 +00:00
|
|
|
void TocWidget::on_typeCO_currentIndexChanged(int)
|
2006-03-31 13:17:44 +00:00
|
|
|
{
|
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
|
|
|
|
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-05-30 11:33:24 +00:00
|
|
|
if (!gui_view_.tocModels().canOutline(typeCO->currentIndex()))
|
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);
|
2007-04-17 10:27:53 +00:00
|
|
|
|
|
|
|
depthSL->setEnabled(enable);
|
2006-04-18 09:57:47 +00:00
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
|
2007-09-08 17:28:14 +00:00
|
|
|
void TocWidget::updateView()
|
2006-04-22 18:48:28 +00:00
|
|
|
{
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::GUI, "In TocWidget::updateView()");
|
2008-06-11 18:13:14 +00:00
|
|
|
setTocModel();
|
2008-05-17 17:03:53 +00:00
|
|
|
setTreeDepth(depth_);
|
2008-05-30 11:33:24 +00:00
|
|
|
select(gui_view_.tocModels().currentIndex(typeCO->currentIndex()));
|
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-05-30 11:33:24 +00:00
|
|
|
QStringList const & type_names = gui_view_.tocModels().typeNames();
|
2008-03-06 19:39:52 +00:00
|
|
|
if (type_names.isEmpty()) {
|
2007-04-17 10:27:53 +00:00
|
|
|
enableControls(false);
|
2008-06-11 18:13:14 +00:00
|
|
|
typeCO->setEnabled(false);
|
2006-10-19 14:35:06 +00:00
|
|
|
tocTV->setModel(new QStandardItemModel);
|
2008-06-11 18:13:14 +00:00
|
|
|
tocTV->setEnabled(false);
|
2006-10-16 08:51:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-06-11 18:13:14 +00:00
|
|
|
typeCO->setEnabled(true);
|
|
|
|
tocTV->setEnabled(true);
|
2008-05-30 11:33:24 +00:00
|
|
|
int selected_type = gui_view_.tocModels().decodeType(str);
|
2008-05-02 12:09:51 +00:00
|
|
|
|
2008-02-12 15:33:01 +00:00
|
|
|
QString const current_text = typeCO->currentText();
|
2007-05-10 16:25:11 +00:00
|
|
|
typeCO->blockSignals(true);
|
2007-05-11 14:05:43 +00:00
|
|
|
typeCO->clear();
|
2008-03-06 19:39:52 +00:00
|
|
|
for (int i = 0; i != type_names.size(); ++i)
|
|
|
|
typeCO->addItem(type_names[i]);
|
2008-05-02 12:09:51 +00:00
|
|
|
if (!str.isEmpty())
|
2008-02-12 09:32:40 +00:00
|
|
|
typeCO->setCurrentIndex(selected_type);
|
2008-02-12 15:33:01 +00:00
|
|
|
else {
|
|
|
|
int const new_index = typeCO->findText(current_text);
|
|
|
|
if (new_index != -1)
|
|
|
|
typeCO->setCurrentIndex(new_index);
|
2008-05-02 12:09:51 +00:00
|
|
|
else
|
|
|
|
typeCO->setCurrentIndex(selected_type);
|
2008-02-12 15:33:01 +00:00
|
|
|
}
|
|
|
|
|
2007-05-10 16:25:11 +00:00
|
|
|
typeCO->blockSignals(false);
|
2006-10-16 08:51:55 +00:00
|
|
|
|
2008-06-11 18:13:14 +00:00
|
|
|
// setTocModel produce QTreeView reset and setting depth again
|
|
|
|
// is needed. That must be done after all Qt updates are processed.
|
|
|
|
QTimer::singleShot(0, this, SLOT(updateView()));
|
2007-03-16 14:14:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-11 18:13:14 +00:00
|
|
|
void TocWidget::setTocModel()
|
2007-03-16 14:14:55 +00:00
|
|
|
{
|
2008-06-11 18:13:14 +00:00
|
|
|
int const toc_type = typeCO->currentIndex();
|
|
|
|
QStandardItemModel * toc_model = gui_view_.tocModels().model(toc_type);
|
|
|
|
LASSERT(toc_model, return);
|
|
|
|
|
|
|
|
if (tocTV->model() != toc_model) {
|
2007-03-16 14:14:55 +00:00
|
|
|
tocTV->setModel(toc_model);
|
2006-12-30 21:51:05 +00:00
|
|
|
tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
|
}
|
2007-03-12 14:23:44 +00:00
|
|
|
|
2008-06-11 18:13:14 +00:00
|
|
|
bool controls_enabled = toc_model->rowCount() > 0;;
|
2007-04-17 10:27:53 +00:00
|
|
|
enableControls(controls_enabled);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-04-17 10:27:53 +00:00
|
|
|
if (controls_enabled) {
|
2008-06-11 18:13:14 +00:00
|
|
|
depthSL->setMaximum(gui_view_.tocModels().depth(toc_type));
|
2007-04-17 10:27:53 +00:00
|
|
|
depthSL->setValue(depth_);
|
|
|
|
}
|
2006-11-16 12:37: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"
|