* ControlToc.[Ch]

- bool ControlToc::canOutline(): new method to test if outline is possible.
* TocModel: new class
* QToc is now the controller (inheriting ControlToc directly) and the model (using TocModel)
* QTocDialog is now only the view (inheriting Dialogs::View directly)
* Dialogs.C: updated toc controller and view correspondingly



git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@13692 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2006-04-18 09:57:47 +00:00
parent 4700431096
commit 9db70c5346
11 changed files with 498 additions and 226 deletions

View File

@ -37,6 +37,12 @@ void ControlToc::goTo(toc::TocItem const & item)
}
bool ControlToc::canOutline(string const & type)
{
return type == "TOC";
}
void ControlToc::outline(toc::OutlineOp op)
{
std::ostringstream o;

View File

@ -41,6 +41,9 @@ public:
/// Apply the selected outlining operation
void outline(toc::OutlineOp op);
/// Test if outlining operation is possible
bool canOutline(std::string const & type);
};
} // namespace frontend

View File

@ -18,7 +18,6 @@
#include "ControlBranch.h"
#include "ControlChanges.h"
#include "ControlCharacter.h"
#include "ControlCitation.h"
#include "ControlDocument.h"
#include "ControlErrorList.h"
#include "ControlERT.h"
@ -40,7 +39,6 @@
#include "ControlSpellchecker.h"
#include "ControlTabular.h"
#include "ControlTabularCreate.h"
#include "ControlToc.h"
#include "ControlVSpace.h"
#include "ControlWrap.h"
@ -83,6 +81,7 @@
#include "QTabularCreate.h"
#include "QTexinfo.h"
#include "QToc.h"
#include "QTocDialog.h"
#include "q3url.h"
#include "QVSpace.h"
#include "QWrap.h"
@ -310,8 +309,9 @@ Dialogs::DialogPtr Dialogs::build(string const & name)
dialog->bc().bp(new OkApplyCancelReadOnlyPolicy);
#endif
} else if (name == "toc") {
dialog->setController(new ControlToc(*dialog));
dialog->setView(new QToc(*dialog));
QToc * qtoc = new QToc(*dialog);
dialog->setController(qtoc);
dialog->setView(new QTocDialog(*dialog, qtoc));
dialog->bc().bp(new OkCancelPolicy);
} else if (name == "url") {
dialog->setController(new ControlCommand(*dialog, name));

View File

@ -123,6 +123,7 @@ MOCFILES = \
QTabularDialog.C QTabularDialog.h \
QTexinfoDialog.C QTexinfoDialog.h \
QThesaurusDialog.C QThesaurusDialog.h \
TocModel.C TocModel.h \
QTocDialog.C QTocDialog.h \
QtView.C QtView.h \
QURLDialog.C QURLDialog.h \

View File

@ -12,7 +12,7 @@
#include <config.h>
#include "QToc.h"
#include "QTocDialog.h"
#include "TocModel.h"
#include "Qt2BC.h"
#include "qt_helpers.h"
@ -20,6 +20,8 @@
#include "controllers/ControlToc.h"
#include <algorithm>
using std::endl;
using std::pair;
@ -29,71 +31,119 @@ using std::string;
namespace lyx {
namespace frontend {
typedef QController<ControlToc, QView<QTocDialog> > base_class;
QToc::QToc(Dialog & parent)
: base_class(parent, _("Table of Contents"))
{}
void QToc::build_dialog()
: ControlToc(parent)
{
dialog_.reset(new QTocDialog(this));
update();
}
void QToc::update_contents()
bool QToc::canOutline()
{
dialog_->updateType();
dialog_->updateToc();
vector<string> const & types = getTypes();
BOOST_ASSERT(type_ >= 0 && type_ < int(types.size()));
return ControlToc::canOutline(types[type_]);
}
void QToc::select(string const & text)
QStandardItemModel * QToc::tocModel()
{
toc::Toc::const_iterator iter = toclist.begin();
lyxerr[Debug::GUI]
<< "QToc: type_ " << type_
<< " toc_models_.size() " << toc_models_.size()
<< endl;
for (; iter != toclist.end(); ++iter) {
if (iter->str == text)
break;
}
BOOST_ASSERT(type_ >= 0 && type_ < int(toc_models_.size()));
return toc_models_[type_];
}
if (iter == toclist.end()) {
lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry: "
<< text << endl;
QStandardItemModel * QToc::setTocModel(int type)
{
type_ = type;
lyxerr[Debug::GUI]
<< "QToc: type_ " << type_
<< " toc_models_.size() " << toc_models_.size()
<< endl;
BOOST_ASSERT(type_ >= 0 && type_ < int(toc_models_.size()));
return toc_models_[type_];
}
void QToc::goTo(QModelIndex const & index)
{
if (!index.isValid()) {
lyxerr[Debug::GUI]
<< "QToc::goTo(): QModelIndex is invalid!"
<< endl;
return;
}
lyxerr[Debug::GUI]
<< "QToc::goTo " << toc_models_[type_]->item(index).str
<< endl;
controller().goTo(*iter);
ControlToc::goTo(toc_models_[type_]->item(index));
}
void QToc::moveUp()
void QToc::update()
{
controller().outline(toc::UP);
update_contents();
toc_models_.clear();
QStringList type_list;
type_ = 0;
vector<string> const & types = getTypes();
string const & selected_type = toc::getType(params().getCmdName());
lyxerr[Debug::GUI] << "selected_type " << selected_type << endl;
QString gui_names_;
for (size_t i = 0; i != types.size(); ++i) {
string const & type_str = types[i];
type_list.append(toqstr(getGuiName(type_str)));
if (type_str == selected_type)
type_ = i;
lyxerr[Debug::GUI]
<< "QToc: new type " << type_str
<< "\ttoc_models_.size() " << toc_models_.size()
<< endl;
toc_models_.push_back(new TocModel(getContents(types[i])));
}
type_model_.setStringList(type_list);
}
void QToc::moveDown()
void QToc::updateToc(int type)
{
controller().outline(toc::DOWN);
update_contents();
vector<string> const & choice = getTypes();
toc_models_[type] = new TocModel(getContents(choice[type]));
}
void QToc::moveIn()
void QToc::move(toc::OutlineOp const operation, QModelIndex & index)
{
controller().outline(toc::IN);
update_contents();
int toc_id = toc_models_[type_]->item(index).id_;
string toc_str = toc_models_[type_]->item(index).str;
toc_str.erase(0, toc_str.find(' ') + 1);
outline(operation);
updateToc(type_);
lyxerr[Debug::GUI]
<< "Toc id " << toc_id
<< " Toc str " << toc_str
<< endl;
index = toc_models_[type_]->index(toc_str);
}
void QToc::moveOut()
{
controller().outline(toc::OUT);
update_contents();
}
} // namespace frontend
} // namespace lyx

View File

@ -14,47 +14,47 @@
#ifndef QTOC_H
#define QTOC_H
#include "QDialogView.h"
#include "toc.h"
#include "ControlToc.h"
#include <QStandardItemModel>
#include <QStringListModel>
namespace lyx {
namespace frontend {
class ControlToc;
class QTocDialog;
class TocModel;
//template<class W>
class QToc :
public QController<ControlToc, QView<QTocDialog> >
class QToc : public ControlToc
{
public:
friend class QTocDialog;
QToc(Dialog &);
/// return the toc list
lyx::toc::Toc & get_toclist() { return toclist;}
void update();
void moveUp();
void moveDown();
void moveIn();
void moveOut();
void updateToc(int type);
bool canOutline();
QStandardItemModel * tocModel();
QStandardItemModel * setTocModel(int type);
QStringListModel * typeModel()
{ return &type_model_; }
void goTo(QModelIndex const & index);
void move(toc::OutlineOp const operation, QModelIndex & index);
private:
/// select an entry
void select(std::string const & text);
std::vector<TocModel *> toc_models_;
virtual void apply() {}
QStringListModel type_model_;
/// update dialog
virtual void update_contents();
/// build dialog
virtual void build_dialog();
/// the toc list
lyx::toc::Toc toclist;
int type_;
int outline_type_;
};
} // namespace frontend

View File

@ -19,11 +19,10 @@
#include "debug.h"
#include <QHeaderView>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QPushButton>
#include <QCloseEvent>
#include <QHeaderView>
#include <vector>
#include <string>
@ -35,28 +34,22 @@ using std::stack;
using std::vector;
using std::string;
namespace lyx {
namespace frontend {
QTocDialog::QTocDialog(QToc * form)
: form_(form), depth_(2)
QTocDialog::QTocDialog(Dialog & dialog, QToc * form)
: Dialog::View(dialog, "Toc"), form_(form), depth_(2)
{
setupUi(this);
// Manage the cancel/close button
form_->bcview().setCancel(closePB);
update();
// disable sorting
tocTW->setSortingEnabled(false);
tocTW->setColumnCount(1);
// hide the pointless QHeader
QHeaderView * w = static_cast<QHeaderView*>(tocTW->header());
if (w)
w->hide();
// connect(closePB, SIGNAL(clicked()),
// form, SLOT(slotClose()));
connect(tocTV->selectionModel(),
SIGNAL(currentChanged(const QModelIndex &,
const QModelIndex &)),
this, SLOT(selectionChanged(const QModelIndex &,
const QModelIndex &)));
}
@ -65,196 +58,174 @@ QTocDialog::~QTocDialog()
accept();
}
void QTocDialog::on_tocTW_currentItemChanged(QTreeWidgetItem * current,
QTreeWidgetItem * previous)
void QTocDialog::selectionChanged(const QModelIndex & current,
const QModelIndex & previous)
{
form_->select(fromqstr(current->text(0)));
lyxerr[Debug::GUI]
<< "selectionChanged index " << current.row() << ", " << current.column()
<< endl;
form_->goTo(current);
}
void QTocDialog::on_tocTV_clicked(const QModelIndex & index )
{
lyxerr[Debug::GUI]
<< "on_tocTV_clicked index " << index.row() << ", " << index.column()
<< endl;
form_->goTo(index);
}
void QTocDialog::on_closePB_clicked()
{
accept();
}
void QTocDialog::on_updatePB_clicked()
{
form_->update();
update();
}
void QTocDialog::on_depthSL_valueChanged(int depth)
{
if (depth == depth_)
return;
depth_ = depth;
updateToc(true);
/*
while (
tocTv->setExpanded();
if (iter->depth > depth_)
tocTV->collapseItem(topLevelItem);
else if (iter->depth <= depth_)
tocTV->expandItem(topLevelItem);
*/
}
void QTocDialog::on_typeCO_activated(int value)
{
updateToc();
form_->setTocModel(value);
enableButtons();
}
void QTocDialog::on_moveUpPB_clicked()
{
enableButtons(false);
form_->moveUp();
enableButtons();
move(toc::UP);
}
void QTocDialog::on_moveDownPB_clicked()
{
enableButtons(false);
form_->moveDown();
enableButtons();
move(toc::DOWN);
}
void QTocDialog::on_moveInPB_clicked()
{
enableButtons(false);
form_->moveIn();
enableButtons();
move(toc::IN);
}
void QTocDialog::on_moveOutPB_clicked()
{
move(toc::OUT);
}
void QTocDialog::move(toc::OutlineOp const operation)
{
enableButtons(false);
form_->moveOut();
QModelIndex index = tocTV->selectionModel()->selectedIndexes()[0];
form_->move(operation, index);
select(index);
enableButtons();
}
void QTocDialog::select(QModelIndex const & index)
{
tocTV->setModel(form_->tocModel());
if (index.isValid()) {
tocTV->scrollTo(index);
tocTV->selectionModel()->select(index, QItemSelectionModel::Select);
}
}
void QTocDialog::enableButtons(bool enable)
{
updatePB->setEnabled(enable);
if (!form_->canOutline())
enable = false;
moveUpPB->setEnabled(enable);
moveDownPB->setEnabled(enable);
moveInPB->setEnabled(enable);
moveOutPB->setEnabled(enable);
updatePB->setEnabled(enable);
}
void QTocDialog::updateType()
void QTocDialog::update()
{
typeCO->clear();
typeCO->setModel(form_->typeModel());
tocTV->setModel(form_->tocModel());
tocTV->showColumn(0);
// hide the pointless QHeader for now
// in the future, new columns may appear
// like labels, bookmarks, etc...
// tocTV->header()->hide();
tocTV->header()->setVisible(true);
enableButtons();
vector<string> const & choice = form_->controller().getTypes();
string const & type = toc::getType(form_->controller().params().getCmdName());
connect(tocTV->selectionModel(),
SIGNAL(currentChanged(const QModelIndex &,
const QModelIndex &)),
this, SLOT(selectionChanged(const QModelIndex &,
const QModelIndex &)));
for (vector<string>::const_iterator it = choice.begin();
it != choice.end(); ++it) {
string const & guiname = form_->controller().getGuiName(*it);
typeCO->insertItem(toqstr(guiname));
if (*it == type) {
typeCO->setCurrentItem(it - choice.begin());
form_->setTitle(guiname);
}
}
lyxerr[Debug::GUI]
<< "form_->tocModel()->rowCount " << form_->tocModel()->rowCount()
<< "\nform_->tocModel()->columnCount " << form_->tocModel()->columnCount()
<< endl;
// setTitle(form_->guiname())
}
void QTocDialog::updateToc(bool newdepth)
void QTocDialog::apply()
{
vector<string> const & choice = form_->controller().getTypes();
string type;
if (!choice.empty())
type = choice[typeCO->currentItem()];
toc::Toc const & contents = form_->controller().getContents(type);
// Check if all elements are the same.
if (!newdepth && form_->get_toclist() == contents) {
return;
}
tocTW->clear();
form_->get_toclist() = contents;
if (form_->get_toclist().empty())
return;
tocTW->setUpdatesEnabled(false);
QTreeWidgetItem * topLevelItem;
toc::Toc::const_iterator iter = form_->get_toclist().begin();
while (iter != form_->get_toclist().end()) {
if (iter->depth == 1) {
topLevelItem = new QTreeWidgetItem(tocTW);
topLevelItem->setText(0, toqstr(iter->str));
if (iter->depth > depth_)
tocTW->collapseItem(topLevelItem);
else if (iter->depth <= depth_)
tocTW->expandItem(topLevelItem);
lyxerr[Debug::GUI]
<< "Table of contents\n"
<< "Added Top Level item " << iter->str
<< " at depth " << iter->depth
<< endl;
populateItem(topLevelItem, iter);
}
if (iter == form_->get_toclist().end())
break;
++iter;
}
tocTW->setUpdatesEnabled(true);
tocTW->update();
form_->setTitle(fromqstr(typeCO->currentText()));
tocTW->show();
// Nothing to do here... for now.
// Ideas welcome... (Abdel, 17042006)
}
void QTocDialog::populateItem(QTreeWidgetItem * parentItem, toc::Toc::const_iterator & iter)
void QTocDialog::hide()
{
int curdepth = iter->depth + 1;
QTreeWidgetItem * item;
while (iter != form_->get_toclist().end()) {
++iter;
if (iter == form_->get_toclist().end())
break;
if (iter->depth < curdepth) {
--iter;
return;
}
if (iter->depth > curdepth) {
// --iter;
return;
}
item = new QTreeWidgetItem(parentItem);
item->setText(0, toqstr(iter->str));
if (iter->depth > depth_)
tocTW->collapseItem(item);
else if (iter->depth <= depth_)
tocTW->expandItem(item);
lyxerr[Debug::GUI]
<< "Table of contents: Added item " << iter->str
<< " at depth " << iter->depth
<< " \", parent \""
<< fromqstr(parentItem->text(0)) << '"'
<< "expanded: " << tocTW->isItemExpanded(item)
<< endl;
populateItem(item, iter);
}
accept();
}
void QTocDialog::closeEvent(QCloseEvent * e)
void QTocDialog::show()
{
form_->slotWMHide();
e->accept();
form_->update();
update();
QDialog::show();
}
bool QTocDialog::isVisible() const
{
return QDialog::isVisible();
}
} // namespace frontend
} // namespace lyx

View File

@ -17,32 +17,47 @@
#include "controllers/ControlToc.h"
#include <QDialog>
#include <QCloseEvent>
class QTreeWidget;
class QTreeWidgetItem;
class QTreeViewItem;
namespace lyx {
namespace frontend {
class QToc;
class QTocDialog : public QDialog, public Ui::QTocUi {
class QTocDialog : public QDialog, public Ui::QTocUi, public Dialog::View {
Q_OBJECT
public:
QTocDialog(QToc * form);
QTocDialog(Dialog &, QToc * form);
~QTocDialog();
/// update the listview
void updateToc(bool newdepth=false);
virtual void apply();
/// update the float types
void updateType();
/// Hide the dialog from sight
void hide();
/// Redraw the dialog (e.g. if the colors have been remapped).
void redraw() {}
/// Create the dialog if necessary, update it and display it.
void show();
/// Update the display of the dialog whilst it is still visible.
void update();
/// \return true if the dialog is visible.
bool isVisible() const;
protected slots:
///
void on_tocTW_currentItemChanged(QTreeWidgetItem * current,
QTreeWidgetItem * previous);
void select(QModelIndex const & index);
///
void selectionChanged(const QModelIndex & current,
const QModelIndex & previous);
/// Temporary until the slot above work.
void on_tocTV_clicked(const QModelIndex & index );
void on_closePB_clicked();
void on_updatePB_clicked();
@ -54,13 +69,14 @@ protected slots:
void on_moveOutPB_clicked();
protected:
///
void enableButtons(bool enable = true);
void closeEvent(QCloseEvent * e);
///
void move(toc::OutlineOp const operation);
///
private:
void populateItem(QTreeWidgetItem * parentItem, toc::Toc::const_iterator& iter);
QToc * form_;
/// depth of list shown

View File

@ -0,0 +1,161 @@
/**
* \file QTocDialog.C
* 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 "TocModel.h"
#include "debug.h"
#include <vector>
#include <string>
using std::endl;
using std::pair;
using std::map;
using std::vector;
using std::string;
using std::make_pair;
namespace lyx {
namespace frontend {
TocModel::TocModel(toc::Toc const & toc_list)
{
populate(toc_list);
}
TocModel const & TocModel::operator=(toc::Toc const & toc_list)
{
populate(toc_list);
return *this;
}
toc::TocItem const TocModel::item(QModelIndex const & index) const
{
ItemMap::const_iterator it = item_map_.find(index);
BOOST_ASSERT(it != item_map_.end());
return it->second;
}
QModelIndex const TocModel::index(string const & toc_str) const
{
IndexMap::const_iterator it = index_map_.find(toc_str);
//BOOST_ASSERT(it != index_map_.end());
if (it == index_map_.end())
return QModelIndex();
return it->second;
}
void TocModel::clear()
{
QStandardItemModel::clear();
item_map_.clear();
index_map_.clear();
removeRows(0, rowCount());
removeColumns(0, columnCount());
}
void TocModel::populate(toc::Toc const & toc_list)
{
clear();
if (toc_list.empty())
return;
int current_row;
QModelIndex top_level_item;
toc::Toc::const_iterator iter = toc_list.begin();
toc::Toc::const_iterator end = toc_list.end();
insertColumns(0, 1);
while (iter != end) {
if (iter->depth == 1) {
current_row = rowCount();
insertRows(current_row, 1);
top_level_item = QStandardItemModel::index(current_row, 0);
//setData(top_level_item, toqstr(iter->str));
setData(top_level_item, toqstr(iter->str), Qt::DisplayRole);
item_map_.insert(make_pair(top_level_item, *iter));
index_map_.insert(make_pair(
iter->str.substr(iter->str.find(' ') + 1), top_level_item));
lyxerr[Debug::GUI]
<< "Toc: at depth " << iter->depth
<< ", added item " << iter->str
<< endl;
populate(iter, end, top_level_item);
}
if (iter == end)
break;
++iter;
}
setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
// emit headerDataChanged();
}
void TocModel::populate(toc::Toc::const_iterator & iter,
toc::Toc::const_iterator const & end,
QModelIndex const & parent)
{
int curdepth = iter->depth + 1;
int current_row;
QModelIndex child_item;
insertColumns(0, 1, parent);
while (iter != end) {
++iter;
if (iter == end)
break;
if (iter->depth < curdepth) {
--iter;
return;
}
if (iter->depth > curdepth) {
return;
}
current_row = rowCount(parent);
insertRows(current_row, 1, parent);
child_item = QStandardItemModel::index(current_row, 0, parent);
//setData(child_item, toqstr(iter->str));
setData(child_item, toqstr(iter->str), Qt::DisplayRole);
item_map_.insert(make_pair(child_item, *iter));
index_map_.insert(make_pair(
iter->str.substr(iter->str.find(' ') + 1), child_item));
// lyxerr[Debug::GUI]
// << "Toc: at depth " << iter->depth
// << ", added item " << iter->str
// << endl;
populate(iter, end, child_item);
}
}
} // namespace frontend
} // namespace lyx

View File

@ -0,0 +1,64 @@
// -*- C++ -*-
/**
* \file TocModel.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Abdelrazak Younes
*
* Full author contact details are available in file CREDITS.
*/
#ifndef TOCMODEL_H
#define TOCMODEL_H
#include "toc.h"
#include "qt_helpers.h"
#include <QStandardItemModel>
#include <map>
#include <string>
namespace lyx {
namespace frontend {
class TocModel: public QStandardItemModel {
Q_OBJECT
public:
///
TocModel() {}
///
TocModel(toc::Toc const & toc_list);
///
~TocModel() {}
///
TocModel const & operator=(toc::Toc const & toc_list);
///
void clear();
///
void populate(toc::Toc const & toc_list);
///
toc::TocItem const item(QModelIndex const & index) const;
///
QModelIndex const index(std::string const & toc_str) const;
private:
///
void populate(toc::Toc::const_iterator & iter,
toc::Toc::const_iterator const & end,
QModelIndex const & parent);
typedef std::map<QModelIndex, toc::TocItem> ItemMap;
///
typedef std::map<std::string, QModelIndex> IndexMap;
///
ItemMap item_map_;
IndexMap index_map_;
};
} // namespace frontend
} // namespace lyx
#endif // TOCMODEL_H

View File

@ -63,9 +63,6 @@
</item>
</layout>
</item>
<item row="1" column="0" >
<widget class="QTreeWidget" name="tocTW" />
</item>
<item row="4" column="0" >
<layout class="QGridLayout" >
<property name="margin" >
@ -170,6 +167,9 @@
</item>
</layout>
</item>
<item row="1" column="0" >
<widget class="QTreeView" name="tocTV" />
</item>
</layout>
</widget>
<pixmapfunction>qPixmapFromMimeSource</pixmapfunction>