GNU Octave 7.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-2022 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
45#include "octave-qobject.h"
46
47namespace octave
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
101 m_empty = addEmptyAction (bar);
102
103 m_figure =
104 dynamic_cast<Figure *> (Object::fromQObject (bar->parentWidget ()));
105
106 if (m_figure)
107 m_figure->addCustomToolBar (bar, tp.is_visible (),
108 tp.get_tag () == "__default_toolbar__");
109
110 bar->installEventFilter (this);
111 }
112
114 { }
115
116 void
118 {
119 uitoolbar::properties& tp = properties<uitoolbar> ();
120 QToolBar *bar = qWidget<QToolBar> ();
121
122 switch (pId)
123 {
124 case base_properties::ID_VISIBLE:
125 if (m_figure)
126 m_figure->showCustomToolBar (bar, tp.is_visible ());
127 break;
128
129 default:
130 Object::update (pId);
131 break;
132 }
133 }
134
135 bool
136 ToolBar::eventFilter (QObject *watched, QEvent *xevent)
137 {
138 if (watched == qObject ())
139 {
140 switch (xevent->type ())
141 {
142 case QEvent::ActionAdded:
143 case QEvent::ActionRemoved:
144 {
145 QActionEvent *ae = dynamic_cast<QActionEvent *> (xevent);
146 QToolBar *bar = qWidget<QToolBar> ();
147
148 if (ae->action () != m_empty)
149 {
150 if (xevent->type () == QEvent::ActionAdded)
151 {
152 if (bar->actions ().size () == 2)
153 QTimer::singleShot (0, this, &ToolBar::hideEmpty);
154 }
155 else
156 {
157 if (bar->actions ().size () == 1)
158 m_empty->setVisible (true);
159 }
160 }
161 }
162 break;
163
164 default:
165 break;
166 }
167 }
168
169 return false;
170 }
171
172 void
174 {
175 m_empty->setVisible (false);
176 }
177
178 void
180 {
181 if (m_figure)
182 {
183 QToolBar *bar = qWidget<QToolBar> ();
184
185 if (bar)
186 m_figure->showCustomToolBar (bar, false);
187 }
188 }
189
190}
void showCustomToolBar(QToolBar *bar, bool visible)
Definition: Figure.cc:827
void addCustomToolBar(QToolBar *bar, bool visible, bool isdefault)
Definition: Figure.cc:800
T * qWidget(void)
Definition: Object.h:85
virtual void update(int pId)
Definition: Object.cc:164
static Object * fromQObject(QObject *obj)
Definition: Object.cc:214
virtual QObject * qObject(void)
Definition: Object.h:82
static Object * parentObject(octave::interpreter &interp, const graphics_object &go)
Definition: Object.cc:201
~ToolBar(void)
Definition: ToolBar.cc:113
bool eventFilter(QObject *watched, QEvent *event)
Definition: ToolBar.cc:136
void beingDeleted(void)
Definition: ToolBar.cc:179
void hideEmpty(void)
Definition: ToolBar.cc:173
void update(int pId)
Definition: ToolBar.cc:117
ToolBar(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go, QToolBar *bar)
Definition: ToolBar.cc:89
Figure * m_figure
Definition: ToolBar.h:71
QAction * m_empty
Definition: ToolBar.h:70
static ToolBar * create(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go)
Definition: ToolBar.cc:72
Base class for Octave interfaces that use Qt.
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)
static QAction * addEmptyAction(QToolBar *bar)
Definition: ToolBar.cc:59
static QIcon makeEmptyIcon(void)
Definition: ToolBar.cc:49