changeset 27330:e449134870fb

allow to sort editor tabs alphabetically (bug #42602) * file-editor.cc (check_actions): enable new action depending in number of tabs; (set_shortcut): set new shortcut to new action; (construct): add new action to view menu and to the tabs context menu; * file-editor.h: new action for sorting tabs; * shortcut-manager.cc (do_init_data): new shortcut for sorting tabs with empty default * tab-bar.cc (switch_tab): new function for sorting tabs alphabetically; (mousePressEvent): do not change current index if no tab was removed by activating an entry of the context menu; * tab-bar.h: new function sort_tabs_alph
author Torsten Lilge <ttl-octave@mailbox.org>
date Wed, 07 Aug 2019 19:21:35 +0200
parents d5e24466835f
children 3bf19af20af8
files libgui/src/m-editor/file-editor.cc libgui/src/m-editor/file-editor.h libgui/src/shortcut-manager.cc libgui/src/tab-bar.cc libgui/src/tab-bar.h
diffstat 5 files changed, 56 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor.cc	Tue Aug 06 21:14:38 2019 -0700
+++ b/libgui/src/m-editor/file-editor.cc	Wed Aug 07 19:21:35 2019 +0200
@@ -205,6 +205,7 @@
     m_close_action->setEnabled (have_tabs);
     m_close_all_action->setEnabled (have_tabs);
     m_close_others_action->setEnabled (have_tabs && m_tab_widget->count () > 1);
+    m_sort_tabs_action->setEnabled (have_tabs && m_tab_widget->count () > 1);
 
     emit editor_tabs_changed_signal (have_tabs);
   }
@@ -1255,6 +1256,7 @@
     shortcut_manager::set_shortcut (m_zoom_in_action, "editor_view:zoom_in");
     shortcut_manager::set_shortcut (m_zoom_out_action, "editor_view:zoom_out");
     shortcut_manager::set_shortcut (m_zoom_normal_action, "editor_view:zoom_normal");
+    shortcut_manager::set_shortcut (m_sort_tabs_action, "editor_view:sort_tabs");
 
     // Debug menu
     shortcut_manager::set_shortcut (m_toggle_breakpoint_action, "editor_debug:toggle_breakpoint");
@@ -2065,6 +2067,13 @@
                     tr ("&Normal Size"),
                     SLOT (zoom_normal (bool)));
 
+    view_menu->addSeparator ();
+
+    m_sort_tabs_action
+      = add_action (view_menu, tr ("&Sort Tabs Alphabetically"),
+                    SLOT (sort_tabs_alph (void)),
+                    m_tab_widget->get_tab_bar ());
+
     m_menu_bar->addMenu (view_menu);
 
     // debug menu
@@ -2196,6 +2205,8 @@
     ctx_men->addAction (m_close_action);
     ctx_men->addAction (m_close_all_action);
     ctx_men->addAction (m_close_others_action);
+    ctx_men->addSeparator ();
+    ctx_men->addAction (m_sort_tabs_action);
 
     // signals
     connect (this, SIGNAL (request_settings_dialog (const QString&)),
--- a/libgui/src/m-editor/file-editor.h	Tue Aug 06 21:14:38 2019 -0700
+++ b/libgui/src/m-editor/file-editor.h	Wed Aug 07 19:21:35 2019 +0200
@@ -431,6 +431,7 @@
     QAction *m_switch_right_tab_action;
     QAction *m_move_tab_left_action;
     QAction *m_move_tab_right_action;
+    QAction *m_sort_tabs_action;
 
     QAction *m_toggle_breakpoint_action;
     QAction *m_next_breakpoint_action;
--- a/libgui/src/shortcut-manager.cc	Tue Aug 06 21:14:38 2019 -0700
+++ b/libgui/src/shortcut-manager.cc	Wed Aug 07 19:21:35 2019 +0200
@@ -456,6 +456,8 @@
           QKeySequence ());
     init (tr ("Show Horizontal Scrollbar"), "editor_view:show_hscrollbar",
           QKeySequence ());
+    init (tr ("Sort Tabs Alphabetically"), "editor_view:sort_tabs",
+          QKeySequence ());
 
     // debug
     init (tr ("Toggle Breakpoint"), "editor_debug:toggle_breakpoint",
--- a/libgui/src/tab-bar.cc	Tue Aug 06 21:14:38 2019 -0700
+++ b/libgui/src/tab-bar.cc	Wed Aug 07 19:21:35 2019 +0200
@@ -90,6 +90,43 @@
       setCurrentIndex (new_pos);
   }
 
+  void tab_bar::sort_tabs_alph (void)
+  {
+    QString current_title = tabText (currentIndex ());
+    int tab_with_focus = 0;
+
+    // Get all tab title and sort
+    QStringList tab_texts;
+
+    for (int i = 0; i < count (); i++)
+      tab_texts.append (tabText (i));
+
+    tab_texts.sort ();
+
+    // Move tab into the order of the generated string list
+    for (int title = 0; title < tab_texts.count (); title++)
+      {
+        // Target tab is same as palce of title in QStringList.
+        // Find index of next title in string list, leaving out the
+        // tabs (or titles) that were alredy moved
+        for (int tab = title; tab < count (); tab++)
+          {
+            if (tabText (tab) == tab_texts.at (title))
+              {
+                // Index of next tile found, so move tab into next position
+                moveTab (tab, title);
+
+                if (tab_texts.at (title) == current_title)
+                    tab_with_focus = title;
+
+                break;
+              }
+          }
+      }
+
+    setCurrentIndex (tab_with_focus);
+  }
+
   // Reimplement mouse event for filtering out the desired mouse clicks
   void tab_bar::mousePressEvent (QMouseEvent *me)
   {
@@ -111,6 +148,8 @@
     if (clicked_idx >= 0)
       {
         int current_idx = currentIndex ();
+        int current_count = count ();
+
         // detect the mouse click
         if ((me->type () == QEvent::MouseButtonDblClick
              && me->button() == Qt::LeftButton)
@@ -138,8 +177,9 @@
                 // No action selected, back to previous tab
                 setCurrentIndex (current_idx);
               }
-            else
+            else if (count () < current_count)
               {
+                // A tab was closed:
                 // Was the possibly only closed tab before or after the
                 // previously current tab? According to the result, use previous
                 // index or reduce it by one. Also prevent using a too large
--- a/libgui/src/tab-bar.h	Tue Aug 06 21:14:38 2019 -0700
+++ b/libgui/src/tab-bar.h	Wed Aug 07 19:21:35 2019 +0200
@@ -56,6 +56,7 @@
     void switch_right_tab (void);
     void move_tab_left (void);
     void move_tab_right (void);
+    void sort_tabs_alph (void);
 
   protected: