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"
|
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
#include "GuiToc.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
#include "qt_helpers.h"
|
2007-05-04 17:46:03 +00:00
|
|
|
#include "support/filetools.h"
|
|
|
|
#include "support/lstrings.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
2006-04-18 09:57:47 +00:00
|
|
|
#include <QHeaderView>
|
2007-03-12 14:23:44 +00:00
|
|
|
#include <QPushButton>
|
|
|
|
#include <QTreeWidgetItem>
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <stack>
|
|
|
|
|
|
|
|
using std::endl;
|
|
|
|
using std::pair;
|
|
|
|
using std::stack;
|
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
|
|
|
|
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
|
|
|
using support::FileName;
|
|
|
|
using support::libFileSearch;
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2007-05-04 17:46:03 +00:00
|
|
|
namespace frontend {
|
2007-03-12 14:23:44 +00:00
|
|
|
|
2007-08-31 05:53:55 +00:00
|
|
|
TocWidget::TocWidget(GuiToc * form, QWidget * parent)
|
2007-09-05 20:33:29 +00:00
|
|
|
: QWidget(parent), depth_(0)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
|
|
|
setupUi(this);
|
2007-09-05 20:33:29 +00:00
|
|
|
form_ = form;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-09-05 20:33:29 +00:00
|
|
|
connect(form_, SIGNAL(modelReset()), SLOT(updateGui()));
|
2007-05-28 22:27:45 +00:00
|
|
|
|
2007-08-30 20:09:12 +00:00
|
|
|
FileName icon_path = libFileSearch("images", "promote.png");
|
2007-05-04 17:46:03 +00:00
|
|
|
moveOutTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
|
2007-08-30 20:09:12 +00:00
|
|
|
icon_path = libFileSearch("images", "demote.png");
|
2007-05-04 17:46:03 +00:00
|
|
|
moveInTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
|
2007-08-30 20:09:12 +00:00
|
|
|
icon_path = libFileSearch("images", "up.png");
|
2007-05-04 17:46:03 +00:00
|
|
|
moveUpTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
|
2007-08-30 20:09:12 +00:00
|
|
|
icon_path = libFileSearch("images", "down.png");
|
2007-05-04 17:46:03 +00:00
|
|
|
moveDownTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
|
2007-08-30 20:09:12 +00:00
|
|
|
icon_path = libFileSearch("images", "reload.png");
|
2007-05-04 17:46:03 +00:00
|
|
|
updateTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
|
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
|
|
|
|
2007-03-12 14:23:44 +00:00
|
|
|
void TocWidget::selectionChanged(const QModelIndex & current,
|
2006-07-06 08:18:51 +00:00
|
|
|
const QModelIndex & /*previous*/)
|
2006-04-18 09:57:47 +00:00
|
|
|
{
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::GUI)
|
2006-07-06 08:18:51 +00:00
|
|
|
<< "selectionChanged index " << current.row()
|
|
|
|
<< ", " << current.column()
|
2006-04-18 09:57:47 +00:00
|
|
|
<< endl;
|
|
|
|
|
2007-03-16 14:14:55 +00:00
|
|
|
form_->goTo(typeCO->currentIndex(), current);
|
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);
|
2007-09-05 20:33:29 +00:00
|
|
|
form_->controller().updateBackend();
|
2006-11-17 17:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME (Ugras 17/11/06):
|
|
|
|
I have implemented a getIndexDepth function to get the model indices. In my
|
|
|
|
opinion, somebody should derive a new qvariant class for tocModelItem
|
|
|
|
which saves the string data and depth information. that will save the
|
|
|
|
depth calculation.
|
|
|
|
*/
|
2007-03-12 14:23:44 +00:00
|
|
|
int TocWidget::getIndexDepth(QModelIndex const & index, int depth)
|
2006-12-30 21:51:05 +00:00
|
|
|
{
|
2006-11-17 17:19:43 +00:00
|
|
|
++depth;
|
2007-09-05 20:33:29 +00:00
|
|
|
return (index.parent() == QModelIndex())
|
|
|
|
? depth : getIndexDepth(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);
|
|
|
|
}
|
|
|
|
|
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];
|
2007-05-28 22:27:45 +00:00
|
|
|
if (getIndexDepth(index) < depth_)
|
|
|
|
tocTV->expand(index);
|
2006-11-17 17:19:43 +00:00
|
|
|
else
|
2007-05-28 22:27:45 +00:00
|
|
|
tocTV->collapse(index);
|
2006-11-17 17:19:43 +00:00
|
|
|
}
|
2006-03-31 13:17:44 +00:00
|
|
|
}
|
|
|
|
|
2007-05-10 16:25:11 +00:00
|
|
|
void TocWidget::on_typeCO_currentIndexChanged(int value)
|
2006-03-31 13:17:44 +00:00
|
|
|
{
|
2007-03-16 14:14:55 +00:00
|
|
|
setTocModel(value);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2006-04-18 09:57:47 +00:00
|
|
|
|
2007-05-04 17:46:03 +00:00
|
|
|
void TocWidget::on_moveUpTB_clicked()
|
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();
|
|
|
|
if (!list.isEmpty()) {
|
2007-04-17 10:27:53 +00:00
|
|
|
enableControls(false);
|
2007-03-16 14:14:55 +00:00
|
|
|
form_->goTo(typeCO->currentIndex(), list[0]);
|
2007-09-05 20:33:29 +00:00
|
|
|
form_->controller().outlineUp();
|
2007-04-17 10:27:53 +00:00
|
|
|
enableControls(true);
|
2007-03-12 14:23:44 +00:00
|
|
|
}
|
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
|
|
|
{
|
2007-04-17 10:27:53 +00:00
|
|
|
enableControls(false);
|
2007-03-12 14:23:44 +00:00
|
|
|
QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
|
|
|
|
if (!list.isEmpty()) {
|
2007-04-17 10:27:53 +00:00
|
|
|
enableControls(false);
|
2007-03-16 14:14:55 +00:00
|
|
|
form_->goTo(typeCO->currentIndex(), list[0]);
|
2007-09-05 20:33:29 +00:00
|
|
|
form_->controller().outlineDown();
|
2007-04-17 10:27:53 +00:00
|
|
|
enableControls(true);
|
2007-03-12 14:23:44 +00:00
|
|
|
}
|
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
|
|
|
{
|
2007-04-17 10:27:53 +00:00
|
|
|
enableControls(false);
|
2007-03-12 14:23:44 +00:00
|
|
|
QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
|
|
|
|
if (!list.isEmpty()) {
|
2007-04-17 10:27:53 +00:00
|
|
|
enableControls(false);
|
2007-03-16 14:14:55 +00:00
|
|
|
form_->goTo(typeCO->currentIndex(), list[0]);
|
2007-09-05 20:33:29 +00:00
|
|
|
form_->controller().outlineIn();
|
2007-04-17 10:27:53 +00:00
|
|
|
enableControls(true);
|
2007-03-12 14:23:44 +00:00
|
|
|
}
|
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
|
|
|
{
|
2007-03-12 14:23:44 +00:00
|
|
|
QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
|
|
|
|
if (!list.isEmpty()) {
|
2007-04-17 10:27:53 +00:00
|
|
|
enableControls(false);
|
2007-03-16 14:14:55 +00:00
|
|
|
form_->goTo(typeCO->currentIndex(), list[0]);
|
2007-09-05 20:33:29 +00:00
|
|
|
form_->controller().outlineOut();
|
2007-04-17 10:27:53 +00:00
|
|
|
enableControls(true);
|
2007-03-12 14:23:44 +00:00
|
|
|
}
|
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-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::GUI)
|
2007-03-12 14:23:44 +00:00
|
|
|
<< "TocWidget::select(): QModelIndex is invalid!" << endl;
|
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-15 11:26:24 +00:00
|
|
|
disconnectSelectionModel();
|
|
|
|
tocTV->setCurrentIndex(index);
|
2007-05-28 22:27:45 +00:00
|
|
|
tocTV->scrollTo(index);
|
2007-05-15 11:26:24 +00:00
|
|
|
reconnectSelectionModel();
|
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
|
|
|
|
2007-03-16 14:14:55 +00:00
|
|
|
if (!form_->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-03-12 14:23:44 +00:00
|
|
|
void TocWidget::update()
|
2006-04-22 18:48:28 +00:00
|
|
|
{
|
2007-09-03 20:28:26 +00:00
|
|
|
LYXERR(Debug::GUI) << "In TocWidget::updateView()" << endl;
|
2007-03-16 14:14:55 +00:00
|
|
|
select(form_->getCurrentIndex(typeCO->currentIndex()));
|
2007-03-12 14:23:44 +00:00
|
|
|
QWidget::update();
|
2006-04-22 18:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-12 14:23:44 +00:00
|
|
|
void TocWidget::updateGui()
|
2006-04-18 09:57:47 +00:00
|
|
|
{
|
2007-09-05 20:33:29 +00:00
|
|
|
vector<docstring> const & type_names = form_->controller().typeNames();
|
2007-05-10 16:25:11 +00:00
|
|
|
if (type_names.empty()) {
|
2007-04-17 10:27:53 +00:00
|
|
|
enableControls(false);
|
2007-05-10 16:25:11 +00:00
|
|
|
typeCO->clear();
|
2006-10-19 14:35:06 +00:00
|
|
|
tocTV->setModel(new QStandardItemModel);
|
2006-12-30 21:51:05 +00:00
|
|
|
tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
2006-10-16 08:51:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-16 14:14:55 +00:00
|
|
|
QString current_text = typeCO->currentText();
|
2007-05-25 09:20:35 +00:00
|
|
|
//lyxerr << "current_text " << fromqstr(current_text) << endl;
|
2007-05-10 16:25:11 +00:00
|
|
|
typeCO->blockSignals(true);
|
2007-05-11 14:05:43 +00:00
|
|
|
typeCO->clear();
|
2007-05-10 16:25:11 +00:00
|
|
|
int current_type = -1;
|
|
|
|
for (size_t i = 0; i != type_names.size(); ++i) {
|
2007-06-04 13:27:27 +00:00
|
|
|
QString item = toqstr(type_names[i]);
|
2007-05-10 16:25:11 +00:00
|
|
|
typeCO->addItem(item);
|
|
|
|
if (item == current_text)
|
|
|
|
current_type = i;
|
|
|
|
}
|
2007-03-16 14:14:55 +00:00
|
|
|
if (current_type != -1)
|
|
|
|
typeCO->setCurrentIndex(current_type);
|
|
|
|
else
|
2007-09-05 20:33:29 +00:00
|
|
|
typeCO->setCurrentIndex(form_->controller().selectedType());
|
2007-05-10 16:25:11 +00:00
|
|
|
typeCO->blockSignals(false);
|
2006-10-16 08:51:55 +00:00
|
|
|
|
2007-03-16 14:14:55 +00:00
|
|
|
setTocModel(typeCO->currentIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TocWidget::setTocModel(size_t type)
|
|
|
|
{
|
2007-04-17 10:27:53 +00:00
|
|
|
bool controls_enabled = false;
|
2007-03-16 14:14:55 +00:00
|
|
|
QStandardItemModel * toc_model = form_->tocModel(type);
|
|
|
|
if (toc_model) {
|
2007-04-17 10:27:53 +00:00
|
|
|
controls_enabled = toc_model->rowCount() > 0;
|
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
|
|
|
|
2007-04-17 10:27:53 +00:00
|
|
|
enableControls(controls_enabled);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-11-16 12:37:55 +00:00
|
|
|
reconnectSelectionModel();
|
2007-04-17 10:27:53 +00:00
|
|
|
|
|
|
|
if (controls_enabled) {
|
|
|
|
depthSL->setMaximum(form_->getTocDepth(type));
|
|
|
|
depthSL->setValue(depth_);
|
|
|
|
}
|
2006-04-19 14:48:22 +00:00
|
|
|
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::GUI) << "In TocWidget::updateGui()" << endl;
|
2007-03-16 14:14:55 +00:00
|
|
|
|
|
|
|
select(form_->getCurrentIndex(typeCO->currentIndex()));
|
|
|
|
|
|
|
|
if (toc_model) {
|
2007-04-01 10:09:49 +00:00
|
|
|
LYXERR(Debug::GUI)
|
2007-09-05 20:33:29 +00:00
|
|
|
<< "tocModel()->rowCount "
|
2007-03-16 14:14:55 +00:00
|
|
|
<< toc_model->rowCount()
|
|
|
|
<< "\nform_->tocModel()->columnCount "
|
|
|
|
<< toc_model->columnCount()
|
|
|
|
<< endl;
|
|
|
|
}
|
2006-04-18 09:57:47 +00:00
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
|
2007-03-12 14:23:44 +00:00
|
|
|
void TocWidget::reconnectSelectionModel()
|
2006-11-16 12:37:55 +00:00
|
|
|
{
|
|
|
|
connect(tocTV->selectionModel(),
|
2007-05-15 11:26:24 +00:00
|
|
|
SIGNAL(currentChanged(const QModelIndex &,
|
|
|
|
const QModelIndex &)),
|
|
|
|
this,
|
|
|
|
SLOT(selectionChanged(const QModelIndex &,
|
|
|
|
const QModelIndex &)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TocWidget::disconnectSelectionModel()
|
|
|
|
{
|
|
|
|
disconnect(tocTV->selectionModel(),
|
2007-05-28 22:27:45 +00:00
|
|
|
SIGNAL(currentChanged(const QModelIndex &,
|
2007-05-15 11:26:24 +00:00
|
|
|
const QModelIndex &)),
|
|
|
|
this,
|
|
|
|
SLOT(selectionChanged(const QModelIndex &,
|
|
|
|
const QModelIndex &)));
|
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"
|