GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
dw-main-window.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2013-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 /* This is the main window derived from QMainWindow for being used
27  as the main window in dock widgets like the variable editor or
28  the file editor
29 */
30 
31 #if defined (HAVE_CONFIG_H)
32 # include "config.h"
33 #endif
34 
35 #include <QDockWidget>
36 #include <QMenu>
37 
38 #include "dw-main-window.h"
39 #include "octave-qobject.h"
40 #include "shortcut-manager.h"
41 #include "gui-preferences-sc.h"
42 
43 namespace octave
44 {
45 
47  : QMainWindow (p), m_octave_qobj (oct_qobj)
48  {
50 
51  // Adding the actions for closing the dock widgets
53  = add_action (nullptr, rmgr.icon ("window-close", false),
54  tr ("&Close"), SLOT (request_close ()), this);
55 
57  = add_action (nullptr, rmgr.icon ("window-close", false),
58  tr ("Close &All"), SLOT (request_close_all ()), this);
59 
61  = add_action (nullptr, rmgr.icon ("window-close", false),
62  tr ("Close &Other"), SLOT (request_close_other ()), this);
63 
65  = add_action (nullptr, QIcon (), tr ("Switch to &Left Widget"),
66  SLOT (request_switch_left ()), this);
67 
69  = add_action (nullptr, QIcon (), tr ("Switch to &Right Widget"),
70  SLOT (request_switch_right ()), this);
71 
72  // The list of actions for floating widgets
78 
79  notice_settings (rmgr.get_settings ());
80  }
81 
82 
83  // Re-implementing the popup menu of the main window
85  {
86  QList<QAction *> new_actions = QList<QAction *> ();
87  new_actions.append (m_close_action);
88  new_actions.append (m_close_others_action);
89  new_actions.append (m_close_all_action);
90 
91  QMenu *menu = QMainWindow::createPopupMenu ();
92  QList<QAction *> actions = menu->actions();
93 
94  if (actions.length () > 0)
95  {
96  QAction *sep = menu->insertSeparator (actions.at (0));
97  menu->insertActions (sep, new_actions);
98  }
99  else
100  menu->addActions (new_actions);
101 
102  return menu;
103  }
104 
105 
106  // Adding an action to the main window
107  QAction * dw_main_window::add_action (QMenu *menu, const QIcon& icon,
108  const QString& text, const char *member,
109  QWidget *receiver)
110  {
111  QAction *a;
112  QWidget *r = this;
113 
114  if (receiver != nullptr)
115  r = receiver;
116 
117  if (menu)
118  a = menu->addAction (icon, text, r, member);
119  else
120  {
121  a = new QAction (icon, text, this);
122  a->setEnabled (true);
123  connect (a, SIGNAL (triggered ()), r, member);
124  }
125 
126  addAction (a); // important for shortcut context
127  a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
128 
129  return a;
130  }
131 
132  // Update the settings
134  {
136 
140 
143  }
144 
145 
146  // Slots for handling actions
147 
148  // Close current widget
150  {
151  for (int i = 0; i < m_dw_list.length (); i++)
152  {
153  if (m_dw_list.at (i)->hasFocus ())
154  {
155  m_dw_list.at (i)->close ();
156  if (i > 0)
157  m_dw_list.at (i-1)->setFocus ();
158  break;
159  }
160  }
161  }
162 
163  // Close other widgets
165  {
166  for (int i = m_dw_list.length () - 1; i >= 0; i--)
167  {
168  if (! m_dw_list.at (i)->hasFocus ())
169  m_dw_list.at (i)->close ();
170  }
171  }
172 
173  // Close all widgets
175  {
176  for (int i = m_dw_list.length () - 1; i >= 0; i--)
177  m_dw_list.at (i)->close ();
178  }
179 
180  // Switch to left widget
182  {
183  request_switch (-1);
184  }
185 
186  // Switch to right widget
188  {
189  request_switch (1);
190  }
191 
192  // Switch to left/right widget
193  void dw_main_window::request_switch (int direction)
194  {
195  int active = -1, next;
196 
197  for (int i = m_dw_list.length () - 1; i >= 0; i--)
198  {
199  if (m_dw_list.at (i)->hasFocus ())
200  {
201  active = i;
202  break;
203  }
204  }
205 
206  if (active == -1)
207  return;
208 
209  if (direction == -1 && active == 0)
210  next = m_dw_list.length () - 1;
211  else if (direction == 1 && active == m_dw_list.length () - 1)
212  next = 0;
213  else
214  next = active + direction;
215 
216  m_dw_list.at (next)->raise ();
217  m_dw_list.at (next)->activateWindow ();
218  m_dw_list.at (next)->setFocus ();
219  }
220 
221 
222  // Reimplemented Event
223  bool dw_main_window::event (QEvent *ev)
224  {
225  if (ev->type () == QEvent::ChildAdded
226  || ev->type () == QEvent::ChildRemoved)
227  {
228  // Adding or Removing a child indicates that a dock widget was
229  // created or removed.
230  // In all cases, the list of dock widgets has to be updated.
231  m_dw_list = findChildren<QDockWidget *>();
232  }
233 
234  if (ev->type () == QEvent::StyleChange)
235  {
236  // This might indicate un- or re-docking a widget: Make sure
237  // floating widgets get a copy of our actions
238  for (int i = m_dw_list.length () - 1; i >= 0; i--)
239  {
240  // First remove possibly existing actions
241  for (int j = m_actions_list.length () - 1; j >0; j--)
242  m_dw_list.at (i)->removeAction (m_actions_list.at (j));
243 
244  // Then add our actions for floating widgets
245  if (m_dw_list.at (i)->isFloating ())
246  m_dw_list.at (i)->addActions (m_actions_list);
247  }
248  }
249 
250  return QMainWindow::event (ev);
251  }
252 
253 }
254 
Base class for Octave interfaces that use Qt.
resource_manager & get_resource_manager(void)
shortcut_manager & get_shortcut_manager(void)
QAction * m_switch_left_action
QList< QAction * > m_actions_list
base_qobject & m_octave_qobj
QAction * add_action(QMenu *menu, const QIcon &icon, const QString &text, const char *member, QWidget *receiver)
void request_switch(int direction)
virtual QMenu * createPopupMenu()
QAction * m_close_others_action
dw_main_window(base_qobject &oct_qboj, QWidget *parent=nullptr)
QList< QDockWidget * > m_dw_list
void notice_settings(const gui_settings *)
virtual bool event(QEvent *ev)
QAction * m_switch_right_action
gui_settings * get_settings(void) const
QIcon icon(const QString &icon_name, bool fallback=true)
void set_shortcut(QAction *action, const sc_pref &scpref)
const sc_pref sc_edit_file_close(sc_edit_file_cl, QKeySequence::Close)
const sc_pref sc_edit_file_close_other(sc_edit_file_cl+"_other", QKeySequence::UnknownKey)
const sc_pref sc_edit_file_close_all(sc_edit_file_cl+"_all", QKeySequence::UnknownKey)
const sc_pref sc_edit_tabs_switch_right_tab(sc_edit_tabs+":switch_right_tab", CTRL+Qt::Key_PageDown)
const sc_pref sc_edit_tabs_switch_left_tab(sc_edit_tabs+":switch_left_tab", CTRL+Qt::Key_PageUp)
T * r
Definition: mx-inlines.cc:773
static uint32_t * next
Definition: randmtzig.cc:189