On 26/09/2008 12:14, Pavel Sanda wrote:
Abdelrazak Younes wrote:
I implemented toc list sorting (attached). I am reasonably confident with
it but I can wait until after rc3.

The patch removes the useless update button in the navigator pane and

it was not useless. in fact particularly with index there was needed from time
to time. what if we put sort checkbox on the right side of the slider?

New patch attached.

Abdel.

Index: TocModel.cpp
===================================================================
--- TocModel.cpp        (revision 26565)
+++ TocModel.cpp        (working copy)
@@ -45,9 +45,52 @@
 }
 
 
+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 @@
 
        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 @@
 }
 
 
-TocModel::TocModel(QObject * parent): QStandardItemModel(parent),      
-               maxdepth_(0), mindepth_(0)
-{
-}
-
-
 void TocModel::reset()
 {
-       QStandardItemModel::reset();
+       model_->reset();
 }
 
 
@@ -89,10 +126,10 @@
                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 @@
                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 @@
                        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 @@
 
        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 @@
                }
                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 @@
        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 @@
 }
 
 
-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 @@
                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 @@
 }
 
 
+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
 
Index: TocModel.h
===================================================================
--- TocModel.h  (revision 26565)
+++ TocModel.h  (working copy)
@@ -16,12 +16,10 @@
 
 #include <QHash>
 #include <QList>
+#include <QSortFilterProxyModel>
 #include <QStandardItemModel>
 #include <QStringList>
 
-class QAbstractItemModel;
-class QSortFilterProxyModel;
-
 namespace lyx {
 
 class Buffer;
@@ -42,7 +40,7 @@
 };
 
 
-class TocModel : public QStandardItemModel
+class TocModel
 {
 public:
        ///
@@ -52,6 +50,16 @@
        ///
        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 @@
        ///
        void populate(unsigned int & index, QModelIndex const & parent);
        ///
+       TocTypeModel * model_;
+       ///
+       QSortFilterProxyModel * sorted_model_;
+       ///
+       bool is_sorted_;
+       ///
        QList<QModelIndex> toc_indexes_;
        ///
        Toc const * toc_;
@@ -82,7 +96,7 @@
        ///
        int depth(QString const & type);
        ///
-       QStandardItemModel * model(QString const & type);
+       QAbstractItemModel * model(QString const & type);
        ///
        QAbstractItemModel * nameModel();
        ///
@@ -93,6 +107,10 @@
        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.
Index: TocWidget.cpp
===================================================================
--- TocWidget.cpp       (revision 26565)
+++ TocWidget.cpp       (working copy)
@@ -99,6 +99,12 @@
 }
 
 
+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 @@
 void TocWidget::enableControls(bool enable)
 {
        updateTB->setEnabled(enable);
+       sortCB->setEnabled(enable);
 
        if (!canOutline(current_type_))
                enable = false;
@@ -251,11 +258,16 @@
        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);
Index: TocWidget.h
===================================================================
--- TocWidget.h (revision 26565)
+++ TocWidget.h (working copy)
@@ -48,6 +48,7 @@
        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();
Index: ui/TocUi.ui
===================================================================
--- ui/TocUi.ui (revision 26565)
+++ ui/TocUi.ui (working copy)
@@ -9,39 +9,44 @@
     <x>0</x>
     <y>0</y>
     <width>185</width>
-    <height>250</height>
+    <height>184</height>
    </rect>
   </property>
   <property name="windowTitle" >
    <string/>
   </property>
-  <layout class="QGridLayout" >
-   <property name="margin" >
-    <number>9</number>
-   </property>
-   <property name="spacing" >
-    <number>6</number>
-   </property>
-   <item row="1" column="0" >
+  <layout class="QGridLayout" name="gridLayout" >
+   <item row="0" column="0" colspan="4" >
+    <widget class="QComboBox" name="typeCO" >
+     <property name="sizePolicy" >
+      <sizepolicy vsizetype="Fixed" hsizetype="Ignored" >
+       <horstretch>0</horstretch>
+       <verstretch>0</verstretch>
+      </sizepolicy>
+     </property>
+     <property name="toolTip" >
+      <string>Switch between available lists (table of contents, list of 
figures, list of tables, and others)</string>
+     </property>
+    </widget>
+   </item>
+   <item row="1" column="0" colspan="4" >
     <widget class="QTreeView" name="tocTV" >
      <property name="sizePolicy" >
-      <sizepolicy>
-       <hsizetype>13</hsizetype>
-       <vsizetype>7</vsizetype>
+      <sizepolicy vsizetype="Expanding" hsizetype="Ignored" >
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
     </widget>
    </item>
-   <item row="3" column="0" >
+   <item row="3" column="0" colspan="4" >
     <layout class="QHBoxLayout" >
-     <property name="margin" >
-      <number>0</number>
-     </property>
      <property name="spacing" >
       <number>6</number>
      </property>
+     <property name="margin" >
+      <number>0</number>
+     </property>
      <item>
       <widget class="QToolButton" name="updateTB" >
        <property name="toolTip" >
@@ -51,7 +56,8 @@
         <string>...</string>
        </property>
        <property name="icon" >
-        <iconset>../../../../lib/images/reload.png</iconset>
+        <iconset>
+         
<normaloff>../../../../lib/images/reload.png</normaloff>../../../../lib/images/reload.png</iconset>
        </property>
        <property name="iconSize" >
         <size>
@@ -66,7 +72,7 @@
        <property name="orientation" >
         <enum>Qt::Horizontal</enum>
        </property>
-       <property name="sizeHint" >
+       <property name="sizeHint" stdset="0" >
         <size>
          <width>16</width>
          <height>20</height>
@@ -83,7 +89,8 @@
         <string>...</string>
        </property>
        <property name="icon" >
-        <iconset>../../../../lib/images/promote.png</iconset>
+        <iconset>
+         
<normaloff>../../../../lib/images/promote.png</normaloff>../../../../lib/images/promote.png</iconset>
        </property>
        <property name="iconSize" >
         <size>
@@ -102,7 +109,8 @@
         <string>...</string>
        </property>
        <property name="icon" >
-        <iconset>../../../../lib/images/demote.png</iconset>
+        <iconset>
+         
<normaloff>../../../../lib/images/demote.png</normaloff>../../../../lib/images/demote.png</iconset>
        </property>
        <property name="iconSize" >
         <size>
@@ -121,7 +129,8 @@
         <string>...</string>
        </property>
        <property name="icon" >
-        <iconset>../../../../lib/images/down.png</iconset>
+        <iconset>
+         
<normaloff>../../../../lib/images/down.png</normaloff>../../../../lib/images/down.png</iconset>
        </property>
        <property name="iconSize" >
         <size>
@@ -140,7 +149,8 @@
         <string>...</string>
        </property>
        <property name="icon" >
-        <iconset>../../../../lib/images/up.png</iconset>
+        <iconset>
+         
<normaloff>../../../../lib/images/up.png</normaloff>../../../../lib/images/up.png</iconset>
        </property>
        <property name="iconSize" >
         <size>
@@ -152,27 +162,10 @@
      </item>
     </layout>
    </item>
-   <item row="0" column="0" >
-    <widget class="QComboBox" name="typeCO" >
-     <property name="sizePolicy" >
-      <sizepolicy>
-       <hsizetype>13</hsizetype>
-       <vsizetype>0</vsizetype>
-       <horstretch>0</horstretch>
-       <verstretch>0</verstretch>
-      </sizepolicy>
-     </property>
-     <property name="toolTip" >
-      <string>Switch between available lists (table of contents, list of 
figures, list of tables, and others)</string>
-     </property>
-    </widget>
-   </item>
-   <item row="2" column="0" >
+   <item row="2" column="0" colspan="3" >
     <widget class="QSlider" name="depthSL" >
      <property name="sizePolicy" >
-      <sizepolicy>
-       <hsizetype>13</hsizetype>
-       <vsizetype>0</vsizetype>
+      <sizepolicy vsizetype="Fixed" hsizetype="Ignored" >
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
@@ -197,12 +190,18 @@
      </property>
     </widget>
    </item>
+   <item row="2" column="3" >
+    <widget class="QCheckBox" name="sortCB" >
+     <property name="text" >
+      <string>Sort</string>
+     </property>
+    </widget>
+   </item>
   </layout>
  </widget>
  <tabstops>
   <tabstop>typeCO</tabstop>
   <tabstop>tocTV</tabstop>
-  <tabstop>depthSL</tabstop>
  </tabstops>
  <includes>
   <include location="local" >qt_i18n.h</include>

Reply via email to