From: Abdelrazak Younes Date: Sun, 28 Sep 2008 17:14:29 +0000 (+0000) Subject: Add a new 'Sort' box to the Navigator so that each list can be optionally sorted. X-Git-Tag: 1.6.10~3285 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=4b8f0d359c29620fdbe54d06f2f2afb3f89e23db;p=lyx.git Add a new 'Sort' box to the Navigator so that each list can be optionally sorted. * TocModel: redesign so that sorted list are available. * TocWidget/TocUi: handle the new Sort check box. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@26602 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/frontends/qt4/TocModel.cpp b/src/frontends/qt4/TocModel.cpp index bc5178b2a9..8bb33f9258 100644 --- a/src/frontends/qt4/TocModel.cpp +++ b/src/frontends/qt4/TocModel.cpp @@ -45,9 +45,52 @@ void TocTypeModel::reset() } +TocModel::TocModel(QObject * parent) + : model_(new TocTypeModel(parent)), + sorted_model_(new QSortFilterProxyModel(parent)), + is_sorted_(false), maxdepth_(0), mindepth_(0) +{ +#if QT_VERSION >= 0x040300 + sorted_model_->setSortLocaleAware(true); +#endif + sorted_model_->setSourceModel(model_); +} + + +QAbstractItemModel * TocModel::model() +{ + if (is_sorted_) + return sorted_model_; + return model_; +} + + +QAbstractItemModel const * TocModel::model() const +{ + if (is_sorted_) + return sorted_model_; + return model_; +} + + +void TocModel::clear() +{ + model_->blockSignals(true); + model_->clear(); + model_->blockSignals(false); +} + + +void TocModel::sort(bool sort_it) +{ + is_sorted_ = sort_it; + if (is_sorted_) + sorted_model_->sort(0); +} + TocItem const & TocModel::tocItem(QModelIndex const & index) const { - return (*toc_)[data(index, Qt::UserRole).toUInt()]; + return (*toc_)[model()->data(index, Qt::UserRole).toUInt()]; } @@ -58,7 +101,7 @@ QModelIndex TocModel::modelIndex(DocIterator const & dit) const unsigned int const toc_index = toc_->item(dit) - toc_->begin(); - QModelIndexList list = match(index(0, 0), Qt::UserRole, + QModelIndexList list = model()->match(model()->index(0, 0), Qt::UserRole, QVariant(toc_index), 1, Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive)); @@ -67,15 +110,9 @@ QModelIndex TocModel::modelIndex(DocIterator const & dit) const } -TocModel::TocModel(QObject * parent): QStandardItemModel(parent), - maxdepth_(0), mindepth_(0) -{ -} - - void TocModel::reset() { - QStandardItemModel::reset(); + model_->reset(); } @@ -89,10 +126,10 @@ void TocModel::reset(Toc const & toc) return; } - blockSignals(true); + model_->blockSignals(true); int current_row; QModelIndex top_level_item; - insertColumns(0, 1); + model_->insertColumns(0, 1); maxdepth_ = 0; mindepth_ = INT_MAX; @@ -101,11 +138,11 @@ void TocModel::reset(Toc const & toc) TocItem const & item = (*toc_)[index]; maxdepth_ = max(maxdepth_, item.depth()); mindepth_ = min(mindepth_, item.depth()); - current_row = rowCount(); - insertRows(current_row, 1); - top_level_item = QStandardItemModel::index(current_row, 0); - setData(top_level_item, toqstr(item.str()), Qt::DisplayRole); - setData(top_level_item, index, Qt::UserRole); + current_row = model_->rowCount(); + model_->insertRows(current_row, 1); + top_level_item = model_->index(current_row, 0); + model_->setData(top_level_item, toqstr(item.str()), Qt::DisplayRole); + model_->setData(top_level_item, index, Qt::UserRole); LYXERR(Debug::GUI, "Toc: at depth " << item.depth() << ", added item " << item.str()); @@ -115,8 +152,10 @@ void TocModel::reset(Toc const & toc) break; } - setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole); - blockSignals(false); + model_->setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole); + if (is_sorted_) + sorted_model_->sort(0); + model_->blockSignals(false); reset(); // emit headerDataChanged(); } @@ -128,7 +167,7 @@ void TocModel::populate(unsigned int & index, QModelIndex const & parent) int current_row; QModelIndex child_item; - insertColumns(0, 1, parent); + model_->insertColumns(0, 1, parent); size_t end = toc_->size(); ++index; @@ -140,11 +179,11 @@ void TocModel::populate(unsigned int & index, QModelIndex const & parent) } maxdepth_ = max(maxdepth_, item.depth()); mindepth_ = min(mindepth_, item.depth()); - current_row = rowCount(parent); - insertRows(current_row, 1, parent); - child_item = QStandardItemModel::index(current_row, 0, parent); - setData(child_item, toqstr(item.str()), Qt::DisplayRole); - setData(child_item, index, Qt::UserRole); + current_row = model_->rowCount(parent); + model_->insertRows(current_row, 1, parent); + child_item = model_->index(current_row, 0, parent); + model_->setData(child_item, toqstr(item.str()), Qt::DisplayRole); + model_->setData(child_item, index, Qt::UserRole); populate(index, child_item); if (index >= end) break; @@ -182,11 +221,8 @@ void TocModels::clear() names_->clear(); names_->blockSignals(false); iterator end = models_.end(); - for (iterator it = models_.begin(); it != end; ++it) { - it.value()->blockSignals(true); + for (iterator it = models_.begin(); it != end; ++it) it.value()->clear(); - it.value()->blockSignals(false); - } } @@ -199,13 +235,13 @@ int TocModels::depth(QString const & type) } -QStandardItemModel * TocModels::model(QString const & type) +QAbstractItemModel * TocModels::model(QString const & type) { if (!bv_) return 0; iterator it = models_.find(type); if (it != models_.end()) - return it.value(); + return it.value()->model(); LYXERR0("type not found: " << type); return 0; } @@ -233,7 +269,7 @@ void TocModels::goTo(QString const & type, QModelIndex const & index) const LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!"); return; } - LASSERT(index.model() == it.value(), return); + LASSERT(index.model() == it.value()->model(), return); TocItem const item = it.value()->tocItem(index); LYXERR(Debug::GUI, "TocModels::goTo " << item.str()); dispatch(item.action()); @@ -286,6 +322,26 @@ void TocModels::reset(BufferView const * bv) } +bool TocModels::isSorted(QString const & type) const +{ + const_iterator it = models_.find(type); + if (it == models_.end()) { + LYXERR0("type not found: " << type); + return false; + } + return it.value()->isSorted(); +} + + +void TocModels::sort(QString const & type, bool sort_it) +{ + iterator it = models_.find(type); + if (it == models_.end()) + LYXERR0("type not found: " << type); + else + it.value()->sort(sort_it); +} + } // namespace frontend } // namespace lyx diff --git a/src/frontends/qt4/TocModel.h b/src/frontends/qt4/TocModel.h index 14ab2622e2..d5e7ea830d 100644 --- a/src/frontends/qt4/TocModel.h +++ b/src/frontends/qt4/TocModel.h @@ -16,12 +16,10 @@ #include #include +#include #include #include -class QAbstractItemModel; -class QSortFilterProxyModel; - namespace lyx { class Buffer; @@ -42,7 +40,7 @@ public: }; -class TocModel : public QStandardItemModel +class TocModel { public: /// @@ -52,6 +50,16 @@ public: /// void reset(); /// + void clear(); + /// + QAbstractItemModel * model(); + /// + QAbstractItemModel const * model() const; + /// + void sort(bool sort_it); + /// + bool isSorted() const { return is_sorted_; } + /// TocItem const & tocItem(QModelIndex const & index) const; /// QModelIndex modelIndex(DocIterator const & dit) const; @@ -62,6 +70,12 @@ private: /// void populate(unsigned int & index, QModelIndex const & parent); /// + TocTypeModel * model_; + /// + QSortFilterProxyModel * sorted_model_; + /// + bool is_sorted_; + /// QList toc_indexes_; /// Toc const * toc_; @@ -82,7 +96,7 @@ public: /// int depth(QString const & type); /// - QStandardItemModel * model(QString const & type); + QAbstractItemModel * model(QString const & type); /// QAbstractItemModel * nameModel(); /// @@ -93,6 +107,10 @@ public: void init(Buffer const & buffer); /// void updateBackend() const; + /// + void sort(QString const & type, bool sort_it); + /// + bool isSorted(QString const & type) const; Q_SIGNALS: /// Signal that the internal toc_models_ has been reset. diff --git a/src/frontends/qt4/TocWidget.cpp b/src/frontends/qt4/TocWidget.cpp index fbb017ad7c..3b7a398857 100644 --- a/src/frontends/qt4/TocWidget.cpp +++ b/src/frontends/qt4/TocWidget.cpp @@ -99,6 +99,12 @@ void TocWidget::on_updateTB_clicked() } +void TocWidget::on_sortCB_stateChanged(int state) +{ + gui_view_.tocModels().sort(current_type_, state == Qt::Checked); + updateView(); +} + /* FIXME (Ugras 17/11/06): I have implemented a indexDepth function to get the model indices. In my opinion, somebody should derive a new qvariant class for tocModelItem @@ -213,6 +219,7 @@ static bool canOutline(QString const & type) void TocWidget::enableControls(bool enable) { updateTB->setEnabled(enable); + sortCB->setEnabled(enable); if (!canOutline(current_type_)) enable = false; @@ -251,11 +258,16 @@ void TocWidget::updateView() typeCO->setEnabled(true); tocTV->setEnabled(true); - QStandardItemModel * toc_model = gui_view_.tocModels().model(current_type_); + QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_); if (tocTV->model() != toc_model) { tocTV->setModel(toc_model); tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers); } + + sortCB->blockSignals(true); + sortCB->setChecked(gui_view_.tocModels().isSorted(current_type_)); + sortCB->blockSignals(false); + bool controls_enabled = toc_model && toc_model->rowCount() > 0 && !gui_view_.buffer()->isReadonly(); enableControls(controls_enabled); diff --git a/src/frontends/qt4/TocWidget.h b/src/frontends/qt4/TocWidget.h index a379bf0238..b80e4776d0 100644 --- a/src/frontends/qt4/TocWidget.h +++ b/src/frontends/qt4/TocWidget.h @@ -48,6 +48,7 @@ protected Q_SLOTS: void on_tocTV_activated(QModelIndex const &); void on_tocTV_clicked(QModelIndex const &); void on_updateTB_clicked(); + void on_sortCB_stateChanged(int state); void on_depthSL_valueChanged(int depth); void on_typeCO_currentIndexChanged(int value); void on_moveUpTB_clicked(); diff --git a/src/frontends/qt4/ui/TocUi.ui b/src/frontends/qt4/ui/TocUi.ui index 61c9ea2bfb..2fe2231eb3 100644 --- a/src/frontends/qt4/ui/TocUi.ui +++ b/src/frontends/qt4/ui/TocUi.ui @@ -9,39 +9,44 @@ 0 0 185 - 250 + 184 - - - 9 - - - 6 - - + + + + + + 0 + 0 + + + + Switch between available lists (table of contents, list of figures, list of tables, and others) + + + + - - 13 - 7 + 0 0 - + - - 0 - 6 + + 0 + @@ -51,7 +56,8 @@ ... - ../../../../lib/images/reload.png + + ../../../../lib/images/reload.png../../../../lib/images/reload.png @@ -66,7 +72,7 @@ Qt::Horizontal - + 16 20 @@ -83,7 +89,8 @@ ... - ../../../../lib/images/promote.png + + ../../../../lib/images/promote.png../../../../lib/images/promote.png @@ -102,7 +109,8 @@ ... - ../../../../lib/images/demote.png + + ../../../../lib/images/demote.png../../../../lib/images/demote.png @@ -121,7 +129,8 @@ ... - ../../../../lib/images/down.png + + ../../../../lib/images/down.png../../../../lib/images/down.png @@ -140,7 +149,8 @@ ... - ../../../../lib/images/up.png + + ../../../../lib/images/up.png../../../../lib/images/up.png @@ -152,27 +162,10 @@ - - - - - 13 - 0 - 0 - 0 - - - - Switch between available lists (table of contents, list of figures, list of tables, and others) - - - - + - - 13 - 0 + 0 0 @@ -197,12 +190,18 @@ + + + + Sort + + + typeCO tocTV - depthSL qt_i18n.h