GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ToolBarButton.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// This file should not include config.h. It is only included in other
27// C++ source files that should have included config.h before including
28// this file.
29
30#include <QAction>
31#include <QIcon>
32#include <QWidget>
33
34#include "ToolBarButton.h"
35#include "QtHandlesUtils.h"
36#include "octave-qobject.h"
37
38namespace octave
39{
40 template <typename T>
42 octave::interpreter& interp,
43 const graphics_object& go, QAction *action)
44 : Object (oct_qobj, interp, go, action), m_separator (nullptr)
45 {
46 typename T::properties& tp = properties<T> ();
47
48 action->setToolTip (Utils::fromStdString (tp.get_tooltipstring ()));
49 action->setVisible (tp.is_visible ());
50
51 // Get the icon data from cdata or as a named icon
52 QImage img = Utils::makeImageFromCData (tp.get_cdata (), 24, 24);
53
54 if (img.width () == 0)
55 {
56 QIcon ico;
57 std::string name = tp.get___named_icon__ ();
58 if (! name.empty ())
59 ico = get_icon (name);
60
61 action->setIcon (ico);
62 }
63 else
64 action->setIcon (QIcon (QPixmap::fromImage (img)));
65
66 if (tp.is_separator ())
67 {
68 m_separator = new QAction (action);
69 m_separator->setSeparator (true);
70 m_separator->setVisible (tp.is_visible ());
71 }
72 action->setEnabled (tp.is_enable ());
73
74 QWidget *w = qobject_cast<QWidget *> (action->parent ());
75
76 w->insertAction (w->actions ().back (), action);
77 if (m_separator)
78 w->insertAction (action, m_separator);
79 }
80
81 template <typename T>
83 { }
84
85 template <typename T>
86 void
88 {
89 typename T::properties& tp = properties<T> ();
90 QAction *action = qWidget<QAction> ();
91
92 switch (pId)
93 {
94 case base_properties::ID_VISIBLE:
95 action->setVisible (tp.is_visible ());
96 if (m_separator)
97 m_separator->setVisible (tp.is_visible ());
98 break;
99
100 case T::properties::ID_TOOLTIPSTRING:
101 action->setToolTip (Utils::fromStdString (tp.get_tooltipstring ()));
102 break;
103
104 case T::properties::ID_CDATA:
105 {
106 // Get the icon data from cdata or as a named icon
107 QImage img = Utils::makeImageFromCData (tp.get_cdata (), 24, 24);
108
109 if (img.width () == 0)
110 {
111 QIcon ico;
112 std::string name = tp.get___named_icon__ ();
113 if (! name.empty ())
114 ico = get_icon (name);
115
116 action->setIcon (ico);
117 }
118 else
119 action->setIcon (QIcon (QPixmap::fromImage (img)));
120 }
121 break;
122
123 case T::properties::ID_SEPARATOR:
124 if (tp.is_separator ())
125 {
126 if (! m_separator)
127 {
128 m_separator = new QAction (action);
129 m_separator->setSeparator (true);
130 m_separator->setVisible (tp.is_visible ());
131
132 QWidget *w = qobject_cast<QWidget *> (action->parent ());
133
134 w->insertAction (action, m_separator);
135 }
136 }
137 else
138 {
139 if (m_separator)
140 delete m_separator;
141 m_separator = nullptr;
142 }
143 break;
144
145 case T::properties::ID_ENABLE:
146 action->setEnabled (tp.is_enable ());
147 break;
148
149 default:
150 Object::update (pId);
151 break;
152 }
153 }
154
155 template <typename T>
156 QIcon ToolBarButton<T>::get_icon (const std::string& name)
157 {
158 octave::resource_manager& rmgr = m_octave_qobj.get_resource_manager ();
159
160 return rmgr.icon (QString::fromStdString (name));
161 }
162
163}
virtual void update(int pId)
Definition: Object.cc:164
void update(int pId)
ToolBarButton(octave::base_qobject &oct_qobj, octave::interpreter &interp, const graphics_object &go, QAction *action)
QIcon get_icon(const std::string &name)
Base class for Octave interfaces that use Qt.
QIcon icon(const QString &icon_name, bool fallback=true)
QString name
std::complex< double > w(std::complex< double > z, double relerr=0)
QImage makeImageFromCData(const octave_value &v, int width, int height)
QString fromStdString(const std::string &s)
T::properties & properties(graphics_object obj)