GNU Octave  4.0.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
ToolBar.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2015 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef 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 0;
80 }
81 
82 ToolBar::ToolBar (const graphics_object& go, QToolBar* bar)
83  : Object (go, bar), m_empty (0), m_figure (0)
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 
106 void
108 {
109  uitoolbar::properties& tp = properties<uitoolbar> ();
110  QToolBar* bar = qWidget<QToolBar> ();
111 
112  switch (pId)
113  {
115  if (m_figure)
116  m_figure->showCustomToolBar (bar, tp.is_visible ());
117  break;
118 
119  default:
120  Object::update (pId);
121  break;
122  }
123 }
124 
125 bool
126 ToolBar::eventFilter (QObject* watched, QEvent* xevent)
127 {
128  if (watched == qObject ())
129  {
130  switch (xevent->type ())
131  {
132  case QEvent::ActionAdded:
133  case QEvent::ActionRemoved:
134  {
135  QActionEvent* ae = dynamic_cast<QActionEvent*> (xevent);
136  QToolBar* bar = qWidget<QToolBar> ();
137 
138  if (ae->action () != m_empty)
139  {
140  if (xevent->type () == QEvent::ActionAdded)
141  {
142  if (bar->actions ().size () == 2)
143  QTimer::singleShot (0, this, SLOT (hideEmpty (void)));
144  }
145  else
146  {
147  if (bar->actions ().size () == 1)
148  m_empty->setVisible (true);
149  }
150  }
151  }
152  break;
153 
154  default:
155  break;
156  }
157  }
158 
159  return false;
160 }
161 
162 void
164 {
165  m_empty->setVisible (false);
166 }
167 
168 void
170 {
171  if (m_figure)
172  {
173  QToolBar* bar = qWidget<QToolBar> ();
174 
175  if (bar)
176  m_figure->showCustomToolBar (bar, false);
177  }
178 }
179 
180 }; // namespace QtHandles
static ToolBar * create(const graphics_object &go)
Definition: ToolBar.cc:67
bool is_visible(void) const
Definition: graphics.h:2758
QAction * m_empty
Definition: ToolBar.h:58
void showCustomToolBar(QToolBar *bar, bool visible)
Definition: Figure.cc:902
Figure * m_figure
Definition: ToolBar.h:59
static Object * parentObject(const graphics_object &go)
Definition: Object.cc:170
void beingDeleted(void)
Definition: ToolBar.cc:169
static QAction * addEmptyAction(QToolBar *bar)
Definition: ToolBar.cc:45
void addCustomToolBar(QToolBar *bar, bool visible)
Definition: Figure.cc:876
T * qWidget(void)
Definition: Object.h:74
static Object * fromQObject(QObject *obj)
Definition: Object.cc:181
bool eventFilter(QObject *watched, QEvent *event)
Definition: ToolBar.cc:126
virtual QObject * qObject(void)
Definition: Object.h:71
void update(int pId)
Definition: ToolBar.cc:107
void hideEmpty(void)
Definition: ToolBar.cc:163
virtual void update(int pId)
Definition: Object.cc:133
ToolBar(const graphics_object &go, QToolBar *bar)
Definition: ToolBar.cc:82