GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
tab-bar.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2018 Torsten <mttl@mailbox.org>
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 // This file implements a tab bar derived from QTabBar with a contextmenu
24 // and possibility to close a tab via double-left and middle mouse click.
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "tab-bar.h"
31 
32 namespace octave
33 {
35  : QTabBar (p), m_context_menu (new QMenu (this))
36  { }
37 
39  {
40  delete m_context_menu;
41  }
42 
43  // slots for tab navigation
45  {
46  switch_tab (-1);
47  }
48 
50  {
51  switch_tab (1);
52  }
53 
55  {
56 #if defined (HAVE_QTABWIDGET_SETMOVABLE)
57  switch_tab (-1, true);
58 #endif
59  }
60 
62  {
63 #if defined (HAVE_QTABWIDGET_SETMOVABLE)
64  switch_tab (1, true);
65 #endif
66  }
67 
68  void tab_bar::switch_tab (int direction, bool movetab)
69  {
70  int tabs = count ();
71 
72  if (tabs < 2)
73  return;
74 
75  int old_pos = currentIndex ();
76  int new_pos = currentIndex () + direction;
77 
78  if (new_pos < 0 || new_pos >= tabs)
79  new_pos = new_pos - direction*tabs;
80 
81  if (movetab)
82  {
83 #if defined (HAVE_QTABWIDGET_SETMOVABLE)
84  moveTab (old_pos, new_pos);
85  setCurrentIndex (old_pos);
86  setCurrentIndex (new_pos);
87 #endif
88  }
89  else
90  setCurrentIndex (new_pos);
91  }
92 
93  // Reimplement mouse event for filtering out the desired mouse clicks
94  void tab_bar::mousePressEvent (QMouseEvent *me)
95  {
96  QPoint click_pos;
97  int clicked_idx = -1;
98 
99  // detect the tab where the click occured
100  for (int i = 0; i < count (); i++)
101  {
102  click_pos = mapToGlobal (me->pos ());
103  if (tabRect (i).contains (mapFromGlobal (click_pos)))
104  {
105  clicked_idx = i;
106  break;
107  }
108  }
109 
110  // If a tab was clicked
111  if (clicked_idx >= 0)
112  {
113  int current_idx = currentIndex ();
114  // detect the mouse click
115  if ((me->type () == QEvent::MouseButtonDblClick &&
116  me->button() == Qt::LeftButton) ||
117  (me->type () != QEvent::MouseButtonDblClick &&
118  me->button() == Qt::MidButton))
119  {
120  // Middle click or double click -> close the tab
121  // Make the clicked tab the current one and close it
122  setCurrentIndex (clicked_idx);
123  emit close_current_tab_signal (true);
124  // Was the closed tab before or after the previously current tab?
125  // According to the result, use previous index or reduce it by one
126  if (current_idx - clicked_idx > 0)
127  setCurrentIndex (current_idx - 1);
128  else if (current_idx - clicked_idx < 0)
129  setCurrentIndex (current_idx);
130  }
131  else if (me->type () != QEvent::MouseButtonDblClick &&
132  me->button() == Qt::RightButton)
133  {
134  // Right click, show context menu
135  setCurrentIndex (clicked_idx);
136  if (! m_context_menu->exec (click_pos))
137  {
138  // No action selected, back to previous tab
139  setCurrentIndex (current_idx);
140  }
141  else
142  {
143  // Was the possibly only closed tab before or after the
144  // previously current tab? According to the result, use previous
145  // index or reduce it by one. Also prevent using a too large
146  // if other or all files were closed.
147  int new_idx = count () - 1;
148  if (new_idx > 0)
149  {
150  if (current_idx - clicked_idx > 0)
151  new_idx = current_idx - 1;
152  else if (current_idx - clicked_idx < 0)
153  new_idx = current_idx;
154  }
155  if (new_idx >= 0)
156  setCurrentIndex (new_idx);
157  }
158  }
159  else
160  {
161  // regular handling of the mouse event
162  QTabBar::mousePressEvent (me);
163  }
164  }
165  else
166  {
167  // regular handling of the mouse event
168  QTabBar::mousePressEvent (me);
169  }
170  }
171 }
void move_tab_left(void)
Definition: tab-bar.cc:54
void switch_left_tab(void)
Definition: tab-bar.cc:44
tab_bar(QWidget *p)
Definition: tab-bar.cc:34
QMenu * m_context_menu
Definition: tab-bar.h:66
void close_current_tab_signal(bool)
void move_tab_right(void)
Definition: tab-bar.cc:61
returns the type of the matrix and caches it for future use Called with more than one the function will not attempt to guess the type if it is still unknown This is useful for debugging purposes The possible matrix types depend on whether the matrix is full or and can be one of the following able sis tem and mark type as unknown tem as the structure of the matrix explicitly gives this(Sparse matrices only) tem code
Definition: matrix_type.cc:120
void switch_right_tab(void)
Definition: tab-bar.cc:49
void switch_tab(int direction, bool movetab=false)
Definition: tab-bar.cc:68
void mousePressEvent(QMouseEvent *event)
Definition: tab-bar.cc:94
p
Definition: lu.cc:138
~tab_bar(void)
Definition: tab-bar.cc:38
for i
Definition: data.cc:5264