]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt/TocWidget.cpp
Make code a bit easier to read
[lyx.git] / src / frontends / qt / TocWidget.cpp
index 957b3a64e03a8c782cb7269eaa5cb9ba9148ce1c..03d870a5f4c37d7123348cf91cc93e1b9646c61e 100644 (file)
@@ -47,8 +47,8 @@ namespace lyx {
 namespace frontend {
 
 TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
-       : QWidget(parent), depth_(0), persistent_(false), gui_view_(gui_view),
-         timer_(new QTimer(this))
+       : QWidget(parent), depth_(0), persistent_(false), keep_expanded_(false),
+         gui_view_(gui_view), timer_(new QTimer(this))
 {
        setupUi(this);
 
@@ -102,13 +102,8 @@ TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
                this, SLOT(showContextMenu(const QPoint &)));
        connect(filter_, SIGNAL(textEdited(QString)),
                this, SLOT(filterContents()));
-#if (QT_VERSION < 0x050000)
-       connect(filter_, SIGNAL(downPressed()),
-               tocTV, SLOT(setFocus()));
-#else
        connect(filter_, &FancyLineEdit::downPressed,
                tocTV, [this](){ focusAndHighlight(tocTV); });
-#endif
        connect(activeFilterCO, SIGNAL(activated(int)),
                this, SLOT(filterContents()));
 
@@ -198,7 +193,6 @@ bool TocWidget::getStatus(Cursor & cur, FuncRequest const & cmd,
 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd,
                DispatchResult & dr)
 {
-
        Inset * inset = itemInset();
 
        QModelIndex const & index = tocTV->currentIndex();
@@ -268,7 +262,8 @@ void TocWidget::on_tocTV_activated(QModelIndex const & index)
 
 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
 {
-
+       DocIterator const & dit = gui_view_.documentBufferView()->cursor();
+       keep_expanded_ = gui_view_.tocModels().currentIndex(current_type_, dit) == index;
        Qt::MouseButtons const button = QApplication::mouseButtons();
        if (button & Qt::LeftButton) {
                goTo(index);
@@ -278,6 +273,12 @@ void TocWidget::on_tocTV_pressed(QModelIndex const & index)
 }
 
 
+void TocWidget::on_tocTV_doubleClicked(QModelIndex const &)
+{
+       keep_expanded_ = true;
+}
+
+
 void TocWidget::goTo(QModelIndex const & index)
 {
        LYXERR(Debug::GUI, "goto " << index.row()
@@ -346,9 +347,8 @@ void TocWidget::setTreeDepth(int depth)
 }
 
 
-void TocWidget::on_typeCO_currentIndexChanged(int index)
+void TocWidget::on_typeCO_activated(int index)
 {
-
        if (index == -1)
                return;
        current_type_ = typeCO->itemData(index).toString();
@@ -366,7 +366,7 @@ void TocWidget::outline(FuncCode func_code)
 
        //if another window is active, this attempt will fail,
        //but it will work at least for the second attempt
-       gui_view_.activateWindow(); 
+       gui_view_.activateWindow();
 
        enableControls(false);
        goTo(list[0]);
@@ -378,9 +378,23 @@ void TocWidget::outline(FuncCode func_code)
 
 void TocWidget::sendDispatch(FuncRequest fr)
 {
-
        fr.setViewOrigin(&gui_view_);
+       GuiWorkArea * old_wa = gui_view_.currentWorkArea();
+       GuiWorkArea * doc_wa = gui_view_.currentMainWorkArea();
+       /* The ToC command should be dispatched to the document work area,
+        * not the Adv. Find&Replace (which is the only other know
+        * possibility.
+        */
+       if (doc_wa != nullptr && doc_wa != old_wa)
+               gui_view_.setCurrentWorkArea(doc_wa);
        DispatchResult const & dr = dispatch(fr);
+       /* If the current workarea has not explicitely changed, and the
+        * original one is still visible, let's reset it.
+        */
+       if (gui_view_.currentWorkArea() == doc_wa
+            && gui_view_.hasVisibleWorkArea(old_wa)
+            && doc_wa != old_wa)
+               gui_view_.setCurrentWorkArea(old_wa);
        if (dr.error())
                gui_view_.message(dr.message());
 }
@@ -483,10 +497,11 @@ void TocWidget::updateView()
        // Expensive operations are on a timer.  We finish the update immediately
        // for sparse edition actions, i.e. there was no edition/cursor movement
        // recently, then every 300ms.
-       if (!timer_->isActive()) {
+       if (!timer_->isActive() && !keep_expanded_) {
                finishUpdateView();
                timer_->start(300);
        }
+       keep_expanded_ = false;
 }
 
 
@@ -503,8 +518,9 @@ void TocWidget::finishUpdateView()
        // text and moving with arrows. For bigger operations, this is negligible,
        // and outweighted by TocModels::reset() anyway.
        if (canNavigate()) {
-               if (!persistent_)
+               if (!persistent_ && !keep_expanded_)
                        setTreeDepth(depth_);
+               keep_expanded_ = false;
                persistentCB->setChecked(persistent_);
                // select the item at current cursor location
                if (gui_view_.documentBufferView()) {
@@ -523,8 +539,13 @@ void TocWidget::filterContents()
 
        QModelIndexList indices = tocTV->model()->match(
                tocTV->model()->index(0, 0),
-               Qt::DisplayRole, "*", -1,
-               Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
+               Qt::DisplayRole, ".*", -1,
+#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
+               Qt::MatchFlags(Qt::MatchRegularExpression|Qt::MatchRecursive));
+#else
+               // deprecated in Qt 5.15.
+               Qt::MatchFlags(Qt::MatchRegExp|Qt::MatchRecursive));
+#endif
 
        bool const show_active =
                activeFilterCO->currentIndex() != 2;
@@ -532,11 +553,11 @@ void TocWidget::filterContents()
                activeFilterCO->currentIndex() != 1;
 
        int size = indices.size();
+       QString const matchstring = filter_ ? filter_->text() : QString();
        for (int i = 0; i < size; i++) {
                QModelIndex index = indices[i];
-               bool matches = filter_ &&
-                       index.data().toString().contains(
-                               filter_->text(), Qt::CaseInsensitive);
+               bool matches = index.data().toString().contains(
+                                       matchstring, Qt::CaseInsensitive);
                TocItem const & item =
                        gui_view_.tocModels().currentItem(current_type_, index);
                matches &= (show_active && item.isOutput()) || (show_inactive && !item.isOutput());
@@ -593,6 +614,8 @@ void TocWidget::init(QString const & str)
        typeCO->setCurrentIndex(new_index);
        typeCO->blockSignals(false);
        updateViewNow();
+       if (persistent_)
+               setTreeDepth(depth_);
 }
 
 } // namespace frontend