Mercurial > octave
changeset 24679:a3e67a9e7be5
move derived tab bar from editor and doc viewer into separate file
* file-editor.cc (file_editor_tab_bar::file_editor_tab_bar),
(file_editor_tab_bar::create_context_menu),
(file_editor_tab_bar::mousePressEvent): remove all functions related to the
derived file_editor_tab_bar class;
(file_editor_tab_widget::file_editor_tab_widget): use the new tab_bar;
(file_editor::construct): add the close actions to the context menu
provided by tab_bar
* file-editor.h: derived tab bar removed
* module.mk: new files tab_bar.cc and tab_bar.h
* webinfo.cc (webinfo) use new tab_bar instead of special webinfo_tab_bar,
connect tab bar signal for closing tab;
(webinfo_tab_bar::webinfo_tab_bar),
(webinfo_tab_bar::mousePressEvent): removed
* webinfo.h: removed webinfo_tab_bar
* tab-bar.cc: new tab bar for editor and doc viewer derived from QTabBar;
(tab_bar): constructor, create context menu;
(mousePressEvent): reimplemented mouse press event for detecting mouse
actions for closing a tab, in case, emitting related signal
* tab-bar.h: new tab bar for editor and doc viewer derived from QTabBar
author | Torsten <mttl@mailbox.org> |
---|---|
date | Sat, 03 Feb 2018 09:51:34 +0100 |
parents | aa9c29f48718 |
children | 2bd8ba9bc39b |
files | libgui/src/m-editor/file-editor.cc libgui/src/m-editor/file-editor.h libgui/src/module.mk libgui/src/qtinfo/webinfo.cc libgui/src/qtinfo/webinfo.h libgui/src/tab-bar.cc libgui/src/tab-bar.h |
diffstat | 7 files changed, 193 insertions(+), 253 deletions(-) [+] |
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor.cc Fri Feb 02 14:16:30 2018 -0800 +++ b/libgui/src/m-editor/file-editor.cc Sat Feb 03 09:51:34 2018 +0100 @@ -29,6 +29,7 @@ #include "file-editor.h" #include "resource-manager.h" #include "shortcut-manager.h" +#include "tab-bar.h" #include <QApplication> #include <QFile> @@ -48,112 +49,14 @@ #include "octave-link.h" #include "utils.h" -// Functions of the the reimplemented tab bar - -file_editor_tab_bar::file_editor_tab_bar (QWidget *p) - : QTabBar (p), m_context_menu (new QMenu (this)) -{ } - -file_editor_tab_bar::~file_editor_tab_bar (void) -{ - delete m_context_menu; -} - -// Create the context menu and fill it with actions from the editor -void -file_editor_tab_bar::create_context_menu (QList<QAction*> *actions) -{ - for (int i = 0; i < actions->count (); i++) - m_context_menu->addAction (actions->at (i)); -} - -// Reimplement mouse event for filtering out the desired mouse clicks -void -file_editor_tab_bar::mousePressEvent (QMouseEvent *me) -{ - QPoint click_pos; - int clicked_idx = -1; - - // detect the tab where the click occured - for (int i = 0; i < count (); i++) - { - click_pos = mapToGlobal (me->pos ()); - if (tabRect (i).contains (mapFromGlobal (click_pos))) - { - clicked_idx = i; - break; - } - } - - // If a tab was clicked - if (clicked_idx >= 0) - { - int current_idx = currentIndex (); - // detect the mouse click - if ((me->type () == QEvent::MouseButtonDblClick && - me->button() == Qt::LeftButton) || - (me->type () != QEvent::MouseButtonDblClick && - me->button() == Qt::MidButton)) - { - // Middle click or double click -> close the tab - // Make the clicked tab the current one and close it - setCurrentIndex (clicked_idx); - emit close_current_tab_signal (true); - // Was the closed tab before or after the previously current tab? - // According to the result, use previous index or reduce it by one - if (current_idx - clicked_idx > 0) - setCurrentIndex (current_idx - 1); - else if (current_idx - clicked_idx < 0) - setCurrentIndex (current_idx); - } - else if (me->type () != QEvent::MouseButtonDblClick && - me->button() == Qt::RightButton) - { - // Right click, show context menu - setCurrentIndex (clicked_idx); - if (! m_context_menu->exec (click_pos)) - { - // No action selected, back to previous tab - setCurrentIndex (current_idx); - } - else - { - // 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 - // if other or all files were closed. - int new_idx = count () - 1; - if (new_idx > 0) - { - if (current_idx - clicked_idx > 0) - new_idx = current_idx - 1; - else if (current_idx - clicked_idx < 0) - new_idx = current_idx; - } - if (new_idx >= 0) - setCurrentIndex (new_idx); - } - } - else - { - // regular handling of the mouse event - QTabBar::mousePressEvent (me); - } - } - else - { - // regular handling of the mouse event - QTabBar::mousePressEvent (me); - } -} // Functions of the the reimplemented tab widget file_editor_tab_widget::file_editor_tab_widget (QWidget *p) : QTabWidget (p) { - file_editor_tab_bar *bar; - bar = new file_editor_tab_bar (this); + tab_bar *bar; + bar = new tab_bar (this); connect (bar, SIGNAL (close_current_tab_signal (bool)), p->parent (), SLOT (request_close_file (bool))); @@ -2055,13 +1958,12 @@ setWidget (editor_widget); // create the context menu of the tab bar - file_editor_tab_bar *bar - = static_cast<file_editor_tab_bar *>(m_tab_widget->tabBar ()); - QList<QAction *> tab_bar_actions; - tab_bar_actions.append (m_close_action); - tab_bar_actions.append (m_close_all_action); - tab_bar_actions.append (m_close_others_action); - bar->create_context_menu (&tab_bar_actions); + tab_bar *bar + = static_cast<tab_bar *>(m_tab_widget->tabBar ()); + QMenu *ctx_men = bar->get_context_menu (); + ctx_men->addAction (m_close_action); + ctx_men->addAction (m_close_all_action); + ctx_men->addAction (m_close_others_action); // signals connect (this, SIGNAL (execute_command_in_terminal_signal (const QString&)),
--- a/libgui/src/m-editor/file-editor.h Fri Feb 02 14:16:30 2018 -0800 +++ b/libgui/src/m-editor/file-editor.h Sat Feb 03 09:51:34 2018 +0100 @@ -40,33 +40,6 @@ #include "file-editor-tab.h" -// subclassed QTabBar for usable tab-bar and reimplemented mouse event - -class file_editor_tab_bar : public QTabBar -{ - Q_OBJECT - -public: - - file_editor_tab_bar (QWidget *p); - - ~file_editor_tab_bar (void); - - void create_context_menu (QList<QAction*> *actions); - -signals: - - void close_current_tab_signal (bool); - -protected: - - void mousePressEvent(QMouseEvent *event); - -private: - - QMenu *m_context_menu; -}; - // subclassed QTabWidget for using custom tabbar class file_editor_tab_widget : public QTabWidget
--- a/libgui/src/module.mk Fri Feb 02 14:16:30 2018 -0800 +++ b/libgui/src/module.mk Sat Feb 03 09:51:34 2018 +0100 @@ -110,6 +110,7 @@ %reldir%/moc-settings-dialog.cc \ %reldir%/moc-terminal-dock-widget.cc \ %reldir%/moc-color-picker.cc \ + %reldir%/moc-tab-bar.cc \ %reldir%/moc-resource-manager.cc \ %reldir%/moc-shortcut-manager.cc \ %reldir%/moc-welcome-wizard.cc \ @@ -167,6 +168,7 @@ %reldir%/resource-manager.h \ %reldir%/settings-dialog.h \ %reldir%/shortcut-manager.h \ + %reldir%/tab-bar.h \ %reldir%/thread-manager.h \ %reldir%/terminal-dock-widget.h \ %reldir%/color-picker.h \ @@ -200,6 +202,7 @@ %reldir%/resource-manager.cc \ %reldir%/settings-dialog.cc \ %reldir%/shortcut-manager.cc \ + %reldir%/tab-bar.cc \ %reldir%/thread-manager.cc \ %reldir%/terminal-dock-widget.cc \ %reldir%/color-picker.cc \
--- a/libgui/src/qtinfo/webinfo.cc Fri Feb 02 14:16:30 2018 -0800 +++ b/libgui/src/qtinfo/webinfo.cc Sat Feb 03 09:51:34 2018 +0100 @@ -55,7 +55,7 @@ hbox_layout->setSpacing (0); vbox_layout->addLayout (hbox_layout); - _tab_bar = new webinfo_tab_bar (this); + _tab_bar = new tab_bar (this); _tab_bar->setSizePolicy (QSizePolicy::Preferred,QSizePolicy::Preferred); _tab_bar->setExpanding (false); _tab_bar->setTabsClosable (true); @@ -97,8 +97,8 @@ _close_action->setEnabled (false); _close_others_action->setEnabled (false); - connect (_tab_bar, SIGNAL (tabCloseRequested (int)), this, - SLOT (close_tab (int))); + connect (_tab_bar, SIGNAL (close_current_tab_signal (bool)), + this, SLOT (request_close_tab (bool))); connect (_tab_bar, SIGNAL (currentChanged (int)), this, SLOT (current_tab_changed (int))); connect (_zoom_in_button, SIGNAL (clicked ()), this, SLOT (zoom_in ())); @@ -377,95 +377,3 @@ shortcut_manager::set_shortcut (_close_others_action, "editor_file:close_other"); } -// -// Functions of the the reimplemented tab bar -// - -webinfo_tab_bar::webinfo_tab_bar (QWidget *p) : QTabBar (p) -{ - // prepare the context menu of the tab bar - _context_menu = new QMenu (this); -} - -webinfo_tab_bar::~webinfo_tab_bar () -{ - delete _context_menu; -} - -// Reimplement mouse event for filtering out the desired mouse clicks -void -webinfo_tab_bar::mousePressEvent (QMouseEvent *me) -{ - QPoint click_pos; - int clicked_idx = -1; - - // detect the tab where the click occured - for (int i = 0; i < count (); i++) - { - click_pos = mapToGlobal (me->pos ()); - if (tabRect (i).contains (mapFromGlobal (click_pos))) - { - clicked_idx = i; - break; - } - } - - // If a tab was clicked - if (clicked_idx >= 0) - { - int current_idx = currentIndex (); - // detect the mouse click - if ((me->type () == QEvent::MouseButtonDblClick && - me->button() == Qt::LeftButton) || - (me->type () != QEvent::MouseButtonDblClick && - me->button() == Qt::MidButton)) - { - // Middle click or double click -> close the tab - // Make the clicked tab the current one and close it - emit tabCloseRequested (clicked_idx); - // Was the closed tab before or after the previously current tab? - // According to the result, use previous index or reduce it by one - if (current_idx - clicked_idx > 0) - setCurrentIndex (current_idx - 1); - else if (current_idx - clicked_idx < 0) - setCurrentIndex (current_idx); - } - else if (me->type () != QEvent::MouseButtonDblClick && - me->button() == Qt::RightButton) - { - setCurrentIndex (clicked_idx); - if (! _context_menu->exec (click_pos)) - { - // No action selected, back to previous tab - setCurrentIndex (current_idx); - } - else - { - // 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 - // if other or all files were closed. - int new_idx = count () - 1; - if (new_idx > 0) - { - if (current_idx - clicked_idx > 0) - new_idx = current_idx - 1; - else if (current_idx - clicked_idx < 0) - new_idx = current_idx; - } - if (new_idx >= 0) - setCurrentIndex (new_idx); - } - } - else - { - // regular handling of the mouse event - QTabBar::mousePressEvent (me); - } - } - else - { - // regular handling of the mouse event - QTabBar::mousePressEvent (me); - } -}
--- a/libgui/src/qtinfo/webinfo.h Fri Feb 02 14:16:30 2018 -0800 +++ b/libgui/src/qtinfo/webinfo.h Sat Feb 03 09:51:34 2018 +0100 @@ -40,29 +40,7 @@ #include <QMouseEvent> #include <QSettings> -// subclassed QTabWidget for usable tab-bar and reimplemented mouse event -class webinfo_tab_bar : public QTabBar -{ - Q_OBJECT - -public: - - webinfo_tab_bar (QWidget *p); - ~webinfo_tab_bar (); - QMenu * get_context_menu () { return _context_menu; } - -public slots: - -protected: - - void mousePressEvent(QMouseEvent *event); - -private: - - QMenu *_context_menu; - -}; - +#include "tab-bar.h" // The webinfo class class webinfo : public QWidget @@ -103,7 +81,7 @@ void tab_state_changed (void); QTextBrowser *_text_browser; - webinfo_tab_bar *_tab_bar; + tab_bar *_tab_bar; QStackedWidget *_stacked_widget; QLineEdit *_search_line_edit; QCheckBox *_search_check_box;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libgui/src/tab-bar.cc Sat Feb 03 09:51:34 2018 +0100 @@ -0,0 +1,116 @@ +/* + +Copyright (C) 2018 Torsten <mttl@mailbox.org> + +This file is part of Octave. + +Octave is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Octave is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, see +<https://www.gnu.org/licenses/>. + +*/ + +// This file implements a tab bar derived from QTabBar with a contextmenu +// and possibility to close a tab via double-left and middle mouse click. + +#include "tab-bar.h" + + +tab_bar::tab_bar (QWidget *p) + : QTabBar (p), m_context_menu (new QMenu (this)) +{ } + +tab_bar::~tab_bar (void) +{ + delete m_context_menu; +} + +// Reimplement mouse event for filtering out the desired mouse clicks +void +tab_bar::mousePressEvent (QMouseEvent *me) +{ + QPoint click_pos; + int clicked_idx = -1; + + // detect the tab where the click occured + for (int i = 0; i < count (); i++) + { + click_pos = mapToGlobal (me->pos ()); + if (tabRect (i).contains (mapFromGlobal (click_pos))) + { + clicked_idx = i; + break; + } + } + + // If a tab was clicked + if (clicked_idx >= 0) + { + int current_idx = currentIndex (); + // detect the mouse click + if ((me->type () == QEvent::MouseButtonDblClick && + me->button() == Qt::LeftButton) || + (me->type () != QEvent::MouseButtonDblClick && + me->button() == Qt::MidButton)) + { + // Middle click or double click -> close the tab + // Make the clicked tab the current one and close it + setCurrentIndex (clicked_idx); + emit close_current_tab_signal (true); + // Was the closed tab before or after the previously current tab? + // According to the result, use previous index or reduce it by one + if (current_idx - clicked_idx > 0) + setCurrentIndex (current_idx - 1); + else if (current_idx - clicked_idx < 0) + setCurrentIndex (current_idx); + } + else if (me->type () != QEvent::MouseButtonDblClick && + me->button() == Qt::RightButton) + { + // Right click, show context menu + setCurrentIndex (clicked_idx); + if (! m_context_menu->exec (click_pos)) + { + // No action selected, back to previous tab + setCurrentIndex (current_idx); + } + else + { + // 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 + // if other or all files were closed. + int new_idx = count () - 1; + if (new_idx > 0) + { + if (current_idx - clicked_idx > 0) + new_idx = current_idx - 1; + else if (current_idx - clicked_idx < 0) + new_idx = current_idx; + } + if (new_idx >= 0) + setCurrentIndex (new_idx); + } + } + else + { + // regular handling of the mouse event + QTabBar::mousePressEvent (me); + } + } + else + { + // regular handling of the mouse event + QTabBar::mousePressEvent (me); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libgui/src/tab-bar.h Sat Feb 03 09:51:34 2018 +0100 @@ -0,0 +1,60 @@ +/* + +Copyright (C) 2018 Torsten <mttl@mailbox.org> + +This file is part of Octave. + +Octave is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Octave is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, see +<https://www.gnu.org/licenses/>. + +*/ + +// This file implements a tab bar derived from QTabBar with a contextmenu +// and possibility to close a tab via double-left and middle mouse click. + +#if ! defined (octave_tab_bar_h) +#define octave_tab_bar_h 1 + +#include <QMenu> +#include <QTabBar> +#include <QMouseEvent> + +// Subclassed QTabBar for usable tab-bar and reimplemented mouse event + +class tab_bar : public QTabBar +{ + Q_OBJECT + +public: + + tab_bar (QWidget *p); + + ~tab_bar (void); + + QMenu *get_context_menu (void) { return m_context_menu; }; + +signals: + + void close_current_tab_signal (bool); + +protected: + + void mousePressEvent(QMouseEvent *event); + +private: + + QMenu *m_context_menu; +}; + +#endif