GNU Octave  4.4.1
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-2018 Michael Goffioul
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 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <QAction>
28 #include <QActionEvent>
29 #include <QApplication>
30 #include <QEvent>
31 #include <QIcon>
32 #include <QMainWindow>
33 #include <QPixmap>
34 #include <QTimer>
35 #include <QToolBar>
36 
37 #include "Figure.h"
38 #include "ToolBar.h"
39 #include "QtHandlesUtils.h"
40 
41 namespace QtHandles
42 {
43 
44  static QAction*
45  addEmptyAction (QToolBar *bar)
46  {
47  static QIcon _empty;
48 
49  if (_empty.isNull ())
50  {
51  QPixmap pix (16, 16);
52 
53  pix.fill (Qt::transparent);
54 
55  _empty = QIcon (pix);
56  }
57 
58  QAction *a = bar->addAction (_empty, "Empty Toolbar");
59 
60  a->setEnabled (false);
61  a->setToolTip ("");
62 
63  return a;
64  }
65 
66  ToolBar*
68  {
69  Object *parent = Object::parentObject (go);
70 
71  if (parent)
72  {
73  QWidget *parentWidget = parent->qWidget<QWidget> ();
74 
75  if (parentWidget)
76  return new ToolBar (go, new QToolBar (parentWidget));
77  }
78 
79  return nullptr;
80  }
81 
82  ToolBar::ToolBar (const graphics_object& go, QToolBar *bar)
83  : Object (go, bar), m_empty (nullptr), m_figure (nullptr)
84  {
85  uitoolbar::properties& tp = properties<uitoolbar> ();
86 
87  bar->setFloatable (false);
88  bar->setMovable (false);
89  bar->setVisible (tp.is_visible ());
90 
91  m_empty = addEmptyAction (bar);
92 
93  m_figure =
94  dynamic_cast<Figure *> (Object::fromQObject (bar->parentWidget ()));
95 
96  if (m_figure)
97  m_figure->addCustomToolBar (bar, tp.is_visible ());
98 
99  bar->installEventFilter (this);
100  }
101 
103  { }
104 
105  void
106  ToolBar::update (int pId)
107  {
108  uitoolbar::properties& tp = properties<uitoolbar> ();
109  QToolBar *bar = qWidget<QToolBar> ();
110 
111  switch (pId)
112  {
113  case base_properties::ID_VISIBLE:
114  if (m_figure)
115  m_figure->showCustomToolBar (bar, tp.is_visible ());
116  break;
117 
118  default:
119  Object::update (pId);
120  break;
121  }
122  }
123 
124  bool
125  ToolBar::eventFilter (QObject *watched, QEvent *xevent)
126  {
127  if (watched == qObject ())
128  {
129  switch (xevent->type ())
130  {
131  case QEvent::ActionAdded:
132  case QEvent::ActionRemoved:
133  {
134  QActionEvent *ae = dynamic_cast<QActionEvent *> (xevent);
135  QToolBar *bar = qWidget<QToolBar> ();
136 
137  if (ae->action () != m_empty)
138  {
139  if (xevent->type () == QEvent::ActionAdded)
140  {
141  if (bar->actions ().size () == 2)
142  QTimer::singleShot (0, this, SLOT (hideEmpty (void)));
143  }
144  else
145  {
146  if (bar->actions ().size () == 1)
147  m_empty->setVisible (true);
148  }
149  }
150  }
151  break;
152 
153  default:
154  break;
155  }
156  }
157 
158  return false;
159  }
160 
161  void
163  {
164  m_empty->setVisible (false);
165  }
166 
167  void
169  {
170  if (m_figure)
171  {
172  QToolBar *bar = qWidget<QToolBar> ();
173 
174  if (bar)
175  m_figure->showCustomToolBar (bar, false);
176  }
177  }
178 
179 }
static ToolBar * create(const graphics_object &go)
Definition: ToolBar.cc:67
QAction * m_empty
Definition: ToolBar.h:58
void showCustomToolBar(QToolBar *bar, bool visible)
Definition: Figure.cc:1025
Figure * m_figure
Definition: ToolBar.h:59
static Object * parentObject(const graphics_object &go)
Definition: Object.cc:165
void beingDeleted(void)
Definition: ToolBar.cc:168
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
Definition: cellfun.cc:400
static QAction * addEmptyAction(QToolBar *bar)
Definition: ToolBar.cc:45
void addCustomToolBar(QToolBar *bar, bool visible)
Definition: Figure.cc:999
T * qWidget(void)
Definition: Object.h:74
static Object * fromQObject(QObject *obj)
Definition: Object.cc:176
bool eventFilter(QObject *watched, QEvent *event)
Definition: ToolBar.cc:125
virtual QObject * qObject(void)
Definition: Object.h:71
void update(int pId)
Definition: ToolBar.cc:106
void hideEmpty(void)
Definition: ToolBar.cc:162
virtual void update(int pId)
Definition: Object.cc:132
ToolBar(const graphics_object &go, QToolBar *bar)
Definition: ToolBar.cc:82