GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ToolBar.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2011-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 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <QAction>
31 #include <QActionEvent>
32 #include <QApplication>
33 #include <QEvent>
34 #include <QIcon>
35 #include <QMainWindow>
36 #include <QPixmap>
37 #include <QTimer>
38 #include <QToolBar>
39 
40 #include "Figure.h"
41 #include "ToolBar.h"
42 #include "QtHandlesUtils.h"
43 
44 #include "gui-preferences-global.h"
45 #include "octave-qobject.h"
46 
48 
49 static QIcon makeEmptyIcon (void)
50 {
51  QPixmap pix (16, 16);
52 
53  pix.fill (Qt::transparent);
54 
55  return QIcon (pix);
56 }
57 
58 static QAction *
59 addEmptyAction (QToolBar *bar)
60 {
61  static const QIcon empty_icon = makeEmptyIcon ();
62 
63  QAction *a = bar->addAction (empty_icon, "Empty Toolbar");
64 
65  a->setEnabled (false);
66  a->setToolTip ("");
67 
68  return a;
69 }
70 
71 ToolBar *
72 ToolBar::create (octave::base_qobject& oct_qobj, octave::interpreter& interp,
73  const graphics_object& go)
74 {
75  Object *parent = parentObject (interp, go);
76 
77  if (parent)
78  {
79  QWidget *parentWidget = parent->qWidget<QWidget> ();
80 
81  if (parentWidget)
82  return new ToolBar (oct_qobj, interp, go,
83  new QToolBar (parentWidget));
84  }
85 
86  return nullptr;
87 }
88 
89 ToolBar::ToolBar (octave::base_qobject& oct_qobj, octave::interpreter& interp,
90  const graphics_object& go, QToolBar *bar)
91  : Object (oct_qobj, interp, go, bar), m_empty (nullptr), m_figure (nullptr)
92 {
93  uitoolbar::properties& tp = properties<uitoolbar> ();
94 
95  bar->setFloatable (false);
96  bar->setMovable (false);
97  bar->setVisible (tp.is_visible ());
98  bar->setStyleSheet (bar->styleSheet () + global_toolbar_style);
99 
100  m_empty = addEmptyAction (bar);
101 
102  m_figure =
103  dynamic_cast<Figure *> (Object::fromQObject (bar->parentWidget ()));
104 
105  if (m_figure)
106  m_figure->addCustomToolBar (bar, tp.is_visible (),
107  tp.get_tag () == "__default_toolbar__");
108 
109  bar->installEventFilter (this);
110 }
111 
113 { }
114 
115 void
117 {
118  uitoolbar::properties& tp = properties<uitoolbar> ();
119  QToolBar *bar = qWidget<QToolBar> ();
120 
121  switch (pId)
122  {
123  case base_properties::ID_VISIBLE:
124  if (m_figure)
125  m_figure->showCustomToolBar (bar, tp.is_visible ());
126  break;
127 
128  default:
129  Object::update (pId);
130  break;
131  }
132 }
133 
134 bool
135 ToolBar::eventFilter (QObject *watched, QEvent *xevent)
136 {
137  if (watched == qObject ())
138  {
139  switch (xevent->type ())
140  {
141  case QEvent::ActionAdded:
142  case QEvent::ActionRemoved:
143  {
144  QActionEvent *ae = dynamic_cast<QActionEvent *> (xevent);
145  QToolBar *bar = qWidget<QToolBar> ();
146 
147  if (ae->action () != m_empty)
148  {
149  if (xevent->type () == QEvent::ActionAdded)
150  {
151  if (bar->actions ().size () == 2)
152  QTimer::singleShot (0, this, &ToolBar::hideEmpty);
153  }
154  else
155  {
156  if (bar->actions ().size () == 1)
157  m_empty->setVisible (true);
158  }
159  }
160  }
161  break;
162 
163  default:
164  break;
165  }
166  }
167 
168  return false;
169 }
170 
171 void
173 {
174  m_empty->setVisible (false);
175 }
176 
177 void
179 {
180  if (m_figure)
181  {
182  QToolBar *bar = qWidget<QToolBar> ();
183 
184  if (bar)
185  m_figure->showCustomToolBar (bar, false);
186  }
187 }
188 
OCTAVE_END_NAMESPACE(octave)
static QAction * addEmptyAction(QToolBar *bar)
Definition: ToolBar.cc:59
static QIcon makeEmptyIcon(void)
Definition: ToolBar.cc:49
Definition: Figure.h:65
void addCustomToolBar(QToolBar *bar, bool visible, bool isdefault)
Definition: Figure.cc:798
void showCustomToolBar(QToolBar *bar, bool visible)
Definition: Figure.cc:825
Definition: Object.h:47
T * qWidget(void)
Definition: Object.h:81
static Object * parentObject(octave::interpreter &interp, const graphics_object &go)
Definition: Object.cc:200
static Object * fromQObject(QObject *obj)
Definition: Object.cc:213
virtual QObject * qObject(void)
Definition: Object.h:78
virtual void update(int pId)
Definition: Object.cc:163
~ToolBar(void)
Definition: ToolBar.cc:112
QAction * m_empty
Definition: ToolBar.h:66
void update(int pId)
Definition: ToolBar.cc:116
bool eventFilter(QObject *watched, QEvent *event)
Definition: ToolBar.cc:135
void beingDeleted(void)
Definition: ToolBar.cc:178
Figure * m_figure
Definition: ToolBar.h:67
ToolBar(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go, QToolBar *bar)
Definition: ToolBar.cc:89
void hideEmpty(void)
Definition: ToolBar.cc:172
static ToolBar * create(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go)
Definition: ToolBar.cc:72
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
const QString global_toolbar_style("QToolBar {" "margin-top: 0px;" "margin-bottom: 0px;" "padding-top: 0px;" "padding-bottom: 0px;" "border-top: 0px;" "border-bottom: 0px;" "}")
T::properties & properties(graphics_object obj)