GNU Octave  8.1.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-2023 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 
44 
46 : QMainWindow (p), m_octave_qobj (oct_qobj)
47 {
48  resource_manager& rmgr = m_octave_qobj.get_resource_manager ();
49 
50  // Adding the actions for closing the dock widgets
51  m_close_action
52  = add_action (nullptr, rmgr.icon ("window-close", false),
53  tr ("&Close"), SLOT (request_close ()), this);
54 
55  m_close_all_action
56  = add_action (nullptr, rmgr.icon ("window-close", false),
57  tr ("Close &All"), SLOT (request_close_all ()), this);
58 
59  m_close_others_action
60  = add_action (nullptr, rmgr.icon ("window-close", false),
61  tr ("Close &Other"), SLOT (request_close_other ()), this);
62 
63  m_switch_left_action
64  = add_action (nullptr, QIcon (), tr ("Switch to &Left Widget"),
65  SLOT (request_switch_left ()), this);
66 
67  m_switch_right_action
68  = add_action (nullptr, QIcon (), tr ("Switch to &Right Widget"),
69  SLOT (request_switch_right ()), this);
70 
71  // The list of actions for floating widgets
72  m_actions_list << m_close_action;
73  m_actions_list << m_close_others_action;
74  m_actions_list << m_close_all_action;
75  m_actions_list << m_switch_left_action;
76  m_actions_list << m_switch_right_action;
77 
78  notice_settings (rmgr.get_settings ());
79 }
80 
81 // Re-implementing the popup menu of the main window
83 {
84  QList<QAction *> new_actions = QList<QAction *> ();
85  new_actions.append (m_close_action);
86  new_actions.append (m_close_others_action);
87  new_actions.append (m_close_all_action);
88 
89  QMenu *menu = QMainWindow::createPopupMenu ();
90  QList<QAction *> actions = menu->actions();
91 
92  if (actions.length () > 0)
93  {
94  QAction *sep = menu->insertSeparator (actions.at (0));
95  menu->insertActions (sep, new_actions);
96  }
97  else
98  menu->addActions (new_actions);
99 
100  return menu;
101 }
102 
103 // Adding an action to the main window
104 QAction * dw_main_window::add_action (QMenu *menu, const QIcon& icon,
105  const QString& text, const char *member,
106  QWidget *receiver)
107 {
108  QAction *a;
109  QWidget *r = this;
110 
111  if (receiver != nullptr)
112  r = receiver;
113 
114  if (menu)
115  a = menu->addAction (icon, text, r, member);
116  else
117  {
118  a = new QAction (icon, text, this);
119  a->setEnabled (true);
120  connect (a, SIGNAL (triggered ()), r, member);
121  }
122 
123  addAction (a); // important for shortcut context
124  a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
125 
126  return a;
127 }
128 
129 // Update the settings
131 {
133 
137 
140 }
141 
142 // Slots for handling actions
143 
144 // Close current widget
146 {
147  for (int i = 0; i < m_dw_list.length (); i++)
148  {
149  if (m_dw_list.at (i)->hasFocus ())
150  {
151  m_dw_list.at (i)->close ();
152  if (i > 0)
153  m_dw_list.at (i-1)->setFocus ();
154  break;
155  }
156  }
157 }
158 
159 // Close other widgets
161 {
162  for (int i = m_dw_list.length () - 1; i >= 0; i--)
163  {
164  if (! m_dw_list.at (i)->hasFocus ())
165  m_dw_list.at (i)->close ();
166  }
167 }
168 
169 // Close all widgets
171 {
172  for (int i = m_dw_list.length () - 1; i >= 0; i--)
173  m_dw_list.at (i)->close ();
174 }
175 
176 // Switch to left widget
178 {
179  request_switch (-1);
180 }
181 
182 // Switch to right widget
184 {
185  request_switch (1);
186 }
187 
188 // Switch to left/right widget
189 void dw_main_window::request_switch (int direction)
190 {
191  int active = -1, next;
192 
193  for (int i = m_dw_list.length () - 1; i >= 0; i--)
194  {
195  if (m_dw_list.at (i)->hasFocus ())
196  {
197  active = i;
198  break;
199  }
200  }
201 
202  if (active == -1)
203  return;
204 
205  if (direction == -1 && active == 0)
206  next = m_dw_list.length () - 1;
207  else if (direction == 1 && active == m_dw_list.length () - 1)
208  next = 0;
209  else
210  next = active + direction;
211 
212  m_dw_list.at (next)->raise ();
213  m_dw_list.at (next)->activateWindow ();
214  m_dw_list.at (next)->setFocus ();
215 }
216 
217 // Reimplemented Event
218 bool dw_main_window::event (QEvent *ev)
219 {
220  if (ev->type () == QEvent::ChildAdded
221  || ev->type () == QEvent::ChildRemoved)
222  {
223  // Adding or Removing a child indicates that a dock widget was
224  // created or removed.
225  // In all cases, the list of dock widgets has to be updated.
226  m_dw_list = findChildren<QDockWidget *>();
227  }
228 
229  if (ev->type () == QEvent::StyleChange)
230  {
231  // This might indicate un- or re-docking a widget: Make sure
232  // floating widgets get a copy of our actions
233  for (int i = m_dw_list.length () - 1; i >= 0; i--)
234  {
235  // First remove possibly existing actions
236  for (int j = m_actions_list.length () - 1; j >0; j--)
237  m_dw_list.at (i)->removeAction (m_actions_list.at (j));
238 
239  // Then add our actions for floating widgets
240  if (m_dw_list.at (i)->isFloating ())
241  m_dw_list.at (i)->addActions (m_actions_list);
242  }
243  }
244 
245  return QMainWindow::event (ev);
246 }
247 
249 
OCTAVE_END_NAMESPACE(octave)
Base class for Octave interfaces that use Qt.
shortcut_manager & get_shortcut_manager(void)
virtual QMenu * createPopupMenu()
void request_switch_left()
QAction * m_close_others_action
QAction * add_action(QMenu *menu, const QIcon &icon, const QString &text, const char *member, QWidget *receiver)
QAction * m_switch_left_action
QList< QDockWidget * > m_dw_list
QAction * m_close_action
QAction * m_close_all_action
void notice_settings(const gui_settings *)
void request_close_all()
base_qobject & m_octave_qobj
void request_switch_right()
QAction * m_switch_right_action
void request_switch(int direction)
virtual bool event(QEvent *ev)
QList< QAction * > m_actions_list
void request_close_other()
gui_settings * get_settings(void) const
QIcon icon(const QString &icon_name, bool octave_only=false, const QString &icon_alt_name=QString())
void set_shortcut(QAction *action, const sc_pref &scpref, bool enable=true)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
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:192