GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
octave-dock-widget.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2012-2013 Richard Crozier
4 Copyright (C) 2013 Torsten <[email protected]>
5 
6 This file is part of Octave.
7 
8 Octave is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <QApplication>
29 #include <QToolBar>
30 #include <QToolButton>
31 #include <QAction>
32 #include <QHBoxLayout>
33 #include <QLabel>
34 #include <QSettings>
35 
36 #include "resource-manager.h"
37 #include "octave-dock-widget.h"
38 
39 
41  : QDockWidget (p)
42 {
43 
44  _parent = static_cast<QMainWindow *> (p); // store main window
45  _floating = false;
46 
47  connect (this, SIGNAL (visibilityChanged (bool)),
48  this, SLOT (handle_visibility_changed (bool)));
49 
50  connect (p, SIGNAL (settings_changed (const QSettings*)),
51  this, SLOT (notice_settings (const QSettings*)));
52 
53 #if defined (Q_OS_WIN32)
54  // windows: add an extra title bar that persists when floating
55 
56  setFeatures (QDockWidget::DockWidgetMovable); // not floatable or closeable
57 
58  // the custom (extra) title bar of the widget
59  _dock_action = new QAction
60  (QIcon (":/actions/icons/widget-undock.png"), "", this);
61  _dock_action-> setToolTip (tr ("Undock widget"));
62  connect (_dock_action, SIGNAL (triggered (bool)),
63  this, SLOT (change_floating (bool)));
64  QToolButton *dock_button = new QToolButton (this);
65  dock_button->setDefaultAction (_dock_action);
66  dock_button->setFocusPolicy (Qt::NoFocus);
67  dock_button->setIconSize (QSize (12,12));
68 
69  QAction *close_action = new QAction
70  (QIcon (":/actions/icons/widget-close.png"), "", this );
71  close_action-> setToolTip (tr ("Hide widget"));
72  connect (close_action, SIGNAL (triggered (bool)),
73  this, SLOT (change_visibility (bool)));
74  QToolButton *close_button = new QToolButton (this);
75  close_button->setDefaultAction (close_action);
76  close_button->setFocusPolicy (Qt::NoFocus);
77  close_button->setIconSize (QSize (12,12));
78 
79  QHBoxLayout *h_layout = new QHBoxLayout ();
80  h_layout->addStretch (100);
81  h_layout->addWidget (dock_button);
82  h_layout->addWidget (close_button);
83  h_layout->setSpacing (0);
84  h_layout->setContentsMargins (6,0,0,0);
85 
86  QWidget *title_widget = new QWidget ();
87  title_widget->setLayout (h_layout);
88  setTitleBarWidget (title_widget);
89 
90 #else
91 
92  // non windows: qt takes control of floating widgets
93  setFeatures (QDockWidget::DockWidgetMovable |
94  QDockWidget::DockWidgetClosable |
95  QDockWidget::DockWidgetFloatable); // floatable and closeable
96 
97  connect (this, SIGNAL (topLevelChanged (bool)),
98  this, SLOT (change_floating (bool)));
99 
100 #endif
101 
102  // copy & paste handling
103  connect (p, SIGNAL (copyClipboard_signal ()),
104  this, SLOT (copyClipboard ()));
105  connect (p, SIGNAL (pasteClipboard_signal ()),
106  this, SLOT (pasteClipboard ()));
107 }
108 
110 {
111  // save state of this dock-widget
112  QString name = objectName ();
113  QSettings *settings = resource_manager::get_settings ();
114 
115  settings->beginGroup ("DockWidgets");
116 
117 #if defined (Q_OS_WIN32)
118  if (_floating) // widget is floating (windows), save actual floating geometry
119  settings->setValue (name+"_floating_geometry", geometry ());
120  else // not floating save docked (normal) geometry
121 #endif
122  settings->setValue (name, saveGeometry ());
123 
124  settings->setValue (name+"Visible", isVisible ()); // store visibility
125  settings->setValue (name+"Floating", _floating); // store visibility
126  settings->setValue (name+"_minimized", isMinimized ()); // store minimized
127 
128  settings->endGroup ();
129  settings->sync ();
130 }
131 
132 // connect signal visibility changed to related slot (called from main-window)
133 void
135 {
136  connect (this, SIGNAL (visibilityChanged (bool)),
137  this, SLOT (handle_visibility (bool)));
138  emit active_changed (isVisible ()); // emit once for init of window menu
139 }
140 
141 
142 // set the title in the dockwidgets title bar
143 void
144 octave_dock_widget::set_title (const QString& title)
145 {
146 #if defined (Q_OS_WIN32)
147  QHBoxLayout* h_layout =
148  static_cast<QHBoxLayout *> (titleBarWidget ()->layout ());
149  QLabel *label = new QLabel (title);
150  h_layout->insertWidget (0,label);
151 #endif
152  setWindowTitle (title);
153 }
154 
155 // make the widget floating
156 void
158 {
159 #if defined (Q_OS_WIN32)
160 
161  // windows: the widget has to be reparented (parent = 0)
162 
163  QSettings *settings = resource_manager::get_settings ();
164 
165  // save the docking area and geometry for later redocking
166  // FIXME: dockWidgetArea always returns 2
167  settings->setValue ("DockWidgets/" + objectName () + "_dock_area",
168  _parent->dockWidgetArea (this));
169  settings->setValue ("DockWidgets/" + objectName (), saveGeometry ());
170  settings->sync ();
171 
172  // remove parent and adjust the (un)dock icon
173  setParent (0, Qt::Window);
174  _dock_action->setIcon (QIcon (":/actions/icons/widget-dock.png"));
175  _dock_action->setToolTip (tr ("Dock widget"));
176 
177  // restore the last geometry( when floating
178  setGeometry (settings->value ("DockWidgets/" + objectName ()
179  + "_floating_geometry",QRect(50,100,480,480)).toRect ());
180 
181 #else
182 
183  // non windows: Just set the appripriate window flag
184  setWindowFlags (Qt::Window);
185 
186 #endif
187 
188  _floating = true;
189 }
190 
191 // dock the widget
192 void
194 {
195 #if defined (Q_OS_WIN32)
196 
197  // windows: Since floating widget has no parent, we have to readd it
198 
199  QSettings *settings = resource_manager::get_settings ();
200 
201  // save last floating geometry if widget really was floating
202  if (_floating)
203  settings->setValue ("DockWidgets/" + objectName () + "_floating_geometry",
204  geometry ());
205  settings->sync ();
206 
207  if (dock)
208  {
209  // add widget to last saved docking area (dock=true is default)
210  int area = settings->value ("DockWidgets/" + objectName () + "_dock_area",
211  Qt::TopDockWidgetArea).toInt ();
212  _parent->addDockWidget (static_cast<Qt::DockWidgetArea> (area), this);
213 
214  // FIXME: restoreGeometry is ignored for docked widgets
215  // and its child widget
216  restoreGeometry (settings->value
217  ("DockWidgets/" + objectName ()).toByteArray ());
218  }
219  else // only reparent, no docking
220  setParent (_parent);
221 
222  // adjust the (un)dock icon
223  _dock_action->setIcon (QIcon (":/actions/icons/widget-undock.png"));
224  _dock_action->setToolTip (tr ("Undock widget"));
225 
226 #else
227 
228  // non windows: just say we are a docked widget again
229  setWindowFlags (Qt::Widget);
230 
231 #endif
232 
233  _floating = false;
234 }
235 
236 // slot for (un)dock action
237 void
239 {
240  if (_floating)
241  make_widget ();
242  else
243  {
244  make_window ();
245  focus ();
246  }
247 }
248 
249 // slot for hiding the widget
250 void
252 {
253  setVisible (false);
254  emit active_changed (false);
255 }
256 
257 // get focus widget
258 QWidget *
260 {
262  if (w && w->focusProxy ()) w = w->focusProxy ();
263  return w;
264 }